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 = NULL;
428 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
429 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
432 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
433 ZR_DEVNAME(zr), size >> 10);
435 fh->v4l_buffers.buffer[i].fbuffer = NULL;
436 fh->v4l_buffers.buffer[i].fbuffer_phys =
437 pmem + i * fh->v4l_buffers.buffer_size;
438 fh->v4l_buffers.buffer[i].fbuffer_bus =
439 pmem + i * fh->v4l_buffers.buffer_size;
442 /* No bigphysarea present, usage of high memory disabled,
443 * but user wants buffers of more than MAX_KMALLOC_MEM */
446 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
450 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
451 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
452 fh->v4l_buffers.buffer_size >> 10);
458 fh->v4l_buffers.allocated = 1;
463 /* free the V4L grab buffers */
465 v4l_fbuffer_free (struct file *file)
467 struct zoran_fh *fh = file->private_data;
468 struct zoran *zr = fh->zr;
472 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
474 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
475 if (!fh->v4l_buffers.buffer[i].fbuffer)
478 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
479 mem = fh->v4l_buffers.buffer[i].fbuffer;
480 for (off = 0; off < fh->v4l_buffers.buffer_size;
482 ClearPageReserved(MAP_NR(mem + off));
483 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
485 #if defined(CONFIG_BIGPHYS_AREA)
487 bigphysarea_free_pages((void *) fh->v4l_buffers.
490 fh->v4l_buffers.buffer[i].fbuffer = NULL;
493 fh->v4l_buffers.allocated = 0;
494 fh->v4l_buffers.ready_to_be_freed = 0;
498 * Allocate the MJPEG grab buffers.
500 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
501 * kmalloc is used to request a physically contiguous area,
502 * else we allocate the memory in framgents with get_zeroed_page.
504 * If a Natoma chipset is present and this is a revision 1 zr36057,
505 * each MJPEG buffer needs to be physically contiguous.
506 * (RJ: This statement is from Dave Perks' original driver,
507 * I could never check it because I have a zr36067)
508 * The driver cares about this because it reduces the buffer
509 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
511 * RJ: The contents grab buffers needs never be accessed in the driver.
512 * Therefore there is no need to allocate them with vmalloc in order
513 * to get a contiguous virtual memory space.
514 * I don't understand why many other drivers first allocate them with
515 * vmalloc (which uses internally also get_zeroed_page, but delivers you
516 * virtual addresses) and then again have to make a lot of efforts
517 * to get the physical address.
522 jpg_fbuffer_alloc (struct file *file)
524 struct zoran_fh *fh = file->private_data;
525 struct zoran *zr = fh->zr;
529 /* we might have old buffers lying around */
530 if (fh->jpg_buffers.ready_to_be_freed) {
531 jpg_fbuffer_free(file);
534 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
535 if (fh->jpg_buffers.buffer[i].frag_tab)
538 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
541 /* Allocate fragment table for this buffer */
543 mem = get_zeroed_page(GFP_KERNEL);
547 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
549 jpg_fbuffer_free(file);
552 memset((void *) mem, 0, PAGE_SIZE);
553 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
554 fh->jpg_buffers.buffer[i].frag_tab_bus =
555 virt_to_bus((void *) mem);
557 //if (alloc_contig) {
558 if (fh->jpg_buffers.need_contiguous) {
560 (unsigned long) kmalloc(fh->jpg_buffers.
566 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
568 jpg_fbuffer_free(file);
571 fh->jpg_buffers.buffer[i].frag_tab[0] =
572 virt_to_bus((void *) mem);
573 fh->jpg_buffers.buffer[i].frag_tab[1] =
574 ((fh->jpg_buffers.buffer_size / 4) << 1) | 1;
575 for (off = 0; off < fh->jpg_buffers.buffer_size;
577 SetPageReserved(MAP_NR(mem + off));
579 /* jpg_bufsize is allreay page aligned */
581 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
583 mem = get_zeroed_page(GFP_KERNEL);
587 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
589 jpg_fbuffer_free(file);
593 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
594 virt_to_bus((void *) mem);
595 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
597 (PAGE_SIZE / 4) << 1;
598 SetPageReserved(MAP_NR(mem));
601 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= 1;
606 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
608 (fh->jpg_buffers.num_buffers *
609 fh->jpg_buffers.buffer_size) >> 10);
611 fh->jpg_buffers.allocated = 1;
616 /* free the MJPEG grab buffers */
618 jpg_fbuffer_free (struct file *file)
620 struct zoran_fh *fh = file->private_data;
621 struct zoran *zr = fh->zr;
625 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
627 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
628 if (!fh->jpg_buffers.buffer[i].frag_tab)
631 //if (alloc_contig) {
632 if (fh->jpg_buffers.need_contiguous) {
633 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
635 (unsigned char *) bus_to_virt(fh->
642 off < fh->jpg_buffers.buffer_size;
644 ClearPageReserved(MAP_NR
647 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
648 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
652 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
654 if (!fh->jpg_buffers.buffer[i].
657 ClearPageReserved(MAP_NR
660 buffer[i].frag_tab[2 *
662 free_page((unsigned long)
663 bus_to_virt(fh->jpg_buffers.
666 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
668 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
673 free_page((unsigned long) fh->jpg_buffers.buffer[i].
675 fh->jpg_buffers.buffer[i].frag_tab = NULL;
678 fh->jpg_buffers.allocated = 0;
679 fh->jpg_buffers.ready_to_be_freed = 0;
683 * V4L Buffer grabbing
687 zoran_v4l_set_format (struct file *file,
690 const struct zoran_format *format)
692 struct zoran_fh *fh = file->private_data;
693 struct zoran *zr = fh->zr;
696 /* Check size and format of the grab wanted */
698 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
699 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
702 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
703 ZR_DEVNAME(zr), width, height);
707 bpp = (format->depth + 7) / 8;
709 /* Check against available buffer size */
710 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
713 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
714 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
718 /* The video front end needs 4-byte alinged line sizes */
720 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
723 "%s: v4l_set_format() - wrong frame alingment\n",
728 fh->v4l_settings.width = width;
729 fh->v4l_settings.height = height;
730 fh->v4l_settings.format = format;
731 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
737 zoran_v4l_queue_frame (struct file *file,
740 struct zoran_fh *fh = file->private_data;
741 struct zoran *zr = fh->zr;
745 if (!fh->v4l_buffers.allocated) {
748 "%s: v4l_queue_frame() - buffers not yet allocated\n",
753 /* No grabbing outside the buffer range! */
754 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
757 "%s: v4l_queue_frame() - buffer %d is out of range\n",
758 ZR_DEVNAME(zr), num);
762 spin_lock_irqsave(&zr->spinlock, flags);
764 if (fh->v4l_buffers.active == ZORAN_FREE) {
765 if (zr->v4l_buffers.active == ZORAN_FREE) {
766 zr->v4l_buffers = fh->v4l_buffers;
767 fh->v4l_buffers.active = ZORAN_ACTIVE;
771 "%s: v4l_queue_frame() - another session is already capturing\n",
777 /* make sure a grab isn't going on currently with this buffer */
779 switch (zr->v4l_buffers.buffer[num].state) {
782 if (zr->v4l_buffers.active == ZORAN_FREE) {
783 fh->v4l_buffers.active = ZORAN_FREE;
784 zr->v4l_buffers.allocated = 0;
786 res = -EBUSY; /* what are you doing? */
791 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
792 ZR_DEVNAME(zr), num);
794 /* since there is at least one unused buffer there's room for at least
795 * one more pend[] entry */
796 zr->v4l_pend[zr->v4l_pend_head++ &
797 V4L_MASK_FRAME] = num;
798 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
799 zr->v4l_buffers.buffer[num].bs.length =
800 fh->v4l_settings.bytesperline *
801 zr->v4l_settings.height;
802 fh->v4l_buffers.buffer[num] =
803 zr->v4l_buffers.buffer[num];
808 spin_unlock_irqrestore(&zr->spinlock, flags);
810 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
811 zr->v4l_buffers.active = fh->v4l_buffers.active;
817 v4l_grab (struct file *file,
818 struct video_mmap *mp)
820 struct zoran_fh *fh = file->private_data;
821 struct zoran *zr = fh->zr;
824 for (i = 0; i < zoran_num_formats; i++) {
825 if (zoran_formats[i].palette == mp->format &&
826 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
827 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
830 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
833 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
839 * To minimize the time spent in the IRQ routine, we avoid setting up
840 * the video front end there.
841 * If this grab has different parameters from a running streaming capture
842 * we stop the streaming capture and start it over again.
844 if (zr->v4l_memgrab_active &&
845 (zr->v4l_settings.width != mp->width ||
846 zr->v4l_settings.height != mp->height ||
847 zr->v4l_settings.format->palette != mp->format)) {
848 res = wait_grab_pending(zr);
852 if ((res = zoran_v4l_set_format(file,
857 zr->v4l_settings = fh->v4l_settings;
859 /* queue the frame in the pending queue */
860 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
861 fh->v4l_buffers.active = ZORAN_FREE;
865 /* put the 36057 into frame grabbing mode */
866 if (!res && !zr->v4l_memgrab_active)
867 zr36057_set_memgrab(zr, 1);
869 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
875 * Sync on a V4L buffer
879 v4l_sync (struct file *file,
882 struct zoran_fh *fh = file->private_data;
883 struct zoran *zr = fh->zr;
886 if (fh->v4l_buffers.active == ZORAN_FREE) {
889 "%s: v4l_sync() - no grab active for this session\n",
894 /* check passed-in frame number */
895 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
897 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
898 ZR_DEVNAME(zr), frame);
902 /* Check if is buffer was queued at all */
903 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
906 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
911 /* wait on this buffer to get ready */
912 while (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_PEND) {
913 if (!interruptible_sleep_on_timeout(&zr->v4l_capq, 10 * HZ))
915 else if (signal_pending(current))
919 /* buffer should now be in BUZ_STATE_DONE */
920 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
922 KERN_ERR "%s: v4l_sync() - internal state error\n",
925 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
926 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
928 spin_lock_irqsave(&zr->spinlock, flags);
930 /* Check if streaming capture has finished */
931 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
932 zr36057_set_memgrab(zr, 0);
933 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
934 fh->v4l_buffers.active = zr->v4l_buffers.active =
936 zr->v4l_buffers.allocated = 0;
940 spin_unlock_irqrestore(&zr->spinlock, flags);
946 * Queue a MJPEG buffer for capture/playback
950 zoran_jpg_queue_frame (struct file *file,
952 enum zoran_codec_mode mode)
954 struct zoran_fh *fh = file->private_data;
955 struct zoran *zr = fh->zr;
959 /* Check if buffers are allocated */
960 if (!fh->jpg_buffers.allocated) {
963 "%s: jpg_queue_frame() - buffers not yet allocated\n",
968 /* No grabbing outside the buffer range! */
969 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
972 "%s: jpg_queue_frame() - buffer %d out of range\n",
973 ZR_DEVNAME(zr), num);
977 /* what is the codec mode right now? */
978 if (zr->codec_mode == BUZ_MODE_IDLE) {
979 zr->jpg_settings = fh->jpg_settings;
980 } else if (zr->codec_mode != mode) {
981 /* wrong codec mode active - invalid */
984 "%s: jpg_queue_frame() - codec in wrong mode\n",
989 spin_lock_irqsave(&zr->spinlock, flags);
991 if (fh->jpg_buffers.active == ZORAN_FREE) {
992 if (zr->jpg_buffers.active == ZORAN_FREE) {
993 zr->jpg_buffers = fh->jpg_buffers;
994 fh->jpg_buffers.active = ZORAN_ACTIVE;
998 "%s: jpg_queue_frame() - another session is already capturing\n",
1004 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1005 /* Ok load up the jpeg codec */
1006 zr36057_enable_jpg(zr, mode);
1010 switch (zr->jpg_buffers.buffer[num].state) {
1011 case BUZ_STATE_DONE:
1014 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1016 case BUZ_STATE_USER:
1017 /* since there is at least one unused buffer there's room for at
1018 *least one more pend[] entry */
1019 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1021 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1022 fh->jpg_buffers.buffer[num] =
1023 zr->jpg_buffers.buffer[num];
1024 zoran_feed_stat_com(zr);
1028 case BUZ_STATE_PEND:
1029 if (zr->jpg_buffers.active == ZORAN_FREE) {
1030 fh->jpg_buffers.active = ZORAN_FREE;
1031 zr->jpg_buffers.allocated = 0;
1033 res = -EBUSY; /* what are you doing? */
1038 spin_unlock_irqrestore(&zr->spinlock, flags);
1040 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1041 zr->jpg_buffers.active = fh->jpg_buffers.active;
1048 jpg_qbuf (struct file *file,
1050 enum zoran_codec_mode mode)
1052 struct zoran_fh *fh = file->private_data;
1053 struct zoran *zr = fh->zr;
1056 /* Does the user want to stop streaming? */
1058 if (zr->codec_mode == mode) {
1059 if (fh->jpg_buffers.active == ZORAN_FREE) {
1062 "%s: jpg_qbuf(-1) - session not active\n",
1066 fh->jpg_buffers.active = zr->jpg_buffers.active =
1068 zr->jpg_buffers.allocated = 0;
1069 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1074 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1080 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1083 /* Start the jpeg codec when the first frame is queued */
1084 if (!res && zr->jpg_que_head == 1)
1091 * Sync on a MJPEG buffer
1095 jpg_sync (struct file *file,
1096 struct zoran_sync *bs)
1098 struct zoran_fh *fh = file->private_data;
1099 struct zoran *zr = fh->zr;
1100 unsigned long flags;
1103 if (fh->jpg_buffers.active == ZORAN_FREE) {
1106 "%s: jpg_sync() - capture is not currently active\n",
1110 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1111 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1114 "%s: jpg_sync() - codec not in streaming mode\n",
1118 while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1119 if (zr->jpg_dma_tail == zr->jpg_dma_head)
1123 interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
1127 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1129 zr->codec->control(zr->codec, CODEC_G_STATUS,
1133 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1134 ZR_DEVNAME(zr), isr);
1138 } else if (signal_pending(current))
1139 return -ERESTARTSYS;
1142 spin_lock_irqsave(&zr->spinlock, flags);
1144 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1145 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1147 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1149 /* buffer should now be in BUZ_STATE_DONE */
1151 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1154 "%s: jpg_sync() - internal state error\n",
1157 *bs = zr->jpg_buffers.buffer[frame].bs;
1159 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1160 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1162 spin_unlock_irqrestore(&zr->spinlock, flags);
1168 zoran_open_init_session (struct file *file)
1171 struct zoran_fh *fh = file->private_data;
1172 struct zoran *zr = fh->zr;
1174 /* Per default, map the V4L Buffers */
1175 fh->map_mode = ZORAN_MAP_MODE_RAW;
1177 /* take over the card's current settings */
1178 fh->overlay_settings = zr->overlay_settings;
1179 fh->overlay_settings.is_set = 0;
1180 fh->overlay_settings.format = zr->overlay_settings.format;
1181 fh->overlay_active = ZORAN_FREE;
1184 fh->v4l_settings = zr->v4l_settings;
1187 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1188 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1189 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1190 fh->v4l_buffers.buffer[i].bs.frame = i;
1192 fh->v4l_buffers.allocated = 0;
1193 fh->v4l_buffers.ready_to_be_freed = 0;
1194 fh->v4l_buffers.active = ZORAN_FREE;
1195 fh->v4l_buffers.buffer_size = v4l_bufsize;
1196 fh->v4l_buffers.num_buffers = v4l_nbufs;
1199 fh->jpg_settings = zr->jpg_settings;
1202 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1203 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1204 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1205 fh->jpg_buffers.buffer[i].bs.frame = i;
1207 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1208 fh->jpg_buffers.allocated = 0;
1209 fh->jpg_buffers.ready_to_be_freed = 0;
1210 fh->jpg_buffers.active = ZORAN_FREE;
1211 fh->jpg_buffers.buffer_size = jpg_bufsize;
1212 fh->jpg_buffers.num_buffers = jpg_nbufs;
1216 zoran_close_end_session (struct file *file)
1218 struct zoran_fh *fh = file->private_data;
1219 struct zoran *zr = fh->zr;
1222 if (fh->overlay_active != ZORAN_FREE) {
1223 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1224 zr->v4l_overlay_active = 0;
1225 if (!zr->v4l_memgrab_active)
1226 zr36057_overlay(zr, 0);
1227 zr->overlay_mask = NULL;
1231 if (fh->v4l_buffers.active != ZORAN_FREE) {
1232 zr36057_set_memgrab(zr, 0);
1233 zr->v4l_buffers.allocated = 0;
1234 zr->v4l_buffers.active = fh->v4l_buffers.active =
1239 if (fh->v4l_buffers.allocated ||
1240 fh->v4l_buffers.ready_to_be_freed) {
1241 v4l_fbuffer_free(file);
1245 if (fh->jpg_buffers.active != ZORAN_FREE) {
1246 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1247 zr->jpg_buffers.allocated = 0;
1248 zr->jpg_buffers.active = fh->jpg_buffers.active =
1253 if (fh->jpg_buffers.allocated ||
1254 fh->jpg_buffers.ready_to_be_freed) {
1255 jpg_fbuffer_free(file);
1260 * Open a zoran card. Right now the flags stuff is just playing
1264 zoran_open (struct inode *inode,
1267 unsigned int minor = iminor(inode);
1268 struct zoran *zr = NULL;
1269 struct zoran_fh *fh;
1270 int i, res, first_open = 0, have_module_locks = 0;
1272 /* find the device */
1273 for (i = 0; i < zoran_num; i++) {
1274 if (zoran[i].video_dev->minor == minor) {
1281 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1283 goto open_unlock_and_return;
1286 /* see fs/device.c - the kernel already locks during open(),
1287 * so locking ourselves only causes deadlocks */
1288 /*down(&zr->resource_lock);*/
1292 KERN_ERR "%s: no TV decoder loaded for device!\n",
1295 goto open_unlock_and_return;
1298 /* try to grab a module lock */
1299 if (!try_module_get(THIS_MODULE)) {
1302 "%s: failed to acquire my own lock! PANIC!\n",
1305 goto open_unlock_and_return;
1307 if (!try_module_get(zr->decoder->driver->owner)) {
1310 "%s: failed to grab ownership of i2c decoder\n",
1313 module_put(THIS_MODULE);
1314 goto open_unlock_and_return;
1317 !try_module_get(zr->encoder->driver->owner)) {
1320 "%s: failed to grab ownership of i2c encoder\n",
1323 module_put(zr->decoder->driver->owner);
1324 module_put(THIS_MODULE);
1325 goto open_unlock_and_return;
1328 have_module_locks = 1;
1330 if (zr->user >= 2048) {
1331 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1332 ZR_DEVNAME(zr), zr->user);
1334 goto open_unlock_and_return;
1337 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1338 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1340 /* now, create the open()-specific file_ops struct */
1341 fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1345 "%s: zoran_open() - allocation of zoran_fh failed\n",
1348 goto open_unlock_and_return;
1350 memset(fh, 0, sizeof(struct zoran_fh));
1351 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1352 * on norm-change! */
1354 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1355 if (!fh->overlay_mask) {
1358 "%s: zoran_open() - allocation of overlay_mask failed\n",
1362 goto open_unlock_and_return;
1365 if (zr->user++ == 0)
1368 /*up(&zr->resource_lock);*/
1370 /* default setup - TODO: look at flags */
1371 if (first_open) { /* First device open */
1372 zr36057_restart(zr);
1373 zoran_open_init_params(zr);
1374 zoran_init_hardware(zr);
1376 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1379 /* set file_ops stuff */
1380 file->private_data = fh;
1382 zoran_open_init_session(file);
1386 open_unlock_and_return:
1387 /* if we grabbed locks, release them accordingly */
1388 if (have_module_locks) {
1389 module_put(zr->decoder->driver->owner);
1391 module_put(zr->encoder->driver->owner);
1393 module_put(THIS_MODULE);
1396 /* if there's no device found, we didn't obtain the lock either */
1398 /*up(&zr->resource_lock);*/
1405 zoran_close (struct inode *inode,
1408 struct zoran_fh *fh = file->private_data;
1409 struct zoran *zr = fh->zr;
1411 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1412 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1414 /* kernel locks (fs/device.c), so don't do that ourselves
1415 * (prevents deadlocks) */
1416 /*down(&zr->resource_lock);*/
1418 zoran_close_end_session(file);
1420 if (zr->user-- == 1) { /* Last process */
1421 /* Clean up JPEG process */
1422 wake_up_interruptible(&zr->jpg_capq);
1423 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1424 zr->jpg_buffers.allocated = 0;
1425 zr->jpg_buffers.active = ZORAN_FREE;
1427 /* disable interrupts */
1428 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1431 print_interrupts(zr);
1434 zr->v4l_overlay_active = 0;
1435 zr36057_overlay(zr, 0);
1436 zr->overlay_mask = NULL;
1439 wake_up_interruptible(&zr->v4l_capq);
1440 zr36057_set_memgrab(zr, 0);
1441 zr->v4l_buffers.allocated = 0;
1442 zr->v4l_buffers.active = ZORAN_FREE;
1443 zoran_set_pci_master(zr, 0);
1445 if (!pass_through) { /* Switch to color bar */
1446 int zero = 0, two = 2;
1447 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1448 encoder_command(zr, ENCODER_SET_INPUT, &two);
1452 file->private_data = NULL;
1453 kfree(fh->overlay_mask);
1456 /* release locks on the i2c modules */
1457 module_put(zr->decoder->driver->owner);
1459 module_put(zr->encoder->driver->owner);
1461 module_put(THIS_MODULE);
1463 /*up(&zr->resource_lock);*/
1465 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1472 zoran_read (struct file *file,
1477 /* we simply don't support read() (yet)... */
1483 zoran_write (struct file *file,
1484 const char __user *data,
1488 /* ...and the same goes for write() */
1494 setup_fbuffer (struct file *file,
1496 const struct zoran_format *fmt,
1501 struct zoran_fh *fh = file->private_data;
1502 struct zoran *zr = fh->zr;
1504 /* (Ronald) v4l/v4l2 guidelines */
1505 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1508 /* we need a bytesperline value, even if not given */
1510 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1513 if (zr->overlay_active) {
1514 /* dzjee... stupid users... don't even bother to turn off
1515 * overlay before changing the memory location...
1516 * normally, we would return errors here. However, one of
1517 * the tools that does this is... xawtv! and since xawtv
1518 * is used by +/- 99% of the users, we'd rather be user-
1519 * friendly and silently do as if nothing went wrong */
1522 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1524 zr36057_overlay(zr, 0);
1528 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1531 "%s: setup_fbuffer() - no valid overlay format given\n",
1535 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1538 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1539 ZR_DEVNAME(zr), width, height, bytesperline);
1542 if (bytesperline & 3) {
1545 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1546 ZR_DEVNAME(zr), bytesperline);
1550 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1551 zr->buffer.height = height;
1552 zr->buffer.width = width;
1553 zr->buffer.depth = fmt->depth;
1554 zr->overlay_settings.format = fmt;
1555 zr->buffer.bytesperline = bytesperline;
1557 /* The user should set new window parameters */
1558 zr->overlay_settings.is_set = 0;
1565 setup_window (struct file *file,
1570 struct video_clip __user *clips,
1572 void __user *bitmap)
1574 struct zoran_fh *fh = file->private_data;
1575 struct zoran *zr = fh->zr;
1576 struct video_clip *vcp = NULL;
1580 if (!zr->buffer.base) {
1583 "%s: setup_window() - frame buffer has to be set first\n",
1588 if (!fh->overlay_settings.format) {
1591 "%s: setup_window() - no overlay format set\n",
1597 * The video front end needs 4-byte alinged line sizes, we correct that
1598 * silently here if necessary
1600 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1601 end = (x + width) & ~1; /* round down */
1602 x = (x + 1) & ~1; /* round up */
1606 if (zr->buffer.depth == 24) {
1607 end = (x + width) & ~3; /* round down */
1608 x = (x + 3) & ~3; /* round up */
1612 if (width > BUZ_MAX_WIDTH)
1613 width = BUZ_MAX_WIDTH;
1614 if (height > BUZ_MAX_HEIGHT)
1615 height = BUZ_MAX_HEIGHT;
1617 /* Check for vaild parameters */
1618 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1619 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1622 "%s: setup_window() - width = %d or height = %d invalid\n",
1623 ZR_DEVNAME(zr), width, height);
1627 fh->overlay_settings.x = x;
1628 fh->overlay_settings.y = y;
1629 fh->overlay_settings.width = width;
1630 fh->overlay_settings.height = height;
1631 fh->overlay_settings.clipcount = clipcount;
1634 * If an overlay is running, we have to switch it off
1635 * and switch it on again in order to get the new settings in effect.
1637 * We also want to avoid that the overlay mask is written
1638 * when an overlay is running.
1641 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1642 zr->overlay_active != ZORAN_FREE &&
1643 fh->overlay_active != ZORAN_FREE;
1645 zr36057_overlay(zr, 0);
1648 * Write the overlay mask if clips are wanted.
1649 * We prefer a bitmap.
1652 /* fake value - it just means we want clips */
1653 fh->overlay_settings.clipcount = 1;
1655 if (copy_from_user(fh->overlay_mask, bitmap,
1656 (width * height + 7) / 8)) {
1659 } else if (clipcount > 0) {
1660 /* write our own bitmap from the clips */
1661 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1665 "%s: setup_window() - Alloc of clip mask failed\n",
1670 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1674 write_overlay_mask(file, vcp, clipcount);
1678 fh->overlay_settings.is_set = 1;
1679 if (fh->overlay_active != ZORAN_FREE &&
1680 zr->overlay_active != ZORAN_FREE)
1681 zr->overlay_settings = fh->overlay_settings;
1684 zr36057_overlay(zr, 1);
1686 /* Make sure the changes come into effect */
1687 return wait_grab_pending(zr);
1691 setup_overlay (struct file *file,
1694 struct zoran_fh *fh = file->private_data;
1695 struct zoran *zr = fh->zr;
1697 /* If there is nothing to do, return immediatly */
1698 if ((on && fh->overlay_active != ZORAN_FREE) ||
1699 (!on && fh->overlay_active == ZORAN_FREE))
1702 /* check whether we're touching someone else's overlay */
1703 if (on && zr->overlay_active != ZORAN_FREE &&
1704 fh->overlay_active == ZORAN_FREE) {
1707 "%s: setup_overlay() - overlay is already active for another session\n",
1711 if (!on && zr->overlay_active != ZORAN_FREE &&
1712 fh->overlay_active == ZORAN_FREE) {
1715 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1721 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1722 zr->v4l_overlay_active = 0;
1723 /* When a grab is running, the video simply
1724 * won't be switched on any more */
1725 if (!zr->v4l_memgrab_active)
1726 zr36057_overlay(zr, 0);
1727 zr->overlay_mask = NULL;
1729 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1732 "%s: setup_overlay() - buffer or window not set\n",
1736 if (!fh->overlay_settings.format) {
1739 "%s: setup_overlay() - no overlay format set\n",
1743 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1744 zr->v4l_overlay_active = 1;
1745 zr->overlay_mask = fh->overlay_mask;
1746 zr->overlay_settings = fh->overlay_settings;
1747 if (!zr->v4l_memgrab_active)
1748 zr36057_overlay(zr, 1);
1749 /* When a grab is running, the video will be
1750 * switched on when grab is finished */
1753 /* Make sure the changes come into effect */
1754 return wait_grab_pending(zr);
1758 /* get the status of a buffer in the clients buffer queue */
1760 zoran_v4l2_buffer_status (struct file *file,
1761 struct v4l2_buffer *buf,
1764 struct zoran_fh *fh = file->private_data;
1765 struct zoran *zr = fh->zr;
1767 buf->flags = V4L2_BUF_FLAG_MAPPED;
1769 switch (fh->map_mode) {
1770 case ZORAN_MAP_MODE_RAW:
1773 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1774 !fh->v4l_buffers.allocated) {
1777 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1782 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1783 buf->length = fh->v4l_buffers.buffer_size;
1786 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1787 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1788 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1789 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1790 buf->flags |= V4L2_BUF_FLAG_DONE;
1792 fh->v4l_buffers.buffer[num].bs.timestamp;
1794 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1797 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1798 buf->field = V4L2_FIELD_TOP;
1800 buf->field = V4L2_FIELD_INTERLACED;
1804 case ZORAN_MAP_MODE_JPG_REC:
1805 case ZORAN_MAP_MODE_JPG_PLAY:
1808 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1809 !fh->jpg_buffers.allocated) {
1812 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1817 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1818 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1819 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1820 buf->length = fh->jpg_buffers.buffer_size;
1822 /* these variables are only written after frame has been captured */
1823 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1824 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1825 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1827 fh->jpg_buffers.buffer[num].bs.timestamp;
1829 fh->jpg_buffers.buffer[num].bs.length;
1830 buf->flags |= V4L2_BUF_FLAG_DONE;
1832 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1835 /* which fields are these? */
1836 if (fh->jpg_settings.TmpDcm != 1)
1839 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1843 odd_even ? V4L2_FIELD_SEQ_TB :
1852 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1853 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1857 buf->memory = V4L2_MEMORY_MMAP;
1859 buf->m.offset = buf->length * num;
1866 zoran_set_norm (struct zoran *zr,
1867 int norm) /* VIDEO_MODE_* */
1869 int norm_encoder, on;
1871 if (zr->v4l_buffers.active != ZORAN_FREE ||
1872 zr->jpg_buffers.active != ZORAN_FREE) {
1875 "%s: set_norm() called while in playback/capture mode\n",
1880 if (lock_norm && norm != zr->norm) {
1881 if (lock_norm > 1) {
1884 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1890 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1896 if (norm != VIDEO_MODE_AUTO &&
1897 (norm < 0 || norm >= zr->card.norms ||
1898 !zr->card.tvn[norm])) {
1900 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1901 ZR_DEVNAME(zr), norm);
1905 if (norm == VIDEO_MODE_AUTO) {
1908 /* if we have autodetect, ... */
1909 struct video_decoder_capability caps;
1910 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1911 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1912 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1917 decoder_command(zr, DECODER_SET_NORM, &norm);
1919 /* let changes come into effect */
1920 current->state = TASK_UNINTERRUPTIBLE;
1921 schedule_timeout(2 * HZ);
1923 decoder_command(zr, DECODER_GET_STATUS, &status);
1924 if (!(status & DECODER_STATUS_GOOD)) {
1927 "%s: set_norm() - no norm detected\n",
1930 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1934 if (status & DECODER_STATUS_NTSC)
1935 norm = VIDEO_MODE_NTSC;
1936 else if (status & DECODER_STATUS_SECAM)
1937 norm = VIDEO_MODE_SECAM;
1939 norm = VIDEO_MODE_PAL;
1941 zr->timing = zr->card.tvn[norm];
1942 norm_encoder = norm;
1944 /* We switch overlay off and on since a change in the
1945 * norm needs different VFE settings */
1946 on = zr->overlay_active && !zr->v4l_memgrab_active;
1948 zr36057_overlay(zr, 0);
1950 decoder_command(zr, DECODER_SET_NORM, &norm);
1951 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1954 zr36057_overlay(zr, 1);
1956 /* Make sure the changes come into effect */
1963 zoran_set_input (struct zoran *zr,
1968 if (input == zr->input) {
1972 if (zr->v4l_buffers.active != ZORAN_FREE ||
1973 zr->jpg_buffers.active != ZORAN_FREE) {
1976 "%s: set_input() called while in playback/capture mode\n",
1981 if (input < 0 || input >= zr->card.inputs) {
1984 "%s: set_input() - unnsupported input %d\n",
1985 ZR_DEVNAME(zr), input);
1989 realinput = zr->card.input[input].muxsel;
1992 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2002 zoran_do_ioctl (struct inode *inode,
2007 struct zoran_fh *fh = file->private_data;
2008 struct zoran *zr = fh->zr;
2010 /* we might have older buffers lying around... We don't want
2011 * to wait, but we do want to try cleaning them up ASAP. So
2012 * we try to obtain the lock and free them. If that fails, we
2013 * don't do anything and wait for the next turn. In the end,
2014 * zoran_close() or a new allocation will still free them...
2015 * This is just a 'the sooner the better' extra 'feature'
2017 * We don't free the buffers right on munmap() because that
2018 * causes oopses (kfree() inside munmap() oopses for no
2019 * apparent reason - it's also not reproduceable in any way,
2020 * but moving the free code outside the munmap() handler fixes
2021 * all this... If someone knows why, please explain me (Ronald)
2023 if (!down_trylock(&zr->resource_lock)) {
2024 /* we obtained it! Let's try to free some things */
2025 if (fh->jpg_buffers.ready_to_be_freed)
2026 jpg_fbuffer_free(file);
2027 if (fh->v4l_buffers.ready_to_be_freed)
2028 v4l_fbuffer_free(file);
2030 up(&zr->resource_lock);
2037 struct video_capability *vcap = arg;
2039 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2041 memset(vcap, 0, sizeof(struct video_capability));
2042 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
2043 vcap->type = ZORAN_VID_TYPE;
2045 vcap->channels = zr->card.inputs;
2047 down(&zr->resource_lock);
2048 vcap->maxwidth = BUZ_MAX_WIDTH;
2049 vcap->maxheight = BUZ_MAX_HEIGHT;
2050 vcap->minwidth = BUZ_MIN_WIDTH;
2051 vcap->minheight = BUZ_MIN_HEIGHT;
2052 up(&zr->resource_lock);
2060 struct video_channel *vchan = arg;
2061 int channel = vchan->channel;
2063 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2064 ZR_DEVNAME(zr), vchan->channel);
2066 memset(vchan, 0, sizeof(struct video_channel));
2067 if (channel > zr->card.inputs || channel < 0) {
2070 "%s: VIDIOCGCHAN on not existing channel %d\n",
2071 ZR_DEVNAME(zr), channel);
2075 strcpy(vchan->name, zr->card.input[channel].name);
2079 vchan->type = VIDEO_TYPE_CAMERA;
2080 down(&zr->resource_lock);
2081 vchan->norm = zr->norm;
2082 up(&zr->resource_lock);
2083 vchan->channel = channel;
2089 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2091 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2093 * * The famos BTTV driver has it implemented with a struct video_channel argument
2094 * * and we follow it for compatibility reasons
2096 * * BTW: this is the only way the user can set the norm!
2101 struct video_channel *vchan = arg;
2106 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2107 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2109 down(&zr->resource_lock);
2110 if ((res = zoran_set_input(zr, vchan->channel)))
2111 goto schan_unlock_and_return;
2112 if ((res = zoran_set_norm(zr, vchan->norm)))
2113 goto schan_unlock_and_return;
2115 /* Make sure the changes come into effect */
2116 res = wait_grab_pending(zr);
2117 schan_unlock_and_return:
2118 up(&zr->resource_lock);
2125 struct video_picture *vpict = arg;
2127 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2129 memset(vpict, 0, sizeof(struct video_picture));
2130 down(&zr->resource_lock);
2131 vpict->hue = zr->hue;
2132 vpict->brightness = zr->brightness;
2133 vpict->contrast = zr->contrast;
2134 vpict->colour = zr->saturation;
2135 if (fh->overlay_settings.format) {
2136 vpict->depth = fh->overlay_settings.format->depth;
2137 vpict->palette = fh->overlay_settings.format->palette;
2141 up(&zr->resource_lock);
2149 struct video_picture *vpict = arg;
2154 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2155 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2156 vpict->colour, vpict->contrast, vpict->depth,
2159 for (i = 0; i < zoran_num_formats; i++) {
2160 const struct zoran_format *fmt = &zoran_formats[i];
2162 if (fmt->palette != -1 &&
2163 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2164 fmt->palette == vpict->palette &&
2165 fmt->depth == vpict->depth)
2168 if (i == zoran_num_formats) {
2171 "%s: VIDIOCSPICT - Invalid palette %d\n",
2172 ZR_DEVNAME(zr), vpict->palette);
2176 down(&zr->resource_lock);
2178 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2180 zr->hue = vpict->hue;
2181 zr->contrast = vpict->contrast;
2182 zr->saturation = vpict->colour;
2183 zr->brightness = vpict->brightness;
2185 fh->overlay_settings.format = &zoran_formats[i];
2187 up(&zr->resource_lock);
2197 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2198 ZR_DEVNAME(zr), *on);
2200 down(&zr->resource_lock);
2201 res = setup_overlay(file, *on);
2202 up(&zr->resource_lock);
2210 struct video_window *vwin = arg;
2212 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2214 memset(vwin, 0, sizeof(struct video_window));
2215 down(&zr->resource_lock);
2216 vwin->x = fh->overlay_settings.x;
2217 vwin->y = fh->overlay_settings.y;
2218 vwin->width = fh->overlay_settings.width;
2219 vwin->height = fh->overlay_settings.height;
2220 up(&zr->resource_lock);
2221 vwin->clipcount = 0;
2228 struct video_window *vwin = arg;
2233 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2234 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2235 vwin->height, vwin->clipcount);
2237 down(&zr->resource_lock);
2239 setup_window(file, vwin->x, vwin->y, vwin->width,
2240 vwin->height, vwin->clips,
2241 vwin->clipcount, NULL);
2242 up(&zr->resource_lock);
2250 struct video_buffer *vbuf = arg;
2252 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2254 down(&zr->resource_lock);
2256 up(&zr->resource_lock);
2263 struct video_buffer *vbuf = arg;
2268 "%s: VIDIOCSFBUF - base=0x%x, w=%d, h=%d, depth=%d, bpl=%d\n",
2269 ZR_DEVNAME(zr), (u32) vbuf->base, vbuf->width,
2270 vbuf->height, vbuf->depth, vbuf->bytesperline);
2272 for (i = 0; i < zoran_num_formats; i++)
2273 if (zoran_formats[i].depth == vbuf->depth)
2275 if (i == zoran_num_formats) {
2278 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2279 ZR_DEVNAME(zr), vbuf->depth);
2283 down(&zr->resource_lock);
2285 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2286 vbuf->width, vbuf->height,
2287 vbuf->bytesperline);
2288 up(&zr->resource_lock);
2296 int *frame = arg, res;
2298 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2299 ZR_DEVNAME(zr), *frame);
2301 down(&zr->resource_lock);
2302 res = v4l_sync(file, *frame);
2303 up(&zr->resource_lock);
2305 zr->v4l_sync_tail++;
2310 case VIDIOCMCAPTURE:
2312 struct video_mmap *vmap = arg;
2317 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2318 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2321 down(&zr->resource_lock);
2322 res = v4l_grab(file, vmap);
2323 up(&zr->resource_lock);
2330 struct video_mbuf *vmbuf = arg;
2333 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2336 fh->v4l_buffers.num_buffers *
2337 fh->v4l_buffers.buffer_size;
2338 vmbuf->frames = fh->v4l_buffers.num_buffers;
2339 for (i = 0; i < vmbuf->frames; i++) {
2341 i * fh->v4l_buffers.buffer_size;
2344 down(&zr->resource_lock);
2346 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2349 "%s: VIDIOCGMBUF - buffers already allocated\n",
2352 goto v4l1reqbuf_unlock_and_return;
2355 if (v4l_fbuffer_alloc(file)) {
2357 goto v4l1reqbuf_unlock_and_return;
2360 /* The next mmap will map the V4L buffers */
2361 fh->map_mode = ZORAN_MAP_MODE_RAW;
2362 v4l1reqbuf_unlock_and_return:
2363 up(&zr->resource_lock);
2371 struct video_unit *vunit = arg;
2373 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2375 vunit->video = zr->video_dev->minor;
2376 vunit->vbi = VIDEO_NO_UNIT;
2377 vunit->radio = VIDEO_NO_UNIT;
2378 vunit->audio = VIDEO_NO_UNIT;
2379 vunit->teletext = VIDEO_NO_UNIT;
2386 * RJ: In principal we could support subcaptures for V4L grabbing.
2387 * Not even the famous BTTV driver has them, however.
2388 * If there should be a strong demand, one could consider
2389 * to implement them.
2391 case VIDIOCGCAPTURE:
2393 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2399 case VIDIOCSCAPTURE:
2401 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2407 case BUZIOC_G_PARAMS:
2409 struct zoran_params *bparams = arg;
2411 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2413 memset(bparams, 0, sizeof(struct zoran_params));
2414 bparams->major_version = MAJOR_VERSION;
2415 bparams->minor_version = MINOR_VERSION;
2417 down(&zr->resource_lock);
2419 bparams->norm = zr->norm;
2420 bparams->input = zr->input;
2422 bparams->decimation = fh->jpg_settings.decimation;
2423 bparams->HorDcm = fh->jpg_settings.HorDcm;
2424 bparams->VerDcm = fh->jpg_settings.VerDcm;
2425 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2426 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2427 bparams->img_x = fh->jpg_settings.img_x;
2428 bparams->img_y = fh->jpg_settings.img_y;
2429 bparams->img_width = fh->jpg_settings.img_width;
2430 bparams->img_height = fh->jpg_settings.img_height;
2431 bparams->odd_even = fh->jpg_settings.odd_even;
2433 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2434 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2435 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2436 memcpy(bparams->APP_data,
2437 fh->jpg_settings.jpg_comp.APP_data,
2438 sizeof(bparams->APP_data));
2439 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2440 memcpy(bparams->COM_data,
2441 fh->jpg_settings.jpg_comp.COM_data,
2442 sizeof(bparams->COM_data));
2443 bparams->jpeg_markers =
2444 fh->jpg_settings.jpg_comp.jpeg_markers;
2446 up(&zr->resource_lock);
2448 bparams->VFIFO_FB = 0;
2454 case BUZIOC_S_PARAMS:
2456 struct zoran_params *bparams = arg;
2457 struct zoran_jpg_settings settings;
2460 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2462 settings.decimation = bparams->decimation;
2463 settings.HorDcm = bparams->HorDcm;
2464 settings.VerDcm = bparams->VerDcm;
2465 settings.TmpDcm = bparams->TmpDcm;
2466 settings.field_per_buff = bparams->field_per_buff;
2467 settings.img_x = bparams->img_x;
2468 settings.img_y = bparams->img_y;
2469 settings.img_width = bparams->img_width;
2470 settings.img_height = bparams->img_height;
2471 settings.odd_even = bparams->odd_even;
2473 settings.jpg_comp.quality = bparams->quality;
2474 settings.jpg_comp.APPn = bparams->APPn;
2475 settings.jpg_comp.APP_len = bparams->APP_len;
2476 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2477 sizeof(bparams->APP_data));
2478 settings.jpg_comp.COM_len = bparams->COM_len;
2479 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2480 sizeof(bparams->COM_data));
2481 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2483 down(&zr->resource_lock);
2485 if (zr->codec_mode != BUZ_MODE_IDLE) {
2488 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2491 goto sparams_unlock_and_return;
2494 /* Check the params first before overwriting our
2496 if (zoran_check_jpg_settings(zr, &settings)) {
2498 goto sparams_unlock_and_return;
2501 fh->jpg_settings = settings;
2502 sparams_unlock_and_return:
2503 up(&zr->resource_lock);
2509 case BUZIOC_REQBUFS:
2511 struct zoran_requestbuffers *breq = arg;
2516 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2517 ZR_DEVNAME(zr), breq->count, breq->size);
2519 /* Enforce reasonable lower and upper limits */
2520 if (breq->count < 4)
2521 breq->count = 4; /* Could be choosen smaller */
2522 if (breq->count > jpg_nbufs)
2523 breq->count = jpg_nbufs;
2524 breq->size = PAGE_ALIGN(breq->size);
2525 if (breq->size < 8192)
2526 breq->size = 8192; /* Arbitrary */
2527 /* breq->size is limited by 1 page for the stat_com
2528 * tables to a Maximum of 2 MB */
2529 if (breq->size > jpg_bufsize)
2530 breq->size = jpg_bufsize;
2531 if (fh->jpg_buffers.need_contiguous &&
2532 breq->size > MAX_KMALLOC_MEM)
2533 breq->size = MAX_KMALLOC_MEM;
2535 down(&zr->resource_lock);
2537 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2540 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2543 goto jpgreqbuf_unlock_and_return;
2546 fh->jpg_buffers.num_buffers = breq->count;
2547 fh->jpg_buffers.buffer_size = breq->size;
2549 if (jpg_fbuffer_alloc(file)) {
2551 goto jpgreqbuf_unlock_and_return;
2554 /* The next mmap will map the MJPEG buffers - could
2555 * also be *_PLAY, but it doesn't matter here */
2556 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2557 jpgreqbuf_unlock_and_return:
2558 up(&zr->resource_lock);
2564 case BUZIOC_QBUF_CAPT:
2566 int *frame = arg, res;
2568 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2569 ZR_DEVNAME(zr), *frame);
2571 down(&zr->resource_lock);
2572 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2573 up(&zr->resource_lock);
2579 case BUZIOC_QBUF_PLAY:
2581 int *frame = arg, res;
2583 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2584 ZR_DEVNAME(zr), *frame);
2586 down(&zr->resource_lock);
2587 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2588 up(&zr->resource_lock);
2596 struct zoran_sync *bsync = arg;
2599 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2601 down(&zr->resource_lock);
2602 res = jpg_sync(file, bsync);
2603 up(&zr->resource_lock);
2609 case BUZIOC_G_STATUS:
2611 struct zoran_status *bstat = arg;
2612 int norm, input, status, res = 0;
2614 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2616 if (zr->codec_mode != BUZ_MODE_IDLE) {
2619 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2624 input = zr->card.input[bstat->input].muxsel;
2625 norm = VIDEO_MODE_AUTO;
2627 down(&zr->resource_lock);
2629 if (zr->codec_mode != BUZ_MODE_IDLE) {
2632 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2635 goto gstat_unlock_and_return;
2638 decoder_command(zr, DECODER_SET_INPUT, &input);
2639 decoder_command(zr, DECODER_SET_NORM, &norm);
2641 /* sleep 1 second */
2642 current->state = TASK_UNINTERRUPTIBLE;
2643 schedule_timeout(1 * HZ);
2645 /* Get status of video decoder */
2646 decoder_command(zr, DECODER_GET_STATUS, &status);
2648 /* restore previous input and norm */
2649 input = zr->card.input[zr->input].muxsel;
2650 decoder_command(zr, DECODER_SET_INPUT, &input);
2651 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2652 gstat_unlock_and_return:
2653 up(&zr->resource_lock);
2657 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2658 if (status & DECODER_STATUS_NTSC)
2659 bstat->norm = VIDEO_MODE_NTSC;
2660 else if (status & DECODER_STATUS_SECAM)
2661 bstat->norm = VIDEO_MODE_SECAM;
2663 bstat->norm = VIDEO_MODE_PAL;
2666 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2675 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2676 * it allows for integrating the JPEG capturing calls inside standard v4l2
2679 case VIDIOC_QUERYCAP:
2681 struct v4l2_capability *cap = arg;
2683 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2685 memset(cap, 0, sizeof(*cap));
2686 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
2687 strncpy(cap->driver, "zoran", sizeof(cap->driver));
2688 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2689 zr->pci_dev->slot_name);
2691 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2693 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2699 case VIDIOC_ENUM_FMT:
2701 struct v4l2_fmtdesc *fmt = arg;
2702 int index = fmt->index, num = -1, i, flag = 0, type =
2705 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2706 ZR_DEVNAME(zr), fmt->index);
2708 switch (fmt->type) {
2709 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2710 flag = ZORAN_FORMAT_CAPTURE;
2712 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2713 flag = ZORAN_FORMAT_PLAYBACK;
2715 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2716 flag = ZORAN_FORMAT_OVERLAY;
2721 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2722 ZR_DEVNAME(zr), fmt->type);
2726 for (i = 0; i < zoran_num_formats; i++) {
2727 if (zoran_formats[i].flags & flag)
2729 if (num == fmt->index)
2732 if (fmt->index < 0 /* late, but not too late */ ||
2733 i == zoran_num_formats)
2736 memset(fmt, 0, sizeof(*fmt));
2739 strncpy(fmt->description, zoran_formats[i].name, 31);
2740 fmt->pixelformat = zoran_formats[i].fourcc;
2741 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2742 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2750 struct v4l2_format *fmt = arg;
2751 int type = fmt->type;
2753 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2755 memset(fmt, 0, sizeof(*fmt));
2758 switch (fmt->type) {
2759 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2761 down(&zr->resource_lock);
2763 fmt->fmt.win.w.left = fh->overlay_settings.x;
2764 fmt->fmt.win.w.top = fh->overlay_settings.y;
2765 fmt->fmt.win.w.width = fh->overlay_settings.width;
2766 fmt->fmt.win.w.height =
2767 fh->overlay_settings.height;
2768 if (fh->overlay_settings.width * 2 >
2770 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2772 fmt->fmt.win.field = V4L2_FIELD_TOP;
2774 up(&zr->resource_lock);
2778 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2779 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2781 down(&zr->resource_lock);
2783 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2784 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2786 fmt->fmt.pix.width =
2787 fh->v4l_settings.width;
2788 fmt->fmt.pix.height =
2789 fh->v4l_settings.height;
2790 fmt->fmt.pix.sizeimage =
2791 fh->v4l_buffers.buffer_size;
2792 fmt->fmt.pix.pixelformat =
2793 fh->v4l_settings.format->fourcc;
2794 fmt->fmt.pix.colorspace =
2795 fh->v4l_settings.format->colorspace;
2796 fmt->fmt.pix.bytesperline = 0;
2797 if (BUZ_MAX_HEIGHT <
2798 (fh->v4l_settings.height * 2))
2799 fmt->fmt.pix.field =
2800 V4L2_FIELD_INTERLACED;
2802 fmt->fmt.pix.field =
2807 fmt->fmt.pix.width =
2808 fh->jpg_settings.img_width /
2809 fh->jpg_settings.HorDcm;
2810 fmt->fmt.pix.height =
2811 fh->jpg_settings.img_height /
2812 (fh->jpg_settings.VerDcm *
2813 fh->jpg_settings.TmpDcm);
2814 fmt->fmt.pix.sizeimage =
2815 zoran_v4l2_calc_bufsize(&fh->
2817 fmt->fmt.pix.pixelformat =
2819 if (fh->jpg_settings.TmpDcm == 1)
2820 fmt->fmt.pix.field =
2822 odd_even ? V4L2_FIELD_SEQ_BT :
2825 fmt->fmt.pix.field =
2827 odd_even ? V4L2_FIELD_TOP :
2830 fmt->fmt.pix.bytesperline = 0;
2831 fmt->fmt.pix.colorspace =
2832 V4L2_COLORSPACE_SMPTE170M;
2835 up(&zr->resource_lock);
2842 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2843 ZR_DEVNAME(zr), fmt->type);
2852 struct v4l2_format *fmt = arg;
2856 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2857 ZR_DEVNAME(zr), fmt->type);
2859 switch (fmt->type) {
2860 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2862 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2863 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2864 fmt->fmt.win.w.width,
2865 fmt->fmt.win.w.height,
2866 fmt->fmt.win.clipcount,
2867 fmt->fmt.win.bitmap);
2868 down(&zr->resource_lock);
2870 setup_window(file, fmt->fmt.win.w.left,
2872 fmt->fmt.win.w.width,
2873 fmt->fmt.win.w.height,
2874 (struct video_clip __user *)
2876 fmt->fmt.win.clipcount,
2877 fmt->fmt.win.bitmap);
2878 up(&zr->resource_lock);
2882 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2883 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2886 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2887 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2888 fmt->fmt.pix.width, fmt->fmt.pix.height,
2889 fmt->fmt.pix.pixelformat,
2890 (char *) &printformat);
2892 if (fmt->fmt.pix.bytesperline > 0) {
2894 KERN_ERR "%s: bpl not supported\n",
2899 /* we can be requested to do JPEG/raw playback/capture */
2901 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2902 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2903 fmt->fmt.pix.pixelformat ==
2904 V4L2_PIX_FMT_MJPEG))) {
2907 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2908 ZR_DEVNAME(zr), fmt->type,
2909 fmt->fmt.pix.pixelformat,
2910 (char *) &printformat);
2914 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2915 struct zoran_jpg_settings settings;
2917 down(&zr->resource_lock);
2919 settings = fh->jpg_settings;
2921 if (fh->v4l_buffers.allocated ||
2922 fh->jpg_buffers.allocated) {
2925 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2928 goto sfmtjpg_unlock_and_return;
2931 /* we actually need to set 'real' parameters now */
2932 if ((fmt->fmt.pix.height * 2) >
2934 settings.TmpDcm = 1;
2936 settings.TmpDcm = 2;
2937 settings.decimation = 0;
2938 if (fmt->fmt.pix.height <=
2939 fh->jpg_settings.img_height / 2)
2940 settings.VerDcm = 2;
2942 settings.VerDcm = 1;
2943 if (fmt->fmt.pix.width <=
2944 fh->jpg_settings.img_width / 4)
2945 settings.HorDcm = 4;
2946 else if (fmt->fmt.pix.width <=
2947 fh->jpg_settings.img_width / 2)
2948 settings.HorDcm = 2;
2950 settings.HorDcm = 1;
2951 if (settings.TmpDcm == 1)
2952 settings.field_per_buff = 2;
2954 settings.field_per_buff = 1;
2958 zoran_check_jpg_settings(zr,
2960 goto sfmtjpg_unlock_and_return;
2962 /* it's ok, so set them */
2963 fh->jpg_settings = settings;
2965 /* tell the user what we actually did */
2966 fmt->fmt.pix.width =
2967 settings.img_width / settings.HorDcm;
2968 fmt->fmt.pix.height =
2969 settings.img_height * 2 /
2970 (settings.TmpDcm * settings.VerDcm);
2971 if (settings.TmpDcm == 1)
2972 fmt->fmt.pix.field =
2974 odd_even ? V4L2_FIELD_SEQ_TB :
2977 fmt->fmt.pix.field =
2979 odd_even ? V4L2_FIELD_TOP :
2981 fh->jpg_buffers.buffer_size =
2982 zoran_v4l2_calc_bufsize(&fh->
2984 fmt->fmt.pix.sizeimage =
2985 fh->jpg_buffers.buffer_size;
2987 /* we hereby abuse this variable to show that
2988 * we're gonna do mjpeg capture */
2991 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2992 ZORAN_MAP_MODE_JPG_REC :
2993 ZORAN_MAP_MODE_JPG_PLAY;
2994 sfmtjpg_unlock_and_return:
2995 up(&zr->resource_lock);
2997 for (i = 0; i < zoran_num_formats; i++)
2998 if (fmt->fmt.pix.pixelformat ==
2999 zoran_formats[i].fourcc)
3001 if (i == zoran_num_formats) {
3004 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3006 fmt->fmt.pix.pixelformat,
3007 (char *) &printformat);
3010 down(&zr->resource_lock);
3011 if (fh->jpg_buffers.allocated ||
3012 (fh->v4l_buffers.allocated &&
3013 fh->v4l_buffers.active !=
3017 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3020 goto sfmtv4l_unlock_and_return;
3022 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3023 fmt->fmt.pix.height =
3025 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3026 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3029 zoran_v4l_set_format(file,
3036 goto sfmtv4l_unlock_and_return;
3038 /* tell the user the
3039 * results/missing stuff */
3040 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3042 if (BUZ_MAX_HEIGHT <
3043 (fh->v4l_settings.height * 2))
3044 fmt->fmt.pix.field =
3045 V4L2_FIELD_INTERLACED;
3047 fmt->fmt.pix.field =
3050 fh->map_mode = ZORAN_MAP_MODE_RAW;
3051 sfmtv4l_unlock_and_return:
3052 up(&zr->resource_lock);
3058 dprintk(3, "unsupported\n");
3061 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3062 ZR_DEVNAME(zr), fmt->type);
3072 struct v4l2_framebuffer *fb = arg;
3074 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3076 memset(fb, 0, sizeof(*fb));
3077 down(&zr->resource_lock);
3078 fb->base = zr->buffer.base;
3079 fb->fmt.width = zr->buffer.width;
3080 fb->fmt.height = zr->buffer.height;
3081 if (zr->overlay_settings.format) {
3082 fb->fmt.pixelformat =
3083 fh->overlay_settings.format->fourcc;
3085 fb->fmt.bytesperline = zr->buffer.bytesperline;
3086 up(&zr->resource_lock);
3087 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3088 fb->fmt.field = V4L2_FIELD_INTERLACED;
3089 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3090 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3099 struct v4l2_framebuffer *fb = arg;
3100 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3104 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3105 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3106 fb->fmt.bytesperline, fb->fmt.pixelformat,
3107 (char *) &printformat);
3109 for (i = 0; i < zoran_num_formats; i++)
3110 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3112 if (i == zoran_num_formats) {
3115 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3116 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3117 (char *) &printformat);
3121 down(&zr->resource_lock);
3123 setup_fbuffer(file, fb->base, &zoran_formats[i],
3124 fb->fmt.width, fb->fmt.height,
3125 fb->fmt.bytesperline);
3126 up(&zr->resource_lock);
3132 case VIDIOC_OVERLAY:
3136 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3137 ZR_DEVNAME(zr), *on);
3139 down(&zr->resource_lock);
3140 res = setup_overlay(file, *on);
3141 up(&zr->resource_lock);
3147 case VIDIOC_REQBUFS:
3149 struct v4l2_requestbuffers *req = arg;
3152 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3153 ZR_DEVNAME(zr), req->type);
3155 if (req->memory != V4L2_MEMORY_MMAP) {
3158 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3159 ZR_DEVNAME(zr), req->memory);
3163 down(&zr->resource_lock);
3165 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3168 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3171 goto v4l2reqbuf_unlock_and_return;
3174 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3175 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3177 /* control user input */
3180 if (req->count > v4l_nbufs)
3181 req->count = v4l_nbufs;
3182 fh->v4l_buffers.num_buffers = req->count;
3184 if (v4l_fbuffer_alloc(file)) {
3186 goto v4l2reqbuf_unlock_and_return;
3189 /* The next mmap will map the V4L buffers */
3190 fh->map_mode = ZORAN_MAP_MODE_RAW;
3192 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3193 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3195 /* we need to calculate size ourselves now */
3198 if (req->count > jpg_nbufs)
3199 req->count = jpg_nbufs;
3200 fh->jpg_buffers.num_buffers = req->count;
3201 fh->jpg_buffers.buffer_size =
3202 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3204 if (jpg_fbuffer_alloc(file)) {
3206 goto v4l2reqbuf_unlock_and_return;
3209 /* The next mmap will map the MJPEG buffers */
3210 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3211 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3213 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3218 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3219 ZR_DEVNAME(zr), req->type);
3221 goto v4l2reqbuf_unlock_and_return;
3223 v4l2reqbuf_unlock_and_return:
3224 up(&zr->resource_lock);
3230 case VIDIOC_QUERYBUF:
3232 struct v4l2_buffer *buf = arg;
3233 __u32 type = buf->type;
3234 int index = buf->index, res;
3238 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3239 ZR_DEVNAME(zr), buf->index, buf->type);
3241 memset(buf, 0, sizeof(buf));
3245 down(&zr->resource_lock);
3246 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3247 up(&zr->resource_lock);
3255 struct v4l2_buffer *buf = arg;
3256 int res = 0, codec_mode, buf_type;
3259 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3260 ZR_DEVNAME(zr), buf->type, buf->index);
3262 down(&zr->resource_lock);
3264 switch (fh->map_mode) {
3265 case ZORAN_MAP_MODE_RAW:
3266 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3269 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3270 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3272 goto qbuf_unlock_and_return;
3275 res = zoran_v4l_queue_frame(file, buf->index);
3277 goto qbuf_unlock_and_return;
3278 if (!zr->v4l_memgrab_active &&
3279 fh->v4l_buffers.active == ZORAN_LOCKED)
3280 zr36057_set_memgrab(zr, 1);
3283 case ZORAN_MAP_MODE_JPG_REC:
3284 case ZORAN_MAP_MODE_JPG_PLAY:
3285 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3286 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3287 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3289 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3290 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3293 if (buf->type != buf_type) {
3296 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3297 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3299 goto qbuf_unlock_and_return;
3303 zoran_jpg_queue_frame(file, buf->index,
3306 goto qbuf_unlock_and_return;
3307 if (zr->codec_mode == BUZ_MODE_IDLE &&
3308 fh->jpg_buffers.active == ZORAN_LOCKED) {
3309 zr36057_enable_jpg(zr, codec_mode);
3316 "%s: VIDIOC_QBUF - unsupported type %d\n",
3317 ZR_DEVNAME(zr), buf->type);
3319 goto qbuf_unlock_and_return;
3321 qbuf_unlock_and_return:
3322 up(&zr->resource_lock);
3330 struct v4l2_buffer *buf = arg;
3331 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3333 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3334 ZR_DEVNAME(zr), buf->type);
3336 down(&zr->resource_lock);
3338 switch (fh->map_mode) {
3339 case ZORAN_MAP_MODE_RAW:
3340 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3343 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3344 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3346 goto dqbuf_unlock_and_return;
3349 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3350 if (file->f_flags & O_NONBLOCK &&
3351 zr->v4l_buffers.buffer[num].state !=
3354 goto dqbuf_unlock_and_return;
3356 res = v4l_sync(file, num);
3358 goto dqbuf_unlock_and_return;
3360 zr->v4l_sync_tail++;
3361 res = zoran_v4l2_buffer_status(file, buf, num);
3364 case ZORAN_MAP_MODE_JPG_REC:
3365 case ZORAN_MAP_MODE_JPG_PLAY:
3367 struct zoran_sync bs;
3369 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3370 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3372 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3374 if (buf->type != buf_type) {
3377 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3378 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3380 goto dqbuf_unlock_and_return;
3385 jpg_que_tail & BUZ_MASK_FRAME];
3387 if (file->f_flags & O_NONBLOCK &&
3388 zr->jpg_buffers.buffer[num].state !=
3391 goto dqbuf_unlock_and_return;
3393 res = jpg_sync(file, &bs);
3395 goto dqbuf_unlock_and_return;
3397 zoran_v4l2_buffer_status(file, buf, bs.frame);
3404 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3405 ZR_DEVNAME(zr), buf->type);
3407 goto dqbuf_unlock_and_return;
3409 dqbuf_unlock_and_return:
3410 up(&zr->resource_lock);
3416 case VIDIOC_STREAMON:
3420 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3422 down(&zr->resource_lock);
3424 switch (fh->map_mode) {
3425 case ZORAN_MAP_MODE_RAW: /* raw capture */
3426 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3427 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3429 goto strmon_unlock_and_return;
3432 zr->v4l_buffers.active = fh->v4l_buffers.active =
3434 zr->v4l_settings = fh->v4l_settings;
3436 zr->v4l_sync_tail = zr->v4l_pend_tail;
3437 if (!zr->v4l_memgrab_active &&
3438 zr->v4l_pend_head != zr->v4l_pend_tail) {
3439 zr36057_set_memgrab(zr, 1);
3443 case ZORAN_MAP_MODE_JPG_REC:
3444 case ZORAN_MAP_MODE_JPG_PLAY:
3445 /* what is the codec mode right now? */
3446 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3447 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3449 goto strmon_unlock_and_return;
3452 zr->jpg_buffers.active = fh->jpg_buffers.active =
3455 if (zr->jpg_que_head != zr->jpg_que_tail) {
3456 /* Start the jpeg codec when the first frame is queued */
3464 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3465 ZR_DEVNAME(zr), fh->map_mode);
3467 goto strmon_unlock_and_return;
3469 strmon_unlock_and_return:
3470 up(&zr->resource_lock);
3476 case VIDIOC_STREAMOFF:
3480 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3482 down(&zr->resource_lock);
3484 switch (fh->map_mode) {
3485 case ZORAN_MAP_MODE_RAW: /* raw capture */
3486 if (fh->v4l_buffers.active == ZORAN_FREE &&
3487 zr->v4l_buffers.active != ZORAN_FREE) {
3488 res = -EPERM; /* stay off other's settings! */
3489 goto strmoff_unlock_and_return;
3491 if (zr->v4l_buffers.active == ZORAN_FREE)
3492 goto strmoff_unlock_and_return;
3494 /* unload capture */
3495 if (zr->v4l_memgrab_active)
3496 zr36057_set_memgrab(zr, 0);
3498 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3499 zr->v4l_buffers.buffer[i].state =
3501 fh->v4l_buffers = zr->v4l_buffers;
3503 zr->v4l_buffers.active = fh->v4l_buffers.active =
3506 zr->v4l_grab_seq = 0;
3507 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3508 zr->v4l_sync_tail = 0;
3512 case ZORAN_MAP_MODE_JPG_REC:
3513 case ZORAN_MAP_MODE_JPG_PLAY:
3514 if (fh->jpg_buffers.active == ZORAN_FREE &&
3515 zr->jpg_buffers.active != ZORAN_FREE) {
3516 res = -EPERM; /* stay off other's settings! */
3517 goto strmoff_unlock_and_return;
3519 if (zr->jpg_buffers.active == ZORAN_FREE)
3520 goto strmoff_unlock_and_return;
3525 ZORAN_MAP_MODE_JPG_REC) ?
3526 BUZ_MODE_MOTION_COMPRESS :
3527 BUZ_MODE_MOTION_DECOMPRESS);
3529 goto strmoff_unlock_and_return;
3534 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3535 ZR_DEVNAME(zr), fh->map_mode);
3537 goto strmoff_unlock_and_return;
3539 strmoff_unlock_and_return:
3540 up(&zr->resource_lock);
3546 case VIDIOC_QUERYCTRL:
3548 struct v4l2_queryctrl *ctrl = arg;
3550 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3551 ZR_DEVNAME(zr), ctrl->id);
3553 /* we only support hue/saturation/contrast/brightness */
3554 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3555 ctrl->id > V4L2_CID_HUE)
3559 memset(ctrl, 0, sizeof(*ctrl));
3564 case V4L2_CID_BRIGHTNESS:
3565 strncpy(ctrl->name, "Brightness", 31);
3567 case V4L2_CID_CONTRAST:
3568 strncpy(ctrl->name, "Contrast", 31);
3570 case V4L2_CID_SATURATION:
3571 strncpy(ctrl->name, "Saturation", 31);
3574 strncpy(ctrl->name, "Hue", 31);
3579 ctrl->maximum = 65535;
3581 ctrl->default_value = 32768;
3582 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3590 struct v4l2_control *ctrl = arg;
3592 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3593 ZR_DEVNAME(zr), ctrl->id);
3595 /* we only support hue/saturation/contrast/brightness */
3596 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3597 ctrl->id > V4L2_CID_HUE)
3600 down(&zr->resource_lock);
3602 case V4L2_CID_BRIGHTNESS:
3603 ctrl->value = zr->brightness;
3605 case V4L2_CID_CONTRAST:
3606 ctrl->value = zr->contrast;
3608 case V4L2_CID_SATURATION:
3609 ctrl->value = zr->saturation;
3612 ctrl->value = zr->hue;
3615 up(&zr->resource_lock);
3623 struct v4l2_control *ctrl = arg;
3624 struct video_picture pict;
3626 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3627 ZR_DEVNAME(zr), ctrl->id);
3629 /* we only support hue/saturation/contrast/brightness */
3630 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3631 ctrl->id > V4L2_CID_HUE)
3634 if (ctrl->value < 0 || ctrl->value > 65535) {
3637 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3638 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3642 down(&zr->resource_lock);
3644 case V4L2_CID_BRIGHTNESS:
3645 zr->brightness = ctrl->value;
3647 case V4L2_CID_CONTRAST:
3648 zr->contrast = ctrl->value;
3650 case V4L2_CID_SATURATION:
3651 zr->saturation = ctrl->value;
3654 zr->hue = ctrl->value;
3657 pict.brightness = zr->brightness;
3658 pict.contrast = zr->contrast;
3659 pict.colour = zr->saturation;
3662 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3664 up(&zr->resource_lock);
3670 case VIDIOC_ENUMSTD:
3672 struct v4l2_standard *std = arg;
3674 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3675 ZR_DEVNAME(zr), std->index);
3677 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3680 int id = std->index;
3681 memset(std, 0, sizeof(*std));
3685 if (std->index == zr->card.norms) {
3686 /* if we have autodetect, ... */
3687 struct video_decoder_capability caps;
3688 decoder_command(zr, DECODER_GET_CAPABILITIES,
3690 if (caps.flags & VIDEO_DECODER_AUTO) {
3691 std->id = V4L2_STD_ALL;
3692 strncpy(std->name, "Autodetect", 31);
3697 switch (std->index) {
3699 std->id = V4L2_STD_PAL;
3700 strncpy(std->name, "PAL", 31);
3701 std->frameperiod.numerator = 1;
3702 std->frameperiod.denominator = 25;
3703 std->framelines = zr->card.tvn[0]->Ht;
3706 std->id = V4L2_STD_NTSC;
3707 strncpy(std->name, "NTSC", 31);
3708 std->frameperiod.numerator = 1001;
3709 std->frameperiod.denominator = 30000;
3710 std->framelines = zr->card.tvn[1]->Ht;
3713 std->id = V4L2_STD_SECAM;
3714 strncpy(std->name, "SECAM", 31);
3715 std->frameperiod.numerator = 1;
3716 std->frameperiod.denominator = 25;
3717 std->framelines = zr->card.tvn[2]->Ht;
3727 v4l2_std_id *std = arg;
3730 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3732 down(&zr->resource_lock);
3734 up(&zr->resource_lock);
3737 case VIDEO_MODE_PAL:
3738 *std = V4L2_STD_PAL;
3740 case VIDEO_MODE_NTSC:
3741 *std = V4L2_STD_NTSC;
3743 case VIDEO_MODE_SECAM:
3744 *std = V4L2_STD_SECAM;
3754 int norm = -1, res = 0;
3755 v4l2_std_id *std = arg;
3757 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3758 ZR_DEVNAME(zr), *std);
3760 if (*std == V4L2_STD_PAL)
3761 norm = VIDEO_MODE_PAL;
3762 else if (*std == V4L2_STD_NTSC)
3763 norm = VIDEO_MODE_NTSC;
3764 else if (*std == V4L2_STD_SECAM)
3765 norm = VIDEO_MODE_SECAM;
3766 else if (*std == V4L2_STD_ALL)
3767 norm = VIDEO_MODE_AUTO;
3771 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3772 ZR_DEVNAME(zr), *std);
3776 down(&zr->resource_lock);
3777 if ((res = zoran_set_norm(zr, norm)))
3778 goto sstd_unlock_and_return;
3780 res = wait_grab_pending(zr);
3781 sstd_unlock_and_return:
3782 up(&zr->resource_lock);
3787 case VIDIOC_ENUMINPUT:
3789 struct v4l2_input *inp = arg;
3792 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3793 ZR_DEVNAME(zr), inp->index);
3795 if (inp->index < 0 || inp->index >= zr->card.inputs)
3798 int id = inp->index;
3799 memset(inp, 0, sizeof(*inp));
3803 strncpy(inp->name, zr->card.input[inp->index].name,
3804 sizeof(inp->name) - 1);
3805 inp->type = V4L2_INPUT_TYPE_CAMERA;
3806 inp->std = V4L2_STD_ALL;
3808 /* Get status of video decoder */
3809 down(&zr->resource_lock);
3810 decoder_command(zr, DECODER_GET_STATUS, &status);
3811 up(&zr->resource_lock);
3813 if (!(status & DECODER_STATUS_GOOD)) {
3814 inp->status |= V4L2_IN_ST_NO_POWER;
3815 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3817 if (!(status & DECODER_STATUS_COLOR))
3818 inp->status |= V4L2_IN_ST_NO_COLOR;
3824 case VIDIOC_G_INPUT:
3828 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3830 down(&zr->resource_lock);
3832 up(&zr->resource_lock);
3838 case VIDIOC_S_INPUT:
3840 int *input = arg, res = 0;
3842 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3843 ZR_DEVNAME(zr), *input);
3845 down(&zr->resource_lock);
3846 if ((res = zoran_set_input(zr, *input)))
3847 goto sinput_unlock_and_return;
3849 /* Make sure the changes come into effect */
3850 res = wait_grab_pending(zr);
3851 sinput_unlock_and_return:
3852 up(&zr->resource_lock);
3857 case VIDIOC_ENUMOUTPUT:
3859 struct v4l2_output *outp = arg;
3861 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3862 ZR_DEVNAME(zr), outp->index);
3864 if (outp->index != 0)
3867 memset(outp, 0, sizeof(*outp));
3869 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3870 strncpy(outp->name, "Autodetect", 31);
3876 case VIDIOC_G_OUTPUT:
3880 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3888 case VIDIOC_S_OUTPUT:
3892 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3893 ZR_DEVNAME(zr), *output);
3902 /* cropping (sub-frame capture) */
3903 case VIDIOC_CROPCAP:
3905 struct v4l2_cropcap *cropcap = arg;
3906 int type = cropcap->type, res = 0;
3908 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3909 ZR_DEVNAME(zr), cropcap->type);
3911 memset(cropcap, 0, sizeof(*cropcap));
3912 cropcap->type = type;
3914 down(&zr->resource_lock);
3916 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3917 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3918 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3921 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3924 goto cropcap_unlock_and_return;
3927 cropcap->bounds.top = cropcap->bounds.left = 0;
3928 cropcap->bounds.width = BUZ_MAX_WIDTH;
3929 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3930 cropcap->defrect.top = cropcap->defrect.left = 0;
3931 cropcap->defrect.width = BUZ_MIN_WIDTH;
3932 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3933 cropcap_unlock_and_return:
3934 up(&zr->resource_lock);
3941 struct v4l2_crop *crop = arg;
3942 int type = crop->type, res = 0;
3944 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3945 ZR_DEVNAME(zr), crop->type);
3947 memset(crop, 0, sizeof(*crop));
3950 down(&zr->resource_lock);
3952 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3953 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3954 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3957 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3960 goto gcrop_unlock_and_return;
3963 crop->c.top = fh->jpg_settings.img_y;
3964 crop->c.left = fh->jpg_settings.img_x;
3965 crop->c.width = fh->jpg_settings.img_width;
3966 crop->c.height = fh->jpg_settings.img_height;
3968 gcrop_unlock_and_return:
3969 up(&zr->resource_lock);
3977 struct v4l2_crop *crop = arg;
3979 struct zoran_jpg_settings settings = fh->jpg_settings;
3983 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3984 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3985 crop->c.width, crop->c.height);
3987 down(&zr->resource_lock);
3989 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3992 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3995 goto scrop_unlock_and_return;
3998 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3999 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4000 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4003 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4006 goto scrop_unlock_and_return;
4009 /* move into a form that we understand */
4010 settings.img_x = crop->c.left;
4011 settings.img_y = crop->c.top;
4012 settings.img_width = crop->c.width;
4013 settings.img_height = crop->c.height;
4015 /* check validity */
4016 if ((res = zoran_check_jpg_settings(zr, &settings)))
4017 goto scrop_unlock_and_return;
4020 fh->jpg_settings = settings;
4022 scrop_unlock_and_return:
4023 up(&zr->resource_lock);
4028 case VIDIOC_G_JPEGCOMP:
4030 struct v4l2_jpegcompression *params = arg;
4032 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4035 memset(params, 0, sizeof(*params));
4037 down(&zr->resource_lock);
4039 params->quality = fh->jpg_settings.jpg_comp.quality;
4040 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4041 memcpy(params->APP_data,
4042 fh->jpg_settings.jpg_comp.APP_data,
4043 fh->jpg_settings.jpg_comp.APP_len);
4044 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4045 memcpy(params->COM_data,
4046 fh->jpg_settings.jpg_comp.COM_data,
4047 fh->jpg_settings.jpg_comp.COM_len);
4048 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4049 params->jpeg_markers =
4050 fh->jpg_settings.jpg_comp.jpeg_markers;
4052 up(&zr->resource_lock);
4058 case VIDIOC_S_JPEGCOMP:
4060 struct v4l2_jpegcompression *params = arg;
4061 struct zoran_jpg_settings settings = fh->jpg_settings;
4066 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4067 ZR_DEVNAME(zr), params->quality, params->APPn,
4068 params->APP_len, params->COM_len);
4070 settings.jpg_comp = *params;
4072 down(&zr->resource_lock);
4074 if (fh->v4l_buffers.active != ZORAN_FREE ||
4075 fh->jpg_buffers.active != ZORAN_FREE) {
4078 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4081 goto sjpegc_unlock_and_return;
4084 if ((res = zoran_check_jpg_settings(zr, &settings)))
4085 goto sjpegc_unlock_and_return;
4086 if (!fh->jpg_buffers.allocated)
4087 fh->jpg_buffers.buffer_size =
4088 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4089 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4090 sjpegc_unlock_and_return:
4091 up(&zr->resource_lock);
4097 case VIDIOC_QUERYSTD: /* why is this useful? */
4099 v4l2_std_id *std = arg;
4102 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4103 ZR_DEVNAME(zr), *std);
4105 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4106 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4107 zr->card.norms == 3)) {
4115 case VIDIOC_TRY_FMT:
4117 struct v4l2_format *fmt = arg;
4120 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4121 ZR_DEVNAME(zr), fmt->type);
4123 switch (fmt->type) {
4124 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4125 down(&zr->resource_lock);
4127 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4128 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4129 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4130 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4131 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4132 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4133 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4134 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4136 up(&zr->resource_lock);
4139 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4140 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4141 if (fmt->fmt.pix.bytesperline > 0)
4144 down(&zr->resource_lock);
4146 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4147 struct zoran_jpg_settings settings =
4150 /* we actually need to set 'real' parameters now */
4151 if ((fmt->fmt.pix.height * 2) >
4153 settings.TmpDcm = 1;
4155 settings.TmpDcm = 2;
4156 settings.decimation = 0;
4157 if (fmt->fmt.pix.height <=
4158 fh->jpg_settings.img_height / 2)
4159 settings.VerDcm = 2;
4161 settings.VerDcm = 1;
4162 if (fmt->fmt.pix.width <=
4163 fh->jpg_settings.img_width / 4)
4164 settings.HorDcm = 4;
4165 else if (fmt->fmt.pix.width <=
4166 fh->jpg_settings.img_width / 2)
4167 settings.HorDcm = 2;
4169 settings.HorDcm = 1;
4170 if (settings.TmpDcm == 1)
4171 settings.field_per_buff = 2;
4173 settings.field_per_buff = 1;
4177 zoran_check_jpg_settings(zr,
4179 goto tryfmt_unlock_and_return;
4181 /* tell the user what we actually did */
4182 fmt->fmt.pix.width =
4183 settings.img_width / settings.HorDcm;
4184 fmt->fmt.pix.height =
4185 settings.img_height * 2 /
4186 (settings.TmpDcm * settings.VerDcm);
4187 if (settings.TmpDcm == 1)
4188 fmt->fmt.pix.field =
4190 odd_even ? V4L2_FIELD_SEQ_TB :
4193 fmt->fmt.pix.field =
4195 odd_even ? V4L2_FIELD_TOP :
4198 fmt->fmt.pix.sizeimage =
4199 zoran_v4l2_calc_bufsize(&settings);
4200 } else if (fmt->type ==
4201 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4204 for (i = 0; i < zoran_num_formats; i++)
4205 if (zoran_formats[i].fourcc ==
4206 fmt->fmt.pix.pixelformat)
4208 if (i == zoran_num_formats) {
4210 goto tryfmt_unlock_and_return;
4213 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4214 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4215 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4216 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4217 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4218 fmt->fmt.pix.height =
4220 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4221 fmt->fmt.pix.height =
4225 goto tryfmt_unlock_and_return;
4227 tryfmt_unlock_and_return:
4228 up(&zr->resource_lock);
4243 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4244 ZR_DEVNAME(zr), cmd);
4245 return -ENOIOCTLCMD;
4254 zoran_ioctl (struct inode *inode,
4259 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4263 zoran_poll (struct file *file,
4266 struct zoran_fh *fh = file->private_data;
4267 struct zoran *zr = fh->zr;
4268 wait_queue_head_t *queue = NULL;
4271 /* we should check whether buffers are ready to be synced on
4272 * (w/o waits - O_NONBLOCK) here
4273 * if ready for read (sync), return POLLIN|POLLRDNORM,
4274 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4275 * if error, return POLLERR,
4276 * if no buffers queued or so, return POLLNVAL
4279 down(&zr->resource_lock);
4281 switch (fh->map_mode) {
4282 case ZORAN_MAP_MODE_RAW:
4283 if (fh->v4l_buffers.active == ZORAN_FREE ||
4284 zr->v4l_pend_head == zr->v4l_pend_tail) {
4286 "%s: zoran_poll() - no buffers queued\n",
4289 goto poll_unlock_and_return;
4291 queue = &zr->v4l_capq;
4292 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4293 poll_wait(file, queue, wait);
4294 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4295 res = POLLIN | POLLRDNORM;
4298 case ZORAN_MAP_MODE_JPG_REC:
4299 case ZORAN_MAP_MODE_JPG_PLAY:
4300 if (fh->jpg_buffers.active == ZORAN_FREE ||
4301 zr->jpg_que_head == zr->jpg_que_tail) {
4303 "%s: zoran_poll() - no buffers queued\n",
4306 goto poll_unlock_and_return;
4308 queue = &zr->jpg_capq;
4309 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4310 poll_wait(file, queue, wait);
4311 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4312 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4313 res = POLLIN | POLLRDNORM;
4315 res = POLLOUT | POLLWRNORM;
4321 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4322 ZR_DEVNAME(zr), fh->map_mode);
4324 goto poll_unlock_and_return;
4327 poll_unlock_and_return:
4328 up(&zr->resource_lock);
4335 * This maps the buffers to user space.
4337 * Depending on the state of fh->map_mode
4338 * the V4L or the MJPEG buffers are mapped
4339 * per buffer or all together
4341 * Note that we need to connect to some
4342 * unmap signal event to unmap the de-allocate
4343 * the buffer accordingly (zoran_vm_close())
4347 zoran_vm_open (struct vm_area_struct *vma)
4349 struct zoran_mapping *map = vma->vm_private_data;
4355 zoran_vm_close (struct vm_area_struct *vma)
4357 struct zoran_mapping *map = vma->vm_private_data;
4358 struct file *file = map->file;
4359 struct zoran_fh *fh = file->private_data;
4360 struct zoran *zr = fh->zr;
4364 if (map->count == 0) {
4365 switch (fh->map_mode) {
4366 case ZORAN_MAP_MODE_JPG_REC:
4367 case ZORAN_MAP_MODE_JPG_PLAY:
4369 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4372 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4373 if (fh->jpg_buffers.buffer[i].map == map) {
4374 fh->jpg_buffers.buffer[i].map =
4380 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4381 if (fh->jpg_buffers.buffer[i].map)
4383 if (i == fh->jpg_buffers.num_buffers) {
4384 down(&zr->resource_lock);
4386 if (fh->jpg_buffers.active != ZORAN_FREE) {
4387 jpg_qbuf(file, -1, zr->codec_mode);
4388 zr->jpg_buffers.allocated = 0;
4389 zr->jpg_buffers.active =
4390 fh->jpg_buffers.active =
4393 //jpg_fbuffer_free(file);
4394 fh->jpg_buffers.allocated = 0;
4395 fh->jpg_buffers.ready_to_be_freed = 1;
4397 up(&zr->resource_lock);
4402 case ZORAN_MAP_MODE_RAW:
4404 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4407 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4408 if (fh->v4l_buffers.buffer[i].map == map) {
4410 fh->v4l_buffers.buffer[i].map =
4416 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4417 if (fh->v4l_buffers.buffer[i].map)
4419 if (i == fh->v4l_buffers.num_buffers) {
4420 down(&zr->resource_lock);
4422 if (fh->v4l_buffers.active != ZORAN_FREE) {
4423 zr36057_set_memgrab(zr, 0);
4424 zr->v4l_buffers.allocated = 0;
4425 zr->v4l_buffers.active =
4426 fh->v4l_buffers.active =
4429 //v4l_fbuffer_free(file);
4430 fh->v4l_buffers.allocated = 0;
4431 fh->v4l_buffers.ready_to_be_freed = 1;
4433 up(&zr->resource_lock);
4440 "%s: munmap() - internal error - unknown map mode %d\n",
4441 ZR_DEVNAME(zr), fh->map_mode);
4448 static struct vm_operations_struct zoran_vm_ops = {
4449 .open = zoran_vm_open,
4450 .close = zoran_vm_close,
4453 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4454 #define zr_remap_page_range(a,b,c,d,e) remap_page_range(b,c,d,e)
4456 #define zr_remap_page_range(a,b,c,d,e) remap_page_range(a,b,c,d,e)
4460 zoran_mmap (struct file *file,
4461 struct vm_area_struct *vma)
4463 struct zoran_fh *fh = file->private_data;
4464 struct zoran *zr = fh->zr;
4465 unsigned long size = (vma->vm_end - vma->vm_start);
4466 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4468 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4470 struct zoran_mapping *map;
4474 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4476 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4477 vma->vm_start, vma->vm_end, size);
4479 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4480 !(vma->vm_flags & VM_WRITE)) {
4483 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4488 switch (fh->map_mode) {
4490 case ZORAN_MAP_MODE_JPG_REC:
4491 case ZORAN_MAP_MODE_JPG_PLAY:
4494 down(&zr->resource_lock);
4496 /* Map the MJPEG buffers */
4497 if (!fh->jpg_buffers.allocated) {
4500 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4503 goto jpg_mmap_unlock_and_return;
4506 first = offset / fh->jpg_buffers.buffer_size;
4507 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4508 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4509 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4510 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4511 last >= fh->jpg_buffers.num_buffers) {
4514 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4515 ZR_DEVNAME(zr), offset, size,
4516 fh->jpg_buffers.buffer_size,
4517 fh->jpg_buffers.num_buffers);
4519 goto jpg_mmap_unlock_and_return;
4521 for (i = first; i <= last; i++) {
4522 if (fh->jpg_buffers.buffer[i].map) {
4525 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4528 goto jpg_mmap_unlock_and_return;
4532 /* map these buffers (v4l_buffers[i]) */
4533 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4536 goto jpg_mmap_unlock_and_return;
4541 vma->vm_ops = &zoran_vm_ops;
4542 vma->vm_flags |= VM_DONTEXPAND;
4543 vma->vm_private_data = map;
4545 for (i = first; i <= last; i++) {
4547 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4550 (fh->jpg_buffers.buffer[i].
4551 frag_tab[2 * j + 1] & ~1) << 1;
4556 (unsigned long) fh->jpg_buffers.
4557 buffer[i].frag_tab[2 * j];
4558 page = virt_to_phys(bus_to_virt(pos)); /* should just be pos on i386 */
4559 if (zr_remap_page_range
4560 (vma, start, page, todo, PAGE_SHARED)) {
4563 "%s: zoran_mmap(V4L) - remap_page_range failed\n",
4566 goto jpg_mmap_unlock_and_return;
4572 if (fh->jpg_buffers.buffer[i].
4573 frag_tab[2 * j + 1] & 1)
4574 break; /* was last fragment */
4576 fh->jpg_buffers.buffer[i].map = map;
4581 jpg_mmap_unlock_and_return:
4582 up(&zr->resource_lock);
4586 case ZORAN_MAP_MODE_RAW:
4588 down(&zr->resource_lock);
4590 /* Map the V4L buffers */
4591 if (!fh->v4l_buffers.allocated) {
4594 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4597 goto v4l_mmap_unlock_and_return;
4600 first = offset / fh->v4l_buffers.buffer_size;
4601 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4602 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4603 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4604 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4605 last >= fh->v4l_buffers.buffer_size) {
4608 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4609 ZR_DEVNAME(zr), offset, size,
4610 fh->v4l_buffers.buffer_size,
4611 fh->v4l_buffers.num_buffers);
4613 goto v4l_mmap_unlock_and_return;
4615 for (i = first; i <= last; i++) {
4616 if (fh->v4l_buffers.buffer[i].map) {
4619 "%s: mmap(V4L) - buffer %d already mapped\n",
4622 goto v4l_mmap_unlock_and_return;
4626 /* map these buffers (v4l_buffers[i]) */
4627 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4630 goto v4l_mmap_unlock_and_return;
4635 vma->vm_ops = &zoran_vm_ops;
4636 vma->vm_flags |= VM_DONTEXPAND;
4637 vma->vm_private_data = map;
4639 for (i = first; i <= last; i++) {
4641 if (todo > fh->v4l_buffers.buffer_size)
4642 todo = fh->v4l_buffers.buffer_size;
4643 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4644 if (zr_remap_page_range
4645 (vma, start, page, todo, PAGE_SHARED)) {
4648 "%s: zoran_mmap(V4L)i - remap_page_range failed\n",
4651 goto v4l_mmap_unlock_and_return;
4655 fh->v4l_buffers.buffer[i].map = map;
4659 v4l_mmap_unlock_and_return:
4660 up(&zr->resource_lock);
4667 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4668 ZR_DEVNAME(zr), fh->map_mode);
4675 static struct file_operations zoran_fops = {
4676 .owner = THIS_MODULE,
4678 .release = zoran_close,
4679 .ioctl = zoran_ioctl,
4680 .llseek = no_llseek,
4682 .write = zoran_write,
4687 struct video_device zoran_template __devinitdata = {
4689 .type = ZORAN_VID_TYPE,
4691 .type2 = ZORAN_V4L2_VID_FLAGS,
4693 .hardware = ZORAN_HARDWARE,
4694 .fops = &zoran_fops,
4695 .release = &zoran_vdev_release,