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/videodev.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 zoran_fh *fh,
1381 struct v4l2_buffer *buf,
1384 struct zoran *zr = fh->zr;
1386 buf->flags = V4L2_BUF_FLAG_MAPPED;
1388 switch (fh->map_mode) {
1389 case ZORAN_MAP_MODE_RAW:
1392 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1393 !fh->v4l_buffers.allocated) {
1396 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1401 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1402 buf->length = fh->v4l_buffers.buffer_size;
1405 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1406 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1407 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1408 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1409 buf->flags |= V4L2_BUF_FLAG_DONE;
1411 fh->v4l_buffers.buffer[num].bs.timestamp;
1413 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1416 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1417 buf->field = V4L2_FIELD_TOP;
1419 buf->field = V4L2_FIELD_INTERLACED;
1423 case ZORAN_MAP_MODE_JPG_REC:
1424 case ZORAN_MAP_MODE_JPG_PLAY:
1427 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1428 !fh->jpg_buffers.allocated) {
1431 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1436 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1437 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1438 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1439 buf->length = fh->jpg_buffers.buffer_size;
1441 /* these variables are only written after frame has been captured */
1442 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1443 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1444 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1446 fh->jpg_buffers.buffer[num].bs.timestamp;
1448 fh->jpg_buffers.buffer[num].bs.length;
1449 buf->flags |= V4L2_BUF_FLAG_DONE;
1451 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1454 /* which fields are these? */
1455 if (fh->jpg_settings.TmpDcm != 1)
1458 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1462 odd_even ? V4L2_FIELD_SEQ_TB :
1471 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1472 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1476 buf->memory = V4L2_MEMORY_MMAP;
1478 buf->m.offset = buf->length * num;
1484 zoran_set_norm (struct zoran *zr,
1489 if (zr->v4l_buffers.active != ZORAN_FREE ||
1490 zr->jpg_buffers.active != ZORAN_FREE) {
1493 "%s: set_norm() called while in playback/capture mode\n",
1498 if (lock_norm && norm != zr->norm) {
1499 if (lock_norm > 1) {
1502 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1508 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1514 if (!(norm & zr->card.norms)) {
1516 KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1517 ZR_DEVNAME(zr), norm);
1521 if (norm == V4L2_STD_ALL) {
1523 v4l2_std_id std = 0;
1525 decoder_call(zr, video, querystd, &std);
1526 decoder_s_std(zr, std);
1528 /* let changes come into effect */
1531 decoder_call(zr, video, g_input_status, &status);
1532 if (status & V4L2_IN_ST_NO_SIGNAL) {
1535 "%s: set_norm() - no norm detected\n",
1538 decoder_s_std(zr, zr->norm);
1544 if (norm & V4L2_STD_SECAM)
1545 zr->timing = zr->card.tvn[2];
1546 else if (norm & V4L2_STD_NTSC)
1547 zr->timing = zr->card.tvn[1];
1549 zr->timing = zr->card.tvn[0];
1551 /* We switch overlay off and on since a change in the
1552 * norm needs different VFE settings */
1553 on = zr->overlay_active && !zr->v4l_memgrab_active;
1555 zr36057_overlay(zr, 0);
1557 decoder_s_std(zr, norm);
1558 encoder_call(zr, video, s_std_output, norm);
1561 zr36057_overlay(zr, 1);
1563 /* Make sure the changes come into effect */
1570 zoran_set_input (struct zoran *zr,
1573 struct v4l2_routing route = { 0, 0 };
1575 if (input == zr->input) {
1579 if (zr->v4l_buffers.active != ZORAN_FREE ||
1580 zr->jpg_buffers.active != ZORAN_FREE) {
1583 "%s: set_input() called while in playback/capture mode\n",
1588 if (input < 0 || input >= zr->card.inputs) {
1591 "%s: set_input() - unnsupported input %d\n",
1592 ZR_DEVNAME(zr), input);
1596 route.input = zr->card.input[input].muxsel;
1599 decoder_s_routing(zr, &route);
1608 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1609 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1611 struct zoran_fh *fh = __fh;
1612 struct zoran *zr = fh->zr;
1613 struct zoran_jpg_settings settings;
1616 case BUZIOC_G_PARAMS:
1618 struct zoran_params *bparams = arg;
1620 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1622 memset(bparams, 0, sizeof(struct zoran_params));
1623 bparams->major_version = MAJOR_VERSION;
1624 bparams->minor_version = MINOR_VERSION;
1626 mutex_lock(&zr->resource_lock);
1628 if (zr->norm & V4L2_STD_NTSC)
1629 bparams->norm = VIDEO_MODE_NTSC;
1630 else if (zr->norm & V4L2_STD_PAL)
1631 bparams->norm = VIDEO_MODE_PAL;
1633 bparams->norm = VIDEO_MODE_SECAM;
1635 bparams->input = zr->input;
1637 bparams->decimation = fh->jpg_settings.decimation;
1638 bparams->HorDcm = fh->jpg_settings.HorDcm;
1639 bparams->VerDcm = fh->jpg_settings.VerDcm;
1640 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1641 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1642 bparams->img_x = fh->jpg_settings.img_x;
1643 bparams->img_y = fh->jpg_settings.img_y;
1644 bparams->img_width = fh->jpg_settings.img_width;
1645 bparams->img_height = fh->jpg_settings.img_height;
1646 bparams->odd_even = fh->jpg_settings.odd_even;
1648 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1649 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1650 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1651 memcpy(bparams->APP_data,
1652 fh->jpg_settings.jpg_comp.APP_data,
1653 sizeof(bparams->APP_data));
1654 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1655 memcpy(bparams->COM_data,
1656 fh->jpg_settings.jpg_comp.COM_data,
1657 sizeof(bparams->COM_data));
1658 bparams->jpeg_markers =
1659 fh->jpg_settings.jpg_comp.jpeg_markers;
1661 mutex_unlock(&zr->resource_lock);
1663 bparams->VFIFO_FB = 0;
1668 case BUZIOC_S_PARAMS:
1670 struct zoran_params *bparams = arg;
1673 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1675 settings.decimation = bparams->decimation;
1676 settings.HorDcm = bparams->HorDcm;
1677 settings.VerDcm = bparams->VerDcm;
1678 settings.TmpDcm = bparams->TmpDcm;
1679 settings.field_per_buff = bparams->field_per_buff;
1680 settings.img_x = bparams->img_x;
1681 settings.img_y = bparams->img_y;
1682 settings.img_width = bparams->img_width;
1683 settings.img_height = bparams->img_height;
1684 settings.odd_even = bparams->odd_even;
1686 settings.jpg_comp.quality = bparams->quality;
1687 settings.jpg_comp.APPn = bparams->APPn;
1688 settings.jpg_comp.APP_len = bparams->APP_len;
1689 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1690 sizeof(bparams->APP_data));
1691 settings.jpg_comp.COM_len = bparams->COM_len;
1692 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1693 sizeof(bparams->COM_data));
1694 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1696 mutex_lock(&zr->resource_lock);
1698 if (zr->codec_mode != BUZ_MODE_IDLE) {
1701 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1704 goto sparams_unlock_and_return;
1707 /* Check the params first before overwriting our
1709 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1711 goto sparams_unlock_and_return;
1714 fh->jpg_settings = settings;
1715 sparams_unlock_and_return:
1716 mutex_unlock(&zr->resource_lock);
1721 case BUZIOC_REQBUFS:
1723 struct zoran_requestbuffers *breq = arg;
1728 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1729 ZR_DEVNAME(zr), breq->count, breq->size);
1731 /* Enforce reasonable lower and upper limits */
1732 if (breq->count < 4)
1733 breq->count = 4; /* Could be choosen smaller */
1734 if (breq->count > jpg_nbufs)
1735 breq->count = jpg_nbufs;
1736 breq->size = PAGE_ALIGN(breq->size);
1737 if (breq->size < 8192)
1738 breq->size = 8192; /* Arbitrary */
1739 /* breq->size is limited by 1 page for the stat_com
1740 * tables to a Maximum of 2 MB */
1741 if (breq->size > jpg_bufsize)
1742 breq->size = jpg_bufsize;
1744 mutex_lock(&zr->resource_lock);
1746 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1749 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1752 goto jpgreqbuf_unlock_and_return;
1755 fh->jpg_buffers.num_buffers = breq->count;
1756 fh->jpg_buffers.buffer_size = breq->size;
1758 if (jpg_fbuffer_alloc(file)) {
1760 goto jpgreqbuf_unlock_and_return;
1763 /* The next mmap will map the MJPEG buffers - could
1764 * also be *_PLAY, but it doesn't matter here */
1765 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1766 jpgreqbuf_unlock_and_return:
1767 mutex_unlock(&zr->resource_lock);
1772 case BUZIOC_QBUF_CAPT:
1774 int *frame = arg, res;
1776 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1777 ZR_DEVNAME(zr), *frame);
1779 mutex_lock(&zr->resource_lock);
1780 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1781 mutex_unlock(&zr->resource_lock);
1786 case BUZIOC_QBUF_PLAY:
1788 int *frame = arg, res;
1790 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1791 ZR_DEVNAME(zr), *frame);
1793 mutex_lock(&zr->resource_lock);
1794 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1795 mutex_unlock(&zr->resource_lock);
1802 struct zoran_sync *bsync = arg;
1805 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1807 mutex_lock(&zr->resource_lock);
1808 res = jpg_sync(file, bsync);
1809 mutex_unlock(&zr->resource_lock);
1814 case BUZIOC_G_STATUS:
1816 struct zoran_status *bstat = arg;
1817 struct v4l2_routing route = { 0, 0 };
1818 int status = 0, res = 0;
1821 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1823 if (zr->codec_mode != BUZ_MODE_IDLE) {
1826 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1831 route.input = zr->card.input[bstat->input].muxsel;
1833 mutex_lock(&zr->resource_lock);
1835 if (zr->codec_mode != BUZ_MODE_IDLE) {
1838 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1841 goto gstat_unlock_and_return;
1844 decoder_s_routing(zr, &route);
1846 /* sleep 1 second */
1849 /* Get status of video decoder */
1850 decoder_call(zr, video, querystd, &norm);
1851 decoder_call(zr, video, g_input_status, &status);
1853 /* restore previous input and norm */
1854 route.input = zr->card.input[zr->input].muxsel;
1855 decoder_s_routing(zr, &route);
1856 gstat_unlock_and_return:
1857 mutex_unlock(&zr->resource_lock);
1861 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1862 if (norm & V4L2_STD_NTSC)
1863 bstat->norm = VIDEO_MODE_NTSC;
1864 else if (norm & V4L2_STD_SECAM)
1865 bstat->norm = VIDEO_MODE_SECAM;
1867 bstat->norm = VIDEO_MODE_PAL;
1870 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1881 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1883 struct zoran_fh *fh = __fh;
1884 struct zoran *zr = fh->zr;
1888 fh->v4l_buffers.num_buffers *
1889 fh->v4l_buffers.buffer_size;
1890 vmbuf->frames = fh->v4l_buffers.num_buffers;
1891 for (i = 0; i < vmbuf->frames; i++) {
1893 i * fh->v4l_buffers.buffer_size;
1896 mutex_lock(&zr->resource_lock);
1898 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1901 "%s: VIDIOCGMBUF - buffers already allocated\n",
1904 goto v4l1reqbuf_unlock_and_return;
1907 if (v4l_fbuffer_alloc(file)) {
1909 goto v4l1reqbuf_unlock_and_return;
1912 /* The next mmap will map the V4L buffers */
1913 fh->map_mode = ZORAN_MAP_MODE_RAW;
1914 v4l1reqbuf_unlock_and_return:
1915 mutex_unlock(&zr->resource_lock);
1921 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1923 struct zoran_fh *fh = __fh;
1924 struct zoran *zr = fh->zr;
1926 memset(cap, 0, sizeof(*cap));
1927 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1928 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1929 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1930 pci_name(zr->pci_dev));
1931 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1933 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1934 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1938 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1942 for (i = 0; i < NUM_FORMATS; i++) {
1943 if (zoran_formats[i].flags & flag)
1945 if (num == fmt->index)
1948 if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
1951 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1952 fmt->pixelformat = zoran_formats[i].fourcc;
1953 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1954 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1958 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1959 struct v4l2_fmtdesc *f)
1961 struct zoran_fh *fh = __fh;
1962 struct zoran *zr = fh->zr;
1964 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1967 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1968 struct v4l2_fmtdesc *f)
1970 struct zoran_fh *fh = __fh;
1971 struct zoran *zr = fh->zr;
1973 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1976 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1977 struct v4l2_fmtdesc *f)
1979 struct zoran_fh *fh = __fh;
1980 struct zoran *zr = fh->zr;
1982 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1985 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1986 struct v4l2_format *fmt)
1988 struct zoran_fh *fh = __fh;
1989 struct zoran *zr = fh->zr;
1991 mutex_lock(&zr->resource_lock);
1993 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1994 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1995 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1996 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1997 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1998 if (fh->jpg_settings.TmpDcm == 1)
1999 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2000 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2002 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2003 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2004 fmt->fmt.pix.bytesperline = 0;
2005 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2007 mutex_unlock(&zr->resource_lock);
2011 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2012 struct v4l2_format *fmt)
2014 struct zoran_fh *fh = __fh;
2015 struct zoran *zr = fh->zr;
2017 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2018 return zoran_g_fmt_vid_out(file, fh, fmt);
2020 mutex_lock(&zr->resource_lock);
2021 fmt->fmt.pix.width = fh->v4l_settings.width;
2022 fmt->fmt.pix.height = fh->v4l_settings.height;
2023 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2024 fh->v4l_settings.height;
2025 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2026 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2027 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2028 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2029 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2031 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2032 mutex_unlock(&zr->resource_lock);
2036 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2037 struct v4l2_format *fmt)
2039 struct zoran_fh *fh = __fh;
2040 struct zoran *zr = fh->zr;
2042 mutex_lock(&zr->resource_lock);
2044 fmt->fmt.win.w.left = fh->overlay_settings.x;
2045 fmt->fmt.win.w.top = fh->overlay_settings.y;
2046 fmt->fmt.win.w.width = fh->overlay_settings.width;
2047 fmt->fmt.win.w.height = fh->overlay_settings.height;
2048 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2049 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2051 fmt->fmt.win.field = V4L2_FIELD_TOP;
2053 mutex_unlock(&zr->resource_lock);
2057 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2058 struct v4l2_format *fmt)
2060 struct zoran_fh *fh = __fh;
2061 struct zoran *zr = fh->zr;
2063 mutex_lock(&zr->resource_lock);
2065 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2066 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2067 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2068 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2069 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2070 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2071 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2072 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2074 mutex_unlock(&zr->resource_lock);
2078 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2079 struct v4l2_format *fmt)
2081 struct zoran_fh *fh = __fh;
2082 struct zoran *zr = fh->zr;
2083 struct zoran_jpg_settings settings;
2086 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2089 mutex_lock(&zr->resource_lock);
2090 settings = fh->jpg_settings;
2092 /* we actually need to set 'real' parameters now */
2093 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2094 settings.TmpDcm = 1;
2096 settings.TmpDcm = 2;
2097 settings.decimation = 0;
2098 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2099 settings.VerDcm = 2;
2101 settings.VerDcm = 1;
2102 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2103 settings.HorDcm = 4;
2104 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2105 settings.HorDcm = 2;
2107 settings.HorDcm = 1;
2108 if (settings.TmpDcm == 1)
2109 settings.field_per_buff = 2;
2111 settings.field_per_buff = 1;
2113 if (settings.HorDcm > 1) {
2114 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2115 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2118 settings.img_width = BUZ_MAX_WIDTH;
2122 res = zoran_check_jpg_settings(zr, &settings, 1);
2124 goto tryfmt_unlock_and_return;
2126 /* tell the user what we actually did */
2127 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2128 fmt->fmt.pix.height = settings.img_height * 2 /
2129 (settings.TmpDcm * settings.VerDcm);
2130 if (settings.TmpDcm == 1)
2131 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2132 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2134 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2135 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2137 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2138 fmt->fmt.pix.bytesperline = 0;
2139 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2140 tryfmt_unlock_and_return:
2141 mutex_unlock(&zr->resource_lock);
2145 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2146 struct v4l2_format *fmt)
2148 struct zoran_fh *fh = __fh;
2149 struct zoran *zr = fh->zr;
2153 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2154 return zoran_try_fmt_vid_out(file, fh, fmt);
2156 mutex_lock(&zr->resource_lock);
2158 for (i = 0; i < NUM_FORMATS; i++)
2159 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2162 if (i == NUM_FORMATS) {
2163 mutex_unlock(&zr->resource_lock);
2167 bpp = (zoran_formats[i].depth + 7) / 8;
2168 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2169 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2170 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2171 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2172 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2173 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2174 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2175 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2176 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2177 mutex_unlock(&zr->resource_lock);
2182 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2183 struct v4l2_format *fmt)
2185 struct zoran_fh *fh = __fh;
2186 struct zoran *zr = fh->zr;
2189 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2190 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2191 fmt->fmt.win.w.width,
2192 fmt->fmt.win.w.height,
2193 fmt->fmt.win.clipcount,
2194 fmt->fmt.win.bitmap);
2195 mutex_lock(&zr->resource_lock);
2196 res = setup_window(file, fmt->fmt.win.w.left,
2198 fmt->fmt.win.w.width,
2199 fmt->fmt.win.w.height,
2200 (struct v4l2_clip __user *)
2202 fmt->fmt.win.clipcount,
2203 fmt->fmt.win.bitmap);
2204 mutex_unlock(&zr->resource_lock);
2208 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2209 struct v4l2_format *fmt)
2211 struct zoran_fh *fh = __fh;
2212 struct zoran *zr = fh->zr;
2213 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2214 struct zoran_jpg_settings settings;
2217 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2218 fmt->fmt.pix.width, fmt->fmt.pix.height,
2219 fmt->fmt.pix.pixelformat,
2220 (char *) &printformat);
2221 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2224 mutex_lock(&zr->resource_lock);
2226 settings = fh->jpg_settings;
2228 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2229 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2232 goto sfmtjpg_unlock_and_return;
2235 /* we actually need to set 'real' parameters now */
2236 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2237 settings.TmpDcm = 1;
2239 settings.TmpDcm = 2;
2240 settings.decimation = 0;
2241 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2242 settings.VerDcm = 2;
2244 settings.VerDcm = 1;
2245 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2246 settings.HorDcm = 4;
2247 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2248 settings.HorDcm = 2;
2250 settings.HorDcm = 1;
2251 if (settings.TmpDcm == 1)
2252 settings.field_per_buff = 2;
2254 settings.field_per_buff = 1;
2256 if (settings.HorDcm > 1) {
2257 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2258 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2261 settings.img_width = BUZ_MAX_WIDTH;
2265 res = zoran_check_jpg_settings(zr, &settings, 0);
2267 goto sfmtjpg_unlock_and_return;
2269 /* it's ok, so set them */
2270 fh->jpg_settings = settings;
2272 /* tell the user what we actually did */
2273 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2274 fmt->fmt.pix.height = settings.img_height * 2 /
2275 (settings.TmpDcm * settings.VerDcm);
2276 if (settings.TmpDcm == 1)
2277 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2278 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2280 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2281 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2282 fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2283 fmt->fmt.pix.bytesperline = 0;
2284 fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2285 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2287 /* we hereby abuse this variable to show that
2288 * we're gonna do mjpeg capture */
2289 fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2290 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2291 sfmtjpg_unlock_and_return:
2292 mutex_unlock(&zr->resource_lock);
2296 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2297 struct v4l2_format *fmt)
2299 struct zoran_fh *fh = __fh;
2300 struct zoran *zr = fh->zr;
2304 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2305 return zoran_s_fmt_vid_out(file, fh, fmt);
2307 for (i = 0; i < NUM_FORMATS; i++)
2308 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2310 if (i == NUM_FORMATS) {
2311 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2312 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2315 mutex_lock(&zr->resource_lock);
2316 if (fh->jpg_buffers.allocated ||
2317 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2318 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2321 goto sfmtv4l_unlock_and_return;
2323 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2324 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2325 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2326 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2328 res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2329 fmt->fmt.pix.height, &zoran_formats[i]);
2331 goto sfmtv4l_unlock_and_return;
2333 /* tell the user the
2334 * results/missing stuff */
2335 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2336 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2337 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2338 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2339 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2341 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2343 fh->map_mode = ZORAN_MAP_MODE_RAW;
2344 sfmtv4l_unlock_and_return:
2345 mutex_unlock(&zr->resource_lock);
2349 static int zoran_g_fbuf(struct file *file, void *__fh,
2350 struct v4l2_framebuffer *fb)
2352 struct zoran_fh *fh = __fh;
2353 struct zoran *zr = fh->zr;
2355 memset(fb, 0, sizeof(*fb));
2356 mutex_lock(&zr->resource_lock);
2357 fb->base = zr->vbuf_base;
2358 fb->fmt.width = zr->vbuf_width;
2359 fb->fmt.height = zr->vbuf_height;
2360 if (zr->overlay_settings.format)
2361 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2362 fb->fmt.bytesperline = zr->vbuf_bytesperline;
2363 mutex_unlock(&zr->resource_lock);
2364 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2365 fb->fmt.field = V4L2_FIELD_INTERLACED;
2366 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2367 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2372 static int zoran_s_fbuf(struct file *file, void *__fh,
2373 struct v4l2_framebuffer *fb)
2375 struct zoran_fh *fh = __fh;
2376 struct zoran *zr = fh->zr;
2378 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2380 for (i = 0; i < NUM_FORMATS; i++)
2381 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2383 if (i == NUM_FORMATS) {
2384 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2385 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2386 (char *)&printformat);
2390 mutex_lock(&zr->resource_lock);
2391 res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2392 fb->fmt.width, fb->fmt.height,
2393 fb->fmt.bytesperline);
2394 mutex_unlock(&zr->resource_lock);
2399 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2401 struct zoran_fh *fh = __fh;
2402 struct zoran *zr = fh->zr;
2405 mutex_lock(&zr->resource_lock);
2406 res = setup_overlay(file, on);
2407 mutex_unlock(&zr->resource_lock);
2412 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2414 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2416 struct zoran_fh *fh = __fh;
2417 struct zoran *zr = fh->zr;
2420 if (req->memory != V4L2_MEMORY_MMAP) {
2423 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2424 ZR_DEVNAME(zr), req->memory);
2428 if (req->count == 0)
2429 return zoran_streamoff(file, fh, req->type);
2431 mutex_lock(&zr->resource_lock);
2432 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2435 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2438 goto v4l2reqbuf_unlock_and_return;
2441 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2442 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2444 /* control user input */
2447 if (req->count > v4l_nbufs)
2448 req->count = v4l_nbufs;
2449 fh->v4l_buffers.num_buffers = req->count;
2451 if (v4l_fbuffer_alloc(file)) {
2453 goto v4l2reqbuf_unlock_and_return;
2456 /* The next mmap will map the V4L buffers */
2457 fh->map_mode = ZORAN_MAP_MODE_RAW;
2459 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2460 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2462 /* we need to calculate size ourselves now */
2465 if (req->count > jpg_nbufs)
2466 req->count = jpg_nbufs;
2467 fh->jpg_buffers.num_buffers = req->count;
2468 fh->jpg_buffers.buffer_size =
2469 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2471 if (jpg_fbuffer_alloc(file)) {
2473 goto v4l2reqbuf_unlock_and_return;
2476 /* The next mmap will map the MJPEG buffers */
2477 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2478 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2480 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2485 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2486 ZR_DEVNAME(zr), req->type);
2488 goto v4l2reqbuf_unlock_and_return;
2490 v4l2reqbuf_unlock_and_return:
2491 mutex_unlock(&zr->resource_lock);
2496 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2498 struct zoran_fh *fh = __fh;
2499 struct zoran *zr = fh->zr;
2502 mutex_lock(&zr->resource_lock);
2503 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2504 mutex_unlock(&zr->resource_lock);
2509 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2511 struct zoran_fh *fh = __fh;
2512 struct zoran *zr = fh->zr;
2513 int res = 0, codec_mode, buf_type;
2515 mutex_lock(&zr->resource_lock);
2517 switch (fh->map_mode) {
2518 case ZORAN_MAP_MODE_RAW:
2519 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2521 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2522 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2524 goto qbuf_unlock_and_return;
2527 res = zoran_v4l_queue_frame(file, buf->index);
2529 goto qbuf_unlock_and_return;
2530 if (!zr->v4l_memgrab_active &&
2531 fh->v4l_buffers.active == ZORAN_LOCKED)
2532 zr36057_set_memgrab(zr, 1);
2535 case ZORAN_MAP_MODE_JPG_REC:
2536 case ZORAN_MAP_MODE_JPG_PLAY:
2537 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2538 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2539 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2541 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2542 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2545 if (buf->type != buf_type) {
2547 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2548 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2550 goto qbuf_unlock_and_return;
2553 res = zoran_jpg_queue_frame(file, buf->index,
2556 goto qbuf_unlock_and_return;
2557 if (zr->codec_mode == BUZ_MODE_IDLE &&
2558 fh->jpg_buffers.active == ZORAN_LOCKED) {
2559 zr36057_enable_jpg(zr, codec_mode);
2565 "%s: VIDIOC_QBUF - unsupported type %d\n",
2566 ZR_DEVNAME(zr), buf->type);
2570 qbuf_unlock_and_return:
2571 mutex_unlock(&zr->resource_lock);
2576 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2578 struct zoran_fh *fh = __fh;
2579 struct zoran *zr = fh->zr;
2580 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2582 mutex_lock(&zr->resource_lock);
2584 switch (fh->map_mode) {
2585 case ZORAN_MAP_MODE_RAW:
2586 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2588 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2589 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2591 goto dqbuf_unlock_and_return;
2594 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2595 if (file->f_flags & O_NONBLOCK &&
2596 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2598 goto dqbuf_unlock_and_return;
2600 res = v4l_sync(file, num);
2602 goto dqbuf_unlock_and_return;
2603 zr->v4l_sync_tail++;
2604 res = zoran_v4l2_buffer_status(fh, buf, num);
2607 case ZORAN_MAP_MODE_JPG_REC:
2608 case ZORAN_MAP_MODE_JPG_PLAY:
2610 struct zoran_sync bs;
2612 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2613 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2615 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2617 if (buf->type != buf_type) {
2619 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2620 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2622 goto dqbuf_unlock_and_return;
2625 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2627 if (file->f_flags & O_NONBLOCK &&
2628 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2630 goto dqbuf_unlock_and_return;
2632 res = jpg_sync(file, &bs);
2634 goto dqbuf_unlock_and_return;
2635 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2641 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2642 ZR_DEVNAME(zr), buf->type);
2646 dqbuf_unlock_and_return:
2647 mutex_unlock(&zr->resource_lock);
2652 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2654 struct zoran_fh *fh = __fh;
2655 struct zoran *zr = fh->zr;
2658 mutex_lock(&zr->resource_lock);
2660 switch (fh->map_mode) {
2661 case ZORAN_MAP_MODE_RAW: /* raw capture */
2662 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2663 fh->v4l_buffers.active != ZORAN_ACTIVE) {
2665 goto strmon_unlock_and_return;
2668 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2669 zr->v4l_settings = fh->v4l_settings;
2671 zr->v4l_sync_tail = zr->v4l_pend_tail;
2672 if (!zr->v4l_memgrab_active &&
2673 zr->v4l_pend_head != zr->v4l_pend_tail) {
2674 zr36057_set_memgrab(zr, 1);
2678 case ZORAN_MAP_MODE_JPG_REC:
2679 case ZORAN_MAP_MODE_JPG_PLAY:
2680 /* what is the codec mode right now? */
2681 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2682 fh->jpg_buffers.active != ZORAN_ACTIVE) {
2684 goto strmon_unlock_and_return;
2687 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2689 if (zr->jpg_que_head != zr->jpg_que_tail) {
2690 /* Start the jpeg codec when the first frame is queued */
2698 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2699 ZR_DEVNAME(zr), fh->map_mode);
2703 strmon_unlock_and_return:
2704 mutex_unlock(&zr->resource_lock);
2709 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2711 struct zoran_fh *fh = __fh;
2712 struct zoran *zr = fh->zr;
2715 mutex_lock(&zr->resource_lock);
2717 switch (fh->map_mode) {
2718 case ZORAN_MAP_MODE_RAW: /* raw capture */
2719 if (fh->v4l_buffers.active == ZORAN_FREE &&
2720 zr->v4l_buffers.active != ZORAN_FREE) {
2721 res = -EPERM; /* stay off other's settings! */
2722 goto strmoff_unlock_and_return;
2724 if (zr->v4l_buffers.active == ZORAN_FREE)
2725 goto strmoff_unlock_and_return;
2727 /* unload capture */
2728 if (zr->v4l_memgrab_active) {
2729 unsigned long flags;
2731 spin_lock_irqsave(&zr->spinlock, flags);
2732 zr36057_set_memgrab(zr, 0);
2733 spin_unlock_irqrestore(&zr->spinlock, flags);
2736 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2737 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2738 fh->v4l_buffers = zr->v4l_buffers;
2740 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2742 zr->v4l_grab_seq = 0;
2743 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2744 zr->v4l_sync_tail = 0;
2748 case ZORAN_MAP_MODE_JPG_REC:
2749 case ZORAN_MAP_MODE_JPG_PLAY:
2750 if (fh->jpg_buffers.active == ZORAN_FREE &&
2751 zr->jpg_buffers.active != ZORAN_FREE) {
2752 res = -EPERM; /* stay off other's settings! */
2753 goto strmoff_unlock_and_return;
2755 if (zr->jpg_buffers.active == ZORAN_FREE)
2756 goto strmoff_unlock_and_return;
2758 res = jpg_qbuf(file, -1,
2759 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2760 BUZ_MODE_MOTION_COMPRESS :
2761 BUZ_MODE_MOTION_DECOMPRESS);
2763 goto strmoff_unlock_and_return;
2767 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2768 ZR_DEVNAME(zr), fh->map_mode);
2772 strmoff_unlock_and_return:
2773 mutex_unlock(&zr->resource_lock);
2778 static int zoran_queryctrl(struct file *file, void *__fh,
2779 struct v4l2_queryctrl *ctrl)
2781 struct zoran_fh *fh = __fh;
2782 struct zoran *zr = fh->zr;
2784 /* we only support hue/saturation/contrast/brightness */
2785 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2786 ctrl->id > V4L2_CID_HUE)
2789 decoder_call(zr, core, queryctrl, ctrl);
2794 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2796 struct zoran_fh *fh = __fh;
2797 struct zoran *zr = fh->zr;
2799 /* we only support hue/saturation/contrast/brightness */
2800 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2801 ctrl->id > V4L2_CID_HUE)
2804 mutex_lock(&zr->resource_lock);
2805 decoder_call(zr, core, g_ctrl, ctrl);
2806 mutex_unlock(&zr->resource_lock);
2811 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2813 struct zoran_fh *fh = __fh;
2814 struct zoran *zr = fh->zr;
2816 /* we only support hue/saturation/contrast/brightness */
2817 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2818 ctrl->id > V4L2_CID_HUE)
2821 mutex_lock(&zr->resource_lock);
2822 decoder_call(zr, core, s_ctrl, ctrl);
2823 mutex_unlock(&zr->resource_lock);
2828 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2830 struct zoran_fh *fh = __fh;
2831 struct zoran *zr = fh->zr;
2833 mutex_lock(&zr->resource_lock);
2835 mutex_unlock(&zr->resource_lock);
2839 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2841 struct zoran_fh *fh = __fh;
2842 struct zoran *zr = fh->zr;
2845 mutex_lock(&zr->resource_lock);
2846 res = zoran_set_norm(zr, *std);
2848 goto sstd_unlock_and_return;
2850 res = wait_grab_pending(zr);
2851 sstd_unlock_and_return:
2852 mutex_unlock(&zr->resource_lock);
2856 static int zoran_enum_input(struct file *file, void *__fh,
2857 struct v4l2_input *inp)
2859 struct zoran_fh *fh = __fh;
2860 struct zoran *zr = fh->zr;
2862 if (inp->index < 0 || inp->index >= zr->card.inputs)
2865 int id = inp->index;
2866 memset(inp, 0, sizeof(*inp));
2870 strncpy(inp->name, zr->card.input[inp->index].name,
2871 sizeof(inp->name) - 1);
2872 inp->type = V4L2_INPUT_TYPE_CAMERA;
2873 inp->std = V4L2_STD_ALL;
2875 /* Get status of video decoder */
2876 mutex_lock(&zr->resource_lock);
2877 decoder_call(zr, video, g_input_status, &inp->status);
2878 mutex_unlock(&zr->resource_lock);
2882 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2884 struct zoran_fh *fh = __fh;
2885 struct zoran *zr = fh->zr;
2887 mutex_lock(&zr->resource_lock);
2889 mutex_unlock(&zr->resource_lock);
2894 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2896 struct zoran_fh *fh = __fh;
2897 struct zoran *zr = fh->zr;
2900 mutex_lock(&zr->resource_lock);
2901 res = zoran_set_input(zr, input);
2903 goto sinput_unlock_and_return;
2905 /* Make sure the changes come into effect */
2906 res = wait_grab_pending(zr);
2907 sinput_unlock_and_return:
2908 mutex_unlock(&zr->resource_lock);
2912 static int zoran_enum_output(struct file *file, void *__fh,
2913 struct v4l2_output *outp)
2915 if (outp->index != 0)
2918 memset(outp, 0, sizeof(*outp));
2920 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2921 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2926 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2933 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2941 /* cropping (sub-frame capture) */
2942 static int zoran_cropcap(struct file *file, void *__fh,
2943 struct v4l2_cropcap *cropcap)
2945 struct zoran_fh *fh = __fh;
2946 struct zoran *zr = fh->zr;
2947 int type = cropcap->type, res = 0;
2949 memset(cropcap, 0, sizeof(*cropcap));
2950 cropcap->type = type;
2952 mutex_lock(&zr->resource_lock);
2954 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2955 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2956 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2958 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2961 goto cropcap_unlock_and_return;
2964 cropcap->bounds.top = cropcap->bounds.left = 0;
2965 cropcap->bounds.width = BUZ_MAX_WIDTH;
2966 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2967 cropcap->defrect.top = cropcap->defrect.left = 0;
2968 cropcap->defrect.width = BUZ_MIN_WIDTH;
2969 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2970 cropcap_unlock_and_return:
2971 mutex_unlock(&zr->resource_lock);
2975 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2977 struct zoran_fh *fh = __fh;
2978 struct zoran *zr = fh->zr;
2979 int type = crop->type, res = 0;
2981 memset(crop, 0, sizeof(*crop));
2984 mutex_lock(&zr->resource_lock);
2986 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2987 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2988 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2991 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2994 goto gcrop_unlock_and_return;
2997 crop->c.top = fh->jpg_settings.img_y;
2998 crop->c.left = fh->jpg_settings.img_x;
2999 crop->c.width = fh->jpg_settings.img_width;
3000 crop->c.height = fh->jpg_settings.img_height;
3002 gcrop_unlock_and_return:
3003 mutex_unlock(&zr->resource_lock);
3008 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3010 struct zoran_fh *fh = __fh;
3011 struct zoran *zr = fh->zr;
3013 struct zoran_jpg_settings settings;
3015 settings = fh->jpg_settings;
3017 mutex_lock(&zr->resource_lock);
3019 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3021 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3024 goto scrop_unlock_and_return;
3027 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3028 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3029 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3031 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3034 goto scrop_unlock_and_return;
3037 /* move into a form that we understand */
3038 settings.img_x = crop->c.left;
3039 settings.img_y = crop->c.top;
3040 settings.img_width = crop->c.width;
3041 settings.img_height = crop->c.height;
3043 /* check validity */
3044 res = zoran_check_jpg_settings(zr, &settings, 0);
3046 goto scrop_unlock_and_return;
3049 fh->jpg_settings = settings;
3051 scrop_unlock_and_return:
3052 mutex_unlock(&zr->resource_lock);
3056 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3057 struct v4l2_jpegcompression *params)
3059 struct zoran_fh *fh = __fh;
3060 struct zoran *zr = fh->zr;
3061 memset(params, 0, sizeof(*params));
3063 mutex_lock(&zr->resource_lock);
3065 params->quality = fh->jpg_settings.jpg_comp.quality;
3066 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3067 memcpy(params->APP_data,
3068 fh->jpg_settings.jpg_comp.APP_data,
3069 fh->jpg_settings.jpg_comp.APP_len);
3070 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3071 memcpy(params->COM_data,
3072 fh->jpg_settings.jpg_comp.COM_data,
3073 fh->jpg_settings.jpg_comp.COM_len);
3074 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3075 params->jpeg_markers =
3076 fh->jpg_settings.jpg_comp.jpeg_markers;
3078 mutex_unlock(&zr->resource_lock);
3083 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3084 struct v4l2_jpegcompression *params)
3086 struct zoran_fh *fh = __fh;
3087 struct zoran *zr = fh->zr;
3089 struct zoran_jpg_settings settings;
3091 settings = fh->jpg_settings;
3093 settings.jpg_comp = *params;
3095 mutex_lock(&zr->resource_lock);
3097 if (fh->v4l_buffers.active != ZORAN_FREE ||
3098 fh->jpg_buffers.active != ZORAN_FREE) {
3099 dprintk(1, KERN_WARNING
3100 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3103 goto sjpegc_unlock_and_return;
3106 res = zoran_check_jpg_settings(zr, &settings, 0);
3108 goto sjpegc_unlock_and_return;
3109 if (!fh->jpg_buffers.allocated)
3110 fh->jpg_buffers.buffer_size =
3111 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3112 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3113 sjpegc_unlock_and_return:
3114 mutex_unlock(&zr->resource_lock);
3120 zoran_poll (struct file *file,
3123 struct zoran_fh *fh = file->private_data;
3124 struct zoran *zr = fh->zr;
3126 unsigned long flags;
3128 /* we should check whether buffers are ready to be synced on
3129 * (w/o waits - O_NONBLOCK) here
3130 * if ready for read (sync), return POLLIN|POLLRDNORM,
3131 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3132 * if error, return POLLERR,
3133 * if no buffers queued or so, return POLLNVAL
3136 mutex_lock(&zr->resource_lock);
3138 switch (fh->map_mode) {
3139 case ZORAN_MAP_MODE_RAW:
3140 poll_wait(file, &zr->v4l_capq, wait);
3141 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3143 spin_lock_irqsave(&zr->spinlock, flags);
3146 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3147 ZR_DEVNAME(zr), __func__,
3148 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3149 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3150 zr->v4l_pend_tail, zr->v4l_pend_head);
3151 /* Process is the one capturing? */
3152 if (fh->v4l_buffers.active != ZORAN_FREE &&
3153 /* Buffer ready to DQBUF? */
3154 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3155 res = POLLIN | POLLRDNORM;
3156 spin_unlock_irqrestore(&zr->spinlock, flags);
3160 case ZORAN_MAP_MODE_JPG_REC:
3161 case ZORAN_MAP_MODE_JPG_PLAY:
3162 poll_wait(file, &zr->jpg_capq, wait);
3163 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3165 spin_lock_irqsave(&zr->spinlock, flags);
3168 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3169 ZR_DEVNAME(zr), __func__,
3170 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3171 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3172 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3173 if (fh->jpg_buffers.active != ZORAN_FREE &&
3174 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3175 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3176 res = POLLIN | POLLRDNORM;
3178 res = POLLOUT | POLLWRNORM;
3180 spin_unlock_irqrestore(&zr->spinlock, flags);
3187 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3188 ZR_DEVNAME(zr), fh->map_mode);
3192 mutex_unlock(&zr->resource_lock);
3199 * This maps the buffers to user space.
3201 * Depending on the state of fh->map_mode
3202 * the V4L or the MJPEG buffers are mapped
3203 * per buffer or all together
3205 * Note that we need to connect to some
3206 * unmap signal event to unmap the de-allocate
3207 * the buffer accordingly (zoran_vm_close())
3211 zoran_vm_open (struct vm_area_struct *vma)
3213 struct zoran_mapping *map = vma->vm_private_data;
3219 zoran_vm_close (struct vm_area_struct *vma)
3221 struct zoran_mapping *map = vma->vm_private_data;
3222 struct file *file = map->file;
3223 struct zoran_fh *fh = file->private_data;
3224 struct zoran *zr = fh->zr;
3228 if (map->count == 0) {
3229 switch (fh->map_mode) {
3230 case ZORAN_MAP_MODE_JPG_REC:
3231 case ZORAN_MAP_MODE_JPG_PLAY:
3233 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3236 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3237 if (fh->jpg_buffers.buffer[i].map == map) {
3238 fh->jpg_buffers.buffer[i].map =
3244 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3245 if (fh->jpg_buffers.buffer[i].map)
3247 if (i == fh->jpg_buffers.num_buffers) {
3248 mutex_lock(&zr->resource_lock);
3250 if (fh->jpg_buffers.active != ZORAN_FREE) {
3251 jpg_qbuf(file, -1, zr->codec_mode);
3252 zr->jpg_buffers.allocated = 0;
3253 zr->jpg_buffers.active =
3254 fh->jpg_buffers.active =
3257 jpg_fbuffer_free(file);
3258 mutex_unlock(&zr->resource_lock);
3263 case ZORAN_MAP_MODE_RAW:
3265 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3268 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3269 if (fh->v4l_buffers.buffer[i].map == map) {
3271 fh->v4l_buffers.buffer[i].map =
3277 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3278 if (fh->v4l_buffers.buffer[i].map)
3280 if (i == fh->v4l_buffers.num_buffers) {
3281 mutex_lock(&zr->resource_lock);
3283 if (fh->v4l_buffers.active != ZORAN_FREE) {
3284 unsigned long flags;
3286 spin_lock_irqsave(&zr->spinlock, flags);
3287 zr36057_set_memgrab(zr, 0);
3288 zr->v4l_buffers.allocated = 0;
3289 zr->v4l_buffers.active =
3290 fh->v4l_buffers.active =
3292 spin_unlock_irqrestore(&zr->spinlock, flags);
3294 v4l_fbuffer_free(file);
3295 mutex_unlock(&zr->resource_lock);
3302 "%s: munmap() - internal error - unknown map mode %d\n",
3303 ZR_DEVNAME(zr), fh->map_mode);
3310 static struct vm_operations_struct zoran_vm_ops = {
3311 .open = zoran_vm_open,
3312 .close = zoran_vm_close,
3316 zoran_mmap (struct file *file,
3317 struct vm_area_struct *vma)
3319 struct zoran_fh *fh = file->private_data;
3320 struct zoran *zr = fh->zr;
3321 unsigned long size = (vma->vm_end - vma->vm_start);
3322 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3324 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3326 struct zoran_mapping *map;
3330 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3332 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3333 vma->vm_start, vma->vm_end, size);
3335 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3336 !(vma->vm_flags & VM_WRITE)) {
3339 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3344 switch (fh->map_mode) {
3346 case ZORAN_MAP_MODE_JPG_REC:
3347 case ZORAN_MAP_MODE_JPG_PLAY:
3350 mutex_lock(&zr->resource_lock);
3352 /* Map the MJPEG buffers */
3353 if (!fh->jpg_buffers.allocated) {
3356 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3359 goto jpg_mmap_unlock_and_return;
3362 first = offset / fh->jpg_buffers.buffer_size;
3363 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3364 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3365 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3366 last < 0 || first >= fh->jpg_buffers.num_buffers ||
3367 last >= fh->jpg_buffers.num_buffers) {
3370 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3371 ZR_DEVNAME(zr), offset, size,
3372 fh->jpg_buffers.buffer_size,
3373 fh->jpg_buffers.num_buffers);
3375 goto jpg_mmap_unlock_and_return;
3377 for (i = first; i <= last; i++) {
3378 if (fh->jpg_buffers.buffer[i].map) {
3381 "%s: mmap(MJPEG) - buffer %d already mapped\n",
3384 goto jpg_mmap_unlock_and_return;
3388 /* map these buffers (v4l_buffers[i]) */
3389 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3392 goto jpg_mmap_unlock_and_return;
3397 vma->vm_ops = &zoran_vm_ops;
3398 vma->vm_flags |= VM_DONTEXPAND;
3399 vma->vm_private_data = map;
3401 for (i = first; i <= last; i++) {
3403 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3406 (le32_to_cpu(fh->jpg_buffers.buffer[i].
3407 frag_tab[2 * j + 1]) & ~1) << 1;
3412 le32_to_cpu(fh->jpg_buffers.
3413 buffer[i].frag_tab[2 * j]);
3414 /* should just be pos on i386 */
3415 page = virt_to_phys(bus_to_virt(pos))
3417 if (remap_pfn_range(vma, start, page,
3418 todo, PAGE_SHARED)) {
3421 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3424 goto jpg_mmap_unlock_and_return;
3430 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3431 frag_tab[2 * j + 1]) & 1)
3432 break; /* was last fragment */
3434 fh->jpg_buffers.buffer[i].map = map;
3439 jpg_mmap_unlock_and_return:
3440 mutex_unlock(&zr->resource_lock);
3444 case ZORAN_MAP_MODE_RAW:
3446 mutex_lock(&zr->resource_lock);
3448 /* Map the V4L buffers */
3449 if (!fh->v4l_buffers.allocated) {
3452 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3455 goto v4l_mmap_unlock_and_return;
3458 first = offset / fh->v4l_buffers.buffer_size;
3459 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3460 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3461 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3462 last < 0 || first >= fh->v4l_buffers.num_buffers ||
3463 last >= fh->v4l_buffers.buffer_size) {
3466 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3467 ZR_DEVNAME(zr), offset, size,
3468 fh->v4l_buffers.buffer_size,
3469 fh->v4l_buffers.num_buffers);
3471 goto v4l_mmap_unlock_and_return;
3473 for (i = first; i <= last; i++) {
3474 if (fh->v4l_buffers.buffer[i].map) {
3477 "%s: mmap(V4L) - buffer %d already mapped\n",
3480 goto v4l_mmap_unlock_and_return;
3484 /* map these buffers (v4l_buffers[i]) */
3485 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3488 goto v4l_mmap_unlock_and_return;
3493 vma->vm_ops = &zoran_vm_ops;
3494 vma->vm_flags |= VM_DONTEXPAND;
3495 vma->vm_private_data = map;
3497 for (i = first; i <= last; i++) {
3499 if (todo > fh->v4l_buffers.buffer_size)
3500 todo = fh->v4l_buffers.buffer_size;
3501 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3502 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3503 todo, PAGE_SHARED)) {
3506 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3509 goto v4l_mmap_unlock_and_return;
3513 fh->v4l_buffers.buffer[i].map = map;
3517 v4l_mmap_unlock_and_return:
3518 mutex_unlock(&zr->resource_lock);
3525 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3526 ZR_DEVNAME(zr), fh->map_mode);
3533 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3534 .vidioc_querycap = zoran_querycap,
3535 .vidioc_cropcap = zoran_cropcap,
3536 .vidioc_s_crop = zoran_s_crop,
3537 .vidioc_g_crop = zoran_g_crop,
3538 .vidioc_enum_input = zoran_enum_input,
3539 .vidioc_g_input = zoran_g_input,
3540 .vidioc_s_input = zoran_s_input,
3541 .vidioc_enum_output = zoran_enum_output,
3542 .vidioc_g_output = zoran_g_output,
3543 .vidioc_s_output = zoran_s_output,
3544 .vidioc_g_fbuf = zoran_g_fbuf,
3545 .vidioc_s_fbuf = zoran_s_fbuf,
3546 .vidioc_g_std = zoran_g_std,
3547 .vidioc_s_std = zoran_s_std,
3548 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3549 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3550 .vidioc_overlay = zoran_overlay,
3551 .vidioc_reqbufs = zoran_reqbufs,
3552 .vidioc_querybuf = zoran_querybuf,
3553 .vidioc_qbuf = zoran_qbuf,
3554 .vidioc_dqbuf = zoran_dqbuf,
3555 .vidioc_streamon = zoran_streamon,
3556 .vidioc_streamoff = zoran_streamoff,
3557 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3558 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3559 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3560 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3561 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3562 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3563 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3564 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3565 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3566 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3567 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3568 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3569 .vidioc_queryctrl = zoran_queryctrl,
3570 .vidioc_s_ctrl = zoran_s_ctrl,
3571 .vidioc_g_ctrl = zoran_g_ctrl,
3572 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3573 .vidioc_default = zoran_default,
3574 .vidiocgmbuf = zoran_vidiocgmbuf,
3578 static const struct v4l2_file_operations zoran_fops = {
3579 .owner = THIS_MODULE,
3581 .release = zoran_close,
3582 .ioctl = video_ioctl2,
3584 .write = zoran_write,
3589 struct video_device zoran_template __devinitdata = {
3591 .fops = &zoran_fops,
3592 .ioctl_ops = &zoran_ioctl_ops,
3593 .release = &zoran_vdev_release,
3594 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,