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/config.h>
48 #include <linux/version.h>
49 #include <linux/init.h>
50 #include <linux/module.h>
51 #include <linux/delay.h>
52 #include <linux/slab.h>
53 #include <linux/pci.h>
54 #include <linux/vmalloc.h>
55 #include <linux/wait.h>
56 #include <linux/byteorder/generic.h>
58 #include <linux/interrupt.h>
59 #include <linux/i2c.h>
60 #include <linux/i2c-algo-bit.h>
62 #include <linux/spinlock.h>
63 #define MAP_NR(x) virt_to_page(x)
64 #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
65 #define ZORAN_VID_TYPE ( \
71 VID_TYPE_MJPEG_DECODER | \
72 VID_TYPE_MJPEG_ENCODER \
75 #include <linux/videodev.h>
76 #include <media/v4l2-common.h>
77 #include "videocodec.h"
80 #include <asm/uaccess.h>
81 #include <linux/proc_fs.h>
83 #include <linux/video_decoder.h>
84 #include <linux/video_encoder.h>
85 #include <linux/mutex.h>
87 #include "zoran_device.h"
88 #include "zoran_card.h"
91 /* we declare some card type definitions here, they mean
92 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
93 #define ZORAN_V4L2_VID_FLAGS ( \
95 V4L2_CAP_VIDEO_CAPTURE |\
96 V4L2_CAP_VIDEO_OUTPUT |\
97 V4L2_CAP_VIDEO_OVERLAY \
101 #include <asm/byteorder.h>
103 const struct zoran_format zoran_formats[] = {
105 .name = "15-bit RGB",
106 .palette = VIDEO_PALETTE_RGB555,
108 #ifdef __LITTLE_ENDIAN
109 .fourcc = V4L2_PIX_FMT_RGB555,
111 .fourcc = V4L2_PIX_FMT_RGB555X,
113 .colorspace = V4L2_COLORSPACE_SRGB,
116 .flags = ZORAN_FORMAT_CAPTURE |
117 ZORAN_FORMAT_OVERLAY,
119 .name = "16-bit RGB",
120 .palette = VIDEO_PALETTE_RGB565,
122 #ifdef __LITTLE_ENDIAN
123 .fourcc = V4L2_PIX_FMT_RGB565,
125 .fourcc = V4L2_PIX_FMT_RGB565X,
127 .colorspace = V4L2_COLORSPACE_SRGB,
130 .flags = ZORAN_FORMAT_CAPTURE |
131 ZORAN_FORMAT_OVERLAY,
133 .name = "24-bit RGB",
134 .palette = VIDEO_PALETTE_RGB24,
136 #ifdef __LITTLE_ENDIAN
137 .fourcc = V4L2_PIX_FMT_BGR24,
139 .fourcc = V4L2_PIX_FMT_RGB24,
141 .colorspace = V4L2_COLORSPACE_SRGB,
144 .flags = ZORAN_FORMAT_CAPTURE |
145 ZORAN_FORMAT_OVERLAY,
147 .name = "32-bit RGB",
148 .palette = VIDEO_PALETTE_RGB32,
150 #ifdef __LITTLE_ENDIAN
151 .fourcc = V4L2_PIX_FMT_BGR32,
153 .fourcc = V4L2_PIX_FMT_RGB32,
155 .colorspace = V4L2_COLORSPACE_SRGB,
158 .flags = ZORAN_FORMAT_CAPTURE |
159 ZORAN_FORMAT_OVERLAY,
161 .name = "4:2:2, packed, YUYV",
162 .palette = VIDEO_PALETTE_YUV422,
164 .fourcc = V4L2_PIX_FMT_YUYV,
165 .colorspace = V4L2_COLORSPACE_SMPTE170M,
168 .flags = ZORAN_FORMAT_CAPTURE |
169 ZORAN_FORMAT_OVERLAY,
171 .name = "Hardware-encoded Motion-JPEG",
174 .fourcc = V4L2_PIX_FMT_MJPEG,
175 .colorspace = V4L2_COLORSPACE_SMPTE170M,
178 .flags = ZORAN_FORMAT_CAPTURE |
179 ZORAN_FORMAT_PLAYBACK |
180 ZORAN_FORMAT_COMPRESSED,
183 static const int zoran_num_formats =
184 (sizeof(zoran_formats) / sizeof(struct zoran_format));
186 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
187 #if !defined(CONFIG_BIGPHYS_AREA)
188 //#undef CONFIG_BIGPHYS_AREA
189 #define BUZ_USE_HIMEM
192 #if defined(CONFIG_BIGPHYS_AREA)
193 # include <linux/bigphysarea.h>
196 extern int *zr_debug;
198 #define dprintk(num, format, args...) \
200 if (*zr_debug >= num) \
201 printk(format, ##args); \
204 extern int v4l_nbufs;
205 extern int v4l_bufsize;
206 extern int jpg_nbufs;
207 extern int jpg_bufsize;
208 extern int pass_through;
210 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
211 module_param(lock_norm, int, 0);
212 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
215 /* small helper function for calculating buffersizes for v4l2
216 * we calculate the nearest higher power-of-two, which
217 * will be the recommended buffersize */
219 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
221 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
222 __u32 num = (1024 * 512) / (div);
231 if (result > jpg_bufsize)
239 /* forward references */
240 static void v4l_fbuffer_free(struct file *file);
241 static void jpg_fbuffer_free(struct file *file);
244 * Allocate the V4L grab buffers
246 * These have to be pysically contiguous.
247 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
248 * else we try to allocate them with bigphysarea_alloc_pages
249 * if the bigphysarea patch is present in the kernel,
250 * else we try to use high memory (if the user has bootet
251 * Linux with the necessary memory left over).
254 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
256 get_high_mem (unsigned long size)
259 * Check if there is usable memory at the end of Linux memory
260 * of at least size. Return the physical address of this memory,
261 * return 0 on failure.
263 * The idea is from Alexandro Rubini's book "Linux device drivers".
264 * The driver from him which is downloadable from O'Reilly's
265 * web site misses the "virt_to_phys(high_memory)" part
266 * (and therefore doesn't work at all - at least with 2.2.x kernels).
268 * It should be unnecessary to mention that THIS IS DANGEROUS,
269 * if more than one driver at a time has the idea to use this memory!!!!
272 volatile unsigned char __iomem *mem;
274 unsigned long hi_mem_ph;
277 /* Map the high memory to user space */
279 hi_mem_ph = virt_to_phys(high_memory);
281 mem = ioremap(hi_mem_ph, size);
284 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
289 for (i = 0; i < size; i++) {
290 /* Check if it is memory */
293 if (readb(mem + i) != c)
297 if (readb(mem + i) != c)
299 writeb(0, mem + i); /* zero out memory */
301 /* give the kernel air to breath */
302 if ((i & 0x3ffff) == 0x3ffff)
311 "%s: get_high_mem() - requested %lu, avail %lu\n",
312 ZORAN_NAME, size, i);
321 v4l_fbuffer_alloc (struct file *file)
323 struct zoran_fh *fh = file->private_data;
324 struct zoran *zr = fh->zr;
327 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
328 unsigned long pmem = 0;
331 /* we might have old buffers lying around... */
332 if (fh->v4l_buffers.ready_to_be_freed) {
333 v4l_fbuffer_free(file);
336 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
337 if (fh->v4l_buffers.buffer[i].fbuffer)
340 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
344 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
348 (unsigned char *) kmalloc(fh->v4l_buffers.
354 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
356 v4l_fbuffer_free(file);
359 fh->v4l_buffers.buffer[i].fbuffer = mem;
360 fh->v4l_buffers.buffer[i].fbuffer_phys =
362 fh->v4l_buffers.buffer[i].fbuffer_bus =
364 for (off = 0; off < fh->v4l_buffers.buffer_size;
366 SetPageReserved(MAP_NR(mem + off));
369 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
370 ZR_DEVNAME(zr), i, (unsigned long) mem,
373 #if defined(CONFIG_BIGPHYS_AREA)
374 /* Use bigphysarea_alloc_pages */
377 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
381 (unsigned char *) bigphysarea_alloc_pages(n, 0,
386 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
388 v4l_fbuffer_free(file);
391 fh->v4l_buffers.buffer[i].fbuffer = mem;
392 fh->v4l_buffers.buffer[i].fbuffer_phys =
394 fh->v4l_buffers.buffer[i].fbuffer_bus =
398 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
399 ZR_DEVNAME(zr), i, (unsigned) mem,
400 (unsigned) virt_to_bus(mem));
402 /* Zero out the allocated memory */
403 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
404 fh->v4l_buffers.buffer_size);
405 #elif defined(BUZ_USE_HIMEM)
407 /* Use high memory which has been left at boot time */
409 /* Ok., Ok. this is an evil hack - we make
410 * the assumption that physical addresses are
411 * the same as bus addresses (true at least
412 * for Intel processors). The whole method of
413 * obtaining and using this memory is not very
414 * nice - but I hope it saves some poor users
415 * from kernel hacking, which might have even
416 * more evil results */
420 fh->v4l_buffers.num_buffers *
421 fh->v4l_buffers.buffer_size;
423 pmem = get_high_mem(size);
427 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
428 ZR_DEVNAME(zr), size >> 10);
431 fh->v4l_buffers.buffer[0].fbuffer = NULL;
432 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
433 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
436 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
437 ZR_DEVNAME(zr), size >> 10);
439 fh->v4l_buffers.buffer[i].fbuffer = NULL;
440 fh->v4l_buffers.buffer[i].fbuffer_phys =
441 pmem + i * fh->v4l_buffers.buffer_size;
442 fh->v4l_buffers.buffer[i].fbuffer_bus =
443 pmem + i * fh->v4l_buffers.buffer_size;
446 /* No bigphysarea present, usage of high memory disabled,
447 * but user wants buffers of more than MAX_KMALLOC_MEM */
450 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
454 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
455 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
456 fh->v4l_buffers.buffer_size >> 10);
462 fh->v4l_buffers.allocated = 1;
467 /* free the V4L grab buffers */
469 v4l_fbuffer_free (struct file *file)
471 struct zoran_fh *fh = file->private_data;
472 struct zoran *zr = fh->zr;
476 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
478 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
479 if (!fh->v4l_buffers.buffer[i].fbuffer)
482 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
483 mem = fh->v4l_buffers.buffer[i].fbuffer;
484 for (off = 0; off < fh->v4l_buffers.buffer_size;
486 ClearPageReserved(MAP_NR(mem + off));
487 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
489 #if defined(CONFIG_BIGPHYS_AREA)
491 bigphysarea_free_pages((void *) fh->v4l_buffers.
494 fh->v4l_buffers.buffer[i].fbuffer = NULL;
497 fh->v4l_buffers.allocated = 0;
498 fh->v4l_buffers.ready_to_be_freed = 0;
502 * Allocate the MJPEG grab buffers.
504 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
505 * kmalloc is used to request a physically contiguous area,
506 * else we allocate the memory in framgents with get_zeroed_page.
508 * If a Natoma chipset is present and this is a revision 1 zr36057,
509 * each MJPEG buffer needs to be physically contiguous.
510 * (RJ: This statement is from Dave Perks' original driver,
511 * I could never check it because I have a zr36067)
512 * The driver cares about this because it reduces the buffer
513 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
515 * RJ: The contents grab buffers needs never be accessed in the driver.
516 * Therefore there is no need to allocate them with vmalloc in order
517 * to get a contiguous virtual memory space.
518 * I don't understand why many other drivers first allocate them with
519 * vmalloc (which uses internally also get_zeroed_page, but delivers you
520 * virtual addresses) and then again have to make a lot of efforts
521 * to get the physical address.
524 * On big-endian architectures (such as ppc) some extra steps
525 * are needed. When reading and writing to the stat_com array
526 * and fragment buffers, the device expects to see little-
527 * endian values. The use of cpu_to_le32() and le32_to_cpu()
528 * in this function (and one or two others in zoran_device.c)
529 * ensure that these values are always stored in little-endian
530 * form, regardless of architecture. The zr36057 does Very Bad
531 * Things on big endian architectures if the stat_com array
532 * and fragment buffers are not little-endian.
536 jpg_fbuffer_alloc (struct file *file)
538 struct zoran_fh *fh = file->private_data;
539 struct zoran *zr = fh->zr;
543 /* we might have old buffers lying around */
544 if (fh->jpg_buffers.ready_to_be_freed) {
545 jpg_fbuffer_free(file);
548 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
549 if (fh->jpg_buffers.buffer[i].frag_tab)
552 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
555 /* Allocate fragment table for this buffer */
557 mem = get_zeroed_page(GFP_KERNEL);
561 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
563 jpg_fbuffer_free(file);
566 memset((void *) mem, 0, PAGE_SIZE);
567 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
568 fh->jpg_buffers.buffer[i].frag_tab_bus =
569 virt_to_bus((void *) mem);
571 //if (alloc_contig) {
572 if (fh->jpg_buffers.need_contiguous) {
574 (unsigned long) kmalloc(fh->jpg_buffers.
580 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
582 jpg_fbuffer_free(file);
585 fh->jpg_buffers.buffer[i].frag_tab[0] =
586 cpu_to_le32(virt_to_bus((void *) mem));
587 fh->jpg_buffers.buffer[i].frag_tab[1] =
588 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
589 for (off = 0; off < fh->jpg_buffers.buffer_size;
591 SetPageReserved(MAP_NR(mem + off));
593 /* jpg_bufsize is allreay page aligned */
595 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
597 mem = get_zeroed_page(GFP_KERNEL);
601 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
603 jpg_fbuffer_free(file);
607 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
608 cpu_to_le32(virt_to_bus((void *) mem));
609 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
611 cpu_to_le32((PAGE_SIZE / 4) << 1);
612 SetPageReserved(MAP_NR(mem));
615 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
620 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
622 (fh->jpg_buffers.num_buffers *
623 fh->jpg_buffers.buffer_size) >> 10);
625 fh->jpg_buffers.allocated = 1;
630 /* free the MJPEG grab buffers */
632 jpg_fbuffer_free (struct file *file)
634 struct zoran_fh *fh = file->private_data;
635 struct zoran *zr = fh->zr;
639 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
641 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
642 if (!fh->jpg_buffers.buffer[i].frag_tab)
645 //if (alloc_contig) {
646 if (fh->jpg_buffers.need_contiguous) {
647 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
648 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
649 fh->jpg_buffers.buffer[i].frag_tab[0]));
651 off < fh->jpg_buffers.buffer_size;
653 ClearPageReserved(MAP_NR
656 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
657 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
661 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
663 if (!fh->jpg_buffers.buffer[i].
666 ClearPageReserved(MAP_NR
670 buffer[i].frag_tab[2 *
672 free_page((unsigned long)
678 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
680 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
685 free_page((unsigned long) fh->jpg_buffers.buffer[i].
687 fh->jpg_buffers.buffer[i].frag_tab = NULL;
690 fh->jpg_buffers.allocated = 0;
691 fh->jpg_buffers.ready_to_be_freed = 0;
695 * V4L Buffer grabbing
699 zoran_v4l_set_format (struct file *file,
702 const struct zoran_format *format)
704 struct zoran_fh *fh = file->private_data;
705 struct zoran *zr = fh->zr;
708 /* Check size and format of the grab wanted */
710 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
711 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
714 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
715 ZR_DEVNAME(zr), width, height);
719 bpp = (format->depth + 7) / 8;
721 /* Check against available buffer size */
722 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
725 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
726 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
730 /* The video front end needs 4-byte alinged line sizes */
732 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
735 "%s: v4l_set_format() - wrong frame alingment\n",
740 fh->v4l_settings.width = width;
741 fh->v4l_settings.height = height;
742 fh->v4l_settings.format = format;
743 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
749 zoran_v4l_queue_frame (struct file *file,
752 struct zoran_fh *fh = file->private_data;
753 struct zoran *zr = fh->zr;
757 if (!fh->v4l_buffers.allocated) {
760 "%s: v4l_queue_frame() - buffers not yet allocated\n",
765 /* No grabbing outside the buffer range! */
766 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
769 "%s: v4l_queue_frame() - buffer %d is out of range\n",
770 ZR_DEVNAME(zr), num);
774 spin_lock_irqsave(&zr->spinlock, flags);
776 if (fh->v4l_buffers.active == ZORAN_FREE) {
777 if (zr->v4l_buffers.active == ZORAN_FREE) {
778 zr->v4l_buffers = fh->v4l_buffers;
779 fh->v4l_buffers.active = ZORAN_ACTIVE;
783 "%s: v4l_queue_frame() - another session is already capturing\n",
789 /* make sure a grab isn't going on currently with this buffer */
791 switch (zr->v4l_buffers.buffer[num].state) {
794 if (zr->v4l_buffers.active == ZORAN_FREE) {
795 fh->v4l_buffers.active = ZORAN_FREE;
796 zr->v4l_buffers.allocated = 0;
798 res = -EBUSY; /* what are you doing? */
803 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
804 ZR_DEVNAME(zr), num);
806 /* since there is at least one unused buffer there's room for at least
807 * one more pend[] entry */
808 zr->v4l_pend[zr->v4l_pend_head++ &
809 V4L_MASK_FRAME] = num;
810 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
811 zr->v4l_buffers.buffer[num].bs.length =
812 fh->v4l_settings.bytesperline *
813 zr->v4l_settings.height;
814 fh->v4l_buffers.buffer[num] =
815 zr->v4l_buffers.buffer[num];
820 spin_unlock_irqrestore(&zr->spinlock, flags);
822 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
823 zr->v4l_buffers.active = fh->v4l_buffers.active;
829 v4l_grab (struct file *file,
830 struct video_mmap *mp)
832 struct zoran_fh *fh = file->private_data;
833 struct zoran *zr = fh->zr;
836 for (i = 0; i < zoran_num_formats; i++) {
837 if (zoran_formats[i].palette == mp->format &&
838 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
839 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
842 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
845 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
851 * To minimize the time spent in the IRQ routine, we avoid setting up
852 * the video front end there.
853 * If this grab has different parameters from a running streaming capture
854 * we stop the streaming capture and start it over again.
856 if (zr->v4l_memgrab_active &&
857 (zr->v4l_settings.width != mp->width ||
858 zr->v4l_settings.height != mp->height ||
859 zr->v4l_settings.format->palette != mp->format)) {
860 res = wait_grab_pending(zr);
864 if ((res = zoran_v4l_set_format(file,
869 zr->v4l_settings = fh->v4l_settings;
871 /* queue the frame in the pending queue */
872 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
873 fh->v4l_buffers.active = ZORAN_FREE;
877 /* put the 36057 into frame grabbing mode */
878 if (!res && !zr->v4l_memgrab_active)
879 zr36057_set_memgrab(zr, 1);
881 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
887 * Sync on a V4L buffer
891 v4l_sync (struct file *file,
894 struct zoran_fh *fh = file->private_data;
895 struct zoran *zr = fh->zr;
898 if (fh->v4l_buffers.active == ZORAN_FREE) {
901 "%s: v4l_sync() - no grab active for this session\n",
906 /* check passed-in frame number */
907 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
909 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
910 ZR_DEVNAME(zr), frame);
914 /* Check if is buffer was queued at all */
915 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
918 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
923 /* wait on this buffer to get ready */
924 if (!wait_event_interruptible_timeout(zr->v4l_capq,
925 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
928 if (signal_pending(current))
931 /* buffer should now be in BUZ_STATE_DONE */
932 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
934 KERN_ERR "%s: v4l_sync() - internal state error\n",
937 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
938 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
940 spin_lock_irqsave(&zr->spinlock, flags);
942 /* Check if streaming capture has finished */
943 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
944 zr36057_set_memgrab(zr, 0);
945 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
946 fh->v4l_buffers.active = zr->v4l_buffers.active =
948 zr->v4l_buffers.allocated = 0;
952 spin_unlock_irqrestore(&zr->spinlock, flags);
958 * Queue a MJPEG buffer for capture/playback
962 zoran_jpg_queue_frame (struct file *file,
964 enum zoran_codec_mode mode)
966 struct zoran_fh *fh = file->private_data;
967 struct zoran *zr = fh->zr;
971 /* Check if buffers are allocated */
972 if (!fh->jpg_buffers.allocated) {
975 "%s: jpg_queue_frame() - buffers not yet allocated\n",
980 /* No grabbing outside the buffer range! */
981 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
984 "%s: jpg_queue_frame() - buffer %d out of range\n",
985 ZR_DEVNAME(zr), num);
989 /* what is the codec mode right now? */
990 if (zr->codec_mode == BUZ_MODE_IDLE) {
991 zr->jpg_settings = fh->jpg_settings;
992 } else if (zr->codec_mode != mode) {
993 /* wrong codec mode active - invalid */
996 "%s: jpg_queue_frame() - codec in wrong mode\n",
1001 if (fh->jpg_buffers.active == ZORAN_FREE) {
1002 if (zr->jpg_buffers.active == ZORAN_FREE) {
1003 zr->jpg_buffers = fh->jpg_buffers;
1004 fh->jpg_buffers.active = ZORAN_ACTIVE;
1008 "%s: jpg_queue_frame() - another session is already capturing\n",
1014 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1015 /* Ok load up the jpeg codec */
1016 zr36057_enable_jpg(zr, mode);
1019 spin_lock_irqsave(&zr->spinlock, flags);
1022 switch (zr->jpg_buffers.buffer[num].state) {
1023 case BUZ_STATE_DONE:
1026 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1028 case BUZ_STATE_USER:
1029 /* since there is at least one unused buffer there's room for at
1030 *least one more pend[] entry */
1031 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1033 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1034 fh->jpg_buffers.buffer[num] =
1035 zr->jpg_buffers.buffer[num];
1036 zoran_feed_stat_com(zr);
1040 case BUZ_STATE_PEND:
1041 if (zr->jpg_buffers.active == ZORAN_FREE) {
1042 fh->jpg_buffers.active = ZORAN_FREE;
1043 zr->jpg_buffers.allocated = 0;
1045 res = -EBUSY; /* what are you doing? */
1050 spin_unlock_irqrestore(&zr->spinlock, flags);
1052 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1053 zr->jpg_buffers.active = fh->jpg_buffers.active;
1060 jpg_qbuf (struct file *file,
1062 enum zoran_codec_mode mode)
1064 struct zoran_fh *fh = file->private_data;
1065 struct zoran *zr = fh->zr;
1068 /* Does the user want to stop streaming? */
1070 if (zr->codec_mode == mode) {
1071 if (fh->jpg_buffers.active == ZORAN_FREE) {
1074 "%s: jpg_qbuf(-1) - session not active\n",
1078 fh->jpg_buffers.active = zr->jpg_buffers.active =
1080 zr->jpg_buffers.allocated = 0;
1081 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1086 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1092 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1095 /* Start the jpeg codec when the first frame is queued */
1096 if (!res && zr->jpg_que_head == 1)
1103 * Sync on a MJPEG buffer
1107 jpg_sync (struct file *file,
1108 struct zoran_sync *bs)
1110 struct zoran_fh *fh = file->private_data;
1111 struct zoran *zr = fh->zr;
1112 unsigned long flags;
1115 if (fh->jpg_buffers.active == ZORAN_FREE) {
1118 "%s: jpg_sync() - capture is not currently active\n",
1122 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1123 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1126 "%s: jpg_sync() - codec not in streaming mode\n",
1130 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1131 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1132 zr->jpg_dma_tail == zr->jpg_dma_head),
1136 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1138 zr->codec->control(zr->codec, CODEC_G_STATUS,
1142 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1143 ZR_DEVNAME(zr), isr);
1148 if (signal_pending(current))
1149 return -ERESTARTSYS;
1151 spin_lock_irqsave(&zr->spinlock, flags);
1153 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1154 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1156 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1158 /* buffer should now be in BUZ_STATE_DONE */
1160 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1163 "%s: jpg_sync() - internal state error\n",
1166 *bs = zr->jpg_buffers.buffer[frame].bs;
1168 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1169 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1171 spin_unlock_irqrestore(&zr->spinlock, flags);
1177 zoran_open_init_session (struct file *file)
1180 struct zoran_fh *fh = file->private_data;
1181 struct zoran *zr = fh->zr;
1183 /* Per default, map the V4L Buffers */
1184 fh->map_mode = ZORAN_MAP_MODE_RAW;
1186 /* take over the card's current settings */
1187 fh->overlay_settings = zr->overlay_settings;
1188 fh->overlay_settings.is_set = 0;
1189 fh->overlay_settings.format = zr->overlay_settings.format;
1190 fh->overlay_active = ZORAN_FREE;
1193 fh->v4l_settings = zr->v4l_settings;
1196 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1197 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1198 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1199 fh->v4l_buffers.buffer[i].bs.frame = i;
1201 fh->v4l_buffers.allocated = 0;
1202 fh->v4l_buffers.ready_to_be_freed = 0;
1203 fh->v4l_buffers.active = ZORAN_FREE;
1204 fh->v4l_buffers.buffer_size = v4l_bufsize;
1205 fh->v4l_buffers.num_buffers = v4l_nbufs;
1208 fh->jpg_settings = zr->jpg_settings;
1211 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1212 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1213 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1214 fh->jpg_buffers.buffer[i].bs.frame = i;
1216 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1217 fh->jpg_buffers.allocated = 0;
1218 fh->jpg_buffers.ready_to_be_freed = 0;
1219 fh->jpg_buffers.active = ZORAN_FREE;
1220 fh->jpg_buffers.buffer_size = jpg_bufsize;
1221 fh->jpg_buffers.num_buffers = jpg_nbufs;
1225 zoran_close_end_session (struct file *file)
1227 struct zoran_fh *fh = file->private_data;
1228 struct zoran *zr = fh->zr;
1231 if (fh->overlay_active != ZORAN_FREE) {
1232 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1233 zr->v4l_overlay_active = 0;
1234 if (!zr->v4l_memgrab_active)
1235 zr36057_overlay(zr, 0);
1236 zr->overlay_mask = NULL;
1240 if (fh->v4l_buffers.active != ZORAN_FREE) {
1241 zr36057_set_memgrab(zr, 0);
1242 zr->v4l_buffers.allocated = 0;
1243 zr->v4l_buffers.active = fh->v4l_buffers.active =
1248 if (fh->v4l_buffers.allocated ||
1249 fh->v4l_buffers.ready_to_be_freed) {
1250 v4l_fbuffer_free(file);
1254 if (fh->jpg_buffers.active != ZORAN_FREE) {
1255 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1256 zr->jpg_buffers.allocated = 0;
1257 zr->jpg_buffers.active = fh->jpg_buffers.active =
1262 if (fh->jpg_buffers.allocated ||
1263 fh->jpg_buffers.ready_to_be_freed) {
1264 jpg_fbuffer_free(file);
1269 * Open a zoran card. Right now the flags stuff is just playing
1273 zoran_open (struct inode *inode,
1276 unsigned int minor = iminor(inode);
1277 struct zoran *zr = NULL;
1278 struct zoran_fh *fh;
1279 int i, res, first_open = 0, have_module_locks = 0;
1281 /* find the device */
1282 for (i = 0; i < zoran_num; i++) {
1283 if (zoran[i].video_dev->minor == minor) {
1290 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1292 goto open_unlock_and_return;
1295 /* see fs/device.c - the kernel already locks during open(),
1296 * so locking ourselves only causes deadlocks */
1297 /*mutex_lock(&zr->resource_lock);*/
1301 KERN_ERR "%s: no TV decoder loaded for device!\n",
1304 goto open_unlock_and_return;
1307 /* try to grab a module lock */
1308 if (!try_module_get(THIS_MODULE)) {
1311 "%s: failed to acquire my own lock! PANIC!\n",
1314 goto open_unlock_and_return;
1316 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1319 "%s: failed to grab ownership of i2c decoder\n",
1322 module_put(THIS_MODULE);
1323 goto open_unlock_and_return;
1326 !try_module_get(zr->encoder->driver->driver.owner)) {
1329 "%s: failed to grab ownership of i2c encoder\n",
1332 module_put(zr->decoder->driver->driver.owner);
1333 module_put(THIS_MODULE);
1334 goto open_unlock_and_return;
1337 have_module_locks = 1;
1339 if (zr->user >= 2048) {
1340 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1341 ZR_DEVNAME(zr), zr->user);
1343 goto open_unlock_and_return;
1346 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1347 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1349 /* now, create the open()-specific file_ops struct */
1350 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1354 "%s: zoran_open() - allocation of zoran_fh failed\n",
1357 goto open_unlock_and_return;
1359 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1360 * on norm-change! */
1362 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1363 if (!fh->overlay_mask) {
1366 "%s: zoran_open() - allocation of overlay_mask failed\n",
1370 goto open_unlock_and_return;
1373 if (zr->user++ == 0)
1376 /*mutex_unlock(&zr->resource_lock);*/
1378 /* default setup - TODO: look at flags */
1379 if (first_open) { /* First device open */
1380 zr36057_restart(zr);
1381 zoran_open_init_params(zr);
1382 zoran_init_hardware(zr);
1384 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1387 /* set file_ops stuff */
1388 file->private_data = fh;
1390 zoran_open_init_session(file);
1394 open_unlock_and_return:
1395 /* if we grabbed locks, release them accordingly */
1396 if (have_module_locks) {
1397 module_put(zr->decoder->driver->driver.owner);
1399 module_put(zr->encoder->driver->driver.owner);
1401 module_put(THIS_MODULE);
1404 /* if there's no device found, we didn't obtain the lock either */
1406 /*mutex_unlock(&zr->resource_lock);*/
1413 zoran_close (struct inode *inode,
1416 struct zoran_fh *fh = file->private_data;
1417 struct zoran *zr = fh->zr;
1419 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1420 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1422 /* kernel locks (fs/device.c), so don't do that ourselves
1423 * (prevents deadlocks) */
1424 /*mutex_lock(&zr->resource_lock);*/
1426 zoran_close_end_session(file);
1428 if (zr->user-- == 1) { /* Last process */
1429 /* Clean up JPEG process */
1430 wake_up_interruptible(&zr->jpg_capq);
1431 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1432 zr->jpg_buffers.allocated = 0;
1433 zr->jpg_buffers.active = ZORAN_FREE;
1435 /* disable interrupts */
1436 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1439 print_interrupts(zr);
1442 zr->v4l_overlay_active = 0;
1443 zr36057_overlay(zr, 0);
1444 zr->overlay_mask = NULL;
1447 wake_up_interruptible(&zr->v4l_capq);
1448 zr36057_set_memgrab(zr, 0);
1449 zr->v4l_buffers.allocated = 0;
1450 zr->v4l_buffers.active = ZORAN_FREE;
1451 zoran_set_pci_master(zr, 0);
1453 if (!pass_through) { /* Switch to color bar */
1454 int zero = 0, two = 2;
1455 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1456 encoder_command(zr, ENCODER_SET_INPUT, &two);
1460 file->private_data = NULL;
1461 kfree(fh->overlay_mask);
1464 /* release locks on the i2c modules */
1465 module_put(zr->decoder->driver->driver.owner);
1467 module_put(zr->encoder->driver->driver.owner);
1469 module_put(THIS_MODULE);
1471 /*mutex_unlock(&zr->resource_lock);*/
1473 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1480 zoran_read (struct file *file,
1485 /* we simply don't support read() (yet)... */
1491 zoran_write (struct file *file,
1492 const char __user *data,
1496 /* ...and the same goes for write() */
1502 setup_fbuffer (struct file *file,
1504 const struct zoran_format *fmt,
1509 struct zoran_fh *fh = file->private_data;
1510 struct zoran *zr = fh->zr;
1512 /* (Ronald) v4l/v4l2 guidelines */
1513 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1516 /* we need a bytesperline value, even if not given */
1518 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1521 if (zr->overlay_active) {
1522 /* dzjee... stupid users... don't even bother to turn off
1523 * overlay before changing the memory location...
1524 * normally, we would return errors here. However, one of
1525 * the tools that does this is... xawtv! and since xawtv
1526 * is used by +/- 99% of the users, we'd rather be user-
1527 * friendly and silently do as if nothing went wrong */
1530 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1532 zr36057_overlay(zr, 0);
1536 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1539 "%s: setup_fbuffer() - no valid overlay format given\n",
1543 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1546 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1547 ZR_DEVNAME(zr), width, height, bytesperline);
1550 if (bytesperline & 3) {
1553 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1554 ZR_DEVNAME(zr), bytesperline);
1558 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1559 zr->buffer.height = height;
1560 zr->buffer.width = width;
1561 zr->buffer.depth = fmt->depth;
1562 zr->overlay_settings.format = fmt;
1563 zr->buffer.bytesperline = bytesperline;
1565 /* The user should set new window parameters */
1566 zr->overlay_settings.is_set = 0;
1573 setup_window (struct file *file,
1578 struct video_clip __user *clips,
1580 void __user *bitmap)
1582 struct zoran_fh *fh = file->private_data;
1583 struct zoran *zr = fh->zr;
1584 struct video_clip *vcp = NULL;
1588 if (!zr->buffer.base) {
1591 "%s: setup_window() - frame buffer has to be set first\n",
1596 if (!fh->overlay_settings.format) {
1599 "%s: setup_window() - no overlay format set\n",
1605 * The video front end needs 4-byte alinged line sizes, we correct that
1606 * silently here if necessary
1608 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1609 end = (x + width) & ~1; /* round down */
1610 x = (x + 1) & ~1; /* round up */
1614 if (zr->buffer.depth == 24) {
1615 end = (x + width) & ~3; /* round down */
1616 x = (x + 3) & ~3; /* round up */
1620 if (width > BUZ_MAX_WIDTH)
1621 width = BUZ_MAX_WIDTH;
1622 if (height > BUZ_MAX_HEIGHT)
1623 height = BUZ_MAX_HEIGHT;
1625 /* Check for vaild parameters */
1626 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1627 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1630 "%s: setup_window() - width = %d or height = %d invalid\n",
1631 ZR_DEVNAME(zr), width, height);
1635 fh->overlay_settings.x = x;
1636 fh->overlay_settings.y = y;
1637 fh->overlay_settings.width = width;
1638 fh->overlay_settings.height = height;
1639 fh->overlay_settings.clipcount = clipcount;
1642 * If an overlay is running, we have to switch it off
1643 * and switch it on again in order to get the new settings in effect.
1645 * We also want to avoid that the overlay mask is written
1646 * when an overlay is running.
1649 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1650 zr->overlay_active != ZORAN_FREE &&
1651 fh->overlay_active != ZORAN_FREE;
1653 zr36057_overlay(zr, 0);
1656 * Write the overlay mask if clips are wanted.
1657 * We prefer a bitmap.
1660 /* fake value - it just means we want clips */
1661 fh->overlay_settings.clipcount = 1;
1663 if (copy_from_user(fh->overlay_mask, bitmap,
1664 (width * height + 7) / 8)) {
1667 } else if (clipcount > 0) {
1668 /* write our own bitmap from the clips */
1669 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1673 "%s: setup_window() - Alloc of clip mask failed\n",
1678 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1682 write_overlay_mask(file, vcp, clipcount);
1686 fh->overlay_settings.is_set = 1;
1687 if (fh->overlay_active != ZORAN_FREE &&
1688 zr->overlay_active != ZORAN_FREE)
1689 zr->overlay_settings = fh->overlay_settings;
1692 zr36057_overlay(zr, 1);
1694 /* Make sure the changes come into effect */
1695 return wait_grab_pending(zr);
1699 setup_overlay (struct file *file,
1702 struct zoran_fh *fh = file->private_data;
1703 struct zoran *zr = fh->zr;
1705 /* If there is nothing to do, return immediatly */
1706 if ((on && fh->overlay_active != ZORAN_FREE) ||
1707 (!on && fh->overlay_active == ZORAN_FREE))
1710 /* check whether we're touching someone else's overlay */
1711 if (on && zr->overlay_active != ZORAN_FREE &&
1712 fh->overlay_active == ZORAN_FREE) {
1715 "%s: setup_overlay() - overlay is already active for another session\n",
1719 if (!on && zr->overlay_active != ZORAN_FREE &&
1720 fh->overlay_active == ZORAN_FREE) {
1723 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1729 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1730 zr->v4l_overlay_active = 0;
1731 /* When a grab is running, the video simply
1732 * won't be switched on any more */
1733 if (!zr->v4l_memgrab_active)
1734 zr36057_overlay(zr, 0);
1735 zr->overlay_mask = NULL;
1737 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1740 "%s: setup_overlay() - buffer or window not set\n",
1744 if (!fh->overlay_settings.format) {
1747 "%s: setup_overlay() - no overlay format set\n",
1751 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1752 zr->v4l_overlay_active = 1;
1753 zr->overlay_mask = fh->overlay_mask;
1754 zr->overlay_settings = fh->overlay_settings;
1755 if (!zr->v4l_memgrab_active)
1756 zr36057_overlay(zr, 1);
1757 /* When a grab is running, the video will be
1758 * switched on when grab is finished */
1761 /* Make sure the changes come into effect */
1762 return wait_grab_pending(zr);
1766 /* get the status of a buffer in the clients buffer queue */
1768 zoran_v4l2_buffer_status (struct file *file,
1769 struct v4l2_buffer *buf,
1772 struct zoran_fh *fh = file->private_data;
1773 struct zoran *zr = fh->zr;
1775 buf->flags = V4L2_BUF_FLAG_MAPPED;
1777 switch (fh->map_mode) {
1778 case ZORAN_MAP_MODE_RAW:
1781 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1782 !fh->v4l_buffers.allocated) {
1785 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1790 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1791 buf->length = fh->v4l_buffers.buffer_size;
1794 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1795 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1796 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1797 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1798 buf->flags |= V4L2_BUF_FLAG_DONE;
1800 fh->v4l_buffers.buffer[num].bs.timestamp;
1802 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1805 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1806 buf->field = V4L2_FIELD_TOP;
1808 buf->field = V4L2_FIELD_INTERLACED;
1812 case ZORAN_MAP_MODE_JPG_REC:
1813 case ZORAN_MAP_MODE_JPG_PLAY:
1816 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1817 !fh->jpg_buffers.allocated) {
1820 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1825 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1826 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1827 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1828 buf->length = fh->jpg_buffers.buffer_size;
1830 /* these variables are only written after frame has been captured */
1831 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1832 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1833 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1835 fh->jpg_buffers.buffer[num].bs.timestamp;
1837 fh->jpg_buffers.buffer[num].bs.length;
1838 buf->flags |= V4L2_BUF_FLAG_DONE;
1840 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1843 /* which fields are these? */
1844 if (fh->jpg_settings.TmpDcm != 1)
1847 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1851 odd_even ? V4L2_FIELD_SEQ_TB :
1860 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1861 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1865 buf->memory = V4L2_MEMORY_MMAP;
1867 buf->m.offset = buf->length * num;
1874 zoran_set_norm (struct zoran *zr,
1875 int norm) /* VIDEO_MODE_* */
1877 int norm_encoder, on;
1879 if (zr->v4l_buffers.active != ZORAN_FREE ||
1880 zr->jpg_buffers.active != ZORAN_FREE) {
1883 "%s: set_norm() called while in playback/capture mode\n",
1888 if (lock_norm && norm != zr->norm) {
1889 if (lock_norm > 1) {
1892 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1898 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1904 if (norm != VIDEO_MODE_AUTO &&
1905 (norm < 0 || norm >= zr->card.norms ||
1906 !zr->card.tvn[norm])) {
1908 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1909 ZR_DEVNAME(zr), norm);
1913 if (norm == VIDEO_MODE_AUTO) {
1916 /* if we have autodetect, ... */
1917 struct video_decoder_capability caps;
1918 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1919 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1920 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1925 decoder_command(zr, DECODER_SET_NORM, &norm);
1927 /* let changes come into effect */
1930 decoder_command(zr, DECODER_GET_STATUS, &status);
1931 if (!(status & DECODER_STATUS_GOOD)) {
1934 "%s: set_norm() - no norm detected\n",
1937 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1941 if (status & DECODER_STATUS_NTSC)
1942 norm = VIDEO_MODE_NTSC;
1943 else if (status & DECODER_STATUS_SECAM)
1944 norm = VIDEO_MODE_SECAM;
1946 norm = VIDEO_MODE_PAL;
1948 zr->timing = zr->card.tvn[norm];
1949 norm_encoder = norm;
1951 /* We switch overlay off and on since a change in the
1952 * norm needs different VFE settings */
1953 on = zr->overlay_active && !zr->v4l_memgrab_active;
1955 zr36057_overlay(zr, 0);
1957 decoder_command(zr, DECODER_SET_NORM, &norm);
1958 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1961 zr36057_overlay(zr, 1);
1963 /* Make sure the changes come into effect */
1970 zoran_set_input (struct zoran *zr,
1975 if (input == zr->input) {
1979 if (zr->v4l_buffers.active != ZORAN_FREE ||
1980 zr->jpg_buffers.active != ZORAN_FREE) {
1983 "%s: set_input() called while in playback/capture mode\n",
1988 if (input < 0 || input >= zr->card.inputs) {
1991 "%s: set_input() - unnsupported input %d\n",
1992 ZR_DEVNAME(zr), input);
1996 realinput = zr->card.input[input].muxsel;
1999 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2009 zoran_do_ioctl (struct inode *inode,
2014 struct zoran_fh *fh = file->private_data;
2015 struct zoran *zr = fh->zr;
2016 /* CAREFUL: used in multiple places here */
2017 struct zoran_jpg_settings settings;
2019 /* we might have older buffers lying around... We don't want
2020 * to wait, but we do want to try cleaning them up ASAP. So
2021 * we try to obtain the lock and free them. If that fails, we
2022 * don't do anything and wait for the next turn. In the end,
2023 * zoran_close() or a new allocation will still free them...
2024 * This is just a 'the sooner the better' extra 'feature'
2026 * We don't free the buffers right on munmap() because that
2027 * causes oopses (kfree() inside munmap() oopses for no
2028 * apparent reason - it's also not reproduceable in any way,
2029 * but moving the free code outside the munmap() handler fixes
2030 * all this... If someone knows why, please explain me (Ronald)
2032 if (!!mutex_trylock(&zr->resource_lock)) {
2033 /* we obtained it! Let's try to free some things */
2034 if (fh->jpg_buffers.ready_to_be_freed)
2035 jpg_fbuffer_free(file);
2036 if (fh->v4l_buffers.ready_to_be_freed)
2037 v4l_fbuffer_free(file);
2039 mutex_unlock(&zr->resource_lock);
2046 struct video_capability *vcap = arg;
2048 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2050 memset(vcap, 0, sizeof(struct video_capability));
2051 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
2052 vcap->type = ZORAN_VID_TYPE;
2054 vcap->channels = zr->card.inputs;
2056 mutex_lock(&zr->resource_lock);
2057 vcap->maxwidth = BUZ_MAX_WIDTH;
2058 vcap->maxheight = BUZ_MAX_HEIGHT;
2059 vcap->minwidth = BUZ_MIN_WIDTH;
2060 vcap->minheight = BUZ_MIN_HEIGHT;
2061 mutex_unlock(&zr->resource_lock);
2069 struct video_channel *vchan = arg;
2070 int channel = vchan->channel;
2072 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2073 ZR_DEVNAME(zr), vchan->channel);
2075 memset(vchan, 0, sizeof(struct video_channel));
2076 if (channel > zr->card.inputs || channel < 0) {
2079 "%s: VIDIOCGCHAN on not existing channel %d\n",
2080 ZR_DEVNAME(zr), channel);
2084 strcpy(vchan->name, zr->card.input[channel].name);
2088 vchan->type = VIDEO_TYPE_CAMERA;
2089 mutex_lock(&zr->resource_lock);
2090 vchan->norm = zr->norm;
2091 mutex_unlock(&zr->resource_lock);
2092 vchan->channel = channel;
2098 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2100 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2102 * * The famos BTTV driver has it implemented with a struct video_channel argument
2103 * * and we follow it for compatibility reasons
2105 * * BTW: this is the only way the user can set the norm!
2110 struct video_channel *vchan = arg;
2115 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2116 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2118 mutex_lock(&zr->resource_lock);
2119 if ((res = zoran_set_input(zr, vchan->channel)))
2120 goto schan_unlock_and_return;
2121 if ((res = zoran_set_norm(zr, vchan->norm)))
2122 goto schan_unlock_and_return;
2124 /* Make sure the changes come into effect */
2125 res = wait_grab_pending(zr);
2126 schan_unlock_and_return:
2127 mutex_unlock(&zr->resource_lock);
2134 struct video_picture *vpict = arg;
2136 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2138 memset(vpict, 0, sizeof(struct video_picture));
2139 mutex_lock(&zr->resource_lock);
2140 vpict->hue = zr->hue;
2141 vpict->brightness = zr->brightness;
2142 vpict->contrast = zr->contrast;
2143 vpict->colour = zr->saturation;
2144 if (fh->overlay_settings.format) {
2145 vpict->depth = fh->overlay_settings.format->depth;
2146 vpict->palette = fh->overlay_settings.format->palette;
2150 mutex_unlock(&zr->resource_lock);
2158 struct video_picture *vpict = arg;
2163 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2164 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2165 vpict->colour, vpict->contrast, vpict->depth,
2168 for (i = 0; i < zoran_num_formats; i++) {
2169 const struct zoran_format *fmt = &zoran_formats[i];
2171 if (fmt->palette != -1 &&
2172 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2173 fmt->palette == vpict->palette &&
2174 fmt->depth == vpict->depth)
2177 if (i == zoran_num_formats) {
2180 "%s: VIDIOCSPICT - Invalid palette %d\n",
2181 ZR_DEVNAME(zr), vpict->palette);
2185 mutex_lock(&zr->resource_lock);
2187 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2189 zr->hue = vpict->hue;
2190 zr->contrast = vpict->contrast;
2191 zr->saturation = vpict->colour;
2192 zr->brightness = vpict->brightness;
2194 fh->overlay_settings.format = &zoran_formats[i];
2196 mutex_unlock(&zr->resource_lock);
2206 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2207 ZR_DEVNAME(zr), *on);
2209 mutex_lock(&zr->resource_lock);
2210 res = setup_overlay(file, *on);
2211 mutex_unlock(&zr->resource_lock);
2219 struct video_window *vwin = arg;
2221 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2223 memset(vwin, 0, sizeof(struct video_window));
2224 mutex_lock(&zr->resource_lock);
2225 vwin->x = fh->overlay_settings.x;
2226 vwin->y = fh->overlay_settings.y;
2227 vwin->width = fh->overlay_settings.width;
2228 vwin->height = fh->overlay_settings.height;
2229 mutex_unlock(&zr->resource_lock);
2230 vwin->clipcount = 0;
2237 struct video_window *vwin = arg;
2242 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2243 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2244 vwin->height, vwin->clipcount);
2246 mutex_lock(&zr->resource_lock);
2248 setup_window(file, vwin->x, vwin->y, vwin->width,
2249 vwin->height, vwin->clips,
2250 vwin->clipcount, NULL);
2251 mutex_unlock(&zr->resource_lock);
2259 struct video_buffer *vbuf = arg;
2261 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2263 mutex_lock(&zr->resource_lock);
2265 mutex_unlock(&zr->resource_lock);
2272 struct video_buffer *vbuf = arg;
2277 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2278 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2279 vbuf->height, vbuf->depth, vbuf->bytesperline);
2281 for (i = 0; i < zoran_num_formats; i++)
2282 if (zoran_formats[i].depth == vbuf->depth)
2284 if (i == zoran_num_formats) {
2287 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2288 ZR_DEVNAME(zr), vbuf->depth);
2292 mutex_lock(&zr->resource_lock);
2294 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2295 vbuf->width, vbuf->height,
2296 vbuf->bytesperline);
2297 mutex_unlock(&zr->resource_lock);
2305 int *frame = arg, res;
2307 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2308 ZR_DEVNAME(zr), *frame);
2310 mutex_lock(&zr->resource_lock);
2311 res = v4l_sync(file, *frame);
2312 mutex_unlock(&zr->resource_lock);
2314 zr->v4l_sync_tail++;
2319 case VIDIOCMCAPTURE:
2321 struct video_mmap *vmap = arg;
2326 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2327 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2330 mutex_lock(&zr->resource_lock);
2331 res = v4l_grab(file, vmap);
2332 mutex_unlock(&zr->resource_lock);
2339 struct video_mbuf *vmbuf = arg;
2342 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2345 fh->v4l_buffers.num_buffers *
2346 fh->v4l_buffers.buffer_size;
2347 vmbuf->frames = fh->v4l_buffers.num_buffers;
2348 for (i = 0; i < vmbuf->frames; i++) {
2350 i * fh->v4l_buffers.buffer_size;
2353 mutex_lock(&zr->resource_lock);
2355 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2358 "%s: VIDIOCGMBUF - buffers already allocated\n",
2361 goto v4l1reqbuf_unlock_and_return;
2364 if (v4l_fbuffer_alloc(file)) {
2366 goto v4l1reqbuf_unlock_and_return;
2369 /* The next mmap will map the V4L buffers */
2370 fh->map_mode = ZORAN_MAP_MODE_RAW;
2371 v4l1reqbuf_unlock_and_return:
2372 mutex_unlock(&zr->resource_lock);
2380 struct video_unit *vunit = arg;
2382 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2384 vunit->video = zr->video_dev->minor;
2385 vunit->vbi = VIDEO_NO_UNIT;
2386 vunit->radio = VIDEO_NO_UNIT;
2387 vunit->audio = VIDEO_NO_UNIT;
2388 vunit->teletext = VIDEO_NO_UNIT;
2395 * RJ: In principal we could support subcaptures for V4L grabbing.
2396 * Not even the famous BTTV driver has them, however.
2397 * If there should be a strong demand, one could consider
2398 * to implement them.
2400 case VIDIOCGCAPTURE:
2402 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2408 case VIDIOCSCAPTURE:
2410 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2416 case BUZIOC_G_PARAMS:
2418 struct zoran_params *bparams = arg;
2420 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2422 memset(bparams, 0, sizeof(struct zoran_params));
2423 bparams->major_version = MAJOR_VERSION;
2424 bparams->minor_version = MINOR_VERSION;
2426 mutex_lock(&zr->resource_lock);
2428 bparams->norm = zr->norm;
2429 bparams->input = zr->input;
2431 bparams->decimation = fh->jpg_settings.decimation;
2432 bparams->HorDcm = fh->jpg_settings.HorDcm;
2433 bparams->VerDcm = fh->jpg_settings.VerDcm;
2434 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2435 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2436 bparams->img_x = fh->jpg_settings.img_x;
2437 bparams->img_y = fh->jpg_settings.img_y;
2438 bparams->img_width = fh->jpg_settings.img_width;
2439 bparams->img_height = fh->jpg_settings.img_height;
2440 bparams->odd_even = fh->jpg_settings.odd_even;
2442 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2443 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2444 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2445 memcpy(bparams->APP_data,
2446 fh->jpg_settings.jpg_comp.APP_data,
2447 sizeof(bparams->APP_data));
2448 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2449 memcpy(bparams->COM_data,
2450 fh->jpg_settings.jpg_comp.COM_data,
2451 sizeof(bparams->COM_data));
2452 bparams->jpeg_markers =
2453 fh->jpg_settings.jpg_comp.jpeg_markers;
2455 mutex_unlock(&zr->resource_lock);
2457 bparams->VFIFO_FB = 0;
2463 case BUZIOC_S_PARAMS:
2465 struct zoran_params *bparams = arg;
2468 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2470 settings.decimation = bparams->decimation;
2471 settings.HorDcm = bparams->HorDcm;
2472 settings.VerDcm = bparams->VerDcm;
2473 settings.TmpDcm = bparams->TmpDcm;
2474 settings.field_per_buff = bparams->field_per_buff;
2475 settings.img_x = bparams->img_x;
2476 settings.img_y = bparams->img_y;
2477 settings.img_width = bparams->img_width;
2478 settings.img_height = bparams->img_height;
2479 settings.odd_even = bparams->odd_even;
2481 settings.jpg_comp.quality = bparams->quality;
2482 settings.jpg_comp.APPn = bparams->APPn;
2483 settings.jpg_comp.APP_len = bparams->APP_len;
2484 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2485 sizeof(bparams->APP_data));
2486 settings.jpg_comp.COM_len = bparams->COM_len;
2487 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2488 sizeof(bparams->COM_data));
2489 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2491 mutex_lock(&zr->resource_lock);
2493 if (zr->codec_mode != BUZ_MODE_IDLE) {
2496 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2499 goto sparams_unlock_and_return;
2502 /* Check the params first before overwriting our
2504 if (zoran_check_jpg_settings(zr, &settings)) {
2506 goto sparams_unlock_and_return;
2509 fh->jpg_settings = settings;
2510 sparams_unlock_and_return:
2511 mutex_unlock(&zr->resource_lock);
2517 case BUZIOC_REQBUFS:
2519 struct zoran_requestbuffers *breq = arg;
2524 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2525 ZR_DEVNAME(zr), breq->count, breq->size);
2527 /* Enforce reasonable lower and upper limits */
2528 if (breq->count < 4)
2529 breq->count = 4; /* Could be choosen smaller */
2530 if (breq->count > jpg_nbufs)
2531 breq->count = jpg_nbufs;
2532 breq->size = PAGE_ALIGN(breq->size);
2533 if (breq->size < 8192)
2534 breq->size = 8192; /* Arbitrary */
2535 /* breq->size is limited by 1 page for the stat_com
2536 * tables to a Maximum of 2 MB */
2537 if (breq->size > jpg_bufsize)
2538 breq->size = jpg_bufsize;
2539 if (fh->jpg_buffers.need_contiguous &&
2540 breq->size > MAX_KMALLOC_MEM)
2541 breq->size = MAX_KMALLOC_MEM;
2543 mutex_lock(&zr->resource_lock);
2545 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2548 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2551 goto jpgreqbuf_unlock_and_return;
2554 fh->jpg_buffers.num_buffers = breq->count;
2555 fh->jpg_buffers.buffer_size = breq->size;
2557 if (jpg_fbuffer_alloc(file)) {
2559 goto jpgreqbuf_unlock_and_return;
2562 /* The next mmap will map the MJPEG buffers - could
2563 * also be *_PLAY, but it doesn't matter here */
2564 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2565 jpgreqbuf_unlock_and_return:
2566 mutex_unlock(&zr->resource_lock);
2572 case BUZIOC_QBUF_CAPT:
2574 int *frame = arg, res;
2576 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2577 ZR_DEVNAME(zr), *frame);
2579 mutex_lock(&zr->resource_lock);
2580 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2581 mutex_unlock(&zr->resource_lock);
2587 case BUZIOC_QBUF_PLAY:
2589 int *frame = arg, res;
2591 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2592 ZR_DEVNAME(zr), *frame);
2594 mutex_lock(&zr->resource_lock);
2595 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2596 mutex_unlock(&zr->resource_lock);
2604 struct zoran_sync *bsync = arg;
2607 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2609 mutex_lock(&zr->resource_lock);
2610 res = jpg_sync(file, bsync);
2611 mutex_unlock(&zr->resource_lock);
2617 case BUZIOC_G_STATUS:
2619 struct zoran_status *bstat = arg;
2620 int norm, input, status, res = 0;
2622 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2624 if (zr->codec_mode != BUZ_MODE_IDLE) {
2627 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2632 input = zr->card.input[bstat->input].muxsel;
2633 norm = VIDEO_MODE_AUTO;
2635 mutex_lock(&zr->resource_lock);
2637 if (zr->codec_mode != BUZ_MODE_IDLE) {
2640 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2643 goto gstat_unlock_and_return;
2646 decoder_command(zr, DECODER_SET_INPUT, &input);
2647 decoder_command(zr, DECODER_SET_NORM, &norm);
2649 /* sleep 1 second */
2652 /* Get status of video decoder */
2653 decoder_command(zr, DECODER_GET_STATUS, &status);
2655 /* restore previous input and norm */
2656 input = zr->card.input[zr->input].muxsel;
2657 decoder_command(zr, DECODER_SET_INPUT, &input);
2658 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2659 gstat_unlock_and_return:
2660 mutex_unlock(&zr->resource_lock);
2664 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2665 if (status & DECODER_STATUS_NTSC)
2666 bstat->norm = VIDEO_MODE_NTSC;
2667 else if (status & DECODER_STATUS_SECAM)
2668 bstat->norm = VIDEO_MODE_SECAM;
2670 bstat->norm = VIDEO_MODE_PAL;
2673 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2682 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2683 * it allows for integrating the JPEG capturing calls inside standard v4l2
2686 case VIDIOC_QUERYCAP:
2688 struct v4l2_capability *cap = arg;
2690 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2692 memset(cap, 0, sizeof(*cap));
2693 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2694 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2695 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2696 pci_name(zr->pci_dev));
2698 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2700 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2706 case VIDIOC_ENUM_FMT:
2708 struct v4l2_fmtdesc *fmt = arg;
2709 int index = fmt->index, num = -1, i, flag = 0, type =
2712 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2713 ZR_DEVNAME(zr), fmt->index);
2715 switch (fmt->type) {
2716 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2717 flag = ZORAN_FORMAT_CAPTURE;
2719 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2720 flag = ZORAN_FORMAT_PLAYBACK;
2722 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2723 flag = ZORAN_FORMAT_OVERLAY;
2728 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2729 ZR_DEVNAME(zr), fmt->type);
2733 for (i = 0; i < zoran_num_formats; i++) {
2734 if (zoran_formats[i].flags & flag)
2736 if (num == fmt->index)
2739 if (fmt->index < 0 /* late, but not too late */ ||
2740 i == zoran_num_formats)
2743 memset(fmt, 0, sizeof(*fmt));
2746 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2747 fmt->pixelformat = zoran_formats[i].fourcc;
2748 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2749 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2757 struct v4l2_format *fmt = arg;
2758 int type = fmt->type;
2760 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2762 memset(fmt, 0, sizeof(*fmt));
2765 switch (fmt->type) {
2766 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2768 mutex_lock(&zr->resource_lock);
2770 fmt->fmt.win.w.left = fh->overlay_settings.x;
2771 fmt->fmt.win.w.top = fh->overlay_settings.y;
2772 fmt->fmt.win.w.width = fh->overlay_settings.width;
2773 fmt->fmt.win.w.height =
2774 fh->overlay_settings.height;
2775 if (fh->overlay_settings.width * 2 >
2777 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2779 fmt->fmt.win.field = V4L2_FIELD_TOP;
2781 mutex_unlock(&zr->resource_lock);
2785 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2786 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2788 mutex_lock(&zr->resource_lock);
2790 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2791 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2793 fmt->fmt.pix.width =
2794 fh->v4l_settings.width;
2795 fmt->fmt.pix.height =
2796 fh->v4l_settings.height;
2797 fmt->fmt.pix.sizeimage =
2798 fh->v4l_buffers.buffer_size;
2799 fmt->fmt.pix.pixelformat =
2800 fh->v4l_settings.format->fourcc;
2801 fmt->fmt.pix.colorspace =
2802 fh->v4l_settings.format->colorspace;
2803 fmt->fmt.pix.bytesperline = 0;
2804 if (BUZ_MAX_HEIGHT <
2805 (fh->v4l_settings.height * 2))
2806 fmt->fmt.pix.field =
2807 V4L2_FIELD_INTERLACED;
2809 fmt->fmt.pix.field =
2814 fmt->fmt.pix.width =
2815 fh->jpg_settings.img_width /
2816 fh->jpg_settings.HorDcm;
2817 fmt->fmt.pix.height =
2818 fh->jpg_settings.img_height /
2819 (fh->jpg_settings.VerDcm *
2820 fh->jpg_settings.TmpDcm);
2821 fmt->fmt.pix.sizeimage =
2822 zoran_v4l2_calc_bufsize(&fh->
2824 fmt->fmt.pix.pixelformat =
2826 if (fh->jpg_settings.TmpDcm == 1)
2827 fmt->fmt.pix.field =
2829 odd_even ? V4L2_FIELD_SEQ_BT :
2832 fmt->fmt.pix.field =
2834 odd_even ? V4L2_FIELD_TOP :
2837 fmt->fmt.pix.bytesperline = 0;
2838 fmt->fmt.pix.colorspace =
2839 V4L2_COLORSPACE_SMPTE170M;
2842 mutex_unlock(&zr->resource_lock);
2849 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2850 ZR_DEVNAME(zr), fmt->type);
2859 struct v4l2_format *fmt = arg;
2863 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2864 ZR_DEVNAME(zr), fmt->type);
2866 switch (fmt->type) {
2867 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2869 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2870 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2871 fmt->fmt.win.w.width,
2872 fmt->fmt.win.w.height,
2873 fmt->fmt.win.clipcount,
2874 fmt->fmt.win.bitmap);
2875 mutex_lock(&zr->resource_lock);
2877 setup_window(file, fmt->fmt.win.w.left,
2879 fmt->fmt.win.w.width,
2880 fmt->fmt.win.w.height,
2881 (struct video_clip __user *)
2883 fmt->fmt.win.clipcount,
2884 fmt->fmt.win.bitmap);
2885 mutex_unlock(&zr->resource_lock);
2889 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2890 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2893 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2894 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2895 fmt->fmt.pix.width, fmt->fmt.pix.height,
2896 fmt->fmt.pix.pixelformat,
2897 (char *) &printformat);
2899 if (fmt->fmt.pix.bytesperline > 0) {
2901 KERN_ERR "%s: bpl not supported\n",
2906 /* we can be requested to do JPEG/raw playback/capture */
2908 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2909 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2910 fmt->fmt.pix.pixelformat ==
2911 V4L2_PIX_FMT_MJPEG))) {
2914 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2915 ZR_DEVNAME(zr), fmt->type,
2916 fmt->fmt.pix.pixelformat,
2917 (char *) &printformat);
2921 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2922 mutex_lock(&zr->resource_lock);
2924 settings = fh->jpg_settings;
2926 if (fh->v4l_buffers.allocated ||
2927 fh->jpg_buffers.allocated) {
2930 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2933 goto sfmtjpg_unlock_and_return;
2936 /* we actually need to set 'real' parameters now */
2937 if ((fmt->fmt.pix.height * 2) >
2939 settings.TmpDcm = 1;
2941 settings.TmpDcm = 2;
2942 settings.decimation = 0;
2943 if (fmt->fmt.pix.height <=
2944 fh->jpg_settings.img_height / 2)
2945 settings.VerDcm = 2;
2947 settings.VerDcm = 1;
2948 if (fmt->fmt.pix.width <=
2949 fh->jpg_settings.img_width / 4)
2950 settings.HorDcm = 4;
2951 else if (fmt->fmt.pix.width <=
2952 fh->jpg_settings.img_width / 2)
2953 settings.HorDcm = 2;
2955 settings.HorDcm = 1;
2956 if (settings.TmpDcm == 1)
2957 settings.field_per_buff = 2;
2959 settings.field_per_buff = 1;
2963 zoran_check_jpg_settings(zr,
2965 goto sfmtjpg_unlock_and_return;
2967 /* it's ok, so set them */
2968 fh->jpg_settings = settings;
2970 /* tell the user what we actually did */
2971 fmt->fmt.pix.width =
2972 settings.img_width / settings.HorDcm;
2973 fmt->fmt.pix.height =
2974 settings.img_height * 2 /
2975 (settings.TmpDcm * settings.VerDcm);
2976 if (settings.TmpDcm == 1)
2977 fmt->fmt.pix.field =
2979 odd_even ? V4L2_FIELD_SEQ_TB :
2982 fmt->fmt.pix.field =
2984 odd_even ? V4L2_FIELD_TOP :
2986 fh->jpg_buffers.buffer_size =
2987 zoran_v4l2_calc_bufsize(&fh->
2989 fmt->fmt.pix.sizeimage =
2990 fh->jpg_buffers.buffer_size;
2992 /* we hereby abuse this variable to show that
2993 * we're gonna do mjpeg capture */
2996 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2997 ZORAN_MAP_MODE_JPG_REC :
2998 ZORAN_MAP_MODE_JPG_PLAY;
2999 sfmtjpg_unlock_and_return:
3000 mutex_unlock(&zr->resource_lock);
3002 for (i = 0; i < zoran_num_formats; i++)
3003 if (fmt->fmt.pix.pixelformat ==
3004 zoran_formats[i].fourcc)
3006 if (i == zoran_num_formats) {
3009 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3011 fmt->fmt.pix.pixelformat,
3012 (char *) &printformat);
3015 mutex_lock(&zr->resource_lock);
3016 if (fh->jpg_buffers.allocated ||
3017 (fh->v4l_buffers.allocated &&
3018 fh->v4l_buffers.active !=
3022 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3025 goto sfmtv4l_unlock_and_return;
3027 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3028 fmt->fmt.pix.height =
3030 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3031 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3034 zoran_v4l_set_format(file,
3041 goto sfmtv4l_unlock_and_return;
3043 /* tell the user the
3044 * results/missing stuff */
3045 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3047 if (BUZ_MAX_HEIGHT <
3048 (fh->v4l_settings.height * 2))
3049 fmt->fmt.pix.field =
3050 V4L2_FIELD_INTERLACED;
3052 fmt->fmt.pix.field =
3055 fh->map_mode = ZORAN_MAP_MODE_RAW;
3056 sfmtv4l_unlock_and_return:
3057 mutex_unlock(&zr->resource_lock);
3063 dprintk(3, "unsupported\n");
3066 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3067 ZR_DEVNAME(zr), fmt->type);
3077 struct v4l2_framebuffer *fb = arg;
3079 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3081 memset(fb, 0, sizeof(*fb));
3082 mutex_lock(&zr->resource_lock);
3083 fb->base = zr->buffer.base;
3084 fb->fmt.width = zr->buffer.width;
3085 fb->fmt.height = zr->buffer.height;
3086 if (zr->overlay_settings.format) {
3087 fb->fmt.pixelformat =
3088 fh->overlay_settings.format->fourcc;
3090 fb->fmt.bytesperline = zr->buffer.bytesperline;
3091 mutex_unlock(&zr->resource_lock);
3092 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3093 fb->fmt.field = V4L2_FIELD_INTERLACED;
3094 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3095 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3104 struct v4l2_framebuffer *fb = arg;
3105 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3109 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3110 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3111 fb->fmt.bytesperline, fb->fmt.pixelformat,
3112 (char *) &printformat);
3114 for (i = 0; i < zoran_num_formats; i++)
3115 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3117 if (i == zoran_num_formats) {
3120 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3121 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3122 (char *) &printformat);
3126 mutex_lock(&zr->resource_lock);
3128 setup_fbuffer(file, fb->base, &zoran_formats[i],
3129 fb->fmt.width, fb->fmt.height,
3130 fb->fmt.bytesperline);
3131 mutex_unlock(&zr->resource_lock);
3137 case VIDIOC_OVERLAY:
3141 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3142 ZR_DEVNAME(zr), *on);
3144 mutex_lock(&zr->resource_lock);
3145 res = setup_overlay(file, *on);
3146 mutex_unlock(&zr->resource_lock);
3152 case VIDIOC_REQBUFS:
3154 struct v4l2_requestbuffers *req = arg;
3157 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3158 ZR_DEVNAME(zr), req->type);
3160 if (req->memory != V4L2_MEMORY_MMAP) {
3163 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3164 ZR_DEVNAME(zr), req->memory);
3168 mutex_lock(&zr->resource_lock);
3170 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3173 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3176 goto v4l2reqbuf_unlock_and_return;
3179 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3180 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3182 /* control user input */
3185 if (req->count > v4l_nbufs)
3186 req->count = v4l_nbufs;
3187 fh->v4l_buffers.num_buffers = req->count;
3189 if (v4l_fbuffer_alloc(file)) {
3191 goto v4l2reqbuf_unlock_and_return;
3194 /* The next mmap will map the V4L buffers */
3195 fh->map_mode = ZORAN_MAP_MODE_RAW;
3197 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3198 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3200 /* we need to calculate size ourselves now */
3203 if (req->count > jpg_nbufs)
3204 req->count = jpg_nbufs;
3205 fh->jpg_buffers.num_buffers = req->count;
3206 fh->jpg_buffers.buffer_size =
3207 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3209 if (jpg_fbuffer_alloc(file)) {
3211 goto v4l2reqbuf_unlock_and_return;
3214 /* The next mmap will map the MJPEG buffers */
3215 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3216 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3218 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3223 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3224 ZR_DEVNAME(zr), req->type);
3226 goto v4l2reqbuf_unlock_and_return;
3228 v4l2reqbuf_unlock_and_return:
3229 mutex_unlock(&zr->resource_lock);
3235 case VIDIOC_QUERYBUF:
3237 struct v4l2_buffer *buf = arg;
3238 __u32 type = buf->type;
3239 int index = buf->index, res;
3243 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3244 ZR_DEVNAME(zr), buf->index, buf->type);
3246 memset(buf, 0, sizeof(buf));
3250 mutex_lock(&zr->resource_lock);
3251 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3252 mutex_unlock(&zr->resource_lock);
3260 struct v4l2_buffer *buf = arg;
3261 int res = 0, codec_mode, buf_type;
3264 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3265 ZR_DEVNAME(zr), buf->type, buf->index);
3267 mutex_lock(&zr->resource_lock);
3269 switch (fh->map_mode) {
3270 case ZORAN_MAP_MODE_RAW:
3271 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3274 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3275 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3277 goto qbuf_unlock_and_return;
3280 res = zoran_v4l_queue_frame(file, buf->index);
3282 goto qbuf_unlock_and_return;
3283 if (!zr->v4l_memgrab_active &&
3284 fh->v4l_buffers.active == ZORAN_LOCKED)
3285 zr36057_set_memgrab(zr, 1);
3288 case ZORAN_MAP_MODE_JPG_REC:
3289 case ZORAN_MAP_MODE_JPG_PLAY:
3290 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3291 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3292 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3294 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3295 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3298 if (buf->type != buf_type) {
3301 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3302 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3304 goto qbuf_unlock_and_return;
3308 zoran_jpg_queue_frame(file, buf->index,
3311 goto qbuf_unlock_and_return;
3312 if (zr->codec_mode == BUZ_MODE_IDLE &&
3313 fh->jpg_buffers.active == ZORAN_LOCKED) {
3314 zr36057_enable_jpg(zr, codec_mode);
3321 "%s: VIDIOC_QBUF - unsupported type %d\n",
3322 ZR_DEVNAME(zr), buf->type);
3324 goto qbuf_unlock_and_return;
3326 qbuf_unlock_and_return:
3327 mutex_unlock(&zr->resource_lock);
3335 struct v4l2_buffer *buf = arg;
3336 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3338 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3339 ZR_DEVNAME(zr), buf->type);
3341 mutex_lock(&zr->resource_lock);
3343 switch (fh->map_mode) {
3344 case ZORAN_MAP_MODE_RAW:
3345 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3348 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3349 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3351 goto dqbuf_unlock_and_return;
3354 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3355 if (file->f_flags & O_NONBLOCK &&
3356 zr->v4l_buffers.buffer[num].state !=
3359 goto dqbuf_unlock_and_return;
3361 res = v4l_sync(file, num);
3363 goto dqbuf_unlock_and_return;
3365 zr->v4l_sync_tail++;
3366 res = zoran_v4l2_buffer_status(file, buf, num);
3369 case ZORAN_MAP_MODE_JPG_REC:
3370 case ZORAN_MAP_MODE_JPG_PLAY:
3372 struct zoran_sync bs;
3374 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3375 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3377 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3379 if (buf->type != buf_type) {
3382 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3383 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3385 goto dqbuf_unlock_and_return;
3390 jpg_que_tail & BUZ_MASK_FRAME];
3392 if (file->f_flags & O_NONBLOCK &&
3393 zr->jpg_buffers.buffer[num].state !=
3396 goto dqbuf_unlock_and_return;
3398 res = jpg_sync(file, &bs);
3400 goto dqbuf_unlock_and_return;
3402 zoran_v4l2_buffer_status(file, buf, bs.frame);
3409 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3410 ZR_DEVNAME(zr), buf->type);
3412 goto dqbuf_unlock_and_return;
3414 dqbuf_unlock_and_return:
3415 mutex_unlock(&zr->resource_lock);
3421 case VIDIOC_STREAMON:
3425 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3427 mutex_lock(&zr->resource_lock);
3429 switch (fh->map_mode) {
3430 case ZORAN_MAP_MODE_RAW: /* raw capture */
3431 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3432 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3434 goto strmon_unlock_and_return;
3437 zr->v4l_buffers.active = fh->v4l_buffers.active =
3439 zr->v4l_settings = fh->v4l_settings;
3441 zr->v4l_sync_tail = zr->v4l_pend_tail;
3442 if (!zr->v4l_memgrab_active &&
3443 zr->v4l_pend_head != zr->v4l_pend_tail) {
3444 zr36057_set_memgrab(zr, 1);
3448 case ZORAN_MAP_MODE_JPG_REC:
3449 case ZORAN_MAP_MODE_JPG_PLAY:
3450 /* what is the codec mode right now? */
3451 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3452 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3454 goto strmon_unlock_and_return;
3457 zr->jpg_buffers.active = fh->jpg_buffers.active =
3460 if (zr->jpg_que_head != zr->jpg_que_tail) {
3461 /* Start the jpeg codec when the first frame is queued */
3469 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3470 ZR_DEVNAME(zr), fh->map_mode);
3472 goto strmon_unlock_and_return;
3474 strmon_unlock_and_return:
3475 mutex_unlock(&zr->resource_lock);
3481 case VIDIOC_STREAMOFF:
3485 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3487 mutex_lock(&zr->resource_lock);
3489 switch (fh->map_mode) {
3490 case ZORAN_MAP_MODE_RAW: /* raw capture */
3491 if (fh->v4l_buffers.active == ZORAN_FREE &&
3492 zr->v4l_buffers.active != ZORAN_FREE) {
3493 res = -EPERM; /* stay off other's settings! */
3494 goto strmoff_unlock_and_return;
3496 if (zr->v4l_buffers.active == ZORAN_FREE)
3497 goto strmoff_unlock_and_return;
3499 /* unload capture */
3500 if (zr->v4l_memgrab_active)
3501 zr36057_set_memgrab(zr, 0);
3503 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3504 zr->v4l_buffers.buffer[i].state =
3506 fh->v4l_buffers = zr->v4l_buffers;
3508 zr->v4l_buffers.active = fh->v4l_buffers.active =
3511 zr->v4l_grab_seq = 0;
3512 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3513 zr->v4l_sync_tail = 0;
3517 case ZORAN_MAP_MODE_JPG_REC:
3518 case ZORAN_MAP_MODE_JPG_PLAY:
3519 if (fh->jpg_buffers.active == ZORAN_FREE &&
3520 zr->jpg_buffers.active != ZORAN_FREE) {
3521 res = -EPERM; /* stay off other's settings! */
3522 goto strmoff_unlock_and_return;
3524 if (zr->jpg_buffers.active == ZORAN_FREE)
3525 goto strmoff_unlock_and_return;
3530 ZORAN_MAP_MODE_JPG_REC) ?
3531 BUZ_MODE_MOTION_COMPRESS :
3532 BUZ_MODE_MOTION_DECOMPRESS);
3534 goto strmoff_unlock_and_return;
3539 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3540 ZR_DEVNAME(zr), fh->map_mode);
3542 goto strmoff_unlock_and_return;
3544 strmoff_unlock_and_return:
3545 mutex_unlock(&zr->resource_lock);
3551 case VIDIOC_QUERYCTRL:
3553 struct v4l2_queryctrl *ctrl = arg;
3555 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3556 ZR_DEVNAME(zr), ctrl->id);
3558 /* we only support hue/saturation/contrast/brightness */
3559 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3560 ctrl->id > V4L2_CID_HUE)
3564 memset(ctrl, 0, sizeof(*ctrl));
3569 case V4L2_CID_BRIGHTNESS:
3570 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3572 case V4L2_CID_CONTRAST:
3573 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3575 case V4L2_CID_SATURATION:
3576 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3579 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3584 ctrl->maximum = 65535;
3586 ctrl->default_value = 32768;
3587 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3595 struct v4l2_control *ctrl = arg;
3597 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3598 ZR_DEVNAME(zr), ctrl->id);
3600 /* we only support hue/saturation/contrast/brightness */
3601 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3602 ctrl->id > V4L2_CID_HUE)
3605 mutex_lock(&zr->resource_lock);
3607 case V4L2_CID_BRIGHTNESS:
3608 ctrl->value = zr->brightness;
3610 case V4L2_CID_CONTRAST:
3611 ctrl->value = zr->contrast;
3613 case V4L2_CID_SATURATION:
3614 ctrl->value = zr->saturation;
3617 ctrl->value = zr->hue;
3620 mutex_unlock(&zr->resource_lock);
3628 struct v4l2_control *ctrl = arg;
3629 struct video_picture pict;
3631 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3632 ZR_DEVNAME(zr), ctrl->id);
3634 /* we only support hue/saturation/contrast/brightness */
3635 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3636 ctrl->id > V4L2_CID_HUE)
3639 if (ctrl->value < 0 || ctrl->value > 65535) {
3642 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3643 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3647 mutex_lock(&zr->resource_lock);
3649 case V4L2_CID_BRIGHTNESS:
3650 zr->brightness = ctrl->value;
3652 case V4L2_CID_CONTRAST:
3653 zr->contrast = ctrl->value;
3655 case V4L2_CID_SATURATION:
3656 zr->saturation = ctrl->value;
3659 zr->hue = ctrl->value;
3662 pict.brightness = zr->brightness;
3663 pict.contrast = zr->contrast;
3664 pict.colour = zr->saturation;
3667 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3669 mutex_unlock(&zr->resource_lock);
3675 case VIDIOC_ENUMSTD:
3677 struct v4l2_standard *std = arg;
3679 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3680 ZR_DEVNAME(zr), std->index);
3682 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3685 int id = std->index;
3686 memset(std, 0, sizeof(*std));
3690 if (std->index == zr->card.norms) {
3691 /* if we have autodetect, ... */
3692 struct video_decoder_capability caps;
3693 decoder_command(zr, DECODER_GET_CAPABILITIES,
3695 if (caps.flags & VIDEO_DECODER_AUTO) {
3696 std->id = V4L2_STD_ALL;
3697 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3702 switch (std->index) {
3704 std->id = V4L2_STD_PAL;
3705 strncpy(std->name, "PAL", sizeof(std->name)-1);
3706 std->frameperiod.numerator = 1;
3707 std->frameperiod.denominator = 25;
3708 std->framelines = zr->card.tvn[0]->Ht;
3711 std->id = V4L2_STD_NTSC;
3712 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3713 std->frameperiod.numerator = 1001;
3714 std->frameperiod.denominator = 30000;
3715 std->framelines = zr->card.tvn[1]->Ht;
3718 std->id = V4L2_STD_SECAM;
3719 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3720 std->frameperiod.numerator = 1;
3721 std->frameperiod.denominator = 25;
3722 std->framelines = zr->card.tvn[2]->Ht;
3732 v4l2_std_id *std = arg;
3735 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3737 mutex_lock(&zr->resource_lock);
3739 mutex_unlock(&zr->resource_lock);
3742 case VIDEO_MODE_PAL:
3743 *std = V4L2_STD_PAL;
3745 case VIDEO_MODE_NTSC:
3746 *std = V4L2_STD_NTSC;
3748 case VIDEO_MODE_SECAM:
3749 *std = V4L2_STD_SECAM;
3759 int norm = -1, res = 0;
3760 v4l2_std_id *std = arg;
3762 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3763 ZR_DEVNAME(zr), (unsigned long long)*std);
3765 if (*std == V4L2_STD_PAL)
3766 norm = VIDEO_MODE_PAL;
3767 else if (*std == V4L2_STD_NTSC)
3768 norm = VIDEO_MODE_NTSC;
3769 else if (*std == V4L2_STD_SECAM)
3770 norm = VIDEO_MODE_SECAM;
3771 else if (*std == V4L2_STD_ALL)
3772 norm = VIDEO_MODE_AUTO;
3776 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3777 ZR_DEVNAME(zr), (unsigned long long)*std);
3781 mutex_lock(&zr->resource_lock);
3782 if ((res = zoran_set_norm(zr, norm)))
3783 goto sstd_unlock_and_return;
3785 res = wait_grab_pending(zr);
3786 sstd_unlock_and_return:
3787 mutex_unlock(&zr->resource_lock);
3792 case VIDIOC_ENUMINPUT:
3794 struct v4l2_input *inp = arg;
3797 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3798 ZR_DEVNAME(zr), inp->index);
3800 if (inp->index < 0 || inp->index >= zr->card.inputs)
3803 int id = inp->index;
3804 memset(inp, 0, sizeof(*inp));
3808 strncpy(inp->name, zr->card.input[inp->index].name,
3809 sizeof(inp->name) - 1);
3810 inp->type = V4L2_INPUT_TYPE_CAMERA;
3811 inp->std = V4L2_STD_ALL;
3813 /* Get status of video decoder */
3814 mutex_lock(&zr->resource_lock);
3815 decoder_command(zr, DECODER_GET_STATUS, &status);
3816 mutex_unlock(&zr->resource_lock);
3818 if (!(status & DECODER_STATUS_GOOD)) {
3819 inp->status |= V4L2_IN_ST_NO_POWER;
3820 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3822 if (!(status & DECODER_STATUS_COLOR))
3823 inp->status |= V4L2_IN_ST_NO_COLOR;
3829 case VIDIOC_G_INPUT:
3833 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3835 mutex_lock(&zr->resource_lock);
3837 mutex_unlock(&zr->resource_lock);
3843 case VIDIOC_S_INPUT:
3845 int *input = arg, res = 0;
3847 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3848 ZR_DEVNAME(zr), *input);
3850 mutex_lock(&zr->resource_lock);
3851 if ((res = zoran_set_input(zr, *input)))
3852 goto sinput_unlock_and_return;
3854 /* Make sure the changes come into effect */
3855 res = wait_grab_pending(zr);
3856 sinput_unlock_and_return:
3857 mutex_unlock(&zr->resource_lock);
3862 case VIDIOC_ENUMOUTPUT:
3864 struct v4l2_output *outp = arg;
3866 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3867 ZR_DEVNAME(zr), outp->index);
3869 if (outp->index != 0)
3872 memset(outp, 0, sizeof(*outp));
3874 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3875 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3881 case VIDIOC_G_OUTPUT:
3885 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3893 case VIDIOC_S_OUTPUT:
3897 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3898 ZR_DEVNAME(zr), *output);
3907 /* cropping (sub-frame capture) */
3908 case VIDIOC_CROPCAP:
3910 struct v4l2_cropcap *cropcap = arg;
3911 int type = cropcap->type, res = 0;
3913 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3914 ZR_DEVNAME(zr), cropcap->type);
3916 memset(cropcap, 0, sizeof(*cropcap));
3917 cropcap->type = type;
3919 mutex_lock(&zr->resource_lock);
3921 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3922 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3923 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3926 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3929 goto cropcap_unlock_and_return;
3932 cropcap->bounds.top = cropcap->bounds.left = 0;
3933 cropcap->bounds.width = BUZ_MAX_WIDTH;
3934 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3935 cropcap->defrect.top = cropcap->defrect.left = 0;
3936 cropcap->defrect.width = BUZ_MIN_WIDTH;
3937 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3938 cropcap_unlock_and_return:
3939 mutex_unlock(&zr->resource_lock);
3946 struct v4l2_crop *crop = arg;
3947 int type = crop->type, res = 0;
3949 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3950 ZR_DEVNAME(zr), crop->type);
3952 memset(crop, 0, sizeof(*crop));
3955 mutex_lock(&zr->resource_lock);
3957 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3958 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3959 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3962 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3965 goto gcrop_unlock_and_return;
3968 crop->c.top = fh->jpg_settings.img_y;
3969 crop->c.left = fh->jpg_settings.img_x;
3970 crop->c.width = fh->jpg_settings.img_width;
3971 crop->c.height = fh->jpg_settings.img_height;
3973 gcrop_unlock_and_return:
3974 mutex_unlock(&zr->resource_lock);
3982 struct v4l2_crop *crop = arg;
3985 settings = fh->jpg_settings;
3989 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3990 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3991 crop->c.width, crop->c.height);
3993 mutex_lock(&zr->resource_lock);
3995 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3998 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
4001 goto scrop_unlock_and_return;
4004 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
4005 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4006 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4009 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4012 goto scrop_unlock_and_return;
4015 /* move into a form that we understand */
4016 settings.img_x = crop->c.left;
4017 settings.img_y = crop->c.top;
4018 settings.img_width = crop->c.width;
4019 settings.img_height = crop->c.height;
4021 /* check validity */
4022 if ((res = zoran_check_jpg_settings(zr, &settings)))
4023 goto scrop_unlock_and_return;
4026 fh->jpg_settings = settings;
4028 scrop_unlock_and_return:
4029 mutex_unlock(&zr->resource_lock);
4034 case VIDIOC_G_JPEGCOMP:
4036 struct v4l2_jpegcompression *params = arg;
4038 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4041 memset(params, 0, sizeof(*params));
4043 mutex_lock(&zr->resource_lock);
4045 params->quality = fh->jpg_settings.jpg_comp.quality;
4046 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4047 memcpy(params->APP_data,
4048 fh->jpg_settings.jpg_comp.APP_data,
4049 fh->jpg_settings.jpg_comp.APP_len);
4050 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4051 memcpy(params->COM_data,
4052 fh->jpg_settings.jpg_comp.COM_data,
4053 fh->jpg_settings.jpg_comp.COM_len);
4054 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4055 params->jpeg_markers =
4056 fh->jpg_settings.jpg_comp.jpeg_markers;
4058 mutex_unlock(&zr->resource_lock);
4064 case VIDIOC_S_JPEGCOMP:
4066 struct v4l2_jpegcompression *params = arg;
4069 settings = fh->jpg_settings;
4073 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4074 ZR_DEVNAME(zr), params->quality, params->APPn,
4075 params->APP_len, params->COM_len);
4077 settings.jpg_comp = *params;
4079 mutex_lock(&zr->resource_lock);
4081 if (fh->v4l_buffers.active != ZORAN_FREE ||
4082 fh->jpg_buffers.active != ZORAN_FREE) {
4085 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4088 goto sjpegc_unlock_and_return;
4091 if ((res = zoran_check_jpg_settings(zr, &settings)))
4092 goto sjpegc_unlock_and_return;
4093 if (!fh->jpg_buffers.allocated)
4094 fh->jpg_buffers.buffer_size =
4095 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4096 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4097 sjpegc_unlock_and_return:
4098 mutex_unlock(&zr->resource_lock);
4104 case VIDIOC_QUERYSTD: /* why is this useful? */
4106 v4l2_std_id *std = arg;
4109 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4110 ZR_DEVNAME(zr), (unsigned long long)*std);
4112 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4113 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4114 zr->card.norms == 3)) {
4122 case VIDIOC_TRY_FMT:
4124 struct v4l2_format *fmt = arg;
4127 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4128 ZR_DEVNAME(zr), fmt->type);
4130 switch (fmt->type) {
4131 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4132 mutex_lock(&zr->resource_lock);
4134 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4135 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4136 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4137 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4138 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4139 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4140 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4141 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4143 mutex_unlock(&zr->resource_lock);
4146 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4147 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4148 if (fmt->fmt.pix.bytesperline > 0)
4151 mutex_lock(&zr->resource_lock);
4153 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4154 settings = fh->jpg_settings;
4156 /* we actually need to set 'real' parameters now */
4157 if ((fmt->fmt.pix.height * 2) >
4159 settings.TmpDcm = 1;
4161 settings.TmpDcm = 2;
4162 settings.decimation = 0;
4163 if (fmt->fmt.pix.height <=
4164 fh->jpg_settings.img_height / 2)
4165 settings.VerDcm = 2;
4167 settings.VerDcm = 1;
4168 if (fmt->fmt.pix.width <=
4169 fh->jpg_settings.img_width / 4)
4170 settings.HorDcm = 4;
4171 else if (fmt->fmt.pix.width <=
4172 fh->jpg_settings.img_width / 2)
4173 settings.HorDcm = 2;
4175 settings.HorDcm = 1;
4176 if (settings.TmpDcm == 1)
4177 settings.field_per_buff = 2;
4179 settings.field_per_buff = 1;
4183 zoran_check_jpg_settings(zr,
4185 goto tryfmt_unlock_and_return;
4187 /* tell the user what we actually did */
4188 fmt->fmt.pix.width =
4189 settings.img_width / settings.HorDcm;
4190 fmt->fmt.pix.height =
4191 settings.img_height * 2 /
4192 (settings.TmpDcm * settings.VerDcm);
4193 if (settings.TmpDcm == 1)
4194 fmt->fmt.pix.field =
4196 odd_even ? V4L2_FIELD_SEQ_TB :
4199 fmt->fmt.pix.field =
4201 odd_even ? V4L2_FIELD_TOP :
4204 fmt->fmt.pix.sizeimage =
4205 zoran_v4l2_calc_bufsize(&settings);
4206 } else if (fmt->type ==
4207 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4210 for (i = 0; i < zoran_num_formats; i++)
4211 if (zoran_formats[i].fourcc ==
4212 fmt->fmt.pix.pixelformat)
4214 if (i == zoran_num_formats) {
4216 goto tryfmt_unlock_and_return;
4219 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4220 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4221 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4222 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4223 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4224 fmt->fmt.pix.height =
4226 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4227 fmt->fmt.pix.height =
4231 goto tryfmt_unlock_and_return;
4233 tryfmt_unlock_and_return:
4234 mutex_unlock(&zr->resource_lock);
4249 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4250 ZR_DEVNAME(zr), cmd);
4251 return -ENOIOCTLCMD;
4260 zoran_ioctl (struct inode *inode,
4265 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4269 zoran_poll (struct file *file,
4272 struct zoran_fh *fh = file->private_data;
4273 struct zoran *zr = fh->zr;
4274 wait_queue_head_t *queue = NULL;
4277 /* we should check whether buffers are ready to be synced on
4278 * (w/o waits - O_NONBLOCK) here
4279 * if ready for read (sync), return POLLIN|POLLRDNORM,
4280 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4281 * if error, return POLLERR,
4282 * if no buffers queued or so, return POLLNVAL
4285 mutex_lock(&zr->resource_lock);
4287 switch (fh->map_mode) {
4288 case ZORAN_MAP_MODE_RAW:
4289 if (fh->v4l_buffers.active == ZORAN_FREE ||
4290 zr->v4l_pend_head == zr->v4l_pend_tail) {
4292 "%s: zoran_poll() - no buffers queued\n",
4295 goto poll_unlock_and_return;
4297 queue = &zr->v4l_capq;
4298 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4299 poll_wait(file, queue, wait);
4300 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4301 res = POLLIN | POLLRDNORM;
4304 case ZORAN_MAP_MODE_JPG_REC:
4305 case ZORAN_MAP_MODE_JPG_PLAY:
4306 if (fh->jpg_buffers.active == ZORAN_FREE ||
4307 zr->jpg_que_head == zr->jpg_que_tail) {
4309 "%s: zoran_poll() - no buffers queued\n",
4312 goto poll_unlock_and_return;
4314 queue = &zr->jpg_capq;
4315 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4316 poll_wait(file, queue, wait);
4317 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4318 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4319 res = POLLIN | POLLRDNORM;
4321 res = POLLOUT | POLLWRNORM;
4327 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4328 ZR_DEVNAME(zr), fh->map_mode);
4330 goto poll_unlock_and_return;
4333 poll_unlock_and_return:
4334 mutex_unlock(&zr->resource_lock);
4341 * This maps the buffers to user space.
4343 * Depending on the state of fh->map_mode
4344 * the V4L or the MJPEG buffers are mapped
4345 * per buffer or all together
4347 * Note that we need to connect to some
4348 * unmap signal event to unmap the de-allocate
4349 * the buffer accordingly (zoran_vm_close())
4353 zoran_vm_open (struct vm_area_struct *vma)
4355 struct zoran_mapping *map = vma->vm_private_data;
4361 zoran_vm_close (struct vm_area_struct *vma)
4363 struct zoran_mapping *map = vma->vm_private_data;
4364 struct file *file = map->file;
4365 struct zoran_fh *fh = file->private_data;
4366 struct zoran *zr = fh->zr;
4370 if (map->count == 0) {
4371 switch (fh->map_mode) {
4372 case ZORAN_MAP_MODE_JPG_REC:
4373 case ZORAN_MAP_MODE_JPG_PLAY:
4375 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4378 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4379 if (fh->jpg_buffers.buffer[i].map == map) {
4380 fh->jpg_buffers.buffer[i].map =
4386 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4387 if (fh->jpg_buffers.buffer[i].map)
4389 if (i == fh->jpg_buffers.num_buffers) {
4390 mutex_lock(&zr->resource_lock);
4392 if (fh->jpg_buffers.active != ZORAN_FREE) {
4393 jpg_qbuf(file, -1, zr->codec_mode);
4394 zr->jpg_buffers.allocated = 0;
4395 zr->jpg_buffers.active =
4396 fh->jpg_buffers.active =
4399 //jpg_fbuffer_free(file);
4400 fh->jpg_buffers.allocated = 0;
4401 fh->jpg_buffers.ready_to_be_freed = 1;
4403 mutex_unlock(&zr->resource_lock);
4408 case ZORAN_MAP_MODE_RAW:
4410 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4413 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4414 if (fh->v4l_buffers.buffer[i].map == map) {
4416 fh->v4l_buffers.buffer[i].map =
4422 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4423 if (fh->v4l_buffers.buffer[i].map)
4425 if (i == fh->v4l_buffers.num_buffers) {
4426 mutex_lock(&zr->resource_lock);
4428 if (fh->v4l_buffers.active != ZORAN_FREE) {
4429 zr36057_set_memgrab(zr, 0);
4430 zr->v4l_buffers.allocated = 0;
4431 zr->v4l_buffers.active =
4432 fh->v4l_buffers.active =
4435 //v4l_fbuffer_free(file);
4436 fh->v4l_buffers.allocated = 0;
4437 fh->v4l_buffers.ready_to_be_freed = 1;
4439 mutex_unlock(&zr->resource_lock);
4446 "%s: munmap() - internal error - unknown map mode %d\n",
4447 ZR_DEVNAME(zr), fh->map_mode);
4454 static struct vm_operations_struct zoran_vm_ops = {
4455 .open = zoran_vm_open,
4456 .close = zoran_vm_close,
4460 zoran_mmap (struct file *file,
4461 struct vm_area_struct *vma)
4463 struct zoran_fh *fh = file->private_data;
4464 struct zoran *zr = fh->zr;
4465 unsigned long size = (vma->vm_end - vma->vm_start);
4466 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4468 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4470 struct zoran_mapping *map;
4474 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4476 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4477 vma->vm_start, vma->vm_end, size);
4479 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4480 !(vma->vm_flags & VM_WRITE)) {
4483 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4488 switch (fh->map_mode) {
4490 case ZORAN_MAP_MODE_JPG_REC:
4491 case ZORAN_MAP_MODE_JPG_PLAY:
4494 mutex_lock(&zr->resource_lock);
4496 /* Map the MJPEG buffers */
4497 if (!fh->jpg_buffers.allocated) {
4500 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4503 goto jpg_mmap_unlock_and_return;
4506 first = offset / fh->jpg_buffers.buffer_size;
4507 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4508 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4509 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4510 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4511 last >= fh->jpg_buffers.num_buffers) {
4514 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4515 ZR_DEVNAME(zr), offset, size,
4516 fh->jpg_buffers.buffer_size,
4517 fh->jpg_buffers.num_buffers);
4519 goto jpg_mmap_unlock_and_return;
4521 for (i = first; i <= last; i++) {
4522 if (fh->jpg_buffers.buffer[i].map) {
4525 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4528 goto jpg_mmap_unlock_and_return;
4532 /* map these buffers (v4l_buffers[i]) */
4533 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4536 goto jpg_mmap_unlock_and_return;
4541 vma->vm_ops = &zoran_vm_ops;
4542 vma->vm_flags |= VM_DONTEXPAND;
4543 vma->vm_private_data = map;
4545 for (i = first; i <= last; i++) {
4547 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4550 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4551 frag_tab[2 * j + 1]) & ~1) << 1;
4556 le32_to_cpu((unsigned long) fh->jpg_buffers.
4557 buffer[i].frag_tab[2 * j]);
4558 /* should just be pos on i386 */
4559 page = virt_to_phys(bus_to_virt(pos))
4561 if (remap_pfn_range(vma, start, page,
4562 todo, PAGE_SHARED)) {
4565 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4568 goto jpg_mmap_unlock_and_return;
4574 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4575 frag_tab[2 * j + 1]) & 1)
4576 break; /* was last fragment */
4578 fh->jpg_buffers.buffer[i].map = map;
4583 jpg_mmap_unlock_and_return:
4584 mutex_unlock(&zr->resource_lock);
4588 case ZORAN_MAP_MODE_RAW:
4590 mutex_lock(&zr->resource_lock);
4592 /* Map the V4L buffers */
4593 if (!fh->v4l_buffers.allocated) {
4596 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4599 goto v4l_mmap_unlock_and_return;
4602 first = offset / fh->v4l_buffers.buffer_size;
4603 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4604 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4605 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4606 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4607 last >= fh->v4l_buffers.buffer_size) {
4610 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4611 ZR_DEVNAME(zr), offset, size,
4612 fh->v4l_buffers.buffer_size,
4613 fh->v4l_buffers.num_buffers);
4615 goto v4l_mmap_unlock_and_return;
4617 for (i = first; i <= last; i++) {
4618 if (fh->v4l_buffers.buffer[i].map) {
4621 "%s: mmap(V4L) - buffer %d already mapped\n",
4624 goto v4l_mmap_unlock_and_return;
4628 /* map these buffers (v4l_buffers[i]) */
4629 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4632 goto v4l_mmap_unlock_and_return;
4637 vma->vm_ops = &zoran_vm_ops;
4638 vma->vm_flags |= VM_DONTEXPAND;
4639 vma->vm_private_data = map;
4641 for (i = first; i <= last; i++) {
4643 if (todo > fh->v4l_buffers.buffer_size)
4644 todo = fh->v4l_buffers.buffer_size;
4645 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4646 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4647 todo, PAGE_SHARED)) {
4650 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4653 goto v4l_mmap_unlock_and_return;
4657 fh->v4l_buffers.buffer[i].map = map;
4661 v4l_mmap_unlock_and_return:
4662 mutex_unlock(&zr->resource_lock);
4669 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4670 ZR_DEVNAME(zr), fh->map_mode);
4677 static struct file_operations zoran_fops = {
4678 .owner = THIS_MODULE,
4680 .release = zoran_close,
4681 .ioctl = zoran_ioctl,
4682 .compat_ioctl = v4l_compat_ioctl32,
4683 .llseek = no_llseek,
4685 .write = zoran_write,
4690 struct video_device zoran_template __devinitdata = {
4692 .type = ZORAN_VID_TYPE,
4694 .type2 = ZORAN_V4L2_VID_FLAGS,
4696 .hardware = ZORAN_HARDWARE,
4697 .fops = &zoran_fops,
4698 .release = &zoran_vdev_release,