2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 #include <linux/config.h>
48 #include <linux/version.h>
49 #include <linux/init.h>
50 #include <linux/module.h>
51 #include <linux/delay.h>
52 #include <linux/slab.h>
53 #include <linux/pci.h>
54 #include <linux/vmalloc.h>
55 #include <linux/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 "videocodec.h"
78 #include <asm/uaccess.h>
79 #include <linux/proc_fs.h>
81 #include <linux/video_decoder.h>
82 #include <linux/video_encoder.h>
84 #include "zoran_device.h"
85 #include "zoran_card.h"
88 /* we declare some card type definitions here, they mean
89 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
90 #define ZORAN_V4L2_VID_FLAGS ( \
92 V4L2_CAP_VIDEO_CAPTURE |\
93 V4L2_CAP_VIDEO_OUTPUT |\
94 V4L2_CAP_VIDEO_OVERLAY \
98 #include <asm/byteorder.h>
100 const struct zoran_format zoran_formats[] = {
102 .name = "15-bit RGB",
103 .palette = VIDEO_PALETTE_RGB555,
105 #ifdef __LITTLE_ENDIAN
106 .fourcc = V4L2_PIX_FMT_RGB555,
108 .fourcc = V4L2_PIX_FMT_RGB555X,
110 .colorspace = V4L2_COLORSPACE_SRGB,
113 .flags = ZORAN_FORMAT_CAPTURE |
114 ZORAN_FORMAT_OVERLAY,
116 .name = "16-bit RGB",
117 .palette = VIDEO_PALETTE_RGB565,
119 #ifdef __LITTLE_ENDIAN
120 .fourcc = V4L2_PIX_FMT_RGB565,
122 .fourcc = V4L2_PIX_FMT_RGB565X,
124 .colorspace = V4L2_COLORSPACE_SRGB,
127 .flags = ZORAN_FORMAT_CAPTURE |
128 ZORAN_FORMAT_OVERLAY,
130 .name = "24-bit RGB",
131 .palette = VIDEO_PALETTE_RGB24,
133 #ifdef __LITTLE_ENDIAN
134 .fourcc = V4L2_PIX_FMT_BGR24,
136 .fourcc = V4L2_PIX_FMT_RGB24,
138 .colorspace = V4L2_COLORSPACE_SRGB,
141 .flags = ZORAN_FORMAT_CAPTURE |
142 ZORAN_FORMAT_OVERLAY,
144 .name = "32-bit RGB",
145 .palette = VIDEO_PALETTE_RGB32,
147 #ifdef __LITTLE_ENDIAN
148 .fourcc = V4L2_PIX_FMT_BGR32,
150 .fourcc = V4L2_PIX_FMT_RGB32,
152 .colorspace = V4L2_COLORSPACE_SRGB,
155 .flags = ZORAN_FORMAT_CAPTURE |
156 ZORAN_FORMAT_OVERLAY,
158 .name = "4:2:2, packed, YUYV",
159 .palette = VIDEO_PALETTE_YUV422,
161 .fourcc = V4L2_PIX_FMT_YUYV,
162 .colorspace = V4L2_COLORSPACE_SMPTE170M,
165 .flags = ZORAN_FORMAT_CAPTURE |
166 ZORAN_FORMAT_OVERLAY,
168 .name = "Hardware-encoded Motion-JPEG",
171 .fourcc = V4L2_PIX_FMT_MJPEG,
172 .colorspace = V4L2_COLORSPACE_SMPTE170M,
175 .flags = ZORAN_FORMAT_CAPTURE |
176 ZORAN_FORMAT_PLAYBACK |
177 ZORAN_FORMAT_COMPRESSED,
180 static const int zoran_num_formats =
181 (sizeof(zoran_formats) / sizeof(struct zoran_format));
183 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
184 #if !defined(CONFIG_BIGPHYS_AREA)
185 //#undef CONFIG_BIGPHYS_AREA
186 #define BUZ_USE_HIMEM
189 #if defined(CONFIG_BIGPHYS_AREA)
190 # include <linux/bigphysarea.h>
193 extern int *zr_debug;
195 #define dprintk(num, format, args...) \
197 if (*zr_debug >= num) \
198 printk(format, ##args); \
201 extern int v4l_nbufs;
202 extern int v4l_bufsize;
203 extern int jpg_nbufs;
204 extern int jpg_bufsize;
205 extern int pass_through;
207 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
208 module_param(lock_norm, int, 0);
209 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
212 /* small helper function for calculating buffersizes for v4l2
213 * we calculate the nearest higher power-of-two, which
214 * will be the recommended buffersize */
216 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
218 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
219 __u32 num = (1024 * 512) / (div);
228 if (result > jpg_bufsize)
236 /* forward references */
237 static void v4l_fbuffer_free(struct file *file);
238 static void jpg_fbuffer_free(struct file *file);
241 * Allocate the V4L grab buffers
243 * These have to be pysically contiguous.
244 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
245 * else we try to allocate them with bigphysarea_alloc_pages
246 * if the bigphysarea patch is present in the kernel,
247 * else we try to use high memory (if the user has bootet
248 * Linux with the necessary memory left over).
251 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
253 get_high_mem (unsigned long size)
256 * Check if there is usable memory at the end of Linux memory
257 * of at least size. Return the physical address of this memory,
258 * return 0 on failure.
260 * The idea is from Alexandro Rubini's book "Linux device drivers".
261 * The driver from him which is downloadable from O'Reilly's
262 * web site misses the "virt_to_phys(high_memory)" part
263 * (and therefore doesn't work at all - at least with 2.2.x kernels).
265 * It should be unnecessary to mention that THIS IS DANGEROUS,
266 * if more than one driver at a time has the idea to use this memory!!!!
269 volatile unsigned char __iomem *mem;
271 unsigned long hi_mem_ph;
274 /* Map the high memory to user space */
276 hi_mem_ph = virt_to_phys(high_memory);
278 mem = ioremap(hi_mem_ph, size);
281 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
286 for (i = 0; i < size; i++) {
287 /* Check if it is memory */
290 if (readb(mem + i) != c)
294 if (readb(mem + i) != c)
296 writeb(0, mem + i); /* zero out memory */
298 /* give the kernel air to breath */
299 if ((i & 0x3ffff) == 0x3ffff)
308 "%s: get_high_mem() - requested %lu, avail %lu\n",
309 ZORAN_NAME, size, i);
318 v4l_fbuffer_alloc (struct file *file)
320 struct zoran_fh *fh = file->private_data;
321 struct zoran *zr = fh->zr;
324 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
325 unsigned long pmem = 0;
328 /* we might have old buffers lying around... */
329 if (fh->v4l_buffers.ready_to_be_freed) {
330 v4l_fbuffer_free(file);
333 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
334 if (fh->v4l_buffers.buffer[i].fbuffer)
337 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
341 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
345 (unsigned char *) kmalloc(fh->v4l_buffers.
351 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
353 v4l_fbuffer_free(file);
356 fh->v4l_buffers.buffer[i].fbuffer = mem;
357 fh->v4l_buffers.buffer[i].fbuffer_phys =
359 fh->v4l_buffers.buffer[i].fbuffer_bus =
361 for (off = 0; off < fh->v4l_buffers.buffer_size;
363 SetPageReserved(MAP_NR(mem + off));
366 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
367 ZR_DEVNAME(zr), i, (unsigned long) mem,
370 #if defined(CONFIG_BIGPHYS_AREA)
371 /* Use bigphysarea_alloc_pages */
374 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
378 (unsigned char *) bigphysarea_alloc_pages(n, 0,
383 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
385 v4l_fbuffer_free(file);
388 fh->v4l_buffers.buffer[i].fbuffer = mem;
389 fh->v4l_buffers.buffer[i].fbuffer_phys =
391 fh->v4l_buffers.buffer[i].fbuffer_bus =
395 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
396 ZR_DEVNAME(zr), i, (unsigned) mem,
397 (unsigned) virt_to_bus(mem));
399 /* Zero out the allocated memory */
400 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
401 fh->v4l_buffers.buffer_size);
402 #elif defined(BUZ_USE_HIMEM)
404 /* Use high memory which has been left at boot time */
406 /* Ok., Ok. this is an evil hack - we make
407 * the assumption that physical addresses are
408 * the same as bus addresses (true at least
409 * for Intel processors). The whole method of
410 * obtaining and using this memory is not very
411 * nice - but I hope it saves some poor users
412 * from kernel hacking, which might have even
413 * more evil results */
417 fh->v4l_buffers.num_buffers *
418 fh->v4l_buffers.buffer_size;
420 pmem = get_high_mem(size);
424 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
425 ZR_DEVNAME(zr), size >> 10);
428 fh->v4l_buffers.buffer[0].fbuffer = NULL;
429 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
430 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
433 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
434 ZR_DEVNAME(zr), size >> 10);
436 fh->v4l_buffers.buffer[i].fbuffer = NULL;
437 fh->v4l_buffers.buffer[i].fbuffer_phys =
438 pmem + i * fh->v4l_buffers.buffer_size;
439 fh->v4l_buffers.buffer[i].fbuffer_bus =
440 pmem + i * fh->v4l_buffers.buffer_size;
443 /* No bigphysarea present, usage of high memory disabled,
444 * but user wants buffers of more than MAX_KMALLOC_MEM */
447 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
451 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
452 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
453 fh->v4l_buffers.buffer_size >> 10);
459 fh->v4l_buffers.allocated = 1;
464 /* free the V4L grab buffers */
466 v4l_fbuffer_free (struct file *file)
468 struct zoran_fh *fh = file->private_data;
469 struct zoran *zr = fh->zr;
473 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
475 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
476 if (!fh->v4l_buffers.buffer[i].fbuffer)
479 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
480 mem = fh->v4l_buffers.buffer[i].fbuffer;
481 for (off = 0; off < fh->v4l_buffers.buffer_size;
483 ClearPageReserved(MAP_NR(mem + off));
484 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
486 #if defined(CONFIG_BIGPHYS_AREA)
488 bigphysarea_free_pages((void *) fh->v4l_buffers.
491 fh->v4l_buffers.buffer[i].fbuffer = NULL;
494 fh->v4l_buffers.allocated = 0;
495 fh->v4l_buffers.ready_to_be_freed = 0;
499 * Allocate the MJPEG grab buffers.
501 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
502 * kmalloc is used to request a physically contiguous area,
503 * else we allocate the memory in framgents with get_zeroed_page.
505 * If a Natoma chipset is present and this is a revision 1 zr36057,
506 * each MJPEG buffer needs to be physically contiguous.
507 * (RJ: This statement is from Dave Perks' original driver,
508 * I could never check it because I have a zr36067)
509 * The driver cares about this because it reduces the buffer
510 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
512 * RJ: The contents grab buffers needs never be accessed in the driver.
513 * Therefore there is no need to allocate them with vmalloc in order
514 * to get a contiguous virtual memory space.
515 * I don't understand why many other drivers first allocate them with
516 * vmalloc (which uses internally also get_zeroed_page, but delivers you
517 * virtual addresses) and then again have to make a lot of efforts
518 * to get the physical address.
521 * On big-endian architectures (such as ppc) some extra steps
522 * are needed. When reading and writing to the stat_com array
523 * and fragment buffers, the device expects to see little-
524 * endian values. The use of cpu_to_le32() and le32_to_cpu()
525 * in this function (and one or two others in zoran_device.c)
526 * ensure that these values are always stored in little-endian
527 * form, regardless of architecture. The zr36057 does Very Bad
528 * Things on big endian architectures if the stat_com array
529 * and fragment buffers are not little-endian.
533 jpg_fbuffer_alloc (struct file *file)
535 struct zoran_fh *fh = file->private_data;
536 struct zoran *zr = fh->zr;
540 /* we might have old buffers lying around */
541 if (fh->jpg_buffers.ready_to_be_freed) {
542 jpg_fbuffer_free(file);
545 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
546 if (fh->jpg_buffers.buffer[i].frag_tab)
549 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
552 /* Allocate fragment table for this buffer */
554 mem = get_zeroed_page(GFP_KERNEL);
558 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
560 jpg_fbuffer_free(file);
563 memset((void *) mem, 0, PAGE_SIZE);
564 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
565 fh->jpg_buffers.buffer[i].frag_tab_bus =
566 virt_to_bus((void *) mem);
568 //if (alloc_contig) {
569 if (fh->jpg_buffers.need_contiguous) {
571 (unsigned long) kmalloc(fh->jpg_buffers.
577 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
579 jpg_fbuffer_free(file);
582 fh->jpg_buffers.buffer[i].frag_tab[0] =
583 cpu_to_le32(virt_to_bus((void *) mem));
584 fh->jpg_buffers.buffer[i].frag_tab[1] =
585 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
586 for (off = 0; off < fh->jpg_buffers.buffer_size;
588 SetPageReserved(MAP_NR(mem + off));
590 /* jpg_bufsize is allreay page aligned */
592 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
594 mem = get_zeroed_page(GFP_KERNEL);
598 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
600 jpg_fbuffer_free(file);
604 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
605 cpu_to_le32(virt_to_bus((void *) mem));
606 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
608 cpu_to_le32((PAGE_SIZE / 4) << 1);
609 SetPageReserved(MAP_NR(mem));
612 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
617 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
619 (fh->jpg_buffers.num_buffers *
620 fh->jpg_buffers.buffer_size) >> 10);
622 fh->jpg_buffers.allocated = 1;
627 /* free the MJPEG grab buffers */
629 jpg_fbuffer_free (struct file *file)
631 struct zoran_fh *fh = file->private_data;
632 struct zoran *zr = fh->zr;
636 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
638 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
639 if (!fh->jpg_buffers.buffer[i].frag_tab)
642 //if (alloc_contig) {
643 if (fh->jpg_buffers.need_contiguous) {
644 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
645 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
646 fh->jpg_buffers.buffer[i].frag_tab[0]));
648 off < fh->jpg_buffers.buffer_size;
650 ClearPageReserved(MAP_NR
653 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
654 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
658 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
660 if (!fh->jpg_buffers.buffer[i].
663 ClearPageReserved(MAP_NR
667 buffer[i].frag_tab[2 *
669 free_page((unsigned long)
675 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
677 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
682 free_page((unsigned long) fh->jpg_buffers.buffer[i].
684 fh->jpg_buffers.buffer[i].frag_tab = NULL;
687 fh->jpg_buffers.allocated = 0;
688 fh->jpg_buffers.ready_to_be_freed = 0;
692 * V4L Buffer grabbing
696 zoran_v4l_set_format (struct file *file,
699 const struct zoran_format *format)
701 struct zoran_fh *fh = file->private_data;
702 struct zoran *zr = fh->zr;
705 /* Check size and format of the grab wanted */
707 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
708 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
711 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
712 ZR_DEVNAME(zr), width, height);
716 bpp = (format->depth + 7) / 8;
718 /* Check against available buffer size */
719 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
722 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
723 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
727 /* The video front end needs 4-byte alinged line sizes */
729 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
732 "%s: v4l_set_format() - wrong frame alingment\n",
737 fh->v4l_settings.width = width;
738 fh->v4l_settings.height = height;
739 fh->v4l_settings.format = format;
740 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
746 zoran_v4l_queue_frame (struct file *file,
749 struct zoran_fh *fh = file->private_data;
750 struct zoran *zr = fh->zr;
754 if (!fh->v4l_buffers.allocated) {
757 "%s: v4l_queue_frame() - buffers not yet allocated\n",
762 /* No grabbing outside the buffer range! */
763 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
766 "%s: v4l_queue_frame() - buffer %d is out of range\n",
767 ZR_DEVNAME(zr), num);
771 spin_lock_irqsave(&zr->spinlock, flags);
773 if (fh->v4l_buffers.active == ZORAN_FREE) {
774 if (zr->v4l_buffers.active == ZORAN_FREE) {
775 zr->v4l_buffers = fh->v4l_buffers;
776 fh->v4l_buffers.active = ZORAN_ACTIVE;
780 "%s: v4l_queue_frame() - another session is already capturing\n",
786 /* make sure a grab isn't going on currently with this buffer */
788 switch (zr->v4l_buffers.buffer[num].state) {
791 if (zr->v4l_buffers.active == ZORAN_FREE) {
792 fh->v4l_buffers.active = ZORAN_FREE;
793 zr->v4l_buffers.allocated = 0;
795 res = -EBUSY; /* what are you doing? */
800 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
801 ZR_DEVNAME(zr), num);
803 /* since there is at least one unused buffer there's room for at least
804 * one more pend[] entry */
805 zr->v4l_pend[zr->v4l_pend_head++ &
806 V4L_MASK_FRAME] = num;
807 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
808 zr->v4l_buffers.buffer[num].bs.length =
809 fh->v4l_settings.bytesperline *
810 zr->v4l_settings.height;
811 fh->v4l_buffers.buffer[num] =
812 zr->v4l_buffers.buffer[num];
817 spin_unlock_irqrestore(&zr->spinlock, flags);
819 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
820 zr->v4l_buffers.active = fh->v4l_buffers.active;
826 v4l_grab (struct file *file,
827 struct video_mmap *mp)
829 struct zoran_fh *fh = file->private_data;
830 struct zoran *zr = fh->zr;
833 for (i = 0; i < zoran_num_formats; i++) {
834 if (zoran_formats[i].palette == mp->format &&
835 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
836 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
839 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
842 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
848 * To minimize the time spent in the IRQ routine, we avoid setting up
849 * the video front end there.
850 * If this grab has different parameters from a running streaming capture
851 * we stop the streaming capture and start it over again.
853 if (zr->v4l_memgrab_active &&
854 (zr->v4l_settings.width != mp->width ||
855 zr->v4l_settings.height != mp->height ||
856 zr->v4l_settings.format->palette != mp->format)) {
857 res = wait_grab_pending(zr);
861 if ((res = zoran_v4l_set_format(file,
866 zr->v4l_settings = fh->v4l_settings;
868 /* queue the frame in the pending queue */
869 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
870 fh->v4l_buffers.active = ZORAN_FREE;
874 /* put the 36057 into frame grabbing mode */
875 if (!res && !zr->v4l_memgrab_active)
876 zr36057_set_memgrab(zr, 1);
878 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
884 * Sync on a V4L buffer
888 v4l_sync (struct file *file,
891 struct zoran_fh *fh = file->private_data;
892 struct zoran *zr = fh->zr;
895 if (fh->v4l_buffers.active == ZORAN_FREE) {
898 "%s: v4l_sync() - no grab active for this session\n",
903 /* check passed-in frame number */
904 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
906 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
907 ZR_DEVNAME(zr), frame);
911 /* Check if is buffer was queued at all */
912 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
915 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
920 /* wait on this buffer to get ready */
921 while (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_PEND) {
922 if (!interruptible_sleep_on_timeout(&zr->v4l_capq, 10 * HZ))
924 else if (signal_pending(current))
928 /* buffer should now be in BUZ_STATE_DONE */
929 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
931 KERN_ERR "%s: v4l_sync() - internal state error\n",
934 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
935 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
937 spin_lock_irqsave(&zr->spinlock, flags);
939 /* Check if streaming capture has finished */
940 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
941 zr36057_set_memgrab(zr, 0);
942 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
943 fh->v4l_buffers.active = zr->v4l_buffers.active =
945 zr->v4l_buffers.allocated = 0;
949 spin_unlock_irqrestore(&zr->spinlock, flags);
955 * Queue a MJPEG buffer for capture/playback
959 zoran_jpg_queue_frame (struct file *file,
961 enum zoran_codec_mode mode)
963 struct zoran_fh *fh = file->private_data;
964 struct zoran *zr = fh->zr;
968 /* Check if buffers are allocated */
969 if (!fh->jpg_buffers.allocated) {
972 "%s: jpg_queue_frame() - buffers not yet allocated\n",
977 /* No grabbing outside the buffer range! */
978 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
981 "%s: jpg_queue_frame() - buffer %d out of range\n",
982 ZR_DEVNAME(zr), num);
986 /* what is the codec mode right now? */
987 if (zr->codec_mode == BUZ_MODE_IDLE) {
988 zr->jpg_settings = fh->jpg_settings;
989 } else if (zr->codec_mode != mode) {
990 /* wrong codec mode active - invalid */
993 "%s: jpg_queue_frame() - codec in wrong mode\n",
998 spin_lock_irqsave(&zr->spinlock, flags);
1000 if (fh->jpg_buffers.active == ZORAN_FREE) {
1001 if (zr->jpg_buffers.active == ZORAN_FREE) {
1002 zr->jpg_buffers = fh->jpg_buffers;
1003 fh->jpg_buffers.active = ZORAN_ACTIVE;
1007 "%s: jpg_queue_frame() - another session is already capturing\n",
1013 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1014 /* Ok load up the jpeg codec */
1015 zr36057_enable_jpg(zr, mode);
1019 switch (zr->jpg_buffers.buffer[num].state) {
1020 case BUZ_STATE_DONE:
1023 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1025 case BUZ_STATE_USER:
1026 /* since there is at least one unused buffer there's room for at
1027 *least one more pend[] entry */
1028 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1030 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1031 fh->jpg_buffers.buffer[num] =
1032 zr->jpg_buffers.buffer[num];
1033 zoran_feed_stat_com(zr);
1037 case BUZ_STATE_PEND:
1038 if (zr->jpg_buffers.active == ZORAN_FREE) {
1039 fh->jpg_buffers.active = ZORAN_FREE;
1040 zr->jpg_buffers.allocated = 0;
1042 res = -EBUSY; /* what are you doing? */
1047 spin_unlock_irqrestore(&zr->spinlock, flags);
1049 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1050 zr->jpg_buffers.active = fh->jpg_buffers.active;
1057 jpg_qbuf (struct file *file,
1059 enum zoran_codec_mode mode)
1061 struct zoran_fh *fh = file->private_data;
1062 struct zoran *zr = fh->zr;
1065 /* Does the user want to stop streaming? */
1067 if (zr->codec_mode == mode) {
1068 if (fh->jpg_buffers.active == ZORAN_FREE) {
1071 "%s: jpg_qbuf(-1) - session not active\n",
1075 fh->jpg_buffers.active = zr->jpg_buffers.active =
1077 zr->jpg_buffers.allocated = 0;
1078 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1083 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1089 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1092 /* Start the jpeg codec when the first frame is queued */
1093 if (!res && zr->jpg_que_head == 1)
1100 * Sync on a MJPEG buffer
1104 jpg_sync (struct file *file,
1105 struct zoran_sync *bs)
1107 struct zoran_fh *fh = file->private_data;
1108 struct zoran *zr = fh->zr;
1109 unsigned long flags;
1112 if (fh->jpg_buffers.active == ZORAN_FREE) {
1115 "%s: jpg_sync() - capture is not currently active\n",
1119 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1120 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1123 "%s: jpg_sync() - codec not in streaming mode\n",
1127 while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1128 if (zr->jpg_dma_tail == zr->jpg_dma_head)
1132 interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
1136 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1138 zr->codec->control(zr->codec, CODEC_G_STATUS,
1142 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1143 ZR_DEVNAME(zr), isr);
1147 } else if (signal_pending(current))
1148 return -ERESTARTSYS;
1151 spin_lock_irqsave(&zr->spinlock, flags);
1153 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1154 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1156 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1158 /* buffer should now be in BUZ_STATE_DONE */
1160 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1163 "%s: jpg_sync() - internal state error\n",
1166 *bs = zr->jpg_buffers.buffer[frame].bs;
1168 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1169 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1171 spin_unlock_irqrestore(&zr->spinlock, flags);
1177 zoran_open_init_session (struct file *file)
1180 struct zoran_fh *fh = file->private_data;
1181 struct zoran *zr = fh->zr;
1183 /* Per default, map the V4L Buffers */
1184 fh->map_mode = ZORAN_MAP_MODE_RAW;
1186 /* take over the card's current settings */
1187 fh->overlay_settings = zr->overlay_settings;
1188 fh->overlay_settings.is_set = 0;
1189 fh->overlay_settings.format = zr->overlay_settings.format;
1190 fh->overlay_active = ZORAN_FREE;
1193 fh->v4l_settings = zr->v4l_settings;
1196 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1197 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1198 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1199 fh->v4l_buffers.buffer[i].bs.frame = i;
1201 fh->v4l_buffers.allocated = 0;
1202 fh->v4l_buffers.ready_to_be_freed = 0;
1203 fh->v4l_buffers.active = ZORAN_FREE;
1204 fh->v4l_buffers.buffer_size = v4l_bufsize;
1205 fh->v4l_buffers.num_buffers = v4l_nbufs;
1208 fh->jpg_settings = zr->jpg_settings;
1211 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1212 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1213 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1214 fh->jpg_buffers.buffer[i].bs.frame = i;
1216 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1217 fh->jpg_buffers.allocated = 0;
1218 fh->jpg_buffers.ready_to_be_freed = 0;
1219 fh->jpg_buffers.active = ZORAN_FREE;
1220 fh->jpg_buffers.buffer_size = jpg_bufsize;
1221 fh->jpg_buffers.num_buffers = jpg_nbufs;
1225 zoran_close_end_session (struct file *file)
1227 struct zoran_fh *fh = file->private_data;
1228 struct zoran *zr = fh->zr;
1231 if (fh->overlay_active != ZORAN_FREE) {
1232 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1233 zr->v4l_overlay_active = 0;
1234 if (!zr->v4l_memgrab_active)
1235 zr36057_overlay(zr, 0);
1236 zr->overlay_mask = NULL;
1240 if (fh->v4l_buffers.active != ZORAN_FREE) {
1241 zr36057_set_memgrab(zr, 0);
1242 zr->v4l_buffers.allocated = 0;
1243 zr->v4l_buffers.active = fh->v4l_buffers.active =
1248 if (fh->v4l_buffers.allocated ||
1249 fh->v4l_buffers.ready_to_be_freed) {
1250 v4l_fbuffer_free(file);
1254 if (fh->jpg_buffers.active != ZORAN_FREE) {
1255 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1256 zr->jpg_buffers.allocated = 0;
1257 zr->jpg_buffers.active = fh->jpg_buffers.active =
1262 if (fh->jpg_buffers.allocated ||
1263 fh->jpg_buffers.ready_to_be_freed) {
1264 jpg_fbuffer_free(file);
1269 * Open a zoran card. Right now the flags stuff is just playing
1273 zoran_open (struct inode *inode,
1276 unsigned int minor = iminor(inode);
1277 struct zoran *zr = NULL;
1278 struct zoran_fh *fh;
1279 int i, res, first_open = 0, have_module_locks = 0;
1281 /* find the device */
1282 for (i = 0; i < zoran_num; i++) {
1283 if (zoran[i].video_dev->minor == minor) {
1290 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1292 goto open_unlock_and_return;
1295 /* see fs/device.c - the kernel already locks during open(),
1296 * so locking ourselves only causes deadlocks */
1297 /*down(&zr->resource_lock);*/
1301 KERN_ERR "%s: no TV decoder loaded for device!\n",
1304 goto open_unlock_and_return;
1307 /* try to grab a module lock */
1308 if (!try_module_get(THIS_MODULE)) {
1311 "%s: failed to acquire my own lock! PANIC!\n",
1314 goto open_unlock_and_return;
1316 if (!try_module_get(zr->decoder->driver->owner)) {
1319 "%s: failed to grab ownership of i2c decoder\n",
1322 module_put(THIS_MODULE);
1323 goto open_unlock_and_return;
1326 !try_module_get(zr->encoder->driver->owner)) {
1329 "%s: failed to grab ownership of i2c encoder\n",
1332 module_put(zr->decoder->driver->owner);
1333 module_put(THIS_MODULE);
1334 goto open_unlock_and_return;
1337 have_module_locks = 1;
1339 if (zr->user >= 2048) {
1340 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1341 ZR_DEVNAME(zr), zr->user);
1343 goto open_unlock_and_return;
1346 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1347 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1349 /* now, create the open()-specific file_ops struct */
1350 fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1354 "%s: zoran_open() - allocation of zoran_fh failed\n",
1357 goto open_unlock_and_return;
1359 memset(fh, 0, sizeof(struct zoran_fh));
1360 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1361 * on norm-change! */
1363 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1364 if (!fh->overlay_mask) {
1367 "%s: zoran_open() - allocation of overlay_mask failed\n",
1371 goto open_unlock_and_return;
1374 if (zr->user++ == 0)
1377 /*up(&zr->resource_lock);*/
1379 /* default setup - TODO: look at flags */
1380 if (first_open) { /* First device open */
1381 zr36057_restart(zr);
1382 zoran_open_init_params(zr);
1383 zoran_init_hardware(zr);
1385 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1388 /* set file_ops stuff */
1389 file->private_data = fh;
1391 zoran_open_init_session(file);
1395 open_unlock_and_return:
1396 /* if we grabbed locks, release them accordingly */
1397 if (have_module_locks) {
1398 module_put(zr->decoder->driver->owner);
1400 module_put(zr->encoder->driver->owner);
1402 module_put(THIS_MODULE);
1405 /* if there's no device found, we didn't obtain the lock either */
1407 /*up(&zr->resource_lock);*/
1414 zoran_close (struct inode *inode,
1417 struct zoran_fh *fh = file->private_data;
1418 struct zoran *zr = fh->zr;
1420 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1421 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1423 /* kernel locks (fs/device.c), so don't do that ourselves
1424 * (prevents deadlocks) */
1425 /*down(&zr->resource_lock);*/
1427 zoran_close_end_session(file);
1429 if (zr->user-- == 1) { /* Last process */
1430 /* Clean up JPEG process */
1431 wake_up_interruptible(&zr->jpg_capq);
1432 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1433 zr->jpg_buffers.allocated = 0;
1434 zr->jpg_buffers.active = ZORAN_FREE;
1436 /* disable interrupts */
1437 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1440 print_interrupts(zr);
1443 zr->v4l_overlay_active = 0;
1444 zr36057_overlay(zr, 0);
1445 zr->overlay_mask = NULL;
1448 wake_up_interruptible(&zr->v4l_capq);
1449 zr36057_set_memgrab(zr, 0);
1450 zr->v4l_buffers.allocated = 0;
1451 zr->v4l_buffers.active = ZORAN_FREE;
1452 zoran_set_pci_master(zr, 0);
1454 if (!pass_through) { /* Switch to color bar */
1455 int zero = 0, two = 2;
1456 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1457 encoder_command(zr, ENCODER_SET_INPUT, &two);
1461 file->private_data = NULL;
1462 kfree(fh->overlay_mask);
1465 /* release locks on the i2c modules */
1466 module_put(zr->decoder->driver->owner);
1468 module_put(zr->encoder->driver->owner);
1470 module_put(THIS_MODULE);
1472 /*up(&zr->resource_lock);*/
1474 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1481 zoran_read (struct file *file,
1486 /* we simply don't support read() (yet)... */
1492 zoran_write (struct file *file,
1493 const char __user *data,
1497 /* ...and the same goes for write() */
1503 setup_fbuffer (struct file *file,
1505 const struct zoran_format *fmt,
1510 struct zoran_fh *fh = file->private_data;
1511 struct zoran *zr = fh->zr;
1513 /* (Ronald) v4l/v4l2 guidelines */
1514 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1517 /* we need a bytesperline value, even if not given */
1519 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1522 if (zr->overlay_active) {
1523 /* dzjee... stupid users... don't even bother to turn off
1524 * overlay before changing the memory location...
1525 * normally, we would return errors here. However, one of
1526 * the tools that does this is... xawtv! and since xawtv
1527 * is used by +/- 99% of the users, we'd rather be user-
1528 * friendly and silently do as if nothing went wrong */
1531 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1533 zr36057_overlay(zr, 0);
1537 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1540 "%s: setup_fbuffer() - no valid overlay format given\n",
1544 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1547 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1548 ZR_DEVNAME(zr), width, height, bytesperline);
1551 if (bytesperline & 3) {
1554 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1555 ZR_DEVNAME(zr), bytesperline);
1559 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1560 zr->buffer.height = height;
1561 zr->buffer.width = width;
1562 zr->buffer.depth = fmt->depth;
1563 zr->overlay_settings.format = fmt;
1564 zr->buffer.bytesperline = bytesperline;
1566 /* The user should set new window parameters */
1567 zr->overlay_settings.is_set = 0;
1574 setup_window (struct file *file,
1579 struct video_clip __user *clips,
1581 void __user *bitmap)
1583 struct zoran_fh *fh = file->private_data;
1584 struct zoran *zr = fh->zr;
1585 struct video_clip *vcp = NULL;
1589 if (!zr->buffer.base) {
1592 "%s: setup_window() - frame buffer has to be set first\n",
1597 if (!fh->overlay_settings.format) {
1600 "%s: setup_window() - no overlay format set\n",
1606 * The video front end needs 4-byte alinged line sizes, we correct that
1607 * silently here if necessary
1609 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1610 end = (x + width) & ~1; /* round down */
1611 x = (x + 1) & ~1; /* round up */
1615 if (zr->buffer.depth == 24) {
1616 end = (x + width) & ~3; /* round down */
1617 x = (x + 3) & ~3; /* round up */
1621 if (width > BUZ_MAX_WIDTH)
1622 width = BUZ_MAX_WIDTH;
1623 if (height > BUZ_MAX_HEIGHT)
1624 height = BUZ_MAX_HEIGHT;
1626 /* Check for vaild parameters */
1627 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1628 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1631 "%s: setup_window() - width = %d or height = %d invalid\n",
1632 ZR_DEVNAME(zr), width, height);
1636 fh->overlay_settings.x = x;
1637 fh->overlay_settings.y = y;
1638 fh->overlay_settings.width = width;
1639 fh->overlay_settings.height = height;
1640 fh->overlay_settings.clipcount = clipcount;
1643 * If an overlay is running, we have to switch it off
1644 * and switch it on again in order to get the new settings in effect.
1646 * We also want to avoid that the overlay mask is written
1647 * when an overlay is running.
1650 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1651 zr->overlay_active != ZORAN_FREE &&
1652 fh->overlay_active != ZORAN_FREE;
1654 zr36057_overlay(zr, 0);
1657 * Write the overlay mask if clips are wanted.
1658 * We prefer a bitmap.
1661 /* fake value - it just means we want clips */
1662 fh->overlay_settings.clipcount = 1;
1664 if (copy_from_user(fh->overlay_mask, bitmap,
1665 (width * height + 7) / 8)) {
1668 } else if (clipcount > 0) {
1669 /* write our own bitmap from the clips */
1670 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1674 "%s: setup_window() - Alloc of clip mask failed\n",
1679 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1683 write_overlay_mask(file, vcp, clipcount);
1687 fh->overlay_settings.is_set = 1;
1688 if (fh->overlay_active != ZORAN_FREE &&
1689 zr->overlay_active != ZORAN_FREE)
1690 zr->overlay_settings = fh->overlay_settings;
1693 zr36057_overlay(zr, 1);
1695 /* Make sure the changes come into effect */
1696 return wait_grab_pending(zr);
1700 setup_overlay (struct file *file,
1703 struct zoran_fh *fh = file->private_data;
1704 struct zoran *zr = fh->zr;
1706 /* If there is nothing to do, return immediatly */
1707 if ((on && fh->overlay_active != ZORAN_FREE) ||
1708 (!on && fh->overlay_active == ZORAN_FREE))
1711 /* check whether we're touching someone else's overlay */
1712 if (on && zr->overlay_active != ZORAN_FREE &&
1713 fh->overlay_active == ZORAN_FREE) {
1716 "%s: setup_overlay() - overlay is already active for another session\n",
1720 if (!on && zr->overlay_active != ZORAN_FREE &&
1721 fh->overlay_active == ZORAN_FREE) {
1724 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1730 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1731 zr->v4l_overlay_active = 0;
1732 /* When a grab is running, the video simply
1733 * won't be switched on any more */
1734 if (!zr->v4l_memgrab_active)
1735 zr36057_overlay(zr, 0);
1736 zr->overlay_mask = NULL;
1738 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1741 "%s: setup_overlay() - buffer or window not set\n",
1745 if (!fh->overlay_settings.format) {
1748 "%s: setup_overlay() - no overlay format set\n",
1752 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1753 zr->v4l_overlay_active = 1;
1754 zr->overlay_mask = fh->overlay_mask;
1755 zr->overlay_settings = fh->overlay_settings;
1756 if (!zr->v4l_memgrab_active)
1757 zr36057_overlay(zr, 1);
1758 /* When a grab is running, the video will be
1759 * switched on when grab is finished */
1762 /* Make sure the changes come into effect */
1763 return wait_grab_pending(zr);
1767 /* get the status of a buffer in the clients buffer queue */
1769 zoran_v4l2_buffer_status (struct file *file,
1770 struct v4l2_buffer *buf,
1773 struct zoran_fh *fh = file->private_data;
1774 struct zoran *zr = fh->zr;
1776 buf->flags = V4L2_BUF_FLAG_MAPPED;
1778 switch (fh->map_mode) {
1779 case ZORAN_MAP_MODE_RAW:
1782 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1783 !fh->v4l_buffers.allocated) {
1786 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1791 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1792 buf->length = fh->v4l_buffers.buffer_size;
1795 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1796 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1797 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1798 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1799 buf->flags |= V4L2_BUF_FLAG_DONE;
1801 fh->v4l_buffers.buffer[num].bs.timestamp;
1803 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1806 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1807 buf->field = V4L2_FIELD_TOP;
1809 buf->field = V4L2_FIELD_INTERLACED;
1813 case ZORAN_MAP_MODE_JPG_REC:
1814 case ZORAN_MAP_MODE_JPG_PLAY:
1817 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1818 !fh->jpg_buffers.allocated) {
1821 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1826 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1827 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1828 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1829 buf->length = fh->jpg_buffers.buffer_size;
1831 /* these variables are only written after frame has been captured */
1832 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1833 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1834 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1836 fh->jpg_buffers.buffer[num].bs.timestamp;
1838 fh->jpg_buffers.buffer[num].bs.length;
1839 buf->flags |= V4L2_BUF_FLAG_DONE;
1841 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1844 /* which fields are these? */
1845 if (fh->jpg_settings.TmpDcm != 1)
1848 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1852 odd_even ? V4L2_FIELD_SEQ_TB :
1861 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1862 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1866 buf->memory = V4L2_MEMORY_MMAP;
1868 buf->m.offset = buf->length * num;
1875 zoran_set_norm (struct zoran *zr,
1876 int norm) /* VIDEO_MODE_* */
1878 int norm_encoder, on;
1880 if (zr->v4l_buffers.active != ZORAN_FREE ||
1881 zr->jpg_buffers.active != ZORAN_FREE) {
1884 "%s: set_norm() called while in playback/capture mode\n",
1889 if (lock_norm && norm != zr->norm) {
1890 if (lock_norm > 1) {
1893 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1899 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1905 if (norm != VIDEO_MODE_AUTO &&
1906 (norm < 0 || norm >= zr->card.norms ||
1907 !zr->card.tvn[norm])) {
1909 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1910 ZR_DEVNAME(zr), norm);
1914 if (norm == VIDEO_MODE_AUTO) {
1917 /* if we have autodetect, ... */
1918 struct video_decoder_capability caps;
1919 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1920 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1921 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1926 decoder_command(zr, DECODER_SET_NORM, &norm);
1928 /* let changes come into effect */
1931 decoder_command(zr, DECODER_GET_STATUS, &status);
1932 if (!(status & DECODER_STATUS_GOOD)) {
1935 "%s: set_norm() - no norm detected\n",
1938 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1942 if (status & DECODER_STATUS_NTSC)
1943 norm = VIDEO_MODE_NTSC;
1944 else if (status & DECODER_STATUS_SECAM)
1945 norm = VIDEO_MODE_SECAM;
1947 norm = VIDEO_MODE_PAL;
1949 zr->timing = zr->card.tvn[norm];
1950 norm_encoder = norm;
1952 /* We switch overlay off and on since a change in the
1953 * norm needs different VFE settings */
1954 on = zr->overlay_active && !zr->v4l_memgrab_active;
1956 zr36057_overlay(zr, 0);
1958 decoder_command(zr, DECODER_SET_NORM, &norm);
1959 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1962 zr36057_overlay(zr, 1);
1964 /* Make sure the changes come into effect */
1971 zoran_set_input (struct zoran *zr,
1976 if (input == zr->input) {
1980 if (zr->v4l_buffers.active != ZORAN_FREE ||
1981 zr->jpg_buffers.active != ZORAN_FREE) {
1984 "%s: set_input() called while in playback/capture mode\n",
1989 if (input < 0 || input >= zr->card.inputs) {
1992 "%s: set_input() - unnsupported input %d\n",
1993 ZR_DEVNAME(zr), input);
1997 realinput = zr->card.input[input].muxsel;
2000 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2010 zoran_do_ioctl (struct inode *inode,
2015 struct zoran_fh *fh = file->private_data;
2016 struct zoran *zr = fh->zr;
2017 /* CAREFUL: used in multiple places here */
2018 struct zoran_jpg_settings settings;
2020 /* we might have older buffers lying around... We don't want
2021 * to wait, but we do want to try cleaning them up ASAP. So
2022 * we try to obtain the lock and free them. If that fails, we
2023 * don't do anything and wait for the next turn. In the end,
2024 * zoran_close() or a new allocation will still free them...
2025 * This is just a 'the sooner the better' extra 'feature'
2027 * We don't free the buffers right on munmap() because that
2028 * causes oopses (kfree() inside munmap() oopses for no
2029 * apparent reason - it's also not reproduceable in any way,
2030 * but moving the free code outside the munmap() handler fixes
2031 * all this... If someone knows why, please explain me (Ronald)
2033 if (!down_trylock(&zr->resource_lock)) {
2034 /* we obtained it! Let's try to free some things */
2035 if (fh->jpg_buffers.ready_to_be_freed)
2036 jpg_fbuffer_free(file);
2037 if (fh->v4l_buffers.ready_to_be_freed)
2038 v4l_fbuffer_free(file);
2040 up(&zr->resource_lock);
2047 struct video_capability *vcap = arg;
2049 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2051 memset(vcap, 0, sizeof(struct video_capability));
2052 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
2053 vcap->type = ZORAN_VID_TYPE;
2055 vcap->channels = zr->card.inputs;
2057 down(&zr->resource_lock);
2058 vcap->maxwidth = BUZ_MAX_WIDTH;
2059 vcap->maxheight = BUZ_MAX_HEIGHT;
2060 vcap->minwidth = BUZ_MIN_WIDTH;
2061 vcap->minheight = BUZ_MIN_HEIGHT;
2062 up(&zr->resource_lock);
2070 struct video_channel *vchan = arg;
2071 int channel = vchan->channel;
2073 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2074 ZR_DEVNAME(zr), vchan->channel);
2076 memset(vchan, 0, sizeof(struct video_channel));
2077 if (channel > zr->card.inputs || channel < 0) {
2080 "%s: VIDIOCGCHAN on not existing channel %d\n",
2081 ZR_DEVNAME(zr), channel);
2085 strcpy(vchan->name, zr->card.input[channel].name);
2089 vchan->type = VIDEO_TYPE_CAMERA;
2090 down(&zr->resource_lock);
2091 vchan->norm = zr->norm;
2092 up(&zr->resource_lock);
2093 vchan->channel = channel;
2099 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2101 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2103 * * The famos BTTV driver has it implemented with a struct video_channel argument
2104 * * and we follow it for compatibility reasons
2106 * * BTW: this is the only way the user can set the norm!
2111 struct video_channel *vchan = arg;
2116 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2117 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2119 down(&zr->resource_lock);
2120 if ((res = zoran_set_input(zr, vchan->channel)))
2121 goto schan_unlock_and_return;
2122 if ((res = zoran_set_norm(zr, vchan->norm)))
2123 goto schan_unlock_and_return;
2125 /* Make sure the changes come into effect */
2126 res = wait_grab_pending(zr);
2127 schan_unlock_and_return:
2128 up(&zr->resource_lock);
2135 struct video_picture *vpict = arg;
2137 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2139 memset(vpict, 0, sizeof(struct video_picture));
2140 down(&zr->resource_lock);
2141 vpict->hue = zr->hue;
2142 vpict->brightness = zr->brightness;
2143 vpict->contrast = zr->contrast;
2144 vpict->colour = zr->saturation;
2145 if (fh->overlay_settings.format) {
2146 vpict->depth = fh->overlay_settings.format->depth;
2147 vpict->palette = fh->overlay_settings.format->palette;
2151 up(&zr->resource_lock);
2159 struct video_picture *vpict = arg;
2164 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2165 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2166 vpict->colour, vpict->contrast, vpict->depth,
2169 for (i = 0; i < zoran_num_formats; i++) {
2170 const struct zoran_format *fmt = &zoran_formats[i];
2172 if (fmt->palette != -1 &&
2173 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2174 fmt->palette == vpict->palette &&
2175 fmt->depth == vpict->depth)
2178 if (i == zoran_num_formats) {
2181 "%s: VIDIOCSPICT - Invalid palette %d\n",
2182 ZR_DEVNAME(zr), vpict->palette);
2186 down(&zr->resource_lock);
2188 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2190 zr->hue = vpict->hue;
2191 zr->contrast = vpict->contrast;
2192 zr->saturation = vpict->colour;
2193 zr->brightness = vpict->brightness;
2195 fh->overlay_settings.format = &zoran_formats[i];
2197 up(&zr->resource_lock);
2207 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2208 ZR_DEVNAME(zr), *on);
2210 down(&zr->resource_lock);
2211 res = setup_overlay(file, *on);
2212 up(&zr->resource_lock);
2220 struct video_window *vwin = arg;
2222 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2224 memset(vwin, 0, sizeof(struct video_window));
2225 down(&zr->resource_lock);
2226 vwin->x = fh->overlay_settings.x;
2227 vwin->y = fh->overlay_settings.y;
2228 vwin->width = fh->overlay_settings.width;
2229 vwin->height = fh->overlay_settings.height;
2230 up(&zr->resource_lock);
2231 vwin->clipcount = 0;
2238 struct video_window *vwin = arg;
2243 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2244 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2245 vwin->height, vwin->clipcount);
2247 down(&zr->resource_lock);
2249 setup_window(file, vwin->x, vwin->y, vwin->width,
2250 vwin->height, vwin->clips,
2251 vwin->clipcount, NULL);
2252 up(&zr->resource_lock);
2260 struct video_buffer *vbuf = arg;
2262 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2264 down(&zr->resource_lock);
2266 up(&zr->resource_lock);
2273 struct video_buffer *vbuf = arg;
2278 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2279 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2280 vbuf->height, vbuf->depth, vbuf->bytesperline);
2282 for (i = 0; i < zoran_num_formats; i++)
2283 if (zoran_formats[i].depth == vbuf->depth)
2285 if (i == zoran_num_formats) {
2288 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2289 ZR_DEVNAME(zr), vbuf->depth);
2293 down(&zr->resource_lock);
2295 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2296 vbuf->width, vbuf->height,
2297 vbuf->bytesperline);
2298 up(&zr->resource_lock);
2306 int *frame = arg, res;
2308 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2309 ZR_DEVNAME(zr), *frame);
2311 down(&zr->resource_lock);
2312 res = v4l_sync(file, *frame);
2313 up(&zr->resource_lock);
2315 zr->v4l_sync_tail++;
2320 case VIDIOCMCAPTURE:
2322 struct video_mmap *vmap = arg;
2327 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2328 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2331 down(&zr->resource_lock);
2332 res = v4l_grab(file, vmap);
2333 up(&zr->resource_lock);
2340 struct video_mbuf *vmbuf = arg;
2343 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2346 fh->v4l_buffers.num_buffers *
2347 fh->v4l_buffers.buffer_size;
2348 vmbuf->frames = fh->v4l_buffers.num_buffers;
2349 for (i = 0; i < vmbuf->frames; i++) {
2351 i * fh->v4l_buffers.buffer_size;
2354 down(&zr->resource_lock);
2356 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2359 "%s: VIDIOCGMBUF - buffers already allocated\n",
2362 goto v4l1reqbuf_unlock_and_return;
2365 if (v4l_fbuffer_alloc(file)) {
2367 goto v4l1reqbuf_unlock_and_return;
2370 /* The next mmap will map the V4L buffers */
2371 fh->map_mode = ZORAN_MAP_MODE_RAW;
2372 v4l1reqbuf_unlock_and_return:
2373 up(&zr->resource_lock);
2381 struct video_unit *vunit = arg;
2383 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2385 vunit->video = zr->video_dev->minor;
2386 vunit->vbi = VIDEO_NO_UNIT;
2387 vunit->radio = VIDEO_NO_UNIT;
2388 vunit->audio = VIDEO_NO_UNIT;
2389 vunit->teletext = VIDEO_NO_UNIT;
2396 * RJ: In principal we could support subcaptures for V4L grabbing.
2397 * Not even the famous BTTV driver has them, however.
2398 * If there should be a strong demand, one could consider
2399 * to implement them.
2401 case VIDIOCGCAPTURE:
2403 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2409 case VIDIOCSCAPTURE:
2411 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2417 case BUZIOC_G_PARAMS:
2419 struct zoran_params *bparams = arg;
2421 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2423 memset(bparams, 0, sizeof(struct zoran_params));
2424 bparams->major_version = MAJOR_VERSION;
2425 bparams->minor_version = MINOR_VERSION;
2427 down(&zr->resource_lock);
2429 bparams->norm = zr->norm;
2430 bparams->input = zr->input;
2432 bparams->decimation = fh->jpg_settings.decimation;
2433 bparams->HorDcm = fh->jpg_settings.HorDcm;
2434 bparams->VerDcm = fh->jpg_settings.VerDcm;
2435 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2436 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2437 bparams->img_x = fh->jpg_settings.img_x;
2438 bparams->img_y = fh->jpg_settings.img_y;
2439 bparams->img_width = fh->jpg_settings.img_width;
2440 bparams->img_height = fh->jpg_settings.img_height;
2441 bparams->odd_even = fh->jpg_settings.odd_even;
2443 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2444 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2445 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2446 memcpy(bparams->APP_data,
2447 fh->jpg_settings.jpg_comp.APP_data,
2448 sizeof(bparams->APP_data));
2449 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2450 memcpy(bparams->COM_data,
2451 fh->jpg_settings.jpg_comp.COM_data,
2452 sizeof(bparams->COM_data));
2453 bparams->jpeg_markers =
2454 fh->jpg_settings.jpg_comp.jpeg_markers;
2456 up(&zr->resource_lock);
2458 bparams->VFIFO_FB = 0;
2464 case BUZIOC_S_PARAMS:
2466 struct zoran_params *bparams = arg;
2469 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2471 settings.decimation = bparams->decimation;
2472 settings.HorDcm = bparams->HorDcm;
2473 settings.VerDcm = bparams->VerDcm;
2474 settings.TmpDcm = bparams->TmpDcm;
2475 settings.field_per_buff = bparams->field_per_buff;
2476 settings.img_x = bparams->img_x;
2477 settings.img_y = bparams->img_y;
2478 settings.img_width = bparams->img_width;
2479 settings.img_height = bparams->img_height;
2480 settings.odd_even = bparams->odd_even;
2482 settings.jpg_comp.quality = bparams->quality;
2483 settings.jpg_comp.APPn = bparams->APPn;
2484 settings.jpg_comp.APP_len = bparams->APP_len;
2485 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2486 sizeof(bparams->APP_data));
2487 settings.jpg_comp.COM_len = bparams->COM_len;
2488 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2489 sizeof(bparams->COM_data));
2490 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2492 down(&zr->resource_lock);
2494 if (zr->codec_mode != BUZ_MODE_IDLE) {
2497 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2500 goto sparams_unlock_and_return;
2503 /* Check the params first before overwriting our
2505 if (zoran_check_jpg_settings(zr, &settings)) {
2507 goto sparams_unlock_and_return;
2510 fh->jpg_settings = settings;
2511 sparams_unlock_and_return:
2512 up(&zr->resource_lock);
2518 case BUZIOC_REQBUFS:
2520 struct zoran_requestbuffers *breq = arg;
2525 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2526 ZR_DEVNAME(zr), breq->count, breq->size);
2528 /* Enforce reasonable lower and upper limits */
2529 if (breq->count < 4)
2530 breq->count = 4; /* Could be choosen smaller */
2531 if (breq->count > jpg_nbufs)
2532 breq->count = jpg_nbufs;
2533 breq->size = PAGE_ALIGN(breq->size);
2534 if (breq->size < 8192)
2535 breq->size = 8192; /* Arbitrary */
2536 /* breq->size is limited by 1 page for the stat_com
2537 * tables to a Maximum of 2 MB */
2538 if (breq->size > jpg_bufsize)
2539 breq->size = jpg_bufsize;
2540 if (fh->jpg_buffers.need_contiguous &&
2541 breq->size > MAX_KMALLOC_MEM)
2542 breq->size = MAX_KMALLOC_MEM;
2544 down(&zr->resource_lock);
2546 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2549 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2552 goto jpgreqbuf_unlock_and_return;
2555 fh->jpg_buffers.num_buffers = breq->count;
2556 fh->jpg_buffers.buffer_size = breq->size;
2558 if (jpg_fbuffer_alloc(file)) {
2560 goto jpgreqbuf_unlock_and_return;
2563 /* The next mmap will map the MJPEG buffers - could
2564 * also be *_PLAY, but it doesn't matter here */
2565 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2566 jpgreqbuf_unlock_and_return:
2567 up(&zr->resource_lock);
2573 case BUZIOC_QBUF_CAPT:
2575 int *frame = arg, res;
2577 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2578 ZR_DEVNAME(zr), *frame);
2580 down(&zr->resource_lock);
2581 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2582 up(&zr->resource_lock);
2588 case BUZIOC_QBUF_PLAY:
2590 int *frame = arg, res;
2592 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2593 ZR_DEVNAME(zr), *frame);
2595 down(&zr->resource_lock);
2596 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2597 up(&zr->resource_lock);
2605 struct zoran_sync *bsync = arg;
2608 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2610 down(&zr->resource_lock);
2611 res = jpg_sync(file, bsync);
2612 up(&zr->resource_lock);
2618 case BUZIOC_G_STATUS:
2620 struct zoran_status *bstat = arg;
2621 int norm, input, status, res = 0;
2623 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2625 if (zr->codec_mode != BUZ_MODE_IDLE) {
2628 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2633 input = zr->card.input[bstat->input].muxsel;
2634 norm = VIDEO_MODE_AUTO;
2636 down(&zr->resource_lock);
2638 if (zr->codec_mode != BUZ_MODE_IDLE) {
2641 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2644 goto gstat_unlock_and_return;
2647 decoder_command(zr, DECODER_SET_INPUT, &input);
2648 decoder_command(zr, DECODER_SET_NORM, &norm);
2650 /* sleep 1 second */
2653 /* Get status of video decoder */
2654 decoder_command(zr, DECODER_GET_STATUS, &status);
2656 /* restore previous input and norm */
2657 input = zr->card.input[zr->input].muxsel;
2658 decoder_command(zr, DECODER_SET_INPUT, &input);
2659 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2660 gstat_unlock_and_return:
2661 up(&zr->resource_lock);
2665 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2666 if (status & DECODER_STATUS_NTSC)
2667 bstat->norm = VIDEO_MODE_NTSC;
2668 else if (status & DECODER_STATUS_SECAM)
2669 bstat->norm = VIDEO_MODE_SECAM;
2671 bstat->norm = VIDEO_MODE_PAL;
2674 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2683 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2684 * it allows for integrating the JPEG capturing calls inside standard v4l2
2687 case VIDIOC_QUERYCAP:
2689 struct v4l2_capability *cap = arg;
2691 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2693 memset(cap, 0, sizeof(*cap));
2694 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
2695 strncpy(cap->driver, "zoran", sizeof(cap->driver));
2696 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2697 zr->pci_dev->slot_name);
2699 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2701 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2707 case VIDIOC_ENUM_FMT:
2709 struct v4l2_fmtdesc *fmt = arg;
2710 int index = fmt->index, num = -1, i, flag = 0, type =
2713 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2714 ZR_DEVNAME(zr), fmt->index);
2716 switch (fmt->type) {
2717 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2718 flag = ZORAN_FORMAT_CAPTURE;
2720 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2721 flag = ZORAN_FORMAT_PLAYBACK;
2723 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2724 flag = ZORAN_FORMAT_OVERLAY;
2729 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2730 ZR_DEVNAME(zr), fmt->type);
2734 for (i = 0; i < zoran_num_formats; i++) {
2735 if (zoran_formats[i].flags & flag)
2737 if (num == fmt->index)
2740 if (fmt->index < 0 /* late, but not too late */ ||
2741 i == zoran_num_formats)
2744 memset(fmt, 0, sizeof(*fmt));
2747 strncpy(fmt->description, zoran_formats[i].name, 31);
2748 fmt->pixelformat = zoran_formats[i].fourcc;
2749 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2750 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2758 struct v4l2_format *fmt = arg;
2759 int type = fmt->type;
2761 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2763 memset(fmt, 0, sizeof(*fmt));
2766 switch (fmt->type) {
2767 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2769 down(&zr->resource_lock);
2771 fmt->fmt.win.w.left = fh->overlay_settings.x;
2772 fmt->fmt.win.w.top = fh->overlay_settings.y;
2773 fmt->fmt.win.w.width = fh->overlay_settings.width;
2774 fmt->fmt.win.w.height =
2775 fh->overlay_settings.height;
2776 if (fh->overlay_settings.width * 2 >
2778 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2780 fmt->fmt.win.field = V4L2_FIELD_TOP;
2782 up(&zr->resource_lock);
2786 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2787 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2789 down(&zr->resource_lock);
2791 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2792 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2794 fmt->fmt.pix.width =
2795 fh->v4l_settings.width;
2796 fmt->fmt.pix.height =
2797 fh->v4l_settings.height;
2798 fmt->fmt.pix.sizeimage =
2799 fh->v4l_buffers.buffer_size;
2800 fmt->fmt.pix.pixelformat =
2801 fh->v4l_settings.format->fourcc;
2802 fmt->fmt.pix.colorspace =
2803 fh->v4l_settings.format->colorspace;
2804 fmt->fmt.pix.bytesperline = 0;
2805 if (BUZ_MAX_HEIGHT <
2806 (fh->v4l_settings.height * 2))
2807 fmt->fmt.pix.field =
2808 V4L2_FIELD_INTERLACED;
2810 fmt->fmt.pix.field =
2815 fmt->fmt.pix.width =
2816 fh->jpg_settings.img_width /
2817 fh->jpg_settings.HorDcm;
2818 fmt->fmt.pix.height =
2819 fh->jpg_settings.img_height /
2820 (fh->jpg_settings.VerDcm *
2821 fh->jpg_settings.TmpDcm);
2822 fmt->fmt.pix.sizeimage =
2823 zoran_v4l2_calc_bufsize(&fh->
2825 fmt->fmt.pix.pixelformat =
2827 if (fh->jpg_settings.TmpDcm == 1)
2828 fmt->fmt.pix.field =
2830 odd_even ? V4L2_FIELD_SEQ_BT :
2833 fmt->fmt.pix.field =
2835 odd_even ? V4L2_FIELD_TOP :
2838 fmt->fmt.pix.bytesperline = 0;
2839 fmt->fmt.pix.colorspace =
2840 V4L2_COLORSPACE_SMPTE170M;
2843 up(&zr->resource_lock);
2850 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2851 ZR_DEVNAME(zr), fmt->type);
2860 struct v4l2_format *fmt = arg;
2864 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2865 ZR_DEVNAME(zr), fmt->type);
2867 switch (fmt->type) {
2868 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2870 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2871 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2872 fmt->fmt.win.w.width,
2873 fmt->fmt.win.w.height,
2874 fmt->fmt.win.clipcount,
2875 fmt->fmt.win.bitmap);
2876 down(&zr->resource_lock);
2878 setup_window(file, fmt->fmt.win.w.left,
2880 fmt->fmt.win.w.width,
2881 fmt->fmt.win.w.height,
2882 (struct video_clip __user *)
2884 fmt->fmt.win.clipcount,
2885 fmt->fmt.win.bitmap);
2886 up(&zr->resource_lock);
2890 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2891 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2894 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2895 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2896 fmt->fmt.pix.width, fmt->fmt.pix.height,
2897 fmt->fmt.pix.pixelformat,
2898 (char *) &printformat);
2900 if (fmt->fmt.pix.bytesperline > 0) {
2902 KERN_ERR "%s: bpl not supported\n",
2907 /* we can be requested to do JPEG/raw playback/capture */
2909 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2910 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2911 fmt->fmt.pix.pixelformat ==
2912 V4L2_PIX_FMT_MJPEG))) {
2915 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2916 ZR_DEVNAME(zr), fmt->type,
2917 fmt->fmt.pix.pixelformat,
2918 (char *) &printformat);
2922 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2923 down(&zr->resource_lock);
2925 settings = fh->jpg_settings;
2927 if (fh->v4l_buffers.allocated ||
2928 fh->jpg_buffers.allocated) {
2931 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2934 goto sfmtjpg_unlock_and_return;
2937 /* we actually need to set 'real' parameters now */
2938 if ((fmt->fmt.pix.height * 2) >
2940 settings.TmpDcm = 1;
2942 settings.TmpDcm = 2;
2943 settings.decimation = 0;
2944 if (fmt->fmt.pix.height <=
2945 fh->jpg_settings.img_height / 2)
2946 settings.VerDcm = 2;
2948 settings.VerDcm = 1;
2949 if (fmt->fmt.pix.width <=
2950 fh->jpg_settings.img_width / 4)
2951 settings.HorDcm = 4;
2952 else if (fmt->fmt.pix.width <=
2953 fh->jpg_settings.img_width / 2)
2954 settings.HorDcm = 2;
2956 settings.HorDcm = 1;
2957 if (settings.TmpDcm == 1)
2958 settings.field_per_buff = 2;
2960 settings.field_per_buff = 1;
2964 zoran_check_jpg_settings(zr,
2966 goto sfmtjpg_unlock_and_return;
2968 /* it's ok, so set them */
2969 fh->jpg_settings = settings;
2971 /* tell the user what we actually did */
2972 fmt->fmt.pix.width =
2973 settings.img_width / settings.HorDcm;
2974 fmt->fmt.pix.height =
2975 settings.img_height * 2 /
2976 (settings.TmpDcm * settings.VerDcm);
2977 if (settings.TmpDcm == 1)
2978 fmt->fmt.pix.field =
2980 odd_even ? V4L2_FIELD_SEQ_TB :
2983 fmt->fmt.pix.field =
2985 odd_even ? V4L2_FIELD_TOP :
2987 fh->jpg_buffers.buffer_size =
2988 zoran_v4l2_calc_bufsize(&fh->
2990 fmt->fmt.pix.sizeimage =
2991 fh->jpg_buffers.buffer_size;
2993 /* we hereby abuse this variable to show that
2994 * we're gonna do mjpeg capture */
2997 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2998 ZORAN_MAP_MODE_JPG_REC :
2999 ZORAN_MAP_MODE_JPG_PLAY;
3000 sfmtjpg_unlock_and_return:
3001 up(&zr->resource_lock);
3003 for (i = 0; i < zoran_num_formats; i++)
3004 if (fmt->fmt.pix.pixelformat ==
3005 zoran_formats[i].fourcc)
3007 if (i == zoran_num_formats) {
3010 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3012 fmt->fmt.pix.pixelformat,
3013 (char *) &printformat);
3016 down(&zr->resource_lock);
3017 if (fh->jpg_buffers.allocated ||
3018 (fh->v4l_buffers.allocated &&
3019 fh->v4l_buffers.active !=
3023 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3026 goto sfmtv4l_unlock_and_return;
3028 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3029 fmt->fmt.pix.height =
3031 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3032 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3035 zoran_v4l_set_format(file,
3042 goto sfmtv4l_unlock_and_return;
3044 /* tell the user the
3045 * results/missing stuff */
3046 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3048 if (BUZ_MAX_HEIGHT <
3049 (fh->v4l_settings.height * 2))
3050 fmt->fmt.pix.field =
3051 V4L2_FIELD_INTERLACED;
3053 fmt->fmt.pix.field =
3056 fh->map_mode = ZORAN_MAP_MODE_RAW;
3057 sfmtv4l_unlock_and_return:
3058 up(&zr->resource_lock);
3064 dprintk(3, "unsupported\n");
3067 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3068 ZR_DEVNAME(zr), fmt->type);
3078 struct v4l2_framebuffer *fb = arg;
3080 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3082 memset(fb, 0, sizeof(*fb));
3083 down(&zr->resource_lock);
3084 fb->base = zr->buffer.base;
3085 fb->fmt.width = zr->buffer.width;
3086 fb->fmt.height = zr->buffer.height;
3087 if (zr->overlay_settings.format) {
3088 fb->fmt.pixelformat =
3089 fh->overlay_settings.format->fourcc;
3091 fb->fmt.bytesperline = zr->buffer.bytesperline;
3092 up(&zr->resource_lock);
3093 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3094 fb->fmt.field = V4L2_FIELD_INTERLACED;
3095 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3096 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3105 struct v4l2_framebuffer *fb = arg;
3106 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3110 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3111 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3112 fb->fmt.bytesperline, fb->fmt.pixelformat,
3113 (char *) &printformat);
3115 for (i = 0; i < zoran_num_formats; i++)
3116 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3118 if (i == zoran_num_formats) {
3121 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3122 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3123 (char *) &printformat);
3127 down(&zr->resource_lock);
3129 setup_fbuffer(file, fb->base, &zoran_formats[i],
3130 fb->fmt.width, fb->fmt.height,
3131 fb->fmt.bytesperline);
3132 up(&zr->resource_lock);
3138 case VIDIOC_OVERLAY:
3142 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3143 ZR_DEVNAME(zr), *on);
3145 down(&zr->resource_lock);
3146 res = setup_overlay(file, *on);
3147 up(&zr->resource_lock);
3153 case VIDIOC_REQBUFS:
3155 struct v4l2_requestbuffers *req = arg;
3158 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3159 ZR_DEVNAME(zr), req->type);
3161 if (req->memory != V4L2_MEMORY_MMAP) {
3164 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3165 ZR_DEVNAME(zr), req->memory);
3169 down(&zr->resource_lock);
3171 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3174 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3177 goto v4l2reqbuf_unlock_and_return;
3180 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3181 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3183 /* control user input */
3186 if (req->count > v4l_nbufs)
3187 req->count = v4l_nbufs;
3188 fh->v4l_buffers.num_buffers = req->count;
3190 if (v4l_fbuffer_alloc(file)) {
3192 goto v4l2reqbuf_unlock_and_return;
3195 /* The next mmap will map the V4L buffers */
3196 fh->map_mode = ZORAN_MAP_MODE_RAW;
3198 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3199 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3201 /* we need to calculate size ourselves now */
3204 if (req->count > jpg_nbufs)
3205 req->count = jpg_nbufs;
3206 fh->jpg_buffers.num_buffers = req->count;
3207 fh->jpg_buffers.buffer_size =
3208 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3210 if (jpg_fbuffer_alloc(file)) {
3212 goto v4l2reqbuf_unlock_and_return;
3215 /* The next mmap will map the MJPEG buffers */
3216 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3217 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3219 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3224 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3225 ZR_DEVNAME(zr), req->type);
3227 goto v4l2reqbuf_unlock_and_return;
3229 v4l2reqbuf_unlock_and_return:
3230 up(&zr->resource_lock);
3236 case VIDIOC_QUERYBUF:
3238 struct v4l2_buffer *buf = arg;
3239 __u32 type = buf->type;
3240 int index = buf->index, res;
3244 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3245 ZR_DEVNAME(zr), buf->index, buf->type);
3247 memset(buf, 0, sizeof(buf));
3251 down(&zr->resource_lock);
3252 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3253 up(&zr->resource_lock);
3261 struct v4l2_buffer *buf = arg;
3262 int res = 0, codec_mode, buf_type;
3265 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3266 ZR_DEVNAME(zr), buf->type, buf->index);
3268 down(&zr->resource_lock);
3270 switch (fh->map_mode) {
3271 case ZORAN_MAP_MODE_RAW:
3272 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3275 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3276 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3278 goto qbuf_unlock_and_return;
3281 res = zoran_v4l_queue_frame(file, buf->index);
3283 goto qbuf_unlock_and_return;
3284 if (!zr->v4l_memgrab_active &&
3285 fh->v4l_buffers.active == ZORAN_LOCKED)
3286 zr36057_set_memgrab(zr, 1);
3289 case ZORAN_MAP_MODE_JPG_REC:
3290 case ZORAN_MAP_MODE_JPG_PLAY:
3291 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3292 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3293 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3295 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3296 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3299 if (buf->type != buf_type) {
3302 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3303 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3305 goto qbuf_unlock_and_return;
3309 zoran_jpg_queue_frame(file, buf->index,
3312 goto qbuf_unlock_and_return;
3313 if (zr->codec_mode == BUZ_MODE_IDLE &&
3314 fh->jpg_buffers.active == ZORAN_LOCKED) {
3315 zr36057_enable_jpg(zr, codec_mode);
3322 "%s: VIDIOC_QBUF - unsupported type %d\n",
3323 ZR_DEVNAME(zr), buf->type);
3325 goto qbuf_unlock_and_return;
3327 qbuf_unlock_and_return:
3328 up(&zr->resource_lock);
3336 struct v4l2_buffer *buf = arg;
3337 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3339 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3340 ZR_DEVNAME(zr), buf->type);
3342 down(&zr->resource_lock);
3344 switch (fh->map_mode) {
3345 case ZORAN_MAP_MODE_RAW:
3346 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3349 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3350 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3352 goto dqbuf_unlock_and_return;
3355 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3356 if (file->f_flags & O_NONBLOCK &&
3357 zr->v4l_buffers.buffer[num].state !=
3360 goto dqbuf_unlock_and_return;
3362 res = v4l_sync(file, num);
3364 goto dqbuf_unlock_and_return;
3366 zr->v4l_sync_tail++;
3367 res = zoran_v4l2_buffer_status(file, buf, num);
3370 case ZORAN_MAP_MODE_JPG_REC:
3371 case ZORAN_MAP_MODE_JPG_PLAY:
3373 struct zoran_sync bs;
3375 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3376 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3378 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3380 if (buf->type != buf_type) {
3383 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3384 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3386 goto dqbuf_unlock_and_return;
3391 jpg_que_tail & BUZ_MASK_FRAME];
3393 if (file->f_flags & O_NONBLOCK &&
3394 zr->jpg_buffers.buffer[num].state !=
3397 goto dqbuf_unlock_and_return;
3399 res = jpg_sync(file, &bs);
3401 goto dqbuf_unlock_and_return;
3403 zoran_v4l2_buffer_status(file, buf, bs.frame);
3410 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3411 ZR_DEVNAME(zr), buf->type);
3413 goto dqbuf_unlock_and_return;
3415 dqbuf_unlock_and_return:
3416 up(&zr->resource_lock);
3422 case VIDIOC_STREAMON:
3426 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3428 down(&zr->resource_lock);
3430 switch (fh->map_mode) {
3431 case ZORAN_MAP_MODE_RAW: /* raw capture */
3432 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3433 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3435 goto strmon_unlock_and_return;
3438 zr->v4l_buffers.active = fh->v4l_buffers.active =
3440 zr->v4l_settings = fh->v4l_settings;
3442 zr->v4l_sync_tail = zr->v4l_pend_tail;
3443 if (!zr->v4l_memgrab_active &&
3444 zr->v4l_pend_head != zr->v4l_pend_tail) {
3445 zr36057_set_memgrab(zr, 1);
3449 case ZORAN_MAP_MODE_JPG_REC:
3450 case ZORAN_MAP_MODE_JPG_PLAY:
3451 /* what is the codec mode right now? */
3452 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3453 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3455 goto strmon_unlock_and_return;
3458 zr->jpg_buffers.active = fh->jpg_buffers.active =
3461 if (zr->jpg_que_head != zr->jpg_que_tail) {
3462 /* Start the jpeg codec when the first frame is queued */
3470 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3471 ZR_DEVNAME(zr), fh->map_mode);
3473 goto strmon_unlock_and_return;
3475 strmon_unlock_and_return:
3476 up(&zr->resource_lock);
3482 case VIDIOC_STREAMOFF:
3486 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3488 down(&zr->resource_lock);
3490 switch (fh->map_mode) {
3491 case ZORAN_MAP_MODE_RAW: /* raw capture */
3492 if (fh->v4l_buffers.active == ZORAN_FREE &&
3493 zr->v4l_buffers.active != ZORAN_FREE) {
3494 res = -EPERM; /* stay off other's settings! */
3495 goto strmoff_unlock_and_return;
3497 if (zr->v4l_buffers.active == ZORAN_FREE)
3498 goto strmoff_unlock_and_return;
3500 /* unload capture */
3501 if (zr->v4l_memgrab_active)
3502 zr36057_set_memgrab(zr, 0);
3504 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3505 zr->v4l_buffers.buffer[i].state =
3507 fh->v4l_buffers = zr->v4l_buffers;
3509 zr->v4l_buffers.active = fh->v4l_buffers.active =
3512 zr->v4l_grab_seq = 0;
3513 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3514 zr->v4l_sync_tail = 0;
3518 case ZORAN_MAP_MODE_JPG_REC:
3519 case ZORAN_MAP_MODE_JPG_PLAY:
3520 if (fh->jpg_buffers.active == ZORAN_FREE &&
3521 zr->jpg_buffers.active != ZORAN_FREE) {
3522 res = -EPERM; /* stay off other's settings! */
3523 goto strmoff_unlock_and_return;
3525 if (zr->jpg_buffers.active == ZORAN_FREE)
3526 goto strmoff_unlock_and_return;
3531 ZORAN_MAP_MODE_JPG_REC) ?
3532 BUZ_MODE_MOTION_COMPRESS :
3533 BUZ_MODE_MOTION_DECOMPRESS);
3535 goto strmoff_unlock_and_return;
3540 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3541 ZR_DEVNAME(zr), fh->map_mode);
3543 goto strmoff_unlock_and_return;
3545 strmoff_unlock_and_return:
3546 up(&zr->resource_lock);
3552 case VIDIOC_QUERYCTRL:
3554 struct v4l2_queryctrl *ctrl = arg;
3556 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3557 ZR_DEVNAME(zr), ctrl->id);
3559 /* we only support hue/saturation/contrast/brightness */
3560 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3561 ctrl->id > V4L2_CID_HUE)
3565 memset(ctrl, 0, sizeof(*ctrl));
3570 case V4L2_CID_BRIGHTNESS:
3571 strncpy(ctrl->name, "Brightness", 31);
3573 case V4L2_CID_CONTRAST:
3574 strncpy(ctrl->name, "Contrast", 31);
3576 case V4L2_CID_SATURATION:
3577 strncpy(ctrl->name, "Saturation", 31);
3580 strncpy(ctrl->name, "Hue", 31);
3585 ctrl->maximum = 65535;
3587 ctrl->default_value = 32768;
3588 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3596 struct v4l2_control *ctrl = arg;
3598 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3599 ZR_DEVNAME(zr), ctrl->id);
3601 /* we only support hue/saturation/contrast/brightness */
3602 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3603 ctrl->id > V4L2_CID_HUE)
3606 down(&zr->resource_lock);
3608 case V4L2_CID_BRIGHTNESS:
3609 ctrl->value = zr->brightness;
3611 case V4L2_CID_CONTRAST:
3612 ctrl->value = zr->contrast;
3614 case V4L2_CID_SATURATION:
3615 ctrl->value = zr->saturation;
3618 ctrl->value = zr->hue;
3621 up(&zr->resource_lock);
3629 struct v4l2_control *ctrl = arg;
3630 struct video_picture pict;
3632 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3633 ZR_DEVNAME(zr), ctrl->id);
3635 /* we only support hue/saturation/contrast/brightness */
3636 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3637 ctrl->id > V4L2_CID_HUE)
3640 if (ctrl->value < 0 || ctrl->value > 65535) {
3643 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3644 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3648 down(&zr->resource_lock);
3650 case V4L2_CID_BRIGHTNESS:
3651 zr->brightness = ctrl->value;
3653 case V4L2_CID_CONTRAST:
3654 zr->contrast = ctrl->value;
3656 case V4L2_CID_SATURATION:
3657 zr->saturation = ctrl->value;
3660 zr->hue = ctrl->value;
3663 pict.brightness = zr->brightness;
3664 pict.contrast = zr->contrast;
3665 pict.colour = zr->saturation;
3668 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3670 up(&zr->resource_lock);
3676 case VIDIOC_ENUMSTD:
3678 struct v4l2_standard *std = arg;
3680 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3681 ZR_DEVNAME(zr), std->index);
3683 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3686 int id = std->index;
3687 memset(std, 0, sizeof(*std));
3691 if (std->index == zr->card.norms) {
3692 /* if we have autodetect, ... */
3693 struct video_decoder_capability caps;
3694 decoder_command(zr, DECODER_GET_CAPABILITIES,
3696 if (caps.flags & VIDEO_DECODER_AUTO) {
3697 std->id = V4L2_STD_ALL;
3698 strncpy(std->name, "Autodetect", 31);
3703 switch (std->index) {
3705 std->id = V4L2_STD_PAL;
3706 strncpy(std->name, "PAL", 31);
3707 std->frameperiod.numerator = 1;
3708 std->frameperiod.denominator = 25;
3709 std->framelines = zr->card.tvn[0]->Ht;
3712 std->id = V4L2_STD_NTSC;
3713 strncpy(std->name, "NTSC", 31);
3714 std->frameperiod.numerator = 1001;
3715 std->frameperiod.denominator = 30000;
3716 std->framelines = zr->card.tvn[1]->Ht;
3719 std->id = V4L2_STD_SECAM;
3720 strncpy(std->name, "SECAM", 31);
3721 std->frameperiod.numerator = 1;
3722 std->frameperiod.denominator = 25;
3723 std->framelines = zr->card.tvn[2]->Ht;
3733 v4l2_std_id *std = arg;
3736 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3738 down(&zr->resource_lock);
3740 up(&zr->resource_lock);
3743 case VIDEO_MODE_PAL:
3744 *std = V4L2_STD_PAL;
3746 case VIDEO_MODE_NTSC:
3747 *std = V4L2_STD_NTSC;
3749 case VIDEO_MODE_SECAM:
3750 *std = V4L2_STD_SECAM;
3760 int norm = -1, res = 0;
3761 v4l2_std_id *std = arg;
3763 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3764 ZR_DEVNAME(zr), *std);
3766 if (*std == V4L2_STD_PAL)
3767 norm = VIDEO_MODE_PAL;
3768 else if (*std == V4L2_STD_NTSC)
3769 norm = VIDEO_MODE_NTSC;
3770 else if (*std == V4L2_STD_SECAM)
3771 norm = VIDEO_MODE_SECAM;
3772 else if (*std == V4L2_STD_ALL)
3773 norm = VIDEO_MODE_AUTO;
3777 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3778 ZR_DEVNAME(zr), *std);
3782 down(&zr->resource_lock);
3783 if ((res = zoran_set_norm(zr, norm)))
3784 goto sstd_unlock_and_return;
3786 res = wait_grab_pending(zr);
3787 sstd_unlock_and_return:
3788 up(&zr->resource_lock);
3793 case VIDIOC_ENUMINPUT:
3795 struct v4l2_input *inp = arg;
3798 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3799 ZR_DEVNAME(zr), inp->index);
3801 if (inp->index < 0 || inp->index >= zr->card.inputs)
3804 int id = inp->index;
3805 memset(inp, 0, sizeof(*inp));
3809 strncpy(inp->name, zr->card.input[inp->index].name,
3810 sizeof(inp->name) - 1);
3811 inp->type = V4L2_INPUT_TYPE_CAMERA;
3812 inp->std = V4L2_STD_ALL;
3814 /* Get status of video decoder */
3815 down(&zr->resource_lock);
3816 decoder_command(zr, DECODER_GET_STATUS, &status);
3817 up(&zr->resource_lock);
3819 if (!(status & DECODER_STATUS_GOOD)) {
3820 inp->status |= V4L2_IN_ST_NO_POWER;
3821 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3823 if (!(status & DECODER_STATUS_COLOR))
3824 inp->status |= V4L2_IN_ST_NO_COLOR;
3830 case VIDIOC_G_INPUT:
3834 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3836 down(&zr->resource_lock);
3838 up(&zr->resource_lock);
3844 case VIDIOC_S_INPUT:
3846 int *input = arg, res = 0;
3848 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3849 ZR_DEVNAME(zr), *input);
3851 down(&zr->resource_lock);
3852 if ((res = zoran_set_input(zr, *input)))
3853 goto sinput_unlock_and_return;
3855 /* Make sure the changes come into effect */
3856 res = wait_grab_pending(zr);
3857 sinput_unlock_and_return:
3858 up(&zr->resource_lock);
3863 case VIDIOC_ENUMOUTPUT:
3865 struct v4l2_output *outp = arg;
3867 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3868 ZR_DEVNAME(zr), outp->index);
3870 if (outp->index != 0)
3873 memset(outp, 0, sizeof(*outp));
3875 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3876 strncpy(outp->name, "Autodetect", 31);
3882 case VIDIOC_G_OUTPUT:
3886 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3894 case VIDIOC_S_OUTPUT:
3898 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3899 ZR_DEVNAME(zr), *output);
3908 /* cropping (sub-frame capture) */
3909 case VIDIOC_CROPCAP:
3911 struct v4l2_cropcap *cropcap = arg;
3912 int type = cropcap->type, res = 0;
3914 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3915 ZR_DEVNAME(zr), cropcap->type);
3917 memset(cropcap, 0, sizeof(*cropcap));
3918 cropcap->type = type;
3920 down(&zr->resource_lock);
3922 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3923 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3924 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3927 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3930 goto cropcap_unlock_and_return;
3933 cropcap->bounds.top = cropcap->bounds.left = 0;
3934 cropcap->bounds.width = BUZ_MAX_WIDTH;
3935 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3936 cropcap->defrect.top = cropcap->defrect.left = 0;
3937 cropcap->defrect.width = BUZ_MIN_WIDTH;
3938 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3939 cropcap_unlock_and_return:
3940 up(&zr->resource_lock);
3947 struct v4l2_crop *crop = arg;
3948 int type = crop->type, res = 0;
3950 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3951 ZR_DEVNAME(zr), crop->type);
3953 memset(crop, 0, sizeof(*crop));
3956 down(&zr->resource_lock);
3958 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3959 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3960 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3963 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3966 goto gcrop_unlock_and_return;
3969 crop->c.top = fh->jpg_settings.img_y;
3970 crop->c.left = fh->jpg_settings.img_x;
3971 crop->c.width = fh->jpg_settings.img_width;
3972 crop->c.height = fh->jpg_settings.img_height;
3974 gcrop_unlock_and_return:
3975 up(&zr->resource_lock);
3983 struct v4l2_crop *crop = arg;
3986 settings = fh->jpg_settings;
3990 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3991 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3992 crop->c.width, crop->c.height);
3994 down(&zr->resource_lock);
3996 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3999 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
4002 goto scrop_unlock_and_return;
4005 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
4006 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4007 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4010 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4013 goto scrop_unlock_and_return;
4016 /* move into a form that we understand */
4017 settings.img_x = crop->c.left;
4018 settings.img_y = crop->c.top;
4019 settings.img_width = crop->c.width;
4020 settings.img_height = crop->c.height;
4022 /* check validity */
4023 if ((res = zoran_check_jpg_settings(zr, &settings)))
4024 goto scrop_unlock_and_return;
4027 fh->jpg_settings = settings;
4029 scrop_unlock_and_return:
4030 up(&zr->resource_lock);
4035 case VIDIOC_G_JPEGCOMP:
4037 struct v4l2_jpegcompression *params = arg;
4039 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4042 memset(params, 0, sizeof(*params));
4044 down(&zr->resource_lock);
4046 params->quality = fh->jpg_settings.jpg_comp.quality;
4047 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4048 memcpy(params->APP_data,
4049 fh->jpg_settings.jpg_comp.APP_data,
4050 fh->jpg_settings.jpg_comp.APP_len);
4051 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4052 memcpy(params->COM_data,
4053 fh->jpg_settings.jpg_comp.COM_data,
4054 fh->jpg_settings.jpg_comp.COM_len);
4055 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4056 params->jpeg_markers =
4057 fh->jpg_settings.jpg_comp.jpeg_markers;
4059 up(&zr->resource_lock);
4065 case VIDIOC_S_JPEGCOMP:
4067 struct v4l2_jpegcompression *params = arg;
4070 settings = fh->jpg_settings;
4074 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4075 ZR_DEVNAME(zr), params->quality, params->APPn,
4076 params->APP_len, params->COM_len);
4078 settings.jpg_comp = *params;
4080 down(&zr->resource_lock);
4082 if (fh->v4l_buffers.active != ZORAN_FREE ||
4083 fh->jpg_buffers.active != ZORAN_FREE) {
4086 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4089 goto sjpegc_unlock_and_return;
4092 if ((res = zoran_check_jpg_settings(zr, &settings)))
4093 goto sjpegc_unlock_and_return;
4094 if (!fh->jpg_buffers.allocated)
4095 fh->jpg_buffers.buffer_size =
4096 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4097 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4098 sjpegc_unlock_and_return:
4099 up(&zr->resource_lock);
4105 case VIDIOC_QUERYSTD: /* why is this useful? */
4107 v4l2_std_id *std = arg;
4110 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4111 ZR_DEVNAME(zr), *std);
4113 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4114 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4115 zr->card.norms == 3)) {
4123 case VIDIOC_TRY_FMT:
4125 struct v4l2_format *fmt = arg;
4128 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4129 ZR_DEVNAME(zr), fmt->type);
4131 switch (fmt->type) {
4132 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4133 down(&zr->resource_lock);
4135 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4136 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4137 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4138 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4139 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4140 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4141 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4142 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4144 up(&zr->resource_lock);
4147 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4148 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4149 if (fmt->fmt.pix.bytesperline > 0)
4152 down(&zr->resource_lock);
4154 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4155 settings = fh->jpg_settings;
4157 /* we actually need to set 'real' parameters now */
4158 if ((fmt->fmt.pix.height * 2) >
4160 settings.TmpDcm = 1;
4162 settings.TmpDcm = 2;
4163 settings.decimation = 0;
4164 if (fmt->fmt.pix.height <=
4165 fh->jpg_settings.img_height / 2)
4166 settings.VerDcm = 2;
4168 settings.VerDcm = 1;
4169 if (fmt->fmt.pix.width <=
4170 fh->jpg_settings.img_width / 4)
4171 settings.HorDcm = 4;
4172 else if (fmt->fmt.pix.width <=
4173 fh->jpg_settings.img_width / 2)
4174 settings.HorDcm = 2;
4176 settings.HorDcm = 1;
4177 if (settings.TmpDcm == 1)
4178 settings.field_per_buff = 2;
4180 settings.field_per_buff = 1;
4184 zoran_check_jpg_settings(zr,
4186 goto tryfmt_unlock_and_return;
4188 /* tell the user what we actually did */
4189 fmt->fmt.pix.width =
4190 settings.img_width / settings.HorDcm;
4191 fmt->fmt.pix.height =
4192 settings.img_height * 2 /
4193 (settings.TmpDcm * settings.VerDcm);
4194 if (settings.TmpDcm == 1)
4195 fmt->fmt.pix.field =
4197 odd_even ? V4L2_FIELD_SEQ_TB :
4200 fmt->fmt.pix.field =
4202 odd_even ? V4L2_FIELD_TOP :
4205 fmt->fmt.pix.sizeimage =
4206 zoran_v4l2_calc_bufsize(&settings);
4207 } else if (fmt->type ==
4208 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4211 for (i = 0; i < zoran_num_formats; i++)
4212 if (zoran_formats[i].fourcc ==
4213 fmt->fmt.pix.pixelformat)
4215 if (i == zoran_num_formats) {
4217 goto tryfmt_unlock_and_return;
4220 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4221 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4222 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4223 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4224 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4225 fmt->fmt.pix.height =
4227 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4228 fmt->fmt.pix.height =
4232 goto tryfmt_unlock_and_return;
4234 tryfmt_unlock_and_return:
4235 up(&zr->resource_lock);
4250 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4251 ZR_DEVNAME(zr), cmd);
4252 return -ENOIOCTLCMD;
4261 zoran_ioctl (struct inode *inode,
4266 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4270 zoran_poll (struct file *file,
4273 struct zoran_fh *fh = file->private_data;
4274 struct zoran *zr = fh->zr;
4275 wait_queue_head_t *queue = NULL;
4278 /* we should check whether buffers are ready to be synced on
4279 * (w/o waits - O_NONBLOCK) here
4280 * if ready for read (sync), return POLLIN|POLLRDNORM,
4281 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4282 * if error, return POLLERR,
4283 * if no buffers queued or so, return POLLNVAL
4286 down(&zr->resource_lock);
4288 switch (fh->map_mode) {
4289 case ZORAN_MAP_MODE_RAW:
4290 if (fh->v4l_buffers.active == ZORAN_FREE ||
4291 zr->v4l_pend_head == zr->v4l_pend_tail) {
4293 "%s: zoran_poll() - no buffers queued\n",
4296 goto poll_unlock_and_return;
4298 queue = &zr->v4l_capq;
4299 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4300 poll_wait(file, queue, wait);
4301 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4302 res = POLLIN | POLLRDNORM;
4305 case ZORAN_MAP_MODE_JPG_REC:
4306 case ZORAN_MAP_MODE_JPG_PLAY:
4307 if (fh->jpg_buffers.active == ZORAN_FREE ||
4308 zr->jpg_que_head == zr->jpg_que_tail) {
4310 "%s: zoran_poll() - no buffers queued\n",
4313 goto poll_unlock_and_return;
4315 queue = &zr->jpg_capq;
4316 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4317 poll_wait(file, queue, wait);
4318 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4319 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4320 res = POLLIN | POLLRDNORM;
4322 res = POLLOUT | POLLWRNORM;
4328 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4329 ZR_DEVNAME(zr), fh->map_mode);
4331 goto poll_unlock_and_return;
4334 poll_unlock_and_return:
4335 up(&zr->resource_lock);
4342 * This maps the buffers to user space.
4344 * Depending on the state of fh->map_mode
4345 * the V4L or the MJPEG buffers are mapped
4346 * per buffer or all together
4348 * Note that we need to connect to some
4349 * unmap signal event to unmap the de-allocate
4350 * the buffer accordingly (zoran_vm_close())
4354 zoran_vm_open (struct vm_area_struct *vma)
4356 struct zoran_mapping *map = vma->vm_private_data;
4362 zoran_vm_close (struct vm_area_struct *vma)
4364 struct zoran_mapping *map = vma->vm_private_data;
4365 struct file *file = map->file;
4366 struct zoran_fh *fh = file->private_data;
4367 struct zoran *zr = fh->zr;
4371 if (map->count == 0) {
4372 switch (fh->map_mode) {
4373 case ZORAN_MAP_MODE_JPG_REC:
4374 case ZORAN_MAP_MODE_JPG_PLAY:
4376 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4379 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4380 if (fh->jpg_buffers.buffer[i].map == map) {
4381 fh->jpg_buffers.buffer[i].map =
4387 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4388 if (fh->jpg_buffers.buffer[i].map)
4390 if (i == fh->jpg_buffers.num_buffers) {
4391 down(&zr->resource_lock);
4393 if (fh->jpg_buffers.active != ZORAN_FREE) {
4394 jpg_qbuf(file, -1, zr->codec_mode);
4395 zr->jpg_buffers.allocated = 0;
4396 zr->jpg_buffers.active =
4397 fh->jpg_buffers.active =
4400 //jpg_fbuffer_free(file);
4401 fh->jpg_buffers.allocated = 0;
4402 fh->jpg_buffers.ready_to_be_freed = 1;
4404 up(&zr->resource_lock);
4409 case ZORAN_MAP_MODE_RAW:
4411 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4414 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4415 if (fh->v4l_buffers.buffer[i].map == map) {
4417 fh->v4l_buffers.buffer[i].map =
4423 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4424 if (fh->v4l_buffers.buffer[i].map)
4426 if (i == fh->v4l_buffers.num_buffers) {
4427 down(&zr->resource_lock);
4429 if (fh->v4l_buffers.active != ZORAN_FREE) {
4430 zr36057_set_memgrab(zr, 0);
4431 zr->v4l_buffers.allocated = 0;
4432 zr->v4l_buffers.active =
4433 fh->v4l_buffers.active =
4436 //v4l_fbuffer_free(file);
4437 fh->v4l_buffers.allocated = 0;
4438 fh->v4l_buffers.ready_to_be_freed = 1;
4440 up(&zr->resource_lock);
4447 "%s: munmap() - internal error - unknown map mode %d\n",
4448 ZR_DEVNAME(zr), fh->map_mode);
4455 static struct vm_operations_struct zoran_vm_ops = {
4456 .open = zoran_vm_open,
4457 .close = zoran_vm_close,
4461 zoran_mmap (struct file *file,
4462 struct vm_area_struct *vma)
4464 struct zoran_fh *fh = file->private_data;
4465 struct zoran *zr = fh->zr;
4466 unsigned long size = (vma->vm_end - vma->vm_start);
4467 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4469 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4471 struct zoran_mapping *map;
4475 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4477 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4478 vma->vm_start, vma->vm_end, size);
4480 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4481 !(vma->vm_flags & VM_WRITE)) {
4484 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4489 switch (fh->map_mode) {
4491 case ZORAN_MAP_MODE_JPG_REC:
4492 case ZORAN_MAP_MODE_JPG_PLAY:
4495 down(&zr->resource_lock);
4497 /* Map the MJPEG buffers */
4498 if (!fh->jpg_buffers.allocated) {
4501 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4504 goto jpg_mmap_unlock_and_return;
4507 first = offset / fh->jpg_buffers.buffer_size;
4508 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4509 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4510 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4511 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4512 last >= fh->jpg_buffers.num_buffers) {
4515 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4516 ZR_DEVNAME(zr), offset, size,
4517 fh->jpg_buffers.buffer_size,
4518 fh->jpg_buffers.num_buffers);
4520 goto jpg_mmap_unlock_and_return;
4522 for (i = first; i <= last; i++) {
4523 if (fh->jpg_buffers.buffer[i].map) {
4526 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4529 goto jpg_mmap_unlock_and_return;
4533 /* map these buffers (v4l_buffers[i]) */
4534 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4537 goto jpg_mmap_unlock_and_return;
4542 vma->vm_ops = &zoran_vm_ops;
4543 vma->vm_flags |= VM_DONTEXPAND;
4544 vma->vm_private_data = map;
4546 for (i = first; i <= last; i++) {
4548 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4551 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4552 frag_tab[2 * j + 1]) & ~1) << 1;
4557 le32_to_cpu((unsigned long) fh->jpg_buffers.
4558 buffer[i].frag_tab[2 * j]);
4559 /* should just be pos on i386 */
4560 page = virt_to_phys(bus_to_virt(pos))
4562 if (remap_pfn_range(vma, start, page,
4563 todo, PAGE_SHARED)) {
4566 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4569 goto jpg_mmap_unlock_and_return;
4575 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4576 frag_tab[2 * j + 1]) & 1)
4577 break; /* was last fragment */
4579 fh->jpg_buffers.buffer[i].map = map;
4584 jpg_mmap_unlock_and_return:
4585 up(&zr->resource_lock);
4589 case ZORAN_MAP_MODE_RAW:
4591 down(&zr->resource_lock);
4593 /* Map the V4L buffers */
4594 if (!fh->v4l_buffers.allocated) {
4597 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4600 goto v4l_mmap_unlock_and_return;
4603 first = offset / fh->v4l_buffers.buffer_size;
4604 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4605 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4606 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4607 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4608 last >= fh->v4l_buffers.buffer_size) {
4611 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4612 ZR_DEVNAME(zr), offset, size,
4613 fh->v4l_buffers.buffer_size,
4614 fh->v4l_buffers.num_buffers);
4616 goto v4l_mmap_unlock_and_return;
4618 for (i = first; i <= last; i++) {
4619 if (fh->v4l_buffers.buffer[i].map) {
4622 "%s: mmap(V4L) - buffer %d already mapped\n",
4625 goto v4l_mmap_unlock_and_return;
4629 /* map these buffers (v4l_buffers[i]) */
4630 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4633 goto v4l_mmap_unlock_and_return;
4638 vma->vm_ops = &zoran_vm_ops;
4639 vma->vm_flags |= VM_DONTEXPAND;
4640 vma->vm_private_data = map;
4642 for (i = first; i <= last; i++) {
4644 if (todo > fh->v4l_buffers.buffer_size)
4645 todo = fh->v4l_buffers.buffer_size;
4646 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4647 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4648 todo, PAGE_SHARED)) {
4651 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4654 goto v4l_mmap_unlock_and_return;
4658 fh->v4l_buffers.buffer[i].map = map;
4662 v4l_mmap_unlock_and_return:
4663 up(&zr->resource_lock);
4670 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4671 ZR_DEVNAME(zr), fh->map_mode);
4678 static struct file_operations zoran_fops = {
4679 .owner = THIS_MODULE,
4681 .release = zoran_close,
4682 .ioctl = zoran_ioctl,
4683 .llseek = no_llseek,
4685 .write = zoran_write,
4690 struct video_device zoran_template __devinitdata = {
4692 .type = ZORAN_VID_TYPE,
4694 .type2 = ZORAN_V4L2_VID_FLAGS,
4696 .hardware = ZORAN_HARDWARE,
4697 .fops = &zoran_fops,
4698 .release = &zoran_vdev_release,