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 int zero = 0, two = 2;
1149 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1150 encoder_command(zr, ENCODER_SET_INPUT, &two);
1154 file->private_data = NULL;
1155 kfree(fh->overlay_mask);
1158 /* release locks on the i2c modules */
1159 module_put(zr->decoder->driver->driver.owner);
1161 module_put(zr->encoder->driver->driver.owner);
1163 /*mutex_unlock(&zr->resource_lock);*/
1165 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1172 zoran_read (struct file *file,
1177 /* we simply don't support read() (yet)... */
1183 zoran_write (struct file *file,
1184 const char __user *data,
1188 /* ...and the same goes for write() */
1194 setup_fbuffer (struct file *file,
1196 const struct zoran_format *fmt,
1201 struct zoran_fh *fh = file->private_data;
1202 struct zoran *zr = fh->zr;
1204 /* (Ronald) v4l/v4l2 guidelines */
1205 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1208 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1209 ALi Magik (that needs very low latency while the card needs a
1210 higher value always) */
1212 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1215 /* we need a bytesperline value, even if not given */
1217 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1220 if (zr->overlay_active) {
1221 /* dzjee... stupid users... don't even bother to turn off
1222 * overlay before changing the memory location...
1223 * normally, we would return errors here. However, one of
1224 * the tools that does this is... xawtv! and since xawtv
1225 * is used by +/- 99% of the users, we'd rather be user-
1226 * friendly and silently do as if nothing went wrong */
1229 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1231 zr36057_overlay(zr, 0);
1235 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1238 "%s: setup_fbuffer() - no valid overlay format given\n",
1242 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1245 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1246 ZR_DEVNAME(zr), width, height, bytesperline);
1249 if (bytesperline & 3) {
1252 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1253 ZR_DEVNAME(zr), bytesperline);
1257 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1258 zr->buffer.height = height;
1259 zr->buffer.width = width;
1260 zr->buffer.depth = fmt->depth;
1261 zr->overlay_settings.format = fmt;
1262 zr->buffer.bytesperline = bytesperline;
1264 /* The user should set new window parameters */
1265 zr->overlay_settings.is_set = 0;
1272 setup_window (struct file *file,
1277 struct video_clip __user *clips,
1279 void __user *bitmap)
1281 struct zoran_fh *fh = file->private_data;
1282 struct zoran *zr = fh->zr;
1283 struct video_clip *vcp = NULL;
1287 if (!zr->buffer.base) {
1290 "%s: setup_window() - frame buffer has to be set first\n",
1295 if (!fh->overlay_settings.format) {
1298 "%s: setup_window() - no overlay format set\n",
1304 * The video front end needs 4-byte alinged line sizes, we correct that
1305 * silently here if necessary
1307 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1308 end = (x + width) & ~1; /* round down */
1309 x = (x + 1) & ~1; /* round up */
1313 if (zr->buffer.depth == 24) {
1314 end = (x + width) & ~3; /* round down */
1315 x = (x + 3) & ~3; /* round up */
1319 if (width > BUZ_MAX_WIDTH)
1320 width = BUZ_MAX_WIDTH;
1321 if (height > BUZ_MAX_HEIGHT)
1322 height = BUZ_MAX_HEIGHT;
1324 /* Check for vaild parameters */
1325 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1326 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1329 "%s: setup_window() - width = %d or height = %d invalid\n",
1330 ZR_DEVNAME(zr), width, height);
1334 fh->overlay_settings.x = x;
1335 fh->overlay_settings.y = y;
1336 fh->overlay_settings.width = width;
1337 fh->overlay_settings.height = height;
1338 fh->overlay_settings.clipcount = clipcount;
1341 * If an overlay is running, we have to switch it off
1342 * and switch it on again in order to get the new settings in effect.
1344 * We also want to avoid that the overlay mask is written
1345 * when an overlay is running.
1348 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1349 zr->overlay_active != ZORAN_FREE &&
1350 fh->overlay_active != ZORAN_FREE;
1352 zr36057_overlay(zr, 0);
1355 * Write the overlay mask if clips are wanted.
1356 * We prefer a bitmap.
1359 /* fake value - it just means we want clips */
1360 fh->overlay_settings.clipcount = 1;
1362 if (copy_from_user(fh->overlay_mask, bitmap,
1363 (width * height + 7) / 8)) {
1366 } else if (clipcount > 0) {
1367 /* write our own bitmap from the clips */
1368 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1372 "%s: setup_window() - Alloc of clip mask failed\n",
1377 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1381 write_overlay_mask(file, vcp, clipcount);
1385 fh->overlay_settings.is_set = 1;
1386 if (fh->overlay_active != ZORAN_FREE &&
1387 zr->overlay_active != ZORAN_FREE)
1388 zr->overlay_settings = fh->overlay_settings;
1391 zr36057_overlay(zr, 1);
1393 /* Make sure the changes come into effect */
1394 return wait_grab_pending(zr);
1398 setup_overlay (struct file *file,
1401 struct zoran_fh *fh = file->private_data;
1402 struct zoran *zr = fh->zr;
1404 /* If there is nothing to do, return immediatly */
1405 if ((on && fh->overlay_active != ZORAN_FREE) ||
1406 (!on && fh->overlay_active == ZORAN_FREE))
1409 /* check whether we're touching someone else's overlay */
1410 if (on && zr->overlay_active != ZORAN_FREE &&
1411 fh->overlay_active == ZORAN_FREE) {
1414 "%s: setup_overlay() - overlay is already active for another session\n",
1418 if (!on && zr->overlay_active != ZORAN_FREE &&
1419 fh->overlay_active == ZORAN_FREE) {
1422 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1428 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1429 zr->v4l_overlay_active = 0;
1430 /* When a grab is running, the video simply
1431 * won't be switched on any more */
1432 if (!zr->v4l_memgrab_active)
1433 zr36057_overlay(zr, 0);
1434 zr->overlay_mask = NULL;
1436 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1439 "%s: setup_overlay() - buffer or window not set\n",
1443 if (!fh->overlay_settings.format) {
1446 "%s: setup_overlay() - no overlay format set\n",
1450 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1451 zr->v4l_overlay_active = 1;
1452 zr->overlay_mask = fh->overlay_mask;
1453 zr->overlay_settings = fh->overlay_settings;
1454 if (!zr->v4l_memgrab_active)
1455 zr36057_overlay(zr, 1);
1456 /* When a grab is running, the video will be
1457 * switched on when grab is finished */
1460 /* Make sure the changes come into effect */
1461 return wait_grab_pending(zr);
1464 /* get the status of a buffer in the clients buffer queue */
1466 zoran_v4l2_buffer_status (struct file *file,
1467 struct v4l2_buffer *buf,
1470 struct zoran_fh *fh = file->private_data;
1471 struct zoran *zr = fh->zr;
1473 buf->flags = V4L2_BUF_FLAG_MAPPED;
1475 switch (fh->map_mode) {
1476 case ZORAN_MAP_MODE_RAW:
1479 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1480 !fh->v4l_buffers.allocated) {
1483 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1488 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1489 buf->length = fh->v4l_buffers.buffer_size;
1492 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1493 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1494 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1495 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1496 buf->flags |= V4L2_BUF_FLAG_DONE;
1498 fh->v4l_buffers.buffer[num].bs.timestamp;
1500 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1503 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1504 buf->field = V4L2_FIELD_TOP;
1506 buf->field = V4L2_FIELD_INTERLACED;
1510 case ZORAN_MAP_MODE_JPG_REC:
1511 case ZORAN_MAP_MODE_JPG_PLAY:
1514 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1515 !fh->jpg_buffers.allocated) {
1518 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1523 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1524 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1525 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1526 buf->length = fh->jpg_buffers.buffer_size;
1528 /* these variables are only written after frame has been captured */
1529 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1530 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1531 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1533 fh->jpg_buffers.buffer[num].bs.timestamp;
1535 fh->jpg_buffers.buffer[num].bs.length;
1536 buf->flags |= V4L2_BUF_FLAG_DONE;
1538 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1541 /* which fields are these? */
1542 if (fh->jpg_settings.TmpDcm != 1)
1545 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1549 odd_even ? V4L2_FIELD_SEQ_TB :
1558 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1559 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1563 buf->memory = V4L2_MEMORY_MMAP;
1565 buf->m.offset = buf->length * num;
1571 zoran_set_norm (struct zoran *zr,
1572 int norm) /* VIDEO_MODE_* */
1574 int norm_encoder, on;
1576 if (zr->v4l_buffers.active != ZORAN_FREE ||
1577 zr->jpg_buffers.active != ZORAN_FREE) {
1580 "%s: set_norm() called while in playback/capture mode\n",
1585 if (lock_norm && norm != zr->norm) {
1586 if (lock_norm > 1) {
1589 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1595 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1601 if (norm != VIDEO_MODE_AUTO &&
1602 (norm < 0 || norm >= zr->card.norms ||
1603 !zr->card.tvn[norm])) {
1605 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1606 ZR_DEVNAME(zr), norm);
1610 if (norm == VIDEO_MODE_AUTO) {
1613 /* if we have autodetect, ... */
1614 struct video_decoder_capability caps;
1615 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1616 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1617 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1622 decoder_command(zr, DECODER_SET_NORM, &norm);
1624 /* let changes come into effect */
1627 decoder_command(zr, DECODER_GET_STATUS, &status);
1628 if (!(status & DECODER_STATUS_GOOD)) {
1631 "%s: set_norm() - no norm detected\n",
1634 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1638 if (status & DECODER_STATUS_NTSC)
1639 norm = VIDEO_MODE_NTSC;
1640 else if (status & DECODER_STATUS_SECAM)
1641 norm = VIDEO_MODE_SECAM;
1643 norm = VIDEO_MODE_PAL;
1645 zr->timing = zr->card.tvn[norm];
1646 norm_encoder = norm;
1648 /* We switch overlay off and on since a change in the
1649 * norm needs different VFE settings */
1650 on = zr->overlay_active && !zr->v4l_memgrab_active;
1652 zr36057_overlay(zr, 0);
1654 decoder_command(zr, DECODER_SET_NORM, &norm);
1655 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1658 zr36057_overlay(zr, 1);
1660 /* Make sure the changes come into effect */
1667 zoran_set_input (struct zoran *zr,
1672 if (input == zr->input) {
1676 if (zr->v4l_buffers.active != ZORAN_FREE ||
1677 zr->jpg_buffers.active != ZORAN_FREE) {
1680 "%s: set_input() called while in playback/capture mode\n",
1685 if (input < 0 || input >= zr->card.inputs) {
1688 "%s: set_input() - unnsupported input %d\n",
1689 ZR_DEVNAME(zr), input);
1693 realinput = zr->card.input[input].muxsel;
1696 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1705 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1706 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1708 struct zoran_fh *fh = __fh;
1709 struct zoran *zr = fh->zr;
1710 struct zoran_jpg_settings settings;
1713 case BUZIOC_G_PARAMS:
1715 struct zoran_params *bparams = arg;
1717 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1719 memset(bparams, 0, sizeof(struct zoran_params));
1720 bparams->major_version = MAJOR_VERSION;
1721 bparams->minor_version = MINOR_VERSION;
1723 mutex_lock(&zr->resource_lock);
1725 bparams->norm = zr->norm;
1726 bparams->input = zr->input;
1728 bparams->decimation = fh->jpg_settings.decimation;
1729 bparams->HorDcm = fh->jpg_settings.HorDcm;
1730 bparams->VerDcm = fh->jpg_settings.VerDcm;
1731 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1732 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1733 bparams->img_x = fh->jpg_settings.img_x;
1734 bparams->img_y = fh->jpg_settings.img_y;
1735 bparams->img_width = fh->jpg_settings.img_width;
1736 bparams->img_height = fh->jpg_settings.img_height;
1737 bparams->odd_even = fh->jpg_settings.odd_even;
1739 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1740 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1741 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1742 memcpy(bparams->APP_data,
1743 fh->jpg_settings.jpg_comp.APP_data,
1744 sizeof(bparams->APP_data));
1745 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1746 memcpy(bparams->COM_data,
1747 fh->jpg_settings.jpg_comp.COM_data,
1748 sizeof(bparams->COM_data));
1749 bparams->jpeg_markers =
1750 fh->jpg_settings.jpg_comp.jpeg_markers;
1752 mutex_unlock(&zr->resource_lock);
1754 bparams->VFIFO_FB = 0;
1759 case BUZIOC_S_PARAMS:
1761 struct zoran_params *bparams = arg;
1764 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1766 settings.decimation = bparams->decimation;
1767 settings.HorDcm = bparams->HorDcm;
1768 settings.VerDcm = bparams->VerDcm;
1769 settings.TmpDcm = bparams->TmpDcm;
1770 settings.field_per_buff = bparams->field_per_buff;
1771 settings.img_x = bparams->img_x;
1772 settings.img_y = bparams->img_y;
1773 settings.img_width = bparams->img_width;
1774 settings.img_height = bparams->img_height;
1775 settings.odd_even = bparams->odd_even;
1777 settings.jpg_comp.quality = bparams->quality;
1778 settings.jpg_comp.APPn = bparams->APPn;
1779 settings.jpg_comp.APP_len = bparams->APP_len;
1780 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1781 sizeof(bparams->APP_data));
1782 settings.jpg_comp.COM_len = bparams->COM_len;
1783 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1784 sizeof(bparams->COM_data));
1785 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1787 mutex_lock(&zr->resource_lock);
1789 if (zr->codec_mode != BUZ_MODE_IDLE) {
1792 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1795 goto sparams_unlock_and_return;
1798 /* Check the params first before overwriting our
1800 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1802 goto sparams_unlock_and_return;
1805 fh->jpg_settings = settings;
1806 sparams_unlock_and_return:
1807 mutex_unlock(&zr->resource_lock);
1812 case BUZIOC_REQBUFS:
1814 struct zoran_requestbuffers *breq = arg;
1819 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1820 ZR_DEVNAME(zr), breq->count, breq->size);
1822 /* Enforce reasonable lower and upper limits */
1823 if (breq->count < 4)
1824 breq->count = 4; /* Could be choosen smaller */
1825 if (breq->count > jpg_nbufs)
1826 breq->count = jpg_nbufs;
1827 breq->size = PAGE_ALIGN(breq->size);
1828 if (breq->size < 8192)
1829 breq->size = 8192; /* Arbitrary */
1830 /* breq->size is limited by 1 page for the stat_com
1831 * tables to a Maximum of 2 MB */
1832 if (breq->size > jpg_bufsize)
1833 breq->size = jpg_bufsize;
1835 mutex_lock(&zr->resource_lock);
1837 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1840 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1843 goto jpgreqbuf_unlock_and_return;
1846 fh->jpg_buffers.num_buffers = breq->count;
1847 fh->jpg_buffers.buffer_size = breq->size;
1849 if (jpg_fbuffer_alloc(file)) {
1851 goto jpgreqbuf_unlock_and_return;
1854 /* The next mmap will map the MJPEG buffers - could
1855 * also be *_PLAY, but it doesn't matter here */
1856 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1857 jpgreqbuf_unlock_and_return:
1858 mutex_unlock(&zr->resource_lock);
1863 case BUZIOC_QBUF_CAPT:
1865 int *frame = arg, res;
1867 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1868 ZR_DEVNAME(zr), *frame);
1870 mutex_lock(&zr->resource_lock);
1871 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1872 mutex_unlock(&zr->resource_lock);
1877 case BUZIOC_QBUF_PLAY:
1879 int *frame = arg, res;
1881 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1882 ZR_DEVNAME(zr), *frame);
1884 mutex_lock(&zr->resource_lock);
1885 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1886 mutex_unlock(&zr->resource_lock);
1893 struct zoran_sync *bsync = arg;
1896 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1898 mutex_lock(&zr->resource_lock);
1899 res = jpg_sync(file, bsync);
1900 mutex_unlock(&zr->resource_lock);
1905 case BUZIOC_G_STATUS:
1907 struct zoran_status *bstat = arg;
1908 int norm, input, status, res = 0;
1910 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1912 if (zr->codec_mode != BUZ_MODE_IDLE) {
1915 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1920 input = zr->card.input[bstat->input].muxsel;
1921 norm = VIDEO_MODE_AUTO;
1923 mutex_lock(&zr->resource_lock);
1925 if (zr->codec_mode != BUZ_MODE_IDLE) {
1928 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1931 goto gstat_unlock_and_return;
1934 decoder_command(zr, DECODER_SET_INPUT, &input);
1935 decoder_command(zr, DECODER_SET_NORM, &norm);
1937 /* sleep 1 second */
1940 /* Get status of video decoder */
1941 decoder_command(zr, DECODER_GET_STATUS, &status);
1943 /* restore previous input and norm */
1944 input = zr->card.input[zr->input].muxsel;
1945 decoder_command(zr, DECODER_SET_INPUT, &input);
1946 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1947 gstat_unlock_and_return:
1948 mutex_unlock(&zr->resource_lock);
1952 (status & DECODER_STATUS_GOOD) ? 1 : 0;
1953 if (status & DECODER_STATUS_NTSC)
1954 bstat->norm = VIDEO_MODE_NTSC;
1955 else if (status & DECODER_STATUS_SECAM)
1956 bstat->norm = VIDEO_MODE_SECAM;
1958 bstat->norm = VIDEO_MODE_PAL;
1961 (status & DECODER_STATUS_COLOR) ? 1 : 0;
1972 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1974 struct zoran_fh *fh = __fh;
1975 struct zoran *zr = fh->zr;
1979 fh->v4l_buffers.num_buffers *
1980 fh->v4l_buffers.buffer_size;
1981 vmbuf->frames = fh->v4l_buffers.num_buffers;
1982 for (i = 0; i < vmbuf->frames; i++) {
1984 i * fh->v4l_buffers.buffer_size;
1987 mutex_lock(&zr->resource_lock);
1989 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1992 "%s: VIDIOCGMBUF - buffers already allocated\n",
1995 goto v4l1reqbuf_unlock_and_return;
1998 if (v4l_fbuffer_alloc(file)) {
2000 goto v4l1reqbuf_unlock_and_return;
2003 /* The next mmap will map the V4L buffers */
2004 fh->map_mode = ZORAN_MAP_MODE_RAW;
2005 v4l1reqbuf_unlock_and_return:
2006 mutex_unlock(&zr->resource_lock);
2012 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
2014 struct zoran_fh *fh = __fh;
2015 struct zoran *zr = fh->zr;
2017 memset(cap, 0, sizeof(*cap));
2018 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2019 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2020 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2021 pci_name(zr->pci_dev));
2023 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2025 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2030 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
2034 for (i = 0; i < NUM_FORMATS; i++) {
2035 if (zoran_formats[i].flags & flag)
2037 if (num == fmt->index)
2040 if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
2043 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2044 fmt->pixelformat = zoran_formats[i].fourcc;
2045 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2046 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2050 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
2051 struct v4l2_fmtdesc *f)
2053 struct zoran_fh *fh = __fh;
2054 struct zoran *zr = fh->zr;
2056 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
2059 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
2060 struct v4l2_fmtdesc *f)
2062 struct zoran_fh *fh = __fh;
2063 struct zoran *zr = fh->zr;
2065 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
2068 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
2069 struct v4l2_fmtdesc *f)
2071 struct zoran_fh *fh = __fh;
2072 struct zoran *zr = fh->zr;
2074 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
2077 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
2078 struct v4l2_format *fmt)
2080 struct zoran_fh *fh = __fh;
2081 struct zoran *zr = fh->zr;
2083 mutex_lock(&zr->resource_lock);
2085 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
2086 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
2087 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
2088 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2089 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
2090 if (fh->jpg_settings.TmpDcm == 1)
2091 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2092 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2094 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2095 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2096 fmt->fmt.pix.bytesperline = 0;
2097 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2099 mutex_unlock(&zr->resource_lock);
2103 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2104 struct v4l2_format *fmt)
2106 struct zoran_fh *fh = __fh;
2107 struct zoran *zr = fh->zr;
2109 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2110 return zoran_g_fmt_vid_out(file, fh, fmt);
2112 mutex_lock(&zr->resource_lock);
2113 fmt->fmt.pix.width = fh->v4l_settings.width;
2114 fmt->fmt.pix.height = fh->v4l_settings.height;
2115 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2116 fh->v4l_settings.height;
2117 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2118 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2119 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2120 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2121 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2123 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2124 mutex_unlock(&zr->resource_lock);
2128 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2129 struct v4l2_format *fmt)
2131 struct zoran_fh *fh = __fh;
2132 struct zoran *zr = fh->zr;
2134 mutex_lock(&zr->resource_lock);
2136 fmt->fmt.win.w.left = fh->overlay_settings.x;
2137 fmt->fmt.win.w.top = fh->overlay_settings.y;
2138 fmt->fmt.win.w.width = fh->overlay_settings.width;
2139 fmt->fmt.win.w.height = fh->overlay_settings.height;
2140 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2141 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2143 fmt->fmt.win.field = V4L2_FIELD_TOP;
2145 mutex_unlock(&zr->resource_lock);
2149 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2150 struct v4l2_format *fmt)
2152 struct zoran_fh *fh = __fh;
2153 struct zoran *zr = fh->zr;
2155 mutex_lock(&zr->resource_lock);
2157 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2158 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2159 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2160 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2161 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2162 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2163 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2164 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2166 mutex_unlock(&zr->resource_lock);
2170 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2171 struct v4l2_format *fmt)
2173 struct zoran_fh *fh = __fh;
2174 struct zoran *zr = fh->zr;
2175 struct zoran_jpg_settings settings;
2178 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2181 fmt->fmt.pix.bytesperline = 0;
2183 mutex_lock(&zr->resource_lock);
2184 settings = fh->jpg_settings;
2186 /* we actually need to set 'real' parameters now */
2187 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2188 settings.TmpDcm = 1;
2190 settings.TmpDcm = 2;
2191 settings.decimation = 0;
2192 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2193 settings.VerDcm = 2;
2195 settings.VerDcm = 1;
2196 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2197 settings.HorDcm = 4;
2198 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2199 settings.HorDcm = 2;
2201 settings.HorDcm = 1;
2202 if (settings.TmpDcm == 1)
2203 settings.field_per_buff = 2;
2205 settings.field_per_buff = 1;
2208 res = zoran_check_jpg_settings(zr, &settings, 1);
2210 goto tryfmt_unlock_and_return;
2212 /* tell the user what we actually did */
2213 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2214 fmt->fmt.pix.height = settings.img_height * 2 /
2215 (settings.TmpDcm * settings.VerDcm);
2216 if (settings.TmpDcm == 1)
2217 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2218 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2220 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2221 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2223 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2224 tryfmt_unlock_and_return:
2225 mutex_unlock(&zr->resource_lock);
2229 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2230 struct v4l2_format *fmt)
2232 struct zoran_fh *fh = __fh;
2233 struct zoran *zr = fh->zr;
2237 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2238 return zoran_try_fmt_vid_out(file, fh, fmt);
2240 mutex_lock(&zr->resource_lock);
2242 for (i = 0; i < NUM_FORMATS; i++)
2243 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2246 if (i == NUM_FORMATS) {
2247 mutex_unlock(&zr->resource_lock);
2251 bpp = (zoran_formats[i].depth + 7) / 8;
2252 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2253 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2254 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2255 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2256 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2257 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2258 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2259 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2260 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2261 mutex_unlock(&zr->resource_lock);
2266 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2267 struct v4l2_format *fmt)
2269 struct zoran_fh *fh = __fh;
2270 struct zoran *zr = fh->zr;
2273 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2274 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2275 fmt->fmt.win.w.width,
2276 fmt->fmt.win.w.height,
2277 fmt->fmt.win.clipcount,
2278 fmt->fmt.win.bitmap);
2279 mutex_lock(&zr->resource_lock);
2280 res = setup_window(file, fmt->fmt.win.w.left,
2282 fmt->fmt.win.w.width,
2283 fmt->fmt.win.w.height,
2284 (struct video_clip __user *)
2286 fmt->fmt.win.clipcount,
2287 fmt->fmt.win.bitmap);
2288 mutex_unlock(&zr->resource_lock);
2292 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2293 struct v4l2_format *fmt)
2295 struct zoran_fh *fh = __fh;
2296 struct zoran *zr = fh->zr;
2297 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2298 struct zoran_jpg_settings settings;
2301 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2302 fmt->fmt.pix.width, fmt->fmt.pix.height,
2303 fmt->fmt.pix.pixelformat,
2304 (char *) &printformat);
2305 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2308 mutex_lock(&zr->resource_lock);
2310 settings = fh->jpg_settings;
2312 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2313 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2316 goto sfmtjpg_unlock_and_return;
2319 /* we actually need to set 'real' parameters now */
2320 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2321 settings.TmpDcm = 1;
2323 settings.TmpDcm = 2;
2324 settings.decimation = 0;
2325 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2326 settings.VerDcm = 2;
2328 settings.VerDcm = 1;
2329 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2330 settings.HorDcm = 4;
2331 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2332 settings.HorDcm = 2;
2334 settings.HorDcm = 1;
2335 if (settings.TmpDcm == 1)
2336 settings.field_per_buff = 2;
2338 settings.field_per_buff = 1;
2340 if (settings.HorDcm > 1) {
2341 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2342 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2345 settings.img_width = BUZ_MAX_WIDTH;
2349 res = zoran_check_jpg_settings(zr, &settings, 0);
2351 goto sfmtjpg_unlock_and_return;
2353 /* it's ok, so set them */
2354 fh->jpg_settings = settings;
2356 /* tell the user what we actually did */
2357 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2358 fmt->fmt.pix.height = settings.img_height * 2 /
2359 (settings.TmpDcm * settings.VerDcm);
2360 if (settings.TmpDcm == 1)
2361 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2362 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2364 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2365 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2366 fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2367 fmt->fmt.pix.bytesperline = 0;
2368 fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2369 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2371 /* we hereby abuse this variable to show that
2372 * we're gonna do mjpeg capture */
2373 fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2374 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2375 sfmtjpg_unlock_and_return:
2376 mutex_unlock(&zr->resource_lock);
2380 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2381 struct v4l2_format *fmt)
2383 struct zoran_fh *fh = __fh;
2384 struct zoran *zr = fh->zr;
2388 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2389 return zoran_s_fmt_vid_out(file, fh, fmt);
2391 for (i = 0; i < NUM_FORMATS; i++)
2392 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2394 if (i == NUM_FORMATS) {
2395 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2396 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2399 mutex_lock(&zr->resource_lock);
2400 if (fh->jpg_buffers.allocated ||
2401 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2402 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2405 goto sfmtv4l_unlock_and_return;
2407 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2408 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2409 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2410 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2412 res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2413 fmt->fmt.pix.height, &zoran_formats[i]);
2415 goto sfmtv4l_unlock_and_return;
2417 /* tell the user the
2418 * results/missing stuff */
2419 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2420 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2421 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2422 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2423 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2425 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2427 fh->map_mode = ZORAN_MAP_MODE_RAW;
2428 sfmtv4l_unlock_and_return:
2429 mutex_unlock(&zr->resource_lock);
2433 static int zoran_g_fbuf(struct file *file, void *__fh,
2434 struct v4l2_framebuffer *fb)
2436 struct zoran_fh *fh = __fh;
2437 struct zoran *zr = fh->zr;
2439 memset(fb, 0, sizeof(*fb));
2440 mutex_lock(&zr->resource_lock);
2441 fb->base = zr->buffer.base;
2442 fb->fmt.width = zr->buffer.width;
2443 fb->fmt.height = zr->buffer.height;
2444 if (zr->overlay_settings.format)
2445 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2446 fb->fmt.bytesperline = zr->buffer.bytesperline;
2447 mutex_unlock(&zr->resource_lock);
2448 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2449 fb->fmt.field = V4L2_FIELD_INTERLACED;
2450 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2451 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2456 static int zoran_s_fbuf(struct file *file, void *__fh,
2457 struct v4l2_framebuffer *fb)
2459 struct zoran_fh *fh = __fh;
2460 struct zoran *zr = fh->zr;
2462 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2464 for (i = 0; i < NUM_FORMATS; i++)
2465 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2467 if (i == NUM_FORMATS) {
2468 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2469 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2470 (char *)&printformat);
2474 mutex_lock(&zr->resource_lock);
2475 res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2476 fb->fmt.width, fb->fmt.height,
2477 fb->fmt.bytesperline);
2478 mutex_unlock(&zr->resource_lock);
2483 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2485 struct zoran_fh *fh = __fh;
2486 struct zoran *zr = fh->zr;
2489 mutex_lock(&zr->resource_lock);
2490 res = setup_overlay(file, on);
2491 mutex_unlock(&zr->resource_lock);
2496 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2498 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2500 struct zoran_fh *fh = __fh;
2501 struct zoran *zr = fh->zr;
2504 if (req->memory != V4L2_MEMORY_MMAP) {
2507 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2508 ZR_DEVNAME(zr), req->memory);
2512 if (req->count == 0)
2513 return zoran_streamoff(file, fh, req->type);
2515 mutex_lock(&zr->resource_lock);
2516 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2519 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2522 goto v4l2reqbuf_unlock_and_return;
2525 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2526 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2528 /* control user input */
2531 if (req->count > v4l_nbufs)
2532 req->count = v4l_nbufs;
2533 fh->v4l_buffers.num_buffers = req->count;
2535 if (v4l_fbuffer_alloc(file)) {
2537 goto v4l2reqbuf_unlock_and_return;
2540 /* The next mmap will map the V4L buffers */
2541 fh->map_mode = ZORAN_MAP_MODE_RAW;
2543 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2544 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2546 /* we need to calculate size ourselves now */
2549 if (req->count > jpg_nbufs)
2550 req->count = jpg_nbufs;
2551 fh->jpg_buffers.num_buffers = req->count;
2552 fh->jpg_buffers.buffer_size =
2553 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2555 if (jpg_fbuffer_alloc(file)) {
2557 goto v4l2reqbuf_unlock_and_return;
2560 /* The next mmap will map the MJPEG buffers */
2561 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2562 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2564 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2569 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2570 ZR_DEVNAME(zr), req->type);
2572 goto v4l2reqbuf_unlock_and_return;
2574 v4l2reqbuf_unlock_and_return:
2575 mutex_unlock(&zr->resource_lock);
2580 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2582 struct zoran_fh *fh = __fh;
2583 struct zoran *zr = fh->zr;
2584 __u32 type = buf->type;
2585 int index = buf->index, res;
2587 memset(buf, 0, sizeof(*buf));
2591 mutex_lock(&zr->resource_lock);
2592 res = zoran_v4l2_buffer_status(file, buf, buf->index);
2593 mutex_unlock(&zr->resource_lock);
2598 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2600 struct zoran_fh *fh = __fh;
2601 struct zoran *zr = fh->zr;
2602 int res = 0, codec_mode, buf_type;
2604 mutex_lock(&zr->resource_lock);
2606 switch (fh->map_mode) {
2607 case ZORAN_MAP_MODE_RAW:
2608 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2610 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2611 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2613 goto qbuf_unlock_and_return;
2616 res = zoran_v4l_queue_frame(file, buf->index);
2618 goto qbuf_unlock_and_return;
2619 if (!zr->v4l_memgrab_active &&
2620 fh->v4l_buffers.active == ZORAN_LOCKED)
2621 zr36057_set_memgrab(zr, 1);
2624 case ZORAN_MAP_MODE_JPG_REC:
2625 case ZORAN_MAP_MODE_JPG_PLAY:
2626 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2627 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2628 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2630 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2631 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2634 if (buf->type != buf_type) {
2636 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2637 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2639 goto qbuf_unlock_and_return;
2642 res = zoran_jpg_queue_frame(file, buf->index,
2645 goto qbuf_unlock_and_return;
2646 if (zr->codec_mode == BUZ_MODE_IDLE &&
2647 fh->jpg_buffers.active == ZORAN_LOCKED) {
2648 zr36057_enable_jpg(zr, codec_mode);
2654 "%s: VIDIOC_QBUF - unsupported type %d\n",
2655 ZR_DEVNAME(zr), buf->type);
2659 qbuf_unlock_and_return:
2660 mutex_unlock(&zr->resource_lock);
2665 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2667 struct zoran_fh *fh = __fh;
2668 struct zoran *zr = fh->zr;
2669 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2671 mutex_lock(&zr->resource_lock);
2673 switch (fh->map_mode) {
2674 case ZORAN_MAP_MODE_RAW:
2675 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2677 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2678 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2680 goto dqbuf_unlock_and_return;
2683 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2684 if (file->f_flags & O_NONBLOCK &&
2685 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2687 goto dqbuf_unlock_and_return;
2689 res = v4l_sync(file, num);
2691 goto dqbuf_unlock_and_return;
2692 zr->v4l_sync_tail++;
2693 res = zoran_v4l2_buffer_status(file, buf, num);
2696 case ZORAN_MAP_MODE_JPG_REC:
2697 case ZORAN_MAP_MODE_JPG_PLAY:
2699 struct zoran_sync bs;
2701 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2702 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2704 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2706 if (buf->type != buf_type) {
2708 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2709 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2711 goto dqbuf_unlock_and_return;
2714 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2716 if (file->f_flags & O_NONBLOCK &&
2717 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2719 goto dqbuf_unlock_and_return;
2721 res = jpg_sync(file, &bs);
2723 goto dqbuf_unlock_and_return;
2724 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2730 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2731 ZR_DEVNAME(zr), buf->type);
2735 dqbuf_unlock_and_return:
2736 mutex_unlock(&zr->resource_lock);
2741 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2743 struct zoran_fh *fh = __fh;
2744 struct zoran *zr = fh->zr;
2747 mutex_lock(&zr->resource_lock);
2749 switch (fh->map_mode) {
2750 case ZORAN_MAP_MODE_RAW: /* raw capture */
2751 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2752 fh->v4l_buffers.active != ZORAN_ACTIVE) {
2754 goto strmon_unlock_and_return;
2757 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2758 zr->v4l_settings = fh->v4l_settings;
2760 zr->v4l_sync_tail = zr->v4l_pend_tail;
2761 if (!zr->v4l_memgrab_active &&
2762 zr->v4l_pend_head != zr->v4l_pend_tail) {
2763 zr36057_set_memgrab(zr, 1);
2767 case ZORAN_MAP_MODE_JPG_REC:
2768 case ZORAN_MAP_MODE_JPG_PLAY:
2769 /* what is the codec mode right now? */
2770 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2771 fh->jpg_buffers.active != ZORAN_ACTIVE) {
2773 goto strmon_unlock_and_return;
2776 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2778 if (zr->jpg_que_head != zr->jpg_que_tail) {
2779 /* Start the jpeg codec when the first frame is queued */
2787 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2788 ZR_DEVNAME(zr), fh->map_mode);
2792 strmon_unlock_and_return:
2793 mutex_unlock(&zr->resource_lock);
2798 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2800 struct zoran_fh *fh = __fh;
2801 struct zoran *zr = fh->zr;
2804 mutex_lock(&zr->resource_lock);
2806 switch (fh->map_mode) {
2807 case ZORAN_MAP_MODE_RAW: /* raw capture */
2808 if (fh->v4l_buffers.active == ZORAN_FREE &&
2809 zr->v4l_buffers.active != ZORAN_FREE) {
2810 res = -EPERM; /* stay off other's settings! */
2811 goto strmoff_unlock_and_return;
2813 if (zr->v4l_buffers.active == ZORAN_FREE)
2814 goto strmoff_unlock_and_return;
2816 /* unload capture */
2817 if (zr->v4l_memgrab_active) {
2818 unsigned long flags;
2820 spin_lock_irqsave(&zr->spinlock, flags);
2821 zr36057_set_memgrab(zr, 0);
2822 spin_unlock_irqrestore(&zr->spinlock, flags);
2825 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2826 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2827 fh->v4l_buffers = zr->v4l_buffers;
2829 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2831 zr->v4l_grab_seq = 0;
2832 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2833 zr->v4l_sync_tail = 0;
2837 case ZORAN_MAP_MODE_JPG_REC:
2838 case ZORAN_MAP_MODE_JPG_PLAY:
2839 if (fh->jpg_buffers.active == ZORAN_FREE &&
2840 zr->jpg_buffers.active != ZORAN_FREE) {
2841 res = -EPERM; /* stay off other's settings! */
2842 goto strmoff_unlock_and_return;
2844 if (zr->jpg_buffers.active == ZORAN_FREE)
2845 goto strmoff_unlock_and_return;
2847 res = jpg_qbuf(file, -1,
2848 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2849 BUZ_MODE_MOTION_COMPRESS :
2850 BUZ_MODE_MOTION_DECOMPRESS);
2852 goto strmoff_unlock_and_return;
2856 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2857 ZR_DEVNAME(zr), fh->map_mode);
2861 strmoff_unlock_and_return:
2862 mutex_unlock(&zr->resource_lock);
2867 static int zoran_queryctrl(struct file *file, void *__fh,
2868 struct v4l2_queryctrl *ctrl)
2870 /* we only support hue/saturation/contrast/brightness */
2871 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2872 ctrl->id > V4L2_CID_HUE)
2876 memset(ctrl, 0, sizeof(*ctrl));
2881 case V4L2_CID_BRIGHTNESS:
2882 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
2884 case V4L2_CID_CONTRAST:
2885 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
2887 case V4L2_CID_SATURATION:
2888 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
2891 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
2896 ctrl->maximum = 65535;
2898 ctrl->default_value = 32768;
2899 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
2900 ctrl->flags = V4L2_CTRL_FLAG_SLIDER;
2905 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2907 struct zoran_fh *fh = __fh;
2908 struct zoran *zr = fh->zr;
2910 /* we only support hue/saturation/contrast/brightness */
2911 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2912 ctrl->id > V4L2_CID_HUE)
2915 mutex_lock(&zr->resource_lock);
2917 case V4L2_CID_BRIGHTNESS:
2918 ctrl->value = zr->brightness;
2920 case V4L2_CID_CONTRAST:
2921 ctrl->value = zr->contrast;
2923 case V4L2_CID_SATURATION:
2924 ctrl->value = zr->saturation;
2927 ctrl->value = zr->hue;
2930 mutex_unlock(&zr->resource_lock);
2935 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2937 struct zoran_fh *fh = __fh;
2938 struct zoran *zr = fh->zr;
2939 struct video_picture pict;
2941 /* we only support hue/saturation/contrast/brightness */
2942 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2943 ctrl->id > V4L2_CID_HUE)
2946 if (ctrl->value < 0 || ctrl->value > 65535) {
2948 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
2949 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
2953 mutex_lock(&zr->resource_lock);
2955 case V4L2_CID_BRIGHTNESS:
2956 zr->brightness = ctrl->value;
2958 case V4L2_CID_CONTRAST:
2959 zr->contrast = ctrl->value;
2961 case V4L2_CID_SATURATION:
2962 zr->saturation = ctrl->value;
2965 zr->hue = ctrl->value;
2968 pict.brightness = zr->brightness;
2969 pict.contrast = zr->contrast;
2970 pict.colour = zr->saturation;
2973 decoder_command(zr, DECODER_SET_PICTURE, &pict);
2975 mutex_unlock(&zr->resource_lock);
2980 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2982 struct zoran_fh *fh = __fh;
2983 struct zoran *zr = fh->zr;
2986 mutex_lock(&zr->resource_lock);
2988 mutex_unlock(&zr->resource_lock);
2991 case VIDEO_MODE_PAL:
2992 *std = V4L2_STD_PAL;
2994 case VIDEO_MODE_NTSC:
2995 *std = V4L2_STD_NTSC;
2997 case VIDEO_MODE_SECAM:
2998 *std = V4L2_STD_SECAM;
3005 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
3007 struct zoran_fh *fh = __fh;
3008 struct zoran *zr = fh->zr;
3009 int norm = -1, res = 0;
3011 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3012 norm = VIDEO_MODE_PAL;
3013 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3014 norm = VIDEO_MODE_NTSC;
3015 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3016 norm = VIDEO_MODE_SECAM;
3017 else if (*std == V4L2_STD_ALL)
3018 norm = VIDEO_MODE_AUTO;
3021 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3022 ZR_DEVNAME(zr), (unsigned long long)*std);
3026 mutex_lock(&zr->resource_lock);
3027 res = zoran_set_norm(zr, norm);
3029 goto sstd_unlock_and_return;
3031 res = wait_grab_pending(zr);
3032 sstd_unlock_and_return:
3033 mutex_unlock(&zr->resource_lock);
3037 static int zoran_enum_input(struct file *file, void *__fh,
3038 struct v4l2_input *inp)
3040 struct zoran_fh *fh = __fh;
3041 struct zoran *zr = fh->zr;
3044 if (inp->index < 0 || inp->index >= zr->card.inputs)
3047 int id = inp->index;
3048 memset(inp, 0, sizeof(*inp));
3052 strncpy(inp->name, zr->card.input[inp->index].name,
3053 sizeof(inp->name) - 1);
3054 inp->type = V4L2_INPUT_TYPE_CAMERA;
3055 inp->std = V4L2_STD_ALL;
3057 /* Get status of video decoder */
3058 mutex_lock(&zr->resource_lock);
3059 decoder_command(zr, DECODER_GET_STATUS, &status);
3060 mutex_unlock(&zr->resource_lock);
3062 if (!(status & DECODER_STATUS_GOOD)) {
3063 inp->status |= V4L2_IN_ST_NO_POWER;
3064 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3066 if (!(status & DECODER_STATUS_COLOR))
3067 inp->status |= V4L2_IN_ST_NO_COLOR;
3072 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
3074 struct zoran_fh *fh = __fh;
3075 struct zoran *zr = fh->zr;
3077 mutex_lock(&zr->resource_lock);
3079 mutex_unlock(&zr->resource_lock);
3084 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
3086 struct zoran_fh *fh = __fh;
3087 struct zoran *zr = fh->zr;
3090 mutex_lock(&zr->resource_lock);
3091 res = zoran_set_input(zr, input);
3093 goto sinput_unlock_and_return;
3095 /* Make sure the changes come into effect */
3096 res = wait_grab_pending(zr);
3097 sinput_unlock_and_return:
3098 mutex_unlock(&zr->resource_lock);
3102 static int zoran_enum_output(struct file *file, void *__fh,
3103 struct v4l2_output *outp)
3105 if (outp->index != 0)
3108 memset(outp, 0, sizeof(*outp));
3110 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3111 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3116 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
3123 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
3131 /* cropping (sub-frame capture) */
3132 static int zoran_cropcap(struct file *file, void *__fh,
3133 struct v4l2_cropcap *cropcap)
3135 struct zoran_fh *fh = __fh;
3136 struct zoran *zr = fh->zr;
3137 int type = cropcap->type, res = 0;
3139 memset(cropcap, 0, sizeof(*cropcap));
3140 cropcap->type = type;
3142 mutex_lock(&zr->resource_lock);
3144 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3145 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3146 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3148 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3151 goto cropcap_unlock_and_return;
3154 cropcap->bounds.top = cropcap->bounds.left = 0;
3155 cropcap->bounds.width = BUZ_MAX_WIDTH;
3156 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3157 cropcap->defrect.top = cropcap->defrect.left = 0;
3158 cropcap->defrect.width = BUZ_MIN_WIDTH;
3159 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3160 cropcap_unlock_and_return:
3161 mutex_unlock(&zr->resource_lock);
3165 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3167 struct zoran_fh *fh = __fh;
3168 struct zoran *zr = fh->zr;
3169 int type = crop->type, res = 0;
3171 memset(crop, 0, sizeof(*crop));
3174 mutex_lock(&zr->resource_lock);
3176 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3177 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3178 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3181 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3184 goto gcrop_unlock_and_return;
3187 crop->c.top = fh->jpg_settings.img_y;
3188 crop->c.left = fh->jpg_settings.img_x;
3189 crop->c.width = fh->jpg_settings.img_width;
3190 crop->c.height = fh->jpg_settings.img_height;
3192 gcrop_unlock_and_return:
3193 mutex_unlock(&zr->resource_lock);
3198 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3200 struct zoran_fh *fh = __fh;
3201 struct zoran *zr = fh->zr;
3203 struct zoran_jpg_settings settings;
3205 settings = fh->jpg_settings;
3207 mutex_lock(&zr->resource_lock);
3209 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3211 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3214 goto scrop_unlock_and_return;
3217 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3218 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3219 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3221 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3224 goto scrop_unlock_and_return;
3227 /* move into a form that we understand */
3228 settings.img_x = crop->c.left;
3229 settings.img_y = crop->c.top;
3230 settings.img_width = crop->c.width;
3231 settings.img_height = crop->c.height;
3233 /* check validity */
3234 res = zoran_check_jpg_settings(zr, &settings, 0);
3236 goto scrop_unlock_and_return;
3239 fh->jpg_settings = settings;
3241 scrop_unlock_and_return:
3242 mutex_unlock(&zr->resource_lock);
3246 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3247 struct v4l2_jpegcompression *params)
3249 struct zoran_fh *fh = __fh;
3250 struct zoran *zr = fh->zr;
3251 memset(params, 0, sizeof(*params));
3253 mutex_lock(&zr->resource_lock);
3255 params->quality = fh->jpg_settings.jpg_comp.quality;
3256 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3257 memcpy(params->APP_data,
3258 fh->jpg_settings.jpg_comp.APP_data,
3259 fh->jpg_settings.jpg_comp.APP_len);
3260 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3261 memcpy(params->COM_data,
3262 fh->jpg_settings.jpg_comp.COM_data,
3263 fh->jpg_settings.jpg_comp.COM_len);
3264 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3265 params->jpeg_markers =
3266 fh->jpg_settings.jpg_comp.jpeg_markers;
3268 mutex_unlock(&zr->resource_lock);
3273 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3274 struct v4l2_jpegcompression *params)
3276 struct zoran_fh *fh = __fh;
3277 struct zoran *zr = fh->zr;
3279 struct zoran_jpg_settings settings;
3281 settings = fh->jpg_settings;
3283 settings.jpg_comp = *params;
3285 mutex_lock(&zr->resource_lock);
3287 if (fh->v4l_buffers.active != ZORAN_FREE ||
3288 fh->jpg_buffers.active != ZORAN_FREE) {
3289 dprintk(1, KERN_WARNING
3290 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3293 goto sjpegc_unlock_and_return;
3296 res = zoran_check_jpg_settings(zr, &settings, 0);
3298 goto sjpegc_unlock_and_return;
3299 if (!fh->jpg_buffers.allocated)
3300 fh->jpg_buffers.buffer_size =
3301 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3302 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3303 sjpegc_unlock_and_return:
3304 mutex_unlock(&zr->resource_lock);
3310 zoran_poll (struct file *file,
3313 struct zoran_fh *fh = file->private_data;
3314 struct zoran *zr = fh->zr;
3316 unsigned long flags;
3318 /* we should check whether buffers are ready to be synced on
3319 * (w/o waits - O_NONBLOCK) here
3320 * if ready for read (sync), return POLLIN|POLLRDNORM,
3321 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3322 * if error, return POLLERR,
3323 * if no buffers queued or so, return POLLNVAL
3326 mutex_lock(&zr->resource_lock);
3328 switch (fh->map_mode) {
3329 case ZORAN_MAP_MODE_RAW:
3330 poll_wait(file, &zr->v4l_capq, wait);
3331 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3333 spin_lock_irqsave(&zr->spinlock, flags);
3336 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3337 ZR_DEVNAME(zr), __func__,
3338 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3339 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3340 zr->v4l_pend_tail, zr->v4l_pend_head);
3341 /* Process is the one capturing? */
3342 if (fh->v4l_buffers.active != ZORAN_FREE &&
3343 /* Buffer ready to DQBUF? */
3344 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3345 res = POLLIN | POLLRDNORM;
3346 spin_unlock_irqrestore(&zr->spinlock, flags);
3350 case ZORAN_MAP_MODE_JPG_REC:
3351 case ZORAN_MAP_MODE_JPG_PLAY:
3352 poll_wait(file, &zr->jpg_capq, wait);
3353 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3355 spin_lock_irqsave(&zr->spinlock, flags);
3358 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3359 ZR_DEVNAME(zr), __func__,
3360 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3361 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3362 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3363 if (fh->jpg_buffers.active != ZORAN_FREE &&
3364 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3365 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3366 res = POLLIN | POLLRDNORM;
3368 res = POLLOUT | POLLWRNORM;
3370 spin_unlock_irqrestore(&zr->spinlock, flags);
3377 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3378 ZR_DEVNAME(zr), fh->map_mode);
3382 mutex_unlock(&zr->resource_lock);
3389 * This maps the buffers to user space.
3391 * Depending on the state of fh->map_mode
3392 * the V4L or the MJPEG buffers are mapped
3393 * per buffer or all together
3395 * Note that we need to connect to some
3396 * unmap signal event to unmap the de-allocate
3397 * the buffer accordingly (zoran_vm_close())
3401 zoran_vm_open (struct vm_area_struct *vma)
3403 struct zoran_mapping *map = vma->vm_private_data;
3409 zoran_vm_close (struct vm_area_struct *vma)
3411 struct zoran_mapping *map = vma->vm_private_data;
3412 struct file *file = map->file;
3413 struct zoran_fh *fh = file->private_data;
3414 struct zoran *zr = fh->zr;
3418 if (map->count == 0) {
3419 switch (fh->map_mode) {
3420 case ZORAN_MAP_MODE_JPG_REC:
3421 case ZORAN_MAP_MODE_JPG_PLAY:
3423 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3426 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3427 if (fh->jpg_buffers.buffer[i].map == map) {
3428 fh->jpg_buffers.buffer[i].map =
3434 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3435 if (fh->jpg_buffers.buffer[i].map)
3437 if (i == fh->jpg_buffers.num_buffers) {
3438 mutex_lock(&zr->resource_lock);
3440 if (fh->jpg_buffers.active != ZORAN_FREE) {
3441 jpg_qbuf(file, -1, zr->codec_mode);
3442 zr->jpg_buffers.allocated = 0;
3443 zr->jpg_buffers.active =
3444 fh->jpg_buffers.active =
3447 jpg_fbuffer_free(file);
3448 mutex_unlock(&zr->resource_lock);
3453 case ZORAN_MAP_MODE_RAW:
3455 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3458 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3459 if (fh->v4l_buffers.buffer[i].map == map) {
3461 fh->v4l_buffers.buffer[i].map =
3467 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3468 if (fh->v4l_buffers.buffer[i].map)
3470 if (i == fh->v4l_buffers.num_buffers) {
3471 mutex_lock(&zr->resource_lock);
3473 if (fh->v4l_buffers.active != ZORAN_FREE) {
3474 unsigned long flags;
3476 spin_lock_irqsave(&zr->spinlock, flags);
3477 zr36057_set_memgrab(zr, 0);
3478 zr->v4l_buffers.allocated = 0;
3479 zr->v4l_buffers.active =
3480 fh->v4l_buffers.active =
3482 spin_unlock_irqrestore(&zr->spinlock, flags);
3484 v4l_fbuffer_free(file);
3485 mutex_unlock(&zr->resource_lock);
3492 "%s: munmap() - internal error - unknown map mode %d\n",
3493 ZR_DEVNAME(zr), fh->map_mode);
3500 static struct vm_operations_struct zoran_vm_ops = {
3501 .open = zoran_vm_open,
3502 .close = zoran_vm_close,
3506 zoran_mmap (struct file *file,
3507 struct vm_area_struct *vma)
3509 struct zoran_fh *fh = file->private_data;
3510 struct zoran *zr = fh->zr;
3511 unsigned long size = (vma->vm_end - vma->vm_start);
3512 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3514 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3516 struct zoran_mapping *map;
3520 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3522 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3523 vma->vm_start, vma->vm_end, size);
3525 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3526 !(vma->vm_flags & VM_WRITE)) {
3529 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3534 switch (fh->map_mode) {
3536 case ZORAN_MAP_MODE_JPG_REC:
3537 case ZORAN_MAP_MODE_JPG_PLAY:
3540 mutex_lock(&zr->resource_lock);
3542 /* Map the MJPEG buffers */
3543 if (!fh->jpg_buffers.allocated) {
3546 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3549 goto jpg_mmap_unlock_and_return;
3552 first = offset / fh->jpg_buffers.buffer_size;
3553 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3554 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3555 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3556 last < 0 || first >= fh->jpg_buffers.num_buffers ||
3557 last >= fh->jpg_buffers.num_buffers) {
3560 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3561 ZR_DEVNAME(zr), offset, size,
3562 fh->jpg_buffers.buffer_size,
3563 fh->jpg_buffers.num_buffers);
3565 goto jpg_mmap_unlock_and_return;
3567 for (i = first; i <= last; i++) {
3568 if (fh->jpg_buffers.buffer[i].map) {
3571 "%s: mmap(MJPEG) - buffer %d already mapped\n",
3574 goto jpg_mmap_unlock_and_return;
3578 /* map these buffers (v4l_buffers[i]) */
3579 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3582 goto jpg_mmap_unlock_and_return;
3587 vma->vm_ops = &zoran_vm_ops;
3588 vma->vm_flags |= VM_DONTEXPAND;
3589 vma->vm_private_data = map;
3591 for (i = first; i <= last; i++) {
3593 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3596 (le32_to_cpu(fh->jpg_buffers.buffer[i].
3597 frag_tab[2 * j + 1]) & ~1) << 1;
3602 le32_to_cpu(fh->jpg_buffers.
3603 buffer[i].frag_tab[2 * j]);
3604 /* should just be pos on i386 */
3605 page = virt_to_phys(bus_to_virt(pos))
3607 if (remap_pfn_range(vma, start, page,
3608 todo, PAGE_SHARED)) {
3611 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3614 goto jpg_mmap_unlock_and_return;
3620 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3621 frag_tab[2 * j + 1]) & 1)
3622 break; /* was last fragment */
3624 fh->jpg_buffers.buffer[i].map = map;
3629 jpg_mmap_unlock_and_return:
3630 mutex_unlock(&zr->resource_lock);
3634 case ZORAN_MAP_MODE_RAW:
3636 mutex_lock(&zr->resource_lock);
3638 /* Map the V4L buffers */
3639 if (!fh->v4l_buffers.allocated) {
3642 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3645 goto v4l_mmap_unlock_and_return;
3648 first = offset / fh->v4l_buffers.buffer_size;
3649 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3650 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3651 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3652 last < 0 || first >= fh->v4l_buffers.num_buffers ||
3653 last >= fh->v4l_buffers.buffer_size) {
3656 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3657 ZR_DEVNAME(zr), offset, size,
3658 fh->v4l_buffers.buffer_size,
3659 fh->v4l_buffers.num_buffers);
3661 goto v4l_mmap_unlock_and_return;
3663 for (i = first; i <= last; i++) {
3664 if (fh->v4l_buffers.buffer[i].map) {
3667 "%s: mmap(V4L) - buffer %d already mapped\n",
3670 goto v4l_mmap_unlock_and_return;
3674 /* map these buffers (v4l_buffers[i]) */
3675 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3678 goto v4l_mmap_unlock_and_return;
3683 vma->vm_ops = &zoran_vm_ops;
3684 vma->vm_flags |= VM_DONTEXPAND;
3685 vma->vm_private_data = map;
3687 for (i = first; i <= last; i++) {
3689 if (todo > fh->v4l_buffers.buffer_size)
3690 todo = fh->v4l_buffers.buffer_size;
3691 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3692 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3693 todo, PAGE_SHARED)) {
3696 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3699 goto v4l_mmap_unlock_and_return;
3703 fh->v4l_buffers.buffer[i].map = map;
3707 v4l_mmap_unlock_and_return:
3708 mutex_unlock(&zr->resource_lock);
3715 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3716 ZR_DEVNAME(zr), fh->map_mode);
3723 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3724 .vidioc_querycap = zoran_querycap,
3725 .vidioc_cropcap = zoran_cropcap,
3726 .vidioc_s_crop = zoran_s_crop,
3727 .vidioc_g_crop = zoran_g_crop,
3728 .vidioc_enum_input = zoran_enum_input,
3729 .vidioc_g_input = zoran_g_input,
3730 .vidioc_s_input = zoran_s_input,
3731 .vidioc_enum_output = zoran_enum_output,
3732 .vidioc_g_output = zoran_g_output,
3733 .vidioc_s_output = zoran_s_output,
3734 .vidioc_g_fbuf = zoran_g_fbuf,
3735 .vidioc_s_fbuf = zoran_s_fbuf,
3736 .vidioc_g_std = zoran_g_std,
3737 .vidioc_s_std = zoran_s_std,
3738 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3739 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3740 .vidioc_overlay = zoran_overlay,
3741 .vidioc_reqbufs = zoran_reqbufs,
3742 .vidioc_querybuf = zoran_querybuf,
3743 .vidioc_qbuf = zoran_qbuf,
3744 .vidioc_dqbuf = zoran_dqbuf,
3745 .vidioc_streamon = zoran_streamon,
3746 .vidioc_streamoff = zoran_streamoff,
3747 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3748 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3749 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3750 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3751 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3752 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3753 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3754 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3755 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3756 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3757 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3758 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3759 .vidioc_queryctrl = zoran_queryctrl,
3760 .vidioc_s_ctrl = zoran_s_ctrl,
3761 .vidioc_g_ctrl = zoran_g_ctrl,
3762 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3763 .vidioc_default = zoran_default,
3764 .vidiocgmbuf = zoran_vidiocgmbuf,
3768 static const struct v4l2_file_operations zoran_fops = {
3769 .owner = THIS_MODULE,
3771 .release = zoran_close,
3772 .ioctl = video_ioctl2,
3774 .write = zoran_write,
3779 struct video_device zoran_template __devinitdata = {
3781 .fops = &zoran_fops,
3782 .ioctl_ops = &zoran_ioctl_ops,
3783 .release = &zoran_vdev_release,
3784 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,