2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
55 #include <linux/byteorder/generic.h>
57 #include <linux/interrupt.h>
58 #include <linux/i2c.h>
59 #include <linux/i2c-algo-bit.h>
61 #include <linux/spinlock.h>
62 #define MAP_NR(x) virt_to_page(x)
63 #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
64 #define ZORAN_VID_TYPE ( \
70 VID_TYPE_MJPEG_DECODER | \
71 VID_TYPE_MJPEG_ENCODER \
74 #include <linux/videodev.h>
75 #include <media/v4l2-common.h>
76 #include "videocodec.h"
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
86 #include "zoran_device.h"
87 #include "zoran_card.h"
89 #ifdef CONFIG_VIDEO_V4L2
90 /* we declare some card type definitions here, they mean
91 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
92 #define ZORAN_V4L2_VID_FLAGS ( \
94 V4L2_CAP_VIDEO_CAPTURE |\
95 V4L2_CAP_VIDEO_OUTPUT |\
96 V4L2_CAP_VIDEO_OVERLAY \
100 #include <asm/byteorder.h>
102 const struct zoran_format zoran_formats[] = {
104 .name = "15-bit RGB",
105 .palette = VIDEO_PALETTE_RGB555,
106 #ifdef CONFIG_VIDEO_V4L2
107 #ifdef __LITTLE_ENDIAN
108 .fourcc = V4L2_PIX_FMT_RGB555,
110 .fourcc = V4L2_PIX_FMT_RGB555X,
112 .colorspace = V4L2_COLORSPACE_SRGB,
115 .flags = ZORAN_FORMAT_CAPTURE |
116 ZORAN_FORMAT_OVERLAY,
118 .name = "16-bit RGB",
119 .palette = VIDEO_PALETTE_RGB565,
120 #ifdef CONFIG_VIDEO_V4L2
121 #ifdef __LITTLE_ENDIAN
122 .fourcc = V4L2_PIX_FMT_RGB565,
124 .fourcc = V4L2_PIX_FMT_RGB565X,
126 .colorspace = V4L2_COLORSPACE_SRGB,
129 .flags = ZORAN_FORMAT_CAPTURE |
130 ZORAN_FORMAT_OVERLAY,
132 .name = "24-bit RGB",
133 .palette = VIDEO_PALETTE_RGB24,
134 #ifdef CONFIG_VIDEO_V4L2
135 #ifdef __LITTLE_ENDIAN
136 .fourcc = V4L2_PIX_FMT_BGR24,
138 .fourcc = V4L2_PIX_FMT_RGB24,
140 .colorspace = V4L2_COLORSPACE_SRGB,
143 .flags = ZORAN_FORMAT_CAPTURE |
144 ZORAN_FORMAT_OVERLAY,
146 .name = "32-bit RGB",
147 .palette = VIDEO_PALETTE_RGB32,
148 #ifdef CONFIG_VIDEO_V4L2
149 #ifdef __LITTLE_ENDIAN
150 .fourcc = V4L2_PIX_FMT_BGR32,
152 .fourcc = V4L2_PIX_FMT_RGB32,
154 .colorspace = V4L2_COLORSPACE_SRGB,
157 .flags = ZORAN_FORMAT_CAPTURE |
158 ZORAN_FORMAT_OVERLAY,
160 .name = "4:2:2, packed, YUYV",
161 .palette = VIDEO_PALETTE_YUV422,
162 #ifdef CONFIG_VIDEO_V4L2
163 .fourcc = V4L2_PIX_FMT_YUYV,
164 .colorspace = V4L2_COLORSPACE_SMPTE170M,
167 .flags = ZORAN_FORMAT_CAPTURE |
168 ZORAN_FORMAT_OVERLAY,
170 .name = "Hardware-encoded Motion-JPEG",
172 #ifdef CONFIG_VIDEO_V4L2
173 .fourcc = V4L2_PIX_FMT_MJPEG,
174 .colorspace = V4L2_COLORSPACE_SMPTE170M,
177 .flags = ZORAN_FORMAT_CAPTURE |
178 ZORAN_FORMAT_PLAYBACK |
179 ZORAN_FORMAT_COMPRESSED,
182 static const int zoran_num_formats =
183 (sizeof(zoran_formats) / sizeof(struct zoran_format));
185 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
188 extern int *zr_debug;
190 #define dprintk(num, format, args...) \
192 if (*zr_debug >= num) \
193 printk(format, ##args); \
196 extern int v4l_nbufs;
197 extern int v4l_bufsize;
198 extern int jpg_nbufs;
199 extern int jpg_bufsize;
200 extern int pass_through;
202 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
203 module_param(lock_norm, int, 0);
204 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
206 #ifdef CONFIG_VIDEO_V4L2
207 /* small helper function for calculating buffersizes for v4l2
208 * we calculate the nearest higher power-of-two, which
209 * will be the recommended buffersize */
211 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
213 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
214 __u32 num = (1024 * 512) / (div);
223 if (result > jpg_bufsize)
231 /* forward references */
232 static void v4l_fbuffer_free(struct file *file);
233 static void jpg_fbuffer_free(struct file *file);
236 * Allocate the V4L grab buffers
238 * These have to be pysically contiguous.
239 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
240 * else we try to allocate them with bigphysarea_alloc_pages
241 * if the bigphysarea patch is present in the kernel,
242 * else we try to use high memory (if the user has bootet
243 * Linux with the necessary memory left over).
247 get_high_mem (unsigned long size)
250 * Check if there is usable memory at the end of Linux memory
251 * of at least size. Return the physical address of this memory,
252 * return 0 on failure.
254 * The idea is from Alexandro Rubini's book "Linux device drivers".
255 * The driver from him which is downloadable from O'Reilly's
256 * web site misses the "virt_to_phys(high_memory)" part
257 * (and therefore doesn't work at all - at least with 2.2.x kernels).
259 * It should be unnecessary to mention that THIS IS DANGEROUS,
260 * if more than one driver at a time has the idea to use this memory!!!!
263 volatile unsigned char __iomem *mem;
265 unsigned long hi_mem_ph;
268 /* Map the high memory to user space */
270 hi_mem_ph = virt_to_phys(high_memory);
272 mem = ioremap(hi_mem_ph, size);
275 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
280 for (i = 0; i < size; i++) {
281 /* Check if it is memory */
284 if (readb(mem + i) != c)
288 if (readb(mem + i) != c)
290 writeb(0, mem + i); /* zero out memory */
292 /* give the kernel air to breath */
293 if ((i & 0x3ffff) == 0x3ffff)
302 "%s: get_high_mem() - requested %lu, avail %lu\n",
303 ZORAN_NAME, size, i);
311 v4l_fbuffer_alloc (struct file *file)
313 struct zoran_fh *fh = file->private_data;
314 struct zoran *zr = fh->zr;
317 unsigned long pmem = 0;
319 /* we might have old buffers lying around... */
320 if (fh->v4l_buffers.ready_to_be_freed) {
321 v4l_fbuffer_free(file);
324 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
325 if (fh->v4l_buffers.buffer[i].fbuffer)
328 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
332 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
336 (unsigned char *) kmalloc(fh->v4l_buffers.
342 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
344 v4l_fbuffer_free(file);
347 fh->v4l_buffers.buffer[i].fbuffer = mem;
348 fh->v4l_buffers.buffer[i].fbuffer_phys =
350 fh->v4l_buffers.buffer[i].fbuffer_bus =
352 for (off = 0; off < fh->v4l_buffers.buffer_size;
354 SetPageReserved(MAP_NR(mem + off));
357 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
358 ZR_DEVNAME(zr), i, (unsigned long) mem,
362 /* Use high memory which has been left at boot time */
364 /* Ok., Ok. this is an evil hack - we make
365 * the assumption that physical addresses are
366 * the same as bus addresses (true at least
367 * for Intel processors). The whole method of
368 * obtaining and using this memory is not very
369 * nice - but I hope it saves some poor users
370 * from kernel hacking, which might have even
371 * more evil results */
375 fh->v4l_buffers.num_buffers *
376 fh->v4l_buffers.buffer_size;
378 pmem = get_high_mem(size);
382 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
383 ZR_DEVNAME(zr), size >> 10);
386 fh->v4l_buffers.buffer[0].fbuffer = NULL;
387 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
388 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
391 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
392 ZR_DEVNAME(zr), size >> 10);
394 fh->v4l_buffers.buffer[i].fbuffer = NULL;
395 fh->v4l_buffers.buffer[i].fbuffer_phys =
396 pmem + i * fh->v4l_buffers.buffer_size;
397 fh->v4l_buffers.buffer[i].fbuffer_bus =
398 pmem + i * fh->v4l_buffers.buffer_size;
403 fh->v4l_buffers.allocated = 1;
408 /* free the V4L grab buffers */
410 v4l_fbuffer_free (struct file *file)
412 struct zoran_fh *fh = file->private_data;
413 struct zoran *zr = fh->zr;
417 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
419 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
420 if (!fh->v4l_buffers.buffer[i].fbuffer)
423 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
424 mem = fh->v4l_buffers.buffer[i].fbuffer;
425 for (off = 0; off < fh->v4l_buffers.buffer_size;
427 ClearPageReserved(MAP_NR(mem + off));
428 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
430 fh->v4l_buffers.buffer[i].fbuffer = NULL;
433 fh->v4l_buffers.allocated = 0;
434 fh->v4l_buffers.ready_to_be_freed = 0;
438 * Allocate the MJPEG grab buffers.
440 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
441 * kmalloc is used to request a physically contiguous area,
442 * else we allocate the memory in framgents with get_zeroed_page.
444 * If a Natoma chipset is present and this is a revision 1 zr36057,
445 * each MJPEG buffer needs to be physically contiguous.
446 * (RJ: This statement is from Dave Perks' original driver,
447 * I could never check it because I have a zr36067)
448 * The driver cares about this because it reduces the buffer
449 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
451 * RJ: The contents grab buffers needs never be accessed in the driver.
452 * Therefore there is no need to allocate them with vmalloc in order
453 * to get a contiguous virtual memory space.
454 * I don't understand why many other drivers first allocate them with
455 * vmalloc (which uses internally also get_zeroed_page, but delivers you
456 * virtual addresses) and then again have to make a lot of efforts
457 * to get the physical address.
460 * On big-endian architectures (such as ppc) some extra steps
461 * are needed. When reading and writing to the stat_com array
462 * and fragment buffers, the device expects to see little-
463 * endian values. The use of cpu_to_le32() and le32_to_cpu()
464 * in this function (and one or two others in zoran_device.c)
465 * ensure that these values are always stored in little-endian
466 * form, regardless of architecture. The zr36057 does Very Bad
467 * Things on big endian architectures if the stat_com array
468 * and fragment buffers are not little-endian.
472 jpg_fbuffer_alloc (struct file *file)
474 struct zoran_fh *fh = file->private_data;
475 struct zoran *zr = fh->zr;
479 /* we might have old buffers lying around */
480 if (fh->jpg_buffers.ready_to_be_freed) {
481 jpg_fbuffer_free(file);
484 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
485 if (fh->jpg_buffers.buffer[i].frag_tab)
488 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
491 /* Allocate fragment table for this buffer */
493 mem = get_zeroed_page(GFP_KERNEL);
497 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
499 jpg_fbuffer_free(file);
502 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
503 fh->jpg_buffers.buffer[i].frag_tab_bus =
504 virt_to_bus((void *) mem);
506 //if (alloc_contig) {
507 if (fh->jpg_buffers.need_contiguous) {
509 (unsigned long) kmalloc(fh->jpg_buffers.
515 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
517 jpg_fbuffer_free(file);
520 fh->jpg_buffers.buffer[i].frag_tab[0] =
521 cpu_to_le32(virt_to_bus((void *) mem));
522 fh->jpg_buffers.buffer[i].frag_tab[1] =
523 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
524 for (off = 0; off < fh->jpg_buffers.buffer_size;
526 SetPageReserved(MAP_NR(mem + off));
528 /* jpg_bufsize is allreay page aligned */
530 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
532 mem = get_zeroed_page(GFP_KERNEL);
536 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
538 jpg_fbuffer_free(file);
542 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
543 cpu_to_le32(virt_to_bus((void *) mem));
544 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
546 cpu_to_le32((PAGE_SIZE / 4) << 1);
547 SetPageReserved(MAP_NR(mem));
550 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
555 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
557 (fh->jpg_buffers.num_buffers *
558 fh->jpg_buffers.buffer_size) >> 10);
560 fh->jpg_buffers.allocated = 1;
565 /* free the MJPEG grab buffers */
567 jpg_fbuffer_free (struct file *file)
569 struct zoran_fh *fh = file->private_data;
570 struct zoran *zr = fh->zr;
574 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
576 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
577 if (!fh->jpg_buffers.buffer[i].frag_tab)
580 //if (alloc_contig) {
581 if (fh->jpg_buffers.need_contiguous) {
582 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
583 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
584 fh->jpg_buffers.buffer[i].frag_tab[0]));
586 off < fh->jpg_buffers.buffer_size;
588 ClearPageReserved(MAP_NR
591 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
592 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
596 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
598 if (!fh->jpg_buffers.buffer[i].
601 ClearPageReserved(MAP_NR
605 buffer[i].frag_tab[2 *
607 free_page((unsigned long)
613 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
615 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
620 free_page((unsigned long) fh->jpg_buffers.buffer[i].
622 fh->jpg_buffers.buffer[i].frag_tab = NULL;
625 fh->jpg_buffers.allocated = 0;
626 fh->jpg_buffers.ready_to_be_freed = 0;
630 * V4L Buffer grabbing
634 zoran_v4l_set_format (struct file *file,
637 const struct zoran_format *format)
639 struct zoran_fh *fh = file->private_data;
640 struct zoran *zr = fh->zr;
643 /* Check size and format of the grab wanted */
645 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
646 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
649 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
650 ZR_DEVNAME(zr), width, height);
654 bpp = (format->depth + 7) / 8;
656 /* Check against available buffer size */
657 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
660 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
661 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
665 /* The video front end needs 4-byte alinged line sizes */
667 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
670 "%s: v4l_set_format() - wrong frame alingment\n",
675 fh->v4l_settings.width = width;
676 fh->v4l_settings.height = height;
677 fh->v4l_settings.format = format;
678 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
684 zoran_v4l_queue_frame (struct file *file,
687 struct zoran_fh *fh = file->private_data;
688 struct zoran *zr = fh->zr;
692 if (!fh->v4l_buffers.allocated) {
695 "%s: v4l_queue_frame() - buffers not yet allocated\n",
700 /* No grabbing outside the buffer range! */
701 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
704 "%s: v4l_queue_frame() - buffer %d is out of range\n",
705 ZR_DEVNAME(zr), num);
709 spin_lock_irqsave(&zr->spinlock, flags);
711 if (fh->v4l_buffers.active == ZORAN_FREE) {
712 if (zr->v4l_buffers.active == ZORAN_FREE) {
713 zr->v4l_buffers = fh->v4l_buffers;
714 fh->v4l_buffers.active = ZORAN_ACTIVE;
718 "%s: v4l_queue_frame() - another session is already capturing\n",
724 /* make sure a grab isn't going on currently with this buffer */
726 switch (zr->v4l_buffers.buffer[num].state) {
729 if (zr->v4l_buffers.active == ZORAN_FREE) {
730 fh->v4l_buffers.active = ZORAN_FREE;
731 zr->v4l_buffers.allocated = 0;
733 res = -EBUSY; /* what are you doing? */
738 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
739 ZR_DEVNAME(zr), num);
741 /* since there is at least one unused buffer there's room for at least
742 * one more pend[] entry */
743 zr->v4l_pend[zr->v4l_pend_head++ &
744 V4L_MASK_FRAME] = num;
745 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
746 zr->v4l_buffers.buffer[num].bs.length =
747 fh->v4l_settings.bytesperline *
748 zr->v4l_settings.height;
749 fh->v4l_buffers.buffer[num] =
750 zr->v4l_buffers.buffer[num];
755 spin_unlock_irqrestore(&zr->spinlock, flags);
757 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
758 zr->v4l_buffers.active = fh->v4l_buffers.active;
764 v4l_grab (struct file *file,
765 struct video_mmap *mp)
767 struct zoran_fh *fh = file->private_data;
768 struct zoran *zr = fh->zr;
771 for (i = 0; i < zoran_num_formats; i++) {
772 if (zoran_formats[i].palette == mp->format &&
773 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
774 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
777 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
780 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
786 * To minimize the time spent in the IRQ routine, we avoid setting up
787 * the video front end there.
788 * If this grab has different parameters from a running streaming capture
789 * we stop the streaming capture and start it over again.
791 if (zr->v4l_memgrab_active &&
792 (zr->v4l_settings.width != mp->width ||
793 zr->v4l_settings.height != mp->height ||
794 zr->v4l_settings.format->palette != mp->format)) {
795 res = wait_grab_pending(zr);
799 if ((res = zoran_v4l_set_format(file,
804 zr->v4l_settings = fh->v4l_settings;
806 /* queue the frame in the pending queue */
807 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
808 fh->v4l_buffers.active = ZORAN_FREE;
812 /* put the 36057 into frame grabbing mode */
813 if (!res && !zr->v4l_memgrab_active)
814 zr36057_set_memgrab(zr, 1);
816 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
822 * Sync on a V4L buffer
826 v4l_sync (struct file *file,
829 struct zoran_fh *fh = file->private_data;
830 struct zoran *zr = fh->zr;
833 if (fh->v4l_buffers.active == ZORAN_FREE) {
836 "%s: v4l_sync() - no grab active for this session\n",
841 /* check passed-in frame number */
842 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
844 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
845 ZR_DEVNAME(zr), frame);
849 /* Check if is buffer was queued at all */
850 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
853 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
858 /* wait on this buffer to get ready */
859 if (!wait_event_interruptible_timeout(zr->v4l_capq,
860 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
863 if (signal_pending(current))
866 /* buffer should now be in BUZ_STATE_DONE */
867 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
869 KERN_ERR "%s: v4l_sync() - internal state error\n",
872 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
873 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
875 spin_lock_irqsave(&zr->spinlock, flags);
877 /* Check if streaming capture has finished */
878 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
879 zr36057_set_memgrab(zr, 0);
880 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
881 fh->v4l_buffers.active = zr->v4l_buffers.active =
883 zr->v4l_buffers.allocated = 0;
887 spin_unlock_irqrestore(&zr->spinlock, flags);
893 * Queue a MJPEG buffer for capture/playback
897 zoran_jpg_queue_frame (struct file *file,
899 enum zoran_codec_mode mode)
901 struct zoran_fh *fh = file->private_data;
902 struct zoran *zr = fh->zr;
906 /* Check if buffers are allocated */
907 if (!fh->jpg_buffers.allocated) {
910 "%s: jpg_queue_frame() - buffers not yet allocated\n",
915 /* No grabbing outside the buffer range! */
916 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
919 "%s: jpg_queue_frame() - buffer %d out of range\n",
920 ZR_DEVNAME(zr), num);
924 /* what is the codec mode right now? */
925 if (zr->codec_mode == BUZ_MODE_IDLE) {
926 zr->jpg_settings = fh->jpg_settings;
927 } else if (zr->codec_mode != mode) {
928 /* wrong codec mode active - invalid */
931 "%s: jpg_queue_frame() - codec in wrong mode\n",
936 if (fh->jpg_buffers.active == ZORAN_FREE) {
937 if (zr->jpg_buffers.active == ZORAN_FREE) {
938 zr->jpg_buffers = fh->jpg_buffers;
939 fh->jpg_buffers.active = ZORAN_ACTIVE;
943 "%s: jpg_queue_frame() - another session is already capturing\n",
949 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
950 /* Ok load up the jpeg codec */
951 zr36057_enable_jpg(zr, mode);
954 spin_lock_irqsave(&zr->spinlock, flags);
957 switch (zr->jpg_buffers.buffer[num].state) {
961 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
964 /* since there is at least one unused buffer there's room for at
965 *least one more pend[] entry */
966 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
968 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
969 fh->jpg_buffers.buffer[num] =
970 zr->jpg_buffers.buffer[num];
971 zoran_feed_stat_com(zr);
976 if (zr->jpg_buffers.active == ZORAN_FREE) {
977 fh->jpg_buffers.active = ZORAN_FREE;
978 zr->jpg_buffers.allocated = 0;
980 res = -EBUSY; /* what are you doing? */
985 spin_unlock_irqrestore(&zr->spinlock, flags);
987 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
988 zr->jpg_buffers.active = fh->jpg_buffers.active;
995 jpg_qbuf (struct file *file,
997 enum zoran_codec_mode mode)
999 struct zoran_fh *fh = file->private_data;
1000 struct zoran *zr = fh->zr;
1003 /* Does the user want to stop streaming? */
1005 if (zr->codec_mode == mode) {
1006 if (fh->jpg_buffers.active == ZORAN_FREE) {
1009 "%s: jpg_qbuf(-1) - session not active\n",
1013 fh->jpg_buffers.active = zr->jpg_buffers.active =
1015 zr->jpg_buffers.allocated = 0;
1016 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1021 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1027 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1030 /* Start the jpeg codec when the first frame is queued */
1031 if (!res && zr->jpg_que_head == 1)
1038 * Sync on a MJPEG buffer
1042 jpg_sync (struct file *file,
1043 struct zoran_sync *bs)
1045 struct zoran_fh *fh = file->private_data;
1046 struct zoran *zr = fh->zr;
1047 unsigned long flags;
1050 if (fh->jpg_buffers.active == ZORAN_FREE) {
1053 "%s: jpg_sync() - capture is not currently active\n",
1057 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1058 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1061 "%s: jpg_sync() - codec not in streaming mode\n",
1065 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1066 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1067 zr->jpg_dma_tail == zr->jpg_dma_head),
1071 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1073 zr->codec->control(zr->codec, CODEC_G_STATUS,
1077 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1078 ZR_DEVNAME(zr), isr);
1083 if (signal_pending(current))
1084 return -ERESTARTSYS;
1086 spin_lock_irqsave(&zr->spinlock, flags);
1088 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1089 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1091 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1093 /* buffer should now be in BUZ_STATE_DONE */
1095 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1098 "%s: jpg_sync() - internal state error\n",
1101 *bs = zr->jpg_buffers.buffer[frame].bs;
1103 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1104 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1106 spin_unlock_irqrestore(&zr->spinlock, flags);
1112 zoran_open_init_session (struct file *file)
1115 struct zoran_fh *fh = file->private_data;
1116 struct zoran *zr = fh->zr;
1118 /* Per default, map the V4L Buffers */
1119 fh->map_mode = ZORAN_MAP_MODE_RAW;
1121 /* take over the card's current settings */
1122 fh->overlay_settings = zr->overlay_settings;
1123 fh->overlay_settings.is_set = 0;
1124 fh->overlay_settings.format = zr->overlay_settings.format;
1125 fh->overlay_active = ZORAN_FREE;
1128 fh->v4l_settings = zr->v4l_settings;
1131 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1132 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1133 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1134 fh->v4l_buffers.buffer[i].bs.frame = i;
1136 fh->v4l_buffers.allocated = 0;
1137 fh->v4l_buffers.ready_to_be_freed = 0;
1138 fh->v4l_buffers.active = ZORAN_FREE;
1139 fh->v4l_buffers.buffer_size = v4l_bufsize;
1140 fh->v4l_buffers.num_buffers = v4l_nbufs;
1143 fh->jpg_settings = zr->jpg_settings;
1146 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1147 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1148 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1149 fh->jpg_buffers.buffer[i].bs.frame = i;
1151 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1152 fh->jpg_buffers.allocated = 0;
1153 fh->jpg_buffers.ready_to_be_freed = 0;
1154 fh->jpg_buffers.active = ZORAN_FREE;
1155 fh->jpg_buffers.buffer_size = jpg_bufsize;
1156 fh->jpg_buffers.num_buffers = jpg_nbufs;
1160 zoran_close_end_session (struct file *file)
1162 struct zoran_fh *fh = file->private_data;
1163 struct zoran *zr = fh->zr;
1166 if (fh->overlay_active != ZORAN_FREE) {
1167 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1168 zr->v4l_overlay_active = 0;
1169 if (!zr->v4l_memgrab_active)
1170 zr36057_overlay(zr, 0);
1171 zr->overlay_mask = NULL;
1175 if (fh->v4l_buffers.active != ZORAN_FREE) {
1176 zr36057_set_memgrab(zr, 0);
1177 zr->v4l_buffers.allocated = 0;
1178 zr->v4l_buffers.active = fh->v4l_buffers.active =
1183 if (fh->v4l_buffers.allocated ||
1184 fh->v4l_buffers.ready_to_be_freed) {
1185 v4l_fbuffer_free(file);
1189 if (fh->jpg_buffers.active != ZORAN_FREE) {
1190 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1191 zr->jpg_buffers.allocated = 0;
1192 zr->jpg_buffers.active = fh->jpg_buffers.active =
1197 if (fh->jpg_buffers.allocated ||
1198 fh->jpg_buffers.ready_to_be_freed) {
1199 jpg_fbuffer_free(file);
1204 * Open a zoran card. Right now the flags stuff is just playing
1208 zoran_open (struct inode *inode,
1211 unsigned int minor = iminor(inode);
1212 struct zoran *zr = NULL;
1213 struct zoran_fh *fh;
1214 int i, res, first_open = 0, have_module_locks = 0;
1216 /* find the device */
1217 for (i = 0; i < zoran_num; i++) {
1218 if (zoran[i].video_dev->minor == minor) {
1225 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1227 goto open_unlock_and_return;
1230 /* see fs/device.c - the kernel already locks during open(),
1231 * so locking ourselves only causes deadlocks */
1232 /*mutex_lock(&zr->resource_lock);*/
1236 KERN_ERR "%s: no TV decoder loaded for device!\n",
1239 goto open_unlock_and_return;
1242 /* try to grab a module lock */
1243 if (!try_module_get(THIS_MODULE)) {
1246 "%s: failed to acquire my own lock! PANIC!\n",
1249 goto open_unlock_and_return;
1251 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1254 "%s: failed to grab ownership of i2c decoder\n",
1257 module_put(THIS_MODULE);
1258 goto open_unlock_and_return;
1261 !try_module_get(zr->encoder->driver->driver.owner)) {
1264 "%s: failed to grab ownership of i2c encoder\n",
1267 module_put(zr->decoder->driver->driver.owner);
1268 module_put(THIS_MODULE);
1269 goto open_unlock_and_return;
1272 have_module_locks = 1;
1274 if (zr->user >= 2048) {
1275 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1276 ZR_DEVNAME(zr), zr->user);
1278 goto open_unlock_and_return;
1281 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1282 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1284 /* now, create the open()-specific file_ops struct */
1285 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1289 "%s: zoran_open() - allocation of zoran_fh failed\n",
1292 goto open_unlock_and_return;
1294 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1295 * on norm-change! */
1297 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1298 if (!fh->overlay_mask) {
1301 "%s: zoran_open() - allocation of overlay_mask failed\n",
1305 goto open_unlock_and_return;
1308 if (zr->user++ == 0)
1311 /*mutex_unlock(&zr->resource_lock);*/
1313 /* default setup - TODO: look at flags */
1314 if (first_open) { /* First device open */
1315 zr36057_restart(zr);
1316 zoran_open_init_params(zr);
1317 zoran_init_hardware(zr);
1319 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1322 /* set file_ops stuff */
1323 file->private_data = fh;
1325 zoran_open_init_session(file);
1329 open_unlock_and_return:
1330 /* if we grabbed locks, release them accordingly */
1331 if (have_module_locks) {
1332 module_put(zr->decoder->driver->driver.owner);
1334 module_put(zr->encoder->driver->driver.owner);
1336 module_put(THIS_MODULE);
1339 /* if there's no device found, we didn't obtain the lock either */
1341 /*mutex_unlock(&zr->resource_lock);*/
1348 zoran_close (struct inode *inode,
1351 struct zoran_fh *fh = file->private_data;
1352 struct zoran *zr = fh->zr;
1354 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1355 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1357 /* kernel locks (fs/device.c), so don't do that ourselves
1358 * (prevents deadlocks) */
1359 /*mutex_lock(&zr->resource_lock);*/
1361 zoran_close_end_session(file);
1363 if (zr->user-- == 1) { /* Last process */
1364 /* Clean up JPEG process */
1365 wake_up_interruptible(&zr->jpg_capq);
1366 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1367 zr->jpg_buffers.allocated = 0;
1368 zr->jpg_buffers.active = ZORAN_FREE;
1370 /* disable interrupts */
1371 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1374 print_interrupts(zr);
1377 zr->v4l_overlay_active = 0;
1378 zr36057_overlay(zr, 0);
1379 zr->overlay_mask = NULL;
1382 wake_up_interruptible(&zr->v4l_capq);
1383 zr36057_set_memgrab(zr, 0);
1384 zr->v4l_buffers.allocated = 0;
1385 zr->v4l_buffers.active = ZORAN_FREE;
1386 zoran_set_pci_master(zr, 0);
1388 if (!pass_through) { /* Switch to color bar */
1389 int zero = 0, two = 2;
1390 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1391 encoder_command(zr, ENCODER_SET_INPUT, &two);
1395 file->private_data = NULL;
1396 kfree(fh->overlay_mask);
1399 /* release locks on the i2c modules */
1400 module_put(zr->decoder->driver->driver.owner);
1402 module_put(zr->encoder->driver->driver.owner);
1404 module_put(THIS_MODULE);
1406 /*mutex_unlock(&zr->resource_lock);*/
1408 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1415 zoran_read (struct file *file,
1420 /* we simply don't support read() (yet)... */
1426 zoran_write (struct file *file,
1427 const char __user *data,
1431 /* ...and the same goes for write() */
1437 setup_fbuffer (struct file *file,
1439 const struct zoran_format *fmt,
1444 struct zoran_fh *fh = file->private_data;
1445 struct zoran *zr = fh->zr;
1447 /* (Ronald) v4l/v4l2 guidelines */
1448 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1451 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1452 ALi Magik (that needs very low latency while the card needs a
1453 higher value always) */
1455 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1458 /* we need a bytesperline value, even if not given */
1460 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1463 if (zr->overlay_active) {
1464 /* dzjee... stupid users... don't even bother to turn off
1465 * overlay before changing the memory location...
1466 * normally, we would return errors here. However, one of
1467 * the tools that does this is... xawtv! and since xawtv
1468 * is used by +/- 99% of the users, we'd rather be user-
1469 * friendly and silently do as if nothing went wrong */
1472 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1474 zr36057_overlay(zr, 0);
1478 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1481 "%s: setup_fbuffer() - no valid overlay format given\n",
1485 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1488 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1489 ZR_DEVNAME(zr), width, height, bytesperline);
1492 if (bytesperline & 3) {
1495 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1496 ZR_DEVNAME(zr), bytesperline);
1500 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1501 zr->buffer.height = height;
1502 zr->buffer.width = width;
1503 zr->buffer.depth = fmt->depth;
1504 zr->overlay_settings.format = fmt;
1505 zr->buffer.bytesperline = bytesperline;
1507 /* The user should set new window parameters */
1508 zr->overlay_settings.is_set = 0;
1515 setup_window (struct file *file,
1520 struct video_clip __user *clips,
1522 void __user *bitmap)
1524 struct zoran_fh *fh = file->private_data;
1525 struct zoran *zr = fh->zr;
1526 struct video_clip *vcp = NULL;
1530 if (!zr->buffer.base) {
1533 "%s: setup_window() - frame buffer has to be set first\n",
1538 if (!fh->overlay_settings.format) {
1541 "%s: setup_window() - no overlay format set\n",
1547 * The video front end needs 4-byte alinged line sizes, we correct that
1548 * silently here if necessary
1550 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1551 end = (x + width) & ~1; /* round down */
1552 x = (x + 1) & ~1; /* round up */
1556 if (zr->buffer.depth == 24) {
1557 end = (x + width) & ~3; /* round down */
1558 x = (x + 3) & ~3; /* round up */
1562 if (width > BUZ_MAX_WIDTH)
1563 width = BUZ_MAX_WIDTH;
1564 if (height > BUZ_MAX_HEIGHT)
1565 height = BUZ_MAX_HEIGHT;
1567 /* Check for vaild parameters */
1568 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1569 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1572 "%s: setup_window() - width = %d or height = %d invalid\n",
1573 ZR_DEVNAME(zr), width, height);
1577 fh->overlay_settings.x = x;
1578 fh->overlay_settings.y = y;
1579 fh->overlay_settings.width = width;
1580 fh->overlay_settings.height = height;
1581 fh->overlay_settings.clipcount = clipcount;
1584 * If an overlay is running, we have to switch it off
1585 * and switch it on again in order to get the new settings in effect.
1587 * We also want to avoid that the overlay mask is written
1588 * when an overlay is running.
1591 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1592 zr->overlay_active != ZORAN_FREE &&
1593 fh->overlay_active != ZORAN_FREE;
1595 zr36057_overlay(zr, 0);
1598 * Write the overlay mask if clips are wanted.
1599 * We prefer a bitmap.
1602 /* fake value - it just means we want clips */
1603 fh->overlay_settings.clipcount = 1;
1605 if (copy_from_user(fh->overlay_mask, bitmap,
1606 (width * height + 7) / 8)) {
1609 } else if (clipcount > 0) {
1610 /* write our own bitmap from the clips */
1611 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1615 "%s: setup_window() - Alloc of clip mask failed\n",
1620 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1624 write_overlay_mask(file, vcp, clipcount);
1628 fh->overlay_settings.is_set = 1;
1629 if (fh->overlay_active != ZORAN_FREE &&
1630 zr->overlay_active != ZORAN_FREE)
1631 zr->overlay_settings = fh->overlay_settings;
1634 zr36057_overlay(zr, 1);
1636 /* Make sure the changes come into effect */
1637 return wait_grab_pending(zr);
1641 setup_overlay (struct file *file,
1644 struct zoran_fh *fh = file->private_data;
1645 struct zoran *zr = fh->zr;
1647 /* If there is nothing to do, return immediatly */
1648 if ((on && fh->overlay_active != ZORAN_FREE) ||
1649 (!on && fh->overlay_active == ZORAN_FREE))
1652 /* check whether we're touching someone else's overlay */
1653 if (on && zr->overlay_active != ZORAN_FREE &&
1654 fh->overlay_active == ZORAN_FREE) {
1657 "%s: setup_overlay() - overlay is already active for another session\n",
1661 if (!on && zr->overlay_active != ZORAN_FREE &&
1662 fh->overlay_active == ZORAN_FREE) {
1665 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1671 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1672 zr->v4l_overlay_active = 0;
1673 /* When a grab is running, the video simply
1674 * won't be switched on any more */
1675 if (!zr->v4l_memgrab_active)
1676 zr36057_overlay(zr, 0);
1677 zr->overlay_mask = NULL;
1679 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1682 "%s: setup_overlay() - buffer or window not set\n",
1686 if (!fh->overlay_settings.format) {
1689 "%s: setup_overlay() - no overlay format set\n",
1693 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1694 zr->v4l_overlay_active = 1;
1695 zr->overlay_mask = fh->overlay_mask;
1696 zr->overlay_settings = fh->overlay_settings;
1697 if (!zr->v4l_memgrab_active)
1698 zr36057_overlay(zr, 1);
1699 /* When a grab is running, the video will be
1700 * switched on when grab is finished */
1703 /* Make sure the changes come into effect */
1704 return wait_grab_pending(zr);
1707 #ifdef CONFIG_VIDEO_V4L2
1708 /* get the status of a buffer in the clients buffer queue */
1710 zoran_v4l2_buffer_status (struct file *file,
1711 struct v4l2_buffer *buf,
1714 struct zoran_fh *fh = file->private_data;
1715 struct zoran *zr = fh->zr;
1717 buf->flags = V4L2_BUF_FLAG_MAPPED;
1719 switch (fh->map_mode) {
1720 case ZORAN_MAP_MODE_RAW:
1723 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1724 !fh->v4l_buffers.allocated) {
1727 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1732 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1733 buf->length = fh->v4l_buffers.buffer_size;
1736 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1737 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1738 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1739 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1740 buf->flags |= V4L2_BUF_FLAG_DONE;
1742 fh->v4l_buffers.buffer[num].bs.timestamp;
1744 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1747 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1748 buf->field = V4L2_FIELD_TOP;
1750 buf->field = V4L2_FIELD_INTERLACED;
1754 case ZORAN_MAP_MODE_JPG_REC:
1755 case ZORAN_MAP_MODE_JPG_PLAY:
1758 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1759 !fh->jpg_buffers.allocated) {
1762 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1767 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1768 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1769 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1770 buf->length = fh->jpg_buffers.buffer_size;
1772 /* these variables are only written after frame has been captured */
1773 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1774 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1775 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1777 fh->jpg_buffers.buffer[num].bs.timestamp;
1779 fh->jpg_buffers.buffer[num].bs.length;
1780 buf->flags |= V4L2_BUF_FLAG_DONE;
1782 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1785 /* which fields are these? */
1786 if (fh->jpg_settings.TmpDcm != 1)
1789 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1793 odd_even ? V4L2_FIELD_SEQ_TB :
1802 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1803 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1807 buf->memory = V4L2_MEMORY_MMAP;
1809 buf->m.offset = buf->length * num;
1816 zoran_set_norm (struct zoran *zr,
1817 int norm) /* VIDEO_MODE_* */
1819 int norm_encoder, on;
1821 if (zr->v4l_buffers.active != ZORAN_FREE ||
1822 zr->jpg_buffers.active != ZORAN_FREE) {
1825 "%s: set_norm() called while in playback/capture mode\n",
1830 if (lock_norm && norm != zr->norm) {
1831 if (lock_norm > 1) {
1834 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1840 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1846 if (norm != VIDEO_MODE_AUTO &&
1847 (norm < 0 || norm >= zr->card.norms ||
1848 !zr->card.tvn[norm])) {
1850 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1851 ZR_DEVNAME(zr), norm);
1855 if (norm == VIDEO_MODE_AUTO) {
1858 /* if we have autodetect, ... */
1859 struct video_decoder_capability caps;
1860 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1861 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1862 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1867 decoder_command(zr, DECODER_SET_NORM, &norm);
1869 /* let changes come into effect */
1872 decoder_command(zr, DECODER_GET_STATUS, &status);
1873 if (!(status & DECODER_STATUS_GOOD)) {
1876 "%s: set_norm() - no norm detected\n",
1879 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1883 if (status & DECODER_STATUS_NTSC)
1884 norm = VIDEO_MODE_NTSC;
1885 else if (status & DECODER_STATUS_SECAM)
1886 norm = VIDEO_MODE_SECAM;
1888 norm = VIDEO_MODE_PAL;
1890 zr->timing = zr->card.tvn[norm];
1891 norm_encoder = norm;
1893 /* We switch overlay off and on since a change in the
1894 * norm needs different VFE settings */
1895 on = zr->overlay_active && !zr->v4l_memgrab_active;
1897 zr36057_overlay(zr, 0);
1899 decoder_command(zr, DECODER_SET_NORM, &norm);
1900 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1903 zr36057_overlay(zr, 1);
1905 /* Make sure the changes come into effect */
1912 zoran_set_input (struct zoran *zr,
1917 if (input == zr->input) {
1921 if (zr->v4l_buffers.active != ZORAN_FREE ||
1922 zr->jpg_buffers.active != ZORAN_FREE) {
1925 "%s: set_input() called while in playback/capture mode\n",
1930 if (input < 0 || input >= zr->card.inputs) {
1933 "%s: set_input() - unnsupported input %d\n",
1934 ZR_DEVNAME(zr), input);
1938 realinput = zr->card.input[input].muxsel;
1941 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1951 zoran_do_ioctl (struct inode *inode,
1956 struct zoran_fh *fh = file->private_data;
1957 struct zoran *zr = fh->zr;
1958 /* CAREFUL: used in multiple places here */
1959 struct zoran_jpg_settings settings;
1961 /* we might have older buffers lying around... We don't want
1962 * to wait, but we do want to try cleaning them up ASAP. So
1963 * we try to obtain the lock and free them. If that fails, we
1964 * don't do anything and wait for the next turn. In the end,
1965 * zoran_close() or a new allocation will still free them...
1966 * This is just a 'the sooner the better' extra 'feature'
1968 * We don't free the buffers right on munmap() because that
1969 * causes oopses (kfree() inside munmap() oopses for no
1970 * apparent reason - it's also not reproduceable in any way,
1971 * but moving the free code outside the munmap() handler fixes
1972 * all this... If someone knows why, please explain me (Ronald)
1974 if (mutex_trylock(&zr->resource_lock)) {
1975 /* we obtained it! Let's try to free some things */
1976 if (fh->jpg_buffers.ready_to_be_freed)
1977 jpg_fbuffer_free(file);
1978 if (fh->v4l_buffers.ready_to_be_freed)
1979 v4l_fbuffer_free(file);
1981 mutex_unlock(&zr->resource_lock);
1988 struct video_capability *vcap = arg;
1990 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
1992 memset(vcap, 0, sizeof(struct video_capability));
1993 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
1994 vcap->type = ZORAN_VID_TYPE;
1996 vcap->channels = zr->card.inputs;
1998 mutex_lock(&zr->resource_lock);
1999 vcap->maxwidth = BUZ_MAX_WIDTH;
2000 vcap->maxheight = BUZ_MAX_HEIGHT;
2001 vcap->minwidth = BUZ_MIN_WIDTH;
2002 vcap->minheight = BUZ_MIN_HEIGHT;
2003 mutex_unlock(&zr->resource_lock);
2011 struct video_channel *vchan = arg;
2012 int channel = vchan->channel;
2014 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2015 ZR_DEVNAME(zr), vchan->channel);
2017 memset(vchan, 0, sizeof(struct video_channel));
2018 if (channel > zr->card.inputs || channel < 0) {
2021 "%s: VIDIOCGCHAN on not existing channel %d\n",
2022 ZR_DEVNAME(zr), channel);
2026 strcpy(vchan->name, zr->card.input[channel].name);
2030 vchan->type = VIDEO_TYPE_CAMERA;
2031 mutex_lock(&zr->resource_lock);
2032 vchan->norm = zr->norm;
2033 mutex_unlock(&zr->resource_lock);
2034 vchan->channel = channel;
2040 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2042 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2044 * * The famos BTTV driver has it implemented with a struct video_channel argument
2045 * * and we follow it for compatibility reasons
2047 * * BTW: this is the only way the user can set the norm!
2052 struct video_channel *vchan = arg;
2057 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2058 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2060 mutex_lock(&zr->resource_lock);
2061 if ((res = zoran_set_input(zr, vchan->channel)))
2062 goto schan_unlock_and_return;
2063 if ((res = zoran_set_norm(zr, vchan->norm)))
2064 goto schan_unlock_and_return;
2066 /* Make sure the changes come into effect */
2067 res = wait_grab_pending(zr);
2068 schan_unlock_and_return:
2069 mutex_unlock(&zr->resource_lock);
2076 struct video_picture *vpict = arg;
2078 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2080 memset(vpict, 0, sizeof(struct video_picture));
2081 mutex_lock(&zr->resource_lock);
2082 vpict->hue = zr->hue;
2083 vpict->brightness = zr->brightness;
2084 vpict->contrast = zr->contrast;
2085 vpict->colour = zr->saturation;
2086 if (fh->overlay_settings.format) {
2087 vpict->depth = fh->overlay_settings.format->depth;
2088 vpict->palette = fh->overlay_settings.format->palette;
2092 mutex_unlock(&zr->resource_lock);
2100 struct video_picture *vpict = arg;
2105 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2106 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2107 vpict->colour, vpict->contrast, vpict->depth,
2110 for (i = 0; i < zoran_num_formats; i++) {
2111 const struct zoran_format *fmt = &zoran_formats[i];
2113 if (fmt->palette != -1 &&
2114 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2115 fmt->palette == vpict->palette &&
2116 fmt->depth == vpict->depth)
2119 if (i == zoran_num_formats) {
2122 "%s: VIDIOCSPICT - Invalid palette %d\n",
2123 ZR_DEVNAME(zr), vpict->palette);
2127 mutex_lock(&zr->resource_lock);
2129 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2131 zr->hue = vpict->hue;
2132 zr->contrast = vpict->contrast;
2133 zr->saturation = vpict->colour;
2134 zr->brightness = vpict->brightness;
2136 fh->overlay_settings.format = &zoran_formats[i];
2138 mutex_unlock(&zr->resource_lock);
2148 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2149 ZR_DEVNAME(zr), *on);
2151 mutex_lock(&zr->resource_lock);
2152 res = setup_overlay(file, *on);
2153 mutex_unlock(&zr->resource_lock);
2161 struct video_window *vwin = arg;
2163 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2165 memset(vwin, 0, sizeof(struct video_window));
2166 mutex_lock(&zr->resource_lock);
2167 vwin->x = fh->overlay_settings.x;
2168 vwin->y = fh->overlay_settings.y;
2169 vwin->width = fh->overlay_settings.width;
2170 vwin->height = fh->overlay_settings.height;
2171 mutex_unlock(&zr->resource_lock);
2172 vwin->clipcount = 0;
2179 struct video_window *vwin = arg;
2184 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2185 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2186 vwin->height, vwin->clipcount);
2188 mutex_lock(&zr->resource_lock);
2190 setup_window(file, vwin->x, vwin->y, vwin->width,
2191 vwin->height, vwin->clips,
2192 vwin->clipcount, NULL);
2193 mutex_unlock(&zr->resource_lock);
2201 struct video_buffer *vbuf = arg;
2203 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2205 mutex_lock(&zr->resource_lock);
2207 mutex_unlock(&zr->resource_lock);
2214 struct video_buffer *vbuf = arg;
2219 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2220 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2221 vbuf->height, vbuf->depth, vbuf->bytesperline);
2223 for (i = 0; i < zoran_num_formats; i++)
2224 if (zoran_formats[i].depth == vbuf->depth)
2226 if (i == zoran_num_formats) {
2229 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2230 ZR_DEVNAME(zr), vbuf->depth);
2234 mutex_lock(&zr->resource_lock);
2236 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2237 vbuf->width, vbuf->height,
2238 vbuf->bytesperline);
2239 mutex_unlock(&zr->resource_lock);
2247 int *frame = arg, res;
2249 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2250 ZR_DEVNAME(zr), *frame);
2252 mutex_lock(&zr->resource_lock);
2253 res = v4l_sync(file, *frame);
2254 mutex_unlock(&zr->resource_lock);
2256 zr->v4l_sync_tail++;
2261 case VIDIOCMCAPTURE:
2263 struct video_mmap *vmap = arg;
2268 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2269 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2272 mutex_lock(&zr->resource_lock);
2273 res = v4l_grab(file, vmap);
2274 mutex_unlock(&zr->resource_lock);
2281 struct video_mbuf *vmbuf = arg;
2284 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2287 fh->v4l_buffers.num_buffers *
2288 fh->v4l_buffers.buffer_size;
2289 vmbuf->frames = fh->v4l_buffers.num_buffers;
2290 for (i = 0; i < vmbuf->frames; i++) {
2292 i * fh->v4l_buffers.buffer_size;
2295 mutex_lock(&zr->resource_lock);
2297 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2300 "%s: VIDIOCGMBUF - buffers already allocated\n",
2303 goto v4l1reqbuf_unlock_and_return;
2306 if (v4l_fbuffer_alloc(file)) {
2308 goto v4l1reqbuf_unlock_and_return;
2311 /* The next mmap will map the V4L buffers */
2312 fh->map_mode = ZORAN_MAP_MODE_RAW;
2313 v4l1reqbuf_unlock_and_return:
2314 mutex_unlock(&zr->resource_lock);
2322 struct video_unit *vunit = arg;
2324 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2326 vunit->video = zr->video_dev->minor;
2327 vunit->vbi = VIDEO_NO_UNIT;
2328 vunit->radio = VIDEO_NO_UNIT;
2329 vunit->audio = VIDEO_NO_UNIT;
2330 vunit->teletext = VIDEO_NO_UNIT;
2337 * RJ: In principal we could support subcaptures for V4L grabbing.
2338 * Not even the famous BTTV driver has them, however.
2339 * If there should be a strong demand, one could consider
2340 * to implement them.
2342 case VIDIOCGCAPTURE:
2344 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2350 case VIDIOCSCAPTURE:
2352 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2358 case BUZIOC_G_PARAMS:
2360 struct zoran_params *bparams = arg;
2362 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2364 memset(bparams, 0, sizeof(struct zoran_params));
2365 bparams->major_version = MAJOR_VERSION;
2366 bparams->minor_version = MINOR_VERSION;
2368 mutex_lock(&zr->resource_lock);
2370 bparams->norm = zr->norm;
2371 bparams->input = zr->input;
2373 bparams->decimation = fh->jpg_settings.decimation;
2374 bparams->HorDcm = fh->jpg_settings.HorDcm;
2375 bparams->VerDcm = fh->jpg_settings.VerDcm;
2376 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2377 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2378 bparams->img_x = fh->jpg_settings.img_x;
2379 bparams->img_y = fh->jpg_settings.img_y;
2380 bparams->img_width = fh->jpg_settings.img_width;
2381 bparams->img_height = fh->jpg_settings.img_height;
2382 bparams->odd_even = fh->jpg_settings.odd_even;
2384 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2385 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2386 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2387 memcpy(bparams->APP_data,
2388 fh->jpg_settings.jpg_comp.APP_data,
2389 sizeof(bparams->APP_data));
2390 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2391 memcpy(bparams->COM_data,
2392 fh->jpg_settings.jpg_comp.COM_data,
2393 sizeof(bparams->COM_data));
2394 bparams->jpeg_markers =
2395 fh->jpg_settings.jpg_comp.jpeg_markers;
2397 mutex_unlock(&zr->resource_lock);
2399 bparams->VFIFO_FB = 0;
2405 case BUZIOC_S_PARAMS:
2407 struct zoran_params *bparams = arg;
2410 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2412 settings.decimation = bparams->decimation;
2413 settings.HorDcm = bparams->HorDcm;
2414 settings.VerDcm = bparams->VerDcm;
2415 settings.TmpDcm = bparams->TmpDcm;
2416 settings.field_per_buff = bparams->field_per_buff;
2417 settings.img_x = bparams->img_x;
2418 settings.img_y = bparams->img_y;
2419 settings.img_width = bparams->img_width;
2420 settings.img_height = bparams->img_height;
2421 settings.odd_even = bparams->odd_even;
2423 settings.jpg_comp.quality = bparams->quality;
2424 settings.jpg_comp.APPn = bparams->APPn;
2425 settings.jpg_comp.APP_len = bparams->APP_len;
2426 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2427 sizeof(bparams->APP_data));
2428 settings.jpg_comp.COM_len = bparams->COM_len;
2429 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2430 sizeof(bparams->COM_data));
2431 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2433 mutex_lock(&zr->resource_lock);
2435 if (zr->codec_mode != BUZ_MODE_IDLE) {
2438 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2441 goto sparams_unlock_and_return;
2444 /* Check the params first before overwriting our
2446 if (zoran_check_jpg_settings(zr, &settings)) {
2448 goto sparams_unlock_and_return;
2451 fh->jpg_settings = settings;
2452 sparams_unlock_and_return:
2453 mutex_unlock(&zr->resource_lock);
2459 case BUZIOC_REQBUFS:
2461 struct zoran_requestbuffers *breq = arg;
2466 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2467 ZR_DEVNAME(zr), breq->count, breq->size);
2469 /* Enforce reasonable lower and upper limits */
2470 if (breq->count < 4)
2471 breq->count = 4; /* Could be choosen smaller */
2472 if (breq->count > jpg_nbufs)
2473 breq->count = jpg_nbufs;
2474 breq->size = PAGE_ALIGN(breq->size);
2475 if (breq->size < 8192)
2476 breq->size = 8192; /* Arbitrary */
2477 /* breq->size is limited by 1 page for the stat_com
2478 * tables to a Maximum of 2 MB */
2479 if (breq->size > jpg_bufsize)
2480 breq->size = jpg_bufsize;
2481 if (fh->jpg_buffers.need_contiguous &&
2482 breq->size > MAX_KMALLOC_MEM)
2483 breq->size = MAX_KMALLOC_MEM;
2485 mutex_lock(&zr->resource_lock);
2487 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2490 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2493 goto jpgreqbuf_unlock_and_return;
2496 fh->jpg_buffers.num_buffers = breq->count;
2497 fh->jpg_buffers.buffer_size = breq->size;
2499 if (jpg_fbuffer_alloc(file)) {
2501 goto jpgreqbuf_unlock_and_return;
2504 /* The next mmap will map the MJPEG buffers - could
2505 * also be *_PLAY, but it doesn't matter here */
2506 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2507 jpgreqbuf_unlock_and_return:
2508 mutex_unlock(&zr->resource_lock);
2514 case BUZIOC_QBUF_CAPT:
2516 int *frame = arg, res;
2518 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2519 ZR_DEVNAME(zr), *frame);
2521 mutex_lock(&zr->resource_lock);
2522 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2523 mutex_unlock(&zr->resource_lock);
2529 case BUZIOC_QBUF_PLAY:
2531 int *frame = arg, res;
2533 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2534 ZR_DEVNAME(zr), *frame);
2536 mutex_lock(&zr->resource_lock);
2537 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2538 mutex_unlock(&zr->resource_lock);
2546 struct zoran_sync *bsync = arg;
2549 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2551 mutex_lock(&zr->resource_lock);
2552 res = jpg_sync(file, bsync);
2553 mutex_unlock(&zr->resource_lock);
2559 case BUZIOC_G_STATUS:
2561 struct zoran_status *bstat = arg;
2562 int norm, input, status, res = 0;
2564 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2566 if (zr->codec_mode != BUZ_MODE_IDLE) {
2569 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2574 input = zr->card.input[bstat->input].muxsel;
2575 norm = VIDEO_MODE_AUTO;
2577 mutex_lock(&zr->resource_lock);
2579 if (zr->codec_mode != BUZ_MODE_IDLE) {
2582 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2585 goto gstat_unlock_and_return;
2588 decoder_command(zr, DECODER_SET_INPUT, &input);
2589 decoder_command(zr, DECODER_SET_NORM, &norm);
2591 /* sleep 1 second */
2594 /* Get status of video decoder */
2595 decoder_command(zr, DECODER_GET_STATUS, &status);
2597 /* restore previous input and norm */
2598 input = zr->card.input[zr->input].muxsel;
2599 decoder_command(zr, DECODER_SET_INPUT, &input);
2600 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2601 gstat_unlock_and_return:
2602 mutex_unlock(&zr->resource_lock);
2606 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2607 if (status & DECODER_STATUS_NTSC)
2608 bstat->norm = VIDEO_MODE_NTSC;
2609 else if (status & DECODER_STATUS_SECAM)
2610 bstat->norm = VIDEO_MODE_SECAM;
2612 bstat->norm = VIDEO_MODE_PAL;
2615 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2622 #ifdef CONFIG_VIDEO_V4L2
2624 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2625 * it allows for integrating the JPEG capturing calls inside standard v4l2
2628 case VIDIOC_QUERYCAP:
2630 struct v4l2_capability *cap = arg;
2632 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2634 memset(cap, 0, sizeof(*cap));
2635 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2636 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2637 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2638 pci_name(zr->pci_dev));
2640 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2642 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2648 case VIDIOC_ENUM_FMT:
2650 struct v4l2_fmtdesc *fmt = arg;
2651 int index = fmt->index, num = -1, i, flag = 0, type =
2654 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2655 ZR_DEVNAME(zr), fmt->index);
2657 switch (fmt->type) {
2658 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2659 flag = ZORAN_FORMAT_CAPTURE;
2661 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2662 flag = ZORAN_FORMAT_PLAYBACK;
2664 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2665 flag = ZORAN_FORMAT_OVERLAY;
2670 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2671 ZR_DEVNAME(zr), fmt->type);
2675 for (i = 0; i < zoran_num_formats; i++) {
2676 if (zoran_formats[i].flags & flag)
2678 if (num == fmt->index)
2681 if (fmt->index < 0 /* late, but not too late */ ||
2682 i == zoran_num_formats)
2685 memset(fmt, 0, sizeof(*fmt));
2688 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2689 fmt->pixelformat = zoran_formats[i].fourcc;
2690 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2691 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2699 struct v4l2_format *fmt = arg;
2700 int type = fmt->type;
2702 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2704 memset(fmt, 0, sizeof(*fmt));
2707 switch (fmt->type) {
2708 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2710 mutex_lock(&zr->resource_lock);
2712 fmt->fmt.win.w.left = fh->overlay_settings.x;
2713 fmt->fmt.win.w.top = fh->overlay_settings.y;
2714 fmt->fmt.win.w.width = fh->overlay_settings.width;
2715 fmt->fmt.win.w.height =
2716 fh->overlay_settings.height;
2717 if (fh->overlay_settings.width * 2 >
2719 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2721 fmt->fmt.win.field = V4L2_FIELD_TOP;
2723 mutex_unlock(&zr->resource_lock);
2727 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2728 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2730 mutex_lock(&zr->resource_lock);
2732 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2733 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2735 fmt->fmt.pix.width =
2736 fh->v4l_settings.width;
2737 fmt->fmt.pix.height =
2738 fh->v4l_settings.height;
2739 fmt->fmt.pix.sizeimage =
2740 fh->v4l_buffers.buffer_size;
2741 fmt->fmt.pix.pixelformat =
2742 fh->v4l_settings.format->fourcc;
2743 fmt->fmt.pix.colorspace =
2744 fh->v4l_settings.format->colorspace;
2745 fmt->fmt.pix.bytesperline = 0;
2746 if (BUZ_MAX_HEIGHT <
2747 (fh->v4l_settings.height * 2))
2748 fmt->fmt.pix.field =
2749 V4L2_FIELD_INTERLACED;
2751 fmt->fmt.pix.field =
2756 fmt->fmt.pix.width =
2757 fh->jpg_settings.img_width /
2758 fh->jpg_settings.HorDcm;
2759 fmt->fmt.pix.height =
2760 fh->jpg_settings.img_height /
2761 (fh->jpg_settings.VerDcm *
2762 fh->jpg_settings.TmpDcm);
2763 fmt->fmt.pix.sizeimage =
2764 zoran_v4l2_calc_bufsize(&fh->
2766 fmt->fmt.pix.pixelformat =
2768 if (fh->jpg_settings.TmpDcm == 1)
2769 fmt->fmt.pix.field =
2771 odd_even ? V4L2_FIELD_SEQ_BT :
2774 fmt->fmt.pix.field =
2776 odd_even ? V4L2_FIELD_TOP :
2779 fmt->fmt.pix.bytesperline = 0;
2780 fmt->fmt.pix.colorspace =
2781 V4L2_COLORSPACE_SMPTE170M;
2784 mutex_unlock(&zr->resource_lock);
2791 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2792 ZR_DEVNAME(zr), fmt->type);
2801 struct v4l2_format *fmt = arg;
2805 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2806 ZR_DEVNAME(zr), fmt->type);
2808 switch (fmt->type) {
2809 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2811 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2812 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2813 fmt->fmt.win.w.width,
2814 fmt->fmt.win.w.height,
2815 fmt->fmt.win.clipcount,
2816 fmt->fmt.win.bitmap);
2817 mutex_lock(&zr->resource_lock);
2819 setup_window(file, fmt->fmt.win.w.left,
2821 fmt->fmt.win.w.width,
2822 fmt->fmt.win.w.height,
2823 (struct video_clip __user *)
2825 fmt->fmt.win.clipcount,
2826 fmt->fmt.win.bitmap);
2827 mutex_unlock(&zr->resource_lock);
2831 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2832 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2835 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2836 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2837 fmt->fmt.pix.width, fmt->fmt.pix.height,
2838 fmt->fmt.pix.pixelformat,
2839 (char *) &printformat);
2841 if (fmt->fmt.pix.bytesperline > 0) {
2843 KERN_ERR "%s: bpl not supported\n",
2848 /* we can be requested to do JPEG/raw playback/capture */
2850 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2851 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2852 fmt->fmt.pix.pixelformat ==
2853 V4L2_PIX_FMT_MJPEG))) {
2856 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2857 ZR_DEVNAME(zr), fmt->type,
2858 fmt->fmt.pix.pixelformat,
2859 (char *) &printformat);
2863 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2864 mutex_lock(&zr->resource_lock);
2866 settings = fh->jpg_settings;
2868 if (fh->v4l_buffers.allocated ||
2869 fh->jpg_buffers.allocated) {
2872 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2875 goto sfmtjpg_unlock_and_return;
2878 /* we actually need to set 'real' parameters now */
2879 if ((fmt->fmt.pix.height * 2) >
2881 settings.TmpDcm = 1;
2883 settings.TmpDcm = 2;
2884 settings.decimation = 0;
2885 if (fmt->fmt.pix.height <=
2886 fh->jpg_settings.img_height / 2)
2887 settings.VerDcm = 2;
2889 settings.VerDcm = 1;
2890 if (fmt->fmt.pix.width <=
2891 fh->jpg_settings.img_width / 4)
2892 settings.HorDcm = 4;
2893 else if (fmt->fmt.pix.width <=
2894 fh->jpg_settings.img_width / 2)
2895 settings.HorDcm = 2;
2897 settings.HorDcm = 1;
2898 if (settings.TmpDcm == 1)
2899 settings.field_per_buff = 2;
2901 settings.field_per_buff = 1;
2905 zoran_check_jpg_settings(zr,
2907 goto sfmtjpg_unlock_and_return;
2909 /* it's ok, so set them */
2910 fh->jpg_settings = settings;
2912 /* tell the user what we actually did */
2913 fmt->fmt.pix.width =
2914 settings.img_width / settings.HorDcm;
2915 fmt->fmt.pix.height =
2916 settings.img_height * 2 /
2917 (settings.TmpDcm * settings.VerDcm);
2918 if (settings.TmpDcm == 1)
2919 fmt->fmt.pix.field =
2921 odd_even ? V4L2_FIELD_SEQ_TB :
2924 fmt->fmt.pix.field =
2926 odd_even ? V4L2_FIELD_TOP :
2928 fh->jpg_buffers.buffer_size =
2929 zoran_v4l2_calc_bufsize(&fh->
2931 fmt->fmt.pix.sizeimage =
2932 fh->jpg_buffers.buffer_size;
2934 /* we hereby abuse this variable to show that
2935 * we're gonna do mjpeg capture */
2938 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2939 ZORAN_MAP_MODE_JPG_REC :
2940 ZORAN_MAP_MODE_JPG_PLAY;
2941 sfmtjpg_unlock_and_return:
2942 mutex_unlock(&zr->resource_lock);
2944 for (i = 0; i < zoran_num_formats; i++)
2945 if (fmt->fmt.pix.pixelformat ==
2946 zoran_formats[i].fourcc)
2948 if (i == zoran_num_formats) {
2951 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2953 fmt->fmt.pix.pixelformat,
2954 (char *) &printformat);
2957 mutex_lock(&zr->resource_lock);
2958 if (fh->jpg_buffers.allocated ||
2959 (fh->v4l_buffers.allocated &&
2960 fh->v4l_buffers.active !=
2964 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2967 goto sfmtv4l_unlock_and_return;
2969 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2970 fmt->fmt.pix.height =
2972 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2973 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2976 zoran_v4l_set_format(file,
2983 goto sfmtv4l_unlock_and_return;
2985 /* tell the user the
2986 * results/missing stuff */
2987 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
2989 if (BUZ_MAX_HEIGHT <
2990 (fh->v4l_settings.height * 2))
2991 fmt->fmt.pix.field =
2992 V4L2_FIELD_INTERLACED;
2994 fmt->fmt.pix.field =
2997 fh->map_mode = ZORAN_MAP_MODE_RAW;
2998 sfmtv4l_unlock_and_return:
2999 mutex_unlock(&zr->resource_lock);
3005 dprintk(3, "unsupported\n");
3008 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3009 ZR_DEVNAME(zr), fmt->type);
3019 struct v4l2_framebuffer *fb = arg;
3021 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3023 memset(fb, 0, sizeof(*fb));
3024 mutex_lock(&zr->resource_lock);
3025 fb->base = zr->buffer.base;
3026 fb->fmt.width = zr->buffer.width;
3027 fb->fmt.height = zr->buffer.height;
3028 if (zr->overlay_settings.format) {
3029 fb->fmt.pixelformat =
3030 fh->overlay_settings.format->fourcc;
3032 fb->fmt.bytesperline = zr->buffer.bytesperline;
3033 mutex_unlock(&zr->resource_lock);
3034 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3035 fb->fmt.field = V4L2_FIELD_INTERLACED;
3036 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3037 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3046 struct v4l2_framebuffer *fb = arg;
3047 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3051 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3052 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3053 fb->fmt.bytesperline, fb->fmt.pixelformat,
3054 (char *) &printformat);
3056 for (i = 0; i < zoran_num_formats; i++)
3057 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3059 if (i == zoran_num_formats) {
3062 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3063 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3064 (char *) &printformat);
3068 mutex_lock(&zr->resource_lock);
3070 setup_fbuffer(file, fb->base, &zoran_formats[i],
3071 fb->fmt.width, fb->fmt.height,
3072 fb->fmt.bytesperline);
3073 mutex_unlock(&zr->resource_lock);
3079 case VIDIOC_OVERLAY:
3083 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3084 ZR_DEVNAME(zr), *on);
3086 mutex_lock(&zr->resource_lock);
3087 res = setup_overlay(file, *on);
3088 mutex_unlock(&zr->resource_lock);
3094 case VIDIOC_REQBUFS:
3096 struct v4l2_requestbuffers *req = arg;
3099 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3100 ZR_DEVNAME(zr), req->type);
3102 if (req->memory != V4L2_MEMORY_MMAP) {
3105 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3106 ZR_DEVNAME(zr), req->memory);
3110 mutex_lock(&zr->resource_lock);
3112 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3115 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3118 goto v4l2reqbuf_unlock_and_return;
3121 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3122 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3124 /* control user input */
3127 if (req->count > v4l_nbufs)
3128 req->count = v4l_nbufs;
3129 fh->v4l_buffers.num_buffers = req->count;
3131 if (v4l_fbuffer_alloc(file)) {
3133 goto v4l2reqbuf_unlock_and_return;
3136 /* The next mmap will map the V4L buffers */
3137 fh->map_mode = ZORAN_MAP_MODE_RAW;
3139 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3140 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3142 /* we need to calculate size ourselves now */
3145 if (req->count > jpg_nbufs)
3146 req->count = jpg_nbufs;
3147 fh->jpg_buffers.num_buffers = req->count;
3148 fh->jpg_buffers.buffer_size =
3149 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3151 if (jpg_fbuffer_alloc(file)) {
3153 goto v4l2reqbuf_unlock_and_return;
3156 /* The next mmap will map the MJPEG buffers */
3157 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3158 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3160 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3165 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3166 ZR_DEVNAME(zr), req->type);
3168 goto v4l2reqbuf_unlock_and_return;
3170 v4l2reqbuf_unlock_and_return:
3171 mutex_unlock(&zr->resource_lock);
3177 case VIDIOC_QUERYBUF:
3179 struct v4l2_buffer *buf = arg;
3180 __u32 type = buf->type;
3181 int index = buf->index, res;
3185 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3186 ZR_DEVNAME(zr), buf->index, buf->type);
3188 memset(buf, 0, sizeof(buf));
3192 mutex_lock(&zr->resource_lock);
3193 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3194 mutex_unlock(&zr->resource_lock);
3202 struct v4l2_buffer *buf = arg;
3203 int res = 0, codec_mode, buf_type;
3206 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3207 ZR_DEVNAME(zr), buf->type, buf->index);
3209 mutex_lock(&zr->resource_lock);
3211 switch (fh->map_mode) {
3212 case ZORAN_MAP_MODE_RAW:
3213 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3216 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3217 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3219 goto qbuf_unlock_and_return;
3222 res = zoran_v4l_queue_frame(file, buf->index);
3224 goto qbuf_unlock_and_return;
3225 if (!zr->v4l_memgrab_active &&
3226 fh->v4l_buffers.active == ZORAN_LOCKED)
3227 zr36057_set_memgrab(zr, 1);
3230 case ZORAN_MAP_MODE_JPG_REC:
3231 case ZORAN_MAP_MODE_JPG_PLAY:
3232 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3233 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3234 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3236 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3237 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3240 if (buf->type != buf_type) {
3243 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3244 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3246 goto qbuf_unlock_and_return;
3250 zoran_jpg_queue_frame(file, buf->index,
3253 goto qbuf_unlock_and_return;
3254 if (zr->codec_mode == BUZ_MODE_IDLE &&
3255 fh->jpg_buffers.active == ZORAN_LOCKED) {
3256 zr36057_enable_jpg(zr, codec_mode);
3263 "%s: VIDIOC_QBUF - unsupported type %d\n",
3264 ZR_DEVNAME(zr), buf->type);
3266 goto qbuf_unlock_and_return;
3268 qbuf_unlock_and_return:
3269 mutex_unlock(&zr->resource_lock);
3277 struct v4l2_buffer *buf = arg;
3278 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3280 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3281 ZR_DEVNAME(zr), buf->type);
3283 mutex_lock(&zr->resource_lock);
3285 switch (fh->map_mode) {
3286 case ZORAN_MAP_MODE_RAW:
3287 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3290 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3291 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3293 goto dqbuf_unlock_and_return;
3296 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3297 if (file->f_flags & O_NONBLOCK &&
3298 zr->v4l_buffers.buffer[num].state !=
3301 goto dqbuf_unlock_and_return;
3303 res = v4l_sync(file, num);
3305 goto dqbuf_unlock_and_return;
3307 zr->v4l_sync_tail++;
3308 res = zoran_v4l2_buffer_status(file, buf, num);
3311 case ZORAN_MAP_MODE_JPG_REC:
3312 case ZORAN_MAP_MODE_JPG_PLAY:
3314 struct zoran_sync bs;
3316 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3317 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3319 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3321 if (buf->type != buf_type) {
3324 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3325 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3327 goto dqbuf_unlock_and_return;
3332 jpg_que_tail & BUZ_MASK_FRAME];
3334 if (file->f_flags & O_NONBLOCK &&
3335 zr->jpg_buffers.buffer[num].state !=
3338 goto dqbuf_unlock_and_return;
3340 res = jpg_sync(file, &bs);
3342 goto dqbuf_unlock_and_return;
3344 zoran_v4l2_buffer_status(file, buf, bs.frame);
3351 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3352 ZR_DEVNAME(zr), buf->type);
3354 goto dqbuf_unlock_and_return;
3356 dqbuf_unlock_and_return:
3357 mutex_unlock(&zr->resource_lock);
3363 case VIDIOC_STREAMON:
3367 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3369 mutex_lock(&zr->resource_lock);
3371 switch (fh->map_mode) {
3372 case ZORAN_MAP_MODE_RAW: /* raw capture */
3373 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3374 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3376 goto strmon_unlock_and_return;
3379 zr->v4l_buffers.active = fh->v4l_buffers.active =
3381 zr->v4l_settings = fh->v4l_settings;
3383 zr->v4l_sync_tail = zr->v4l_pend_tail;
3384 if (!zr->v4l_memgrab_active &&
3385 zr->v4l_pend_head != zr->v4l_pend_tail) {
3386 zr36057_set_memgrab(zr, 1);
3390 case ZORAN_MAP_MODE_JPG_REC:
3391 case ZORAN_MAP_MODE_JPG_PLAY:
3392 /* what is the codec mode right now? */
3393 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3394 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3396 goto strmon_unlock_and_return;
3399 zr->jpg_buffers.active = fh->jpg_buffers.active =
3402 if (zr->jpg_que_head != zr->jpg_que_tail) {
3403 /* Start the jpeg codec when the first frame is queued */
3411 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3412 ZR_DEVNAME(zr), fh->map_mode);
3414 goto strmon_unlock_and_return;
3416 strmon_unlock_and_return:
3417 mutex_unlock(&zr->resource_lock);
3423 case VIDIOC_STREAMOFF:
3427 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3429 mutex_lock(&zr->resource_lock);
3431 switch (fh->map_mode) {
3432 case ZORAN_MAP_MODE_RAW: /* raw capture */
3433 if (fh->v4l_buffers.active == ZORAN_FREE &&
3434 zr->v4l_buffers.active != ZORAN_FREE) {
3435 res = -EPERM; /* stay off other's settings! */
3436 goto strmoff_unlock_and_return;
3438 if (zr->v4l_buffers.active == ZORAN_FREE)
3439 goto strmoff_unlock_and_return;
3441 /* unload capture */
3442 if (zr->v4l_memgrab_active)
3443 zr36057_set_memgrab(zr, 0);
3445 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3446 zr->v4l_buffers.buffer[i].state =
3448 fh->v4l_buffers = zr->v4l_buffers;
3450 zr->v4l_buffers.active = fh->v4l_buffers.active =
3453 zr->v4l_grab_seq = 0;
3454 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3455 zr->v4l_sync_tail = 0;
3459 case ZORAN_MAP_MODE_JPG_REC:
3460 case ZORAN_MAP_MODE_JPG_PLAY:
3461 if (fh->jpg_buffers.active == ZORAN_FREE &&
3462 zr->jpg_buffers.active != ZORAN_FREE) {
3463 res = -EPERM; /* stay off other's settings! */
3464 goto strmoff_unlock_and_return;
3466 if (zr->jpg_buffers.active == ZORAN_FREE)
3467 goto strmoff_unlock_and_return;
3472 ZORAN_MAP_MODE_JPG_REC) ?
3473 BUZ_MODE_MOTION_COMPRESS :
3474 BUZ_MODE_MOTION_DECOMPRESS);
3476 goto strmoff_unlock_and_return;
3481 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3482 ZR_DEVNAME(zr), fh->map_mode);
3484 goto strmoff_unlock_and_return;
3486 strmoff_unlock_and_return:
3487 mutex_unlock(&zr->resource_lock);
3493 case VIDIOC_QUERYCTRL:
3495 struct v4l2_queryctrl *ctrl = arg;
3497 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3498 ZR_DEVNAME(zr), ctrl->id);
3500 /* we only support hue/saturation/contrast/brightness */
3501 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3502 ctrl->id > V4L2_CID_HUE)
3506 memset(ctrl, 0, sizeof(*ctrl));
3511 case V4L2_CID_BRIGHTNESS:
3512 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3514 case V4L2_CID_CONTRAST:
3515 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3517 case V4L2_CID_SATURATION:
3518 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3521 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3526 ctrl->maximum = 65535;
3528 ctrl->default_value = 32768;
3529 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3537 struct v4l2_control *ctrl = arg;
3539 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3540 ZR_DEVNAME(zr), ctrl->id);
3542 /* we only support hue/saturation/contrast/brightness */
3543 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3544 ctrl->id > V4L2_CID_HUE)
3547 mutex_lock(&zr->resource_lock);
3549 case V4L2_CID_BRIGHTNESS:
3550 ctrl->value = zr->brightness;
3552 case V4L2_CID_CONTRAST:
3553 ctrl->value = zr->contrast;
3555 case V4L2_CID_SATURATION:
3556 ctrl->value = zr->saturation;
3559 ctrl->value = zr->hue;
3562 mutex_unlock(&zr->resource_lock);
3570 struct v4l2_control *ctrl = arg;
3571 struct video_picture pict;
3573 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3574 ZR_DEVNAME(zr), ctrl->id);
3576 /* we only support hue/saturation/contrast/brightness */
3577 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3578 ctrl->id > V4L2_CID_HUE)
3581 if (ctrl->value < 0 || ctrl->value > 65535) {
3584 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3585 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3589 mutex_lock(&zr->resource_lock);
3591 case V4L2_CID_BRIGHTNESS:
3592 zr->brightness = ctrl->value;
3594 case V4L2_CID_CONTRAST:
3595 zr->contrast = ctrl->value;
3597 case V4L2_CID_SATURATION:
3598 zr->saturation = ctrl->value;
3601 zr->hue = ctrl->value;
3604 pict.brightness = zr->brightness;
3605 pict.contrast = zr->contrast;
3606 pict.colour = zr->saturation;
3609 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3611 mutex_unlock(&zr->resource_lock);
3617 case VIDIOC_ENUMSTD:
3619 struct v4l2_standard *std = arg;
3621 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3622 ZR_DEVNAME(zr), std->index);
3624 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3627 int id = std->index;
3628 memset(std, 0, sizeof(*std));
3632 if (std->index == zr->card.norms) {
3633 /* if we have autodetect, ... */
3634 struct video_decoder_capability caps;
3635 decoder_command(zr, DECODER_GET_CAPABILITIES,
3637 if (caps.flags & VIDEO_DECODER_AUTO) {
3638 std->id = V4L2_STD_ALL;
3639 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3644 switch (std->index) {
3646 std->id = V4L2_STD_PAL;
3647 strncpy(std->name, "PAL", sizeof(std->name)-1);
3648 std->frameperiod.numerator = 1;
3649 std->frameperiod.denominator = 25;
3650 std->framelines = zr->card.tvn[0]->Ht;
3653 std->id = V4L2_STD_NTSC;
3654 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3655 std->frameperiod.numerator = 1001;
3656 std->frameperiod.denominator = 30000;
3657 std->framelines = zr->card.tvn[1]->Ht;
3660 std->id = V4L2_STD_SECAM;
3661 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3662 std->frameperiod.numerator = 1;
3663 std->frameperiod.denominator = 25;
3664 std->framelines = zr->card.tvn[2]->Ht;
3674 v4l2_std_id *std = arg;
3677 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3679 mutex_lock(&zr->resource_lock);
3681 mutex_unlock(&zr->resource_lock);
3684 case VIDEO_MODE_PAL:
3685 *std = V4L2_STD_PAL;
3687 case VIDEO_MODE_NTSC:
3688 *std = V4L2_STD_NTSC;
3690 case VIDEO_MODE_SECAM:
3691 *std = V4L2_STD_SECAM;
3701 int norm = -1, res = 0;
3702 v4l2_std_id *std = arg;
3704 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3705 ZR_DEVNAME(zr), (unsigned long long)*std);
3707 if (*std == V4L2_STD_PAL)
3708 norm = VIDEO_MODE_PAL;
3709 else if (*std == V4L2_STD_NTSC)
3710 norm = VIDEO_MODE_NTSC;
3711 else if (*std == V4L2_STD_SECAM)
3712 norm = VIDEO_MODE_SECAM;
3713 else if (*std == V4L2_STD_ALL)
3714 norm = VIDEO_MODE_AUTO;
3718 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3719 ZR_DEVNAME(zr), (unsigned long long)*std);
3723 mutex_lock(&zr->resource_lock);
3724 if ((res = zoran_set_norm(zr, norm)))
3725 goto sstd_unlock_and_return;
3727 res = wait_grab_pending(zr);
3728 sstd_unlock_and_return:
3729 mutex_unlock(&zr->resource_lock);
3734 case VIDIOC_ENUMINPUT:
3736 struct v4l2_input *inp = arg;
3739 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3740 ZR_DEVNAME(zr), inp->index);
3742 if (inp->index < 0 || inp->index >= zr->card.inputs)
3745 int id = inp->index;
3746 memset(inp, 0, sizeof(*inp));
3750 strncpy(inp->name, zr->card.input[inp->index].name,
3751 sizeof(inp->name) - 1);
3752 inp->type = V4L2_INPUT_TYPE_CAMERA;
3753 inp->std = V4L2_STD_ALL;
3755 /* Get status of video decoder */
3756 mutex_lock(&zr->resource_lock);
3757 decoder_command(zr, DECODER_GET_STATUS, &status);
3758 mutex_unlock(&zr->resource_lock);
3760 if (!(status & DECODER_STATUS_GOOD)) {
3761 inp->status |= V4L2_IN_ST_NO_POWER;
3762 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3764 if (!(status & DECODER_STATUS_COLOR))
3765 inp->status |= V4L2_IN_ST_NO_COLOR;
3771 case VIDIOC_G_INPUT:
3775 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3777 mutex_lock(&zr->resource_lock);
3779 mutex_unlock(&zr->resource_lock);
3785 case VIDIOC_S_INPUT:
3787 int *input = arg, res = 0;
3789 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3790 ZR_DEVNAME(zr), *input);
3792 mutex_lock(&zr->resource_lock);
3793 if ((res = zoran_set_input(zr, *input)))
3794 goto sinput_unlock_and_return;
3796 /* Make sure the changes come into effect */
3797 res = wait_grab_pending(zr);
3798 sinput_unlock_and_return:
3799 mutex_unlock(&zr->resource_lock);
3804 case VIDIOC_ENUMOUTPUT:
3806 struct v4l2_output *outp = arg;
3808 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3809 ZR_DEVNAME(zr), outp->index);
3811 if (outp->index != 0)
3814 memset(outp, 0, sizeof(*outp));
3816 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3817 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3823 case VIDIOC_G_OUTPUT:
3827 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3835 case VIDIOC_S_OUTPUT:
3839 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3840 ZR_DEVNAME(zr), *output);
3849 /* cropping (sub-frame capture) */
3850 case VIDIOC_CROPCAP:
3852 struct v4l2_cropcap *cropcap = arg;
3853 int type = cropcap->type, res = 0;
3855 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3856 ZR_DEVNAME(zr), cropcap->type);
3858 memset(cropcap, 0, sizeof(*cropcap));
3859 cropcap->type = type;
3861 mutex_lock(&zr->resource_lock);
3863 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3864 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3865 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3868 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3871 goto cropcap_unlock_and_return;
3874 cropcap->bounds.top = cropcap->bounds.left = 0;
3875 cropcap->bounds.width = BUZ_MAX_WIDTH;
3876 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3877 cropcap->defrect.top = cropcap->defrect.left = 0;
3878 cropcap->defrect.width = BUZ_MIN_WIDTH;
3879 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3880 cropcap_unlock_and_return:
3881 mutex_unlock(&zr->resource_lock);
3888 struct v4l2_crop *crop = arg;
3889 int type = crop->type, res = 0;
3891 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3892 ZR_DEVNAME(zr), crop->type);
3894 memset(crop, 0, sizeof(*crop));
3897 mutex_lock(&zr->resource_lock);
3899 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3900 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3901 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3904 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3907 goto gcrop_unlock_and_return;
3910 crop->c.top = fh->jpg_settings.img_y;
3911 crop->c.left = fh->jpg_settings.img_x;
3912 crop->c.width = fh->jpg_settings.img_width;
3913 crop->c.height = fh->jpg_settings.img_height;
3915 gcrop_unlock_and_return:
3916 mutex_unlock(&zr->resource_lock);
3924 struct v4l2_crop *crop = arg;
3927 settings = fh->jpg_settings;
3931 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3932 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3933 crop->c.width, crop->c.height);
3935 mutex_lock(&zr->resource_lock);
3937 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3940 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3943 goto scrop_unlock_and_return;
3946 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3947 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3948 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3951 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3954 goto scrop_unlock_and_return;
3957 /* move into a form that we understand */
3958 settings.img_x = crop->c.left;
3959 settings.img_y = crop->c.top;
3960 settings.img_width = crop->c.width;
3961 settings.img_height = crop->c.height;
3963 /* check validity */
3964 if ((res = zoran_check_jpg_settings(zr, &settings)))
3965 goto scrop_unlock_and_return;
3968 fh->jpg_settings = settings;
3970 scrop_unlock_and_return:
3971 mutex_unlock(&zr->resource_lock);
3976 case VIDIOC_G_JPEGCOMP:
3978 struct v4l2_jpegcompression *params = arg;
3980 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
3983 memset(params, 0, sizeof(*params));
3985 mutex_lock(&zr->resource_lock);
3987 params->quality = fh->jpg_settings.jpg_comp.quality;
3988 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3989 memcpy(params->APP_data,
3990 fh->jpg_settings.jpg_comp.APP_data,
3991 fh->jpg_settings.jpg_comp.APP_len);
3992 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3993 memcpy(params->COM_data,
3994 fh->jpg_settings.jpg_comp.COM_data,
3995 fh->jpg_settings.jpg_comp.COM_len);
3996 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3997 params->jpeg_markers =
3998 fh->jpg_settings.jpg_comp.jpeg_markers;
4000 mutex_unlock(&zr->resource_lock);
4006 case VIDIOC_S_JPEGCOMP:
4008 struct v4l2_jpegcompression *params = arg;
4011 settings = fh->jpg_settings;
4015 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4016 ZR_DEVNAME(zr), params->quality, params->APPn,
4017 params->APP_len, params->COM_len);
4019 settings.jpg_comp = *params;
4021 mutex_lock(&zr->resource_lock);
4023 if (fh->v4l_buffers.active != ZORAN_FREE ||
4024 fh->jpg_buffers.active != ZORAN_FREE) {
4027 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4030 goto sjpegc_unlock_and_return;
4033 if ((res = zoran_check_jpg_settings(zr, &settings)))
4034 goto sjpegc_unlock_and_return;
4035 if (!fh->jpg_buffers.allocated)
4036 fh->jpg_buffers.buffer_size =
4037 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4038 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4039 sjpegc_unlock_and_return:
4040 mutex_unlock(&zr->resource_lock);
4046 case VIDIOC_QUERYSTD: /* why is this useful? */
4048 v4l2_std_id *std = arg;
4051 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4052 ZR_DEVNAME(zr), (unsigned long long)*std);
4054 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4055 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4056 zr->card.norms == 3)) {
4064 case VIDIOC_TRY_FMT:
4066 struct v4l2_format *fmt = arg;
4069 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4070 ZR_DEVNAME(zr), fmt->type);
4072 switch (fmt->type) {
4073 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4074 mutex_lock(&zr->resource_lock);
4076 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4077 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4078 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4079 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4080 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4081 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4082 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4083 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4085 mutex_unlock(&zr->resource_lock);
4088 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4089 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4090 if (fmt->fmt.pix.bytesperline > 0)
4093 mutex_lock(&zr->resource_lock);
4095 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4096 settings = fh->jpg_settings;
4098 /* we actually need to set 'real' parameters now */
4099 if ((fmt->fmt.pix.height * 2) >
4101 settings.TmpDcm = 1;
4103 settings.TmpDcm = 2;
4104 settings.decimation = 0;
4105 if (fmt->fmt.pix.height <=
4106 fh->jpg_settings.img_height / 2)
4107 settings.VerDcm = 2;
4109 settings.VerDcm = 1;
4110 if (fmt->fmt.pix.width <=
4111 fh->jpg_settings.img_width / 4)
4112 settings.HorDcm = 4;
4113 else if (fmt->fmt.pix.width <=
4114 fh->jpg_settings.img_width / 2)
4115 settings.HorDcm = 2;
4117 settings.HorDcm = 1;
4118 if (settings.TmpDcm == 1)
4119 settings.field_per_buff = 2;
4121 settings.field_per_buff = 1;
4125 zoran_check_jpg_settings(zr,
4127 goto tryfmt_unlock_and_return;
4129 /* tell the user what we actually did */
4130 fmt->fmt.pix.width =
4131 settings.img_width / settings.HorDcm;
4132 fmt->fmt.pix.height =
4133 settings.img_height * 2 /
4134 (settings.TmpDcm * settings.VerDcm);
4135 if (settings.TmpDcm == 1)
4136 fmt->fmt.pix.field =
4138 odd_even ? V4L2_FIELD_SEQ_TB :
4141 fmt->fmt.pix.field =
4143 odd_even ? V4L2_FIELD_TOP :
4146 fmt->fmt.pix.sizeimage =
4147 zoran_v4l2_calc_bufsize(&settings);
4148 } else if (fmt->type ==
4149 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4152 for (i = 0; i < zoran_num_formats; i++)
4153 if (zoran_formats[i].fourcc ==
4154 fmt->fmt.pix.pixelformat)
4156 if (i == zoran_num_formats) {
4158 goto tryfmt_unlock_and_return;
4161 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4162 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4163 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4164 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4165 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4166 fmt->fmt.pix.height =
4168 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4169 fmt->fmt.pix.height =
4173 goto tryfmt_unlock_and_return;
4175 tryfmt_unlock_and_return:
4176 mutex_unlock(&zr->resource_lock);
4191 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4192 ZR_DEVNAME(zr), cmd);
4193 return -ENOIOCTLCMD;
4202 zoran_ioctl (struct inode *inode,
4207 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4211 zoran_poll (struct file *file,
4214 struct zoran_fh *fh = file->private_data;
4215 struct zoran *zr = fh->zr;
4216 wait_queue_head_t *queue = NULL;
4219 /* we should check whether buffers are ready to be synced on
4220 * (w/o waits - O_NONBLOCK) here
4221 * if ready for read (sync), return POLLIN|POLLRDNORM,
4222 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4223 * if error, return POLLERR,
4224 * if no buffers queued or so, return POLLNVAL
4227 mutex_lock(&zr->resource_lock);
4229 switch (fh->map_mode) {
4230 case ZORAN_MAP_MODE_RAW:
4231 if (fh->v4l_buffers.active == ZORAN_FREE ||
4232 zr->v4l_pend_head == zr->v4l_pend_tail) {
4234 "%s: zoran_poll() - no buffers queued\n",
4237 goto poll_unlock_and_return;
4239 queue = &zr->v4l_capq;
4240 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4241 poll_wait(file, queue, wait);
4242 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4243 res = POLLIN | POLLRDNORM;
4246 case ZORAN_MAP_MODE_JPG_REC:
4247 case ZORAN_MAP_MODE_JPG_PLAY:
4248 if (fh->jpg_buffers.active == ZORAN_FREE ||
4249 zr->jpg_que_head == zr->jpg_que_tail) {
4251 "%s: zoran_poll() - no buffers queued\n",
4254 goto poll_unlock_and_return;
4256 queue = &zr->jpg_capq;
4257 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4258 poll_wait(file, queue, wait);
4259 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4260 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4261 res = POLLIN | POLLRDNORM;
4263 res = POLLOUT | POLLWRNORM;
4269 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4270 ZR_DEVNAME(zr), fh->map_mode);
4272 goto poll_unlock_and_return;
4275 poll_unlock_and_return:
4276 mutex_unlock(&zr->resource_lock);
4283 * This maps the buffers to user space.
4285 * Depending on the state of fh->map_mode
4286 * the V4L or the MJPEG buffers are mapped
4287 * per buffer or all together
4289 * Note that we need to connect to some
4290 * unmap signal event to unmap the de-allocate
4291 * the buffer accordingly (zoran_vm_close())
4295 zoran_vm_open (struct vm_area_struct *vma)
4297 struct zoran_mapping *map = vma->vm_private_data;
4303 zoran_vm_close (struct vm_area_struct *vma)
4305 struct zoran_mapping *map = vma->vm_private_data;
4306 struct file *file = map->file;
4307 struct zoran_fh *fh = file->private_data;
4308 struct zoran *zr = fh->zr;
4312 if (map->count == 0) {
4313 switch (fh->map_mode) {
4314 case ZORAN_MAP_MODE_JPG_REC:
4315 case ZORAN_MAP_MODE_JPG_PLAY:
4317 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4320 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4321 if (fh->jpg_buffers.buffer[i].map == map) {
4322 fh->jpg_buffers.buffer[i].map =
4328 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4329 if (fh->jpg_buffers.buffer[i].map)
4331 if (i == fh->jpg_buffers.num_buffers) {
4332 mutex_lock(&zr->resource_lock);
4334 if (fh->jpg_buffers.active != ZORAN_FREE) {
4335 jpg_qbuf(file, -1, zr->codec_mode);
4336 zr->jpg_buffers.allocated = 0;
4337 zr->jpg_buffers.active =
4338 fh->jpg_buffers.active =
4341 //jpg_fbuffer_free(file);
4342 fh->jpg_buffers.allocated = 0;
4343 fh->jpg_buffers.ready_to_be_freed = 1;
4345 mutex_unlock(&zr->resource_lock);
4350 case ZORAN_MAP_MODE_RAW:
4352 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4355 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4356 if (fh->v4l_buffers.buffer[i].map == map) {
4358 fh->v4l_buffers.buffer[i].map =
4364 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4365 if (fh->v4l_buffers.buffer[i].map)
4367 if (i == fh->v4l_buffers.num_buffers) {
4368 mutex_lock(&zr->resource_lock);
4370 if (fh->v4l_buffers.active != ZORAN_FREE) {
4371 zr36057_set_memgrab(zr, 0);
4372 zr->v4l_buffers.allocated = 0;
4373 zr->v4l_buffers.active =
4374 fh->v4l_buffers.active =
4377 //v4l_fbuffer_free(file);
4378 fh->v4l_buffers.allocated = 0;
4379 fh->v4l_buffers.ready_to_be_freed = 1;
4381 mutex_unlock(&zr->resource_lock);
4388 "%s: munmap() - internal error - unknown map mode %d\n",
4389 ZR_DEVNAME(zr), fh->map_mode);
4396 static struct vm_operations_struct zoran_vm_ops = {
4397 .open = zoran_vm_open,
4398 .close = zoran_vm_close,
4402 zoran_mmap (struct file *file,
4403 struct vm_area_struct *vma)
4405 struct zoran_fh *fh = file->private_data;
4406 struct zoran *zr = fh->zr;
4407 unsigned long size = (vma->vm_end - vma->vm_start);
4408 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4410 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4412 struct zoran_mapping *map;
4416 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4418 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4419 vma->vm_start, vma->vm_end, size);
4421 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4422 !(vma->vm_flags & VM_WRITE)) {
4425 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4430 switch (fh->map_mode) {
4432 case ZORAN_MAP_MODE_JPG_REC:
4433 case ZORAN_MAP_MODE_JPG_PLAY:
4436 mutex_lock(&zr->resource_lock);
4438 /* Map the MJPEG buffers */
4439 if (!fh->jpg_buffers.allocated) {
4442 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4445 goto jpg_mmap_unlock_and_return;
4448 first = offset / fh->jpg_buffers.buffer_size;
4449 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4450 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4451 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4452 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4453 last >= fh->jpg_buffers.num_buffers) {
4456 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4457 ZR_DEVNAME(zr), offset, size,
4458 fh->jpg_buffers.buffer_size,
4459 fh->jpg_buffers.num_buffers);
4461 goto jpg_mmap_unlock_and_return;
4463 for (i = first; i <= last; i++) {
4464 if (fh->jpg_buffers.buffer[i].map) {
4467 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4470 goto jpg_mmap_unlock_and_return;
4474 /* map these buffers (v4l_buffers[i]) */
4475 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4478 goto jpg_mmap_unlock_and_return;
4483 vma->vm_ops = &zoran_vm_ops;
4484 vma->vm_flags |= VM_DONTEXPAND;
4485 vma->vm_private_data = map;
4487 for (i = first; i <= last; i++) {
4489 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4492 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4493 frag_tab[2 * j + 1]) & ~1) << 1;
4498 le32_to_cpu((unsigned long) fh->jpg_buffers.
4499 buffer[i].frag_tab[2 * j]);
4500 /* should just be pos on i386 */
4501 page = virt_to_phys(bus_to_virt(pos))
4503 if (remap_pfn_range(vma, start, page,
4504 todo, PAGE_SHARED)) {
4507 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4510 goto jpg_mmap_unlock_and_return;
4516 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4517 frag_tab[2 * j + 1]) & 1)
4518 break; /* was last fragment */
4520 fh->jpg_buffers.buffer[i].map = map;
4525 jpg_mmap_unlock_and_return:
4526 mutex_unlock(&zr->resource_lock);
4530 case ZORAN_MAP_MODE_RAW:
4532 mutex_lock(&zr->resource_lock);
4534 /* Map the V4L buffers */
4535 if (!fh->v4l_buffers.allocated) {
4538 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4541 goto v4l_mmap_unlock_and_return;
4544 first = offset / fh->v4l_buffers.buffer_size;
4545 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4546 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4547 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4548 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4549 last >= fh->v4l_buffers.buffer_size) {
4552 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4553 ZR_DEVNAME(zr), offset, size,
4554 fh->v4l_buffers.buffer_size,
4555 fh->v4l_buffers.num_buffers);
4557 goto v4l_mmap_unlock_and_return;
4559 for (i = first; i <= last; i++) {
4560 if (fh->v4l_buffers.buffer[i].map) {
4563 "%s: mmap(V4L) - buffer %d already mapped\n",
4566 goto v4l_mmap_unlock_and_return;
4570 /* map these buffers (v4l_buffers[i]) */
4571 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4574 goto v4l_mmap_unlock_and_return;
4579 vma->vm_ops = &zoran_vm_ops;
4580 vma->vm_flags |= VM_DONTEXPAND;
4581 vma->vm_private_data = map;
4583 for (i = first; i <= last; i++) {
4585 if (todo > fh->v4l_buffers.buffer_size)
4586 todo = fh->v4l_buffers.buffer_size;
4587 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4588 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4589 todo, PAGE_SHARED)) {
4592 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4595 goto v4l_mmap_unlock_and_return;
4599 fh->v4l_buffers.buffer[i].map = map;
4603 v4l_mmap_unlock_and_return:
4604 mutex_unlock(&zr->resource_lock);
4611 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4612 ZR_DEVNAME(zr), fh->map_mode);
4619 static const struct file_operations zoran_fops = {
4620 .owner = THIS_MODULE,
4622 .release = zoran_close,
4623 .ioctl = zoran_ioctl,
4624 .compat_ioctl = v4l_compat_ioctl32,
4625 .llseek = no_llseek,
4627 .write = zoran_write,
4632 struct video_device zoran_template __devinitdata = {
4634 .type = ZORAN_VID_TYPE,
4635 #ifdef CONFIG_VIDEO_V4L2
4636 .type2 = ZORAN_V4L2_VID_FLAGS,
4638 .hardware = ZORAN_HARDWARE,
4639 .fops = &zoran_fops,
4640 .release = &zoran_vdev_release,