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>
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
60 #include <linux/spinlock.h>
61 #define MAP_NR(x) virt_to_page(x)
62 #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
63 #define ZORAN_VID_TYPE ( \
69 VID_TYPE_MJPEG_DECODER | \
70 VID_TYPE_MJPEG_ENCODER \
73 #include <linux/videodev.h>
74 #include "videocodec.h"
77 #include <asm/uaccess.h>
78 #include <linux/proc_fs.h>
80 #include <linux/video_decoder.h>
81 #include <linux/video_encoder.h>
83 #include "zoran_device.h"
84 #include "zoran_card.h"
87 /* we declare some card type definitions here, they mean
88 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
89 #define ZORAN_V4L2_VID_FLAGS ( \
91 V4L2_CAP_VIDEO_CAPTURE |\
92 V4L2_CAP_VIDEO_OUTPUT |\
93 V4L2_CAP_VIDEO_OVERLAY \
97 #include <asm/byteorder.h>
99 const struct zoran_format zoran_formats[] = {
101 .name = "15-bit RGB",
102 .palette = VIDEO_PALETTE_RGB555,
104 #ifdef __LITTLE_ENDIAN
105 .fourcc = V4L2_PIX_FMT_RGB555,
107 .fourcc = V4L2_PIX_FMT_RGB555X,
109 .colorspace = V4L2_COLORSPACE_SRGB,
112 .flags = ZORAN_FORMAT_CAPTURE |
113 ZORAN_FORMAT_OVERLAY,
115 .name = "16-bit RGB",
116 .palette = VIDEO_PALETTE_RGB565,
118 #ifdef __LITTLE_ENDIAN
119 .fourcc = V4L2_PIX_FMT_RGB565,
121 .fourcc = V4L2_PIX_FMT_RGB565X,
123 .colorspace = V4L2_COLORSPACE_SRGB,
126 .flags = ZORAN_FORMAT_CAPTURE |
127 ZORAN_FORMAT_OVERLAY,
129 .name = "24-bit RGB",
130 .palette = VIDEO_PALETTE_RGB24,
132 #ifdef __LITTLE_ENDIAN
133 .fourcc = V4L2_PIX_FMT_BGR24,
135 .fourcc = V4L2_PIX_FMT_RGB24,
137 .colorspace = V4L2_COLORSPACE_SRGB,
140 .flags = ZORAN_FORMAT_CAPTURE |
141 ZORAN_FORMAT_OVERLAY,
143 .name = "32-bit RGB",
144 .palette = VIDEO_PALETTE_RGB32,
146 #ifdef __LITTLE_ENDIAN
147 .fourcc = V4L2_PIX_FMT_BGR32,
149 .fourcc = V4L2_PIX_FMT_RGB32,
151 .colorspace = V4L2_COLORSPACE_SRGB,
154 .flags = ZORAN_FORMAT_CAPTURE |
155 ZORAN_FORMAT_OVERLAY,
157 .name = "4:2:2, packed, YUYV",
158 .palette = VIDEO_PALETTE_YUV422,
160 .fourcc = V4L2_PIX_FMT_YUYV,
161 .colorspace = V4L2_COLORSPACE_SMPTE170M,
164 .flags = ZORAN_FORMAT_CAPTURE |
165 ZORAN_FORMAT_OVERLAY,
167 .name = "Hardware-encoded Motion-JPEG",
170 .fourcc = V4L2_PIX_FMT_MJPEG,
171 .colorspace = V4L2_COLORSPACE_SMPTE170M,
174 .flags = ZORAN_FORMAT_CAPTURE |
175 ZORAN_FORMAT_PLAYBACK |
176 ZORAN_FORMAT_COMPRESSED,
179 const int zoran_num_formats =
180 (sizeof(zoran_formats) / sizeof(struct zoran_format));
182 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
183 #if !defined(CONFIG_BIGPHYS_AREA)
184 //#undef CONFIG_BIGPHYS_AREA
185 #define BUZ_USE_HIMEM
188 #if defined(CONFIG_BIGPHYS_AREA)
189 # include <linux/bigphysarea.h>
192 extern int *zr_debug;
194 #define dprintk(num, format, args...) \
196 if (*zr_debug >= num) \
197 printk(format, ##args); \
200 extern int v4l_nbufs;
201 extern int v4l_bufsize;
202 extern int jpg_nbufs;
203 extern int jpg_bufsize;
204 extern int pass_through;
206 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
207 MODULE_PARM(lock_norm, "i");
208 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
211 /* small helper function for calculating buffersizes for v4l2
212 * we calculate the nearest higher power-of-two, which
213 * will be the recommended buffersize */
215 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
217 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
218 __u32 num = (1024 * 512) / (div);
227 if (result > jpg_bufsize)
235 /* forward references */
236 static void v4l_fbuffer_free(struct file *file);
237 static void jpg_fbuffer_free(struct file *file);
240 * Allocate the V4L grab buffers
242 * These have to be pysically contiguous.
243 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
244 * else we try to allocate them with bigphysarea_alloc_pages
245 * if the bigphysarea patch is present in the kernel,
246 * else we try to use high memory (if the user has bootet
247 * Linux with the necessary memory left over).
250 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
252 get_high_mem (unsigned long size)
255 * Check if there is usable memory at the end of Linux memory
256 * of at least size. Return the physical address of this memory,
257 * return 0 on failure.
259 * The idea is from Alexandro Rubini's book "Linux device drivers".
260 * The driver from him which is downloadable from O'Reilly's
261 * web site misses the "virt_to_phys(high_memory)" part
262 * (and therefore doesn't work at all - at least with 2.2.x kernels).
264 * It should be unnecessary to mention that THIS IS DANGEROUS,
265 * if more than one driver at a time has the idea to use this memory!!!!
268 volatile unsigned char *mem;
270 unsigned long hi_mem_ph;
273 /* Map the high memory to user space */
275 hi_mem_ph = virt_to_phys(high_memory);
277 mem = ioremap(hi_mem_ph, size);
280 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
285 for (i = 0; i < size; i++) {
286 /* Check if it is memory */
295 mem[i] = 0; /* zero out memory */
297 /* give the kernel air to breath */
298 if ((i & 0x3ffff) == 0x3ffff)
302 iounmap((void *) mem);
307 "%s: get_high_mem() - requested %lu, avail %lu\n",
308 ZORAN_NAME, size, i);
317 v4l_fbuffer_alloc (struct file *file)
319 struct zoran_fh *fh = file->private_data;
320 struct zoran *zr = fh->zr;
323 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
324 unsigned long pmem = 0;
327 /* we might have old buffers lying around... */
328 if (fh->v4l_buffers.ready_to_be_freed) {
329 v4l_fbuffer_free(file);
332 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
333 if (fh->v4l_buffers.buffer[i].fbuffer)
336 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
340 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
344 (unsigned char *) kmalloc(fh->v4l_buffers.
350 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
352 v4l_fbuffer_free(file);
355 fh->v4l_buffers.buffer[i].fbuffer = mem;
356 fh->v4l_buffers.buffer[i].fbuffer_phys =
358 fh->v4l_buffers.buffer[i].fbuffer_bus =
360 for (off = 0; off < fh->v4l_buffers.buffer_size;
362 SetPageReserved(MAP_NR(mem + off));
365 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
366 ZR_DEVNAME(zr), i, (unsigned long) mem,
369 #if defined(CONFIG_BIGPHYS_AREA)
370 /* Use bigphysarea_alloc_pages */
373 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
377 (unsigned char *) bigphysarea_alloc_pages(n, 0,
382 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
384 v4l_fbuffer_free(file);
387 fh->v4l_buffers.buffer[i].fbuffer = mem;
388 fh->v4l_buffers.buffer[i].fbuffer_phys =
390 fh->v4l_buffers.buffer[i].fbuffer_bus =
394 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
395 ZR_DEVNAME(zr), i, (unsigned) mem,
396 (unsigned) virt_to_bus(mem));
398 /* Zero out the allocated memory */
399 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
400 fh->v4l_buffers.buffer_size);
401 #elif defined(BUZ_USE_HIMEM)
403 /* Use high memory which has been left at boot time */
405 /* Ok., Ok. this is an evil hack - we make
406 * the assumption that physical addresses are
407 * the same as bus addresses (true at least
408 * for Intel processors). The whole method of
409 * obtaining and using this memory is not very
410 * nice - but I hope it saves some poor users
411 * from kernel hacking, which might have even
412 * more evil results */
416 fh->v4l_buffers.num_buffers *
417 fh->v4l_buffers.buffer_size;
419 pmem = get_high_mem(size);
423 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
424 ZR_DEVNAME(zr), size >> 10);
427 fh->v4l_buffers.buffer[0].fbuffer = 0;
428 fh->v4l_buffers.buffer[0].fbuffer_phys =
430 fh->v4l_buffers.buffer[0].fbuffer_bus =
434 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
435 ZR_DEVNAME(zr), size >> 10);
437 fh->v4l_buffers.buffer[i].fbuffer = 0;
438 fh->v4l_buffers.buffer[i].fbuffer_phys =
439 pmem + i * fh->v4l_buffers.buffer_size;
440 fh->v4l_buffers.buffer[i].fbuffer_bus =
441 pmem + i * fh->v4l_buffers.buffer_size;
444 /* No bigphysarea present, usage of high memory disabled,
445 * but user wants buffers of more than MAX_KMALLOC_MEM */
448 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
452 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
453 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
454 fh->v4l_buffers.buffer_size >> 10);
460 fh->v4l_buffers.allocated = 1;
465 /* free the V4L grab buffers */
467 v4l_fbuffer_free (struct file *file)
469 struct zoran_fh *fh = file->private_data;
470 struct zoran *zr = fh->zr;
474 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
476 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
477 if (!fh->v4l_buffers.buffer[i].fbuffer)
480 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
481 mem = fh->v4l_buffers.buffer[i].fbuffer;
482 for (off = 0; off < fh->v4l_buffers.buffer_size;
484 ClearPageReserved(MAP_NR(mem + off));
485 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
487 #if defined(CONFIG_BIGPHYS_AREA)
489 bigphysarea_free_pages((void *) fh->v4l_buffers.
492 fh->v4l_buffers.buffer[i].fbuffer = NULL;
495 fh->v4l_buffers.allocated = 0;
496 fh->v4l_buffers.ready_to_be_freed = 0;
500 * Allocate the MJPEG grab buffers.
502 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
503 * kmalloc is used to request a physically contiguous area,
504 * else we allocate the memory in framgents with get_zeroed_page.
506 * If a Natoma chipset is present and this is a revision 1 zr36057,
507 * each MJPEG buffer needs to be physically contiguous.
508 * (RJ: This statement is from Dave Perks' original driver,
509 * I could never check it because I have a zr36067)
510 * The driver cares about this because it reduces the buffer
511 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
513 * RJ: The contents grab buffers needs never be accessed in the driver.
514 * Therefore there is no need to allocate them with vmalloc in order
515 * to get a contiguous virtual memory space.
516 * I don't understand why many other drivers first allocate them with
517 * vmalloc (which uses internally also get_zeroed_page, but delivers you
518 * virtual addresses) and then again have to make a lot of efforts
519 * to get the physical address.
524 jpg_fbuffer_alloc (struct file *file)
526 struct zoran_fh *fh = file->private_data;
527 struct zoran *zr = fh->zr;
531 /* we might have old buffers lying around */
532 if (fh->jpg_buffers.ready_to_be_freed) {
533 jpg_fbuffer_free(file);
536 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
537 if (fh->jpg_buffers.buffer[i].frag_tab)
540 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
543 /* Allocate fragment table for this buffer */
545 mem = get_zeroed_page(GFP_KERNEL);
549 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
551 jpg_fbuffer_free(file);
554 memset((void *) mem, 0, PAGE_SIZE);
555 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
556 fh->jpg_buffers.buffer[i].frag_tab_bus =
557 virt_to_bus((void *) mem);
559 //if (alloc_contig) {
560 if (fh->jpg_buffers.need_contiguous) {
562 (unsigned long) kmalloc(fh->jpg_buffers.
568 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
570 jpg_fbuffer_free(file);
573 fh->jpg_buffers.buffer[i].frag_tab[0] =
574 virt_to_bus((void *) mem);
575 fh->jpg_buffers.buffer[i].frag_tab[1] =
576 ((fh->jpg_buffers.buffer_size / 4) << 1) | 1;
577 for (off = 0; off < fh->jpg_buffers.buffer_size;
579 SetPageReserved(MAP_NR(mem + off));
581 /* jpg_bufsize is allreay page aligned */
583 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
585 mem = get_zeroed_page(GFP_KERNEL);
589 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
591 jpg_fbuffer_free(file);
595 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
596 virt_to_bus((void *) mem);
597 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
599 (PAGE_SIZE / 4) << 1;
600 SetPageReserved(MAP_NR(mem));
603 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= 1;
608 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
610 (fh->jpg_buffers.num_buffers *
611 fh->jpg_buffers.buffer_size) >> 10);
613 fh->jpg_buffers.allocated = 1;
618 /* free the MJPEG grab buffers */
620 jpg_fbuffer_free (struct file *file)
622 struct zoran_fh *fh = file->private_data;
623 struct zoran *zr = fh->zr;
627 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
629 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
630 if (!fh->jpg_buffers.buffer[i].frag_tab)
633 //if (alloc_contig) {
634 if (fh->jpg_buffers.need_contiguous) {
635 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
637 (unsigned char *) bus_to_virt(fh->
644 off < fh->jpg_buffers.buffer_size;
646 ClearPageReserved(MAP_NR
649 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
650 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
654 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
656 if (!fh->jpg_buffers.buffer[i].
659 ClearPageReserved(MAP_NR
662 buffer[i].frag_tab[2 *
664 free_page((unsigned long)
665 bus_to_virt(fh->jpg_buffers.
668 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
670 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
675 free_page((unsigned long) fh->jpg_buffers.buffer[i].
677 fh->jpg_buffers.buffer[i].frag_tab = NULL;
680 fh->jpg_buffers.allocated = 0;
681 fh->jpg_buffers.ready_to_be_freed = 0;
685 * V4L Buffer grabbing
689 zoran_v4l_set_format (struct file *file,
692 const struct zoran_format *format)
694 struct zoran_fh *fh = file->private_data;
695 struct zoran *zr = fh->zr;
698 /* Check size and format of the grab wanted */
700 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
701 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
704 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
705 ZR_DEVNAME(zr), width, height);
709 bpp = (format->depth + 7) / 8;
711 /* Check against available buffer size */
712 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
715 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
716 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
720 /* The video front end needs 4-byte alinged line sizes */
722 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
725 "%s: v4l_set_format() - wrong frame alingment\n",
730 fh->v4l_settings.width = width;
731 fh->v4l_settings.height = height;
732 fh->v4l_settings.format = format;
733 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
739 zoran_v4l_queue_frame (struct file *file,
742 struct zoran_fh *fh = file->private_data;
743 struct zoran *zr = fh->zr;
747 if (!fh->v4l_buffers.allocated) {
750 "%s: v4l_queue_frame() - buffers not yet allocated\n",
755 /* No grabbing outside the buffer range! */
756 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
759 "%s: v4l_queue_frame() - buffer %d is out of range\n",
760 ZR_DEVNAME(zr), num);
764 spin_lock_irqsave(&zr->spinlock, flags);
766 if (fh->v4l_buffers.active == ZORAN_FREE) {
767 if (zr->v4l_buffers.active == ZORAN_FREE) {
768 zr->v4l_buffers = fh->v4l_buffers;
769 fh->v4l_buffers.active = ZORAN_ACTIVE;
773 "%s: v4l_queue_frame() - another session is already capturing\n",
779 /* make sure a grab isn't going on currently with this buffer */
781 switch (zr->v4l_buffers.buffer[num].state) {
784 if (zr->v4l_buffers.active == ZORAN_FREE) {
785 fh->v4l_buffers.active = ZORAN_FREE;
786 zr->v4l_buffers.allocated = 0;
788 res = -EBUSY; /* what are you doing? */
793 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
794 ZR_DEVNAME(zr), num);
796 /* since there is at least one unused buffer there's room for at least
797 * one more pend[] entry */
798 zr->v4l_pend[zr->v4l_pend_head++ &
799 V4L_MASK_FRAME] = num;
800 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
801 zr->v4l_buffers.buffer[num].bs.length =
802 fh->v4l_settings.bytesperline *
803 zr->v4l_settings.height;
804 fh->v4l_buffers.buffer[num] =
805 zr->v4l_buffers.buffer[num];
810 spin_unlock_irqrestore(&zr->spinlock, flags);
812 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
813 zr->v4l_buffers.active = fh->v4l_buffers.active;
819 v4l_grab (struct file *file,
820 struct video_mmap *mp)
822 struct zoran_fh *fh = file->private_data;
823 struct zoran *zr = fh->zr;
826 for (i = 0; i < zoran_num_formats; i++) {
827 if (zoran_formats[i].palette == mp->format &&
828 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
829 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
832 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
835 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
841 * To minimize the time spent in the IRQ routine, we avoid setting up
842 * the video front end there.
843 * If this grab has different parameters from a running streaming capture
844 * we stop the streaming capture and start it over again.
846 if (zr->v4l_memgrab_active &&
847 (zr->v4l_settings.width != mp->width ||
848 zr->v4l_settings.height != mp->height ||
849 zr->v4l_settings.format->palette != mp->format)) {
850 res = wait_grab_pending(zr);
854 if ((res = zoran_v4l_set_format(file,
859 zr->v4l_settings = fh->v4l_settings;
861 /* queue the frame in the pending queue */
862 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
863 fh->v4l_buffers.active = ZORAN_FREE;
867 /* put the 36057 into frame grabbing mode */
868 if (!res && !zr->v4l_memgrab_active)
869 zr36057_set_memgrab(zr, 1);
871 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
877 * Sync on a V4L buffer
881 v4l_sync (struct file *file,
884 struct zoran_fh *fh = file->private_data;
885 struct zoran *zr = fh->zr;
888 if (fh->v4l_buffers.active == ZORAN_FREE) {
891 "%s: v4l_sync() - no grab active for this session\n",
896 /* check passed-in frame number */
897 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
899 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
900 ZR_DEVNAME(zr), frame);
904 /* Check if is buffer was queued at all */
905 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
908 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
913 /* wait on this buffer to get ready */
914 while (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_PEND) {
915 if (!interruptible_sleep_on_timeout(&zr->v4l_capq, 10 * HZ))
917 else if (signal_pending(current))
921 /* buffer should now be in BUZ_STATE_DONE */
922 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
924 KERN_ERR "%s: v4l_sync() - internal state error\n",
927 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
928 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
930 spin_lock_irqsave(&zr->spinlock, flags);
932 /* Check if streaming capture has finished */
933 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
934 zr36057_set_memgrab(zr, 0);
935 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
936 fh->v4l_buffers.active = zr->v4l_buffers.active =
938 zr->v4l_buffers.allocated = 0;
942 spin_unlock_irqrestore(&zr->spinlock, flags);
948 * Queue a MJPEG buffer for capture/playback
952 zoran_jpg_queue_frame (struct file *file,
954 enum zoran_codec_mode mode)
956 struct zoran_fh *fh = file->private_data;
957 struct zoran *zr = fh->zr;
961 /* Check if buffers are allocated */
962 if (!fh->jpg_buffers.allocated) {
965 "%s: jpg_queue_frame() - buffers not yet allocated\n",
970 /* No grabbing outside the buffer range! */
971 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
974 "%s: jpg_queue_frame() - buffer %d out of range\n",
975 ZR_DEVNAME(zr), num);
979 /* what is the codec mode right now? */
980 if (zr->codec_mode == BUZ_MODE_IDLE) {
981 zr->jpg_settings = fh->jpg_settings;
982 } else if (zr->codec_mode != mode) {
983 /* wrong codec mode active - invalid */
986 "%s: jpg_queue_frame() - codec in wrong mode\n",
991 spin_lock_irqsave(&zr->spinlock, flags);
993 if (fh->jpg_buffers.active == ZORAN_FREE) {
994 if (zr->jpg_buffers.active == ZORAN_FREE) {
995 zr->jpg_buffers = fh->jpg_buffers;
996 fh->jpg_buffers.active = ZORAN_ACTIVE;
1000 "%s: jpg_queue_frame() - another session is already capturing\n",
1006 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1007 /* Ok load up the jpeg codec */
1008 zr36057_enable_jpg(zr, mode);
1012 switch (zr->jpg_buffers.buffer[num].state) {
1013 case BUZ_STATE_DONE:
1016 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1018 case BUZ_STATE_USER:
1019 /* since there is at least one unused buffer there's room for at
1020 *least one more pend[] entry */
1021 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1023 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1024 fh->jpg_buffers.buffer[num] =
1025 zr->jpg_buffers.buffer[num];
1026 zoran_feed_stat_com(zr);
1030 case BUZ_STATE_PEND:
1031 if (zr->jpg_buffers.active == ZORAN_FREE) {
1032 fh->jpg_buffers.active = ZORAN_FREE;
1033 zr->jpg_buffers.allocated = 0;
1035 res = -EBUSY; /* what are you doing? */
1040 spin_unlock_irqrestore(&zr->spinlock, flags);
1042 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1043 zr->jpg_buffers.active = fh->jpg_buffers.active;
1050 jpg_qbuf (struct file *file,
1052 enum zoran_codec_mode mode)
1054 struct zoran_fh *fh = file->private_data;
1055 struct zoran *zr = fh->zr;
1058 /* Does the user want to stop streaming? */
1060 if (zr->codec_mode == mode) {
1061 if (fh->jpg_buffers.active == ZORAN_FREE) {
1064 "%s: jpg_qbuf(-1) - session not active\n",
1068 fh->jpg_buffers.active = zr->jpg_buffers.active =
1070 zr->jpg_buffers.allocated = 0;
1071 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1076 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1082 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1085 /* Start the jpeg codec when the first frame is queued */
1086 if (!res && zr->jpg_que_head == 1)
1093 * Sync on a MJPEG buffer
1097 jpg_sync (struct file *file,
1098 struct zoran_sync *bs)
1100 struct zoran_fh *fh = file->private_data;
1101 struct zoran *zr = fh->zr;
1102 unsigned long flags;
1105 if (fh->jpg_buffers.active == ZORAN_FREE) {
1108 "%s: jpg_sync() - capture is not currently active\n",
1112 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1113 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1116 "%s: jpg_sync() - codec not in streaming mode\n",
1120 while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1121 if (zr->jpg_dma_tail == zr->jpg_dma_head)
1125 interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
1129 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1131 zr->codec->control(zr->codec, CODEC_G_STATUS,
1135 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1136 ZR_DEVNAME(zr), isr);
1140 } else if (signal_pending(current))
1141 return -ERESTARTSYS;
1144 spin_lock_irqsave(&zr->spinlock, flags);
1146 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1147 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1149 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1151 /* buffer should now be in BUZ_STATE_DONE */
1153 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1156 "%s: jpg_sync() - internal state error\n",
1159 *bs = zr->jpg_buffers.buffer[frame].bs;
1161 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1162 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1164 spin_unlock_irqrestore(&zr->spinlock, flags);
1170 zoran_open_init_session (struct file *file)
1173 struct zoran_fh *fh = file->private_data;
1174 struct zoran *zr = fh->zr;
1176 /* Per default, map the V4L Buffers */
1177 fh->map_mode = ZORAN_MAP_MODE_RAW;
1179 /* take over the card's current settings */
1180 fh->overlay_settings = zr->overlay_settings;
1181 fh->overlay_settings.is_set = 0;
1182 fh->overlay_settings.format = zr->overlay_settings.format;
1183 fh->overlay_active = ZORAN_FREE;
1186 fh->v4l_settings = zr->v4l_settings;
1189 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1190 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1191 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1192 fh->v4l_buffers.buffer[i].bs.frame = i;
1194 fh->v4l_buffers.allocated = 0;
1195 fh->v4l_buffers.ready_to_be_freed = 0;
1196 fh->v4l_buffers.active = ZORAN_FREE;
1197 fh->v4l_buffers.buffer_size = v4l_bufsize;
1198 fh->v4l_buffers.num_buffers = v4l_nbufs;
1201 fh->jpg_settings = zr->jpg_settings;
1204 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1205 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1206 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1207 fh->jpg_buffers.buffer[i].bs.frame = i;
1209 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1210 fh->jpg_buffers.allocated = 0;
1211 fh->jpg_buffers.ready_to_be_freed = 0;
1212 fh->jpg_buffers.active = ZORAN_FREE;
1213 fh->jpg_buffers.buffer_size = jpg_bufsize;
1214 fh->jpg_buffers.num_buffers = jpg_nbufs;
1218 zoran_close_end_session (struct file *file)
1220 struct zoran_fh *fh = file->private_data;
1221 struct zoran *zr = fh->zr;
1224 if (fh->overlay_active != ZORAN_FREE) {
1225 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1226 zr->v4l_overlay_active = 0;
1227 if (!zr->v4l_memgrab_active)
1228 zr36057_overlay(zr, 0);
1229 zr->overlay_mask = NULL;
1233 if (fh->v4l_buffers.active != ZORAN_FREE) {
1234 zr36057_set_memgrab(zr, 0);
1235 zr->v4l_buffers.allocated = 0;
1236 zr->v4l_buffers.active = fh->v4l_buffers.active =
1241 if (fh->v4l_buffers.allocated ||
1242 fh->v4l_buffers.ready_to_be_freed) {
1243 v4l_fbuffer_free(file);
1247 if (fh->jpg_buffers.active != ZORAN_FREE) {
1248 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1249 zr->jpg_buffers.allocated = 0;
1250 zr->jpg_buffers.active = fh->jpg_buffers.active =
1255 if (fh->jpg_buffers.allocated ||
1256 fh->jpg_buffers.ready_to_be_freed) {
1257 jpg_fbuffer_free(file);
1262 * Open a zoran card. Right now the flags stuff is just playing
1266 zoran_open (struct inode *inode,
1269 unsigned int minor = iminor(inode);
1270 struct zoran *zr = NULL;
1271 struct zoran_fh *fh;
1272 int i, res, first_open = 0, have_module_locks = 0;
1274 /* find the device */
1275 for (i = 0; i < zoran_num; i++) {
1276 if (zoran[i].video_dev->minor == minor) {
1283 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1285 goto open_unlock_and_return;
1288 /* see fs/device.c - the kernel already locks during open(),
1289 * so locking ourselves only causes deadlocks */
1290 /*down(&zr->resource_lock);*/
1294 KERN_ERR "%s: no TV decoder loaded for device!\n",
1297 goto open_unlock_and_return;
1300 /* try to grab a module lock */
1301 if (!try_module_get(THIS_MODULE)) {
1304 "%s: failed to acquire my own lock! PANIC!\n",
1307 goto open_unlock_and_return;
1309 if (!try_module_get(zr->decoder->driver->owner)) {
1312 "%s: failed to grab ownership of i2c decoder\n",
1315 module_put(THIS_MODULE);
1316 goto open_unlock_and_return;
1319 !try_module_get(zr->encoder->driver->owner)) {
1322 "%s: failed to grab ownership of i2c encoder\n",
1325 module_put(zr->decoder->driver->owner);
1326 module_put(THIS_MODULE);
1327 goto open_unlock_and_return;
1330 have_module_locks = 1;
1332 if (zr->user >= 2048) {
1333 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1334 ZR_DEVNAME(zr), zr->user);
1336 goto open_unlock_and_return;
1339 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1340 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1342 /* now, create the open()-specific file_ops struct */
1343 fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1347 "%s: zoran_open() - allocation of zoran_fh failed\n",
1350 goto open_unlock_and_return;
1352 memset(fh, 0, sizeof(struct zoran_fh));
1353 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1354 * on norm-change! */
1356 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1357 if (!fh->overlay_mask) {
1360 "%s: zoran_open() - allocation of overlay_mask failed\n",
1364 goto open_unlock_and_return;
1367 if (zr->user++ == 0)
1370 /*up(&zr->resource_lock);*/
1372 /* default setup - TODO: look at flags */
1373 if (first_open) { /* First device open */
1374 zr36057_restart(zr);
1375 zoran_open_init_params(zr);
1376 zoran_init_hardware(zr);
1378 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1381 /* set file_ops stuff */
1382 file->private_data = fh;
1384 zoran_open_init_session(file);
1388 open_unlock_and_return:
1389 /* if we grabbed locks, release them accordingly */
1390 if (have_module_locks) {
1391 module_put(zr->decoder->driver->owner);
1393 module_put(zr->encoder->driver->owner);
1395 module_put(THIS_MODULE);
1398 /* if there's no device found, we didn't obtain the lock either */
1400 /*up(&zr->resource_lock);*/
1407 zoran_close (struct inode *inode,
1410 struct zoran_fh *fh = file->private_data;
1411 struct zoran *zr = fh->zr;
1413 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1414 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1416 /* kernel locks (fs/device.c), so don't do that ourselves
1417 * (prevents deadlocks) */
1418 /*down(&zr->resource_lock);*/
1420 zoran_close_end_session(file);
1422 if (zr->user-- == 1) { /* Last process */
1423 /* Clean up JPEG process */
1424 wake_up_interruptible(&zr->jpg_capq);
1425 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1426 zr->jpg_buffers.allocated = 0;
1427 zr->jpg_buffers.active = ZORAN_FREE;
1429 /* disable interrupts */
1430 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1433 print_interrupts(zr);
1436 zr->v4l_overlay_active = 0;
1437 zr36057_overlay(zr, 0);
1438 zr->overlay_mask = NULL;
1441 wake_up_interruptible(&zr->v4l_capq);
1442 zr36057_set_memgrab(zr, 0);
1443 zr->v4l_buffers.allocated = 0;
1444 zr->v4l_buffers.active = ZORAN_FREE;
1445 zoran_set_pci_master(zr, 0);
1447 if (!pass_through) { /* Switch to color bar */
1448 int zero = 0, two = 2;
1449 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1450 encoder_command(zr, ENCODER_SET_INPUT, &two);
1454 file->private_data = NULL;
1455 kfree(fh->overlay_mask);
1458 /* release locks on the i2c modules */
1459 module_put(zr->decoder->driver->owner);
1461 module_put(zr->encoder->driver->owner);
1463 module_put(THIS_MODULE);
1465 /*up(&zr->resource_lock);*/
1467 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1474 zoran_read (struct file *file,
1479 /* we simply don't support read() (yet)... */
1485 zoran_write (struct file *file,
1490 /* ...and the same goes for write() */
1496 setup_fbuffer (struct file *file,
1498 const struct zoran_format *fmt,
1503 struct zoran_fh *fh = file->private_data;
1504 struct zoran *zr = fh->zr;
1506 /* (Ronald) v4l/v4l2 guidelines */
1507 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1510 /* we need a bytesperline value, even if not given */
1512 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1515 if (zr->overlay_active) {
1516 /* dzjee... stupid users... don't even bother to turn off
1517 * overlay before changing the memory location...
1518 * normally, we would return errors here. However, one of
1519 * the tools that does this is... xawtv! and since xawtv
1520 * is used by +/- 99% of the users, we'd rather be user-
1521 * friendly and silently do as if nothing went wrong */
1524 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1526 zr36057_overlay(zr, 0);
1530 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1533 "%s: setup_fbuffer() - no valid overlay format given\n",
1537 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1540 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1541 ZR_DEVNAME(zr), width, height, bytesperline);
1544 if (bytesperline & 3) {
1547 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1548 ZR_DEVNAME(zr), bytesperline);
1552 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1553 zr->buffer.height = height;
1554 zr->buffer.width = width;
1555 zr->buffer.depth = fmt->depth;
1556 zr->overlay_settings.format = fmt;
1557 zr->buffer.bytesperline = bytesperline;
1559 /* The user should set new window parameters */
1560 zr->overlay_settings.is_set = 0;
1567 setup_window (struct file *file,
1572 struct video_clip *clips,
1576 struct zoran_fh *fh = file->private_data;
1577 struct zoran *zr = fh->zr;
1578 struct video_clip *vcp = NULL;
1582 if (!zr->buffer.base) {
1585 "%s: setup_window() - frame buffer has to be set first\n",
1590 if (!fh->overlay_settings.format) {
1593 "%s: setup_window() - no overlay format set\n",
1599 * The video front end needs 4-byte alinged line sizes, we correct that
1600 * silently here if necessary
1602 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1603 end = (x + width) & ~1; /* round down */
1604 x = (x + 1) & ~1; /* round up */
1608 if (zr->buffer.depth == 24) {
1609 end = (x + width) & ~3; /* round down */
1610 x = (x + 3) & ~3; /* round up */
1614 if (width > BUZ_MAX_WIDTH)
1615 width = BUZ_MAX_WIDTH;
1616 if (height > BUZ_MAX_HEIGHT)
1617 height = BUZ_MAX_HEIGHT;
1619 /* Check for vaild parameters */
1620 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1621 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1624 "%s: setup_window() - width = %d or height = %d invalid\n",
1625 ZR_DEVNAME(zr), width, height);
1629 fh->overlay_settings.x = x;
1630 fh->overlay_settings.y = y;
1631 fh->overlay_settings.width = width;
1632 fh->overlay_settings.height = height;
1633 fh->overlay_settings.clipcount = clipcount;
1636 * If an overlay is running, we have to switch it off
1637 * and switch it on again in order to get the new settings in effect.
1639 * We also want to avoid that the overlay mask is written
1640 * when an overlay is running.
1643 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1644 zr->overlay_active != ZORAN_FREE &&
1645 fh->overlay_active != ZORAN_FREE;
1647 zr36057_overlay(zr, 0);
1650 * Write the overlay mask if clips are wanted.
1651 * We prefer a bitmap.
1654 /* fake value - it just means we want clips */
1655 fh->overlay_settings.clipcount = 1;
1657 if (copy_from_user(fh->overlay_mask, bitmap,
1658 (width * height + 7) / 8)) {
1661 } else if (clipcount > 0) {
1662 /* write our own bitmap from the clips */
1663 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1667 "%s: setup_window() - Alloc of clip mask failed\n",
1672 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1676 write_overlay_mask(file, vcp, clipcount);
1680 fh->overlay_settings.is_set = 1;
1681 if (fh->overlay_active != ZORAN_FREE &&
1682 zr->overlay_active != ZORAN_FREE)
1683 zr->overlay_settings = fh->overlay_settings;
1686 zr36057_overlay(zr, 1);
1688 /* Make sure the changes come into effect */
1689 return wait_grab_pending(zr);
1693 setup_overlay (struct file *file,
1696 struct zoran_fh *fh = file->private_data;
1697 struct zoran *zr = fh->zr;
1699 /* If there is nothing to do, return immediatly */
1700 if ((on && fh->overlay_active != ZORAN_FREE) ||
1701 (!on && fh->overlay_active == ZORAN_FREE))
1704 /* check whether we're touching someone else's overlay */
1705 if (on && zr->overlay_active != ZORAN_FREE &&
1706 fh->overlay_active == ZORAN_FREE) {
1709 "%s: setup_overlay() - overlay is already active for another session\n",
1713 if (!on && zr->overlay_active != ZORAN_FREE &&
1714 fh->overlay_active == ZORAN_FREE) {
1717 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1723 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1724 zr->v4l_overlay_active = 0;
1725 /* When a grab is running, the video simply
1726 * won't be switched on any more */
1727 if (!zr->v4l_memgrab_active)
1728 zr36057_overlay(zr, 0);
1729 zr->overlay_mask = NULL;
1731 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1734 "%s: setup_overlay() - buffer or window not set\n",
1738 if (!fh->overlay_settings.format) {
1741 "%s: setup_overlay() - no overlay format set\n",
1745 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1746 zr->v4l_overlay_active = 1;
1747 zr->overlay_mask = fh->overlay_mask;
1748 zr->overlay_settings = fh->overlay_settings;
1749 if (!zr->v4l_memgrab_active)
1750 zr36057_overlay(zr, 1);
1751 /* When a grab is running, the video will be
1752 * switched on when grab is finished */
1755 /* Make sure the changes come into effect */
1756 return wait_grab_pending(zr);
1760 /* get the status of a buffer in the clients buffer queue */
1762 zoran_v4l2_buffer_status (struct file *file,
1763 struct v4l2_buffer *buf,
1766 struct zoran_fh *fh = file->private_data;
1767 struct zoran *zr = fh->zr;
1769 buf->flags = V4L2_BUF_FLAG_MAPPED;
1771 switch (fh->map_mode) {
1772 case ZORAN_MAP_MODE_RAW:
1775 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1776 !fh->v4l_buffers.allocated) {
1779 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1784 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1785 buf->length = fh->v4l_buffers.buffer_size;
1788 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1789 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1790 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1791 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1792 buf->flags |= V4L2_BUF_FLAG_DONE;
1794 fh->v4l_buffers.buffer[num].bs.timestamp;
1796 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1799 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1800 buf->field = V4L2_FIELD_TOP;
1802 buf->field = V4L2_FIELD_INTERLACED;
1806 case ZORAN_MAP_MODE_JPG_REC:
1807 case ZORAN_MAP_MODE_JPG_PLAY:
1810 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1811 !fh->jpg_buffers.allocated) {
1814 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1819 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1820 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1821 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1822 buf->length = fh->jpg_buffers.buffer_size;
1824 /* these variables are only written after frame has been captured */
1825 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1826 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1827 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1829 fh->jpg_buffers.buffer[num].bs.timestamp;
1831 fh->jpg_buffers.buffer[num].bs.length;
1832 buf->flags |= V4L2_BUF_FLAG_DONE;
1834 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1837 /* which fields are these? */
1838 if (fh->jpg_settings.TmpDcm != 1)
1841 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1845 odd_even ? V4L2_FIELD_SEQ_TB :
1854 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1855 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1859 buf->memory = V4L2_MEMORY_MMAP;
1861 buf->m.offset = buf->length * num;
1868 zoran_set_norm (struct zoran *zr,
1869 int norm) /* VIDEO_MODE_* */
1871 int norm_encoder, on;
1873 if (zr->v4l_buffers.active != ZORAN_FREE ||
1874 zr->jpg_buffers.active != ZORAN_FREE) {
1877 "%s: set_norm() called while in playback/capture mode\n",
1882 if (lock_norm && norm != zr->norm) {
1883 if (lock_norm > 1) {
1886 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1892 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1898 if (norm != VIDEO_MODE_AUTO &&
1899 (norm < 0 || norm >= zr->card.norms ||
1900 !zr->card.tvn[norm])) {
1902 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1903 ZR_DEVNAME(zr), norm);
1907 if (norm == VIDEO_MODE_AUTO) {
1910 /* if we have autodetect, ... */
1911 struct video_decoder_capability caps;
1912 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1913 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1914 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1919 decoder_command(zr, DECODER_SET_NORM, &norm);
1921 /* let changes come into effect */
1922 current->state = TASK_UNINTERRUPTIBLE;
1923 schedule_timeout(2 * HZ);
1925 decoder_command(zr, DECODER_GET_STATUS, &status);
1926 if (!(status & DECODER_STATUS_GOOD)) {
1929 "%s: set_norm() - no norm detected\n",
1932 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1936 if (status & DECODER_STATUS_NTSC)
1937 norm = VIDEO_MODE_NTSC;
1938 else if (status & DECODER_STATUS_SECAM)
1939 norm = VIDEO_MODE_SECAM;
1941 norm = VIDEO_MODE_PAL;
1943 zr->timing = zr->card.tvn[norm];
1944 norm_encoder = norm;
1946 /* We switch overlay off and on since a change in the
1947 * norm needs different VFE settings */
1948 on = zr->overlay_active && !zr->v4l_memgrab_active;
1950 zr36057_overlay(zr, 0);
1952 decoder_command(zr, DECODER_SET_NORM, &norm);
1953 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1956 zr36057_overlay(zr, 1);
1958 /* Make sure the changes come into effect */
1965 zoran_set_input (struct zoran *zr,
1970 if (input == zr->input) {
1974 if (zr->v4l_buffers.active != ZORAN_FREE ||
1975 zr->jpg_buffers.active != ZORAN_FREE) {
1978 "%s: set_input() called while in playback/capture mode\n",
1983 if (input < 0 || input >= zr->card.inputs) {
1986 "%s: set_input() - unnsupported input %d\n",
1987 ZR_DEVNAME(zr), input);
1991 realinput = zr->card.input[input].muxsel;
1994 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2004 zoran_do_ioctl (struct inode *inode,
2009 struct zoran_fh *fh = file->private_data;
2010 struct zoran *zr = fh->zr;
2012 /* we might have older buffers lying around... We don't want
2013 * to wait, but we do want to try cleaning them up ASAP. So
2014 * we try to obtain the lock and free them. If that fails, we
2015 * don't do anything and wait for the next turn. In the end,
2016 * zoran_close() or a new allocation will still free them...
2017 * This is just a 'the sooner the better' extra 'feature'
2019 * We don't free the buffers right on munmap() because that
2020 * causes oopses (kfree() inside munmap() oopses for no
2021 * apparent reason - it's also not reproduceable in any way,
2022 * but moving the free code outside the munmap() handler fixes
2023 * all this... If someone knows why, please explain me (Ronald)
2025 if (!down_trylock(&zr->resource_lock)) {
2026 /* we obtained it! Let's try to free some things */
2027 if (fh->jpg_buffers.ready_to_be_freed)
2028 jpg_fbuffer_free(file);
2029 if (fh->v4l_buffers.ready_to_be_freed)
2030 v4l_fbuffer_free(file);
2032 up(&zr->resource_lock);
2039 struct video_capability *vcap = arg;
2041 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2043 memset(vcap, 0, sizeof(struct video_capability));
2044 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
2045 vcap->type = ZORAN_VID_TYPE;
2047 vcap->channels = zr->card.inputs;
2049 down(&zr->resource_lock);
2050 vcap->maxwidth = BUZ_MAX_WIDTH;
2051 vcap->maxheight = BUZ_MAX_HEIGHT;
2052 vcap->minwidth = BUZ_MIN_WIDTH;
2053 vcap->minheight = BUZ_MIN_HEIGHT;
2054 up(&zr->resource_lock);
2062 struct video_channel *vchan = arg;
2063 int channel = vchan->channel;
2065 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2066 ZR_DEVNAME(zr), vchan->channel);
2068 memset(vchan, 0, sizeof(struct video_channel));
2069 if (channel > zr->card.inputs || channel < 0) {
2072 "%s: VIDIOCGCHAN on not existing channel %d\n",
2073 ZR_DEVNAME(zr), channel);
2077 strcpy(vchan->name, zr->card.input[channel].name);
2081 vchan->type = VIDEO_TYPE_CAMERA;
2082 down(&zr->resource_lock);
2083 vchan->norm = zr->norm;
2084 up(&zr->resource_lock);
2085 vchan->channel = channel;
2091 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2093 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2095 * * The famos BTTV driver has it implemented with a struct video_channel argument
2096 * * and we follow it for compatibility reasons
2098 * * BTW: this is the only way the user can set the norm!
2103 struct video_channel *vchan = arg;
2108 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2109 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2111 down(&zr->resource_lock);
2112 if ((res = zoran_set_input(zr, vchan->channel)))
2113 goto schan_unlock_and_return;
2114 if ((res = zoran_set_norm(zr, vchan->norm)))
2115 goto schan_unlock_and_return;
2117 /* Make sure the changes come into effect */
2118 res = wait_grab_pending(zr);
2119 schan_unlock_and_return:
2120 up(&zr->resource_lock);
2127 struct video_picture *vpict = arg;
2129 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2131 memset(vpict, 0, sizeof(struct video_picture));
2132 down(&zr->resource_lock);
2133 vpict->hue = zr->hue;
2134 vpict->brightness = zr->brightness;
2135 vpict->contrast = zr->contrast;
2136 vpict->colour = zr->saturation;
2137 if (fh->overlay_settings.format) {
2138 vpict->depth = fh->overlay_settings.format->depth;
2139 vpict->palette = fh->overlay_settings.format->palette;
2143 up(&zr->resource_lock);
2151 struct video_picture *vpict = arg;
2156 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2157 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2158 vpict->colour, vpict->contrast, vpict->depth,
2161 for (i = 0; i < zoran_num_formats; i++) {
2162 const struct zoran_format *fmt = &zoran_formats[i];
2164 if (fmt->palette != -1 &&
2165 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2166 fmt->palette == vpict->palette &&
2167 fmt->depth == vpict->depth)
2170 if (i == zoran_num_formats) {
2173 "%s: VIDIOCSPICT - Invalid palette %d\n",
2174 ZR_DEVNAME(zr), vpict->palette);
2178 down(&zr->resource_lock);
2180 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2182 zr->hue = vpict->hue;
2183 zr->contrast = vpict->contrast;
2184 zr->saturation = vpict->colour;
2185 zr->brightness = vpict->brightness;
2187 fh->overlay_settings.format = &zoran_formats[i];
2189 up(&zr->resource_lock);
2199 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2200 ZR_DEVNAME(zr), *on);
2202 down(&zr->resource_lock);
2203 res = setup_overlay(file, *on);
2204 up(&zr->resource_lock);
2212 struct video_window *vwin = arg;
2214 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2216 memset(vwin, 0, sizeof(struct video_window));
2217 down(&zr->resource_lock);
2218 vwin->x = fh->overlay_settings.x;
2219 vwin->y = fh->overlay_settings.y;
2220 vwin->width = fh->overlay_settings.width;
2221 vwin->height = fh->overlay_settings.height;
2222 up(&zr->resource_lock);
2223 vwin->clipcount = 0;
2230 struct video_window *vwin = arg;
2235 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2236 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2237 vwin->height, vwin->clipcount);
2239 down(&zr->resource_lock);
2241 setup_window(file, vwin->x, vwin->y, vwin->width,
2242 vwin->height, vwin->clips,
2243 vwin->clipcount, NULL);
2244 up(&zr->resource_lock);
2252 struct video_buffer *vbuf = arg;
2254 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2256 down(&zr->resource_lock);
2258 up(&zr->resource_lock);
2265 struct video_buffer *vbuf = arg;
2270 "%s: VIDIOCSFBUF - base=0x%x, w=%d, h=%d, depth=%d, bpl=%d\n",
2271 ZR_DEVNAME(zr), (u32) vbuf->base, vbuf->width,
2272 vbuf->height, vbuf->depth, vbuf->bytesperline);
2274 for (i = 0; i < zoran_num_formats; i++)
2275 if (zoran_formats[i].depth == vbuf->depth)
2277 if (i == zoran_num_formats) {
2280 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2281 ZR_DEVNAME(zr), vbuf->depth);
2285 down(&zr->resource_lock);
2287 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2288 vbuf->width, vbuf->height,
2289 vbuf->bytesperline);
2290 up(&zr->resource_lock);
2298 int *frame = arg, res;
2300 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2301 ZR_DEVNAME(zr), *frame);
2303 down(&zr->resource_lock);
2304 res = v4l_sync(file, *frame);
2305 up(&zr->resource_lock);
2307 zr->v4l_sync_tail++;
2312 case VIDIOCMCAPTURE:
2314 struct video_mmap *vmap = arg;
2319 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2320 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2323 down(&zr->resource_lock);
2324 res = v4l_grab(file, vmap);
2325 up(&zr->resource_lock);
2332 struct video_mbuf *vmbuf = arg;
2335 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2338 fh->v4l_buffers.num_buffers *
2339 fh->v4l_buffers.buffer_size;
2340 vmbuf->frames = fh->v4l_buffers.num_buffers;
2341 for (i = 0; i < vmbuf->frames; i++) {
2343 i * fh->v4l_buffers.buffer_size;
2346 down(&zr->resource_lock);
2348 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2351 "%s: VIDIOCGMBUF - buffers already allocated\n",
2354 goto v4l1reqbuf_unlock_and_return;
2357 if (v4l_fbuffer_alloc(file)) {
2359 goto v4l1reqbuf_unlock_and_return;
2362 /* The next mmap will map the V4L buffers */
2363 fh->map_mode = ZORAN_MAP_MODE_RAW;
2364 v4l1reqbuf_unlock_and_return:
2365 up(&zr->resource_lock);
2373 struct video_unit *vunit = arg;
2375 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2377 vunit->video = zr->video_dev->minor;
2378 vunit->vbi = VIDEO_NO_UNIT;
2379 vunit->radio = VIDEO_NO_UNIT;
2380 vunit->audio = VIDEO_NO_UNIT;
2381 vunit->teletext = VIDEO_NO_UNIT;
2388 * RJ: In principal we could support subcaptures for V4L grabbing.
2389 * Not even the famous BTTV driver has them, however.
2390 * If there should be a strong demand, one could consider
2391 * to implement them.
2393 case VIDIOCGCAPTURE:
2395 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2401 case VIDIOCSCAPTURE:
2403 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2409 case BUZIOC_G_PARAMS:
2411 struct zoran_params *bparams = arg;
2413 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2415 memset(bparams, 0, sizeof(struct zoran_params));
2416 bparams->major_version = MAJOR_VERSION;
2417 bparams->minor_version = MINOR_VERSION;
2419 down(&zr->resource_lock);
2421 bparams->norm = zr->norm;
2422 bparams->input = zr->input;
2424 bparams->decimation = fh->jpg_settings.decimation;
2425 bparams->HorDcm = fh->jpg_settings.HorDcm;
2426 bparams->VerDcm = fh->jpg_settings.VerDcm;
2427 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2428 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2429 bparams->img_x = fh->jpg_settings.img_x;
2430 bparams->img_y = fh->jpg_settings.img_y;
2431 bparams->img_width = fh->jpg_settings.img_width;
2432 bparams->img_height = fh->jpg_settings.img_height;
2433 bparams->odd_even = fh->jpg_settings.odd_even;
2435 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2436 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2437 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2438 memcpy(bparams->APP_data,
2439 fh->jpg_settings.jpg_comp.APP_data,
2440 sizeof(bparams->APP_data));
2441 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2442 memcpy(bparams->COM_data,
2443 fh->jpg_settings.jpg_comp.COM_data,
2444 sizeof(bparams->COM_data));
2445 bparams->jpeg_markers =
2446 fh->jpg_settings.jpg_comp.jpeg_markers;
2448 up(&zr->resource_lock);
2450 bparams->VFIFO_FB = 0;
2456 case BUZIOC_S_PARAMS:
2458 struct zoran_params *bparams = arg;
2459 struct zoran_jpg_settings settings;
2462 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2464 settings.decimation = bparams->decimation;
2465 settings.HorDcm = bparams->HorDcm;
2466 settings.VerDcm = bparams->VerDcm;
2467 settings.TmpDcm = bparams->TmpDcm;
2468 settings.field_per_buff = bparams->field_per_buff;
2469 settings.img_x = bparams->img_x;
2470 settings.img_y = bparams->img_y;
2471 settings.img_width = bparams->img_width;
2472 settings.img_height = bparams->img_height;
2473 settings.odd_even = bparams->odd_even;
2475 settings.jpg_comp.quality = bparams->quality;
2476 settings.jpg_comp.APPn = bparams->APPn;
2477 settings.jpg_comp.APP_len = bparams->APP_len;
2478 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2479 sizeof(bparams->APP_data));
2480 settings.jpg_comp.COM_len = bparams->COM_len;
2481 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2482 sizeof(bparams->COM_data));
2483 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2485 down(&zr->resource_lock);
2487 if (zr->codec_mode != BUZ_MODE_IDLE) {
2490 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2493 goto sparams_unlock_and_return;
2496 /* Check the params first before overwriting our
2498 if (zoran_check_jpg_settings(zr, &settings)) {
2500 goto sparams_unlock_and_return;
2503 fh->jpg_settings = settings;
2504 sparams_unlock_and_return:
2505 up(&zr->resource_lock);
2511 case BUZIOC_REQBUFS:
2513 struct zoran_requestbuffers *breq = arg;
2518 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2519 ZR_DEVNAME(zr), breq->count, breq->size);
2521 /* Enforce reasonable lower and upper limits */
2522 if (breq->count < 4)
2523 breq->count = 4; /* Could be choosen smaller */
2524 if (breq->count > jpg_nbufs)
2525 breq->count = jpg_nbufs;
2526 breq->size = PAGE_ALIGN(breq->size);
2527 if (breq->size < 8192)
2528 breq->size = 8192; /* Arbitrary */
2529 /* breq->size is limited by 1 page for the stat_com
2530 * tables to a Maximum of 2 MB */
2531 if (breq->size > jpg_bufsize)
2532 breq->size = jpg_bufsize;
2533 if (fh->jpg_buffers.need_contiguous &&
2534 breq->size > MAX_KMALLOC_MEM)
2535 breq->size = MAX_KMALLOC_MEM;
2537 down(&zr->resource_lock);
2539 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2542 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2545 goto jpgreqbuf_unlock_and_return;
2548 fh->jpg_buffers.num_buffers = breq->count;
2549 fh->jpg_buffers.buffer_size = breq->size;
2551 if (jpg_fbuffer_alloc(file)) {
2553 goto jpgreqbuf_unlock_and_return;
2556 /* The next mmap will map the MJPEG buffers - could
2557 * also be *_PLAY, but it doesn't matter here */
2558 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2559 jpgreqbuf_unlock_and_return:
2560 up(&zr->resource_lock);
2566 case BUZIOC_QBUF_CAPT:
2568 int *frame = arg, res;
2570 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2571 ZR_DEVNAME(zr), *frame);
2573 down(&zr->resource_lock);
2574 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2575 up(&zr->resource_lock);
2581 case BUZIOC_QBUF_PLAY:
2583 int *frame = arg, res;
2585 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2586 ZR_DEVNAME(zr), *frame);
2588 down(&zr->resource_lock);
2589 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2590 up(&zr->resource_lock);
2598 struct zoran_sync *bsync = arg;
2601 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2603 down(&zr->resource_lock);
2604 res = jpg_sync(file, bsync);
2605 up(&zr->resource_lock);
2611 case BUZIOC_G_STATUS:
2613 struct zoran_status *bstat = arg;
2614 int norm, input, status, res = 0;
2616 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2618 if (zr->codec_mode != BUZ_MODE_IDLE) {
2621 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2626 input = zr->card.input[bstat->input].muxsel;
2627 norm = VIDEO_MODE_AUTO;
2629 down(&zr->resource_lock);
2631 if (zr->codec_mode != BUZ_MODE_IDLE) {
2634 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2637 goto gstat_unlock_and_return;
2640 decoder_command(zr, DECODER_SET_INPUT, &input);
2641 decoder_command(zr, DECODER_SET_NORM, &norm);
2643 /* sleep 1 second */
2644 current->state = TASK_UNINTERRUPTIBLE;
2645 schedule_timeout(1 * HZ);
2647 /* Get status of video decoder */
2648 decoder_command(zr, DECODER_GET_STATUS, &status);
2650 /* restore previous input and norm */
2651 input = zr->card.input[zr->input].muxsel;
2652 decoder_command(zr, DECODER_SET_INPUT, &input);
2653 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2654 gstat_unlock_and_return:
2655 up(&zr->resource_lock);
2659 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2660 if (status & DECODER_STATUS_NTSC)
2661 bstat->norm = VIDEO_MODE_NTSC;
2662 else if (status & DECODER_STATUS_SECAM)
2663 bstat->norm = VIDEO_MODE_SECAM;
2665 bstat->norm = VIDEO_MODE_PAL;
2668 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2677 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2678 * it allows for integrating the JPEG capturing calls inside standard v4l2
2681 case VIDIOC_QUERYCAP:
2683 struct v4l2_capability *cap = arg;
2685 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2687 memset(cap, 0, sizeof(*cap));
2688 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
2689 strncpy(cap->driver, "zoran", sizeof(cap->driver));
2690 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2691 zr->pci_dev->slot_name);
2693 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2695 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2701 case VIDIOC_ENUM_FMT:
2703 struct v4l2_fmtdesc *fmt = arg;
2704 int index = fmt->index, num = -1, i, flag = 0, type =
2707 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2708 ZR_DEVNAME(zr), fmt->index);
2710 switch (fmt->type) {
2711 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2712 flag = ZORAN_FORMAT_CAPTURE;
2714 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2715 flag = ZORAN_FORMAT_PLAYBACK;
2717 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2718 flag = ZORAN_FORMAT_OVERLAY;
2723 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2724 ZR_DEVNAME(zr), fmt->type);
2728 for (i = 0; i < zoran_num_formats; i++) {
2729 if (zoran_formats[i].flags & flag)
2731 if (num == fmt->index)
2734 if (fmt->index < 0 /* late, but not too late */ ||
2735 i == zoran_num_formats)
2738 memset(fmt, 0, sizeof(*fmt));
2741 strncpy(fmt->description, zoran_formats[i].name, 31);
2742 fmt->pixelformat = zoran_formats[i].fourcc;
2743 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2744 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2752 struct v4l2_format *fmt = arg;
2753 int type = fmt->type;
2755 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2757 memset(fmt, 0, sizeof(*fmt));
2760 switch (fmt->type) {
2761 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2763 down(&zr->resource_lock);
2765 fmt->fmt.win.w.left = fh->overlay_settings.x;
2766 fmt->fmt.win.w.top = fh->overlay_settings.y;
2767 fmt->fmt.win.w.width = fh->overlay_settings.width;
2768 fmt->fmt.win.w.height =
2769 fh->overlay_settings.height;
2770 if (fh->overlay_settings.width * 2 >
2772 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2774 fmt->fmt.win.field = V4L2_FIELD_TOP;
2776 up(&zr->resource_lock);
2780 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2781 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2783 down(&zr->resource_lock);
2785 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2786 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2788 fmt->fmt.pix.width =
2789 fh->v4l_settings.width;
2790 fmt->fmt.pix.height =
2791 fh->v4l_settings.height;
2792 fmt->fmt.pix.sizeimage =
2793 fh->v4l_buffers.buffer_size;
2794 fmt->fmt.pix.pixelformat =
2795 fh->v4l_settings.format->fourcc;
2796 fmt->fmt.pix.colorspace =
2797 fh->v4l_settings.format->colorspace;
2798 fmt->fmt.pix.bytesperline = 0;
2799 if (BUZ_MAX_HEIGHT <
2800 (fh->v4l_settings.height * 2))
2801 fmt->fmt.pix.field =
2802 V4L2_FIELD_INTERLACED;
2804 fmt->fmt.pix.field =
2809 fmt->fmt.pix.width =
2810 fh->jpg_settings.img_width /
2811 fh->jpg_settings.HorDcm;
2812 fmt->fmt.pix.height =
2813 fh->jpg_settings.img_height /
2814 (fh->jpg_settings.VerDcm *
2815 fh->jpg_settings.TmpDcm);
2816 fmt->fmt.pix.sizeimage =
2817 zoran_v4l2_calc_bufsize(&fh->
2819 fmt->fmt.pix.pixelformat =
2821 if (fh->jpg_settings.TmpDcm == 1)
2822 fmt->fmt.pix.field =
2824 odd_even ? V4L2_FIELD_SEQ_BT :
2827 fmt->fmt.pix.field =
2829 odd_even ? V4L2_FIELD_TOP :
2832 fmt->fmt.pix.bytesperline = 0;
2833 fmt->fmt.pix.colorspace =
2834 V4L2_COLORSPACE_SMPTE170M;
2837 up(&zr->resource_lock);
2844 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2845 ZR_DEVNAME(zr), fmt->type);
2854 struct v4l2_format *fmt = arg;
2858 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2859 ZR_DEVNAME(zr), fmt->type);
2861 switch (fmt->type) {
2862 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2864 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2865 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2866 fmt->fmt.win.w.width,
2867 fmt->fmt.win.w.height,
2868 fmt->fmt.win.clipcount,
2869 fmt->fmt.win.bitmap);
2870 down(&zr->resource_lock);
2872 setup_window(file, fmt->fmt.win.w.left,
2874 fmt->fmt.win.w.width,
2875 fmt->fmt.win.w.height,
2876 (struct video_clip *)
2878 fmt->fmt.win.clipcount,
2879 fmt->fmt.win.bitmap);
2880 up(&zr->resource_lock);
2884 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2885 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2888 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2889 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2890 fmt->fmt.pix.width, fmt->fmt.pix.height,
2891 fmt->fmt.pix.pixelformat,
2892 (char *) &printformat);
2894 if (fmt->fmt.pix.bytesperline > 0) {
2896 KERN_ERR "%s: bpl not supported\n",
2901 /* we can be requested to do JPEG/raw playback/capture */
2903 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2904 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2905 fmt->fmt.pix.pixelformat ==
2906 V4L2_PIX_FMT_MJPEG))) {
2909 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2910 ZR_DEVNAME(zr), fmt->type,
2911 fmt->fmt.pix.pixelformat,
2912 (char *) &printformat);
2916 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2917 struct zoran_jpg_settings settings;
2919 down(&zr->resource_lock);
2921 settings = fh->jpg_settings;
2923 if (fh->v4l_buffers.allocated ||
2924 fh->jpg_buffers.allocated) {
2927 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2930 goto sfmtjpg_unlock_and_return;
2933 /* we actually need to set 'real' parameters now */
2934 if ((fmt->fmt.pix.height * 2) >
2936 settings.TmpDcm = 1;
2938 settings.TmpDcm = 2;
2939 settings.decimation = 0;
2940 if (fmt->fmt.pix.height <=
2941 fh->jpg_settings.img_height / 2)
2942 settings.VerDcm = 2;
2944 settings.VerDcm = 1;
2945 if (fmt->fmt.pix.width <=
2946 fh->jpg_settings.img_width / 4)
2947 settings.HorDcm = 4;
2948 else if (fmt->fmt.pix.width <=
2949 fh->jpg_settings.img_width / 2)
2950 settings.HorDcm = 2;
2952 settings.HorDcm = 1;
2953 if (settings.TmpDcm == 1)
2954 settings.field_per_buff = 2;
2956 settings.field_per_buff = 1;
2960 zoran_check_jpg_settings(zr,
2962 goto sfmtjpg_unlock_and_return;
2964 /* it's ok, so set them */
2965 fh->jpg_settings = settings;
2967 /* tell the user what we actually did */
2968 fmt->fmt.pix.width =
2969 settings.img_width / settings.HorDcm;
2970 fmt->fmt.pix.height =
2971 settings.img_height * 2 /
2972 (settings.TmpDcm * settings.VerDcm);
2973 if (settings.TmpDcm == 1)
2974 fmt->fmt.pix.field =
2976 odd_even ? V4L2_FIELD_SEQ_TB :
2979 fmt->fmt.pix.field =
2981 odd_even ? V4L2_FIELD_TOP :
2983 fh->jpg_buffers.buffer_size =
2984 zoran_v4l2_calc_bufsize(&fh->
2986 fmt->fmt.pix.sizeimage =
2987 fh->jpg_buffers.buffer_size;
2989 /* we hereby abuse this variable to show that
2990 * we're gonna do mjpeg capture */
2993 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2994 ZORAN_MAP_MODE_JPG_REC :
2995 ZORAN_MAP_MODE_JPG_PLAY;
2996 sfmtjpg_unlock_and_return:
2997 up(&zr->resource_lock);
2999 for (i = 0; i < zoran_num_formats; i++)
3000 if (fmt->fmt.pix.pixelformat ==
3001 zoran_formats[i].fourcc)
3003 if (i == zoran_num_formats) {
3006 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3008 fmt->fmt.pix.pixelformat,
3009 (char *) &printformat);
3012 down(&zr->resource_lock);
3013 if (fh->jpg_buffers.allocated ||
3014 (fh->v4l_buffers.allocated &&
3015 fh->v4l_buffers.active !=
3019 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3022 goto sfmtv4l_unlock_and_return;
3024 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3025 fmt->fmt.pix.height =
3027 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3028 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3031 zoran_v4l_set_format(file,
3038 goto sfmtv4l_unlock_and_return;
3040 /* tell the user the
3041 * results/missing stuff */
3042 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3044 if (BUZ_MAX_HEIGHT <
3045 (fh->v4l_settings.height * 2))
3046 fmt->fmt.pix.field =
3047 V4L2_FIELD_INTERLACED;
3049 fmt->fmt.pix.field =
3052 fh->map_mode = ZORAN_MAP_MODE_RAW;
3053 sfmtv4l_unlock_and_return:
3054 up(&zr->resource_lock);
3060 dprintk(3, "unsupported\n");
3063 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3064 ZR_DEVNAME(zr), fmt->type);
3074 struct v4l2_framebuffer *fb = arg;
3076 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3078 memset(fb, 0, sizeof(*fb));
3079 down(&zr->resource_lock);
3080 fb->base = zr->buffer.base;
3081 fb->fmt.width = zr->buffer.width;
3082 fb->fmt.height = zr->buffer.height;
3083 if (zr->overlay_settings.format) {
3084 fb->fmt.pixelformat =
3085 fh->overlay_settings.format->fourcc;
3087 fb->fmt.bytesperline = zr->buffer.bytesperline;
3088 up(&zr->resource_lock);
3089 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3090 fb->fmt.field = V4L2_FIELD_INTERLACED;
3091 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3092 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3101 struct v4l2_framebuffer *fb = arg;
3102 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3106 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3107 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3108 fb->fmt.bytesperline, fb->fmt.pixelformat,
3109 (char *) &printformat);
3111 for (i = 0; i < zoran_num_formats; i++)
3112 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3114 if (i == zoran_num_formats) {
3117 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3118 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3119 (char *) &printformat);
3123 down(&zr->resource_lock);
3125 setup_fbuffer(file, fb->base, &zoran_formats[i],
3126 fb->fmt.width, fb->fmt.height,
3127 fb->fmt.bytesperline);
3128 up(&zr->resource_lock);
3134 case VIDIOC_OVERLAY:
3138 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3139 ZR_DEVNAME(zr), *on);
3141 down(&zr->resource_lock);
3142 res = setup_overlay(file, *on);
3143 up(&zr->resource_lock);
3149 case VIDIOC_REQBUFS:
3151 struct v4l2_requestbuffers *req = arg;
3154 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3155 ZR_DEVNAME(zr), req->type);
3157 if (req->memory != V4L2_MEMORY_MMAP) {
3160 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3161 ZR_DEVNAME(zr), req->memory);
3165 down(&zr->resource_lock);
3167 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3170 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3173 goto v4l2reqbuf_unlock_and_return;
3176 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3177 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3179 /* control user input */
3182 if (req->count > v4l_nbufs)
3183 req->count = v4l_nbufs;
3184 fh->v4l_buffers.num_buffers = req->count;
3186 if (v4l_fbuffer_alloc(file)) {
3188 goto v4l2reqbuf_unlock_and_return;
3191 /* The next mmap will map the V4L buffers */
3192 fh->map_mode = ZORAN_MAP_MODE_RAW;
3194 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3195 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3197 /* we need to calculate size ourselves now */
3200 if (req->count > jpg_nbufs)
3201 req->count = jpg_nbufs;
3202 fh->jpg_buffers.num_buffers = req->count;
3203 fh->jpg_buffers.buffer_size =
3204 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3206 if (jpg_fbuffer_alloc(file)) {
3208 goto v4l2reqbuf_unlock_and_return;
3211 /* The next mmap will map the MJPEG buffers */
3212 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3213 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3215 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3220 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3221 ZR_DEVNAME(zr), req->type);
3223 goto v4l2reqbuf_unlock_and_return;
3225 v4l2reqbuf_unlock_and_return:
3226 up(&zr->resource_lock);
3232 case VIDIOC_QUERYBUF:
3234 struct v4l2_buffer *buf = arg;
3235 __u32 type = buf->type;
3236 int index = buf->index, res;
3240 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3241 ZR_DEVNAME(zr), buf->index, buf->type);
3243 memset(buf, 0, sizeof(buf));
3247 down(&zr->resource_lock);
3248 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3249 up(&zr->resource_lock);
3257 struct v4l2_buffer *buf = arg;
3258 int res = 0, codec_mode, buf_type;
3261 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3262 ZR_DEVNAME(zr), buf->type, buf->index);
3264 down(&zr->resource_lock);
3266 switch (fh->map_mode) {
3267 case ZORAN_MAP_MODE_RAW:
3268 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3271 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3272 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3274 goto qbuf_unlock_and_return;
3277 res = zoran_v4l_queue_frame(file, buf->index);
3279 goto qbuf_unlock_and_return;
3280 if (!zr->v4l_memgrab_active &&
3281 fh->v4l_buffers.active == ZORAN_LOCKED)
3282 zr36057_set_memgrab(zr, 1);
3285 case ZORAN_MAP_MODE_JPG_REC:
3286 case ZORAN_MAP_MODE_JPG_PLAY:
3287 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3288 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3289 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3291 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3292 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3295 if (buf->type != buf_type) {
3298 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3299 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3301 goto qbuf_unlock_and_return;
3305 zoran_jpg_queue_frame(file, buf->index,
3308 goto qbuf_unlock_and_return;
3309 if (zr->codec_mode == BUZ_MODE_IDLE &&
3310 fh->jpg_buffers.active == ZORAN_LOCKED) {
3311 zr36057_enable_jpg(zr, codec_mode);
3318 "%s: VIDIOC_QBUF - unsupported type %d\n",
3319 ZR_DEVNAME(zr), buf->type);
3321 goto qbuf_unlock_and_return;
3323 qbuf_unlock_and_return:
3324 up(&zr->resource_lock);
3332 struct v4l2_buffer *buf = arg;
3333 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3335 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3336 ZR_DEVNAME(zr), buf->type);
3338 down(&zr->resource_lock);
3340 switch (fh->map_mode) {
3341 case ZORAN_MAP_MODE_RAW:
3342 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3345 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3346 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3348 goto dqbuf_unlock_and_return;
3351 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3352 if (file->f_flags & O_NONBLOCK &&
3353 zr->v4l_buffers.buffer[num].state !=
3356 goto dqbuf_unlock_and_return;
3358 res = v4l_sync(file, num);
3360 goto dqbuf_unlock_and_return;
3362 zr->v4l_sync_tail++;
3363 res = zoran_v4l2_buffer_status(file, buf, num);
3366 case ZORAN_MAP_MODE_JPG_REC:
3367 case ZORAN_MAP_MODE_JPG_PLAY:
3369 struct zoran_sync bs;
3371 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3372 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3374 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3376 if (buf->type != buf_type) {
3379 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3380 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3382 goto dqbuf_unlock_and_return;
3387 jpg_que_tail & BUZ_MASK_FRAME];
3389 if (file->f_flags & O_NONBLOCK &&
3390 zr->jpg_buffers.buffer[num].state !=
3393 goto dqbuf_unlock_and_return;
3395 res = jpg_sync(file, &bs);
3397 goto dqbuf_unlock_and_return;
3399 zoran_v4l2_buffer_status(file, buf, bs.frame);
3406 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3407 ZR_DEVNAME(zr), buf->type);
3409 goto dqbuf_unlock_and_return;
3411 dqbuf_unlock_and_return:
3412 up(&zr->resource_lock);
3418 case VIDIOC_STREAMON:
3422 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3424 down(&zr->resource_lock);
3426 switch (fh->map_mode) {
3427 case ZORAN_MAP_MODE_RAW: /* raw capture */
3428 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3429 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3431 goto strmon_unlock_and_return;
3434 zr->v4l_buffers.active = fh->v4l_buffers.active =
3436 zr->v4l_settings = fh->v4l_settings;
3438 zr->v4l_sync_tail = zr->v4l_pend_tail;
3439 if (!zr->v4l_memgrab_active &&
3440 zr->v4l_pend_head != zr->v4l_pend_tail) {
3441 zr36057_set_memgrab(zr, 1);
3445 case ZORAN_MAP_MODE_JPG_REC:
3446 case ZORAN_MAP_MODE_JPG_PLAY:
3447 /* what is the codec mode right now? */
3448 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3449 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3451 goto strmon_unlock_and_return;
3454 zr->jpg_buffers.active = fh->jpg_buffers.active =
3457 if (zr->jpg_que_head != zr->jpg_que_tail) {
3458 /* Start the jpeg codec when the first frame is queued */
3466 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3467 ZR_DEVNAME(zr), fh->map_mode);
3469 goto strmon_unlock_and_return;
3471 strmon_unlock_and_return:
3472 up(&zr->resource_lock);
3478 case VIDIOC_STREAMOFF:
3482 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3484 down(&zr->resource_lock);
3486 switch (fh->map_mode) {
3487 case ZORAN_MAP_MODE_RAW: /* raw capture */
3488 if (fh->v4l_buffers.active == ZORAN_FREE &&
3489 zr->v4l_buffers.active != ZORAN_FREE) {
3490 res = -EPERM; /* stay off other's settings! */
3491 goto strmoff_unlock_and_return;
3493 if (zr->v4l_buffers.active == ZORAN_FREE)
3494 goto strmoff_unlock_and_return;
3496 /* unload capture */
3497 if (zr->v4l_memgrab_active)
3498 zr36057_set_memgrab(zr, 0);
3500 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3501 zr->v4l_buffers.buffer[i].state =
3503 fh->v4l_buffers = zr->v4l_buffers;
3505 zr->v4l_buffers.active = fh->v4l_buffers.active =
3508 zr->v4l_grab_seq = 0;
3509 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3510 zr->v4l_sync_tail = 0;
3514 case ZORAN_MAP_MODE_JPG_REC:
3515 case ZORAN_MAP_MODE_JPG_PLAY:
3516 if (fh->jpg_buffers.active == ZORAN_FREE &&
3517 zr->jpg_buffers.active != ZORAN_FREE) {
3518 res = -EPERM; /* stay off other's settings! */
3519 goto strmoff_unlock_and_return;
3521 if (zr->jpg_buffers.active == ZORAN_FREE)
3522 goto strmoff_unlock_and_return;
3527 ZORAN_MAP_MODE_JPG_REC) ?
3528 BUZ_MODE_MOTION_COMPRESS :
3529 BUZ_MODE_MOTION_DECOMPRESS);
3531 goto strmoff_unlock_and_return;
3536 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3537 ZR_DEVNAME(zr), fh->map_mode);
3539 goto strmoff_unlock_and_return;
3541 strmoff_unlock_and_return:
3542 up(&zr->resource_lock);
3548 case VIDIOC_QUERYCTRL:
3550 struct v4l2_queryctrl *ctrl = arg;
3552 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3553 ZR_DEVNAME(zr), ctrl->id);
3555 /* we only support hue/saturation/contrast/brightness */
3556 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3557 ctrl->id > V4L2_CID_HUE)
3561 memset(ctrl, 0, sizeof(*ctrl));
3566 case V4L2_CID_BRIGHTNESS:
3567 strncpy(ctrl->name, "Brightness", 31);
3569 case V4L2_CID_CONTRAST:
3570 strncpy(ctrl->name, "Contrast", 31);
3572 case V4L2_CID_SATURATION:
3573 strncpy(ctrl->name, "Saturation", 31);
3576 strncpy(ctrl->name, "Hue", 31);
3581 ctrl->maximum = 65535;
3583 ctrl->default_value = 32768;
3584 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3592 struct v4l2_control *ctrl = arg;
3594 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3595 ZR_DEVNAME(zr), ctrl->id);
3597 /* we only support hue/saturation/contrast/brightness */
3598 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3599 ctrl->id > V4L2_CID_HUE)
3602 down(&zr->resource_lock);
3604 case V4L2_CID_BRIGHTNESS:
3605 ctrl->value = zr->brightness;
3607 case V4L2_CID_CONTRAST:
3608 ctrl->value = zr->contrast;
3610 case V4L2_CID_SATURATION:
3611 ctrl->value = zr->saturation;
3614 ctrl->value = zr->hue;
3617 up(&zr->resource_lock);
3625 struct v4l2_control *ctrl = arg;
3626 struct video_picture pict;
3628 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3629 ZR_DEVNAME(zr), ctrl->id);
3631 /* we only support hue/saturation/contrast/brightness */
3632 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3633 ctrl->id > V4L2_CID_HUE)
3636 if (ctrl->value < 0 || ctrl->value > 65535) {
3639 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3640 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3644 down(&zr->resource_lock);
3646 case V4L2_CID_BRIGHTNESS:
3647 zr->brightness = ctrl->value;
3649 case V4L2_CID_CONTRAST:
3650 zr->contrast = ctrl->value;
3652 case V4L2_CID_SATURATION:
3653 zr->saturation = ctrl->value;
3656 zr->hue = ctrl->value;
3659 pict.brightness = zr->brightness;
3660 pict.contrast = zr->contrast;
3661 pict.colour = zr->saturation;
3664 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3666 up(&zr->resource_lock);
3672 case VIDIOC_ENUMSTD:
3674 struct v4l2_standard *std = arg;
3676 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3677 ZR_DEVNAME(zr), std->index);
3679 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3682 int id = std->index;
3683 memset(std, 0, sizeof(*std));
3687 if (std->index == zr->card.norms) {
3688 /* if we have autodetect, ... */
3689 struct video_decoder_capability caps;
3690 decoder_command(zr, DECODER_GET_CAPABILITIES,
3692 if (caps.flags & VIDEO_DECODER_AUTO) {
3693 std->id = V4L2_STD_ALL;
3694 strncpy(std->name, "Autodetect", 31);
3699 switch (std->index) {
3701 std->id = V4L2_STD_PAL;
3702 strncpy(std->name, "PAL", 31);
3703 std->frameperiod.numerator = 1;
3704 std->frameperiod.denominator = 25;
3705 std->framelines = zr->card.tvn[0]->Ht;
3708 std->id = V4L2_STD_NTSC;
3709 strncpy(std->name, "NTSC", 31);
3710 std->frameperiod.numerator = 1001;
3711 std->frameperiod.denominator = 30000;
3712 std->framelines = zr->card.tvn[1]->Ht;
3715 std->id = V4L2_STD_SECAM;
3716 strncpy(std->name, "SECAM", 31);
3717 std->frameperiod.numerator = 1;
3718 std->frameperiod.denominator = 25;
3719 std->framelines = zr->card.tvn[2]->Ht;
3729 v4l2_std_id *std = arg;
3732 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3734 down(&zr->resource_lock);
3736 up(&zr->resource_lock);
3739 case VIDEO_MODE_PAL:
3740 *std = V4L2_STD_PAL;
3742 case VIDEO_MODE_NTSC:
3743 *std = V4L2_STD_NTSC;
3745 case VIDEO_MODE_SECAM:
3746 *std = V4L2_STD_SECAM;
3756 int norm = -1, res = 0;
3757 v4l2_std_id *std = arg;
3759 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3760 ZR_DEVNAME(zr), *std);
3762 if (*std == V4L2_STD_PAL)
3763 norm = VIDEO_MODE_PAL;
3764 else if (*std == V4L2_STD_NTSC)
3765 norm = VIDEO_MODE_NTSC;
3766 else if (*std == V4L2_STD_SECAM)
3767 norm = VIDEO_MODE_SECAM;
3768 else if (*std == V4L2_STD_ALL)
3769 norm = VIDEO_MODE_AUTO;
3773 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3774 ZR_DEVNAME(zr), *std);
3778 down(&zr->resource_lock);
3779 if ((res = zoran_set_norm(zr, norm)))
3780 goto sstd_unlock_and_return;
3782 res = wait_grab_pending(zr);
3783 sstd_unlock_and_return:
3784 up(&zr->resource_lock);
3789 case VIDIOC_ENUMINPUT:
3791 struct v4l2_input *inp = arg;
3794 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3795 ZR_DEVNAME(zr), inp->index);
3797 if (inp->index < 0 || inp->index >= zr->card.inputs)
3800 int id = inp->index;
3801 memset(inp, 0, sizeof(*inp));
3805 strncpy(inp->name, zr->card.input[inp->index].name,
3806 sizeof(inp->name) - 1);
3807 inp->type = V4L2_INPUT_TYPE_CAMERA;
3808 inp->std = V4L2_STD_ALL;
3810 /* Get status of video decoder */
3811 down(&zr->resource_lock);
3812 decoder_command(zr, DECODER_GET_STATUS, &status);
3813 up(&zr->resource_lock);
3815 if (!(status & DECODER_STATUS_GOOD)) {
3816 inp->status |= V4L2_IN_ST_NO_POWER;
3817 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3819 if (!(status & DECODER_STATUS_COLOR))
3820 inp->status |= V4L2_IN_ST_NO_COLOR;
3826 case VIDIOC_G_INPUT:
3830 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3832 down(&zr->resource_lock);
3834 up(&zr->resource_lock);
3840 case VIDIOC_S_INPUT:
3842 int *input = arg, res = 0;
3844 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3845 ZR_DEVNAME(zr), *input);
3847 down(&zr->resource_lock);
3848 if ((res = zoran_set_input(zr, *input)))
3849 goto sinput_unlock_and_return;
3851 /* Make sure the changes come into effect */
3852 res = wait_grab_pending(zr);
3853 sinput_unlock_and_return:
3854 up(&zr->resource_lock);
3859 case VIDIOC_ENUMOUTPUT:
3861 struct v4l2_output *outp = arg;
3863 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3864 ZR_DEVNAME(zr), outp->index);
3866 if (outp->index != 0)
3869 memset(outp, 0, sizeof(*outp));
3871 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3872 strncpy(outp->name, "Autodetect", 31);
3878 case VIDIOC_G_OUTPUT:
3882 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3890 case VIDIOC_S_OUTPUT:
3894 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3895 ZR_DEVNAME(zr), *output);
3904 /* cropping (sub-frame capture) */
3905 case VIDIOC_CROPCAP:
3907 struct v4l2_cropcap *cropcap = arg;
3908 int type = cropcap->type, res = 0;
3910 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3911 ZR_DEVNAME(zr), cropcap->type);
3913 memset(cropcap, 0, sizeof(*cropcap));
3914 cropcap->type = type;
3916 down(&zr->resource_lock);
3918 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3919 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3920 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3923 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3926 goto cropcap_unlock_and_return;
3929 cropcap->bounds.top = cropcap->bounds.left = 0;
3930 cropcap->bounds.width = BUZ_MAX_WIDTH;
3931 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3932 cropcap->defrect.top = cropcap->defrect.left = 0;
3933 cropcap->defrect.width = BUZ_MIN_WIDTH;
3934 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3935 cropcap_unlock_and_return:
3936 up(&zr->resource_lock);
3943 struct v4l2_crop *crop = arg;
3944 int type = crop->type, res = 0;
3946 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3947 ZR_DEVNAME(zr), crop->type);
3949 memset(crop, 0, sizeof(*crop));
3952 down(&zr->resource_lock);
3954 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3955 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3956 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3959 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3962 goto gcrop_unlock_and_return;
3965 crop->c.top = fh->jpg_settings.img_y;
3966 crop->c.left = fh->jpg_settings.img_x;
3967 crop->c.width = fh->jpg_settings.img_width;
3968 crop->c.height = fh->jpg_settings.img_height;
3970 gcrop_unlock_and_return:
3971 up(&zr->resource_lock);
3979 struct v4l2_crop *crop = arg;
3981 struct zoran_jpg_settings settings = fh->jpg_settings;
3985 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3986 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3987 crop->c.width, crop->c.height);
3989 down(&zr->resource_lock);
3991 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3994 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3997 goto scrop_unlock_and_return;
4000 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
4001 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4002 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4005 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4008 goto scrop_unlock_and_return;
4011 /* move into a form that we understand */
4012 settings.img_x = crop->c.left;
4013 settings.img_y = crop->c.top;
4014 settings.img_width = crop->c.width;
4015 settings.img_height = crop->c.height;
4017 /* check validity */
4018 if ((res = zoran_check_jpg_settings(zr, &settings)))
4019 goto scrop_unlock_and_return;
4022 fh->jpg_settings = settings;
4024 scrop_unlock_and_return:
4025 up(&zr->resource_lock);
4030 case VIDIOC_G_JPEGCOMP:
4032 struct v4l2_jpegcompression *params = arg;
4034 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4037 memset(params, 0, sizeof(*params));
4039 down(&zr->resource_lock);
4041 params->quality = fh->jpg_settings.jpg_comp.quality;
4042 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4043 memcpy(params->APP_data,
4044 fh->jpg_settings.jpg_comp.APP_data,
4045 fh->jpg_settings.jpg_comp.APP_len);
4046 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4047 memcpy(params->COM_data,
4048 fh->jpg_settings.jpg_comp.COM_data,
4049 fh->jpg_settings.jpg_comp.COM_len);
4050 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4051 params->jpeg_markers =
4052 fh->jpg_settings.jpg_comp.jpeg_markers;
4054 up(&zr->resource_lock);
4060 case VIDIOC_S_JPEGCOMP:
4062 struct v4l2_jpegcompression *params = arg;
4063 struct zoran_jpg_settings settings = fh->jpg_settings;
4068 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4069 ZR_DEVNAME(zr), params->quality, params->APPn,
4070 params->APP_len, params->COM_len);
4072 settings.jpg_comp = *params;
4074 down(&zr->resource_lock);
4076 if (fh->v4l_buffers.active != ZORAN_FREE ||
4077 fh->jpg_buffers.active != ZORAN_FREE) {
4080 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4083 goto sjpegc_unlock_and_return;
4086 if ((res = zoran_check_jpg_settings(zr, &settings)))
4087 goto sjpegc_unlock_and_return;
4088 if (!fh->jpg_buffers.allocated)
4089 fh->jpg_buffers.buffer_size =
4090 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4091 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4092 sjpegc_unlock_and_return:
4093 up(&zr->resource_lock);
4099 case VIDIOC_QUERYSTD: /* why is this useful? */
4101 v4l2_std_id *std = arg;
4104 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4105 ZR_DEVNAME(zr), *std);
4107 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4108 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4109 zr->card.norms == 3)) {
4117 case VIDIOC_TRY_FMT:
4119 struct v4l2_format *fmt = arg;
4122 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4123 ZR_DEVNAME(zr), fmt->type);
4125 switch (fmt->type) {
4126 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4127 down(&zr->resource_lock);
4129 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4130 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4131 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4132 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4133 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4134 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4135 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4136 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4138 up(&zr->resource_lock);
4141 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4142 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4143 if (fmt->fmt.pix.bytesperline > 0)
4146 down(&zr->resource_lock);
4148 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4149 struct zoran_jpg_settings settings =
4152 /* we actually need to set 'real' parameters now */
4153 if ((fmt->fmt.pix.height * 2) >
4155 settings.TmpDcm = 1;
4157 settings.TmpDcm = 2;
4158 settings.decimation = 0;
4159 if (fmt->fmt.pix.height <=
4160 fh->jpg_settings.img_height / 2)
4161 settings.VerDcm = 2;
4163 settings.VerDcm = 1;
4164 if (fmt->fmt.pix.width <=
4165 fh->jpg_settings.img_width / 4)
4166 settings.HorDcm = 4;
4167 else if (fmt->fmt.pix.width <=
4168 fh->jpg_settings.img_width / 2)
4169 settings.HorDcm = 2;
4171 settings.HorDcm = 1;
4172 if (settings.TmpDcm == 1)
4173 settings.field_per_buff = 2;
4175 settings.field_per_buff = 1;
4179 zoran_check_jpg_settings(zr,
4181 goto tryfmt_unlock_and_return;
4183 /* tell the user what we actually did */
4184 fmt->fmt.pix.width =
4185 settings.img_width / settings.HorDcm;
4186 fmt->fmt.pix.height =
4187 settings.img_height * 2 /
4188 (settings.TmpDcm * settings.VerDcm);
4189 if (settings.TmpDcm == 1)
4190 fmt->fmt.pix.field =
4192 odd_even ? V4L2_FIELD_SEQ_TB :
4195 fmt->fmt.pix.field =
4197 odd_even ? V4L2_FIELD_TOP :
4200 fmt->fmt.pix.sizeimage =
4201 zoran_v4l2_calc_bufsize(&settings);
4202 } else if (fmt->type ==
4203 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4206 for (i = 0; i < zoran_num_formats; i++)
4207 if (zoran_formats[i].fourcc ==
4208 fmt->fmt.pix.pixelformat)
4210 if (i == zoran_num_formats) {
4212 goto tryfmt_unlock_and_return;
4215 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4216 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4217 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4218 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4219 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4220 fmt->fmt.pix.height =
4222 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4223 fmt->fmt.pix.height =
4227 goto tryfmt_unlock_and_return;
4229 tryfmt_unlock_and_return:
4230 up(&zr->resource_lock);
4245 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4246 ZR_DEVNAME(zr), cmd);
4247 return -ENOIOCTLCMD;
4256 zoran_ioctl (struct inode *inode,
4261 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4265 zoran_poll (struct file *file,
4268 struct zoran_fh *fh = file->private_data;
4269 struct zoran *zr = fh->zr;
4270 wait_queue_head_t *queue = NULL;
4273 /* we should check whether buffers are ready to be synced on
4274 * (w/o waits - O_NONBLOCK) here
4275 * if ready for read (sync), return POLLIN|POLLRDNORM,
4276 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4277 * if error, return POLLERR,
4278 * if no buffers queued or so, return POLLNVAL
4281 down(&zr->resource_lock);
4283 switch (fh->map_mode) {
4284 case ZORAN_MAP_MODE_RAW:
4285 if (fh->v4l_buffers.active == ZORAN_FREE ||
4286 zr->v4l_pend_head == zr->v4l_pend_tail) {
4288 "%s: zoran_poll() - no buffers queued\n",
4291 goto poll_unlock_and_return;
4293 queue = &zr->v4l_capq;
4294 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4295 poll_wait(file, queue, wait);
4296 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4297 res = POLLIN | POLLRDNORM;
4300 case ZORAN_MAP_MODE_JPG_REC:
4301 case ZORAN_MAP_MODE_JPG_PLAY:
4302 if (fh->jpg_buffers.active == ZORAN_FREE ||
4303 zr->jpg_que_head == zr->jpg_que_tail) {
4305 "%s: zoran_poll() - no buffers queued\n",
4308 goto poll_unlock_and_return;
4310 queue = &zr->jpg_capq;
4311 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4312 poll_wait(file, queue, wait);
4313 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4314 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4315 res = POLLIN | POLLRDNORM;
4317 res = POLLOUT | POLLWRNORM;
4323 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4324 ZR_DEVNAME(zr), fh->map_mode);
4326 goto poll_unlock_and_return;
4329 poll_unlock_and_return:
4330 up(&zr->resource_lock);
4337 * This maps the buffers to user space.
4339 * Depending on the state of fh->map_mode
4340 * the V4L or the MJPEG buffers are mapped
4341 * per buffer or all together
4343 * Note that we need to connect to some
4344 * unmap signal event to unmap the de-allocate
4345 * the buffer accordingly (zoran_vm_close())
4349 zoran_vm_open (struct vm_area_struct *vma)
4351 struct zoran_mapping *map = vma->vm_private_data;
4357 zoran_vm_close (struct vm_area_struct *vma)
4359 struct zoran_mapping *map = vma->vm_private_data;
4360 struct file *file = map->file;
4361 struct zoran_fh *fh = file->private_data;
4362 struct zoran *zr = fh->zr;
4366 if (map->count == 0) {
4367 switch (fh->map_mode) {
4368 case ZORAN_MAP_MODE_JPG_REC:
4369 case ZORAN_MAP_MODE_JPG_PLAY:
4371 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4374 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4375 if (fh->jpg_buffers.buffer[i].map == map) {
4376 fh->jpg_buffers.buffer[i].map =
4382 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4383 if (fh->jpg_buffers.buffer[i].map)
4385 if (i == fh->jpg_buffers.num_buffers) {
4386 down(&zr->resource_lock);
4388 if (fh->jpg_buffers.active != ZORAN_FREE) {
4389 jpg_qbuf(file, -1, zr->codec_mode);
4390 zr->jpg_buffers.allocated = 0;
4391 zr->jpg_buffers.active =
4392 fh->jpg_buffers.active =
4395 //jpg_fbuffer_free(file);
4396 fh->jpg_buffers.allocated = 0;
4397 fh->jpg_buffers.ready_to_be_freed = 1;
4399 up(&zr->resource_lock);
4404 case ZORAN_MAP_MODE_RAW:
4406 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4409 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4410 if (fh->v4l_buffers.buffer[i].map == map) {
4412 fh->v4l_buffers.buffer[i].map =
4418 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4419 if (fh->v4l_buffers.buffer[i].map)
4421 if (i == fh->v4l_buffers.num_buffers) {
4422 down(&zr->resource_lock);
4424 if (fh->v4l_buffers.active != ZORAN_FREE) {
4425 zr36057_set_memgrab(zr, 0);
4426 zr->v4l_buffers.allocated = 0;
4427 zr->v4l_buffers.active =
4428 fh->v4l_buffers.active =
4431 //v4l_fbuffer_free(file);
4432 fh->v4l_buffers.allocated = 0;
4433 fh->v4l_buffers.ready_to_be_freed = 1;
4435 up(&zr->resource_lock);
4442 "%s: munmap() - internal error - unknown map mode %d\n",
4443 ZR_DEVNAME(zr), fh->map_mode);
4450 static struct vm_operations_struct zoran_vm_ops = {
4451 .open = zoran_vm_open,
4452 .close = zoran_vm_close,
4455 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4456 #define zr_remap_page_range(a,b,c,d,e) remap_page_range(b,c,d,e)
4458 #define zr_remap_page_range(a,b,c,d,e) remap_page_range(a,b,c,d,e)
4462 zoran_mmap (struct file *file,
4463 struct vm_area_struct *vma)
4465 struct zoran_fh *fh = file->private_data;
4466 struct zoran *zr = fh->zr;
4467 unsigned long size = (vma->vm_end - vma->vm_start);
4468 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4470 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4472 struct zoran_mapping *map;
4476 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4478 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4479 vma->vm_start, vma->vm_end, size);
4481 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4482 !(vma->vm_flags & VM_WRITE)) {
4485 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4490 switch (fh->map_mode) {
4492 case ZORAN_MAP_MODE_JPG_REC:
4493 case ZORAN_MAP_MODE_JPG_PLAY:
4496 down(&zr->resource_lock);
4498 /* Map the MJPEG buffers */
4499 if (!fh->jpg_buffers.allocated) {
4502 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4505 goto jpg_mmap_unlock_and_return;
4508 first = offset / fh->jpg_buffers.buffer_size;
4509 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4510 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4511 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4512 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4513 last >= fh->jpg_buffers.num_buffers) {
4516 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4517 ZR_DEVNAME(zr), offset, size,
4518 fh->jpg_buffers.buffer_size,
4519 fh->jpg_buffers.num_buffers);
4521 goto jpg_mmap_unlock_and_return;
4523 for (i = first; i <= last; i++) {
4524 if (fh->jpg_buffers.buffer[i].map) {
4527 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4530 goto jpg_mmap_unlock_and_return;
4534 /* map these buffers (v4l_buffers[i]) */
4535 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4538 goto jpg_mmap_unlock_and_return;
4543 vma->vm_ops = &zoran_vm_ops;
4544 vma->vm_flags |= VM_DONTEXPAND;
4545 vma->vm_private_data = map;
4547 for (i = first; i <= last; i++) {
4549 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4552 (fh->jpg_buffers.buffer[i].
4553 frag_tab[2 * j + 1] & ~1) << 1;
4558 (unsigned long) fh->jpg_buffers.
4559 buffer[i].frag_tab[2 * j];
4560 page = virt_to_phys(bus_to_virt(pos)); /* should just be pos on i386 */
4561 if (zr_remap_page_range
4562 (vma, start, page, todo, PAGE_SHARED)) {
4565 "%s: zoran_mmap(V4L) - remap_page_range failed\n",
4568 goto jpg_mmap_unlock_and_return;
4574 if (fh->jpg_buffers.buffer[i].
4575 frag_tab[2 * j + 1] & 1)
4576 break; /* was last fragment */
4578 fh->jpg_buffers.buffer[i].map = map;
4583 jpg_mmap_unlock_and_return:
4584 up(&zr->resource_lock);
4588 case ZORAN_MAP_MODE_RAW:
4590 down(&zr->resource_lock);
4592 /* Map the V4L buffers */
4593 if (!fh->v4l_buffers.allocated) {
4596 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4599 goto v4l_mmap_unlock_and_return;
4602 first = offset / fh->v4l_buffers.buffer_size;
4603 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4604 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4605 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4606 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4607 last >= fh->v4l_buffers.buffer_size) {
4610 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4611 ZR_DEVNAME(zr), offset, size,
4612 fh->v4l_buffers.buffer_size,
4613 fh->v4l_buffers.num_buffers);
4615 goto v4l_mmap_unlock_and_return;
4617 for (i = first; i <= last; i++) {
4618 if (fh->v4l_buffers.buffer[i].map) {
4621 "%s: mmap(V4L) - buffer %d already mapped\n",
4624 goto v4l_mmap_unlock_and_return;
4628 /* map these buffers (v4l_buffers[i]) */
4629 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4632 goto v4l_mmap_unlock_and_return;
4637 vma->vm_ops = &zoran_vm_ops;
4638 vma->vm_flags |= VM_DONTEXPAND;
4639 vma->vm_private_data = map;
4641 for (i = first; i <= last; i++) {
4643 if (todo > fh->v4l_buffers.buffer_size)
4644 todo = fh->v4l_buffers.buffer_size;
4645 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4646 if (zr_remap_page_range
4647 (vma, start, page, todo, PAGE_SHARED)) {
4650 "%s: zoran_mmap(V4L)i - remap_page_range failed\n",
4653 goto v4l_mmap_unlock_and_return;
4657 fh->v4l_buffers.buffer[i].map = map;
4661 v4l_mmap_unlock_and_return:
4662 up(&zr->resource_lock);
4669 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4670 ZR_DEVNAME(zr), fh->map_mode);
4677 static struct file_operations zoran_fops = {
4678 .owner = THIS_MODULE,
4680 .release = zoran_close,
4681 .ioctl = zoran_ioctl,
4682 .llseek = no_llseek,
4684 .write = zoran_write,
4689 struct video_device zoran_template __devinitdata = {
4691 .type = ZORAN_VID_TYPE,
4693 .type2 = ZORAN_V4L2_VID_FLAGS,
4695 .hardware = ZORAN_HARDWARE,
4696 .fops = &zoran_fops,
4697 .release = &zoran_vdev_release,