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_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2498 struct zoran_fh *fh = __fh;
2499 struct zoran *zr = fh->zr;
2502 if (req->memory != V4L2_MEMORY_MMAP) {
2505 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2506 ZR_DEVNAME(zr), req->memory);
2510 mutex_lock(&zr->resource_lock);
2512 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2515 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2518 goto v4l2reqbuf_unlock_and_return;
2521 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2522 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2524 /* control user input */
2527 if (req->count > v4l_nbufs)
2528 req->count = v4l_nbufs;
2529 fh->v4l_buffers.num_buffers = req->count;
2531 if (v4l_fbuffer_alloc(file)) {
2533 goto v4l2reqbuf_unlock_and_return;
2536 /* The next mmap will map the V4L buffers */
2537 fh->map_mode = ZORAN_MAP_MODE_RAW;
2539 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2540 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2542 /* we need to calculate size ourselves now */
2545 if (req->count > jpg_nbufs)
2546 req->count = jpg_nbufs;
2547 fh->jpg_buffers.num_buffers = req->count;
2548 fh->jpg_buffers.buffer_size =
2549 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2551 if (jpg_fbuffer_alloc(file)) {
2553 goto v4l2reqbuf_unlock_and_return;
2556 /* The next mmap will map the MJPEG buffers */
2557 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2558 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2560 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2565 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2566 ZR_DEVNAME(zr), req->type);
2568 goto v4l2reqbuf_unlock_and_return;
2570 v4l2reqbuf_unlock_and_return:
2571 mutex_unlock(&zr->resource_lock);
2576 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2578 struct zoran_fh *fh = __fh;
2579 struct zoran *zr = fh->zr;
2580 __u32 type = buf->type;
2581 int index = buf->index, res;
2583 memset(buf, 0, sizeof(*buf));
2587 mutex_lock(&zr->resource_lock);
2588 res = zoran_v4l2_buffer_status(file, buf, buf->index);
2589 mutex_unlock(&zr->resource_lock);
2594 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2596 struct zoran_fh *fh = __fh;
2597 struct zoran *zr = fh->zr;
2598 int res = 0, codec_mode, buf_type;
2600 mutex_lock(&zr->resource_lock);
2602 switch (fh->map_mode) {
2603 case ZORAN_MAP_MODE_RAW:
2604 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2606 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2607 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2609 goto qbuf_unlock_and_return;
2612 res = zoran_v4l_queue_frame(file, buf->index);
2614 goto qbuf_unlock_and_return;
2615 if (!zr->v4l_memgrab_active &&
2616 fh->v4l_buffers.active == ZORAN_LOCKED)
2617 zr36057_set_memgrab(zr, 1);
2620 case ZORAN_MAP_MODE_JPG_REC:
2621 case ZORAN_MAP_MODE_JPG_PLAY:
2622 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2623 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2624 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2626 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2627 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2630 if (buf->type != buf_type) {
2632 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2633 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2635 goto qbuf_unlock_and_return;
2638 res = zoran_jpg_queue_frame(file, buf->index,
2641 goto qbuf_unlock_and_return;
2642 if (zr->codec_mode == BUZ_MODE_IDLE &&
2643 fh->jpg_buffers.active == ZORAN_LOCKED) {
2644 zr36057_enable_jpg(zr, codec_mode);
2650 "%s: VIDIOC_QBUF - unsupported type %d\n",
2651 ZR_DEVNAME(zr), buf->type);
2655 qbuf_unlock_and_return:
2656 mutex_unlock(&zr->resource_lock);
2661 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2663 struct zoran_fh *fh = __fh;
2664 struct zoran *zr = fh->zr;
2665 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2667 mutex_lock(&zr->resource_lock);
2669 switch (fh->map_mode) {
2670 case ZORAN_MAP_MODE_RAW:
2671 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2673 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2674 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2676 goto dqbuf_unlock_and_return;
2679 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2680 if (file->f_flags & O_NONBLOCK &&
2681 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2683 goto dqbuf_unlock_and_return;
2685 res = v4l_sync(file, num);
2687 goto dqbuf_unlock_and_return;
2688 zr->v4l_sync_tail++;
2689 res = zoran_v4l2_buffer_status(file, buf, num);
2692 case ZORAN_MAP_MODE_JPG_REC:
2693 case ZORAN_MAP_MODE_JPG_PLAY:
2695 struct zoran_sync bs;
2697 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2698 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2700 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2702 if (buf->type != buf_type) {
2704 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2705 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2707 goto dqbuf_unlock_and_return;
2710 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2712 if (file->f_flags & O_NONBLOCK &&
2713 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2715 goto dqbuf_unlock_and_return;
2717 res = jpg_sync(file, &bs);
2719 goto dqbuf_unlock_and_return;
2720 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2726 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2727 ZR_DEVNAME(zr), buf->type);
2731 dqbuf_unlock_and_return:
2732 mutex_unlock(&zr->resource_lock);
2737 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2739 struct zoran_fh *fh = __fh;
2740 struct zoran *zr = fh->zr;
2743 mutex_lock(&zr->resource_lock);
2745 switch (fh->map_mode) {
2746 case ZORAN_MAP_MODE_RAW: /* raw capture */
2747 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2748 fh->v4l_buffers.active != ZORAN_ACTIVE) {
2750 goto strmon_unlock_and_return;
2753 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2754 zr->v4l_settings = fh->v4l_settings;
2756 zr->v4l_sync_tail = zr->v4l_pend_tail;
2757 if (!zr->v4l_memgrab_active &&
2758 zr->v4l_pend_head != zr->v4l_pend_tail) {
2759 zr36057_set_memgrab(zr, 1);
2763 case ZORAN_MAP_MODE_JPG_REC:
2764 case ZORAN_MAP_MODE_JPG_PLAY:
2765 /* what is the codec mode right now? */
2766 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2767 fh->jpg_buffers.active != ZORAN_ACTIVE) {
2769 goto strmon_unlock_and_return;
2772 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2774 if (zr->jpg_que_head != zr->jpg_que_tail) {
2775 /* Start the jpeg codec when the first frame is queued */
2783 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2784 ZR_DEVNAME(zr), fh->map_mode);
2788 strmon_unlock_and_return:
2789 mutex_unlock(&zr->resource_lock);
2794 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2796 struct zoran_fh *fh = __fh;
2797 struct zoran *zr = fh->zr;
2800 mutex_lock(&zr->resource_lock);
2802 switch (fh->map_mode) {
2803 case ZORAN_MAP_MODE_RAW: /* raw capture */
2804 if (fh->v4l_buffers.active == ZORAN_FREE &&
2805 zr->v4l_buffers.active != ZORAN_FREE) {
2806 res = -EPERM; /* stay off other's settings! */
2807 goto strmoff_unlock_and_return;
2809 if (zr->v4l_buffers.active == ZORAN_FREE)
2810 goto strmoff_unlock_and_return;
2812 /* unload capture */
2813 if (zr->v4l_memgrab_active) {
2814 unsigned long flags;
2816 spin_lock_irqsave(&zr->spinlock, flags);
2817 zr36057_set_memgrab(zr, 0);
2818 spin_unlock_irqrestore(&zr->spinlock, flags);
2821 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2822 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2823 fh->v4l_buffers = zr->v4l_buffers;
2825 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2827 zr->v4l_grab_seq = 0;
2828 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2829 zr->v4l_sync_tail = 0;
2833 case ZORAN_MAP_MODE_JPG_REC:
2834 case ZORAN_MAP_MODE_JPG_PLAY:
2835 if (fh->jpg_buffers.active == ZORAN_FREE &&
2836 zr->jpg_buffers.active != ZORAN_FREE) {
2837 res = -EPERM; /* stay off other's settings! */
2838 goto strmoff_unlock_and_return;
2840 if (zr->jpg_buffers.active == ZORAN_FREE)
2841 goto strmoff_unlock_and_return;
2843 res = jpg_qbuf(file, -1,
2844 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2845 BUZ_MODE_MOTION_COMPRESS :
2846 BUZ_MODE_MOTION_DECOMPRESS);
2848 goto strmoff_unlock_and_return;
2852 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2853 ZR_DEVNAME(zr), fh->map_mode);
2857 strmoff_unlock_and_return:
2858 mutex_unlock(&zr->resource_lock);
2863 static int zoran_queryctrl(struct file *file, void *__fh,
2864 struct v4l2_queryctrl *ctrl)
2866 /* we only support hue/saturation/contrast/brightness */
2867 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2868 ctrl->id > V4L2_CID_HUE)
2872 memset(ctrl, 0, sizeof(*ctrl));
2877 case V4L2_CID_BRIGHTNESS:
2878 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
2880 case V4L2_CID_CONTRAST:
2881 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
2883 case V4L2_CID_SATURATION:
2884 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
2887 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
2892 ctrl->maximum = 65535;
2894 ctrl->default_value = 32768;
2895 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
2896 ctrl->flags = V4L2_CTRL_FLAG_SLIDER;
2901 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2903 struct zoran_fh *fh = __fh;
2904 struct zoran *zr = fh->zr;
2906 /* we only support hue/saturation/contrast/brightness */
2907 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2908 ctrl->id > V4L2_CID_HUE)
2911 mutex_lock(&zr->resource_lock);
2913 case V4L2_CID_BRIGHTNESS:
2914 ctrl->value = zr->brightness;
2916 case V4L2_CID_CONTRAST:
2917 ctrl->value = zr->contrast;
2919 case V4L2_CID_SATURATION:
2920 ctrl->value = zr->saturation;
2923 ctrl->value = zr->hue;
2926 mutex_unlock(&zr->resource_lock);
2931 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2933 struct zoran_fh *fh = __fh;
2934 struct zoran *zr = fh->zr;
2935 struct video_picture pict;
2937 /* we only support hue/saturation/contrast/brightness */
2938 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2939 ctrl->id > V4L2_CID_HUE)
2942 if (ctrl->value < 0 || ctrl->value > 65535) {
2944 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
2945 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
2949 mutex_lock(&zr->resource_lock);
2951 case V4L2_CID_BRIGHTNESS:
2952 zr->brightness = ctrl->value;
2954 case V4L2_CID_CONTRAST:
2955 zr->contrast = ctrl->value;
2957 case V4L2_CID_SATURATION:
2958 zr->saturation = ctrl->value;
2961 zr->hue = ctrl->value;
2964 pict.brightness = zr->brightness;
2965 pict.contrast = zr->contrast;
2966 pict.colour = zr->saturation;
2969 decoder_command(zr, DECODER_SET_PICTURE, &pict);
2971 mutex_unlock(&zr->resource_lock);
2976 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2978 struct zoran_fh *fh = __fh;
2979 struct zoran *zr = fh->zr;
2982 mutex_lock(&zr->resource_lock);
2984 mutex_unlock(&zr->resource_lock);
2987 case VIDEO_MODE_PAL:
2988 *std = V4L2_STD_PAL;
2990 case VIDEO_MODE_NTSC:
2991 *std = V4L2_STD_NTSC;
2993 case VIDEO_MODE_SECAM:
2994 *std = V4L2_STD_SECAM;
3001 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
3003 struct zoran_fh *fh = __fh;
3004 struct zoran *zr = fh->zr;
3005 int norm = -1, res = 0;
3007 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3008 norm = VIDEO_MODE_PAL;
3009 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3010 norm = VIDEO_MODE_NTSC;
3011 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3012 norm = VIDEO_MODE_SECAM;
3013 else if (*std == V4L2_STD_ALL)
3014 norm = VIDEO_MODE_AUTO;
3017 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3018 ZR_DEVNAME(zr), (unsigned long long)*std);
3022 mutex_lock(&zr->resource_lock);
3023 res = zoran_set_norm(zr, norm);
3025 goto sstd_unlock_and_return;
3027 res = wait_grab_pending(zr);
3028 sstd_unlock_and_return:
3029 mutex_unlock(&zr->resource_lock);
3033 static int zoran_enum_input(struct file *file, void *__fh,
3034 struct v4l2_input *inp)
3036 struct zoran_fh *fh = __fh;
3037 struct zoran *zr = fh->zr;
3040 if (inp->index < 0 || inp->index >= zr->card.inputs)
3043 int id = inp->index;
3044 memset(inp, 0, sizeof(*inp));
3048 strncpy(inp->name, zr->card.input[inp->index].name,
3049 sizeof(inp->name) - 1);
3050 inp->type = V4L2_INPUT_TYPE_CAMERA;
3051 inp->std = V4L2_STD_ALL;
3053 /* Get status of video decoder */
3054 mutex_lock(&zr->resource_lock);
3055 decoder_command(zr, DECODER_GET_STATUS, &status);
3056 mutex_unlock(&zr->resource_lock);
3058 if (!(status & DECODER_STATUS_GOOD)) {
3059 inp->status |= V4L2_IN_ST_NO_POWER;
3060 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3062 if (!(status & DECODER_STATUS_COLOR))
3063 inp->status |= V4L2_IN_ST_NO_COLOR;
3068 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
3070 struct zoran_fh *fh = __fh;
3071 struct zoran *zr = fh->zr;
3073 mutex_lock(&zr->resource_lock);
3075 mutex_unlock(&zr->resource_lock);
3080 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
3082 struct zoran_fh *fh = __fh;
3083 struct zoran *zr = fh->zr;
3086 mutex_lock(&zr->resource_lock);
3087 res = zoran_set_input(zr, input);
3089 goto sinput_unlock_and_return;
3091 /* Make sure the changes come into effect */
3092 res = wait_grab_pending(zr);
3093 sinput_unlock_and_return:
3094 mutex_unlock(&zr->resource_lock);
3098 static int zoran_enum_output(struct file *file, void *__fh,
3099 struct v4l2_output *outp)
3101 if (outp->index != 0)
3104 memset(outp, 0, sizeof(*outp));
3106 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3107 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3112 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
3119 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
3127 /* cropping (sub-frame capture) */
3128 static int zoran_cropcap(struct file *file, void *__fh,
3129 struct v4l2_cropcap *cropcap)
3131 struct zoran_fh *fh = __fh;
3132 struct zoran *zr = fh->zr;
3133 int type = cropcap->type, res = 0;
3135 memset(cropcap, 0, sizeof(*cropcap));
3136 cropcap->type = type;
3138 mutex_lock(&zr->resource_lock);
3140 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3141 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3142 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3144 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3147 goto cropcap_unlock_and_return;
3150 cropcap->bounds.top = cropcap->bounds.left = 0;
3151 cropcap->bounds.width = BUZ_MAX_WIDTH;
3152 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3153 cropcap->defrect.top = cropcap->defrect.left = 0;
3154 cropcap->defrect.width = BUZ_MIN_WIDTH;
3155 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3156 cropcap_unlock_and_return:
3157 mutex_unlock(&zr->resource_lock);
3161 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3163 struct zoran_fh *fh = __fh;
3164 struct zoran *zr = fh->zr;
3165 int type = crop->type, res = 0;
3167 memset(crop, 0, sizeof(*crop));
3170 mutex_lock(&zr->resource_lock);
3172 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3173 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3174 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3177 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3180 goto gcrop_unlock_and_return;
3183 crop->c.top = fh->jpg_settings.img_y;
3184 crop->c.left = fh->jpg_settings.img_x;
3185 crop->c.width = fh->jpg_settings.img_width;
3186 crop->c.height = fh->jpg_settings.img_height;
3188 gcrop_unlock_and_return:
3189 mutex_unlock(&zr->resource_lock);
3194 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3196 struct zoran_fh *fh = __fh;
3197 struct zoran *zr = fh->zr;
3199 struct zoran_jpg_settings settings;
3201 settings = fh->jpg_settings;
3203 mutex_lock(&zr->resource_lock);
3205 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3207 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3210 goto scrop_unlock_and_return;
3213 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3214 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3215 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3217 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3220 goto scrop_unlock_and_return;
3223 /* move into a form that we understand */
3224 settings.img_x = crop->c.left;
3225 settings.img_y = crop->c.top;
3226 settings.img_width = crop->c.width;
3227 settings.img_height = crop->c.height;
3229 /* check validity */
3230 res = zoran_check_jpg_settings(zr, &settings, 0);
3232 goto scrop_unlock_and_return;
3235 fh->jpg_settings = settings;
3237 scrop_unlock_and_return:
3238 mutex_unlock(&zr->resource_lock);
3242 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3243 struct v4l2_jpegcompression *params)
3245 struct zoran_fh *fh = __fh;
3246 struct zoran *zr = fh->zr;
3247 memset(params, 0, sizeof(*params));
3249 mutex_lock(&zr->resource_lock);
3251 params->quality = fh->jpg_settings.jpg_comp.quality;
3252 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3253 memcpy(params->APP_data,
3254 fh->jpg_settings.jpg_comp.APP_data,
3255 fh->jpg_settings.jpg_comp.APP_len);
3256 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3257 memcpy(params->COM_data,
3258 fh->jpg_settings.jpg_comp.COM_data,
3259 fh->jpg_settings.jpg_comp.COM_len);
3260 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3261 params->jpeg_markers =
3262 fh->jpg_settings.jpg_comp.jpeg_markers;
3264 mutex_unlock(&zr->resource_lock);
3269 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3270 struct v4l2_jpegcompression *params)
3272 struct zoran_fh *fh = __fh;
3273 struct zoran *zr = fh->zr;
3275 struct zoran_jpg_settings settings;
3277 settings = fh->jpg_settings;
3279 settings.jpg_comp = *params;
3281 mutex_lock(&zr->resource_lock);
3283 if (fh->v4l_buffers.active != ZORAN_FREE ||
3284 fh->jpg_buffers.active != ZORAN_FREE) {
3285 dprintk(1, KERN_WARNING
3286 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3289 goto sjpegc_unlock_and_return;
3292 res = zoran_check_jpg_settings(zr, &settings, 0);
3294 goto sjpegc_unlock_and_return;
3295 if (!fh->jpg_buffers.allocated)
3296 fh->jpg_buffers.buffer_size =
3297 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3298 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3299 sjpegc_unlock_and_return:
3300 mutex_unlock(&zr->resource_lock);
3306 zoran_poll (struct file *file,
3309 struct zoran_fh *fh = file->private_data;
3310 struct zoran *zr = fh->zr;
3312 unsigned long flags;
3314 /* we should check whether buffers are ready to be synced on
3315 * (w/o waits - O_NONBLOCK) here
3316 * if ready for read (sync), return POLLIN|POLLRDNORM,
3317 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3318 * if error, return POLLERR,
3319 * if no buffers queued or so, return POLLNVAL
3322 mutex_lock(&zr->resource_lock);
3324 switch (fh->map_mode) {
3325 case ZORAN_MAP_MODE_RAW:
3326 poll_wait(file, &zr->v4l_capq, wait);
3327 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3329 spin_lock_irqsave(&zr->spinlock, flags);
3332 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3333 ZR_DEVNAME(zr), __func__,
3334 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3335 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3336 zr->v4l_pend_tail, zr->v4l_pend_head);
3337 /* Process is the one capturing? */
3338 if (fh->v4l_buffers.active != ZORAN_FREE &&
3339 /* Buffer ready to DQBUF? */
3340 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3341 res = POLLIN | POLLRDNORM;
3342 spin_unlock_irqrestore(&zr->spinlock, flags);
3346 case ZORAN_MAP_MODE_JPG_REC:
3347 case ZORAN_MAP_MODE_JPG_PLAY:
3348 poll_wait(file, &zr->jpg_capq, wait);
3349 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3351 spin_lock_irqsave(&zr->spinlock, flags);
3354 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3355 ZR_DEVNAME(zr), __func__,
3356 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3357 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3358 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3359 if (fh->jpg_buffers.active != ZORAN_FREE &&
3360 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3361 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3362 res = POLLIN | POLLRDNORM;
3364 res = POLLOUT | POLLWRNORM;
3366 spin_unlock_irqrestore(&zr->spinlock, flags);
3373 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3374 ZR_DEVNAME(zr), fh->map_mode);
3378 mutex_unlock(&zr->resource_lock);
3385 * This maps the buffers to user space.
3387 * Depending on the state of fh->map_mode
3388 * the V4L or the MJPEG buffers are mapped
3389 * per buffer or all together
3391 * Note that we need to connect to some
3392 * unmap signal event to unmap the de-allocate
3393 * the buffer accordingly (zoran_vm_close())
3397 zoran_vm_open (struct vm_area_struct *vma)
3399 struct zoran_mapping *map = vma->vm_private_data;
3405 zoran_vm_close (struct vm_area_struct *vma)
3407 struct zoran_mapping *map = vma->vm_private_data;
3408 struct file *file = map->file;
3409 struct zoran_fh *fh = file->private_data;
3410 struct zoran *zr = fh->zr;
3414 if (map->count == 0) {
3415 switch (fh->map_mode) {
3416 case ZORAN_MAP_MODE_JPG_REC:
3417 case ZORAN_MAP_MODE_JPG_PLAY:
3419 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3422 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3423 if (fh->jpg_buffers.buffer[i].map == map) {
3424 fh->jpg_buffers.buffer[i].map =
3430 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3431 if (fh->jpg_buffers.buffer[i].map)
3433 if (i == fh->jpg_buffers.num_buffers) {
3434 mutex_lock(&zr->resource_lock);
3436 if (fh->jpg_buffers.active != ZORAN_FREE) {
3437 jpg_qbuf(file, -1, zr->codec_mode);
3438 zr->jpg_buffers.allocated = 0;
3439 zr->jpg_buffers.active =
3440 fh->jpg_buffers.active =
3443 jpg_fbuffer_free(file);
3444 mutex_unlock(&zr->resource_lock);
3449 case ZORAN_MAP_MODE_RAW:
3451 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3454 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3455 if (fh->v4l_buffers.buffer[i].map == map) {
3457 fh->v4l_buffers.buffer[i].map =
3463 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3464 if (fh->v4l_buffers.buffer[i].map)
3466 if (i == fh->v4l_buffers.num_buffers) {
3467 mutex_lock(&zr->resource_lock);
3469 if (fh->v4l_buffers.active != ZORAN_FREE) {
3470 unsigned long flags;
3472 spin_lock_irqsave(&zr->spinlock, flags);
3473 zr36057_set_memgrab(zr, 0);
3474 zr->v4l_buffers.allocated = 0;
3475 zr->v4l_buffers.active =
3476 fh->v4l_buffers.active =
3478 spin_unlock_irqrestore(&zr->spinlock, flags);
3480 v4l_fbuffer_free(file);
3481 mutex_unlock(&zr->resource_lock);
3488 "%s: munmap() - internal error - unknown map mode %d\n",
3489 ZR_DEVNAME(zr), fh->map_mode);
3496 static struct vm_operations_struct zoran_vm_ops = {
3497 .open = zoran_vm_open,
3498 .close = zoran_vm_close,
3502 zoran_mmap (struct file *file,
3503 struct vm_area_struct *vma)
3505 struct zoran_fh *fh = file->private_data;
3506 struct zoran *zr = fh->zr;
3507 unsigned long size = (vma->vm_end - vma->vm_start);
3508 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3510 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3512 struct zoran_mapping *map;
3516 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3518 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3519 vma->vm_start, vma->vm_end, size);
3521 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3522 !(vma->vm_flags & VM_WRITE)) {
3525 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3530 switch (fh->map_mode) {
3532 case ZORAN_MAP_MODE_JPG_REC:
3533 case ZORAN_MAP_MODE_JPG_PLAY:
3536 mutex_lock(&zr->resource_lock);
3538 /* Map the MJPEG buffers */
3539 if (!fh->jpg_buffers.allocated) {
3542 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3545 goto jpg_mmap_unlock_and_return;
3548 first = offset / fh->jpg_buffers.buffer_size;
3549 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3550 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3551 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3552 last < 0 || first >= fh->jpg_buffers.num_buffers ||
3553 last >= fh->jpg_buffers.num_buffers) {
3556 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3557 ZR_DEVNAME(zr), offset, size,
3558 fh->jpg_buffers.buffer_size,
3559 fh->jpg_buffers.num_buffers);
3561 goto jpg_mmap_unlock_and_return;
3563 for (i = first; i <= last; i++) {
3564 if (fh->jpg_buffers.buffer[i].map) {
3567 "%s: mmap(MJPEG) - buffer %d already mapped\n",
3570 goto jpg_mmap_unlock_and_return;
3574 /* map these buffers (v4l_buffers[i]) */
3575 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3578 goto jpg_mmap_unlock_and_return;
3583 vma->vm_ops = &zoran_vm_ops;
3584 vma->vm_flags |= VM_DONTEXPAND;
3585 vma->vm_private_data = map;
3587 for (i = first; i <= last; i++) {
3589 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3592 (le32_to_cpu(fh->jpg_buffers.buffer[i].
3593 frag_tab[2 * j + 1]) & ~1) << 1;
3598 le32_to_cpu(fh->jpg_buffers.
3599 buffer[i].frag_tab[2 * j]);
3600 /* should just be pos on i386 */
3601 page = virt_to_phys(bus_to_virt(pos))
3603 if (remap_pfn_range(vma, start, page,
3604 todo, PAGE_SHARED)) {
3607 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3610 goto jpg_mmap_unlock_and_return;
3616 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3617 frag_tab[2 * j + 1]) & 1)
3618 break; /* was last fragment */
3620 fh->jpg_buffers.buffer[i].map = map;
3625 jpg_mmap_unlock_and_return:
3626 mutex_unlock(&zr->resource_lock);
3630 case ZORAN_MAP_MODE_RAW:
3632 mutex_lock(&zr->resource_lock);
3634 /* Map the V4L buffers */
3635 if (!fh->v4l_buffers.allocated) {
3638 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3641 goto v4l_mmap_unlock_and_return;
3644 first = offset / fh->v4l_buffers.buffer_size;
3645 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3646 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3647 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3648 last < 0 || first >= fh->v4l_buffers.num_buffers ||
3649 last >= fh->v4l_buffers.buffer_size) {
3652 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3653 ZR_DEVNAME(zr), offset, size,
3654 fh->v4l_buffers.buffer_size,
3655 fh->v4l_buffers.num_buffers);
3657 goto v4l_mmap_unlock_and_return;
3659 for (i = first; i <= last; i++) {
3660 if (fh->v4l_buffers.buffer[i].map) {
3663 "%s: mmap(V4L) - buffer %d already mapped\n",
3666 goto v4l_mmap_unlock_and_return;
3670 /* map these buffers (v4l_buffers[i]) */
3671 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3674 goto v4l_mmap_unlock_and_return;
3679 vma->vm_ops = &zoran_vm_ops;
3680 vma->vm_flags |= VM_DONTEXPAND;
3681 vma->vm_private_data = map;
3683 for (i = first; i <= last; i++) {
3685 if (todo > fh->v4l_buffers.buffer_size)
3686 todo = fh->v4l_buffers.buffer_size;
3687 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3688 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3689 todo, PAGE_SHARED)) {
3692 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3695 goto v4l_mmap_unlock_and_return;
3699 fh->v4l_buffers.buffer[i].map = map;
3703 v4l_mmap_unlock_and_return:
3704 mutex_unlock(&zr->resource_lock);
3711 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3712 ZR_DEVNAME(zr), fh->map_mode);
3719 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3720 .vidioc_querycap = zoran_querycap,
3721 .vidioc_cropcap = zoran_cropcap,
3722 .vidioc_s_crop = zoran_s_crop,
3723 .vidioc_g_crop = zoran_g_crop,
3724 .vidioc_enum_input = zoran_enum_input,
3725 .vidioc_g_input = zoran_g_input,
3726 .vidioc_s_input = zoran_s_input,
3727 .vidioc_enum_output = zoran_enum_output,
3728 .vidioc_g_output = zoran_g_output,
3729 .vidioc_s_output = zoran_s_output,
3730 .vidioc_g_fbuf = zoran_g_fbuf,
3731 .vidioc_s_fbuf = zoran_s_fbuf,
3732 .vidioc_g_std = zoran_g_std,
3733 .vidioc_s_std = zoran_s_std,
3734 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3735 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3736 .vidioc_overlay = zoran_overlay,
3737 .vidioc_reqbufs = zoran_reqbufs,
3738 .vidioc_querybuf = zoran_querybuf,
3739 .vidioc_qbuf = zoran_qbuf,
3740 .vidioc_dqbuf = zoran_dqbuf,
3741 .vidioc_streamon = zoran_streamon,
3742 .vidioc_streamoff = zoran_streamoff,
3743 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3744 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3745 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3746 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3747 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3748 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3749 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3750 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3751 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3752 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3753 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3754 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3755 .vidioc_queryctrl = zoran_queryctrl,
3756 .vidioc_s_ctrl = zoran_s_ctrl,
3757 .vidioc_g_ctrl = zoran_g_ctrl,
3758 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3759 .vidioc_default = zoran_default,
3760 .vidiocgmbuf = zoran_vidiocgmbuf,
3764 static const struct v4l2_file_operations zoran_fops = {
3765 .owner = THIS_MODULE,
3767 .release = zoran_close,
3768 .ioctl = video_ioctl2,
3770 .write = zoran_write,
3775 struct video_device zoran_template __devinitdata = {
3777 .fops = &zoran_fops,
3778 .ioctl_ops = &zoran_ioctl_ops,
3779 .release = &zoran_vdev_release,
3780 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,