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 __iomem *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 */
289 if (readb(mem + i) != c)
293 if (readb(mem + i) != c)
295 writeb(0, mem + i); /* zero out memory */
297 /* give the kernel air to breath */
298 if ((i & 0x3ffff) == 0x3ffff)
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 */
1922 decoder_command(zr, DECODER_GET_STATUS, &status);
1923 if (!(status & DECODER_STATUS_GOOD)) {
1926 "%s: set_norm() - no norm detected\n",
1929 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1933 if (status & DECODER_STATUS_NTSC)
1934 norm = VIDEO_MODE_NTSC;
1935 else if (status & DECODER_STATUS_SECAM)
1936 norm = VIDEO_MODE_SECAM;
1938 norm = VIDEO_MODE_PAL;
1940 zr->timing = zr->card.tvn[norm];
1941 norm_encoder = norm;
1943 /* We switch overlay off and on since a change in the
1944 * norm needs different VFE settings */
1945 on = zr->overlay_active && !zr->v4l_memgrab_active;
1947 zr36057_overlay(zr, 0);
1949 decoder_command(zr, DECODER_SET_NORM, &norm);
1950 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1953 zr36057_overlay(zr, 1);
1955 /* Make sure the changes come into effect */
1962 zoran_set_input (struct zoran *zr,
1967 if (input == zr->input) {
1971 if (zr->v4l_buffers.active != ZORAN_FREE ||
1972 zr->jpg_buffers.active != ZORAN_FREE) {
1975 "%s: set_input() called while in playback/capture mode\n",
1980 if (input < 0 || input >= zr->card.inputs) {
1983 "%s: set_input() - unnsupported input %d\n",
1984 ZR_DEVNAME(zr), input);
1988 realinput = zr->card.input[input].muxsel;
1991 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2001 zoran_do_ioctl (struct inode *inode,
2006 struct zoran_fh *fh = file->private_data;
2007 struct zoran *zr = fh->zr;
2009 /* we might have older buffers lying around... We don't want
2010 * to wait, but we do want to try cleaning them up ASAP. So
2011 * we try to obtain the lock and free them. If that fails, we
2012 * don't do anything and wait for the next turn. In the end,
2013 * zoran_close() or a new allocation will still free them...
2014 * This is just a 'the sooner the better' extra 'feature'
2016 * We don't free the buffers right on munmap() because that
2017 * causes oopses (kfree() inside munmap() oopses for no
2018 * apparent reason - it's also not reproduceable in any way,
2019 * but moving the free code outside the munmap() handler fixes
2020 * all this... If someone knows why, please explain me (Ronald)
2022 if (!down_trylock(&zr->resource_lock)) {
2023 /* we obtained it! Let's try to free some things */
2024 if (fh->jpg_buffers.ready_to_be_freed)
2025 jpg_fbuffer_free(file);
2026 if (fh->v4l_buffers.ready_to_be_freed)
2027 v4l_fbuffer_free(file);
2029 up(&zr->resource_lock);
2036 struct video_capability *vcap = arg;
2038 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2040 memset(vcap, 0, sizeof(struct video_capability));
2041 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
2042 vcap->type = ZORAN_VID_TYPE;
2044 vcap->channels = zr->card.inputs;
2046 down(&zr->resource_lock);
2047 vcap->maxwidth = BUZ_MAX_WIDTH;
2048 vcap->maxheight = BUZ_MAX_HEIGHT;
2049 vcap->minwidth = BUZ_MIN_WIDTH;
2050 vcap->minheight = BUZ_MIN_HEIGHT;
2051 up(&zr->resource_lock);
2059 struct video_channel *vchan = arg;
2060 int channel = vchan->channel;
2062 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2063 ZR_DEVNAME(zr), vchan->channel);
2065 memset(vchan, 0, sizeof(struct video_channel));
2066 if (channel > zr->card.inputs || channel < 0) {
2069 "%s: VIDIOCGCHAN on not existing channel %d\n",
2070 ZR_DEVNAME(zr), channel);
2074 strcpy(vchan->name, zr->card.input[channel].name);
2078 vchan->type = VIDEO_TYPE_CAMERA;
2079 down(&zr->resource_lock);
2080 vchan->norm = zr->norm;
2081 up(&zr->resource_lock);
2082 vchan->channel = channel;
2088 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2090 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2092 * * The famos BTTV driver has it implemented with a struct video_channel argument
2093 * * and we follow it for compatibility reasons
2095 * * BTW: this is the only way the user can set the norm!
2100 struct video_channel *vchan = arg;
2105 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2106 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2108 down(&zr->resource_lock);
2109 if ((res = zoran_set_input(zr, vchan->channel)))
2110 goto schan_unlock_and_return;
2111 if ((res = zoran_set_norm(zr, vchan->norm)))
2112 goto schan_unlock_and_return;
2114 /* Make sure the changes come into effect */
2115 res = wait_grab_pending(zr);
2116 schan_unlock_and_return:
2117 up(&zr->resource_lock);
2124 struct video_picture *vpict = arg;
2126 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2128 memset(vpict, 0, sizeof(struct video_picture));
2129 down(&zr->resource_lock);
2130 vpict->hue = zr->hue;
2131 vpict->brightness = zr->brightness;
2132 vpict->contrast = zr->contrast;
2133 vpict->colour = zr->saturation;
2134 if (fh->overlay_settings.format) {
2135 vpict->depth = fh->overlay_settings.format->depth;
2136 vpict->palette = fh->overlay_settings.format->palette;
2140 up(&zr->resource_lock);
2148 struct video_picture *vpict = arg;
2153 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2154 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2155 vpict->colour, vpict->contrast, vpict->depth,
2158 for (i = 0; i < zoran_num_formats; i++) {
2159 const struct zoran_format *fmt = &zoran_formats[i];
2161 if (fmt->palette != -1 &&
2162 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2163 fmt->palette == vpict->palette &&
2164 fmt->depth == vpict->depth)
2167 if (i == zoran_num_formats) {
2170 "%s: VIDIOCSPICT - Invalid palette %d\n",
2171 ZR_DEVNAME(zr), vpict->palette);
2175 down(&zr->resource_lock);
2177 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2179 zr->hue = vpict->hue;
2180 zr->contrast = vpict->contrast;
2181 zr->saturation = vpict->colour;
2182 zr->brightness = vpict->brightness;
2184 fh->overlay_settings.format = &zoran_formats[i];
2186 up(&zr->resource_lock);
2196 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2197 ZR_DEVNAME(zr), *on);
2199 down(&zr->resource_lock);
2200 res = setup_overlay(file, *on);
2201 up(&zr->resource_lock);
2209 struct video_window *vwin = arg;
2211 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2213 memset(vwin, 0, sizeof(struct video_window));
2214 down(&zr->resource_lock);
2215 vwin->x = fh->overlay_settings.x;
2216 vwin->y = fh->overlay_settings.y;
2217 vwin->width = fh->overlay_settings.width;
2218 vwin->height = fh->overlay_settings.height;
2219 up(&zr->resource_lock);
2220 vwin->clipcount = 0;
2227 struct video_window *vwin = arg;
2232 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2233 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2234 vwin->height, vwin->clipcount);
2236 down(&zr->resource_lock);
2238 setup_window(file, vwin->x, vwin->y, vwin->width,
2239 vwin->height, vwin->clips,
2240 vwin->clipcount, NULL);
2241 up(&zr->resource_lock);
2249 struct video_buffer *vbuf = arg;
2251 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2253 down(&zr->resource_lock);
2255 up(&zr->resource_lock);
2262 struct video_buffer *vbuf = arg;
2267 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2268 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2269 vbuf->height, vbuf->depth, vbuf->bytesperline);
2271 for (i = 0; i < zoran_num_formats; i++)
2272 if (zoran_formats[i].depth == vbuf->depth)
2274 if (i == zoran_num_formats) {
2277 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2278 ZR_DEVNAME(zr), vbuf->depth);
2282 down(&zr->resource_lock);
2284 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2285 vbuf->width, vbuf->height,
2286 vbuf->bytesperline);
2287 up(&zr->resource_lock);
2295 int *frame = arg, res;
2297 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2298 ZR_DEVNAME(zr), *frame);
2300 down(&zr->resource_lock);
2301 res = v4l_sync(file, *frame);
2302 up(&zr->resource_lock);
2304 zr->v4l_sync_tail++;
2309 case VIDIOCMCAPTURE:
2311 struct video_mmap *vmap = arg;
2316 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2317 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2320 down(&zr->resource_lock);
2321 res = v4l_grab(file, vmap);
2322 up(&zr->resource_lock);
2329 struct video_mbuf *vmbuf = arg;
2332 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2335 fh->v4l_buffers.num_buffers *
2336 fh->v4l_buffers.buffer_size;
2337 vmbuf->frames = fh->v4l_buffers.num_buffers;
2338 for (i = 0; i < vmbuf->frames; i++) {
2340 i * fh->v4l_buffers.buffer_size;
2343 down(&zr->resource_lock);
2345 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2348 "%s: VIDIOCGMBUF - buffers already allocated\n",
2351 goto v4l1reqbuf_unlock_and_return;
2354 if (v4l_fbuffer_alloc(file)) {
2356 goto v4l1reqbuf_unlock_and_return;
2359 /* The next mmap will map the V4L buffers */
2360 fh->map_mode = ZORAN_MAP_MODE_RAW;
2361 v4l1reqbuf_unlock_and_return:
2362 up(&zr->resource_lock);
2370 struct video_unit *vunit = arg;
2372 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2374 vunit->video = zr->video_dev->minor;
2375 vunit->vbi = VIDEO_NO_UNIT;
2376 vunit->radio = VIDEO_NO_UNIT;
2377 vunit->audio = VIDEO_NO_UNIT;
2378 vunit->teletext = VIDEO_NO_UNIT;
2385 * RJ: In principal we could support subcaptures for V4L grabbing.
2386 * Not even the famous BTTV driver has them, however.
2387 * If there should be a strong demand, one could consider
2388 * to implement them.
2390 case VIDIOCGCAPTURE:
2392 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2398 case VIDIOCSCAPTURE:
2400 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2406 case BUZIOC_G_PARAMS:
2408 struct zoran_params *bparams = arg;
2410 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2412 memset(bparams, 0, sizeof(struct zoran_params));
2413 bparams->major_version = MAJOR_VERSION;
2414 bparams->minor_version = MINOR_VERSION;
2416 down(&zr->resource_lock);
2418 bparams->norm = zr->norm;
2419 bparams->input = zr->input;
2421 bparams->decimation = fh->jpg_settings.decimation;
2422 bparams->HorDcm = fh->jpg_settings.HorDcm;
2423 bparams->VerDcm = fh->jpg_settings.VerDcm;
2424 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2425 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2426 bparams->img_x = fh->jpg_settings.img_x;
2427 bparams->img_y = fh->jpg_settings.img_y;
2428 bparams->img_width = fh->jpg_settings.img_width;
2429 bparams->img_height = fh->jpg_settings.img_height;
2430 bparams->odd_even = fh->jpg_settings.odd_even;
2432 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2433 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2434 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2435 memcpy(bparams->APP_data,
2436 fh->jpg_settings.jpg_comp.APP_data,
2437 sizeof(bparams->APP_data));
2438 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2439 memcpy(bparams->COM_data,
2440 fh->jpg_settings.jpg_comp.COM_data,
2441 sizeof(bparams->COM_data));
2442 bparams->jpeg_markers =
2443 fh->jpg_settings.jpg_comp.jpeg_markers;
2445 up(&zr->resource_lock);
2447 bparams->VFIFO_FB = 0;
2453 case BUZIOC_S_PARAMS:
2455 struct zoran_params *bparams = arg;
2456 struct zoran_jpg_settings settings;
2459 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2461 settings.decimation = bparams->decimation;
2462 settings.HorDcm = bparams->HorDcm;
2463 settings.VerDcm = bparams->VerDcm;
2464 settings.TmpDcm = bparams->TmpDcm;
2465 settings.field_per_buff = bparams->field_per_buff;
2466 settings.img_x = bparams->img_x;
2467 settings.img_y = bparams->img_y;
2468 settings.img_width = bparams->img_width;
2469 settings.img_height = bparams->img_height;
2470 settings.odd_even = bparams->odd_even;
2472 settings.jpg_comp.quality = bparams->quality;
2473 settings.jpg_comp.APPn = bparams->APPn;
2474 settings.jpg_comp.APP_len = bparams->APP_len;
2475 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2476 sizeof(bparams->APP_data));
2477 settings.jpg_comp.COM_len = bparams->COM_len;
2478 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2479 sizeof(bparams->COM_data));
2480 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2482 down(&zr->resource_lock);
2484 if (zr->codec_mode != BUZ_MODE_IDLE) {
2487 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2490 goto sparams_unlock_and_return;
2493 /* Check the params first before overwriting our
2495 if (zoran_check_jpg_settings(zr, &settings)) {
2497 goto sparams_unlock_and_return;
2500 fh->jpg_settings = settings;
2501 sparams_unlock_and_return:
2502 up(&zr->resource_lock);
2508 case BUZIOC_REQBUFS:
2510 struct zoran_requestbuffers *breq = arg;
2515 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2516 ZR_DEVNAME(zr), breq->count, breq->size);
2518 /* Enforce reasonable lower and upper limits */
2519 if (breq->count < 4)
2520 breq->count = 4; /* Could be choosen smaller */
2521 if (breq->count > jpg_nbufs)
2522 breq->count = jpg_nbufs;
2523 breq->size = PAGE_ALIGN(breq->size);
2524 if (breq->size < 8192)
2525 breq->size = 8192; /* Arbitrary */
2526 /* breq->size is limited by 1 page for the stat_com
2527 * tables to a Maximum of 2 MB */
2528 if (breq->size > jpg_bufsize)
2529 breq->size = jpg_bufsize;
2530 if (fh->jpg_buffers.need_contiguous &&
2531 breq->size > MAX_KMALLOC_MEM)
2532 breq->size = MAX_KMALLOC_MEM;
2534 down(&zr->resource_lock);
2536 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2539 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2542 goto jpgreqbuf_unlock_and_return;
2545 fh->jpg_buffers.num_buffers = breq->count;
2546 fh->jpg_buffers.buffer_size = breq->size;
2548 if (jpg_fbuffer_alloc(file)) {
2550 goto jpgreqbuf_unlock_and_return;
2553 /* The next mmap will map the MJPEG buffers - could
2554 * also be *_PLAY, but it doesn't matter here */
2555 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2556 jpgreqbuf_unlock_and_return:
2557 up(&zr->resource_lock);
2563 case BUZIOC_QBUF_CAPT:
2565 int *frame = arg, res;
2567 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2568 ZR_DEVNAME(zr), *frame);
2570 down(&zr->resource_lock);
2571 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2572 up(&zr->resource_lock);
2578 case BUZIOC_QBUF_PLAY:
2580 int *frame = arg, res;
2582 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2583 ZR_DEVNAME(zr), *frame);
2585 down(&zr->resource_lock);
2586 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2587 up(&zr->resource_lock);
2595 struct zoran_sync *bsync = arg;
2598 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2600 down(&zr->resource_lock);
2601 res = jpg_sync(file, bsync);
2602 up(&zr->resource_lock);
2608 case BUZIOC_G_STATUS:
2610 struct zoran_status *bstat = arg;
2611 int norm, input, status, res = 0;
2613 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2615 if (zr->codec_mode != BUZ_MODE_IDLE) {
2618 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2623 input = zr->card.input[bstat->input].muxsel;
2624 norm = VIDEO_MODE_AUTO;
2626 down(&zr->resource_lock);
2628 if (zr->codec_mode != BUZ_MODE_IDLE) {
2631 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2634 goto gstat_unlock_and_return;
2637 decoder_command(zr, DECODER_SET_INPUT, &input);
2638 decoder_command(zr, DECODER_SET_NORM, &norm);
2640 /* sleep 1 second */
2643 /* Get status of video decoder */
2644 decoder_command(zr, DECODER_GET_STATUS, &status);
2646 /* restore previous input and norm */
2647 input = zr->card.input[zr->input].muxsel;
2648 decoder_command(zr, DECODER_SET_INPUT, &input);
2649 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2650 gstat_unlock_and_return:
2651 up(&zr->resource_lock);
2655 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2656 if (status & DECODER_STATUS_NTSC)
2657 bstat->norm = VIDEO_MODE_NTSC;
2658 else if (status & DECODER_STATUS_SECAM)
2659 bstat->norm = VIDEO_MODE_SECAM;
2661 bstat->norm = VIDEO_MODE_PAL;
2664 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2673 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2674 * it allows for integrating the JPEG capturing calls inside standard v4l2
2677 case VIDIOC_QUERYCAP:
2679 struct v4l2_capability *cap = arg;
2681 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2683 memset(cap, 0, sizeof(*cap));
2684 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
2685 strncpy(cap->driver, "zoran", sizeof(cap->driver));
2686 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2687 zr->pci_dev->slot_name);
2689 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2691 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2697 case VIDIOC_ENUM_FMT:
2699 struct v4l2_fmtdesc *fmt = arg;
2700 int index = fmt->index, num = -1, i, flag = 0, type =
2703 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2704 ZR_DEVNAME(zr), fmt->index);
2706 switch (fmt->type) {
2707 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2708 flag = ZORAN_FORMAT_CAPTURE;
2710 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2711 flag = ZORAN_FORMAT_PLAYBACK;
2713 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2714 flag = ZORAN_FORMAT_OVERLAY;
2719 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2720 ZR_DEVNAME(zr), fmt->type);
2724 for (i = 0; i < zoran_num_formats; i++) {
2725 if (zoran_formats[i].flags & flag)
2727 if (num == fmt->index)
2730 if (fmt->index < 0 /* late, but not too late */ ||
2731 i == zoran_num_formats)
2734 memset(fmt, 0, sizeof(*fmt));
2737 strncpy(fmt->description, zoran_formats[i].name, 31);
2738 fmt->pixelformat = zoran_formats[i].fourcc;
2739 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2740 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2748 struct v4l2_format *fmt = arg;
2749 int type = fmt->type;
2751 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2753 memset(fmt, 0, sizeof(*fmt));
2756 switch (fmt->type) {
2757 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2759 down(&zr->resource_lock);
2761 fmt->fmt.win.w.left = fh->overlay_settings.x;
2762 fmt->fmt.win.w.top = fh->overlay_settings.y;
2763 fmt->fmt.win.w.width = fh->overlay_settings.width;
2764 fmt->fmt.win.w.height =
2765 fh->overlay_settings.height;
2766 if (fh->overlay_settings.width * 2 >
2768 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2770 fmt->fmt.win.field = V4L2_FIELD_TOP;
2772 up(&zr->resource_lock);
2776 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2777 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2779 down(&zr->resource_lock);
2781 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2782 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2784 fmt->fmt.pix.width =
2785 fh->v4l_settings.width;
2786 fmt->fmt.pix.height =
2787 fh->v4l_settings.height;
2788 fmt->fmt.pix.sizeimage =
2789 fh->v4l_buffers.buffer_size;
2790 fmt->fmt.pix.pixelformat =
2791 fh->v4l_settings.format->fourcc;
2792 fmt->fmt.pix.colorspace =
2793 fh->v4l_settings.format->colorspace;
2794 fmt->fmt.pix.bytesperline = 0;
2795 if (BUZ_MAX_HEIGHT <
2796 (fh->v4l_settings.height * 2))
2797 fmt->fmt.pix.field =
2798 V4L2_FIELD_INTERLACED;
2800 fmt->fmt.pix.field =
2805 fmt->fmt.pix.width =
2806 fh->jpg_settings.img_width /
2807 fh->jpg_settings.HorDcm;
2808 fmt->fmt.pix.height =
2809 fh->jpg_settings.img_height /
2810 (fh->jpg_settings.VerDcm *
2811 fh->jpg_settings.TmpDcm);
2812 fmt->fmt.pix.sizeimage =
2813 zoran_v4l2_calc_bufsize(&fh->
2815 fmt->fmt.pix.pixelformat =
2817 if (fh->jpg_settings.TmpDcm == 1)
2818 fmt->fmt.pix.field =
2820 odd_even ? V4L2_FIELD_SEQ_BT :
2823 fmt->fmt.pix.field =
2825 odd_even ? V4L2_FIELD_TOP :
2828 fmt->fmt.pix.bytesperline = 0;
2829 fmt->fmt.pix.colorspace =
2830 V4L2_COLORSPACE_SMPTE170M;
2833 up(&zr->resource_lock);
2840 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2841 ZR_DEVNAME(zr), fmt->type);
2850 struct v4l2_format *fmt = arg;
2854 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2855 ZR_DEVNAME(zr), fmt->type);
2857 switch (fmt->type) {
2858 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2860 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2861 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2862 fmt->fmt.win.w.width,
2863 fmt->fmt.win.w.height,
2864 fmt->fmt.win.clipcount,
2865 fmt->fmt.win.bitmap);
2866 down(&zr->resource_lock);
2868 setup_window(file, fmt->fmt.win.w.left,
2870 fmt->fmt.win.w.width,
2871 fmt->fmt.win.w.height,
2872 (struct video_clip __user *)
2874 fmt->fmt.win.clipcount,
2875 fmt->fmt.win.bitmap);
2876 up(&zr->resource_lock);
2880 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2881 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2884 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2885 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2886 fmt->fmt.pix.width, fmt->fmt.pix.height,
2887 fmt->fmt.pix.pixelformat,
2888 (char *) &printformat);
2890 if (fmt->fmt.pix.bytesperline > 0) {
2892 KERN_ERR "%s: bpl not supported\n",
2897 /* we can be requested to do JPEG/raw playback/capture */
2899 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2900 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2901 fmt->fmt.pix.pixelformat ==
2902 V4L2_PIX_FMT_MJPEG))) {
2905 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2906 ZR_DEVNAME(zr), fmt->type,
2907 fmt->fmt.pix.pixelformat,
2908 (char *) &printformat);
2912 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2913 struct zoran_jpg_settings settings;
2915 down(&zr->resource_lock);
2917 settings = fh->jpg_settings;
2919 if (fh->v4l_buffers.allocated ||
2920 fh->jpg_buffers.allocated) {
2923 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2926 goto sfmtjpg_unlock_and_return;
2929 /* we actually need to set 'real' parameters now */
2930 if ((fmt->fmt.pix.height * 2) >
2932 settings.TmpDcm = 1;
2934 settings.TmpDcm = 2;
2935 settings.decimation = 0;
2936 if (fmt->fmt.pix.height <=
2937 fh->jpg_settings.img_height / 2)
2938 settings.VerDcm = 2;
2940 settings.VerDcm = 1;
2941 if (fmt->fmt.pix.width <=
2942 fh->jpg_settings.img_width / 4)
2943 settings.HorDcm = 4;
2944 else if (fmt->fmt.pix.width <=
2945 fh->jpg_settings.img_width / 2)
2946 settings.HorDcm = 2;
2948 settings.HorDcm = 1;
2949 if (settings.TmpDcm == 1)
2950 settings.field_per_buff = 2;
2952 settings.field_per_buff = 1;
2956 zoran_check_jpg_settings(zr,
2958 goto sfmtjpg_unlock_and_return;
2960 /* it's ok, so set them */
2961 fh->jpg_settings = settings;
2963 /* tell the user what we actually did */
2964 fmt->fmt.pix.width =
2965 settings.img_width / settings.HorDcm;
2966 fmt->fmt.pix.height =
2967 settings.img_height * 2 /
2968 (settings.TmpDcm * settings.VerDcm);
2969 if (settings.TmpDcm == 1)
2970 fmt->fmt.pix.field =
2972 odd_even ? V4L2_FIELD_SEQ_TB :
2975 fmt->fmt.pix.field =
2977 odd_even ? V4L2_FIELD_TOP :
2979 fh->jpg_buffers.buffer_size =
2980 zoran_v4l2_calc_bufsize(&fh->
2982 fmt->fmt.pix.sizeimage =
2983 fh->jpg_buffers.buffer_size;
2985 /* we hereby abuse this variable to show that
2986 * we're gonna do mjpeg capture */
2989 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2990 ZORAN_MAP_MODE_JPG_REC :
2991 ZORAN_MAP_MODE_JPG_PLAY;
2992 sfmtjpg_unlock_and_return:
2993 up(&zr->resource_lock);
2995 for (i = 0; i < zoran_num_formats; i++)
2996 if (fmt->fmt.pix.pixelformat ==
2997 zoran_formats[i].fourcc)
2999 if (i == zoran_num_formats) {
3002 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3004 fmt->fmt.pix.pixelformat,
3005 (char *) &printformat);
3008 down(&zr->resource_lock);
3009 if (fh->jpg_buffers.allocated ||
3010 (fh->v4l_buffers.allocated &&
3011 fh->v4l_buffers.active !=
3015 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3018 goto sfmtv4l_unlock_and_return;
3020 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3021 fmt->fmt.pix.height =
3023 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3024 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3027 zoran_v4l_set_format(file,
3034 goto sfmtv4l_unlock_and_return;
3036 /* tell the user the
3037 * results/missing stuff */
3038 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3040 if (BUZ_MAX_HEIGHT <
3041 (fh->v4l_settings.height * 2))
3042 fmt->fmt.pix.field =
3043 V4L2_FIELD_INTERLACED;
3045 fmt->fmt.pix.field =
3048 fh->map_mode = ZORAN_MAP_MODE_RAW;
3049 sfmtv4l_unlock_and_return:
3050 up(&zr->resource_lock);
3056 dprintk(3, "unsupported\n");
3059 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3060 ZR_DEVNAME(zr), fmt->type);
3070 struct v4l2_framebuffer *fb = arg;
3072 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3074 memset(fb, 0, sizeof(*fb));
3075 down(&zr->resource_lock);
3076 fb->base = zr->buffer.base;
3077 fb->fmt.width = zr->buffer.width;
3078 fb->fmt.height = zr->buffer.height;
3079 if (zr->overlay_settings.format) {
3080 fb->fmt.pixelformat =
3081 fh->overlay_settings.format->fourcc;
3083 fb->fmt.bytesperline = zr->buffer.bytesperline;
3084 up(&zr->resource_lock);
3085 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3086 fb->fmt.field = V4L2_FIELD_INTERLACED;
3087 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3088 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3097 struct v4l2_framebuffer *fb = arg;
3098 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3102 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3103 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3104 fb->fmt.bytesperline, fb->fmt.pixelformat,
3105 (char *) &printformat);
3107 for (i = 0; i < zoran_num_formats; i++)
3108 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3110 if (i == zoran_num_formats) {
3113 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3114 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3115 (char *) &printformat);
3119 down(&zr->resource_lock);
3121 setup_fbuffer(file, fb->base, &zoran_formats[i],
3122 fb->fmt.width, fb->fmt.height,
3123 fb->fmt.bytesperline);
3124 up(&zr->resource_lock);
3130 case VIDIOC_OVERLAY:
3134 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3135 ZR_DEVNAME(zr), *on);
3137 down(&zr->resource_lock);
3138 res = setup_overlay(file, *on);
3139 up(&zr->resource_lock);
3145 case VIDIOC_REQBUFS:
3147 struct v4l2_requestbuffers *req = arg;
3150 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3151 ZR_DEVNAME(zr), req->type);
3153 if (req->memory != V4L2_MEMORY_MMAP) {
3156 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3157 ZR_DEVNAME(zr), req->memory);
3161 down(&zr->resource_lock);
3163 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3166 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3169 goto v4l2reqbuf_unlock_and_return;
3172 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3173 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3175 /* control user input */
3178 if (req->count > v4l_nbufs)
3179 req->count = v4l_nbufs;
3180 fh->v4l_buffers.num_buffers = req->count;
3182 if (v4l_fbuffer_alloc(file)) {
3184 goto v4l2reqbuf_unlock_and_return;
3187 /* The next mmap will map the V4L buffers */
3188 fh->map_mode = ZORAN_MAP_MODE_RAW;
3190 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3191 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3193 /* we need to calculate size ourselves now */
3196 if (req->count > jpg_nbufs)
3197 req->count = jpg_nbufs;
3198 fh->jpg_buffers.num_buffers = req->count;
3199 fh->jpg_buffers.buffer_size =
3200 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3202 if (jpg_fbuffer_alloc(file)) {
3204 goto v4l2reqbuf_unlock_and_return;
3207 /* The next mmap will map the MJPEG buffers */
3208 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3209 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3211 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3216 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3217 ZR_DEVNAME(zr), req->type);
3219 goto v4l2reqbuf_unlock_and_return;
3221 v4l2reqbuf_unlock_and_return:
3222 up(&zr->resource_lock);
3228 case VIDIOC_QUERYBUF:
3230 struct v4l2_buffer *buf = arg;
3231 __u32 type = buf->type;
3232 int index = buf->index, res;
3236 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3237 ZR_DEVNAME(zr), buf->index, buf->type);
3239 memset(buf, 0, sizeof(buf));
3243 down(&zr->resource_lock);
3244 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3245 up(&zr->resource_lock);
3253 struct v4l2_buffer *buf = arg;
3254 int res = 0, codec_mode, buf_type;
3257 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3258 ZR_DEVNAME(zr), buf->type, buf->index);
3260 down(&zr->resource_lock);
3262 switch (fh->map_mode) {
3263 case ZORAN_MAP_MODE_RAW:
3264 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3267 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3268 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3270 goto qbuf_unlock_and_return;
3273 res = zoran_v4l_queue_frame(file, buf->index);
3275 goto qbuf_unlock_and_return;
3276 if (!zr->v4l_memgrab_active &&
3277 fh->v4l_buffers.active == ZORAN_LOCKED)
3278 zr36057_set_memgrab(zr, 1);
3281 case ZORAN_MAP_MODE_JPG_REC:
3282 case ZORAN_MAP_MODE_JPG_PLAY:
3283 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3284 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3285 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3287 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3288 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3291 if (buf->type != buf_type) {
3294 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3295 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3297 goto qbuf_unlock_and_return;
3301 zoran_jpg_queue_frame(file, buf->index,
3304 goto qbuf_unlock_and_return;
3305 if (zr->codec_mode == BUZ_MODE_IDLE &&
3306 fh->jpg_buffers.active == ZORAN_LOCKED) {
3307 zr36057_enable_jpg(zr, codec_mode);
3314 "%s: VIDIOC_QBUF - unsupported type %d\n",
3315 ZR_DEVNAME(zr), buf->type);
3317 goto qbuf_unlock_and_return;
3319 qbuf_unlock_and_return:
3320 up(&zr->resource_lock);
3328 struct v4l2_buffer *buf = arg;
3329 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3331 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3332 ZR_DEVNAME(zr), buf->type);
3334 down(&zr->resource_lock);
3336 switch (fh->map_mode) {
3337 case ZORAN_MAP_MODE_RAW:
3338 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3341 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3342 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3344 goto dqbuf_unlock_and_return;
3347 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3348 if (file->f_flags & O_NONBLOCK &&
3349 zr->v4l_buffers.buffer[num].state !=
3352 goto dqbuf_unlock_and_return;
3354 res = v4l_sync(file, num);
3356 goto dqbuf_unlock_and_return;
3358 zr->v4l_sync_tail++;
3359 res = zoran_v4l2_buffer_status(file, buf, num);
3362 case ZORAN_MAP_MODE_JPG_REC:
3363 case ZORAN_MAP_MODE_JPG_PLAY:
3365 struct zoran_sync bs;
3367 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3368 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3370 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3372 if (buf->type != buf_type) {
3375 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3376 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3378 goto dqbuf_unlock_and_return;
3383 jpg_que_tail & BUZ_MASK_FRAME];
3385 if (file->f_flags & O_NONBLOCK &&
3386 zr->jpg_buffers.buffer[num].state !=
3389 goto dqbuf_unlock_and_return;
3391 res = jpg_sync(file, &bs);
3393 goto dqbuf_unlock_and_return;
3395 zoran_v4l2_buffer_status(file, buf, bs.frame);
3402 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3403 ZR_DEVNAME(zr), buf->type);
3405 goto dqbuf_unlock_and_return;
3407 dqbuf_unlock_and_return:
3408 up(&zr->resource_lock);
3414 case VIDIOC_STREAMON:
3418 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3420 down(&zr->resource_lock);
3422 switch (fh->map_mode) {
3423 case ZORAN_MAP_MODE_RAW: /* raw capture */
3424 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3425 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3427 goto strmon_unlock_and_return;
3430 zr->v4l_buffers.active = fh->v4l_buffers.active =
3432 zr->v4l_settings = fh->v4l_settings;
3434 zr->v4l_sync_tail = zr->v4l_pend_tail;
3435 if (!zr->v4l_memgrab_active &&
3436 zr->v4l_pend_head != zr->v4l_pend_tail) {
3437 zr36057_set_memgrab(zr, 1);
3441 case ZORAN_MAP_MODE_JPG_REC:
3442 case ZORAN_MAP_MODE_JPG_PLAY:
3443 /* what is the codec mode right now? */
3444 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3445 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3447 goto strmon_unlock_and_return;
3450 zr->jpg_buffers.active = fh->jpg_buffers.active =
3453 if (zr->jpg_que_head != zr->jpg_que_tail) {
3454 /* Start the jpeg codec when the first frame is queued */
3462 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3463 ZR_DEVNAME(zr), fh->map_mode);
3465 goto strmon_unlock_and_return;
3467 strmon_unlock_and_return:
3468 up(&zr->resource_lock);
3474 case VIDIOC_STREAMOFF:
3478 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3480 down(&zr->resource_lock);
3482 switch (fh->map_mode) {
3483 case ZORAN_MAP_MODE_RAW: /* raw capture */
3484 if (fh->v4l_buffers.active == ZORAN_FREE &&
3485 zr->v4l_buffers.active != ZORAN_FREE) {
3486 res = -EPERM; /* stay off other's settings! */
3487 goto strmoff_unlock_and_return;
3489 if (zr->v4l_buffers.active == ZORAN_FREE)
3490 goto strmoff_unlock_and_return;
3492 /* unload capture */
3493 if (zr->v4l_memgrab_active)
3494 zr36057_set_memgrab(zr, 0);
3496 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3497 zr->v4l_buffers.buffer[i].state =
3499 fh->v4l_buffers = zr->v4l_buffers;
3501 zr->v4l_buffers.active = fh->v4l_buffers.active =
3504 zr->v4l_grab_seq = 0;
3505 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3506 zr->v4l_sync_tail = 0;
3510 case ZORAN_MAP_MODE_JPG_REC:
3511 case ZORAN_MAP_MODE_JPG_PLAY:
3512 if (fh->jpg_buffers.active == ZORAN_FREE &&
3513 zr->jpg_buffers.active != ZORAN_FREE) {
3514 res = -EPERM; /* stay off other's settings! */
3515 goto strmoff_unlock_and_return;
3517 if (zr->jpg_buffers.active == ZORAN_FREE)
3518 goto strmoff_unlock_and_return;
3523 ZORAN_MAP_MODE_JPG_REC) ?
3524 BUZ_MODE_MOTION_COMPRESS :
3525 BUZ_MODE_MOTION_DECOMPRESS);
3527 goto strmoff_unlock_and_return;
3532 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3533 ZR_DEVNAME(zr), fh->map_mode);
3535 goto strmoff_unlock_and_return;
3537 strmoff_unlock_and_return:
3538 up(&zr->resource_lock);
3544 case VIDIOC_QUERYCTRL:
3546 struct v4l2_queryctrl *ctrl = arg;
3548 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3549 ZR_DEVNAME(zr), ctrl->id);
3551 /* we only support hue/saturation/contrast/brightness */
3552 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3553 ctrl->id > V4L2_CID_HUE)
3557 memset(ctrl, 0, sizeof(*ctrl));
3562 case V4L2_CID_BRIGHTNESS:
3563 strncpy(ctrl->name, "Brightness", 31);
3565 case V4L2_CID_CONTRAST:
3566 strncpy(ctrl->name, "Contrast", 31);
3568 case V4L2_CID_SATURATION:
3569 strncpy(ctrl->name, "Saturation", 31);
3572 strncpy(ctrl->name, "Hue", 31);
3577 ctrl->maximum = 65535;
3579 ctrl->default_value = 32768;
3580 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3588 struct v4l2_control *ctrl = arg;
3590 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3591 ZR_DEVNAME(zr), ctrl->id);
3593 /* we only support hue/saturation/contrast/brightness */
3594 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3595 ctrl->id > V4L2_CID_HUE)
3598 down(&zr->resource_lock);
3600 case V4L2_CID_BRIGHTNESS:
3601 ctrl->value = zr->brightness;
3603 case V4L2_CID_CONTRAST:
3604 ctrl->value = zr->contrast;
3606 case V4L2_CID_SATURATION:
3607 ctrl->value = zr->saturation;
3610 ctrl->value = zr->hue;
3613 up(&zr->resource_lock);
3621 struct v4l2_control *ctrl = arg;
3622 struct video_picture pict;
3624 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3625 ZR_DEVNAME(zr), ctrl->id);
3627 /* we only support hue/saturation/contrast/brightness */
3628 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3629 ctrl->id > V4L2_CID_HUE)
3632 if (ctrl->value < 0 || ctrl->value > 65535) {
3635 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3636 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3640 down(&zr->resource_lock);
3642 case V4L2_CID_BRIGHTNESS:
3643 zr->brightness = ctrl->value;
3645 case V4L2_CID_CONTRAST:
3646 zr->contrast = ctrl->value;
3648 case V4L2_CID_SATURATION:
3649 zr->saturation = ctrl->value;
3652 zr->hue = ctrl->value;
3655 pict.brightness = zr->brightness;
3656 pict.contrast = zr->contrast;
3657 pict.colour = zr->saturation;
3660 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3662 up(&zr->resource_lock);
3668 case VIDIOC_ENUMSTD:
3670 struct v4l2_standard *std = arg;
3672 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3673 ZR_DEVNAME(zr), std->index);
3675 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3678 int id = std->index;
3679 memset(std, 0, sizeof(*std));
3683 if (std->index == zr->card.norms) {
3684 /* if we have autodetect, ... */
3685 struct video_decoder_capability caps;
3686 decoder_command(zr, DECODER_GET_CAPABILITIES,
3688 if (caps.flags & VIDEO_DECODER_AUTO) {
3689 std->id = V4L2_STD_ALL;
3690 strncpy(std->name, "Autodetect", 31);
3695 switch (std->index) {
3697 std->id = V4L2_STD_PAL;
3698 strncpy(std->name, "PAL", 31);
3699 std->frameperiod.numerator = 1;
3700 std->frameperiod.denominator = 25;
3701 std->framelines = zr->card.tvn[0]->Ht;
3704 std->id = V4L2_STD_NTSC;
3705 strncpy(std->name, "NTSC", 31);
3706 std->frameperiod.numerator = 1001;
3707 std->frameperiod.denominator = 30000;
3708 std->framelines = zr->card.tvn[1]->Ht;
3711 std->id = V4L2_STD_SECAM;
3712 strncpy(std->name, "SECAM", 31);
3713 std->frameperiod.numerator = 1;
3714 std->frameperiod.denominator = 25;
3715 std->framelines = zr->card.tvn[2]->Ht;
3725 v4l2_std_id *std = arg;
3728 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3730 down(&zr->resource_lock);
3732 up(&zr->resource_lock);
3735 case VIDEO_MODE_PAL:
3736 *std = V4L2_STD_PAL;
3738 case VIDEO_MODE_NTSC:
3739 *std = V4L2_STD_NTSC;
3741 case VIDEO_MODE_SECAM:
3742 *std = V4L2_STD_SECAM;
3752 int norm = -1, res = 0;
3753 v4l2_std_id *std = arg;
3755 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3756 ZR_DEVNAME(zr), *std);
3758 if (*std == V4L2_STD_PAL)
3759 norm = VIDEO_MODE_PAL;
3760 else if (*std == V4L2_STD_NTSC)
3761 norm = VIDEO_MODE_NTSC;
3762 else if (*std == V4L2_STD_SECAM)
3763 norm = VIDEO_MODE_SECAM;
3764 else if (*std == V4L2_STD_ALL)
3765 norm = VIDEO_MODE_AUTO;
3769 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3770 ZR_DEVNAME(zr), *std);
3774 down(&zr->resource_lock);
3775 if ((res = zoran_set_norm(zr, norm)))
3776 goto sstd_unlock_and_return;
3778 res = wait_grab_pending(zr);
3779 sstd_unlock_and_return:
3780 up(&zr->resource_lock);
3785 case VIDIOC_ENUMINPUT:
3787 struct v4l2_input *inp = arg;
3790 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3791 ZR_DEVNAME(zr), inp->index);
3793 if (inp->index < 0 || inp->index >= zr->card.inputs)
3796 int id = inp->index;
3797 memset(inp, 0, sizeof(*inp));
3801 strncpy(inp->name, zr->card.input[inp->index].name,
3802 sizeof(inp->name) - 1);
3803 inp->type = V4L2_INPUT_TYPE_CAMERA;
3804 inp->std = V4L2_STD_ALL;
3806 /* Get status of video decoder */
3807 down(&zr->resource_lock);
3808 decoder_command(zr, DECODER_GET_STATUS, &status);
3809 up(&zr->resource_lock);
3811 if (!(status & DECODER_STATUS_GOOD)) {
3812 inp->status |= V4L2_IN_ST_NO_POWER;
3813 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3815 if (!(status & DECODER_STATUS_COLOR))
3816 inp->status |= V4L2_IN_ST_NO_COLOR;
3822 case VIDIOC_G_INPUT:
3826 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3828 down(&zr->resource_lock);
3830 up(&zr->resource_lock);
3836 case VIDIOC_S_INPUT:
3838 int *input = arg, res = 0;
3840 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3841 ZR_DEVNAME(zr), *input);
3843 down(&zr->resource_lock);
3844 if ((res = zoran_set_input(zr, *input)))
3845 goto sinput_unlock_and_return;
3847 /* Make sure the changes come into effect */
3848 res = wait_grab_pending(zr);
3849 sinput_unlock_and_return:
3850 up(&zr->resource_lock);
3855 case VIDIOC_ENUMOUTPUT:
3857 struct v4l2_output *outp = arg;
3859 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3860 ZR_DEVNAME(zr), outp->index);
3862 if (outp->index != 0)
3865 memset(outp, 0, sizeof(*outp));
3867 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3868 strncpy(outp->name, "Autodetect", 31);
3874 case VIDIOC_G_OUTPUT:
3878 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3886 case VIDIOC_S_OUTPUT:
3890 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3891 ZR_DEVNAME(zr), *output);
3900 /* cropping (sub-frame capture) */
3901 case VIDIOC_CROPCAP:
3903 struct v4l2_cropcap *cropcap = arg;
3904 int type = cropcap->type, res = 0;
3906 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3907 ZR_DEVNAME(zr), cropcap->type);
3909 memset(cropcap, 0, sizeof(*cropcap));
3910 cropcap->type = type;
3912 down(&zr->resource_lock);
3914 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3915 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3916 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3919 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3922 goto cropcap_unlock_and_return;
3925 cropcap->bounds.top = cropcap->bounds.left = 0;
3926 cropcap->bounds.width = BUZ_MAX_WIDTH;
3927 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3928 cropcap->defrect.top = cropcap->defrect.left = 0;
3929 cropcap->defrect.width = BUZ_MIN_WIDTH;
3930 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3931 cropcap_unlock_and_return:
3932 up(&zr->resource_lock);
3939 struct v4l2_crop *crop = arg;
3940 int type = crop->type, res = 0;
3942 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3943 ZR_DEVNAME(zr), crop->type);
3945 memset(crop, 0, sizeof(*crop));
3948 down(&zr->resource_lock);
3950 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3951 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3952 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3955 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3958 goto gcrop_unlock_and_return;
3961 crop->c.top = fh->jpg_settings.img_y;
3962 crop->c.left = fh->jpg_settings.img_x;
3963 crop->c.width = fh->jpg_settings.img_width;
3964 crop->c.height = fh->jpg_settings.img_height;
3966 gcrop_unlock_and_return:
3967 up(&zr->resource_lock);
3975 struct v4l2_crop *crop = arg;
3977 struct zoran_jpg_settings settings = fh->jpg_settings;
3981 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3982 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3983 crop->c.width, crop->c.height);
3985 down(&zr->resource_lock);
3987 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3990 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3993 goto scrop_unlock_and_return;
3996 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3997 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3998 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4001 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4004 goto scrop_unlock_and_return;
4007 /* move into a form that we understand */
4008 settings.img_x = crop->c.left;
4009 settings.img_y = crop->c.top;
4010 settings.img_width = crop->c.width;
4011 settings.img_height = crop->c.height;
4013 /* check validity */
4014 if ((res = zoran_check_jpg_settings(zr, &settings)))
4015 goto scrop_unlock_and_return;
4018 fh->jpg_settings = settings;
4020 scrop_unlock_and_return:
4021 up(&zr->resource_lock);
4026 case VIDIOC_G_JPEGCOMP:
4028 struct v4l2_jpegcompression *params = arg;
4030 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4033 memset(params, 0, sizeof(*params));
4035 down(&zr->resource_lock);
4037 params->quality = fh->jpg_settings.jpg_comp.quality;
4038 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4039 memcpy(params->APP_data,
4040 fh->jpg_settings.jpg_comp.APP_data,
4041 fh->jpg_settings.jpg_comp.APP_len);
4042 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4043 memcpy(params->COM_data,
4044 fh->jpg_settings.jpg_comp.COM_data,
4045 fh->jpg_settings.jpg_comp.COM_len);
4046 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4047 params->jpeg_markers =
4048 fh->jpg_settings.jpg_comp.jpeg_markers;
4050 up(&zr->resource_lock);
4056 case VIDIOC_S_JPEGCOMP:
4058 struct v4l2_jpegcompression *params = arg;
4059 struct zoran_jpg_settings settings = fh->jpg_settings;
4064 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4065 ZR_DEVNAME(zr), params->quality, params->APPn,
4066 params->APP_len, params->COM_len);
4068 settings.jpg_comp = *params;
4070 down(&zr->resource_lock);
4072 if (fh->v4l_buffers.active != ZORAN_FREE ||
4073 fh->jpg_buffers.active != ZORAN_FREE) {
4076 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4079 goto sjpegc_unlock_and_return;
4082 if ((res = zoran_check_jpg_settings(zr, &settings)))
4083 goto sjpegc_unlock_and_return;
4084 if (!fh->jpg_buffers.allocated)
4085 fh->jpg_buffers.buffer_size =
4086 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4087 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4088 sjpegc_unlock_and_return:
4089 up(&zr->resource_lock);
4095 case VIDIOC_QUERYSTD: /* why is this useful? */
4097 v4l2_std_id *std = arg;
4100 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4101 ZR_DEVNAME(zr), *std);
4103 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4104 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4105 zr->card.norms == 3)) {
4113 case VIDIOC_TRY_FMT:
4115 struct v4l2_format *fmt = arg;
4118 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4119 ZR_DEVNAME(zr), fmt->type);
4121 switch (fmt->type) {
4122 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4123 down(&zr->resource_lock);
4125 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4126 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4127 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4128 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4129 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4130 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4131 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4132 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4134 up(&zr->resource_lock);
4137 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4138 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4139 if (fmt->fmt.pix.bytesperline > 0)
4142 down(&zr->resource_lock);
4144 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4145 struct zoran_jpg_settings settings =
4148 /* we actually need to set 'real' parameters now */
4149 if ((fmt->fmt.pix.height * 2) >
4151 settings.TmpDcm = 1;
4153 settings.TmpDcm = 2;
4154 settings.decimation = 0;
4155 if (fmt->fmt.pix.height <=
4156 fh->jpg_settings.img_height / 2)
4157 settings.VerDcm = 2;
4159 settings.VerDcm = 1;
4160 if (fmt->fmt.pix.width <=
4161 fh->jpg_settings.img_width / 4)
4162 settings.HorDcm = 4;
4163 else if (fmt->fmt.pix.width <=
4164 fh->jpg_settings.img_width / 2)
4165 settings.HorDcm = 2;
4167 settings.HorDcm = 1;
4168 if (settings.TmpDcm == 1)
4169 settings.field_per_buff = 2;
4171 settings.field_per_buff = 1;
4175 zoran_check_jpg_settings(zr,
4177 goto tryfmt_unlock_and_return;
4179 /* tell the user what we actually did */
4180 fmt->fmt.pix.width =
4181 settings.img_width / settings.HorDcm;
4182 fmt->fmt.pix.height =
4183 settings.img_height * 2 /
4184 (settings.TmpDcm * settings.VerDcm);
4185 if (settings.TmpDcm == 1)
4186 fmt->fmt.pix.field =
4188 odd_even ? V4L2_FIELD_SEQ_TB :
4191 fmt->fmt.pix.field =
4193 odd_even ? V4L2_FIELD_TOP :
4196 fmt->fmt.pix.sizeimage =
4197 zoran_v4l2_calc_bufsize(&settings);
4198 } else if (fmt->type ==
4199 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4202 for (i = 0; i < zoran_num_formats; i++)
4203 if (zoran_formats[i].fourcc ==
4204 fmt->fmt.pix.pixelformat)
4206 if (i == zoran_num_formats) {
4208 goto tryfmt_unlock_and_return;
4211 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4212 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4213 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4214 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4215 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4216 fmt->fmt.pix.height =
4218 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4219 fmt->fmt.pix.height =
4223 goto tryfmt_unlock_and_return;
4225 tryfmt_unlock_and_return:
4226 up(&zr->resource_lock);
4241 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4242 ZR_DEVNAME(zr), cmd);
4243 return -ENOIOCTLCMD;
4252 zoran_ioctl (struct inode *inode,
4257 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4261 zoran_poll (struct file *file,
4264 struct zoran_fh *fh = file->private_data;
4265 struct zoran *zr = fh->zr;
4266 wait_queue_head_t *queue = NULL;
4269 /* we should check whether buffers are ready to be synced on
4270 * (w/o waits - O_NONBLOCK) here
4271 * if ready for read (sync), return POLLIN|POLLRDNORM,
4272 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4273 * if error, return POLLERR,
4274 * if no buffers queued or so, return POLLNVAL
4277 down(&zr->resource_lock);
4279 switch (fh->map_mode) {
4280 case ZORAN_MAP_MODE_RAW:
4281 if (fh->v4l_buffers.active == ZORAN_FREE ||
4282 zr->v4l_pend_head == zr->v4l_pend_tail) {
4284 "%s: zoran_poll() - no buffers queued\n",
4287 goto poll_unlock_and_return;
4289 queue = &zr->v4l_capq;
4290 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4291 poll_wait(file, queue, wait);
4292 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4293 res = POLLIN | POLLRDNORM;
4296 case ZORAN_MAP_MODE_JPG_REC:
4297 case ZORAN_MAP_MODE_JPG_PLAY:
4298 if (fh->jpg_buffers.active == ZORAN_FREE ||
4299 zr->jpg_que_head == zr->jpg_que_tail) {
4301 "%s: zoran_poll() - no buffers queued\n",
4304 goto poll_unlock_and_return;
4306 queue = &zr->jpg_capq;
4307 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4308 poll_wait(file, queue, wait);
4309 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4310 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4311 res = POLLIN | POLLRDNORM;
4313 res = POLLOUT | POLLWRNORM;
4319 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4320 ZR_DEVNAME(zr), fh->map_mode);
4322 goto poll_unlock_and_return;
4325 poll_unlock_and_return:
4326 up(&zr->resource_lock);
4333 * This maps the buffers to user space.
4335 * Depending on the state of fh->map_mode
4336 * the V4L or the MJPEG buffers are mapped
4337 * per buffer or all together
4339 * Note that we need to connect to some
4340 * unmap signal event to unmap the de-allocate
4341 * the buffer accordingly (zoran_vm_close())
4345 zoran_vm_open (struct vm_area_struct *vma)
4347 struct zoran_mapping *map = vma->vm_private_data;
4353 zoran_vm_close (struct vm_area_struct *vma)
4355 struct zoran_mapping *map = vma->vm_private_data;
4356 struct file *file = map->file;
4357 struct zoran_fh *fh = file->private_data;
4358 struct zoran *zr = fh->zr;
4362 if (map->count == 0) {
4363 switch (fh->map_mode) {
4364 case ZORAN_MAP_MODE_JPG_REC:
4365 case ZORAN_MAP_MODE_JPG_PLAY:
4367 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4370 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4371 if (fh->jpg_buffers.buffer[i].map == map) {
4372 fh->jpg_buffers.buffer[i].map =
4378 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4379 if (fh->jpg_buffers.buffer[i].map)
4381 if (i == fh->jpg_buffers.num_buffers) {
4382 down(&zr->resource_lock);
4384 if (fh->jpg_buffers.active != ZORAN_FREE) {
4385 jpg_qbuf(file, -1, zr->codec_mode);
4386 zr->jpg_buffers.allocated = 0;
4387 zr->jpg_buffers.active =
4388 fh->jpg_buffers.active =
4391 //jpg_fbuffer_free(file);
4392 fh->jpg_buffers.allocated = 0;
4393 fh->jpg_buffers.ready_to_be_freed = 1;
4395 up(&zr->resource_lock);
4400 case ZORAN_MAP_MODE_RAW:
4402 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4405 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4406 if (fh->v4l_buffers.buffer[i].map == map) {
4408 fh->v4l_buffers.buffer[i].map =
4414 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4415 if (fh->v4l_buffers.buffer[i].map)
4417 if (i == fh->v4l_buffers.num_buffers) {
4418 down(&zr->resource_lock);
4420 if (fh->v4l_buffers.active != ZORAN_FREE) {
4421 zr36057_set_memgrab(zr, 0);
4422 zr->v4l_buffers.allocated = 0;
4423 zr->v4l_buffers.active =
4424 fh->v4l_buffers.active =
4427 //v4l_fbuffer_free(file);
4428 fh->v4l_buffers.allocated = 0;
4429 fh->v4l_buffers.ready_to_be_freed = 1;
4431 up(&zr->resource_lock);
4438 "%s: munmap() - internal error - unknown map mode %d\n",
4439 ZR_DEVNAME(zr), fh->map_mode);
4446 static struct vm_operations_struct zoran_vm_ops = {
4447 .open = zoran_vm_open,
4448 .close = zoran_vm_close,
4452 zoran_mmap (struct file *file,
4453 struct vm_area_struct *vma)
4455 struct zoran_fh *fh = file->private_data;
4456 struct zoran *zr = fh->zr;
4457 unsigned long size = (vma->vm_end - vma->vm_start);
4458 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4460 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4462 struct zoran_mapping *map;
4466 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4468 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4469 vma->vm_start, vma->vm_end, size);
4471 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4472 !(vma->vm_flags & VM_WRITE)) {
4475 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4480 switch (fh->map_mode) {
4482 case ZORAN_MAP_MODE_JPG_REC:
4483 case ZORAN_MAP_MODE_JPG_PLAY:
4486 down(&zr->resource_lock);
4488 /* Map the MJPEG buffers */
4489 if (!fh->jpg_buffers.allocated) {
4492 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4495 goto jpg_mmap_unlock_and_return;
4498 first = offset / fh->jpg_buffers.buffer_size;
4499 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4500 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4501 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4502 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4503 last >= fh->jpg_buffers.num_buffers) {
4506 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4507 ZR_DEVNAME(zr), offset, size,
4508 fh->jpg_buffers.buffer_size,
4509 fh->jpg_buffers.num_buffers);
4511 goto jpg_mmap_unlock_and_return;
4513 for (i = first; i <= last; i++) {
4514 if (fh->jpg_buffers.buffer[i].map) {
4517 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4520 goto jpg_mmap_unlock_and_return;
4524 /* map these buffers (v4l_buffers[i]) */
4525 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4528 goto jpg_mmap_unlock_and_return;
4533 vma->vm_ops = &zoran_vm_ops;
4534 vma->vm_flags |= VM_DONTEXPAND;
4535 vma->vm_private_data = map;
4537 for (i = first; i <= last; i++) {
4539 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4542 (fh->jpg_buffers.buffer[i].
4543 frag_tab[2 * j + 1] & ~1) << 1;
4548 (unsigned long) fh->jpg_buffers.
4549 buffer[i].frag_tab[2 * j];
4550 /* should just be pos on i386 */
4551 page = virt_to_phys(bus_to_virt(pos))
4553 if (remap_pfn_range(vma, start, page,
4554 todo, PAGE_SHARED)) {
4557 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4560 goto jpg_mmap_unlock_and_return;
4566 if (fh->jpg_buffers.buffer[i].
4567 frag_tab[2 * j + 1] & 1)
4568 break; /* was last fragment */
4570 fh->jpg_buffers.buffer[i].map = map;
4575 jpg_mmap_unlock_and_return:
4576 up(&zr->resource_lock);
4580 case ZORAN_MAP_MODE_RAW:
4582 down(&zr->resource_lock);
4584 /* Map the V4L buffers */
4585 if (!fh->v4l_buffers.allocated) {
4588 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4591 goto v4l_mmap_unlock_and_return;
4594 first = offset / fh->v4l_buffers.buffer_size;
4595 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4596 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4597 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4598 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4599 last >= fh->v4l_buffers.buffer_size) {
4602 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4603 ZR_DEVNAME(zr), offset, size,
4604 fh->v4l_buffers.buffer_size,
4605 fh->v4l_buffers.num_buffers);
4607 goto v4l_mmap_unlock_and_return;
4609 for (i = first; i <= last; i++) {
4610 if (fh->v4l_buffers.buffer[i].map) {
4613 "%s: mmap(V4L) - buffer %d already mapped\n",
4616 goto v4l_mmap_unlock_and_return;
4620 /* map these buffers (v4l_buffers[i]) */
4621 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4624 goto v4l_mmap_unlock_and_return;
4629 vma->vm_ops = &zoran_vm_ops;
4630 vma->vm_flags |= VM_DONTEXPAND;
4631 vma->vm_private_data = map;
4633 for (i = first; i <= last; i++) {
4635 if (todo > fh->v4l_buffers.buffer_size)
4636 todo = fh->v4l_buffers.buffer_size;
4637 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4638 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4639 todo, PAGE_SHARED)) {
4642 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4645 goto v4l_mmap_unlock_and_return;
4649 fh->v4l_buffers.buffer[i].map = map;
4653 v4l_mmap_unlock_and_return:
4654 up(&zr->resource_lock);
4661 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4662 ZR_DEVNAME(zr), fh->map_mode);
4669 static struct file_operations zoran_fops = {
4670 .owner = THIS_MODULE,
4672 .release = zoran_close,
4673 .ioctl = zoran_ioctl,
4674 .llseek = no_llseek,
4676 .write = zoran_write,
4681 struct video_device zoran_template __devinitdata = {
4683 .type = ZORAN_VID_TYPE,
4685 .type2 = ZORAN_V4L2_VID_FLAGS,
4687 .hardware = ZORAN_HARDWARE,
4688 .fops = &zoran_fops,
4689 .release = &zoran_vdev_release,