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>
61 #define MAP_NR(x) virt_to_page(x)
62 #define ZORAN_VID_TYPE ( \
68 VID_TYPE_MJPEG_DECODER | \
69 VID_TYPE_MJPEG_ENCODER \
72 #include <linux/videodev.h>
73 #include <media/v4l2-common.h>
74 #include <media/v4l2-ioctl.h>
75 #include "videocodec.h"
77 #include <asm/byteorder.h>
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
86 #include "zoran_device.h"
87 #include "zoran_card.h"
89 /* we declare some card type definitions here, they mean
90 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
91 #define ZORAN_V4L2_VID_FLAGS ( \
93 V4L2_CAP_VIDEO_CAPTURE |\
94 V4L2_CAP_VIDEO_OUTPUT |\
95 V4L2_CAP_VIDEO_OVERLAY \
99 #if defined(CONFIG_VIDEO_V4L1_COMPAT)
100 #define ZFMT(pal, fcc, cs) \
101 .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
103 #define ZFMT(pal, fcc, cs) \
104 .fourcc = (fcc), .colorspace = (cs)
107 const struct zoran_format zoran_formats[] = {
109 .name = "15-bit RGB LE",
110 ZFMT(VIDEO_PALETTE_RGB555,
111 V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
113 .flags = ZORAN_FORMAT_CAPTURE |
114 ZORAN_FORMAT_OVERLAY,
115 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
116 ZR36057_VFESPFR_LittleEndian,
118 .name = "15-bit RGB BE",
120 V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
122 .flags = ZORAN_FORMAT_CAPTURE |
123 ZORAN_FORMAT_OVERLAY,
124 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
126 .name = "16-bit RGB LE",
127 ZFMT(VIDEO_PALETTE_RGB565,
128 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
130 .flags = ZORAN_FORMAT_CAPTURE |
131 ZORAN_FORMAT_OVERLAY,
132 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
133 ZR36057_VFESPFR_LittleEndian,
135 .name = "16-bit RGB BE",
137 V4L2_PIX_FMT_RGB565X, V4L2_COLORSPACE_SRGB),
139 .flags = ZORAN_FORMAT_CAPTURE |
140 ZORAN_FORMAT_OVERLAY,
141 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
143 .name = "24-bit RGB",
144 ZFMT(VIDEO_PALETTE_RGB24,
145 V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
147 .flags = ZORAN_FORMAT_CAPTURE |
148 ZORAN_FORMAT_OVERLAY,
149 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
151 .name = "32-bit RGB LE",
152 ZFMT(VIDEO_PALETTE_RGB32,
153 V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
155 .flags = ZORAN_FORMAT_CAPTURE |
156 ZORAN_FORMAT_OVERLAY,
157 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
159 .name = "32-bit RGB BE",
161 V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
163 .flags = ZORAN_FORMAT_CAPTURE |
164 ZORAN_FORMAT_OVERLAY,
165 .vfespfr = ZR36057_VFESPFR_RGB888,
167 .name = "4:2:2, packed, YUYV",
168 ZFMT(VIDEO_PALETTE_YUV422,
169 V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
171 .flags = ZORAN_FORMAT_CAPTURE |
172 ZORAN_FORMAT_OVERLAY,
173 .vfespfr = ZR36057_VFESPFR_YUV422,
175 .name = "4:2:2, packed, UYVY",
176 ZFMT(VIDEO_PALETTE_UYVY,
177 V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
179 .flags = ZORAN_FORMAT_CAPTURE |
180 ZORAN_FORMAT_OVERLAY,
181 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
183 .name = "Hardware-encoded Motion-JPEG",
185 V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
187 .flags = ZORAN_FORMAT_CAPTURE |
188 ZORAN_FORMAT_PLAYBACK |
189 ZORAN_FORMAT_COMPRESSED,
192 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
194 static int lock_norm; /* 0 = default 1 = Don't change TV standard (norm) */
195 module_param(lock_norm, int, 0644);
196 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
198 /* small helper function for calculating buffersizes for v4l2
199 * we calculate the nearest higher power-of-two, which
200 * will be the recommended buffersize */
202 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
204 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
205 __u32 num = (1024 * 512) / (div);
214 if (result > jpg_bufsize)
221 /* forward references */
222 static void v4l_fbuffer_free(struct file *file);
223 static void jpg_fbuffer_free(struct file *file);
226 * Allocate the V4L grab buffers
228 * These have to be pysically contiguous.
232 v4l_fbuffer_alloc (struct file *file)
234 struct zoran_fh *fh = file->private_data;
235 struct zoran *zr = fh->zr;
239 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
240 if (fh->v4l_buffers.buffer[i].fbuffer)
243 "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
247 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
251 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
253 v4l_fbuffer_free(file);
256 fh->v4l_buffers.buffer[i].fbuffer = mem;
257 fh->v4l_buffers.buffer[i].fbuffer_phys =
259 fh->v4l_buffers.buffer[i].fbuffer_bus =
261 for (off = 0; off < fh->v4l_buffers.buffer_size;
263 SetPageReserved(MAP_NR(mem + off));
266 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
267 ZR_DEVNAME(zr), i, (unsigned long) mem,
271 fh->v4l_buffers.allocated = 1;
276 /* free the V4L grab buffers */
278 v4l_fbuffer_free (struct file *file)
280 struct zoran_fh *fh = file->private_data;
281 struct zoran *zr = fh->zr;
285 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
287 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
288 if (!fh->v4l_buffers.buffer[i].fbuffer)
291 mem = fh->v4l_buffers.buffer[i].fbuffer;
292 for (off = 0; off < fh->v4l_buffers.buffer_size;
294 ClearPageReserved(MAP_NR(mem + off));
295 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
296 fh->v4l_buffers.buffer[i].fbuffer = NULL;
299 fh->v4l_buffers.allocated = 0;
303 * Allocate the MJPEG grab buffers.
305 * If a Natoma chipset is present and this is a revision 1 zr36057,
306 * each MJPEG buffer needs to be physically contiguous.
307 * (RJ: This statement is from Dave Perks' original driver,
308 * I could never check it because I have a zr36067)
310 * RJ: The contents grab buffers needs never be accessed in the driver.
311 * Therefore there is no need to allocate them with vmalloc in order
312 * to get a contiguous virtual memory space.
313 * I don't understand why many other drivers first allocate them with
314 * vmalloc (which uses internally also get_zeroed_page, but delivers you
315 * virtual addresses) and then again have to make a lot of efforts
316 * to get the physical address.
319 * On big-endian architectures (such as ppc) some extra steps
320 * are needed. When reading and writing to the stat_com array
321 * and fragment buffers, the device expects to see little-
322 * endian values. The use of cpu_to_le32() and le32_to_cpu()
323 * in this function (and one or two others in zoran_device.c)
324 * ensure that these values are always stored in little-endian
325 * form, regardless of architecture. The zr36057 does Very Bad
326 * Things on big endian architectures if the stat_com array
327 * and fragment buffers are not little-endian.
331 jpg_fbuffer_alloc (struct file *file)
333 struct zoran_fh *fh = file->private_data;
334 struct zoran *zr = fh->zr;
338 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
339 if (fh->jpg_buffers.buffer[i].frag_tab)
342 "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
345 /* Allocate fragment table for this buffer */
347 mem = get_zeroed_page(GFP_KERNEL);
351 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
353 jpg_fbuffer_free(file);
356 fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
357 fh->jpg_buffers.buffer[i].frag_tab_bus =
358 virt_to_bus((void *) mem);
360 //if (alloc_contig) {
361 if (fh->jpg_buffers.need_contiguous) {
363 (unsigned long) kmalloc(fh->jpg_buffers.
369 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
371 jpg_fbuffer_free(file);
374 fh->jpg_buffers.buffer[i].frag_tab[0] =
375 cpu_to_le32(virt_to_bus((void *) mem));
376 fh->jpg_buffers.buffer[i].frag_tab[1] =
377 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
378 for (off = 0; off < fh->jpg_buffers.buffer_size;
380 SetPageReserved(MAP_NR(mem + off));
382 /* jpg_bufsize is already page aligned */
384 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
386 mem = get_zeroed_page(GFP_KERNEL);
390 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
392 jpg_fbuffer_free(file);
396 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
397 cpu_to_le32(virt_to_bus((void *) mem));
398 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
400 cpu_to_le32((PAGE_SIZE / 4) << 1);
401 SetPageReserved(MAP_NR(mem));
404 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
409 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
411 (fh->jpg_buffers.num_buffers *
412 fh->jpg_buffers.buffer_size) >> 10);
414 fh->jpg_buffers.allocated = 1;
419 /* free the MJPEG grab buffers */
421 jpg_fbuffer_free (struct file *file)
423 struct zoran_fh *fh = file->private_data;
424 struct zoran *zr = fh->zr;
428 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
430 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
431 if (!fh->jpg_buffers.buffer[i].frag_tab)
434 //if (alloc_contig) {
435 if (fh->jpg_buffers.need_contiguous) {
436 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
437 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
438 fh->jpg_buffers.buffer[i].frag_tab[0]));
440 off < fh->jpg_buffers.buffer_size;
442 ClearPageReserved(MAP_NR
445 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
446 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
450 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
452 if (!fh->jpg_buffers.buffer[i].
455 ClearPageReserved(MAP_NR
459 buffer[i].frag_tab[2 *
461 free_page((unsigned long)
467 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
469 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
474 free_page((unsigned long) fh->jpg_buffers.buffer[i].
476 fh->jpg_buffers.buffer[i].frag_tab = NULL;
479 fh->jpg_buffers.allocated = 0;
483 * V4L Buffer grabbing
487 zoran_v4l_set_format (struct file *file,
490 const struct zoran_format *format)
492 struct zoran_fh *fh = file->private_data;
493 struct zoran *zr = fh->zr;
496 /* Check size and format of the grab wanted */
498 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
499 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
502 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
503 ZR_DEVNAME(zr), width, height);
507 bpp = (format->depth + 7) / 8;
509 /* Check against available buffer size */
510 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
513 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
514 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
518 /* The video front end needs 4-byte alinged line sizes */
520 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
523 "%s: v4l_set_format() - wrong frame alignment\n",
528 fh->v4l_settings.width = width;
529 fh->v4l_settings.height = height;
530 fh->v4l_settings.format = format;
531 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
537 zoran_v4l_queue_frame (struct file *file,
540 struct zoran_fh *fh = file->private_data;
541 struct zoran *zr = fh->zr;
545 if (!fh->v4l_buffers.allocated) {
548 "%s: v4l_queue_frame() - buffers not yet allocated\n",
553 /* No grabbing outside the buffer range! */
554 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
557 "%s: v4l_queue_frame() - buffer %d is out of range\n",
558 ZR_DEVNAME(zr), num);
562 spin_lock_irqsave(&zr->spinlock, flags);
564 if (fh->v4l_buffers.active == ZORAN_FREE) {
565 if (zr->v4l_buffers.active == ZORAN_FREE) {
566 zr->v4l_buffers = fh->v4l_buffers;
567 fh->v4l_buffers.active = ZORAN_ACTIVE;
571 "%s: v4l_queue_frame() - another session is already capturing\n",
577 /* make sure a grab isn't going on currently with this buffer */
579 switch (zr->v4l_buffers.buffer[num].state) {
582 if (zr->v4l_buffers.active == ZORAN_FREE) {
583 fh->v4l_buffers.active = ZORAN_FREE;
584 zr->v4l_buffers.allocated = 0;
586 res = -EBUSY; /* what are you doing? */
591 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
592 ZR_DEVNAME(zr), num);
594 /* since there is at least one unused buffer there's room for at least
595 * one more pend[] entry */
596 zr->v4l_pend[zr->v4l_pend_head++ &
597 V4L_MASK_FRAME] = num;
598 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
599 zr->v4l_buffers.buffer[num].bs.length =
600 fh->v4l_settings.bytesperline *
601 zr->v4l_settings.height;
602 fh->v4l_buffers.buffer[num] =
603 zr->v4l_buffers.buffer[num];
608 spin_unlock_irqrestore(&zr->spinlock, flags);
610 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
611 zr->v4l_buffers.active = fh->v4l_buffers.active;
617 * Sync on a V4L buffer
621 v4l_sync (struct file *file,
624 struct zoran_fh *fh = file->private_data;
625 struct zoran *zr = fh->zr;
628 if (fh->v4l_buffers.active == ZORAN_FREE) {
631 "%s: v4l_sync() - no grab active for this session\n",
636 /* check passed-in frame number */
637 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
639 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
640 ZR_DEVNAME(zr), frame);
644 /* Check if is buffer was queued at all */
645 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
648 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
653 /* wait on this buffer to get ready */
654 if (!wait_event_interruptible_timeout(zr->v4l_capq,
655 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
658 if (signal_pending(current))
661 /* buffer should now be in BUZ_STATE_DONE */
662 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
664 KERN_ERR "%s: v4l_sync() - internal state error\n",
667 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
668 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
670 spin_lock_irqsave(&zr->spinlock, flags);
672 /* Check if streaming capture has finished */
673 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
674 zr36057_set_memgrab(zr, 0);
675 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
676 fh->v4l_buffers.active = zr->v4l_buffers.active =
678 zr->v4l_buffers.allocated = 0;
682 spin_unlock_irqrestore(&zr->spinlock, flags);
688 * Queue a MJPEG buffer for capture/playback
692 zoran_jpg_queue_frame (struct file *file,
694 enum zoran_codec_mode mode)
696 struct zoran_fh *fh = file->private_data;
697 struct zoran *zr = fh->zr;
701 /* Check if buffers are allocated */
702 if (!fh->jpg_buffers.allocated) {
705 "%s: jpg_queue_frame() - buffers not yet allocated\n",
710 /* No grabbing outside the buffer range! */
711 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
714 "%s: jpg_queue_frame() - buffer %d out of range\n",
715 ZR_DEVNAME(zr), num);
719 /* what is the codec mode right now? */
720 if (zr->codec_mode == BUZ_MODE_IDLE) {
721 zr->jpg_settings = fh->jpg_settings;
722 } else if (zr->codec_mode != mode) {
723 /* wrong codec mode active - invalid */
726 "%s: jpg_queue_frame() - codec in wrong mode\n",
731 if (fh->jpg_buffers.active == ZORAN_FREE) {
732 if (zr->jpg_buffers.active == ZORAN_FREE) {
733 zr->jpg_buffers = fh->jpg_buffers;
734 fh->jpg_buffers.active = ZORAN_ACTIVE;
738 "%s: jpg_queue_frame() - another session is already capturing\n",
744 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
745 /* Ok load up the jpeg codec */
746 zr36057_enable_jpg(zr, mode);
749 spin_lock_irqsave(&zr->spinlock, flags);
752 switch (zr->jpg_buffers.buffer[num].state) {
756 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
759 /* since there is at least one unused buffer there's room for at
760 *least one more pend[] entry */
761 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
763 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
764 fh->jpg_buffers.buffer[num] =
765 zr->jpg_buffers.buffer[num];
766 zoran_feed_stat_com(zr);
771 if (zr->jpg_buffers.active == ZORAN_FREE) {
772 fh->jpg_buffers.active = ZORAN_FREE;
773 zr->jpg_buffers.allocated = 0;
775 res = -EBUSY; /* what are you doing? */
780 spin_unlock_irqrestore(&zr->spinlock, flags);
782 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
783 zr->jpg_buffers.active = fh->jpg_buffers.active;
790 jpg_qbuf (struct file *file,
792 enum zoran_codec_mode mode)
794 struct zoran_fh *fh = file->private_data;
795 struct zoran *zr = fh->zr;
798 /* Does the user want to stop streaming? */
800 if (zr->codec_mode == mode) {
801 if (fh->jpg_buffers.active == ZORAN_FREE) {
804 "%s: jpg_qbuf(-1) - session not active\n",
808 fh->jpg_buffers.active = zr->jpg_buffers.active =
810 zr->jpg_buffers.allocated = 0;
811 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
816 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
822 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
825 /* Start the jpeg codec when the first frame is queued */
826 if (!res && zr->jpg_que_head == 1)
833 * Sync on a MJPEG buffer
837 jpg_sync (struct file *file,
838 struct zoran_sync *bs)
840 struct zoran_fh *fh = file->private_data;
841 struct zoran *zr = fh->zr;
845 if (fh->jpg_buffers.active == ZORAN_FREE) {
848 "%s: jpg_sync() - capture is not currently active\n",
852 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
853 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
856 "%s: jpg_sync() - codec not in streaming mode\n",
860 if (!wait_event_interruptible_timeout(zr->jpg_capq,
861 (zr->jpg_que_tail != zr->jpg_dma_tail ||
862 zr->jpg_dma_tail == zr->jpg_dma_head),
866 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
868 zr->codec->control(zr->codec, CODEC_G_STATUS,
872 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
873 ZR_DEVNAME(zr), isr);
878 if (signal_pending(current))
881 spin_lock_irqsave(&zr->spinlock, flags);
883 if (zr->jpg_dma_tail != zr->jpg_dma_head)
884 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
886 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
888 /* buffer should now be in BUZ_STATE_DONE */
889 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
891 KERN_ERR "%s: jpg_sync() - internal state error\n",
894 *bs = zr->jpg_buffers.buffer[frame].bs;
896 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
897 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
899 spin_unlock_irqrestore(&zr->spinlock, flags);
905 zoran_open_init_session (struct file *file)
908 struct zoran_fh *fh = file->private_data;
909 struct zoran *zr = fh->zr;
911 /* Per default, map the V4L Buffers */
912 fh->map_mode = ZORAN_MAP_MODE_RAW;
914 /* take over the card's current settings */
915 fh->overlay_settings = zr->overlay_settings;
916 fh->overlay_settings.is_set = 0;
917 fh->overlay_settings.format = zr->overlay_settings.format;
918 fh->overlay_active = ZORAN_FREE;
921 fh->v4l_settings = zr->v4l_settings;
924 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
925 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
926 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
927 fh->v4l_buffers.buffer[i].bs.frame = i;
929 fh->v4l_buffers.allocated = 0;
930 fh->v4l_buffers.active = ZORAN_FREE;
931 fh->v4l_buffers.buffer_size = v4l_bufsize;
932 fh->v4l_buffers.num_buffers = v4l_nbufs;
935 fh->jpg_settings = zr->jpg_settings;
938 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
939 for (i = 0; i < BUZ_MAX_FRAME; i++) {
940 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
941 fh->jpg_buffers.buffer[i].bs.frame = i;
943 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
944 fh->jpg_buffers.allocated = 0;
945 fh->jpg_buffers.active = ZORAN_FREE;
946 fh->jpg_buffers.buffer_size = jpg_bufsize;
947 fh->jpg_buffers.num_buffers = jpg_nbufs;
951 zoran_close_end_session (struct file *file)
953 struct zoran_fh *fh = file->private_data;
954 struct zoran *zr = fh->zr;
957 if (fh->overlay_active != ZORAN_FREE) {
958 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
959 zr->v4l_overlay_active = 0;
960 if (!zr->v4l_memgrab_active)
961 zr36057_overlay(zr, 0);
962 zr->overlay_mask = NULL;
966 if (fh->v4l_buffers.active != ZORAN_FREE) {
969 spin_lock_irqsave(&zr->spinlock, flags);
970 zr36057_set_memgrab(zr, 0);
971 zr->v4l_buffers.allocated = 0;
972 zr->v4l_buffers.active = fh->v4l_buffers.active =
974 spin_unlock_irqrestore(&zr->spinlock, flags);
978 if (fh->v4l_buffers.allocated)
979 v4l_fbuffer_free(file);
982 if (fh->jpg_buffers.active != ZORAN_FREE) {
983 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
984 zr->jpg_buffers.allocated = 0;
985 zr->jpg_buffers.active = fh->jpg_buffers.active =
990 if (fh->jpg_buffers.allocated)
991 jpg_fbuffer_free(file);
995 * Open a zoran card. Right now the flags stuff is just playing
998 static int zoran_open(struct file *file)
1000 struct zoran *zr = video_drvdata(file);
1001 struct zoran_fh *fh;
1002 int res, first_open = 0;
1004 dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1005 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
1009 /* see fs/device.c - the kernel already locks during open(),
1010 * so locking ourselves only causes deadlocks */
1011 /*mutex_lock(&zr->resource_lock);*/
1013 if (zr->user >= 2048) {
1014 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1015 ZR_DEVNAME(zr), zr->user);
1022 KERN_ERR "%s: no TV decoder loaded for device!\n",
1028 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1031 "%s: failed to grab ownership of video decoder\n",
1037 !try_module_get(zr->encoder->driver->driver.owner)) {
1040 "%s: failed to grab ownership of video encoder\n",
1046 /* now, create the open()-specific file_ops struct */
1047 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1051 "%s: zoran_open() - allocation of zoran_fh failed\n",
1056 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1057 * on norm-change! */
1059 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1060 if (!fh->overlay_mask) {
1063 "%s: zoran_open() - allocation of overlay_mask failed\n",
1069 if (zr->user++ == 0)
1072 /*mutex_unlock(&zr->resource_lock);*/
1074 /* default setup - TODO: look at flags */
1075 if (first_open) { /* First device open */
1076 zr36057_restart(zr);
1077 zoran_open_init_params(zr);
1078 zoran_init_hardware(zr);
1080 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1083 /* set file_ops stuff */
1084 file->private_data = fh;
1086 zoran_open_init_session(file);
1095 module_put(zr->encoder->driver->driver.owner);
1097 module_put(zr->decoder->driver->driver.owner);
1101 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1102 ZR_DEVNAME(zr), res, zr->user);
1108 zoran_close(struct file *file)
1110 struct zoran_fh *fh = file->private_data;
1111 struct zoran *zr = fh->zr;
1113 dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1114 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1116 /* kernel locks (fs/device.c), so don't do that ourselves
1117 * (prevents deadlocks) */
1118 /*mutex_lock(&zr->resource_lock);*/
1120 zoran_close_end_session(file);
1122 if (zr->user-- == 1) { /* Last process */
1123 /* Clean up JPEG process */
1124 wake_up_interruptible(&zr->jpg_capq);
1125 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1126 zr->jpg_buffers.allocated = 0;
1127 zr->jpg_buffers.active = ZORAN_FREE;
1129 /* disable interrupts */
1130 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1132 if (zr36067_debug > 1)
1133 print_interrupts(zr);
1136 zr->v4l_overlay_active = 0;
1137 zr36057_overlay(zr, 0);
1138 zr->overlay_mask = NULL;
1141 wake_up_interruptible(&zr->v4l_capq);
1142 zr36057_set_memgrab(zr, 0);
1143 zr->v4l_buffers.allocated = 0;
1144 zr->v4l_buffers.active = ZORAN_FREE;
1145 zoran_set_pci_master(zr, 0);
1147 if (!pass_through) { /* Switch to color bar */
1148 struct v4l2_routing route = { 2, 0 };
1150 decoder_command(zr, VIDIOC_STREAMOFF, 0);
1151 encoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1155 file->private_data = NULL;
1156 kfree(fh->overlay_mask);
1159 /* release locks on the i2c modules */
1160 module_put(zr->decoder->driver->driver.owner);
1162 module_put(zr->encoder->driver->driver.owner);
1164 /*mutex_unlock(&zr->resource_lock);*/
1166 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1173 zoran_read (struct file *file,
1178 /* we simply don't support read() (yet)... */
1184 zoran_write (struct file *file,
1185 const char __user *data,
1189 /* ...and the same goes for write() */
1195 setup_fbuffer (struct file *file,
1197 const struct zoran_format *fmt,
1202 struct zoran_fh *fh = file->private_data;
1203 struct zoran *zr = fh->zr;
1205 /* (Ronald) v4l/v4l2 guidelines */
1206 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1209 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1210 ALi Magik (that needs very low latency while the card needs a
1211 higher value always) */
1213 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1216 /* we need a bytesperline value, even if not given */
1218 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1221 if (zr->overlay_active) {
1222 /* dzjee... stupid users... don't even bother to turn off
1223 * overlay before changing the memory location...
1224 * normally, we would return errors here. However, one of
1225 * the tools that does this is... xawtv! and since xawtv
1226 * is used by +/- 99% of the users, we'd rather be user-
1227 * friendly and silently do as if nothing went wrong */
1230 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1232 zr36057_overlay(zr, 0);
1236 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1239 "%s: setup_fbuffer() - no valid overlay format given\n",
1243 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1246 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1247 ZR_DEVNAME(zr), width, height, bytesperline);
1250 if (bytesperline & 3) {
1253 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1254 ZR_DEVNAME(zr), bytesperline);
1258 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1259 zr->buffer.height = height;
1260 zr->buffer.width = width;
1261 zr->buffer.depth = fmt->depth;
1262 zr->overlay_settings.format = fmt;
1263 zr->buffer.bytesperline = bytesperline;
1265 /* The user should set new window parameters */
1266 zr->overlay_settings.is_set = 0;
1273 setup_window (struct file *file,
1278 struct video_clip __user *clips,
1280 void __user *bitmap)
1282 struct zoran_fh *fh = file->private_data;
1283 struct zoran *zr = fh->zr;
1284 struct video_clip *vcp = NULL;
1288 if (!zr->buffer.base) {
1291 "%s: setup_window() - frame buffer has to be set first\n",
1296 if (!fh->overlay_settings.format) {
1299 "%s: setup_window() - no overlay format set\n",
1305 * The video front end needs 4-byte alinged line sizes, we correct that
1306 * silently here if necessary
1308 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1309 end = (x + width) & ~1; /* round down */
1310 x = (x + 1) & ~1; /* round up */
1314 if (zr->buffer.depth == 24) {
1315 end = (x + width) & ~3; /* round down */
1316 x = (x + 3) & ~3; /* round up */
1320 if (width > BUZ_MAX_WIDTH)
1321 width = BUZ_MAX_WIDTH;
1322 if (height > BUZ_MAX_HEIGHT)
1323 height = BUZ_MAX_HEIGHT;
1325 /* Check for vaild parameters */
1326 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1327 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1330 "%s: setup_window() - width = %d or height = %d invalid\n",
1331 ZR_DEVNAME(zr), width, height);
1335 fh->overlay_settings.x = x;
1336 fh->overlay_settings.y = y;
1337 fh->overlay_settings.width = width;
1338 fh->overlay_settings.height = height;
1339 fh->overlay_settings.clipcount = clipcount;
1342 * If an overlay is running, we have to switch it off
1343 * and switch it on again in order to get the new settings in effect.
1345 * We also want to avoid that the overlay mask is written
1346 * when an overlay is running.
1349 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1350 zr->overlay_active != ZORAN_FREE &&
1351 fh->overlay_active != ZORAN_FREE;
1353 zr36057_overlay(zr, 0);
1356 * Write the overlay mask if clips are wanted.
1357 * We prefer a bitmap.
1360 /* fake value - it just means we want clips */
1361 fh->overlay_settings.clipcount = 1;
1363 if (copy_from_user(fh->overlay_mask, bitmap,
1364 (width * height + 7) / 8)) {
1367 } else if (clipcount > 0) {
1368 /* write our own bitmap from the clips */
1369 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1373 "%s: setup_window() - Alloc of clip mask failed\n",
1378 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1382 write_overlay_mask(file, vcp, clipcount);
1386 fh->overlay_settings.is_set = 1;
1387 if (fh->overlay_active != ZORAN_FREE &&
1388 zr->overlay_active != ZORAN_FREE)
1389 zr->overlay_settings = fh->overlay_settings;
1392 zr36057_overlay(zr, 1);
1394 /* Make sure the changes come into effect */
1395 return wait_grab_pending(zr);
1399 setup_overlay (struct file *file,
1402 struct zoran_fh *fh = file->private_data;
1403 struct zoran *zr = fh->zr;
1405 /* If there is nothing to do, return immediatly */
1406 if ((on && fh->overlay_active != ZORAN_FREE) ||
1407 (!on && fh->overlay_active == ZORAN_FREE))
1410 /* check whether we're touching someone else's overlay */
1411 if (on && zr->overlay_active != ZORAN_FREE &&
1412 fh->overlay_active == ZORAN_FREE) {
1415 "%s: setup_overlay() - overlay is already active for another session\n",
1419 if (!on && zr->overlay_active != ZORAN_FREE &&
1420 fh->overlay_active == ZORAN_FREE) {
1423 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1429 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1430 zr->v4l_overlay_active = 0;
1431 /* When a grab is running, the video simply
1432 * won't be switched on any more */
1433 if (!zr->v4l_memgrab_active)
1434 zr36057_overlay(zr, 0);
1435 zr->overlay_mask = NULL;
1437 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1440 "%s: setup_overlay() - buffer or window not set\n",
1444 if (!fh->overlay_settings.format) {
1447 "%s: setup_overlay() - no overlay format set\n",
1451 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1452 zr->v4l_overlay_active = 1;
1453 zr->overlay_mask = fh->overlay_mask;
1454 zr->overlay_settings = fh->overlay_settings;
1455 if (!zr->v4l_memgrab_active)
1456 zr36057_overlay(zr, 1);
1457 /* When a grab is running, the video will be
1458 * switched on when grab is finished */
1461 /* Make sure the changes come into effect */
1462 return wait_grab_pending(zr);
1465 /* get the status of a buffer in the clients buffer queue */
1467 zoran_v4l2_buffer_status (struct file *file,
1468 struct v4l2_buffer *buf,
1471 struct zoran_fh *fh = file->private_data;
1472 struct zoran *zr = fh->zr;
1474 buf->flags = V4L2_BUF_FLAG_MAPPED;
1476 switch (fh->map_mode) {
1477 case ZORAN_MAP_MODE_RAW:
1480 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1481 !fh->v4l_buffers.allocated) {
1484 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1489 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1490 buf->length = fh->v4l_buffers.buffer_size;
1493 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1494 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1495 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1496 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1497 buf->flags |= V4L2_BUF_FLAG_DONE;
1499 fh->v4l_buffers.buffer[num].bs.timestamp;
1501 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1504 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1505 buf->field = V4L2_FIELD_TOP;
1507 buf->field = V4L2_FIELD_INTERLACED;
1511 case ZORAN_MAP_MODE_JPG_REC:
1512 case ZORAN_MAP_MODE_JPG_PLAY:
1515 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1516 !fh->jpg_buffers.allocated) {
1519 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1524 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1525 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1526 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1527 buf->length = fh->jpg_buffers.buffer_size;
1529 /* these variables are only written after frame has been captured */
1530 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1531 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1532 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1534 fh->jpg_buffers.buffer[num].bs.timestamp;
1536 fh->jpg_buffers.buffer[num].bs.length;
1537 buf->flags |= V4L2_BUF_FLAG_DONE;
1539 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1542 /* which fields are these? */
1543 if (fh->jpg_settings.TmpDcm != 1)
1546 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1550 odd_even ? V4L2_FIELD_SEQ_TB :
1559 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1560 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1564 buf->memory = V4L2_MEMORY_MMAP;
1566 buf->m.offset = buf->length * num;
1572 zoran_set_norm (struct zoran *zr,
1577 if (zr->v4l_buffers.active != ZORAN_FREE ||
1578 zr->jpg_buffers.active != ZORAN_FREE) {
1581 "%s: set_norm() called while in playback/capture mode\n",
1586 if (lock_norm && norm != zr->norm) {
1587 if (lock_norm > 1) {
1590 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1596 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1602 if (!(norm & zr->card.norms)) {
1604 KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1605 ZR_DEVNAME(zr), norm);
1609 if (norm == V4L2_STD_ALL) {
1611 v4l2_std_id std = 0;
1613 decoder_command(zr, VIDIOC_QUERYSTD, &std);
1614 decoder_command(zr, VIDIOC_S_STD, &std);
1616 /* let changes come into effect */
1619 decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
1620 if (status & V4L2_IN_ST_NO_SIGNAL) {
1623 "%s: set_norm() - no norm detected\n",
1626 decoder_command(zr, VIDIOC_S_STD, &zr->norm);
1632 if (norm & V4L2_STD_SECAM)
1633 zr->timing = zr->card.tvn[2];
1634 else if (norm & V4L2_STD_NTSC)
1635 zr->timing = zr->card.tvn[1];
1637 zr->timing = zr->card.tvn[0];
1639 /* We switch overlay off and on since a change in the
1640 * norm needs different VFE settings */
1641 on = zr->overlay_active && !zr->v4l_memgrab_active;
1643 zr36057_overlay(zr, 0);
1645 decoder_command(zr, VIDIOC_S_STD, &norm);
1646 encoder_command(zr, VIDIOC_INT_S_STD_OUTPUT, &norm);
1649 zr36057_overlay(zr, 1);
1651 /* Make sure the changes come into effect */
1658 zoran_set_input (struct zoran *zr,
1661 struct v4l2_routing route = { 0, 0 };
1663 if (input == zr->input) {
1667 if (zr->v4l_buffers.active != ZORAN_FREE ||
1668 zr->jpg_buffers.active != ZORAN_FREE) {
1671 "%s: set_input() called while in playback/capture mode\n",
1676 if (input < 0 || input >= zr->card.inputs) {
1679 "%s: set_input() - unnsupported input %d\n",
1680 ZR_DEVNAME(zr), input);
1684 route.input = zr->card.input[input].muxsel;
1687 decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1696 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1697 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1699 struct zoran_fh *fh = __fh;
1700 struct zoran *zr = fh->zr;
1701 struct zoran_jpg_settings settings;
1704 case BUZIOC_G_PARAMS:
1706 struct zoran_params *bparams = arg;
1708 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1710 memset(bparams, 0, sizeof(struct zoran_params));
1711 bparams->major_version = MAJOR_VERSION;
1712 bparams->minor_version = MINOR_VERSION;
1714 mutex_lock(&zr->resource_lock);
1716 if (zr->norm & V4L2_STD_NTSC)
1717 bparams->norm = VIDEO_MODE_NTSC;
1718 else if (zr->norm & V4L2_STD_PAL)
1719 bparams->norm = VIDEO_MODE_PAL;
1721 bparams->norm = VIDEO_MODE_SECAM;
1723 bparams->input = zr->input;
1725 bparams->decimation = fh->jpg_settings.decimation;
1726 bparams->HorDcm = fh->jpg_settings.HorDcm;
1727 bparams->VerDcm = fh->jpg_settings.VerDcm;
1728 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1729 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1730 bparams->img_x = fh->jpg_settings.img_x;
1731 bparams->img_y = fh->jpg_settings.img_y;
1732 bparams->img_width = fh->jpg_settings.img_width;
1733 bparams->img_height = fh->jpg_settings.img_height;
1734 bparams->odd_even = fh->jpg_settings.odd_even;
1736 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1737 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1738 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1739 memcpy(bparams->APP_data,
1740 fh->jpg_settings.jpg_comp.APP_data,
1741 sizeof(bparams->APP_data));
1742 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1743 memcpy(bparams->COM_data,
1744 fh->jpg_settings.jpg_comp.COM_data,
1745 sizeof(bparams->COM_data));
1746 bparams->jpeg_markers =
1747 fh->jpg_settings.jpg_comp.jpeg_markers;
1749 mutex_unlock(&zr->resource_lock);
1751 bparams->VFIFO_FB = 0;
1756 case BUZIOC_S_PARAMS:
1758 struct zoran_params *bparams = arg;
1761 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1763 settings.decimation = bparams->decimation;
1764 settings.HorDcm = bparams->HorDcm;
1765 settings.VerDcm = bparams->VerDcm;
1766 settings.TmpDcm = bparams->TmpDcm;
1767 settings.field_per_buff = bparams->field_per_buff;
1768 settings.img_x = bparams->img_x;
1769 settings.img_y = bparams->img_y;
1770 settings.img_width = bparams->img_width;
1771 settings.img_height = bparams->img_height;
1772 settings.odd_even = bparams->odd_even;
1774 settings.jpg_comp.quality = bparams->quality;
1775 settings.jpg_comp.APPn = bparams->APPn;
1776 settings.jpg_comp.APP_len = bparams->APP_len;
1777 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1778 sizeof(bparams->APP_data));
1779 settings.jpg_comp.COM_len = bparams->COM_len;
1780 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1781 sizeof(bparams->COM_data));
1782 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1784 mutex_lock(&zr->resource_lock);
1786 if (zr->codec_mode != BUZ_MODE_IDLE) {
1789 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1792 goto sparams_unlock_and_return;
1795 /* Check the params first before overwriting our
1797 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1799 goto sparams_unlock_and_return;
1802 fh->jpg_settings = settings;
1803 sparams_unlock_and_return:
1804 mutex_unlock(&zr->resource_lock);
1809 case BUZIOC_REQBUFS:
1811 struct zoran_requestbuffers *breq = arg;
1816 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1817 ZR_DEVNAME(zr), breq->count, breq->size);
1819 /* Enforce reasonable lower and upper limits */
1820 if (breq->count < 4)
1821 breq->count = 4; /* Could be choosen smaller */
1822 if (breq->count > jpg_nbufs)
1823 breq->count = jpg_nbufs;
1824 breq->size = PAGE_ALIGN(breq->size);
1825 if (breq->size < 8192)
1826 breq->size = 8192; /* Arbitrary */
1827 /* breq->size is limited by 1 page for the stat_com
1828 * tables to a Maximum of 2 MB */
1829 if (breq->size > jpg_bufsize)
1830 breq->size = jpg_bufsize;
1832 mutex_lock(&zr->resource_lock);
1834 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1837 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1840 goto jpgreqbuf_unlock_and_return;
1843 fh->jpg_buffers.num_buffers = breq->count;
1844 fh->jpg_buffers.buffer_size = breq->size;
1846 if (jpg_fbuffer_alloc(file)) {
1848 goto jpgreqbuf_unlock_and_return;
1851 /* The next mmap will map the MJPEG buffers - could
1852 * also be *_PLAY, but it doesn't matter here */
1853 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1854 jpgreqbuf_unlock_and_return:
1855 mutex_unlock(&zr->resource_lock);
1860 case BUZIOC_QBUF_CAPT:
1862 int *frame = arg, res;
1864 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1865 ZR_DEVNAME(zr), *frame);
1867 mutex_lock(&zr->resource_lock);
1868 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1869 mutex_unlock(&zr->resource_lock);
1874 case BUZIOC_QBUF_PLAY:
1876 int *frame = arg, res;
1878 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1879 ZR_DEVNAME(zr), *frame);
1881 mutex_lock(&zr->resource_lock);
1882 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1883 mutex_unlock(&zr->resource_lock);
1890 struct zoran_sync *bsync = arg;
1893 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1895 mutex_lock(&zr->resource_lock);
1896 res = jpg_sync(file, bsync);
1897 mutex_unlock(&zr->resource_lock);
1902 case BUZIOC_G_STATUS:
1904 struct zoran_status *bstat = arg;
1905 struct v4l2_routing route = { 0, 0 };
1906 int status = 0, res = 0;
1909 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1911 if (zr->codec_mode != BUZ_MODE_IDLE) {
1914 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1919 route.input = zr->card.input[bstat->input].muxsel;
1921 mutex_lock(&zr->resource_lock);
1923 if (zr->codec_mode != BUZ_MODE_IDLE) {
1926 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1929 goto gstat_unlock_and_return;
1932 decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1934 /* sleep 1 second */
1937 /* Get status of video decoder */
1938 decoder_command(zr, VIDIOC_QUERYSTD, &norm);
1939 decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
1941 /* restore previous input and norm */
1942 route.input = zr->card.input[zr->input].muxsel;
1943 decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1944 gstat_unlock_and_return:
1945 mutex_unlock(&zr->resource_lock);
1949 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1950 if (norm & V4L2_STD_NTSC)
1951 bstat->norm = VIDEO_MODE_NTSC;
1952 else if (norm & V4L2_STD_SECAM)
1953 bstat->norm = VIDEO_MODE_SECAM;
1955 bstat->norm = VIDEO_MODE_PAL;
1958 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1969 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1971 struct zoran_fh *fh = __fh;
1972 struct zoran *zr = fh->zr;
1976 fh->v4l_buffers.num_buffers *
1977 fh->v4l_buffers.buffer_size;
1978 vmbuf->frames = fh->v4l_buffers.num_buffers;
1979 for (i = 0; i < vmbuf->frames; i++) {
1981 i * fh->v4l_buffers.buffer_size;
1984 mutex_lock(&zr->resource_lock);
1986 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1989 "%s: VIDIOCGMBUF - buffers already allocated\n",
1992 goto v4l1reqbuf_unlock_and_return;
1995 if (v4l_fbuffer_alloc(file)) {
1997 goto v4l1reqbuf_unlock_and_return;
2000 /* The next mmap will map the V4L buffers */
2001 fh->map_mode = ZORAN_MAP_MODE_RAW;
2002 v4l1reqbuf_unlock_and_return:
2003 mutex_unlock(&zr->resource_lock);
2009 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
2011 struct zoran_fh *fh = __fh;
2012 struct zoran *zr = fh->zr;
2014 memset(cap, 0, sizeof(*cap));
2015 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2016 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2017 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2018 pci_name(zr->pci_dev));
2020 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2022 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2027 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
2031 for (i = 0; i < NUM_FORMATS; i++) {
2032 if (zoran_formats[i].flags & flag)
2034 if (num == fmt->index)
2037 if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
2040 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2041 fmt->pixelformat = zoran_formats[i].fourcc;
2042 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2043 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2047 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
2048 struct v4l2_fmtdesc *f)
2050 struct zoran_fh *fh = __fh;
2051 struct zoran *zr = fh->zr;
2053 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
2056 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
2057 struct v4l2_fmtdesc *f)
2059 struct zoran_fh *fh = __fh;
2060 struct zoran *zr = fh->zr;
2062 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
2065 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
2066 struct v4l2_fmtdesc *f)
2068 struct zoran_fh *fh = __fh;
2069 struct zoran *zr = fh->zr;
2071 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
2074 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
2075 struct v4l2_format *fmt)
2077 struct zoran_fh *fh = __fh;
2078 struct zoran *zr = fh->zr;
2080 mutex_lock(&zr->resource_lock);
2082 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
2083 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
2084 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
2085 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2086 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
2087 if (fh->jpg_settings.TmpDcm == 1)
2088 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2089 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2091 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2092 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2093 fmt->fmt.pix.bytesperline = 0;
2094 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2096 mutex_unlock(&zr->resource_lock);
2100 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2101 struct v4l2_format *fmt)
2103 struct zoran_fh *fh = __fh;
2104 struct zoran *zr = fh->zr;
2106 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2107 return zoran_g_fmt_vid_out(file, fh, fmt);
2109 mutex_lock(&zr->resource_lock);
2110 fmt->fmt.pix.width = fh->v4l_settings.width;
2111 fmt->fmt.pix.height = fh->v4l_settings.height;
2112 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2113 fh->v4l_settings.height;
2114 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2115 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2116 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2117 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2118 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2120 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2121 mutex_unlock(&zr->resource_lock);
2125 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2126 struct v4l2_format *fmt)
2128 struct zoran_fh *fh = __fh;
2129 struct zoran *zr = fh->zr;
2131 mutex_lock(&zr->resource_lock);
2133 fmt->fmt.win.w.left = fh->overlay_settings.x;
2134 fmt->fmt.win.w.top = fh->overlay_settings.y;
2135 fmt->fmt.win.w.width = fh->overlay_settings.width;
2136 fmt->fmt.win.w.height = fh->overlay_settings.height;
2137 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2138 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2140 fmt->fmt.win.field = V4L2_FIELD_TOP;
2142 mutex_unlock(&zr->resource_lock);
2146 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2147 struct v4l2_format *fmt)
2149 struct zoran_fh *fh = __fh;
2150 struct zoran *zr = fh->zr;
2152 mutex_lock(&zr->resource_lock);
2154 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2155 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2156 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2157 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2158 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2159 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2160 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2161 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2163 mutex_unlock(&zr->resource_lock);
2167 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2168 struct v4l2_format *fmt)
2170 struct zoran_fh *fh = __fh;
2171 struct zoran *zr = fh->zr;
2172 struct zoran_jpg_settings settings;
2175 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2178 fmt->fmt.pix.bytesperline = 0;
2180 mutex_lock(&zr->resource_lock);
2181 settings = fh->jpg_settings;
2183 /* we actually need to set 'real' parameters now */
2184 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2185 settings.TmpDcm = 1;
2187 settings.TmpDcm = 2;
2188 settings.decimation = 0;
2189 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2190 settings.VerDcm = 2;
2192 settings.VerDcm = 1;
2193 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2194 settings.HorDcm = 4;
2195 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2196 settings.HorDcm = 2;
2198 settings.HorDcm = 1;
2199 if (settings.TmpDcm == 1)
2200 settings.field_per_buff = 2;
2202 settings.field_per_buff = 1;
2205 res = zoran_check_jpg_settings(zr, &settings, 1);
2207 goto tryfmt_unlock_and_return;
2209 /* tell the user what we actually did */
2210 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2211 fmt->fmt.pix.height = settings.img_height * 2 /
2212 (settings.TmpDcm * settings.VerDcm);
2213 if (settings.TmpDcm == 1)
2214 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2215 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2217 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2218 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2220 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2221 tryfmt_unlock_and_return:
2222 mutex_unlock(&zr->resource_lock);
2226 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2227 struct v4l2_format *fmt)
2229 struct zoran_fh *fh = __fh;
2230 struct zoran *zr = fh->zr;
2234 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2235 return zoran_try_fmt_vid_out(file, fh, fmt);
2237 mutex_lock(&zr->resource_lock);
2239 for (i = 0; i < NUM_FORMATS; i++)
2240 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2243 if (i == NUM_FORMATS) {
2244 mutex_unlock(&zr->resource_lock);
2248 bpp = (zoran_formats[i].depth + 7) / 8;
2249 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2250 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2251 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2252 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2253 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2254 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2255 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2256 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2257 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2258 mutex_unlock(&zr->resource_lock);
2263 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2264 struct v4l2_format *fmt)
2266 struct zoran_fh *fh = __fh;
2267 struct zoran *zr = fh->zr;
2270 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2271 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2272 fmt->fmt.win.w.width,
2273 fmt->fmt.win.w.height,
2274 fmt->fmt.win.clipcount,
2275 fmt->fmt.win.bitmap);
2276 mutex_lock(&zr->resource_lock);
2277 res = setup_window(file, fmt->fmt.win.w.left,
2279 fmt->fmt.win.w.width,
2280 fmt->fmt.win.w.height,
2281 (struct video_clip __user *)
2283 fmt->fmt.win.clipcount,
2284 fmt->fmt.win.bitmap);
2285 mutex_unlock(&zr->resource_lock);
2289 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2290 struct v4l2_format *fmt)
2292 struct zoran_fh *fh = __fh;
2293 struct zoran *zr = fh->zr;
2294 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2295 struct zoran_jpg_settings settings;
2298 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2299 fmt->fmt.pix.width, fmt->fmt.pix.height,
2300 fmt->fmt.pix.pixelformat,
2301 (char *) &printformat);
2302 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2305 mutex_lock(&zr->resource_lock);
2307 settings = fh->jpg_settings;
2309 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2310 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2313 goto sfmtjpg_unlock_and_return;
2316 /* we actually need to set 'real' parameters now */
2317 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2318 settings.TmpDcm = 1;
2320 settings.TmpDcm = 2;
2321 settings.decimation = 0;
2322 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2323 settings.VerDcm = 2;
2325 settings.VerDcm = 1;
2326 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2327 settings.HorDcm = 4;
2328 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2329 settings.HorDcm = 2;
2331 settings.HorDcm = 1;
2332 if (settings.TmpDcm == 1)
2333 settings.field_per_buff = 2;
2335 settings.field_per_buff = 1;
2337 if (settings.HorDcm > 1) {
2338 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2339 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2342 settings.img_width = BUZ_MAX_WIDTH;
2346 res = zoran_check_jpg_settings(zr, &settings, 0);
2348 goto sfmtjpg_unlock_and_return;
2350 /* it's ok, so set them */
2351 fh->jpg_settings = settings;
2353 /* tell the user what we actually did */
2354 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2355 fmt->fmt.pix.height = settings.img_height * 2 /
2356 (settings.TmpDcm * settings.VerDcm);
2357 if (settings.TmpDcm == 1)
2358 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2359 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2361 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2362 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2363 fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2364 fmt->fmt.pix.bytesperline = 0;
2365 fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2366 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2368 /* we hereby abuse this variable to show that
2369 * we're gonna do mjpeg capture */
2370 fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2371 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2372 sfmtjpg_unlock_and_return:
2373 mutex_unlock(&zr->resource_lock);
2377 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2378 struct v4l2_format *fmt)
2380 struct zoran_fh *fh = __fh;
2381 struct zoran *zr = fh->zr;
2385 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2386 return zoran_s_fmt_vid_out(file, fh, fmt);
2388 for (i = 0; i < NUM_FORMATS; i++)
2389 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2391 if (i == NUM_FORMATS) {
2392 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2393 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2396 mutex_lock(&zr->resource_lock);
2397 if (fh->jpg_buffers.allocated ||
2398 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2399 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2402 goto sfmtv4l_unlock_and_return;
2404 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2405 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2406 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2407 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2409 res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2410 fmt->fmt.pix.height, &zoran_formats[i]);
2412 goto sfmtv4l_unlock_and_return;
2414 /* tell the user the
2415 * results/missing stuff */
2416 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2417 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2418 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2419 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2420 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2422 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2424 fh->map_mode = ZORAN_MAP_MODE_RAW;
2425 sfmtv4l_unlock_and_return:
2426 mutex_unlock(&zr->resource_lock);
2430 static int zoran_g_fbuf(struct file *file, void *__fh,
2431 struct v4l2_framebuffer *fb)
2433 struct zoran_fh *fh = __fh;
2434 struct zoran *zr = fh->zr;
2436 memset(fb, 0, sizeof(*fb));
2437 mutex_lock(&zr->resource_lock);
2438 fb->base = zr->buffer.base;
2439 fb->fmt.width = zr->buffer.width;
2440 fb->fmt.height = zr->buffer.height;
2441 if (zr->overlay_settings.format)
2442 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2443 fb->fmt.bytesperline = zr->buffer.bytesperline;
2444 mutex_unlock(&zr->resource_lock);
2445 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2446 fb->fmt.field = V4L2_FIELD_INTERLACED;
2447 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2448 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2453 static int zoran_s_fbuf(struct file *file, void *__fh,
2454 struct v4l2_framebuffer *fb)
2456 struct zoran_fh *fh = __fh;
2457 struct zoran *zr = fh->zr;
2459 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2461 for (i = 0; i < NUM_FORMATS; i++)
2462 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2464 if (i == NUM_FORMATS) {
2465 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2466 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2467 (char *)&printformat);
2471 mutex_lock(&zr->resource_lock);
2472 res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2473 fb->fmt.width, fb->fmt.height,
2474 fb->fmt.bytesperline);
2475 mutex_unlock(&zr->resource_lock);
2480 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2482 struct zoran_fh *fh = __fh;
2483 struct zoran *zr = fh->zr;
2486 mutex_lock(&zr->resource_lock);
2487 res = setup_overlay(file, on);
2488 mutex_unlock(&zr->resource_lock);
2493 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2495 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2497 struct zoran_fh *fh = __fh;
2498 struct zoran *zr = fh->zr;
2501 if (req->memory != V4L2_MEMORY_MMAP) {
2504 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2505 ZR_DEVNAME(zr), req->memory);
2509 if (req->count == 0)
2510 return zoran_streamoff(file, fh, req->type);
2512 mutex_lock(&zr->resource_lock);
2513 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2516 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2519 goto v4l2reqbuf_unlock_and_return;
2522 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2523 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2525 /* control user input */
2528 if (req->count > v4l_nbufs)
2529 req->count = v4l_nbufs;
2530 fh->v4l_buffers.num_buffers = req->count;
2532 if (v4l_fbuffer_alloc(file)) {
2534 goto v4l2reqbuf_unlock_and_return;
2537 /* The next mmap will map the V4L buffers */
2538 fh->map_mode = ZORAN_MAP_MODE_RAW;
2540 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2541 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2543 /* we need to calculate size ourselves now */
2546 if (req->count > jpg_nbufs)
2547 req->count = jpg_nbufs;
2548 fh->jpg_buffers.num_buffers = req->count;
2549 fh->jpg_buffers.buffer_size =
2550 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2552 if (jpg_fbuffer_alloc(file)) {
2554 goto v4l2reqbuf_unlock_and_return;
2557 /* The next mmap will map the MJPEG buffers */
2558 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2559 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2561 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2566 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2567 ZR_DEVNAME(zr), req->type);
2569 goto v4l2reqbuf_unlock_and_return;
2571 v4l2reqbuf_unlock_and_return:
2572 mutex_unlock(&zr->resource_lock);
2577 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2579 struct zoran_fh *fh = __fh;
2580 struct zoran *zr = fh->zr;
2581 __u32 type = buf->type;
2582 int index = buf->index, res;
2584 memset(buf, 0, sizeof(*buf));
2588 mutex_lock(&zr->resource_lock);
2589 res = zoran_v4l2_buffer_status(file, buf, buf->index);
2590 mutex_unlock(&zr->resource_lock);
2595 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2597 struct zoran_fh *fh = __fh;
2598 struct zoran *zr = fh->zr;
2599 int res = 0, codec_mode, buf_type;
2601 mutex_lock(&zr->resource_lock);
2603 switch (fh->map_mode) {
2604 case ZORAN_MAP_MODE_RAW:
2605 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2607 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2608 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2610 goto qbuf_unlock_and_return;
2613 res = zoran_v4l_queue_frame(file, buf->index);
2615 goto qbuf_unlock_and_return;
2616 if (!zr->v4l_memgrab_active &&
2617 fh->v4l_buffers.active == ZORAN_LOCKED)
2618 zr36057_set_memgrab(zr, 1);
2621 case ZORAN_MAP_MODE_JPG_REC:
2622 case ZORAN_MAP_MODE_JPG_PLAY:
2623 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2624 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2625 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2627 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2628 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2631 if (buf->type != buf_type) {
2633 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2634 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2636 goto qbuf_unlock_and_return;
2639 res = zoran_jpg_queue_frame(file, buf->index,
2642 goto qbuf_unlock_and_return;
2643 if (zr->codec_mode == BUZ_MODE_IDLE &&
2644 fh->jpg_buffers.active == ZORAN_LOCKED) {
2645 zr36057_enable_jpg(zr, codec_mode);
2651 "%s: VIDIOC_QBUF - unsupported type %d\n",
2652 ZR_DEVNAME(zr), buf->type);
2656 qbuf_unlock_and_return:
2657 mutex_unlock(&zr->resource_lock);
2662 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2664 struct zoran_fh *fh = __fh;
2665 struct zoran *zr = fh->zr;
2666 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2668 mutex_lock(&zr->resource_lock);
2670 switch (fh->map_mode) {
2671 case ZORAN_MAP_MODE_RAW:
2672 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2674 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2675 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2677 goto dqbuf_unlock_and_return;
2680 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2681 if (file->f_flags & O_NONBLOCK &&
2682 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2684 goto dqbuf_unlock_and_return;
2686 res = v4l_sync(file, num);
2688 goto dqbuf_unlock_and_return;
2689 zr->v4l_sync_tail++;
2690 res = zoran_v4l2_buffer_status(file, buf, num);
2693 case ZORAN_MAP_MODE_JPG_REC:
2694 case ZORAN_MAP_MODE_JPG_PLAY:
2696 struct zoran_sync bs;
2698 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2699 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2701 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2703 if (buf->type != buf_type) {
2705 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2706 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2708 goto dqbuf_unlock_and_return;
2711 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2713 if (file->f_flags & O_NONBLOCK &&
2714 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2716 goto dqbuf_unlock_and_return;
2718 res = jpg_sync(file, &bs);
2720 goto dqbuf_unlock_and_return;
2721 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2727 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2728 ZR_DEVNAME(zr), buf->type);
2732 dqbuf_unlock_and_return:
2733 mutex_unlock(&zr->resource_lock);
2738 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2740 struct zoran_fh *fh = __fh;
2741 struct zoran *zr = fh->zr;
2744 mutex_lock(&zr->resource_lock);
2746 switch (fh->map_mode) {
2747 case ZORAN_MAP_MODE_RAW: /* raw capture */
2748 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2749 fh->v4l_buffers.active != ZORAN_ACTIVE) {
2751 goto strmon_unlock_and_return;
2754 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2755 zr->v4l_settings = fh->v4l_settings;
2757 zr->v4l_sync_tail = zr->v4l_pend_tail;
2758 if (!zr->v4l_memgrab_active &&
2759 zr->v4l_pend_head != zr->v4l_pend_tail) {
2760 zr36057_set_memgrab(zr, 1);
2764 case ZORAN_MAP_MODE_JPG_REC:
2765 case ZORAN_MAP_MODE_JPG_PLAY:
2766 /* what is the codec mode right now? */
2767 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2768 fh->jpg_buffers.active != ZORAN_ACTIVE) {
2770 goto strmon_unlock_and_return;
2773 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2775 if (zr->jpg_que_head != zr->jpg_que_tail) {
2776 /* Start the jpeg codec when the first frame is queued */
2784 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2785 ZR_DEVNAME(zr), fh->map_mode);
2789 strmon_unlock_and_return:
2790 mutex_unlock(&zr->resource_lock);
2795 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2797 struct zoran_fh *fh = __fh;
2798 struct zoran *zr = fh->zr;
2801 mutex_lock(&zr->resource_lock);
2803 switch (fh->map_mode) {
2804 case ZORAN_MAP_MODE_RAW: /* raw capture */
2805 if (fh->v4l_buffers.active == ZORAN_FREE &&
2806 zr->v4l_buffers.active != ZORAN_FREE) {
2807 res = -EPERM; /* stay off other's settings! */
2808 goto strmoff_unlock_and_return;
2810 if (zr->v4l_buffers.active == ZORAN_FREE)
2811 goto strmoff_unlock_and_return;
2813 /* unload capture */
2814 if (zr->v4l_memgrab_active) {
2815 unsigned long flags;
2817 spin_lock_irqsave(&zr->spinlock, flags);
2818 zr36057_set_memgrab(zr, 0);
2819 spin_unlock_irqrestore(&zr->spinlock, flags);
2822 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2823 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2824 fh->v4l_buffers = zr->v4l_buffers;
2826 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2828 zr->v4l_grab_seq = 0;
2829 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2830 zr->v4l_sync_tail = 0;
2834 case ZORAN_MAP_MODE_JPG_REC:
2835 case ZORAN_MAP_MODE_JPG_PLAY:
2836 if (fh->jpg_buffers.active == ZORAN_FREE &&
2837 zr->jpg_buffers.active != ZORAN_FREE) {
2838 res = -EPERM; /* stay off other's settings! */
2839 goto strmoff_unlock_and_return;
2841 if (zr->jpg_buffers.active == ZORAN_FREE)
2842 goto strmoff_unlock_and_return;
2844 res = jpg_qbuf(file, -1,
2845 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2846 BUZ_MODE_MOTION_COMPRESS :
2847 BUZ_MODE_MOTION_DECOMPRESS);
2849 goto strmoff_unlock_and_return;
2853 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2854 ZR_DEVNAME(zr), fh->map_mode);
2858 strmoff_unlock_and_return:
2859 mutex_unlock(&zr->resource_lock);
2864 static int zoran_queryctrl(struct file *file, void *__fh,
2865 struct v4l2_queryctrl *ctrl)
2867 struct zoran_fh *fh = __fh;
2868 struct zoran *zr = fh->zr;
2870 /* we only support hue/saturation/contrast/brightness */
2871 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2872 ctrl->id > V4L2_CID_HUE)
2875 decoder_command(zr, VIDIOC_QUERYCTRL, ctrl);
2880 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2882 struct zoran_fh *fh = __fh;
2883 struct zoran *zr = fh->zr;
2885 /* we only support hue/saturation/contrast/brightness */
2886 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2887 ctrl->id > V4L2_CID_HUE)
2890 mutex_lock(&zr->resource_lock);
2891 decoder_command(zr, VIDIOC_G_CTRL, ctrl);
2892 mutex_unlock(&zr->resource_lock);
2897 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2899 struct zoran_fh *fh = __fh;
2900 struct zoran *zr = fh->zr;
2902 /* we only support hue/saturation/contrast/brightness */
2903 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2904 ctrl->id > V4L2_CID_HUE)
2907 mutex_lock(&zr->resource_lock);
2908 decoder_command(zr, VIDIOC_S_CTRL, ctrl);
2909 mutex_unlock(&zr->resource_lock);
2914 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2916 struct zoran_fh *fh = __fh;
2917 struct zoran *zr = fh->zr;
2919 mutex_lock(&zr->resource_lock);
2921 mutex_unlock(&zr->resource_lock);
2925 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2927 struct zoran_fh *fh = __fh;
2928 struct zoran *zr = fh->zr;
2931 mutex_lock(&zr->resource_lock);
2932 res = zoran_set_norm(zr, *std);
2934 goto sstd_unlock_and_return;
2936 res = wait_grab_pending(zr);
2937 sstd_unlock_and_return:
2938 mutex_unlock(&zr->resource_lock);
2942 static int zoran_enum_input(struct file *file, void *__fh,
2943 struct v4l2_input *inp)
2945 struct zoran_fh *fh = __fh;
2946 struct zoran *zr = fh->zr;
2948 if (inp->index < 0 || inp->index >= zr->card.inputs)
2951 int id = inp->index;
2952 memset(inp, 0, sizeof(*inp));
2956 strncpy(inp->name, zr->card.input[inp->index].name,
2957 sizeof(inp->name) - 1);
2958 inp->type = V4L2_INPUT_TYPE_CAMERA;
2959 inp->std = V4L2_STD_ALL;
2961 /* Get status of video decoder */
2962 mutex_lock(&zr->resource_lock);
2963 decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &inp->status);
2964 mutex_unlock(&zr->resource_lock);
2968 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2970 struct zoran_fh *fh = __fh;
2971 struct zoran *zr = fh->zr;
2973 mutex_lock(&zr->resource_lock);
2975 mutex_unlock(&zr->resource_lock);
2980 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2982 struct zoran_fh *fh = __fh;
2983 struct zoran *zr = fh->zr;
2986 mutex_lock(&zr->resource_lock);
2987 res = zoran_set_input(zr, input);
2989 goto sinput_unlock_and_return;
2991 /* Make sure the changes come into effect */
2992 res = wait_grab_pending(zr);
2993 sinput_unlock_and_return:
2994 mutex_unlock(&zr->resource_lock);
2998 static int zoran_enum_output(struct file *file, void *__fh,
2999 struct v4l2_output *outp)
3001 if (outp->index != 0)
3004 memset(outp, 0, sizeof(*outp));
3006 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3007 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3012 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
3019 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
3027 /* cropping (sub-frame capture) */
3028 static int zoran_cropcap(struct file *file, void *__fh,
3029 struct v4l2_cropcap *cropcap)
3031 struct zoran_fh *fh = __fh;
3032 struct zoran *zr = fh->zr;
3033 int type = cropcap->type, res = 0;
3035 memset(cropcap, 0, sizeof(*cropcap));
3036 cropcap->type = type;
3038 mutex_lock(&zr->resource_lock);
3040 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3041 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3042 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3044 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3047 goto cropcap_unlock_and_return;
3050 cropcap->bounds.top = cropcap->bounds.left = 0;
3051 cropcap->bounds.width = BUZ_MAX_WIDTH;
3052 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3053 cropcap->defrect.top = cropcap->defrect.left = 0;
3054 cropcap->defrect.width = BUZ_MIN_WIDTH;
3055 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3056 cropcap_unlock_and_return:
3057 mutex_unlock(&zr->resource_lock);
3061 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3063 struct zoran_fh *fh = __fh;
3064 struct zoran *zr = fh->zr;
3065 int type = crop->type, res = 0;
3067 memset(crop, 0, sizeof(*crop));
3070 mutex_lock(&zr->resource_lock);
3072 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3073 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3074 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3077 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3080 goto gcrop_unlock_and_return;
3083 crop->c.top = fh->jpg_settings.img_y;
3084 crop->c.left = fh->jpg_settings.img_x;
3085 crop->c.width = fh->jpg_settings.img_width;
3086 crop->c.height = fh->jpg_settings.img_height;
3088 gcrop_unlock_and_return:
3089 mutex_unlock(&zr->resource_lock);
3094 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3096 struct zoran_fh *fh = __fh;
3097 struct zoran *zr = fh->zr;
3099 struct zoran_jpg_settings settings;
3101 settings = fh->jpg_settings;
3103 mutex_lock(&zr->resource_lock);
3105 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3107 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3110 goto scrop_unlock_and_return;
3113 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3114 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3115 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3117 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3120 goto scrop_unlock_and_return;
3123 /* move into a form that we understand */
3124 settings.img_x = crop->c.left;
3125 settings.img_y = crop->c.top;
3126 settings.img_width = crop->c.width;
3127 settings.img_height = crop->c.height;
3129 /* check validity */
3130 res = zoran_check_jpg_settings(zr, &settings, 0);
3132 goto scrop_unlock_and_return;
3135 fh->jpg_settings = settings;
3137 scrop_unlock_and_return:
3138 mutex_unlock(&zr->resource_lock);
3142 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3143 struct v4l2_jpegcompression *params)
3145 struct zoran_fh *fh = __fh;
3146 struct zoran *zr = fh->zr;
3147 memset(params, 0, sizeof(*params));
3149 mutex_lock(&zr->resource_lock);
3151 params->quality = fh->jpg_settings.jpg_comp.quality;
3152 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3153 memcpy(params->APP_data,
3154 fh->jpg_settings.jpg_comp.APP_data,
3155 fh->jpg_settings.jpg_comp.APP_len);
3156 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3157 memcpy(params->COM_data,
3158 fh->jpg_settings.jpg_comp.COM_data,
3159 fh->jpg_settings.jpg_comp.COM_len);
3160 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3161 params->jpeg_markers =
3162 fh->jpg_settings.jpg_comp.jpeg_markers;
3164 mutex_unlock(&zr->resource_lock);
3169 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3170 struct v4l2_jpegcompression *params)
3172 struct zoran_fh *fh = __fh;
3173 struct zoran *zr = fh->zr;
3175 struct zoran_jpg_settings settings;
3177 settings = fh->jpg_settings;
3179 settings.jpg_comp = *params;
3181 mutex_lock(&zr->resource_lock);
3183 if (fh->v4l_buffers.active != ZORAN_FREE ||
3184 fh->jpg_buffers.active != ZORAN_FREE) {
3185 dprintk(1, KERN_WARNING
3186 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3189 goto sjpegc_unlock_and_return;
3192 res = zoran_check_jpg_settings(zr, &settings, 0);
3194 goto sjpegc_unlock_and_return;
3195 if (!fh->jpg_buffers.allocated)
3196 fh->jpg_buffers.buffer_size =
3197 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3198 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3199 sjpegc_unlock_and_return:
3200 mutex_unlock(&zr->resource_lock);
3206 zoran_poll (struct file *file,
3209 struct zoran_fh *fh = file->private_data;
3210 struct zoran *zr = fh->zr;
3212 unsigned long flags;
3214 /* we should check whether buffers are ready to be synced on
3215 * (w/o waits - O_NONBLOCK) here
3216 * if ready for read (sync), return POLLIN|POLLRDNORM,
3217 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3218 * if error, return POLLERR,
3219 * if no buffers queued or so, return POLLNVAL
3222 mutex_lock(&zr->resource_lock);
3224 switch (fh->map_mode) {
3225 case ZORAN_MAP_MODE_RAW:
3226 poll_wait(file, &zr->v4l_capq, wait);
3227 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3229 spin_lock_irqsave(&zr->spinlock, flags);
3232 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3233 ZR_DEVNAME(zr), __func__,
3234 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3235 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3236 zr->v4l_pend_tail, zr->v4l_pend_head);
3237 /* Process is the one capturing? */
3238 if (fh->v4l_buffers.active != ZORAN_FREE &&
3239 /* Buffer ready to DQBUF? */
3240 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3241 res = POLLIN | POLLRDNORM;
3242 spin_unlock_irqrestore(&zr->spinlock, flags);
3246 case ZORAN_MAP_MODE_JPG_REC:
3247 case ZORAN_MAP_MODE_JPG_PLAY:
3248 poll_wait(file, &zr->jpg_capq, wait);
3249 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3251 spin_lock_irqsave(&zr->spinlock, flags);
3254 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3255 ZR_DEVNAME(zr), __func__,
3256 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3257 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3258 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3259 if (fh->jpg_buffers.active != ZORAN_FREE &&
3260 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3261 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3262 res = POLLIN | POLLRDNORM;
3264 res = POLLOUT | POLLWRNORM;
3266 spin_unlock_irqrestore(&zr->spinlock, flags);
3273 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3274 ZR_DEVNAME(zr), fh->map_mode);
3278 mutex_unlock(&zr->resource_lock);
3285 * This maps the buffers to user space.
3287 * Depending on the state of fh->map_mode
3288 * the V4L or the MJPEG buffers are mapped
3289 * per buffer or all together
3291 * Note that we need to connect to some
3292 * unmap signal event to unmap the de-allocate
3293 * the buffer accordingly (zoran_vm_close())
3297 zoran_vm_open (struct vm_area_struct *vma)
3299 struct zoran_mapping *map = vma->vm_private_data;
3305 zoran_vm_close (struct vm_area_struct *vma)
3307 struct zoran_mapping *map = vma->vm_private_data;
3308 struct file *file = map->file;
3309 struct zoran_fh *fh = file->private_data;
3310 struct zoran *zr = fh->zr;
3314 if (map->count == 0) {
3315 switch (fh->map_mode) {
3316 case ZORAN_MAP_MODE_JPG_REC:
3317 case ZORAN_MAP_MODE_JPG_PLAY:
3319 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3322 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3323 if (fh->jpg_buffers.buffer[i].map == map) {
3324 fh->jpg_buffers.buffer[i].map =
3330 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3331 if (fh->jpg_buffers.buffer[i].map)
3333 if (i == fh->jpg_buffers.num_buffers) {
3334 mutex_lock(&zr->resource_lock);
3336 if (fh->jpg_buffers.active != ZORAN_FREE) {
3337 jpg_qbuf(file, -1, zr->codec_mode);
3338 zr->jpg_buffers.allocated = 0;
3339 zr->jpg_buffers.active =
3340 fh->jpg_buffers.active =
3343 jpg_fbuffer_free(file);
3344 mutex_unlock(&zr->resource_lock);
3349 case ZORAN_MAP_MODE_RAW:
3351 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3354 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3355 if (fh->v4l_buffers.buffer[i].map == map) {
3357 fh->v4l_buffers.buffer[i].map =
3363 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3364 if (fh->v4l_buffers.buffer[i].map)
3366 if (i == fh->v4l_buffers.num_buffers) {
3367 mutex_lock(&zr->resource_lock);
3369 if (fh->v4l_buffers.active != ZORAN_FREE) {
3370 unsigned long flags;
3372 spin_lock_irqsave(&zr->spinlock, flags);
3373 zr36057_set_memgrab(zr, 0);
3374 zr->v4l_buffers.allocated = 0;
3375 zr->v4l_buffers.active =
3376 fh->v4l_buffers.active =
3378 spin_unlock_irqrestore(&zr->spinlock, flags);
3380 v4l_fbuffer_free(file);
3381 mutex_unlock(&zr->resource_lock);
3388 "%s: munmap() - internal error - unknown map mode %d\n",
3389 ZR_DEVNAME(zr), fh->map_mode);
3396 static struct vm_operations_struct zoran_vm_ops = {
3397 .open = zoran_vm_open,
3398 .close = zoran_vm_close,
3402 zoran_mmap (struct file *file,
3403 struct vm_area_struct *vma)
3405 struct zoran_fh *fh = file->private_data;
3406 struct zoran *zr = fh->zr;
3407 unsigned long size = (vma->vm_end - vma->vm_start);
3408 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3410 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3412 struct zoran_mapping *map;
3416 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3418 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3419 vma->vm_start, vma->vm_end, size);
3421 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3422 !(vma->vm_flags & VM_WRITE)) {
3425 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3430 switch (fh->map_mode) {
3432 case ZORAN_MAP_MODE_JPG_REC:
3433 case ZORAN_MAP_MODE_JPG_PLAY:
3436 mutex_lock(&zr->resource_lock);
3438 /* Map the MJPEG buffers */
3439 if (!fh->jpg_buffers.allocated) {
3442 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3445 goto jpg_mmap_unlock_and_return;
3448 first = offset / fh->jpg_buffers.buffer_size;
3449 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3450 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3451 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3452 last < 0 || first >= fh->jpg_buffers.num_buffers ||
3453 last >= fh->jpg_buffers.num_buffers) {
3456 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3457 ZR_DEVNAME(zr), offset, size,
3458 fh->jpg_buffers.buffer_size,
3459 fh->jpg_buffers.num_buffers);
3461 goto jpg_mmap_unlock_and_return;
3463 for (i = first; i <= last; i++) {
3464 if (fh->jpg_buffers.buffer[i].map) {
3467 "%s: mmap(MJPEG) - buffer %d already mapped\n",
3470 goto jpg_mmap_unlock_and_return;
3474 /* map these buffers (v4l_buffers[i]) */
3475 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3478 goto jpg_mmap_unlock_and_return;
3483 vma->vm_ops = &zoran_vm_ops;
3484 vma->vm_flags |= VM_DONTEXPAND;
3485 vma->vm_private_data = map;
3487 for (i = first; i <= last; i++) {
3489 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3492 (le32_to_cpu(fh->jpg_buffers.buffer[i].
3493 frag_tab[2 * j + 1]) & ~1) << 1;
3498 le32_to_cpu(fh->jpg_buffers.
3499 buffer[i].frag_tab[2 * j]);
3500 /* should just be pos on i386 */
3501 page = virt_to_phys(bus_to_virt(pos))
3503 if (remap_pfn_range(vma, start, page,
3504 todo, PAGE_SHARED)) {
3507 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3510 goto jpg_mmap_unlock_and_return;
3516 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3517 frag_tab[2 * j + 1]) & 1)
3518 break; /* was last fragment */
3520 fh->jpg_buffers.buffer[i].map = map;
3525 jpg_mmap_unlock_and_return:
3526 mutex_unlock(&zr->resource_lock);
3530 case ZORAN_MAP_MODE_RAW:
3532 mutex_lock(&zr->resource_lock);
3534 /* Map the V4L buffers */
3535 if (!fh->v4l_buffers.allocated) {
3538 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3541 goto v4l_mmap_unlock_and_return;
3544 first = offset / fh->v4l_buffers.buffer_size;
3545 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3546 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3547 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3548 last < 0 || first >= fh->v4l_buffers.num_buffers ||
3549 last >= fh->v4l_buffers.buffer_size) {
3552 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3553 ZR_DEVNAME(zr), offset, size,
3554 fh->v4l_buffers.buffer_size,
3555 fh->v4l_buffers.num_buffers);
3557 goto v4l_mmap_unlock_and_return;
3559 for (i = first; i <= last; i++) {
3560 if (fh->v4l_buffers.buffer[i].map) {
3563 "%s: mmap(V4L) - buffer %d already mapped\n",
3566 goto v4l_mmap_unlock_and_return;
3570 /* map these buffers (v4l_buffers[i]) */
3571 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3574 goto v4l_mmap_unlock_and_return;
3579 vma->vm_ops = &zoran_vm_ops;
3580 vma->vm_flags |= VM_DONTEXPAND;
3581 vma->vm_private_data = map;
3583 for (i = first; i <= last; i++) {
3585 if (todo > fh->v4l_buffers.buffer_size)
3586 todo = fh->v4l_buffers.buffer_size;
3587 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3588 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3589 todo, PAGE_SHARED)) {
3592 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3595 goto v4l_mmap_unlock_and_return;
3599 fh->v4l_buffers.buffer[i].map = map;
3603 v4l_mmap_unlock_and_return:
3604 mutex_unlock(&zr->resource_lock);
3611 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3612 ZR_DEVNAME(zr), fh->map_mode);
3619 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3620 .vidioc_querycap = zoran_querycap,
3621 .vidioc_cropcap = zoran_cropcap,
3622 .vidioc_s_crop = zoran_s_crop,
3623 .vidioc_g_crop = zoran_g_crop,
3624 .vidioc_enum_input = zoran_enum_input,
3625 .vidioc_g_input = zoran_g_input,
3626 .vidioc_s_input = zoran_s_input,
3627 .vidioc_enum_output = zoran_enum_output,
3628 .vidioc_g_output = zoran_g_output,
3629 .vidioc_s_output = zoran_s_output,
3630 .vidioc_g_fbuf = zoran_g_fbuf,
3631 .vidioc_s_fbuf = zoran_s_fbuf,
3632 .vidioc_g_std = zoran_g_std,
3633 .vidioc_s_std = zoran_s_std,
3634 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3635 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3636 .vidioc_overlay = zoran_overlay,
3637 .vidioc_reqbufs = zoran_reqbufs,
3638 .vidioc_querybuf = zoran_querybuf,
3639 .vidioc_qbuf = zoran_qbuf,
3640 .vidioc_dqbuf = zoran_dqbuf,
3641 .vidioc_streamon = zoran_streamon,
3642 .vidioc_streamoff = zoran_streamoff,
3643 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3644 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3645 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3646 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3647 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3648 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3649 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3650 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3651 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3652 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3653 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3654 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3655 .vidioc_queryctrl = zoran_queryctrl,
3656 .vidioc_s_ctrl = zoran_s_ctrl,
3657 .vidioc_g_ctrl = zoran_g_ctrl,
3658 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3659 .vidioc_default = zoran_default,
3660 .vidiocgmbuf = zoran_vidiocgmbuf,
3664 static const struct v4l2_file_operations zoran_fops = {
3665 .owner = THIS_MODULE,
3667 .release = zoran_close,
3668 .ioctl = video_ioctl2,
3670 .write = zoran_write,
3675 struct video_device zoran_template __devinitdata = {
3677 .fops = &zoran_fops,
3678 .ioctl_ops = &zoran_ioctl_ops,
3679 .release = &zoran_vdev_release,
3680 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,