2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
60 #include <linux/spinlock.h>
62 #include <linux/videodev2.h>
63 #include <media/v4l2-common.h>
64 #include <media/v4l2-ioctl.h>
65 #include "videocodec.h"
67 #include <asm/byteorder.h>
69 #include <asm/uaccess.h>
70 #include <linux/proc_fs.h>
72 #include <linux/mutex.h>
74 #include "zoran_device.h"
75 #include "zoran_card.h"
78 const struct zoran_format zoran_formats[] = {
80 .name = "15-bit RGB LE",
81 .fourcc = V4L2_PIX_FMT_RGB555,
82 .colorspace = V4L2_COLORSPACE_SRGB,
84 .flags = ZORAN_FORMAT_CAPTURE |
86 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
87 ZR36057_VFESPFR_LittleEndian,
89 .name = "15-bit RGB BE",
90 .fourcc = V4L2_PIX_FMT_RGB555X,
91 .colorspace = V4L2_COLORSPACE_SRGB,
93 .flags = ZORAN_FORMAT_CAPTURE |
95 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
97 .name = "16-bit RGB LE",
98 .fourcc = V4L2_PIX_FMT_RGB565,
99 .colorspace = V4L2_COLORSPACE_SRGB,
101 .flags = ZORAN_FORMAT_CAPTURE |
102 ZORAN_FORMAT_OVERLAY,
103 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
104 ZR36057_VFESPFR_LittleEndian,
106 .name = "16-bit RGB BE",
107 .fourcc = V4L2_PIX_FMT_RGB565X,
108 .colorspace = V4L2_COLORSPACE_SRGB,
110 .flags = ZORAN_FORMAT_CAPTURE |
111 ZORAN_FORMAT_OVERLAY,
112 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
114 .name = "24-bit RGB",
115 .fourcc = V4L2_PIX_FMT_BGR24,
116 .colorspace = V4L2_COLORSPACE_SRGB,
118 .flags = ZORAN_FORMAT_CAPTURE |
119 ZORAN_FORMAT_OVERLAY,
120 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
122 .name = "32-bit RGB LE",
123 .fourcc = V4L2_PIX_FMT_BGR32,
124 .colorspace = V4L2_COLORSPACE_SRGB,
126 .flags = ZORAN_FORMAT_CAPTURE |
127 ZORAN_FORMAT_OVERLAY,
128 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
130 .name = "32-bit RGB BE",
131 .fourcc = V4L2_PIX_FMT_RGB32,
132 .colorspace = V4L2_COLORSPACE_SRGB,
134 .flags = ZORAN_FORMAT_CAPTURE |
135 ZORAN_FORMAT_OVERLAY,
136 .vfespfr = ZR36057_VFESPFR_RGB888,
138 .name = "4:2:2, packed, YUYV",
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .colorspace = V4L2_COLORSPACE_SMPTE170M,
142 .flags = ZORAN_FORMAT_CAPTURE |
143 ZORAN_FORMAT_OVERLAY,
144 .vfespfr = ZR36057_VFESPFR_YUV422,
146 .name = "4:2:2, packed, UYVY",
147 .fourcc = V4L2_PIX_FMT_UYVY,
148 .colorspace = V4L2_COLORSPACE_SMPTE170M,
150 .flags = ZORAN_FORMAT_CAPTURE |
151 ZORAN_FORMAT_OVERLAY,
152 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
154 .name = "Hardware-encoded Motion-JPEG",
155 .fourcc = V4L2_PIX_FMT_MJPEG,
156 .colorspace = V4L2_COLORSPACE_SMPTE170M,
158 .flags = ZORAN_FORMAT_CAPTURE |
159 ZORAN_FORMAT_PLAYBACK |
160 ZORAN_FORMAT_COMPRESSED,
163 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
165 static int lock_norm; /* 0 = default 1 = Don't change TV standard (norm) */
166 module_param(lock_norm, int, 0644);
167 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
169 /* small helper function for calculating buffersizes for v4l2
170 * we calculate the nearest higher power-of-two, which
171 * will be the recommended buffersize */
173 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
175 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
176 __u32 num = (1024 * 512) / (div);
185 if (result > jpg_bufsize)
192 /* forward references */
193 static void v4l_fbuffer_free(struct file *file);
194 static void jpg_fbuffer_free(struct file *file);
197 * Allocate the V4L grab buffers
199 * These have to be pysically contiguous.
203 v4l_fbuffer_alloc (struct file *file)
205 struct zoran_fh *fh = file->private_data;
206 struct zoran *zr = fh->zr;
210 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
211 if (fh->v4l_buffers.buffer[i].fbuffer)
214 "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
218 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
222 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
224 v4l_fbuffer_free(file);
227 fh->v4l_buffers.buffer[i].fbuffer = mem;
228 fh->v4l_buffers.buffer[i].fbuffer_phys =
230 fh->v4l_buffers.buffer[i].fbuffer_bus =
232 for (off = 0; off < fh->v4l_buffers.buffer_size;
234 SetPageReserved(virt_to_page(mem + off));
237 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
238 ZR_DEVNAME(zr), i, (unsigned long) mem,
242 fh->v4l_buffers.allocated = 1;
247 /* free the V4L grab buffers */
249 v4l_fbuffer_free (struct file *file)
251 struct zoran_fh *fh = file->private_data;
252 struct zoran *zr = fh->zr;
256 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
258 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
259 if (!fh->v4l_buffers.buffer[i].fbuffer)
262 mem = fh->v4l_buffers.buffer[i].fbuffer;
263 for (off = 0; off < fh->v4l_buffers.buffer_size;
265 ClearPageReserved(virt_to_page(mem + off));
266 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
267 fh->v4l_buffers.buffer[i].fbuffer = NULL;
270 fh->v4l_buffers.allocated = 0;
274 * Allocate the MJPEG grab buffers.
276 * If a Natoma chipset is present and this is a revision 1 zr36057,
277 * each MJPEG buffer needs to be physically contiguous.
278 * (RJ: This statement is from Dave Perks' original driver,
279 * I could never check it because I have a zr36067)
281 * RJ: The contents grab buffers needs never be accessed in the driver.
282 * Therefore there is no need to allocate them with vmalloc in order
283 * to get a contiguous virtual memory space.
284 * I don't understand why many other drivers first allocate them with
285 * vmalloc (which uses internally also get_zeroed_page, but delivers you
286 * virtual addresses) and then again have to make a lot of efforts
287 * to get the physical address.
290 * On big-endian architectures (such as ppc) some extra steps
291 * are needed. When reading and writing to the stat_com array
292 * and fragment buffers, the device expects to see little-
293 * endian values. The use of cpu_to_le32() and le32_to_cpu()
294 * in this function (and one or two others in zoran_device.c)
295 * ensure that these values are always stored in little-endian
296 * form, regardless of architecture. The zr36057 does Very Bad
297 * Things on big endian architectures if the stat_com array
298 * and fragment buffers are not little-endian.
302 jpg_fbuffer_alloc (struct file *file)
304 struct zoran_fh *fh = file->private_data;
305 struct zoran *zr = fh->zr;
309 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
310 if (fh->jpg_buffers.buffer[i].frag_tab)
313 "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
316 /* Allocate fragment table for this buffer */
318 mem = get_zeroed_page(GFP_KERNEL);
322 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
324 jpg_fbuffer_free(file);
327 fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
328 fh->jpg_buffers.buffer[i].frag_tab_bus =
329 virt_to_bus((void *) mem);
331 //if (alloc_contig) {
332 if (fh->jpg_buffers.need_contiguous) {
334 (unsigned long) kmalloc(fh->jpg_buffers.
340 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
342 jpg_fbuffer_free(file);
345 fh->jpg_buffers.buffer[i].frag_tab[0] =
346 cpu_to_le32(virt_to_bus((void *) mem));
347 fh->jpg_buffers.buffer[i].frag_tab[1] =
348 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
349 for (off = 0; off < fh->jpg_buffers.buffer_size;
351 SetPageReserved(virt_to_page(mem + off));
353 /* jpg_bufsize is already page aligned */
355 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
357 mem = get_zeroed_page(GFP_KERNEL);
361 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
363 jpg_fbuffer_free(file);
367 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
368 cpu_to_le32(virt_to_bus((void *) mem));
369 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
371 cpu_to_le32((PAGE_SIZE / 4) << 1);
372 SetPageReserved(virt_to_page(mem));
375 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
380 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
382 (fh->jpg_buffers.num_buffers *
383 fh->jpg_buffers.buffer_size) >> 10);
385 fh->jpg_buffers.allocated = 1;
390 /* free the MJPEG grab buffers */
392 jpg_fbuffer_free (struct file *file)
394 struct zoran_fh *fh = file->private_data;
395 struct zoran *zr = fh->zr;
400 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
402 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
403 if (!fh->jpg_buffers.buffer[i].frag_tab)
406 if (fh->jpg_buffers.need_contiguous) {
407 frag_tab = fh->jpg_buffers.buffer[i].frag_tab[0];
410 mem = (unsigned char *)bus_to_virt(le32_to_cpu(frag_tab));
411 for (off = 0; off < fh->jpg_buffers.buffer_size; off += PAGE_SIZE)
412 ClearPageReserved(virt_to_page(mem + off));
414 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
415 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
418 for (j = 0; j < fh->jpg_buffers.buffer_size / PAGE_SIZE; j++) {
419 frag_tab = fh->jpg_buffers.buffer[i].frag_tab[2 * j];
423 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
424 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
425 fh->jpg_buffers.buffer[i].frag_tab[2 * j] = 0;
426 fh->jpg_buffers.buffer[i].frag_tab[2 * j + 1] = 0;
430 free_page((unsigned long)fh->jpg_buffers.buffer[i].frag_tab);
431 fh->jpg_buffers.buffer[i].frag_tab = NULL;
434 fh->jpg_buffers.allocated = 0;
438 * V4L Buffer grabbing
442 zoran_v4l_set_format (struct file *file,
445 const struct zoran_format *format)
447 struct zoran_fh *fh = file->private_data;
448 struct zoran *zr = fh->zr;
451 /* Check size and format of the grab wanted */
453 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
454 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
457 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
458 ZR_DEVNAME(zr), width, height);
462 bpp = (format->depth + 7) / 8;
464 /* Check against available buffer size */
465 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
468 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
469 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
473 /* The video front end needs 4-byte alinged line sizes */
475 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
478 "%s: v4l_set_format() - wrong frame alignment\n",
483 fh->v4l_settings.width = width;
484 fh->v4l_settings.height = height;
485 fh->v4l_settings.format = format;
486 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
492 zoran_v4l_queue_frame (struct file *file,
495 struct zoran_fh *fh = file->private_data;
496 struct zoran *zr = fh->zr;
500 if (!fh->v4l_buffers.allocated) {
503 "%s: v4l_queue_frame() - buffers not yet allocated\n",
508 /* No grabbing outside the buffer range! */
509 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
512 "%s: v4l_queue_frame() - buffer %d is out of range\n",
513 ZR_DEVNAME(zr), num);
517 spin_lock_irqsave(&zr->spinlock, flags);
519 if (fh->v4l_buffers.active == ZORAN_FREE) {
520 if (zr->v4l_buffers.active == ZORAN_FREE) {
521 zr->v4l_buffers = fh->v4l_buffers;
522 fh->v4l_buffers.active = ZORAN_ACTIVE;
526 "%s: v4l_queue_frame() - another session is already capturing\n",
532 /* make sure a grab isn't going on currently with this buffer */
534 switch (zr->v4l_buffers.buffer[num].state) {
537 if (zr->v4l_buffers.active == ZORAN_FREE) {
538 fh->v4l_buffers.active = ZORAN_FREE;
539 zr->v4l_buffers.allocated = 0;
541 res = -EBUSY; /* what are you doing? */
546 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
547 ZR_DEVNAME(zr), num);
549 /* since there is at least one unused buffer there's room for at least
550 * one more pend[] entry */
551 zr->v4l_pend[zr->v4l_pend_head++ &
552 V4L_MASK_FRAME] = num;
553 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
554 zr->v4l_buffers.buffer[num].bs.length =
555 fh->v4l_settings.bytesperline *
556 zr->v4l_settings.height;
557 fh->v4l_buffers.buffer[num] =
558 zr->v4l_buffers.buffer[num];
563 spin_unlock_irqrestore(&zr->spinlock, flags);
565 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
566 zr->v4l_buffers.active = fh->v4l_buffers.active;
572 * Sync on a V4L buffer
576 v4l_sync (struct file *file,
579 struct zoran_fh *fh = file->private_data;
580 struct zoran *zr = fh->zr;
583 if (fh->v4l_buffers.active == ZORAN_FREE) {
586 "%s: v4l_sync() - no grab active for this session\n",
591 /* check passed-in frame number */
592 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
594 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
595 ZR_DEVNAME(zr), frame);
599 /* Check if is buffer was queued at all */
600 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
603 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
608 /* wait on this buffer to get ready */
609 if (!wait_event_interruptible_timeout(zr->v4l_capq,
610 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
613 if (signal_pending(current))
616 /* buffer should now be in BUZ_STATE_DONE */
617 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
619 KERN_ERR "%s: v4l_sync() - internal state error\n",
622 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
623 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
625 spin_lock_irqsave(&zr->spinlock, flags);
627 /* Check if streaming capture has finished */
628 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
629 zr36057_set_memgrab(zr, 0);
630 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
631 fh->v4l_buffers.active = zr->v4l_buffers.active =
633 zr->v4l_buffers.allocated = 0;
637 spin_unlock_irqrestore(&zr->spinlock, flags);
643 * Queue a MJPEG buffer for capture/playback
647 zoran_jpg_queue_frame (struct file *file,
649 enum zoran_codec_mode mode)
651 struct zoran_fh *fh = file->private_data;
652 struct zoran *zr = fh->zr;
656 /* Check if buffers are allocated */
657 if (!fh->jpg_buffers.allocated) {
660 "%s: jpg_queue_frame() - buffers not yet allocated\n",
665 /* No grabbing outside the buffer range! */
666 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
669 "%s: jpg_queue_frame() - buffer %d out of range\n",
670 ZR_DEVNAME(zr), num);
674 /* what is the codec mode right now? */
675 if (zr->codec_mode == BUZ_MODE_IDLE) {
676 zr->jpg_settings = fh->jpg_settings;
677 } else if (zr->codec_mode != mode) {
678 /* wrong codec mode active - invalid */
681 "%s: jpg_queue_frame() - codec in wrong mode\n",
686 if (fh->jpg_buffers.active == ZORAN_FREE) {
687 if (zr->jpg_buffers.active == ZORAN_FREE) {
688 zr->jpg_buffers = fh->jpg_buffers;
689 fh->jpg_buffers.active = ZORAN_ACTIVE;
693 "%s: jpg_queue_frame() - another session is already capturing\n",
699 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
700 /* Ok load up the jpeg codec */
701 zr36057_enable_jpg(zr, mode);
704 spin_lock_irqsave(&zr->spinlock, flags);
707 switch (zr->jpg_buffers.buffer[num].state) {
711 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
714 /* since there is at least one unused buffer there's room for at
715 *least one more pend[] entry */
716 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
718 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
719 fh->jpg_buffers.buffer[num] =
720 zr->jpg_buffers.buffer[num];
721 zoran_feed_stat_com(zr);
726 if (zr->jpg_buffers.active == ZORAN_FREE) {
727 fh->jpg_buffers.active = ZORAN_FREE;
728 zr->jpg_buffers.allocated = 0;
730 res = -EBUSY; /* what are you doing? */
735 spin_unlock_irqrestore(&zr->spinlock, flags);
737 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
738 zr->jpg_buffers.active = fh->jpg_buffers.active;
745 jpg_qbuf (struct file *file,
747 enum zoran_codec_mode mode)
749 struct zoran_fh *fh = file->private_data;
750 struct zoran *zr = fh->zr;
753 /* Does the user want to stop streaming? */
755 if (zr->codec_mode == mode) {
756 if (fh->jpg_buffers.active == ZORAN_FREE) {
759 "%s: jpg_qbuf(-1) - session not active\n",
763 fh->jpg_buffers.active = zr->jpg_buffers.active =
765 zr->jpg_buffers.allocated = 0;
766 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
771 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
777 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
780 /* Start the jpeg codec when the first frame is queued */
781 if (!res && zr->jpg_que_head == 1)
788 * Sync on a MJPEG buffer
792 jpg_sync (struct file *file,
793 struct zoran_sync *bs)
795 struct zoran_fh *fh = file->private_data;
796 struct zoran *zr = fh->zr;
800 if (fh->jpg_buffers.active == ZORAN_FREE) {
803 "%s: jpg_sync() - capture is not currently active\n",
807 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
808 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
811 "%s: jpg_sync() - codec not in streaming mode\n",
815 if (!wait_event_interruptible_timeout(zr->jpg_capq,
816 (zr->jpg_que_tail != zr->jpg_dma_tail ||
817 zr->jpg_dma_tail == zr->jpg_dma_head),
821 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
823 zr->codec->control(zr->codec, CODEC_G_STATUS,
827 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
828 ZR_DEVNAME(zr), isr);
833 if (signal_pending(current))
836 spin_lock_irqsave(&zr->spinlock, flags);
838 if (zr->jpg_dma_tail != zr->jpg_dma_head)
839 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
841 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
843 /* buffer should now be in BUZ_STATE_DONE */
844 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
846 KERN_ERR "%s: jpg_sync() - internal state error\n",
849 *bs = zr->jpg_buffers.buffer[frame].bs;
851 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
852 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
854 spin_unlock_irqrestore(&zr->spinlock, flags);
860 zoran_open_init_session (struct file *file)
863 struct zoran_fh *fh = file->private_data;
864 struct zoran *zr = fh->zr;
866 /* Per default, map the V4L Buffers */
867 fh->map_mode = ZORAN_MAP_MODE_RAW;
869 /* take over the card's current settings */
870 fh->overlay_settings = zr->overlay_settings;
871 fh->overlay_settings.is_set = 0;
872 fh->overlay_settings.format = zr->overlay_settings.format;
873 fh->overlay_active = ZORAN_FREE;
876 fh->v4l_settings = zr->v4l_settings;
879 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
880 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
881 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
882 fh->v4l_buffers.buffer[i].bs.frame = i;
884 fh->v4l_buffers.allocated = 0;
885 fh->v4l_buffers.active = ZORAN_FREE;
886 fh->v4l_buffers.buffer_size = v4l_bufsize;
887 fh->v4l_buffers.num_buffers = v4l_nbufs;
890 fh->jpg_settings = zr->jpg_settings;
893 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
894 for (i = 0; i < BUZ_MAX_FRAME; i++) {
895 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
896 fh->jpg_buffers.buffer[i].bs.frame = i;
898 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
899 fh->jpg_buffers.allocated = 0;
900 fh->jpg_buffers.active = ZORAN_FREE;
901 fh->jpg_buffers.buffer_size = jpg_bufsize;
902 fh->jpg_buffers.num_buffers = jpg_nbufs;
906 zoran_close_end_session (struct file *file)
908 struct zoran_fh *fh = file->private_data;
909 struct zoran *zr = fh->zr;
912 if (fh->overlay_active != ZORAN_FREE) {
913 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
914 zr->v4l_overlay_active = 0;
915 if (!zr->v4l_memgrab_active)
916 zr36057_overlay(zr, 0);
917 zr->overlay_mask = NULL;
921 if (fh->v4l_buffers.active != ZORAN_FREE) {
924 spin_lock_irqsave(&zr->spinlock, flags);
925 zr36057_set_memgrab(zr, 0);
926 zr->v4l_buffers.allocated = 0;
927 zr->v4l_buffers.active = fh->v4l_buffers.active =
929 spin_unlock_irqrestore(&zr->spinlock, flags);
933 if (fh->v4l_buffers.allocated)
934 v4l_fbuffer_free(file);
937 if (fh->jpg_buffers.active != ZORAN_FREE) {
938 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
939 zr->jpg_buffers.allocated = 0;
940 zr->jpg_buffers.active = fh->jpg_buffers.active =
945 if (fh->jpg_buffers.allocated)
946 jpg_fbuffer_free(file);
950 * Open a zoran card. Right now the flags stuff is just playing
953 static int zoran_open(struct file *file)
955 struct zoran *zr = video_drvdata(file);
957 int res, first_open = 0;
959 dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
960 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
964 if (zr->user >= 2048) {
965 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
966 ZR_DEVNAME(zr), zr->user);
971 /* now, create the open()-specific file_ops struct */
972 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
976 "%s: zoran_open() - allocation of zoran_fh failed\n",
981 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
984 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
985 if (!fh->overlay_mask) {
988 "%s: zoran_open() - allocation of overlay_mask failed\n",
997 /*mutex_unlock(&zr->resource_lock);*/
999 /* default setup - TODO: look at flags */
1000 if (first_open) { /* First device open */
1001 zr36057_restart(zr);
1002 zoran_open_init_params(zr);
1003 zoran_init_hardware(zr);
1005 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1008 /* set file_ops stuff */
1009 file->private_data = fh;
1011 zoran_open_init_session(file);
1021 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1022 ZR_DEVNAME(zr), res, zr->user);
1028 zoran_close(struct file *file)
1030 struct zoran_fh *fh = file->private_data;
1031 struct zoran *zr = fh->zr;
1033 dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1034 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1036 /* kernel locks (fs/device.c), so don't do that ourselves
1037 * (prevents deadlocks) */
1038 /*mutex_lock(&zr->resource_lock);*/
1040 zoran_close_end_session(file);
1042 if (zr->user-- == 1) { /* Last process */
1043 /* Clean up JPEG process */
1044 wake_up_interruptible(&zr->jpg_capq);
1045 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1046 zr->jpg_buffers.allocated = 0;
1047 zr->jpg_buffers.active = ZORAN_FREE;
1049 /* disable interrupts */
1050 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1052 if (zr36067_debug > 1)
1053 print_interrupts(zr);
1056 zr->v4l_overlay_active = 0;
1057 zr36057_overlay(zr, 0);
1058 zr->overlay_mask = NULL;
1061 wake_up_interruptible(&zr->v4l_capq);
1062 zr36057_set_memgrab(zr, 0);
1063 zr->v4l_buffers.allocated = 0;
1064 zr->v4l_buffers.active = ZORAN_FREE;
1065 zoran_set_pci_master(zr, 0);
1067 if (!pass_through) { /* Switch to color bar */
1068 struct v4l2_routing route = { 2, 0 };
1070 decoder_call(zr, video, s_stream, 0);
1071 encoder_call(zr, video, s_routing, &route);
1075 file->private_data = NULL;
1076 kfree(fh->overlay_mask);
1079 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1086 zoran_read (struct file *file,
1091 /* we simply don't support read() (yet)... */
1097 zoran_write (struct file *file,
1098 const char __user *data,
1102 /* ...and the same goes for write() */
1108 setup_fbuffer (struct file *file,
1110 const struct zoran_format *fmt,
1115 struct zoran_fh *fh = file->private_data;
1116 struct zoran *zr = fh->zr;
1118 /* (Ronald) v4l/v4l2 guidelines */
1119 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1122 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1123 ALi Magik (that needs very low latency while the card needs a
1124 higher value always) */
1126 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1129 /* we need a bytesperline value, even if not given */
1131 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1134 if (zr->overlay_active) {
1135 /* dzjee... stupid users... don't even bother to turn off
1136 * overlay before changing the memory location...
1137 * normally, we would return errors here. However, one of
1138 * the tools that does this is... xawtv! and since xawtv
1139 * is used by +/- 99% of the users, we'd rather be user-
1140 * friendly and silently do as if nothing went wrong */
1143 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1145 zr36057_overlay(zr, 0);
1149 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1152 "%s: setup_fbuffer() - no valid overlay format given\n",
1156 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1159 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1160 ZR_DEVNAME(zr), width, height, bytesperline);
1163 if (bytesperline & 3) {
1166 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1167 ZR_DEVNAME(zr), bytesperline);
1171 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1172 zr->vbuf_height = height;
1173 zr->vbuf_width = width;
1174 zr->vbuf_depth = fmt->depth;
1175 zr->overlay_settings.format = fmt;
1176 zr->vbuf_bytesperline = bytesperline;
1178 /* The user should set new window parameters */
1179 zr->overlay_settings.is_set = 0;
1186 setup_window (struct file *file,
1191 struct v4l2_clip __user *clips,
1193 void __user *bitmap)
1195 struct zoran_fh *fh = file->private_data;
1196 struct zoran *zr = fh->zr;
1197 struct v4l2_clip *vcp = NULL;
1201 if (!zr->vbuf_base) {
1204 "%s: setup_window() - frame buffer has to be set first\n",
1209 if (!fh->overlay_settings.format) {
1212 "%s: setup_window() - no overlay format set\n",
1218 * The video front end needs 4-byte alinged line sizes, we correct that
1219 * silently here if necessary
1221 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1222 end = (x + width) & ~1; /* round down */
1223 x = (x + 1) & ~1; /* round up */
1227 if (zr->vbuf_depth == 24) {
1228 end = (x + width) & ~3; /* round down */
1229 x = (x + 3) & ~3; /* round up */
1233 if (width > BUZ_MAX_WIDTH)
1234 width = BUZ_MAX_WIDTH;
1235 if (height > BUZ_MAX_HEIGHT)
1236 height = BUZ_MAX_HEIGHT;
1238 /* Check for vaild parameters */
1239 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1240 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1243 "%s: setup_window() - width = %d or height = %d invalid\n",
1244 ZR_DEVNAME(zr), width, height);
1248 fh->overlay_settings.x = x;
1249 fh->overlay_settings.y = y;
1250 fh->overlay_settings.width = width;
1251 fh->overlay_settings.height = height;
1252 fh->overlay_settings.clipcount = clipcount;
1255 * If an overlay is running, we have to switch it off
1256 * and switch it on again in order to get the new settings in effect.
1258 * We also want to avoid that the overlay mask is written
1259 * when an overlay is running.
1262 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1263 zr->overlay_active != ZORAN_FREE &&
1264 fh->overlay_active != ZORAN_FREE;
1266 zr36057_overlay(zr, 0);
1269 * Write the overlay mask if clips are wanted.
1270 * We prefer a bitmap.
1273 /* fake value - it just means we want clips */
1274 fh->overlay_settings.clipcount = 1;
1276 if (copy_from_user(fh->overlay_mask, bitmap,
1277 (width * height + 7) / 8)) {
1280 } else if (clipcount > 0) {
1281 /* write our own bitmap from the clips */
1282 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1286 "%s: setup_window() - Alloc of clip mask failed\n",
1291 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1295 write_overlay_mask(file, vcp, clipcount);
1299 fh->overlay_settings.is_set = 1;
1300 if (fh->overlay_active != ZORAN_FREE &&
1301 zr->overlay_active != ZORAN_FREE)
1302 zr->overlay_settings = fh->overlay_settings;
1305 zr36057_overlay(zr, 1);
1307 /* Make sure the changes come into effect */
1308 return wait_grab_pending(zr);
1312 setup_overlay (struct file *file,
1315 struct zoran_fh *fh = file->private_data;
1316 struct zoran *zr = fh->zr;
1318 /* If there is nothing to do, return immediatly */
1319 if ((on && fh->overlay_active != ZORAN_FREE) ||
1320 (!on && fh->overlay_active == ZORAN_FREE))
1323 /* check whether we're touching someone else's overlay */
1324 if (on && zr->overlay_active != ZORAN_FREE &&
1325 fh->overlay_active == ZORAN_FREE) {
1328 "%s: setup_overlay() - overlay is already active for another session\n",
1332 if (!on && zr->overlay_active != ZORAN_FREE &&
1333 fh->overlay_active == ZORAN_FREE) {
1336 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1342 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1343 zr->v4l_overlay_active = 0;
1344 /* When a grab is running, the video simply
1345 * won't be switched on any more */
1346 if (!zr->v4l_memgrab_active)
1347 zr36057_overlay(zr, 0);
1348 zr->overlay_mask = NULL;
1350 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1353 "%s: setup_overlay() - buffer or window not set\n",
1357 if (!fh->overlay_settings.format) {
1360 "%s: setup_overlay() - no overlay format set\n",
1364 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1365 zr->v4l_overlay_active = 1;
1366 zr->overlay_mask = fh->overlay_mask;
1367 zr->overlay_settings = fh->overlay_settings;
1368 if (!zr->v4l_memgrab_active)
1369 zr36057_overlay(zr, 1);
1370 /* When a grab is running, the video will be
1371 * switched on when grab is finished */
1374 /* Make sure the changes come into effect */
1375 return wait_grab_pending(zr);
1378 /* get the status of a buffer in the clients buffer queue */
1380 zoran_v4l2_buffer_status (struct file *file,
1381 struct v4l2_buffer *buf,
1384 struct zoran_fh *fh = file->private_data;
1385 struct zoran *zr = fh->zr;
1387 buf->flags = V4L2_BUF_FLAG_MAPPED;
1389 switch (fh->map_mode) {
1390 case ZORAN_MAP_MODE_RAW:
1393 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1394 !fh->v4l_buffers.allocated) {
1397 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1402 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1403 buf->length = fh->v4l_buffers.buffer_size;
1406 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1407 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1408 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1409 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1410 buf->flags |= V4L2_BUF_FLAG_DONE;
1412 fh->v4l_buffers.buffer[num].bs.timestamp;
1414 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1417 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1418 buf->field = V4L2_FIELD_TOP;
1420 buf->field = V4L2_FIELD_INTERLACED;
1424 case ZORAN_MAP_MODE_JPG_REC:
1425 case ZORAN_MAP_MODE_JPG_PLAY:
1428 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1429 !fh->jpg_buffers.allocated) {
1432 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1437 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1438 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1439 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1440 buf->length = fh->jpg_buffers.buffer_size;
1442 /* these variables are only written after frame has been captured */
1443 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1444 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1445 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1447 fh->jpg_buffers.buffer[num].bs.timestamp;
1449 fh->jpg_buffers.buffer[num].bs.length;
1450 buf->flags |= V4L2_BUF_FLAG_DONE;
1452 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1455 /* which fields are these? */
1456 if (fh->jpg_settings.TmpDcm != 1)
1459 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1463 odd_even ? V4L2_FIELD_SEQ_TB :
1472 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1473 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1477 buf->memory = V4L2_MEMORY_MMAP;
1479 buf->m.offset = buf->length * num;
1485 zoran_set_norm (struct zoran *zr,
1490 if (zr->v4l_buffers.active != ZORAN_FREE ||
1491 zr->jpg_buffers.active != ZORAN_FREE) {
1494 "%s: set_norm() called while in playback/capture mode\n",
1499 if (lock_norm && norm != zr->norm) {
1500 if (lock_norm > 1) {
1503 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1509 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1515 if (!(norm & zr->card.norms)) {
1517 KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1518 ZR_DEVNAME(zr), norm);
1522 if (norm == V4L2_STD_ALL) {
1524 v4l2_std_id std = 0;
1526 decoder_call(zr, video, querystd, &std);
1527 decoder_s_std(zr, std);
1529 /* let changes come into effect */
1532 decoder_call(zr, video, g_input_status, &status);
1533 if (status & V4L2_IN_ST_NO_SIGNAL) {
1536 "%s: set_norm() - no norm detected\n",
1539 decoder_s_std(zr, zr->norm);
1545 if (norm & V4L2_STD_SECAM)
1546 zr->timing = zr->card.tvn[2];
1547 else if (norm & V4L2_STD_NTSC)
1548 zr->timing = zr->card.tvn[1];
1550 zr->timing = zr->card.tvn[0];
1552 /* We switch overlay off and on since a change in the
1553 * norm needs different VFE settings */
1554 on = zr->overlay_active && !zr->v4l_memgrab_active;
1556 zr36057_overlay(zr, 0);
1558 decoder_s_std(zr, norm);
1559 encoder_call(zr, video, s_std_output, norm);
1562 zr36057_overlay(zr, 1);
1564 /* Make sure the changes come into effect */
1571 zoran_set_input (struct zoran *zr,
1574 struct v4l2_routing route = { 0, 0 };
1576 if (input == zr->input) {
1580 if (zr->v4l_buffers.active != ZORAN_FREE ||
1581 zr->jpg_buffers.active != ZORAN_FREE) {
1584 "%s: set_input() called while in playback/capture mode\n",
1589 if (input < 0 || input >= zr->card.inputs) {
1592 "%s: set_input() - unnsupported input %d\n",
1593 ZR_DEVNAME(zr), input);
1597 route.input = zr->card.input[input].muxsel;
1600 decoder_s_routing(zr, &route);
1609 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1610 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1612 struct zoran_fh *fh = __fh;
1613 struct zoran *zr = fh->zr;
1614 struct zoran_jpg_settings settings;
1617 case BUZIOC_G_PARAMS:
1619 struct zoran_params *bparams = arg;
1621 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1623 memset(bparams, 0, sizeof(struct zoran_params));
1624 bparams->major_version = MAJOR_VERSION;
1625 bparams->minor_version = MINOR_VERSION;
1627 mutex_lock(&zr->resource_lock);
1629 if (zr->norm & V4L2_STD_NTSC)
1630 bparams->norm = VIDEO_MODE_NTSC;
1631 else if (zr->norm & V4L2_STD_PAL)
1632 bparams->norm = VIDEO_MODE_PAL;
1634 bparams->norm = VIDEO_MODE_SECAM;
1636 bparams->input = zr->input;
1638 bparams->decimation = fh->jpg_settings.decimation;
1639 bparams->HorDcm = fh->jpg_settings.HorDcm;
1640 bparams->VerDcm = fh->jpg_settings.VerDcm;
1641 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1642 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1643 bparams->img_x = fh->jpg_settings.img_x;
1644 bparams->img_y = fh->jpg_settings.img_y;
1645 bparams->img_width = fh->jpg_settings.img_width;
1646 bparams->img_height = fh->jpg_settings.img_height;
1647 bparams->odd_even = fh->jpg_settings.odd_even;
1649 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1650 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1651 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1652 memcpy(bparams->APP_data,
1653 fh->jpg_settings.jpg_comp.APP_data,
1654 sizeof(bparams->APP_data));
1655 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1656 memcpy(bparams->COM_data,
1657 fh->jpg_settings.jpg_comp.COM_data,
1658 sizeof(bparams->COM_data));
1659 bparams->jpeg_markers =
1660 fh->jpg_settings.jpg_comp.jpeg_markers;
1662 mutex_unlock(&zr->resource_lock);
1664 bparams->VFIFO_FB = 0;
1669 case BUZIOC_S_PARAMS:
1671 struct zoran_params *bparams = arg;
1674 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1676 settings.decimation = bparams->decimation;
1677 settings.HorDcm = bparams->HorDcm;
1678 settings.VerDcm = bparams->VerDcm;
1679 settings.TmpDcm = bparams->TmpDcm;
1680 settings.field_per_buff = bparams->field_per_buff;
1681 settings.img_x = bparams->img_x;
1682 settings.img_y = bparams->img_y;
1683 settings.img_width = bparams->img_width;
1684 settings.img_height = bparams->img_height;
1685 settings.odd_even = bparams->odd_even;
1687 settings.jpg_comp.quality = bparams->quality;
1688 settings.jpg_comp.APPn = bparams->APPn;
1689 settings.jpg_comp.APP_len = bparams->APP_len;
1690 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1691 sizeof(bparams->APP_data));
1692 settings.jpg_comp.COM_len = bparams->COM_len;
1693 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1694 sizeof(bparams->COM_data));
1695 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1697 mutex_lock(&zr->resource_lock);
1699 if (zr->codec_mode != BUZ_MODE_IDLE) {
1702 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1705 goto sparams_unlock_and_return;
1708 /* Check the params first before overwriting our
1710 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1712 goto sparams_unlock_and_return;
1715 fh->jpg_settings = settings;
1716 sparams_unlock_and_return:
1717 mutex_unlock(&zr->resource_lock);
1722 case BUZIOC_REQBUFS:
1724 struct zoran_requestbuffers *breq = arg;
1729 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1730 ZR_DEVNAME(zr), breq->count, breq->size);
1732 /* Enforce reasonable lower and upper limits */
1733 if (breq->count < 4)
1734 breq->count = 4; /* Could be choosen smaller */
1735 if (breq->count > jpg_nbufs)
1736 breq->count = jpg_nbufs;
1737 breq->size = PAGE_ALIGN(breq->size);
1738 if (breq->size < 8192)
1739 breq->size = 8192; /* Arbitrary */
1740 /* breq->size is limited by 1 page for the stat_com
1741 * tables to a Maximum of 2 MB */
1742 if (breq->size > jpg_bufsize)
1743 breq->size = jpg_bufsize;
1745 mutex_lock(&zr->resource_lock);
1747 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1750 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1753 goto jpgreqbuf_unlock_and_return;
1756 fh->jpg_buffers.num_buffers = breq->count;
1757 fh->jpg_buffers.buffer_size = breq->size;
1759 if (jpg_fbuffer_alloc(file)) {
1761 goto jpgreqbuf_unlock_and_return;
1764 /* The next mmap will map the MJPEG buffers - could
1765 * also be *_PLAY, but it doesn't matter here */
1766 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1767 jpgreqbuf_unlock_and_return:
1768 mutex_unlock(&zr->resource_lock);
1773 case BUZIOC_QBUF_CAPT:
1775 int *frame = arg, res;
1777 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1778 ZR_DEVNAME(zr), *frame);
1780 mutex_lock(&zr->resource_lock);
1781 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1782 mutex_unlock(&zr->resource_lock);
1787 case BUZIOC_QBUF_PLAY:
1789 int *frame = arg, res;
1791 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1792 ZR_DEVNAME(zr), *frame);
1794 mutex_lock(&zr->resource_lock);
1795 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1796 mutex_unlock(&zr->resource_lock);
1803 struct zoran_sync *bsync = arg;
1806 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1808 mutex_lock(&zr->resource_lock);
1809 res = jpg_sync(file, bsync);
1810 mutex_unlock(&zr->resource_lock);
1815 case BUZIOC_G_STATUS:
1817 struct zoran_status *bstat = arg;
1818 struct v4l2_routing route = { 0, 0 };
1819 int status = 0, res = 0;
1822 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1824 if (zr->codec_mode != BUZ_MODE_IDLE) {
1827 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1832 route.input = zr->card.input[bstat->input].muxsel;
1834 mutex_lock(&zr->resource_lock);
1836 if (zr->codec_mode != BUZ_MODE_IDLE) {
1839 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1842 goto gstat_unlock_and_return;
1845 decoder_s_routing(zr, &route);
1847 /* sleep 1 second */
1850 /* Get status of video decoder */
1851 decoder_call(zr, video, querystd, &norm);
1852 decoder_call(zr, video, g_input_status, &status);
1854 /* restore previous input and norm */
1855 route.input = zr->card.input[zr->input].muxsel;
1856 decoder_s_routing(zr, &route);
1857 gstat_unlock_and_return:
1858 mutex_unlock(&zr->resource_lock);
1862 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1863 if (norm & V4L2_STD_NTSC)
1864 bstat->norm = VIDEO_MODE_NTSC;
1865 else if (norm & V4L2_STD_SECAM)
1866 bstat->norm = VIDEO_MODE_SECAM;
1868 bstat->norm = VIDEO_MODE_PAL;
1871 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1882 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1884 struct zoran_fh *fh = __fh;
1885 struct zoran *zr = fh->zr;
1889 fh->v4l_buffers.num_buffers *
1890 fh->v4l_buffers.buffer_size;
1891 vmbuf->frames = fh->v4l_buffers.num_buffers;
1892 for (i = 0; i < vmbuf->frames; i++) {
1894 i * fh->v4l_buffers.buffer_size;
1897 mutex_lock(&zr->resource_lock);
1899 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1902 "%s: VIDIOCGMBUF - buffers already allocated\n",
1905 goto v4l1reqbuf_unlock_and_return;
1908 if (v4l_fbuffer_alloc(file)) {
1910 goto v4l1reqbuf_unlock_and_return;
1913 /* The next mmap will map the V4L buffers */
1914 fh->map_mode = ZORAN_MAP_MODE_RAW;
1915 v4l1reqbuf_unlock_and_return:
1916 mutex_unlock(&zr->resource_lock);
1922 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1924 struct zoran_fh *fh = __fh;
1925 struct zoran *zr = fh->zr;
1927 memset(cap, 0, sizeof(*cap));
1928 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1929 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1930 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1931 pci_name(zr->pci_dev));
1932 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1934 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1935 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1939 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1943 for (i = 0; i < NUM_FORMATS; i++) {
1944 if (zoran_formats[i].flags & flag)
1946 if (num == fmt->index)
1949 if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
1952 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1953 fmt->pixelformat = zoran_formats[i].fourcc;
1954 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1955 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1959 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1960 struct v4l2_fmtdesc *f)
1962 struct zoran_fh *fh = __fh;
1963 struct zoran *zr = fh->zr;
1965 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1968 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1969 struct v4l2_fmtdesc *f)
1971 struct zoran_fh *fh = __fh;
1972 struct zoran *zr = fh->zr;
1974 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1977 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1978 struct v4l2_fmtdesc *f)
1980 struct zoran_fh *fh = __fh;
1981 struct zoran *zr = fh->zr;
1983 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1986 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1987 struct v4l2_format *fmt)
1989 struct zoran_fh *fh = __fh;
1990 struct zoran *zr = fh->zr;
1992 mutex_lock(&zr->resource_lock);
1994 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1995 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1996 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1997 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1998 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1999 if (fh->jpg_settings.TmpDcm == 1)
2000 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2001 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2003 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2004 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2005 fmt->fmt.pix.bytesperline = 0;
2006 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2008 mutex_unlock(&zr->resource_lock);
2012 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2013 struct v4l2_format *fmt)
2015 struct zoran_fh *fh = __fh;
2016 struct zoran *zr = fh->zr;
2018 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2019 return zoran_g_fmt_vid_out(file, fh, fmt);
2021 mutex_lock(&zr->resource_lock);
2022 fmt->fmt.pix.width = fh->v4l_settings.width;
2023 fmt->fmt.pix.height = fh->v4l_settings.height;
2024 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2025 fh->v4l_settings.height;
2026 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2027 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2028 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2029 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2030 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2032 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2033 mutex_unlock(&zr->resource_lock);
2037 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2038 struct v4l2_format *fmt)
2040 struct zoran_fh *fh = __fh;
2041 struct zoran *zr = fh->zr;
2043 mutex_lock(&zr->resource_lock);
2045 fmt->fmt.win.w.left = fh->overlay_settings.x;
2046 fmt->fmt.win.w.top = fh->overlay_settings.y;
2047 fmt->fmt.win.w.width = fh->overlay_settings.width;
2048 fmt->fmt.win.w.height = fh->overlay_settings.height;
2049 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2050 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2052 fmt->fmt.win.field = V4L2_FIELD_TOP;
2054 mutex_unlock(&zr->resource_lock);
2058 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2059 struct v4l2_format *fmt)
2061 struct zoran_fh *fh = __fh;
2062 struct zoran *zr = fh->zr;
2064 mutex_lock(&zr->resource_lock);
2066 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2067 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2068 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2069 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2070 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2071 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2072 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2073 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2075 mutex_unlock(&zr->resource_lock);
2079 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2080 struct v4l2_format *fmt)
2082 struct zoran_fh *fh = __fh;
2083 struct zoran *zr = fh->zr;
2084 struct zoran_jpg_settings settings;
2087 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2090 mutex_lock(&zr->resource_lock);
2091 settings = fh->jpg_settings;
2093 /* we actually need to set 'real' parameters now */
2094 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2095 settings.TmpDcm = 1;
2097 settings.TmpDcm = 2;
2098 settings.decimation = 0;
2099 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2100 settings.VerDcm = 2;
2102 settings.VerDcm = 1;
2103 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2104 settings.HorDcm = 4;
2105 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2106 settings.HorDcm = 2;
2108 settings.HorDcm = 1;
2109 if (settings.TmpDcm == 1)
2110 settings.field_per_buff = 2;
2112 settings.field_per_buff = 1;
2114 if (settings.HorDcm > 1) {
2115 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2116 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2119 settings.img_width = BUZ_MAX_WIDTH;
2123 res = zoran_check_jpg_settings(zr, &settings, 1);
2125 goto tryfmt_unlock_and_return;
2127 /* tell the user what we actually did */
2128 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2129 fmt->fmt.pix.height = settings.img_height * 2 /
2130 (settings.TmpDcm * settings.VerDcm);
2131 if (settings.TmpDcm == 1)
2132 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2133 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2135 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2136 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2138 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2139 fmt->fmt.pix.bytesperline = 0;
2140 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2141 tryfmt_unlock_and_return:
2142 mutex_unlock(&zr->resource_lock);
2146 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2147 struct v4l2_format *fmt)
2149 struct zoran_fh *fh = __fh;
2150 struct zoran *zr = fh->zr;
2154 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2155 return zoran_try_fmt_vid_out(file, fh, fmt);
2157 mutex_lock(&zr->resource_lock);
2159 for (i = 0; i < NUM_FORMATS; i++)
2160 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2163 if (i == NUM_FORMATS) {
2164 mutex_unlock(&zr->resource_lock);
2168 bpp = (zoran_formats[i].depth + 7) / 8;
2169 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2170 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2171 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2172 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2173 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2174 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2175 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2176 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2177 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2178 mutex_unlock(&zr->resource_lock);
2183 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2184 struct v4l2_format *fmt)
2186 struct zoran_fh *fh = __fh;
2187 struct zoran *zr = fh->zr;
2190 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2191 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2192 fmt->fmt.win.w.width,
2193 fmt->fmt.win.w.height,
2194 fmt->fmt.win.clipcount,
2195 fmt->fmt.win.bitmap);
2196 mutex_lock(&zr->resource_lock);
2197 res = setup_window(file, fmt->fmt.win.w.left,
2199 fmt->fmt.win.w.width,
2200 fmt->fmt.win.w.height,
2201 (struct v4l2_clip __user *)
2203 fmt->fmt.win.clipcount,
2204 fmt->fmt.win.bitmap);
2205 mutex_unlock(&zr->resource_lock);
2209 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2210 struct v4l2_format *fmt)
2212 struct zoran_fh *fh = __fh;
2213 struct zoran *zr = fh->zr;
2214 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2215 struct zoran_jpg_settings settings;
2218 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2219 fmt->fmt.pix.width, fmt->fmt.pix.height,
2220 fmt->fmt.pix.pixelformat,
2221 (char *) &printformat);
2222 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2225 mutex_lock(&zr->resource_lock);
2227 settings = fh->jpg_settings;
2229 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2230 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2233 goto sfmtjpg_unlock_and_return;
2236 /* we actually need to set 'real' parameters now */
2237 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2238 settings.TmpDcm = 1;
2240 settings.TmpDcm = 2;
2241 settings.decimation = 0;
2242 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2243 settings.VerDcm = 2;
2245 settings.VerDcm = 1;
2246 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2247 settings.HorDcm = 4;
2248 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2249 settings.HorDcm = 2;
2251 settings.HorDcm = 1;
2252 if (settings.TmpDcm == 1)
2253 settings.field_per_buff = 2;
2255 settings.field_per_buff = 1;
2257 if (settings.HorDcm > 1) {
2258 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2259 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2262 settings.img_width = BUZ_MAX_WIDTH;
2266 res = zoran_check_jpg_settings(zr, &settings, 0);
2268 goto sfmtjpg_unlock_and_return;
2270 /* it's ok, so set them */
2271 fh->jpg_settings = settings;
2273 /* tell the user what we actually did */
2274 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2275 fmt->fmt.pix.height = settings.img_height * 2 /
2276 (settings.TmpDcm * settings.VerDcm);
2277 if (settings.TmpDcm == 1)
2278 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2279 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2281 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2282 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2283 fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2284 fmt->fmt.pix.bytesperline = 0;
2285 fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2286 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2288 /* we hereby abuse this variable to show that
2289 * we're gonna do mjpeg capture */
2290 fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2291 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2292 sfmtjpg_unlock_and_return:
2293 mutex_unlock(&zr->resource_lock);
2297 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2298 struct v4l2_format *fmt)
2300 struct zoran_fh *fh = __fh;
2301 struct zoran *zr = fh->zr;
2305 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2306 return zoran_s_fmt_vid_out(file, fh, fmt);
2308 for (i = 0; i < NUM_FORMATS; i++)
2309 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2311 if (i == NUM_FORMATS) {
2312 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2313 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2316 mutex_lock(&zr->resource_lock);
2317 if (fh->jpg_buffers.allocated ||
2318 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2319 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2322 goto sfmtv4l_unlock_and_return;
2324 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2325 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2326 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2327 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2329 res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2330 fmt->fmt.pix.height, &zoran_formats[i]);
2332 goto sfmtv4l_unlock_and_return;
2334 /* tell the user the
2335 * results/missing stuff */
2336 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2337 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2338 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2339 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2340 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2342 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2344 fh->map_mode = ZORAN_MAP_MODE_RAW;
2345 sfmtv4l_unlock_and_return:
2346 mutex_unlock(&zr->resource_lock);
2350 static int zoran_g_fbuf(struct file *file, void *__fh,
2351 struct v4l2_framebuffer *fb)
2353 struct zoran_fh *fh = __fh;
2354 struct zoran *zr = fh->zr;
2356 memset(fb, 0, sizeof(*fb));
2357 mutex_lock(&zr->resource_lock);
2358 fb->base = zr->vbuf_base;
2359 fb->fmt.width = zr->vbuf_width;
2360 fb->fmt.height = zr->vbuf_height;
2361 if (zr->overlay_settings.format)
2362 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2363 fb->fmt.bytesperline = zr->vbuf_bytesperline;
2364 mutex_unlock(&zr->resource_lock);
2365 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2366 fb->fmt.field = V4L2_FIELD_INTERLACED;
2367 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2368 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2373 static int zoran_s_fbuf(struct file *file, void *__fh,
2374 struct v4l2_framebuffer *fb)
2376 struct zoran_fh *fh = __fh;
2377 struct zoran *zr = fh->zr;
2379 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2381 for (i = 0; i < NUM_FORMATS; i++)
2382 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2384 if (i == NUM_FORMATS) {
2385 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2386 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2387 (char *)&printformat);
2391 mutex_lock(&zr->resource_lock);
2392 res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2393 fb->fmt.width, fb->fmt.height,
2394 fb->fmt.bytesperline);
2395 mutex_unlock(&zr->resource_lock);
2400 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2402 struct zoran_fh *fh = __fh;
2403 struct zoran *zr = fh->zr;
2406 mutex_lock(&zr->resource_lock);
2407 res = setup_overlay(file, on);
2408 mutex_unlock(&zr->resource_lock);
2413 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2415 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2417 struct zoran_fh *fh = __fh;
2418 struct zoran *zr = fh->zr;
2421 if (req->memory != V4L2_MEMORY_MMAP) {
2424 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2425 ZR_DEVNAME(zr), req->memory);
2429 if (req->count == 0)
2430 return zoran_streamoff(file, fh, req->type);
2432 mutex_lock(&zr->resource_lock);
2433 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2436 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2439 goto v4l2reqbuf_unlock_and_return;
2442 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2443 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2445 /* control user input */
2448 if (req->count > v4l_nbufs)
2449 req->count = v4l_nbufs;
2450 fh->v4l_buffers.num_buffers = req->count;
2452 if (v4l_fbuffer_alloc(file)) {
2454 goto v4l2reqbuf_unlock_and_return;
2457 /* The next mmap will map the V4L buffers */
2458 fh->map_mode = ZORAN_MAP_MODE_RAW;
2460 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2461 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2463 /* we need to calculate size ourselves now */
2466 if (req->count > jpg_nbufs)
2467 req->count = jpg_nbufs;
2468 fh->jpg_buffers.num_buffers = req->count;
2469 fh->jpg_buffers.buffer_size =
2470 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2472 if (jpg_fbuffer_alloc(file)) {
2474 goto v4l2reqbuf_unlock_and_return;
2477 /* The next mmap will map the MJPEG buffers */
2478 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2479 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2481 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2486 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2487 ZR_DEVNAME(zr), req->type);
2489 goto v4l2reqbuf_unlock_and_return;
2491 v4l2reqbuf_unlock_and_return:
2492 mutex_unlock(&zr->resource_lock);
2497 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2499 struct zoran_fh *fh = __fh;
2500 struct zoran *zr = fh->zr;
2503 mutex_lock(&zr->resource_lock);
2504 res = zoran_v4l2_buffer_status(file, buf, buf->index);
2505 mutex_unlock(&zr->resource_lock);
2510 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2512 struct zoran_fh *fh = __fh;
2513 struct zoran *zr = fh->zr;
2514 int res = 0, codec_mode, buf_type;
2516 mutex_lock(&zr->resource_lock);
2518 switch (fh->map_mode) {
2519 case ZORAN_MAP_MODE_RAW:
2520 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2522 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2523 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2525 goto qbuf_unlock_and_return;
2528 res = zoran_v4l_queue_frame(file, buf->index);
2530 goto qbuf_unlock_and_return;
2531 if (!zr->v4l_memgrab_active &&
2532 fh->v4l_buffers.active == ZORAN_LOCKED)
2533 zr36057_set_memgrab(zr, 1);
2536 case ZORAN_MAP_MODE_JPG_REC:
2537 case ZORAN_MAP_MODE_JPG_PLAY:
2538 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2539 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2540 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2542 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2543 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2546 if (buf->type != buf_type) {
2548 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2549 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2551 goto qbuf_unlock_and_return;
2554 res = zoran_jpg_queue_frame(file, buf->index,
2557 goto qbuf_unlock_and_return;
2558 if (zr->codec_mode == BUZ_MODE_IDLE &&
2559 fh->jpg_buffers.active == ZORAN_LOCKED) {
2560 zr36057_enable_jpg(zr, codec_mode);
2566 "%s: VIDIOC_QBUF - unsupported type %d\n",
2567 ZR_DEVNAME(zr), buf->type);
2571 qbuf_unlock_and_return:
2572 mutex_unlock(&zr->resource_lock);
2577 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2579 struct zoran_fh *fh = __fh;
2580 struct zoran *zr = fh->zr;
2581 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2583 mutex_lock(&zr->resource_lock);
2585 switch (fh->map_mode) {
2586 case ZORAN_MAP_MODE_RAW:
2587 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2589 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2590 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2592 goto dqbuf_unlock_and_return;
2595 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2596 if (file->f_flags & O_NONBLOCK &&
2597 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2599 goto dqbuf_unlock_and_return;
2601 res = v4l_sync(file, num);
2603 goto dqbuf_unlock_and_return;
2604 zr->v4l_sync_tail++;
2605 res = zoran_v4l2_buffer_status(file, buf, num);
2608 case ZORAN_MAP_MODE_JPG_REC:
2609 case ZORAN_MAP_MODE_JPG_PLAY:
2611 struct zoran_sync bs;
2613 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2614 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2616 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2618 if (buf->type != buf_type) {
2620 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2621 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2623 goto dqbuf_unlock_and_return;
2626 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2628 if (file->f_flags & O_NONBLOCK &&
2629 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2631 goto dqbuf_unlock_and_return;
2633 res = jpg_sync(file, &bs);
2635 goto dqbuf_unlock_and_return;
2636 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2642 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2643 ZR_DEVNAME(zr), buf->type);
2647 dqbuf_unlock_and_return:
2648 mutex_unlock(&zr->resource_lock);
2653 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2655 struct zoran_fh *fh = __fh;
2656 struct zoran *zr = fh->zr;
2659 mutex_lock(&zr->resource_lock);
2661 switch (fh->map_mode) {
2662 case ZORAN_MAP_MODE_RAW: /* raw capture */
2663 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2664 fh->v4l_buffers.active != ZORAN_ACTIVE) {
2666 goto strmon_unlock_and_return;
2669 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2670 zr->v4l_settings = fh->v4l_settings;
2672 zr->v4l_sync_tail = zr->v4l_pend_tail;
2673 if (!zr->v4l_memgrab_active &&
2674 zr->v4l_pend_head != zr->v4l_pend_tail) {
2675 zr36057_set_memgrab(zr, 1);
2679 case ZORAN_MAP_MODE_JPG_REC:
2680 case ZORAN_MAP_MODE_JPG_PLAY:
2681 /* what is the codec mode right now? */
2682 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2683 fh->jpg_buffers.active != ZORAN_ACTIVE) {
2685 goto strmon_unlock_and_return;
2688 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2690 if (zr->jpg_que_head != zr->jpg_que_tail) {
2691 /* Start the jpeg codec when the first frame is queued */
2699 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2700 ZR_DEVNAME(zr), fh->map_mode);
2704 strmon_unlock_and_return:
2705 mutex_unlock(&zr->resource_lock);
2710 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2712 struct zoran_fh *fh = __fh;
2713 struct zoran *zr = fh->zr;
2716 mutex_lock(&zr->resource_lock);
2718 switch (fh->map_mode) {
2719 case ZORAN_MAP_MODE_RAW: /* raw capture */
2720 if (fh->v4l_buffers.active == ZORAN_FREE &&
2721 zr->v4l_buffers.active != ZORAN_FREE) {
2722 res = -EPERM; /* stay off other's settings! */
2723 goto strmoff_unlock_and_return;
2725 if (zr->v4l_buffers.active == ZORAN_FREE)
2726 goto strmoff_unlock_and_return;
2728 /* unload capture */
2729 if (zr->v4l_memgrab_active) {
2730 unsigned long flags;
2732 spin_lock_irqsave(&zr->spinlock, flags);
2733 zr36057_set_memgrab(zr, 0);
2734 spin_unlock_irqrestore(&zr->spinlock, flags);
2737 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2738 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2739 fh->v4l_buffers = zr->v4l_buffers;
2741 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2743 zr->v4l_grab_seq = 0;
2744 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2745 zr->v4l_sync_tail = 0;
2749 case ZORAN_MAP_MODE_JPG_REC:
2750 case ZORAN_MAP_MODE_JPG_PLAY:
2751 if (fh->jpg_buffers.active == ZORAN_FREE &&
2752 zr->jpg_buffers.active != ZORAN_FREE) {
2753 res = -EPERM; /* stay off other's settings! */
2754 goto strmoff_unlock_and_return;
2756 if (zr->jpg_buffers.active == ZORAN_FREE)
2757 goto strmoff_unlock_and_return;
2759 res = jpg_qbuf(file, -1,
2760 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2761 BUZ_MODE_MOTION_COMPRESS :
2762 BUZ_MODE_MOTION_DECOMPRESS);
2764 goto strmoff_unlock_and_return;
2768 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2769 ZR_DEVNAME(zr), fh->map_mode);
2773 strmoff_unlock_and_return:
2774 mutex_unlock(&zr->resource_lock);
2779 static int zoran_queryctrl(struct file *file, void *__fh,
2780 struct v4l2_queryctrl *ctrl)
2782 struct zoran_fh *fh = __fh;
2783 struct zoran *zr = fh->zr;
2785 /* we only support hue/saturation/contrast/brightness */
2786 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2787 ctrl->id > V4L2_CID_HUE)
2790 decoder_call(zr, core, queryctrl, ctrl);
2795 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2797 struct zoran_fh *fh = __fh;
2798 struct zoran *zr = fh->zr;
2800 /* we only support hue/saturation/contrast/brightness */
2801 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2802 ctrl->id > V4L2_CID_HUE)
2805 mutex_lock(&zr->resource_lock);
2806 decoder_call(zr, core, g_ctrl, ctrl);
2807 mutex_unlock(&zr->resource_lock);
2812 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2814 struct zoran_fh *fh = __fh;
2815 struct zoran *zr = fh->zr;
2817 /* we only support hue/saturation/contrast/brightness */
2818 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2819 ctrl->id > V4L2_CID_HUE)
2822 mutex_lock(&zr->resource_lock);
2823 decoder_call(zr, core, s_ctrl, ctrl);
2824 mutex_unlock(&zr->resource_lock);
2829 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2831 struct zoran_fh *fh = __fh;
2832 struct zoran *zr = fh->zr;
2834 mutex_lock(&zr->resource_lock);
2836 mutex_unlock(&zr->resource_lock);
2840 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2842 struct zoran_fh *fh = __fh;
2843 struct zoran *zr = fh->zr;
2846 mutex_lock(&zr->resource_lock);
2847 res = zoran_set_norm(zr, *std);
2849 goto sstd_unlock_and_return;
2851 res = wait_grab_pending(zr);
2852 sstd_unlock_and_return:
2853 mutex_unlock(&zr->resource_lock);
2857 static int zoran_enum_input(struct file *file, void *__fh,
2858 struct v4l2_input *inp)
2860 struct zoran_fh *fh = __fh;
2861 struct zoran *zr = fh->zr;
2863 if (inp->index < 0 || inp->index >= zr->card.inputs)
2866 int id = inp->index;
2867 memset(inp, 0, sizeof(*inp));
2871 strncpy(inp->name, zr->card.input[inp->index].name,
2872 sizeof(inp->name) - 1);
2873 inp->type = V4L2_INPUT_TYPE_CAMERA;
2874 inp->std = V4L2_STD_ALL;
2876 /* Get status of video decoder */
2877 mutex_lock(&zr->resource_lock);
2878 decoder_call(zr, video, g_input_status, &inp->status);
2879 mutex_unlock(&zr->resource_lock);
2883 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2885 struct zoran_fh *fh = __fh;
2886 struct zoran *zr = fh->zr;
2888 mutex_lock(&zr->resource_lock);
2890 mutex_unlock(&zr->resource_lock);
2895 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2897 struct zoran_fh *fh = __fh;
2898 struct zoran *zr = fh->zr;
2901 mutex_lock(&zr->resource_lock);
2902 res = zoran_set_input(zr, input);
2904 goto sinput_unlock_and_return;
2906 /* Make sure the changes come into effect */
2907 res = wait_grab_pending(zr);
2908 sinput_unlock_and_return:
2909 mutex_unlock(&zr->resource_lock);
2913 static int zoran_enum_output(struct file *file, void *__fh,
2914 struct v4l2_output *outp)
2916 if (outp->index != 0)
2919 memset(outp, 0, sizeof(*outp));
2921 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2922 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2927 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2934 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2942 /* cropping (sub-frame capture) */
2943 static int zoran_cropcap(struct file *file, void *__fh,
2944 struct v4l2_cropcap *cropcap)
2946 struct zoran_fh *fh = __fh;
2947 struct zoran *zr = fh->zr;
2948 int type = cropcap->type, res = 0;
2950 memset(cropcap, 0, sizeof(*cropcap));
2951 cropcap->type = type;
2953 mutex_lock(&zr->resource_lock);
2955 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2956 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2957 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2959 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2962 goto cropcap_unlock_and_return;
2965 cropcap->bounds.top = cropcap->bounds.left = 0;
2966 cropcap->bounds.width = BUZ_MAX_WIDTH;
2967 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2968 cropcap->defrect.top = cropcap->defrect.left = 0;
2969 cropcap->defrect.width = BUZ_MIN_WIDTH;
2970 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2971 cropcap_unlock_and_return:
2972 mutex_unlock(&zr->resource_lock);
2976 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2978 struct zoran_fh *fh = __fh;
2979 struct zoran *zr = fh->zr;
2980 int type = crop->type, res = 0;
2982 memset(crop, 0, sizeof(*crop));
2985 mutex_lock(&zr->resource_lock);
2987 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2988 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2989 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2992 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2995 goto gcrop_unlock_and_return;
2998 crop->c.top = fh->jpg_settings.img_y;
2999 crop->c.left = fh->jpg_settings.img_x;
3000 crop->c.width = fh->jpg_settings.img_width;
3001 crop->c.height = fh->jpg_settings.img_height;
3003 gcrop_unlock_and_return:
3004 mutex_unlock(&zr->resource_lock);
3009 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3011 struct zoran_fh *fh = __fh;
3012 struct zoran *zr = fh->zr;
3014 struct zoran_jpg_settings settings;
3016 settings = fh->jpg_settings;
3018 mutex_lock(&zr->resource_lock);
3020 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3022 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3025 goto scrop_unlock_and_return;
3028 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3029 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3030 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3032 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3035 goto scrop_unlock_and_return;
3038 /* move into a form that we understand */
3039 settings.img_x = crop->c.left;
3040 settings.img_y = crop->c.top;
3041 settings.img_width = crop->c.width;
3042 settings.img_height = crop->c.height;
3044 /* check validity */
3045 res = zoran_check_jpg_settings(zr, &settings, 0);
3047 goto scrop_unlock_and_return;
3050 fh->jpg_settings = settings;
3052 scrop_unlock_and_return:
3053 mutex_unlock(&zr->resource_lock);
3057 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3058 struct v4l2_jpegcompression *params)
3060 struct zoran_fh *fh = __fh;
3061 struct zoran *zr = fh->zr;
3062 memset(params, 0, sizeof(*params));
3064 mutex_lock(&zr->resource_lock);
3066 params->quality = fh->jpg_settings.jpg_comp.quality;
3067 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3068 memcpy(params->APP_data,
3069 fh->jpg_settings.jpg_comp.APP_data,
3070 fh->jpg_settings.jpg_comp.APP_len);
3071 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3072 memcpy(params->COM_data,
3073 fh->jpg_settings.jpg_comp.COM_data,
3074 fh->jpg_settings.jpg_comp.COM_len);
3075 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3076 params->jpeg_markers =
3077 fh->jpg_settings.jpg_comp.jpeg_markers;
3079 mutex_unlock(&zr->resource_lock);
3084 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3085 struct v4l2_jpegcompression *params)
3087 struct zoran_fh *fh = __fh;
3088 struct zoran *zr = fh->zr;
3090 struct zoran_jpg_settings settings;
3092 settings = fh->jpg_settings;
3094 settings.jpg_comp = *params;
3096 mutex_lock(&zr->resource_lock);
3098 if (fh->v4l_buffers.active != ZORAN_FREE ||
3099 fh->jpg_buffers.active != ZORAN_FREE) {
3100 dprintk(1, KERN_WARNING
3101 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3104 goto sjpegc_unlock_and_return;
3107 res = zoran_check_jpg_settings(zr, &settings, 0);
3109 goto sjpegc_unlock_and_return;
3110 if (!fh->jpg_buffers.allocated)
3111 fh->jpg_buffers.buffer_size =
3112 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3113 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3114 sjpegc_unlock_and_return:
3115 mutex_unlock(&zr->resource_lock);
3121 zoran_poll (struct file *file,
3124 struct zoran_fh *fh = file->private_data;
3125 struct zoran *zr = fh->zr;
3127 unsigned long flags;
3129 /* we should check whether buffers are ready to be synced on
3130 * (w/o waits - O_NONBLOCK) here
3131 * if ready for read (sync), return POLLIN|POLLRDNORM,
3132 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3133 * if error, return POLLERR,
3134 * if no buffers queued or so, return POLLNVAL
3137 mutex_lock(&zr->resource_lock);
3139 switch (fh->map_mode) {
3140 case ZORAN_MAP_MODE_RAW:
3141 poll_wait(file, &zr->v4l_capq, wait);
3142 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3144 spin_lock_irqsave(&zr->spinlock, flags);
3147 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3148 ZR_DEVNAME(zr), __func__,
3149 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3150 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3151 zr->v4l_pend_tail, zr->v4l_pend_head);
3152 /* Process is the one capturing? */
3153 if (fh->v4l_buffers.active != ZORAN_FREE &&
3154 /* Buffer ready to DQBUF? */
3155 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3156 res = POLLIN | POLLRDNORM;
3157 spin_unlock_irqrestore(&zr->spinlock, flags);
3161 case ZORAN_MAP_MODE_JPG_REC:
3162 case ZORAN_MAP_MODE_JPG_PLAY:
3163 poll_wait(file, &zr->jpg_capq, wait);
3164 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3166 spin_lock_irqsave(&zr->spinlock, flags);
3169 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3170 ZR_DEVNAME(zr), __func__,
3171 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3172 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3173 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3174 if (fh->jpg_buffers.active != ZORAN_FREE &&
3175 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3176 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3177 res = POLLIN | POLLRDNORM;
3179 res = POLLOUT | POLLWRNORM;
3181 spin_unlock_irqrestore(&zr->spinlock, flags);
3188 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3189 ZR_DEVNAME(zr), fh->map_mode);
3193 mutex_unlock(&zr->resource_lock);
3200 * This maps the buffers to user space.
3202 * Depending on the state of fh->map_mode
3203 * the V4L or the MJPEG buffers are mapped
3204 * per buffer or all together
3206 * Note that we need to connect to some
3207 * unmap signal event to unmap the de-allocate
3208 * the buffer accordingly (zoran_vm_close())
3212 zoran_vm_open (struct vm_area_struct *vma)
3214 struct zoran_mapping *map = vma->vm_private_data;
3220 zoran_vm_close (struct vm_area_struct *vma)
3222 struct zoran_mapping *map = vma->vm_private_data;
3223 struct file *file = map->file;
3224 struct zoran_fh *fh = file->private_data;
3225 struct zoran *zr = fh->zr;
3229 if (map->count == 0) {
3230 switch (fh->map_mode) {
3231 case ZORAN_MAP_MODE_JPG_REC:
3232 case ZORAN_MAP_MODE_JPG_PLAY:
3234 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3237 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3238 if (fh->jpg_buffers.buffer[i].map == map) {
3239 fh->jpg_buffers.buffer[i].map =
3245 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3246 if (fh->jpg_buffers.buffer[i].map)
3248 if (i == fh->jpg_buffers.num_buffers) {
3249 mutex_lock(&zr->resource_lock);
3251 if (fh->jpg_buffers.active != ZORAN_FREE) {
3252 jpg_qbuf(file, -1, zr->codec_mode);
3253 zr->jpg_buffers.allocated = 0;
3254 zr->jpg_buffers.active =
3255 fh->jpg_buffers.active =
3258 jpg_fbuffer_free(file);
3259 mutex_unlock(&zr->resource_lock);
3264 case ZORAN_MAP_MODE_RAW:
3266 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3269 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3270 if (fh->v4l_buffers.buffer[i].map == map) {
3272 fh->v4l_buffers.buffer[i].map =
3278 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3279 if (fh->v4l_buffers.buffer[i].map)
3281 if (i == fh->v4l_buffers.num_buffers) {
3282 mutex_lock(&zr->resource_lock);
3284 if (fh->v4l_buffers.active != ZORAN_FREE) {
3285 unsigned long flags;
3287 spin_lock_irqsave(&zr->spinlock, flags);
3288 zr36057_set_memgrab(zr, 0);
3289 zr->v4l_buffers.allocated = 0;
3290 zr->v4l_buffers.active =
3291 fh->v4l_buffers.active =
3293 spin_unlock_irqrestore(&zr->spinlock, flags);
3295 v4l_fbuffer_free(file);
3296 mutex_unlock(&zr->resource_lock);
3303 "%s: munmap() - internal error - unknown map mode %d\n",
3304 ZR_DEVNAME(zr), fh->map_mode);
3311 static struct vm_operations_struct zoran_vm_ops = {
3312 .open = zoran_vm_open,
3313 .close = zoran_vm_close,
3317 zoran_mmap (struct file *file,
3318 struct vm_area_struct *vma)
3320 struct zoran_fh *fh = file->private_data;
3321 struct zoran *zr = fh->zr;
3322 unsigned long size = (vma->vm_end - vma->vm_start);
3323 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3325 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3327 struct zoran_mapping *map;
3331 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3333 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3334 vma->vm_start, vma->vm_end, size);
3336 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3337 !(vma->vm_flags & VM_WRITE)) {
3340 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3345 switch (fh->map_mode) {
3347 case ZORAN_MAP_MODE_JPG_REC:
3348 case ZORAN_MAP_MODE_JPG_PLAY:
3351 mutex_lock(&zr->resource_lock);
3353 /* Map the MJPEG buffers */
3354 if (!fh->jpg_buffers.allocated) {
3357 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3360 goto jpg_mmap_unlock_and_return;
3363 first = offset / fh->jpg_buffers.buffer_size;
3364 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3365 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3366 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3367 last < 0 || first >= fh->jpg_buffers.num_buffers ||
3368 last >= fh->jpg_buffers.num_buffers) {
3371 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3372 ZR_DEVNAME(zr), offset, size,
3373 fh->jpg_buffers.buffer_size,
3374 fh->jpg_buffers.num_buffers);
3376 goto jpg_mmap_unlock_and_return;
3378 for (i = first; i <= last; i++) {
3379 if (fh->jpg_buffers.buffer[i].map) {
3382 "%s: mmap(MJPEG) - buffer %d already mapped\n",
3385 goto jpg_mmap_unlock_and_return;
3389 /* map these buffers (v4l_buffers[i]) */
3390 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3393 goto jpg_mmap_unlock_and_return;
3398 vma->vm_ops = &zoran_vm_ops;
3399 vma->vm_flags |= VM_DONTEXPAND;
3400 vma->vm_private_data = map;
3402 for (i = first; i <= last; i++) {
3404 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3407 (le32_to_cpu(fh->jpg_buffers.buffer[i].
3408 frag_tab[2 * j + 1]) & ~1) << 1;
3413 le32_to_cpu(fh->jpg_buffers.
3414 buffer[i].frag_tab[2 * j]);
3415 /* should just be pos on i386 */
3416 page = virt_to_phys(bus_to_virt(pos))
3418 if (remap_pfn_range(vma, start, page,
3419 todo, PAGE_SHARED)) {
3422 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3425 goto jpg_mmap_unlock_and_return;
3431 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3432 frag_tab[2 * j + 1]) & 1)
3433 break; /* was last fragment */
3435 fh->jpg_buffers.buffer[i].map = map;
3440 jpg_mmap_unlock_and_return:
3441 mutex_unlock(&zr->resource_lock);
3445 case ZORAN_MAP_MODE_RAW:
3447 mutex_lock(&zr->resource_lock);
3449 /* Map the V4L buffers */
3450 if (!fh->v4l_buffers.allocated) {
3453 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3456 goto v4l_mmap_unlock_and_return;
3459 first = offset / fh->v4l_buffers.buffer_size;
3460 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3461 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3462 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3463 last < 0 || first >= fh->v4l_buffers.num_buffers ||
3464 last >= fh->v4l_buffers.buffer_size) {
3467 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3468 ZR_DEVNAME(zr), offset, size,
3469 fh->v4l_buffers.buffer_size,
3470 fh->v4l_buffers.num_buffers);
3472 goto v4l_mmap_unlock_and_return;
3474 for (i = first; i <= last; i++) {
3475 if (fh->v4l_buffers.buffer[i].map) {
3478 "%s: mmap(V4L) - buffer %d already mapped\n",
3481 goto v4l_mmap_unlock_and_return;
3485 /* map these buffers (v4l_buffers[i]) */
3486 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3489 goto v4l_mmap_unlock_and_return;
3494 vma->vm_ops = &zoran_vm_ops;
3495 vma->vm_flags |= VM_DONTEXPAND;
3496 vma->vm_private_data = map;
3498 for (i = first; i <= last; i++) {
3500 if (todo > fh->v4l_buffers.buffer_size)
3501 todo = fh->v4l_buffers.buffer_size;
3502 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3503 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3504 todo, PAGE_SHARED)) {
3507 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3510 goto v4l_mmap_unlock_and_return;
3514 fh->v4l_buffers.buffer[i].map = map;
3518 v4l_mmap_unlock_and_return:
3519 mutex_unlock(&zr->resource_lock);
3526 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3527 ZR_DEVNAME(zr), fh->map_mode);
3534 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3535 .vidioc_querycap = zoran_querycap,
3536 .vidioc_cropcap = zoran_cropcap,
3537 .vidioc_s_crop = zoran_s_crop,
3538 .vidioc_g_crop = zoran_g_crop,
3539 .vidioc_enum_input = zoran_enum_input,
3540 .vidioc_g_input = zoran_g_input,
3541 .vidioc_s_input = zoran_s_input,
3542 .vidioc_enum_output = zoran_enum_output,
3543 .vidioc_g_output = zoran_g_output,
3544 .vidioc_s_output = zoran_s_output,
3545 .vidioc_g_fbuf = zoran_g_fbuf,
3546 .vidioc_s_fbuf = zoran_s_fbuf,
3547 .vidioc_g_std = zoran_g_std,
3548 .vidioc_s_std = zoran_s_std,
3549 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3550 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3551 .vidioc_overlay = zoran_overlay,
3552 .vidioc_reqbufs = zoran_reqbufs,
3553 .vidioc_querybuf = zoran_querybuf,
3554 .vidioc_qbuf = zoran_qbuf,
3555 .vidioc_dqbuf = zoran_dqbuf,
3556 .vidioc_streamon = zoran_streamon,
3557 .vidioc_streamoff = zoran_streamoff,
3558 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3559 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3560 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3561 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3562 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3563 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3564 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3565 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3566 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3567 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3568 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3569 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3570 .vidioc_queryctrl = zoran_queryctrl,
3571 .vidioc_s_ctrl = zoran_s_ctrl,
3572 .vidioc_g_ctrl = zoran_g_ctrl,
3573 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3574 .vidioc_default = zoran_default,
3575 .vidiocgmbuf = zoran_vidiocgmbuf,
3579 static const struct v4l2_file_operations zoran_fops = {
3580 .owner = THIS_MODULE,
3582 .release = zoran_close,
3583 .ioctl = video_ioctl2,
3585 .write = zoran_write,
3590 struct video_device zoran_template __devinitdata = {
3592 .fops = &zoran_fops,
3593 .ioctl_ops = &zoran_ioctl_ops,
3594 .release = &zoran_vdev_release,
3595 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,