2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
60 #include <linux/spinlock.h>
62 #include <linux/videodev.h>
63 #include <media/v4l2-common.h>
64 #include <media/v4l2-ioctl.h>
65 #include "videocodec.h"
67 #include <asm/byteorder.h>
69 #include <asm/uaccess.h>
70 #include <linux/proc_fs.h>
72 #include <linux/mutex.h>
74 #include "zoran_device.h"
75 #include "zoran_card.h"
78 const struct zoran_format zoran_formats[] = {
80 .name = "15-bit RGB LE",
81 .fourcc = V4L2_PIX_FMT_RGB555,
82 .colorspace = V4L2_COLORSPACE_SRGB,
84 .flags = ZORAN_FORMAT_CAPTURE |
86 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
87 ZR36057_VFESPFR_LittleEndian,
89 .name = "15-bit RGB BE",
90 .fourcc = V4L2_PIX_FMT_RGB555X,
91 .colorspace = V4L2_COLORSPACE_SRGB,
93 .flags = ZORAN_FORMAT_CAPTURE |
95 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
97 .name = "16-bit RGB LE",
98 .fourcc = V4L2_PIX_FMT_RGB565,
99 .colorspace = V4L2_COLORSPACE_SRGB,
101 .flags = ZORAN_FORMAT_CAPTURE |
102 ZORAN_FORMAT_OVERLAY,
103 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
104 ZR36057_VFESPFR_LittleEndian,
106 .name = "16-bit RGB BE",
107 .fourcc = V4L2_PIX_FMT_RGB565X,
108 .colorspace = V4L2_COLORSPACE_SRGB,
110 .flags = ZORAN_FORMAT_CAPTURE |
111 ZORAN_FORMAT_OVERLAY,
112 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
114 .name = "24-bit RGB",
115 .fourcc = V4L2_PIX_FMT_BGR24,
116 .colorspace = V4L2_COLORSPACE_SRGB,
118 .flags = ZORAN_FORMAT_CAPTURE |
119 ZORAN_FORMAT_OVERLAY,
120 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
122 .name = "32-bit RGB LE",
123 .fourcc = V4L2_PIX_FMT_BGR32,
124 .colorspace = V4L2_COLORSPACE_SRGB,
126 .flags = ZORAN_FORMAT_CAPTURE |
127 ZORAN_FORMAT_OVERLAY,
128 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
130 .name = "32-bit RGB BE",
131 .fourcc = V4L2_PIX_FMT_RGB32,
132 .colorspace = V4L2_COLORSPACE_SRGB,
134 .flags = ZORAN_FORMAT_CAPTURE |
135 ZORAN_FORMAT_OVERLAY,
136 .vfespfr = ZR36057_VFESPFR_RGB888,
138 .name = "4:2:2, packed, YUYV",
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .colorspace = V4L2_COLORSPACE_SMPTE170M,
142 .flags = ZORAN_FORMAT_CAPTURE |
143 ZORAN_FORMAT_OVERLAY,
144 .vfespfr = ZR36057_VFESPFR_YUV422,
146 .name = "4:2:2, packed, UYVY",
147 .fourcc = V4L2_PIX_FMT_UYVY,
148 .colorspace = V4L2_COLORSPACE_SMPTE170M,
150 .flags = ZORAN_FORMAT_CAPTURE |
151 ZORAN_FORMAT_OVERLAY,
152 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
154 .name = "Hardware-encoded Motion-JPEG",
155 .fourcc = V4L2_PIX_FMT_MJPEG,
156 .colorspace = V4L2_COLORSPACE_SMPTE170M,
158 .flags = ZORAN_FORMAT_CAPTURE |
159 ZORAN_FORMAT_PLAYBACK |
160 ZORAN_FORMAT_COMPRESSED,
163 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
165 /* small helper function for calculating buffersizes for v4l2
166 * we calculate the nearest higher power-of-two, which
167 * will be the recommended buffersize */
169 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
171 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
172 __u32 num = (1024 * 512) / (div);
181 if (result > jpg_bufsize)
188 /* forward references */
189 static void v4l_fbuffer_free(struct file *file);
190 static void jpg_fbuffer_free(struct file *file);
192 /* Set mapping mode */
193 static void map_mode_raw(struct zoran_fh *fh)
195 fh->map_mode = ZORAN_MAP_MODE_RAW;
196 fh->buffers.buffer_size = v4l_bufsize;
197 fh->buffers.num_buffers = v4l_nbufs;
199 static void map_mode_jpg(struct zoran_fh *fh, int play)
201 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
202 fh->buffers.buffer_size = jpg_bufsize;
203 fh->buffers.num_buffers = jpg_nbufs;
205 static inline const char *mode_name(enum zoran_map_mode mode)
207 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
211 * Allocate the V4L grab buffers
213 * These have to be pysically contiguous.
217 v4l_fbuffer_alloc (struct file *file)
219 struct zoran_fh *fh = file->private_data;
220 struct zoran *zr = fh->zr;
224 for (i = 0; i < fh->buffers.num_buffers; i++) {
225 if (fh->buffers.buffer[i].v4l.fbuffer)
228 "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
232 mem = kmalloc(fh->buffers.buffer_size,
233 GFP_KERNEL | __GFP_NOWARN);
237 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
239 v4l_fbuffer_free(file);
242 fh->buffers.buffer[i].v4l.fbuffer = mem;
243 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
244 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
245 for (off = 0; off < fh->buffers.buffer_size;
247 SetPageReserved(virt_to_page(mem + off));
250 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
251 ZR_DEVNAME(zr), i, (unsigned long) mem,
255 fh->buffers.allocated = 1;
260 /* free the V4L grab buffers */
262 v4l_fbuffer_free (struct file *file)
264 struct zoran_fh *fh = file->private_data;
265 struct zoran *zr = fh->zr;
269 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
271 for (i = 0; i < fh->buffers.num_buffers; i++) {
272 if (!fh->buffers.buffer[i].v4l.fbuffer)
275 mem = fh->buffers.buffer[i].v4l.fbuffer;
276 for (off = 0; off < fh->buffers.buffer_size;
278 ClearPageReserved(virt_to_page(mem + off));
279 kfree(fh->buffers.buffer[i].v4l.fbuffer);
280 fh->buffers.buffer[i].v4l.fbuffer = NULL;
283 fh->buffers.allocated = 0;
287 * Allocate the MJPEG grab buffers.
289 * If a Natoma chipset is present and this is a revision 1 zr36057,
290 * each MJPEG buffer needs to be physically contiguous.
291 * (RJ: This statement is from Dave Perks' original driver,
292 * I could never check it because I have a zr36067)
294 * RJ: The contents grab buffers needs never be accessed in the driver.
295 * Therefore there is no need to allocate them with vmalloc in order
296 * to get a contiguous virtual memory space.
297 * I don't understand why many other drivers first allocate them with
298 * vmalloc (which uses internally also get_zeroed_page, but delivers you
299 * virtual addresses) and then again have to make a lot of efforts
300 * to get the physical address.
303 * On big-endian architectures (such as ppc) some extra steps
304 * are needed. When reading and writing to the stat_com array
305 * and fragment buffers, the device expects to see little-
306 * endian values. The use of cpu_to_le32() and le32_to_cpu()
307 * in this function (and one or two others in zoran_device.c)
308 * ensure that these values are always stored in little-endian
309 * form, regardless of architecture. The zr36057 does Very Bad
310 * Things on big endian architectures if the stat_com array
311 * and fragment buffers are not little-endian.
315 jpg_fbuffer_alloc (struct file *file)
317 struct zoran_fh *fh = file->private_data;
318 struct zoran *zr = fh->zr;
322 for (i = 0; i < fh->buffers.num_buffers; i++) {
323 if (fh->buffers.buffer[i].jpg.frag_tab)
326 "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
329 /* Allocate fragment table for this buffer */
331 mem = (void *)get_zeroed_page(GFP_KERNEL);
335 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
337 jpg_fbuffer_free(file);
340 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
341 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
343 if (fh->buffers.need_contiguous) {
344 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
348 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
350 jpg_fbuffer_free(file);
353 fh->buffers.buffer[i].jpg.frag_tab[0] =
354 cpu_to_le32(virt_to_bus(mem));
355 fh->buffers.buffer[i].jpg.frag_tab[1] =
356 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
357 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
358 SetPageReserved(virt_to_page(mem + off));
360 /* jpg_bufsize is already page aligned */
361 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
362 mem = (void *)get_zeroed_page(GFP_KERNEL);
366 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
368 jpg_fbuffer_free(file);
372 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
373 cpu_to_le32(virt_to_bus(mem));
374 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
375 cpu_to_le32((PAGE_SIZE >> 2) << 1);
376 SetPageReserved(virt_to_page(mem));
379 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
384 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
386 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
388 fh->buffers.allocated = 1;
393 /* free the MJPEG grab buffers */
395 jpg_fbuffer_free (struct file *file)
397 struct zoran_fh *fh = file->private_data;
398 struct zoran *zr = fh->zr;
402 struct zoran_buffer *buffer;
404 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
406 for (i = 0, buffer = &fh->buffers.buffer[0];
407 i < fh->buffers.num_buffers; i++, buffer++) {
408 if (!buffer->jpg.frag_tab)
411 if (fh->buffers.need_contiguous) {
412 frag_tab = buffer->jpg.frag_tab[0];
415 mem = bus_to_virt(le32_to_cpu(frag_tab));
416 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
417 ClearPageReserved(virt_to_page(mem + off));
419 buffer->jpg.frag_tab[0] = 0;
420 buffer->jpg.frag_tab[1] = 0;
423 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
424 frag_tab = buffer->jpg.frag_tab[2 * j];
428 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
429 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
430 buffer->jpg.frag_tab[2 * j] = 0;
431 buffer->jpg.frag_tab[2 * j + 1] = 0;
435 free_page((unsigned long)buffer->jpg.frag_tab);
436 buffer->jpg.frag_tab = NULL;
439 fh->buffers.allocated = 0;
443 * V4L Buffer grabbing
447 zoran_v4l_set_format (struct zoran_fh *fh,
450 const struct zoran_format *format)
452 struct zoran *zr = fh->zr;
455 /* Check size and format of the grab wanted */
457 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
458 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
461 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
462 ZR_DEVNAME(zr), width, height);
466 bpp = (format->depth + 7) / 8;
468 /* Check against available buffer size */
469 if (height * width * bpp > fh->buffers.buffer_size) {
472 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
473 ZR_DEVNAME(zr), fh->buffers.buffer_size >> 10);
477 /* The video front end needs 4-byte alinged line sizes */
479 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
482 "%s: v4l_set_format() - wrong frame alignment\n",
487 fh->v4l_settings.width = width;
488 fh->v4l_settings.height = height;
489 fh->v4l_settings.format = format;
490 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
496 zoran_v4l_queue_frame (struct file *file,
499 struct zoran_fh *fh = file->private_data;
500 struct zoran *zr = fh->zr;
504 if (!fh->buffers.allocated) {
507 "%s: v4l_queue_frame() - buffers not yet allocated\n",
512 /* No grabbing outside the buffer range! */
513 if (num >= fh->buffers.num_buffers || num < 0) {
516 "%s: v4l_queue_frame() - buffer %d is out of range\n",
517 ZR_DEVNAME(zr), num);
521 spin_lock_irqsave(&zr->spinlock, flags);
523 if (fh->buffers.active == ZORAN_FREE) {
524 if (zr->v4l_buffers.active == ZORAN_FREE) {
525 zr->v4l_buffers = fh->buffers;
526 fh->buffers.active = ZORAN_ACTIVE;
530 "%s: v4l_queue_frame() - another session is already capturing\n",
536 /* make sure a grab isn't going on currently with this buffer */
538 switch (zr->v4l_buffers.buffer[num].state) {
541 if (zr->v4l_buffers.active == ZORAN_FREE) {
542 fh->buffers.active = ZORAN_FREE;
543 zr->v4l_buffers.allocated = 0;
545 res = -EBUSY; /* what are you doing? */
550 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
551 ZR_DEVNAME(zr), num);
553 /* since there is at least one unused buffer there's room for at least
554 * one more pend[] entry */
555 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
556 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
557 zr->v4l_buffers.buffer[num].bs.length =
558 fh->v4l_settings.bytesperline *
559 zr->v4l_settings.height;
560 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
565 spin_unlock_irqrestore(&zr->spinlock, flags);
567 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
568 zr->v4l_buffers.active = fh->buffers.active;
574 * Sync on a V4L buffer
578 v4l_sync (struct file *file,
581 struct zoran_fh *fh = file->private_data;
582 struct zoran *zr = fh->zr;
585 if (fh->buffers.active == ZORAN_FREE) {
588 "%s: v4l_sync() - no grab active for this session\n",
593 /* check passed-in frame number */
594 if (frame >= fh->buffers.num_buffers || frame < 0) {
596 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
597 ZR_DEVNAME(zr), frame);
601 /* Check if is buffer was queued at all */
602 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
605 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
610 /* wait on this buffer to get ready */
611 if (!wait_event_interruptible_timeout(zr->v4l_capq,
612 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
614 if (signal_pending(current))
617 /* buffer should now be in BUZ_STATE_DONE */
618 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
620 KERN_ERR "%s: v4l_sync() - internal state error\n",
623 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
624 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
626 spin_lock_irqsave(&zr->spinlock, flags);
628 /* Check if streaming capture has finished */
629 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
630 zr36057_set_memgrab(zr, 0);
631 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
632 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
633 zr->v4l_buffers.allocated = 0;
637 spin_unlock_irqrestore(&zr->spinlock, flags);
643 * Queue a MJPEG buffer for capture/playback
647 zoran_jpg_queue_frame (struct file *file,
649 enum zoran_codec_mode mode)
651 struct zoran_fh *fh = file->private_data;
652 struct zoran *zr = fh->zr;
656 /* Check if buffers are allocated */
657 if (!fh->buffers.allocated) {
660 "%s: jpg_queue_frame() - buffers not yet allocated\n",
665 /* No grabbing outside the buffer range! */
666 if (num >= fh->buffers.num_buffers || num < 0) {
669 "%s: jpg_queue_frame() - buffer %d out of range\n",
670 ZR_DEVNAME(zr), num);
674 /* what is the codec mode right now? */
675 if (zr->codec_mode == BUZ_MODE_IDLE) {
676 zr->jpg_settings = fh->jpg_settings;
677 } else if (zr->codec_mode != mode) {
678 /* wrong codec mode active - invalid */
681 "%s: jpg_queue_frame() - codec in wrong mode\n",
686 if (fh->buffers.active == ZORAN_FREE) {
687 if (zr->jpg_buffers.active == ZORAN_FREE) {
688 zr->jpg_buffers = fh->buffers;
689 fh->buffers.active = ZORAN_ACTIVE;
693 "%s: jpg_queue_frame() - another session is already capturing\n",
699 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
700 /* Ok load up the jpeg codec */
701 zr36057_enable_jpg(zr, mode);
704 spin_lock_irqsave(&zr->spinlock, flags);
707 switch (zr->jpg_buffers.buffer[num].state) {
711 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
714 /* since there is at least one unused buffer there's room for at
715 *least one more pend[] entry */
716 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
717 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
718 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
719 zoran_feed_stat_com(zr);
724 if (zr->jpg_buffers.active == ZORAN_FREE) {
725 fh->buffers.active = ZORAN_FREE;
726 zr->jpg_buffers.allocated = 0;
728 res = -EBUSY; /* what are you doing? */
733 spin_unlock_irqrestore(&zr->spinlock, flags);
735 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
736 zr->jpg_buffers.active = fh->buffers.active;
742 jpg_qbuf (struct file *file,
744 enum zoran_codec_mode mode)
746 struct zoran_fh *fh = file->private_data;
747 struct zoran *zr = fh->zr;
750 /* Does the user want to stop streaming? */
752 if (zr->codec_mode == mode) {
753 if (fh->buffers.active == ZORAN_FREE) {
756 "%s: jpg_qbuf(-1) - session not active\n",
760 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
761 zr->jpg_buffers.allocated = 0;
762 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
767 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
773 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
776 /* Start the jpeg codec when the first frame is queued */
777 if (!res && zr->jpg_que_head == 1)
784 * Sync on a MJPEG buffer
788 jpg_sync (struct file *file,
789 struct zoran_sync *bs)
791 struct zoran_fh *fh = file->private_data;
792 struct zoran *zr = fh->zr;
796 if (fh->buffers.active == ZORAN_FREE) {
799 "%s: jpg_sync() - capture is not currently active\n",
803 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
804 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
807 "%s: jpg_sync() - codec not in streaming mode\n",
811 if (!wait_event_interruptible_timeout(zr->jpg_capq,
812 (zr->jpg_que_tail != zr->jpg_dma_tail ||
813 zr->jpg_dma_tail == zr->jpg_dma_head),
817 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
819 zr->codec->control(zr->codec, CODEC_G_STATUS,
823 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
824 ZR_DEVNAME(zr), isr);
829 if (signal_pending(current))
832 spin_lock_irqsave(&zr->spinlock, flags);
834 if (zr->jpg_dma_tail != zr->jpg_dma_head)
835 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
837 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
839 /* buffer should now be in BUZ_STATE_DONE */
840 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
842 KERN_ERR "%s: jpg_sync() - internal state error\n",
845 *bs = zr->jpg_buffers.buffer[frame].bs;
847 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
848 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
850 spin_unlock_irqrestore(&zr->spinlock, flags);
856 zoran_open_init_session (struct file *file)
859 struct zoran_fh *fh = file->private_data;
860 struct zoran *zr = fh->zr;
862 /* Per default, map the V4L Buffers */
865 /* take over the card's current settings */
866 fh->overlay_settings = zr->overlay_settings;
867 fh->overlay_settings.is_set = 0;
868 fh->overlay_settings.format = zr->overlay_settings.format;
869 fh->overlay_active = ZORAN_FREE;
872 fh->v4l_settings = zr->v4l_settings;
874 fh->jpg_settings = zr->jpg_settings;
877 memset(&fh->buffers, 0, sizeof(fh->buffers));
878 for (i = 0; i < MAX_FRAME; i++) {
879 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
880 fh->buffers.buffer[i].bs.frame = i;
882 fh->buffers.allocated = 0;
883 fh->buffers.active = ZORAN_FREE;
887 zoran_close_end_session (struct file *file)
889 struct zoran_fh *fh = file->private_data;
890 struct zoran *zr = fh->zr;
893 if (fh->overlay_active != ZORAN_FREE) {
894 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
895 zr->v4l_overlay_active = 0;
896 if (!zr->v4l_memgrab_active)
897 zr36057_overlay(zr, 0);
898 zr->overlay_mask = NULL;
901 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
903 if (fh->buffers.active != ZORAN_FREE) {
906 spin_lock_irqsave(&zr->spinlock, flags);
907 zr36057_set_memgrab(zr, 0);
908 zr->v4l_buffers.allocated = 0;
909 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
910 spin_unlock_irqrestore(&zr->spinlock, flags);
914 if (fh->buffers.allocated)
915 v4l_fbuffer_free(file);
918 if (fh->buffers.active != ZORAN_FREE) {
919 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
920 zr->jpg_buffers.allocated = 0;
921 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
925 if (fh->buffers.allocated)
926 jpg_fbuffer_free(file);
931 * Open a zoran card. Right now the flags stuff is just playing
934 static int zoran_open(struct file *file)
936 struct zoran *zr = video_drvdata(file);
938 int res, first_open = 0;
940 dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
941 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
945 if (zr->user >= 2048) {
946 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
947 ZR_DEVNAME(zr), zr->user);
952 /* now, create the open()-specific file_ops struct */
953 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
957 "%s: zoran_open() - allocation of zoran_fh failed\n",
962 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
965 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
966 if (!fh->overlay_mask) {
969 "%s: zoran_open() - allocation of overlay_mask failed\n",
978 /*mutex_unlock(&zr->resource_lock);*/
980 /* default setup - TODO: look at flags */
981 if (first_open) { /* First device open */
983 zoran_open_init_params(zr);
984 zoran_init_hardware(zr);
986 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
989 /* set file_ops stuff */
990 file->private_data = fh;
992 zoran_open_init_session(file);
1002 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1003 ZR_DEVNAME(zr), res, zr->user);
1009 zoran_close(struct file *file)
1011 struct zoran_fh *fh = file->private_data;
1012 struct zoran *zr = fh->zr;
1014 dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1015 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1017 /* kernel locks (fs/device.c), so don't do that ourselves
1018 * (prevents deadlocks) */
1019 /*mutex_lock(&zr->resource_lock);*/
1021 zoran_close_end_session(file);
1023 if (zr->user-- == 1) { /* Last process */
1024 /* Clean up JPEG process */
1025 wake_up_interruptible(&zr->jpg_capq);
1026 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1027 zr->jpg_buffers.allocated = 0;
1028 zr->jpg_buffers.active = ZORAN_FREE;
1030 /* disable interrupts */
1031 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1033 if (zr36067_debug > 1)
1034 print_interrupts(zr);
1037 zr->v4l_overlay_active = 0;
1038 zr36057_overlay(zr, 0);
1039 zr->overlay_mask = NULL;
1042 wake_up_interruptible(&zr->v4l_capq);
1043 zr36057_set_memgrab(zr, 0);
1044 zr->v4l_buffers.allocated = 0;
1045 zr->v4l_buffers.active = ZORAN_FREE;
1046 zoran_set_pci_master(zr, 0);
1048 if (!pass_through) { /* Switch to color bar */
1049 struct v4l2_routing route = { 2, 0 };
1051 decoder_call(zr, video, s_stream, 0);
1052 encoder_call(zr, video, s_routing, &route);
1056 file->private_data = NULL;
1057 kfree(fh->overlay_mask);
1060 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1067 zoran_read (struct file *file,
1072 /* we simply don't support read() (yet)... */
1078 zoran_write (struct file *file,
1079 const char __user *data,
1083 /* ...and the same goes for write() */
1089 setup_fbuffer (struct file *file,
1091 const struct zoran_format *fmt,
1096 struct zoran_fh *fh = file->private_data;
1097 struct zoran *zr = fh->zr;
1099 /* (Ronald) v4l/v4l2 guidelines */
1100 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1103 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1104 ALi Magik (that needs very low latency while the card needs a
1105 higher value always) */
1107 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1110 /* we need a bytesperline value, even if not given */
1112 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1115 if (zr->overlay_active) {
1116 /* dzjee... stupid users... don't even bother to turn off
1117 * overlay before changing the memory location...
1118 * normally, we would return errors here. However, one of
1119 * the tools that does this is... xawtv! and since xawtv
1120 * is used by +/- 99% of the users, we'd rather be user-
1121 * friendly and silently do as if nothing went wrong */
1124 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1126 zr36057_overlay(zr, 0);
1130 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1133 "%s: setup_fbuffer() - no valid overlay format given\n",
1137 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1140 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1141 ZR_DEVNAME(zr), width, height, bytesperline);
1144 if (bytesperline & 3) {
1147 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1148 ZR_DEVNAME(zr), bytesperline);
1152 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1153 zr->vbuf_height = height;
1154 zr->vbuf_width = width;
1155 zr->vbuf_depth = fmt->depth;
1156 zr->overlay_settings.format = fmt;
1157 zr->vbuf_bytesperline = bytesperline;
1159 /* The user should set new window parameters */
1160 zr->overlay_settings.is_set = 0;
1167 setup_window (struct file *file,
1172 struct v4l2_clip __user *clips,
1174 void __user *bitmap)
1176 struct zoran_fh *fh = file->private_data;
1177 struct zoran *zr = fh->zr;
1178 struct v4l2_clip *vcp = NULL;
1182 if (!zr->vbuf_base) {
1185 "%s: setup_window() - frame buffer has to be set first\n",
1190 if (!fh->overlay_settings.format) {
1193 "%s: setup_window() - no overlay format set\n",
1199 * The video front end needs 4-byte alinged line sizes, we correct that
1200 * silently here if necessary
1202 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1203 end = (x + width) & ~1; /* round down */
1204 x = (x + 1) & ~1; /* round up */
1208 if (zr->vbuf_depth == 24) {
1209 end = (x + width) & ~3; /* round down */
1210 x = (x + 3) & ~3; /* round up */
1214 if (width > BUZ_MAX_WIDTH)
1215 width = BUZ_MAX_WIDTH;
1216 if (height > BUZ_MAX_HEIGHT)
1217 height = BUZ_MAX_HEIGHT;
1219 /* Check for vaild parameters */
1220 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1221 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1224 "%s: setup_window() - width = %d or height = %d invalid\n",
1225 ZR_DEVNAME(zr), width, height);
1229 fh->overlay_settings.x = x;
1230 fh->overlay_settings.y = y;
1231 fh->overlay_settings.width = width;
1232 fh->overlay_settings.height = height;
1233 fh->overlay_settings.clipcount = clipcount;
1236 * If an overlay is running, we have to switch it off
1237 * and switch it on again in order to get the new settings in effect.
1239 * We also want to avoid that the overlay mask is written
1240 * when an overlay is running.
1243 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1244 zr->overlay_active != ZORAN_FREE &&
1245 fh->overlay_active != ZORAN_FREE;
1247 zr36057_overlay(zr, 0);
1250 * Write the overlay mask if clips are wanted.
1251 * We prefer a bitmap.
1254 /* fake value - it just means we want clips */
1255 fh->overlay_settings.clipcount = 1;
1257 if (copy_from_user(fh->overlay_mask, bitmap,
1258 (width * height + 7) / 8)) {
1261 } else if (clipcount > 0) {
1262 /* write our own bitmap from the clips */
1263 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1267 "%s: setup_window() - Alloc of clip mask failed\n",
1272 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1276 write_overlay_mask(file, vcp, clipcount);
1280 fh->overlay_settings.is_set = 1;
1281 if (fh->overlay_active != ZORAN_FREE &&
1282 zr->overlay_active != ZORAN_FREE)
1283 zr->overlay_settings = fh->overlay_settings;
1286 zr36057_overlay(zr, 1);
1288 /* Make sure the changes come into effect */
1289 return wait_grab_pending(zr);
1293 setup_overlay (struct file *file,
1296 struct zoran_fh *fh = file->private_data;
1297 struct zoran *zr = fh->zr;
1299 /* If there is nothing to do, return immediatly */
1300 if ((on && fh->overlay_active != ZORAN_FREE) ||
1301 (!on && fh->overlay_active == ZORAN_FREE))
1304 /* check whether we're touching someone else's overlay */
1305 if (on && zr->overlay_active != ZORAN_FREE &&
1306 fh->overlay_active == ZORAN_FREE) {
1309 "%s: setup_overlay() - overlay is already active for another session\n",
1313 if (!on && zr->overlay_active != ZORAN_FREE &&
1314 fh->overlay_active == ZORAN_FREE) {
1317 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1323 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1324 zr->v4l_overlay_active = 0;
1325 /* When a grab is running, the video simply
1326 * won't be switched on any more */
1327 if (!zr->v4l_memgrab_active)
1328 zr36057_overlay(zr, 0);
1329 zr->overlay_mask = NULL;
1331 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1334 "%s: setup_overlay() - buffer or window not set\n",
1338 if (!fh->overlay_settings.format) {
1341 "%s: setup_overlay() - no overlay format set\n",
1345 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1346 zr->v4l_overlay_active = 1;
1347 zr->overlay_mask = fh->overlay_mask;
1348 zr->overlay_settings = fh->overlay_settings;
1349 if (!zr->v4l_memgrab_active)
1350 zr36057_overlay(zr, 1);
1351 /* When a grab is running, the video will be
1352 * switched on when grab is finished */
1355 /* Make sure the changes come into effect */
1356 return wait_grab_pending(zr);
1359 /* get the status of a buffer in the clients buffer queue */
1361 zoran_v4l2_buffer_status (struct zoran_fh *fh,
1362 struct v4l2_buffer *buf,
1365 struct zoran *zr = fh->zr;
1366 unsigned long flags;
1368 buf->flags = V4L2_BUF_FLAG_MAPPED;
1370 switch (fh->map_mode) {
1371 case ZORAN_MAP_MODE_RAW:
1373 if (num < 0 || num >= fh->buffers.num_buffers ||
1374 !fh->buffers.allocated) {
1377 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1382 spin_lock_irqsave(&zr->spinlock, flags);
1385 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1386 ZR_DEVNAME(zr), __func__,
1387 "FAL"[fh->buffers.active], num,
1388 "UPMD"[zr->v4l_buffers.buffer[num].state],
1389 fh->buffers.buffer[num].map ? 'Y' : 'N');
1390 spin_unlock_irqrestore(&zr->spinlock, flags);
1392 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1393 buf->length = fh->buffers.buffer_size;
1396 buf->bytesused = fh->buffers.buffer[num].bs.length;
1397 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1398 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1399 buf->sequence = fh->buffers.buffer[num].bs.seq;
1400 buf->flags |= V4L2_BUF_FLAG_DONE;
1401 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1403 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1406 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1407 buf->field = V4L2_FIELD_TOP;
1409 buf->field = V4L2_FIELD_INTERLACED;
1413 case ZORAN_MAP_MODE_JPG_REC:
1414 case ZORAN_MAP_MODE_JPG_PLAY:
1417 if (num < 0 || num >= fh->buffers.num_buffers ||
1418 !fh->buffers.allocated) {
1421 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1426 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1427 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1428 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1429 buf->length = fh->buffers.buffer_size;
1431 /* these variables are only written after frame has been captured */
1432 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1433 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1434 buf->sequence = fh->buffers.buffer[num].bs.seq;
1435 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1436 buf->bytesused = fh->buffers.buffer[num].bs.length;
1437 buf->flags |= V4L2_BUF_FLAG_DONE;
1439 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1442 /* which fields are these? */
1443 if (fh->jpg_settings.TmpDcm != 1)
1444 buf->field = fh->jpg_settings.odd_even ?
1445 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1447 buf->field = fh->jpg_settings.odd_even ?
1448 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1456 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1457 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1461 buf->memory = V4L2_MEMORY_MMAP;
1463 buf->m.offset = buf->length * num;
1469 zoran_set_norm (struct zoran *zr,
1474 if (zr->v4l_buffers.active != ZORAN_FREE ||
1475 zr->jpg_buffers.active != ZORAN_FREE) {
1478 "%s: set_norm() called while in playback/capture mode\n",
1483 if (!(norm & zr->card.norms)) {
1485 KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1486 ZR_DEVNAME(zr), norm);
1490 if (norm == V4L2_STD_ALL) {
1492 v4l2_std_id std = 0;
1494 decoder_call(zr, video, querystd, &std);
1495 decoder_s_std(zr, std);
1497 /* let changes come into effect */
1500 decoder_call(zr, video, g_input_status, &status);
1501 if (status & V4L2_IN_ST_NO_SIGNAL) {
1504 "%s: set_norm() - no norm detected\n",
1507 decoder_s_std(zr, zr->norm);
1513 if (norm & V4L2_STD_SECAM)
1514 zr->timing = zr->card.tvn[2];
1515 else if (norm & V4L2_STD_NTSC)
1516 zr->timing = zr->card.tvn[1];
1518 zr->timing = zr->card.tvn[0];
1520 /* We switch overlay off and on since a change in the
1521 * norm needs different VFE settings */
1522 on = zr->overlay_active && !zr->v4l_memgrab_active;
1524 zr36057_overlay(zr, 0);
1526 decoder_s_std(zr, norm);
1527 encoder_call(zr, video, s_std_output, norm);
1530 zr36057_overlay(zr, 1);
1532 /* Make sure the changes come into effect */
1539 zoran_set_input (struct zoran *zr,
1542 struct v4l2_routing route = { 0, 0 };
1544 if (input == zr->input) {
1548 if (zr->v4l_buffers.active != ZORAN_FREE ||
1549 zr->jpg_buffers.active != ZORAN_FREE) {
1552 "%s: set_input() called while in playback/capture mode\n",
1557 if (input < 0 || input >= zr->card.inputs) {
1560 "%s: set_input() - unnsupported input %d\n",
1561 ZR_DEVNAME(zr), input);
1565 route.input = zr->card.input[input].muxsel;
1568 decoder_s_routing(zr, &route);
1577 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1578 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1580 struct zoran_fh *fh = __fh;
1581 struct zoran *zr = fh->zr;
1582 struct zoran_jpg_settings settings;
1585 case BUZIOC_G_PARAMS:
1587 struct zoran_params *bparams = arg;
1589 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1591 memset(bparams, 0, sizeof(struct zoran_params));
1592 bparams->major_version = MAJOR_VERSION;
1593 bparams->minor_version = MINOR_VERSION;
1595 mutex_lock(&zr->resource_lock);
1597 if (zr->norm & V4L2_STD_NTSC)
1598 bparams->norm = VIDEO_MODE_NTSC;
1599 else if (zr->norm & V4L2_STD_PAL)
1600 bparams->norm = VIDEO_MODE_PAL;
1602 bparams->norm = VIDEO_MODE_SECAM;
1604 bparams->input = zr->input;
1606 bparams->decimation = fh->jpg_settings.decimation;
1607 bparams->HorDcm = fh->jpg_settings.HorDcm;
1608 bparams->VerDcm = fh->jpg_settings.VerDcm;
1609 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1610 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1611 bparams->img_x = fh->jpg_settings.img_x;
1612 bparams->img_y = fh->jpg_settings.img_y;
1613 bparams->img_width = fh->jpg_settings.img_width;
1614 bparams->img_height = fh->jpg_settings.img_height;
1615 bparams->odd_even = fh->jpg_settings.odd_even;
1617 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1618 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1619 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1620 memcpy(bparams->APP_data,
1621 fh->jpg_settings.jpg_comp.APP_data,
1622 sizeof(bparams->APP_data));
1623 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1624 memcpy(bparams->COM_data,
1625 fh->jpg_settings.jpg_comp.COM_data,
1626 sizeof(bparams->COM_data));
1627 bparams->jpeg_markers =
1628 fh->jpg_settings.jpg_comp.jpeg_markers;
1630 mutex_unlock(&zr->resource_lock);
1632 bparams->VFIFO_FB = 0;
1637 case BUZIOC_S_PARAMS:
1639 struct zoran_params *bparams = arg;
1642 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1644 settings.decimation = bparams->decimation;
1645 settings.HorDcm = bparams->HorDcm;
1646 settings.VerDcm = bparams->VerDcm;
1647 settings.TmpDcm = bparams->TmpDcm;
1648 settings.field_per_buff = bparams->field_per_buff;
1649 settings.img_x = bparams->img_x;
1650 settings.img_y = bparams->img_y;
1651 settings.img_width = bparams->img_width;
1652 settings.img_height = bparams->img_height;
1653 settings.odd_even = bparams->odd_even;
1655 settings.jpg_comp.quality = bparams->quality;
1656 settings.jpg_comp.APPn = bparams->APPn;
1657 settings.jpg_comp.APP_len = bparams->APP_len;
1658 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1659 sizeof(bparams->APP_data));
1660 settings.jpg_comp.COM_len = bparams->COM_len;
1661 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1662 sizeof(bparams->COM_data));
1663 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1665 mutex_lock(&zr->resource_lock);
1667 if (zr->codec_mode != BUZ_MODE_IDLE) {
1670 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1673 goto sparams_unlock_and_return;
1676 /* Check the params first before overwriting our
1678 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1680 goto sparams_unlock_and_return;
1683 fh->jpg_settings = settings;
1684 sparams_unlock_and_return:
1685 mutex_unlock(&zr->resource_lock);
1690 case BUZIOC_REQBUFS:
1692 struct zoran_requestbuffers *breq = arg;
1697 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1698 ZR_DEVNAME(zr), breq->count, breq->size);
1700 /* Enforce reasonable lower and upper limits */
1701 if (breq->count < 4)
1702 breq->count = 4; /* Could be choosen smaller */
1703 if (breq->count > jpg_nbufs)
1704 breq->count = jpg_nbufs;
1705 breq->size = PAGE_ALIGN(breq->size);
1706 if (breq->size < 8192)
1707 breq->size = 8192; /* Arbitrary */
1708 /* breq->size is limited by 1 page for the stat_com
1709 * tables to a Maximum of 2 MB */
1710 if (breq->size > jpg_bufsize)
1711 breq->size = jpg_bufsize;
1713 mutex_lock(&zr->resource_lock);
1715 if (fh->buffers.allocated) {
1718 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1721 goto jpgreqbuf_unlock_and_return;
1724 /* The next mmap will map the MJPEG buffers - could
1725 * also be *_PLAY, but it doesn't matter here */
1726 map_mode_jpg(fh, 0);
1727 fh->buffers.num_buffers = breq->count;
1728 fh->buffers.buffer_size = breq->size;
1730 if (jpg_fbuffer_alloc(file)) {
1732 goto jpgreqbuf_unlock_and_return;
1735 jpgreqbuf_unlock_and_return:
1736 mutex_unlock(&zr->resource_lock);
1741 case BUZIOC_QBUF_CAPT:
1743 int *frame = arg, res;
1745 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1746 ZR_DEVNAME(zr), *frame);
1748 mutex_lock(&zr->resource_lock);
1749 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1750 mutex_unlock(&zr->resource_lock);
1755 case BUZIOC_QBUF_PLAY:
1757 int *frame = arg, res;
1759 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1760 ZR_DEVNAME(zr), *frame);
1762 mutex_lock(&zr->resource_lock);
1763 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1764 mutex_unlock(&zr->resource_lock);
1771 struct zoran_sync *bsync = arg;
1774 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1776 mutex_lock(&zr->resource_lock);
1778 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1779 dprintk(2, KERN_WARNING
1780 "%s: %s - not in jpg capture mode\n",
1781 ZR_DEVNAME(zr), __func__);
1784 res = jpg_sync(file, bsync);
1786 mutex_unlock(&zr->resource_lock);
1791 case BUZIOC_G_STATUS:
1793 struct zoran_status *bstat = arg;
1794 struct v4l2_routing route = { 0, 0 };
1795 int status = 0, res = 0;
1798 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1800 if (zr->codec_mode != BUZ_MODE_IDLE) {
1803 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1808 route.input = zr->card.input[bstat->input].muxsel;
1810 mutex_lock(&zr->resource_lock);
1812 if (zr->codec_mode != BUZ_MODE_IDLE) {
1815 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1818 goto gstat_unlock_and_return;
1821 decoder_s_routing(zr, &route);
1823 /* sleep 1 second */
1826 /* Get status of video decoder */
1827 decoder_call(zr, video, querystd, &norm);
1828 decoder_call(zr, video, g_input_status, &status);
1830 /* restore previous input and norm */
1831 route.input = zr->card.input[zr->input].muxsel;
1832 decoder_s_routing(zr, &route);
1833 gstat_unlock_and_return:
1834 mutex_unlock(&zr->resource_lock);
1838 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1839 if (norm & V4L2_STD_NTSC)
1840 bstat->norm = VIDEO_MODE_NTSC;
1841 else if (norm & V4L2_STD_SECAM)
1842 bstat->norm = VIDEO_MODE_SECAM;
1844 bstat->norm = VIDEO_MODE_PAL;
1847 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1858 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1860 struct zoran_fh *fh = __fh;
1861 struct zoran *zr = fh->zr;
1865 mutex_lock(&zr->resource_lock);
1867 if (fh->buffers.allocated) {
1870 "%s: VIDIOCGMBUF - buffers already allocated\n",
1873 goto v4l1reqbuf_unlock_and_return;
1876 /* The next mmap will map the V4L buffers */
1879 if (v4l_fbuffer_alloc(file)) {
1881 goto v4l1reqbuf_unlock_and_return;
1884 vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1885 vmbuf->frames = fh->buffers.num_buffers;
1886 for (i = 0; i < vmbuf->frames; i++)
1887 vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1889 v4l1reqbuf_unlock_and_return:
1890 mutex_unlock(&zr->resource_lock);
1896 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1898 struct zoran_fh *fh = __fh;
1899 struct zoran *zr = fh->zr;
1901 memset(cap, 0, sizeof(*cap));
1902 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1903 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1904 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1905 pci_name(zr->pci_dev));
1906 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1908 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1909 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1913 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1917 for (i = 0; i < NUM_FORMATS; i++) {
1918 if (zoran_formats[i].flags & flag)
1920 if (num == fmt->index)
1923 if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
1926 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1927 fmt->pixelformat = zoran_formats[i].fourcc;
1928 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1929 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1933 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1934 struct v4l2_fmtdesc *f)
1936 struct zoran_fh *fh = __fh;
1937 struct zoran *zr = fh->zr;
1939 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1942 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1943 struct v4l2_fmtdesc *f)
1945 struct zoran_fh *fh = __fh;
1946 struct zoran *zr = fh->zr;
1948 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1951 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1952 struct v4l2_fmtdesc *f)
1954 struct zoran_fh *fh = __fh;
1955 struct zoran *zr = fh->zr;
1957 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1960 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1961 struct v4l2_format *fmt)
1963 struct zoran_fh *fh = __fh;
1964 struct zoran *zr = fh->zr;
1966 mutex_lock(&zr->resource_lock);
1968 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1969 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1970 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1971 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1972 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1973 if (fh->jpg_settings.TmpDcm == 1)
1974 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1975 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1977 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1978 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1979 fmt->fmt.pix.bytesperline = 0;
1980 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1982 mutex_unlock(&zr->resource_lock);
1986 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1987 struct v4l2_format *fmt)
1989 struct zoran_fh *fh = __fh;
1990 struct zoran *zr = fh->zr;
1992 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1993 return zoran_g_fmt_vid_out(file, fh, fmt);
1995 mutex_lock(&zr->resource_lock);
1996 fmt->fmt.pix.width = fh->v4l_settings.width;
1997 fmt->fmt.pix.height = fh->v4l_settings.height;
1998 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1999 fh->v4l_settings.height;
2000 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2001 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2002 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2003 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2004 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2006 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2007 mutex_unlock(&zr->resource_lock);
2011 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2012 struct v4l2_format *fmt)
2014 struct zoran_fh *fh = __fh;
2015 struct zoran *zr = fh->zr;
2017 mutex_lock(&zr->resource_lock);
2019 fmt->fmt.win.w.left = fh->overlay_settings.x;
2020 fmt->fmt.win.w.top = fh->overlay_settings.y;
2021 fmt->fmt.win.w.width = fh->overlay_settings.width;
2022 fmt->fmt.win.w.height = fh->overlay_settings.height;
2023 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2024 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2026 fmt->fmt.win.field = V4L2_FIELD_TOP;
2028 mutex_unlock(&zr->resource_lock);
2032 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2033 struct v4l2_format *fmt)
2035 struct zoran_fh *fh = __fh;
2036 struct zoran *zr = fh->zr;
2038 mutex_lock(&zr->resource_lock);
2040 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2041 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2042 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2043 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2044 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2045 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2046 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2047 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2049 mutex_unlock(&zr->resource_lock);
2053 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2054 struct v4l2_format *fmt)
2056 struct zoran_fh *fh = __fh;
2057 struct zoran *zr = fh->zr;
2058 struct zoran_jpg_settings settings;
2061 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2064 mutex_lock(&zr->resource_lock);
2065 settings = fh->jpg_settings;
2067 /* we actually need to set 'real' parameters now */
2068 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2069 settings.TmpDcm = 1;
2071 settings.TmpDcm = 2;
2072 settings.decimation = 0;
2073 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2074 settings.VerDcm = 2;
2076 settings.VerDcm = 1;
2077 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2078 settings.HorDcm = 4;
2079 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2080 settings.HorDcm = 2;
2082 settings.HorDcm = 1;
2083 if (settings.TmpDcm == 1)
2084 settings.field_per_buff = 2;
2086 settings.field_per_buff = 1;
2088 if (settings.HorDcm > 1) {
2089 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2090 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2093 settings.img_width = BUZ_MAX_WIDTH;
2097 res = zoran_check_jpg_settings(zr, &settings, 1);
2099 goto tryfmt_unlock_and_return;
2101 /* tell the user what we actually did */
2102 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2103 fmt->fmt.pix.height = settings.img_height * 2 /
2104 (settings.TmpDcm * settings.VerDcm);
2105 if (settings.TmpDcm == 1)
2106 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2107 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2109 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2110 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2112 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2113 fmt->fmt.pix.bytesperline = 0;
2114 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2115 tryfmt_unlock_and_return:
2116 mutex_unlock(&zr->resource_lock);
2120 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2121 struct v4l2_format *fmt)
2123 struct zoran_fh *fh = __fh;
2124 struct zoran *zr = fh->zr;
2128 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2129 return zoran_try_fmt_vid_out(file, fh, fmt);
2131 mutex_lock(&zr->resource_lock);
2133 for (i = 0; i < NUM_FORMATS; i++)
2134 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2137 if (i == NUM_FORMATS) {
2138 mutex_unlock(&zr->resource_lock);
2142 bpp = (zoran_formats[i].depth + 7) / 8;
2143 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2144 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2145 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2146 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2147 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2148 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2149 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2150 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2151 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2152 mutex_unlock(&zr->resource_lock);
2157 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2158 struct v4l2_format *fmt)
2160 struct zoran_fh *fh = __fh;
2161 struct zoran *zr = fh->zr;
2164 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2165 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2166 fmt->fmt.win.w.width,
2167 fmt->fmt.win.w.height,
2168 fmt->fmt.win.clipcount,
2169 fmt->fmt.win.bitmap);
2170 mutex_lock(&zr->resource_lock);
2171 res = setup_window(file, fmt->fmt.win.w.left,
2173 fmt->fmt.win.w.width,
2174 fmt->fmt.win.w.height,
2175 (struct v4l2_clip __user *)
2177 fmt->fmt.win.clipcount,
2178 fmt->fmt.win.bitmap);
2179 mutex_unlock(&zr->resource_lock);
2183 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2184 struct v4l2_format *fmt)
2186 struct zoran_fh *fh = __fh;
2187 struct zoran *zr = fh->zr;
2188 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2189 struct zoran_jpg_settings settings;
2192 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2193 fmt->fmt.pix.width, fmt->fmt.pix.height,
2194 fmt->fmt.pix.pixelformat,
2195 (char *) &printformat);
2196 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2199 mutex_lock(&zr->resource_lock);
2201 if (fh->buffers.allocated) {
2202 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2205 goto sfmtjpg_unlock_and_return;
2208 settings = fh->jpg_settings;
2210 /* we actually need to set 'real' parameters now */
2211 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2212 settings.TmpDcm = 1;
2214 settings.TmpDcm = 2;
2215 settings.decimation = 0;
2216 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2217 settings.VerDcm = 2;
2219 settings.VerDcm = 1;
2220 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2221 settings.HorDcm = 4;
2222 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2223 settings.HorDcm = 2;
2225 settings.HorDcm = 1;
2226 if (settings.TmpDcm == 1)
2227 settings.field_per_buff = 2;
2229 settings.field_per_buff = 1;
2231 if (settings.HorDcm > 1) {
2232 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2233 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2236 settings.img_width = BUZ_MAX_WIDTH;
2240 res = zoran_check_jpg_settings(zr, &settings, 0);
2242 goto sfmtjpg_unlock_and_return;
2244 /* it's ok, so set them */
2245 fh->jpg_settings = settings;
2247 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2248 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2250 /* tell the user what we actually did */
2251 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2252 fmt->fmt.pix.height = settings.img_height * 2 /
2253 (settings.TmpDcm * settings.VerDcm);
2254 if (settings.TmpDcm == 1)
2255 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2256 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2258 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2259 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2260 fmt->fmt.pix.bytesperline = 0;
2261 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
2262 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2264 sfmtjpg_unlock_and_return:
2265 mutex_unlock(&zr->resource_lock);
2269 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2270 struct v4l2_format *fmt)
2272 struct zoran_fh *fh = __fh;
2273 struct zoran *zr = fh->zr;
2277 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2278 return zoran_s_fmt_vid_out(file, fh, fmt);
2280 for (i = 0; i < NUM_FORMATS; i++)
2281 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2283 if (i == NUM_FORMATS) {
2284 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2285 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2289 mutex_lock(&zr->resource_lock);
2291 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2292 fh->buffers.active != ZORAN_FREE) {
2293 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2296 goto sfmtv4l_unlock_and_return;
2298 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2299 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2300 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2301 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2305 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2308 goto sfmtv4l_unlock_and_return;
2310 /* tell the user the results/missing stuff */
2311 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2312 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2313 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2314 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2315 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2317 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2319 sfmtv4l_unlock_and_return:
2320 mutex_unlock(&zr->resource_lock);
2324 static int zoran_g_fbuf(struct file *file, void *__fh,
2325 struct v4l2_framebuffer *fb)
2327 struct zoran_fh *fh = __fh;
2328 struct zoran *zr = fh->zr;
2330 memset(fb, 0, sizeof(*fb));
2331 mutex_lock(&zr->resource_lock);
2332 fb->base = zr->vbuf_base;
2333 fb->fmt.width = zr->vbuf_width;
2334 fb->fmt.height = zr->vbuf_height;
2335 if (zr->overlay_settings.format)
2336 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2337 fb->fmt.bytesperline = zr->vbuf_bytesperline;
2338 mutex_unlock(&zr->resource_lock);
2339 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2340 fb->fmt.field = V4L2_FIELD_INTERLACED;
2341 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2342 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2347 static int zoran_s_fbuf(struct file *file, void *__fh,
2348 struct v4l2_framebuffer *fb)
2350 struct zoran_fh *fh = __fh;
2351 struct zoran *zr = fh->zr;
2353 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2355 for (i = 0; i < NUM_FORMATS; i++)
2356 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2358 if (i == NUM_FORMATS) {
2359 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2360 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2361 (char *)&printformat);
2365 mutex_lock(&zr->resource_lock);
2366 res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2367 fb->fmt.width, fb->fmt.height,
2368 fb->fmt.bytesperline);
2369 mutex_unlock(&zr->resource_lock);
2374 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2376 struct zoran_fh *fh = __fh;
2377 struct zoran *zr = fh->zr;
2380 mutex_lock(&zr->resource_lock);
2381 res = setup_overlay(file, on);
2382 mutex_unlock(&zr->resource_lock);
2387 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2389 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2391 struct zoran_fh *fh = __fh;
2392 struct zoran *zr = fh->zr;
2395 if (req->memory != V4L2_MEMORY_MMAP) {
2398 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2399 ZR_DEVNAME(zr), req->memory);
2403 if (req->count == 0)
2404 return zoran_streamoff(file, fh, req->type);
2406 mutex_lock(&zr->resource_lock);
2407 if (fh->buffers.allocated) {
2410 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2413 goto v4l2reqbuf_unlock_and_return;
2416 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2417 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2418 /* control user input */
2421 if (req->count > v4l_nbufs)
2422 req->count = v4l_nbufs;
2424 /* The next mmap will map the V4L buffers */
2426 fh->buffers.num_buffers = req->count;
2428 if (v4l_fbuffer_alloc(file)) {
2430 goto v4l2reqbuf_unlock_and_return;
2432 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2433 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2434 /* we need to calculate size ourselves now */
2437 if (req->count > jpg_nbufs)
2438 req->count = jpg_nbufs;
2440 /* The next mmap will map the MJPEG buffers */
2441 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2442 fh->buffers.num_buffers = req->count;
2443 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2445 if (jpg_fbuffer_alloc(file)) {
2447 goto v4l2reqbuf_unlock_and_return;
2452 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2453 ZR_DEVNAME(zr), req->type);
2455 goto v4l2reqbuf_unlock_and_return;
2457 v4l2reqbuf_unlock_and_return:
2458 mutex_unlock(&zr->resource_lock);
2463 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2465 struct zoran_fh *fh = __fh;
2466 struct zoran *zr = fh->zr;
2469 mutex_lock(&zr->resource_lock);
2470 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2471 mutex_unlock(&zr->resource_lock);
2476 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2478 struct zoran_fh *fh = __fh;
2479 struct zoran *zr = fh->zr;
2480 int res = 0, codec_mode, buf_type;
2482 mutex_lock(&zr->resource_lock);
2484 switch (fh->map_mode) {
2485 case ZORAN_MAP_MODE_RAW:
2486 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2488 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2489 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2491 goto qbuf_unlock_and_return;
2494 res = zoran_v4l_queue_frame(file, buf->index);
2496 goto qbuf_unlock_and_return;
2497 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2498 zr36057_set_memgrab(zr, 1);
2501 case ZORAN_MAP_MODE_JPG_REC:
2502 case ZORAN_MAP_MODE_JPG_PLAY:
2503 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2504 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2505 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2507 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2508 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2511 if (buf->type != buf_type) {
2513 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2514 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2516 goto qbuf_unlock_and_return;
2519 res = zoran_jpg_queue_frame(file, buf->index,
2522 goto qbuf_unlock_and_return;
2523 if (zr->codec_mode == BUZ_MODE_IDLE &&
2524 fh->buffers.active == ZORAN_LOCKED)
2525 zr36057_enable_jpg(zr, codec_mode);
2531 "%s: VIDIOC_QBUF - unsupported type %d\n",
2532 ZR_DEVNAME(zr), buf->type);
2536 qbuf_unlock_and_return:
2537 mutex_unlock(&zr->resource_lock);
2542 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2544 struct zoran_fh *fh = __fh;
2545 struct zoran *zr = fh->zr;
2546 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2548 mutex_lock(&zr->resource_lock);
2550 switch (fh->map_mode) {
2551 case ZORAN_MAP_MODE_RAW:
2552 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2554 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2555 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2557 goto dqbuf_unlock_and_return;
2560 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2561 if (file->f_flags & O_NONBLOCK &&
2562 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2564 goto dqbuf_unlock_and_return;
2566 res = v4l_sync(file, num);
2568 goto dqbuf_unlock_and_return;
2569 zr->v4l_sync_tail++;
2570 res = zoran_v4l2_buffer_status(fh, buf, num);
2573 case ZORAN_MAP_MODE_JPG_REC:
2574 case ZORAN_MAP_MODE_JPG_PLAY:
2576 struct zoran_sync bs;
2578 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2579 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2581 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2583 if (buf->type != buf_type) {
2585 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2586 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2588 goto dqbuf_unlock_and_return;
2591 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2593 if (file->f_flags & O_NONBLOCK &&
2594 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2596 goto dqbuf_unlock_and_return;
2598 res = jpg_sync(file, &bs);
2600 goto dqbuf_unlock_and_return;
2601 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2607 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2608 ZR_DEVNAME(zr), buf->type);
2612 dqbuf_unlock_and_return:
2613 mutex_unlock(&zr->resource_lock);
2618 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2620 struct zoran_fh *fh = __fh;
2621 struct zoran *zr = fh->zr;
2624 mutex_lock(&zr->resource_lock);
2626 switch (fh->map_mode) {
2627 case ZORAN_MAP_MODE_RAW: /* raw capture */
2628 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2629 fh->buffers.active != ZORAN_ACTIVE) {
2631 goto strmon_unlock_and_return;
2634 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2635 zr->v4l_settings = fh->v4l_settings;
2637 zr->v4l_sync_tail = zr->v4l_pend_tail;
2638 if (!zr->v4l_memgrab_active &&
2639 zr->v4l_pend_head != zr->v4l_pend_tail) {
2640 zr36057_set_memgrab(zr, 1);
2644 case ZORAN_MAP_MODE_JPG_REC:
2645 case ZORAN_MAP_MODE_JPG_PLAY:
2646 /* what is the codec mode right now? */
2647 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2648 fh->buffers.active != ZORAN_ACTIVE) {
2650 goto strmon_unlock_and_return;
2653 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2655 if (zr->jpg_que_head != zr->jpg_que_tail) {
2656 /* Start the jpeg codec when the first frame is queued */
2664 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2665 ZR_DEVNAME(zr), fh->map_mode);
2669 strmon_unlock_and_return:
2670 mutex_unlock(&zr->resource_lock);
2675 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2677 struct zoran_fh *fh = __fh;
2678 struct zoran *zr = fh->zr;
2680 unsigned long flags;
2682 mutex_lock(&zr->resource_lock);
2684 switch (fh->map_mode) {
2685 case ZORAN_MAP_MODE_RAW: /* raw capture */
2686 if (fh->buffers.active == ZORAN_FREE &&
2687 zr->v4l_buffers.active != ZORAN_FREE) {
2688 res = -EPERM; /* stay off other's settings! */
2689 goto strmoff_unlock_and_return;
2691 if (zr->v4l_buffers.active == ZORAN_FREE)
2692 goto strmoff_unlock_and_return;
2694 spin_lock_irqsave(&zr->spinlock, flags);
2695 /* unload capture */
2696 if (zr->v4l_memgrab_active) {
2698 zr36057_set_memgrab(zr, 0);
2701 for (i = 0; i < fh->buffers.num_buffers; i++)
2702 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2703 fh->buffers = zr->v4l_buffers;
2705 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2707 zr->v4l_grab_seq = 0;
2708 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2709 zr->v4l_sync_tail = 0;
2711 spin_unlock_irqrestore(&zr->spinlock, flags);
2715 case ZORAN_MAP_MODE_JPG_REC:
2716 case ZORAN_MAP_MODE_JPG_PLAY:
2717 if (fh->buffers.active == ZORAN_FREE &&
2718 zr->jpg_buffers.active != ZORAN_FREE) {
2719 res = -EPERM; /* stay off other's settings! */
2720 goto strmoff_unlock_and_return;
2722 if (zr->jpg_buffers.active == ZORAN_FREE)
2723 goto strmoff_unlock_and_return;
2725 res = jpg_qbuf(file, -1,
2726 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2727 BUZ_MODE_MOTION_COMPRESS :
2728 BUZ_MODE_MOTION_DECOMPRESS);
2730 goto strmoff_unlock_and_return;
2734 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2735 ZR_DEVNAME(zr), fh->map_mode);
2739 strmoff_unlock_and_return:
2740 mutex_unlock(&zr->resource_lock);
2745 static int zoran_queryctrl(struct file *file, void *__fh,
2746 struct v4l2_queryctrl *ctrl)
2748 struct zoran_fh *fh = __fh;
2749 struct zoran *zr = fh->zr;
2751 /* we only support hue/saturation/contrast/brightness */
2752 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2753 ctrl->id > V4L2_CID_HUE)
2756 decoder_call(zr, core, queryctrl, ctrl);
2761 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2763 struct zoran_fh *fh = __fh;
2764 struct zoran *zr = fh->zr;
2766 /* we only support hue/saturation/contrast/brightness */
2767 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2768 ctrl->id > V4L2_CID_HUE)
2771 mutex_lock(&zr->resource_lock);
2772 decoder_call(zr, core, g_ctrl, ctrl);
2773 mutex_unlock(&zr->resource_lock);
2778 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2780 struct zoran_fh *fh = __fh;
2781 struct zoran *zr = fh->zr;
2783 /* we only support hue/saturation/contrast/brightness */
2784 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2785 ctrl->id > V4L2_CID_HUE)
2788 mutex_lock(&zr->resource_lock);
2789 decoder_call(zr, core, s_ctrl, ctrl);
2790 mutex_unlock(&zr->resource_lock);
2795 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2797 struct zoran_fh *fh = __fh;
2798 struct zoran *zr = fh->zr;
2800 mutex_lock(&zr->resource_lock);
2802 mutex_unlock(&zr->resource_lock);
2806 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2808 struct zoran_fh *fh = __fh;
2809 struct zoran *zr = fh->zr;
2812 mutex_lock(&zr->resource_lock);
2813 res = zoran_set_norm(zr, *std);
2815 goto sstd_unlock_and_return;
2817 res = wait_grab_pending(zr);
2818 sstd_unlock_and_return:
2819 mutex_unlock(&zr->resource_lock);
2823 static int zoran_enum_input(struct file *file, void *__fh,
2824 struct v4l2_input *inp)
2826 struct zoran_fh *fh = __fh;
2827 struct zoran *zr = fh->zr;
2829 if (inp->index < 0 || inp->index >= zr->card.inputs)
2832 int id = inp->index;
2833 memset(inp, 0, sizeof(*inp));
2837 strncpy(inp->name, zr->card.input[inp->index].name,
2838 sizeof(inp->name) - 1);
2839 inp->type = V4L2_INPUT_TYPE_CAMERA;
2840 inp->std = V4L2_STD_ALL;
2842 /* Get status of video decoder */
2843 mutex_lock(&zr->resource_lock);
2844 decoder_call(zr, video, g_input_status, &inp->status);
2845 mutex_unlock(&zr->resource_lock);
2849 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2851 struct zoran_fh *fh = __fh;
2852 struct zoran *zr = fh->zr;
2854 mutex_lock(&zr->resource_lock);
2856 mutex_unlock(&zr->resource_lock);
2861 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2863 struct zoran_fh *fh = __fh;
2864 struct zoran *zr = fh->zr;
2867 mutex_lock(&zr->resource_lock);
2868 res = zoran_set_input(zr, input);
2870 goto sinput_unlock_and_return;
2872 /* Make sure the changes come into effect */
2873 res = wait_grab_pending(zr);
2874 sinput_unlock_and_return:
2875 mutex_unlock(&zr->resource_lock);
2879 static int zoran_enum_output(struct file *file, void *__fh,
2880 struct v4l2_output *outp)
2882 if (outp->index != 0)
2885 memset(outp, 0, sizeof(*outp));
2887 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2888 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2893 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2900 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2908 /* cropping (sub-frame capture) */
2909 static int zoran_cropcap(struct file *file, void *__fh,
2910 struct v4l2_cropcap *cropcap)
2912 struct zoran_fh *fh = __fh;
2913 struct zoran *zr = fh->zr;
2914 int type = cropcap->type, res = 0;
2916 memset(cropcap, 0, sizeof(*cropcap));
2917 cropcap->type = type;
2919 mutex_lock(&zr->resource_lock);
2921 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2922 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2923 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2925 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2928 goto cropcap_unlock_and_return;
2931 cropcap->bounds.top = cropcap->bounds.left = 0;
2932 cropcap->bounds.width = BUZ_MAX_WIDTH;
2933 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2934 cropcap->defrect.top = cropcap->defrect.left = 0;
2935 cropcap->defrect.width = BUZ_MIN_WIDTH;
2936 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2937 cropcap_unlock_and_return:
2938 mutex_unlock(&zr->resource_lock);
2942 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2944 struct zoran_fh *fh = __fh;
2945 struct zoran *zr = fh->zr;
2946 int type = crop->type, res = 0;
2948 memset(crop, 0, sizeof(*crop));
2951 mutex_lock(&zr->resource_lock);
2953 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2954 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2955 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2958 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2961 goto gcrop_unlock_and_return;
2964 crop->c.top = fh->jpg_settings.img_y;
2965 crop->c.left = fh->jpg_settings.img_x;
2966 crop->c.width = fh->jpg_settings.img_width;
2967 crop->c.height = fh->jpg_settings.img_height;
2969 gcrop_unlock_and_return:
2970 mutex_unlock(&zr->resource_lock);
2975 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2977 struct zoran_fh *fh = __fh;
2978 struct zoran *zr = fh->zr;
2980 struct zoran_jpg_settings settings;
2982 settings = fh->jpg_settings;
2984 mutex_lock(&zr->resource_lock);
2986 if (fh->buffers.allocated) {
2988 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2991 goto scrop_unlock_and_return;
2994 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2995 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2996 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2998 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3001 goto scrop_unlock_and_return;
3004 /* move into a form that we understand */
3005 settings.img_x = crop->c.left;
3006 settings.img_y = crop->c.top;
3007 settings.img_width = crop->c.width;
3008 settings.img_height = crop->c.height;
3010 /* check validity */
3011 res = zoran_check_jpg_settings(zr, &settings, 0);
3013 goto scrop_unlock_and_return;
3016 fh->jpg_settings = settings;
3018 scrop_unlock_and_return:
3019 mutex_unlock(&zr->resource_lock);
3023 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3024 struct v4l2_jpegcompression *params)
3026 struct zoran_fh *fh = __fh;
3027 struct zoran *zr = fh->zr;
3028 memset(params, 0, sizeof(*params));
3030 mutex_lock(&zr->resource_lock);
3032 params->quality = fh->jpg_settings.jpg_comp.quality;
3033 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3034 memcpy(params->APP_data,
3035 fh->jpg_settings.jpg_comp.APP_data,
3036 fh->jpg_settings.jpg_comp.APP_len);
3037 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3038 memcpy(params->COM_data,
3039 fh->jpg_settings.jpg_comp.COM_data,
3040 fh->jpg_settings.jpg_comp.COM_len);
3041 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3042 params->jpeg_markers =
3043 fh->jpg_settings.jpg_comp.jpeg_markers;
3045 mutex_unlock(&zr->resource_lock);
3050 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3051 struct v4l2_jpegcompression *params)
3053 struct zoran_fh *fh = __fh;
3054 struct zoran *zr = fh->zr;
3056 struct zoran_jpg_settings settings;
3058 settings = fh->jpg_settings;
3060 settings.jpg_comp = *params;
3062 mutex_lock(&zr->resource_lock);
3064 if (fh->buffers.active != ZORAN_FREE) {
3065 dprintk(1, KERN_WARNING
3066 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3069 goto sjpegc_unlock_and_return;
3072 res = zoran_check_jpg_settings(zr, &settings, 0);
3074 goto sjpegc_unlock_and_return;
3075 if (!fh->buffers.allocated)
3076 fh->buffers.buffer_size =
3077 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3078 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3079 sjpegc_unlock_and_return:
3080 mutex_unlock(&zr->resource_lock);
3086 zoran_poll (struct file *file,
3089 struct zoran_fh *fh = file->private_data;
3090 struct zoran *zr = fh->zr;
3092 unsigned long flags;
3094 /* we should check whether buffers are ready to be synced on
3095 * (w/o waits - O_NONBLOCK) here
3096 * if ready for read (sync), return POLLIN|POLLRDNORM,
3097 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3098 * if error, return POLLERR,
3099 * if no buffers queued or so, return POLLNVAL
3102 mutex_lock(&zr->resource_lock);
3104 switch (fh->map_mode) {
3105 case ZORAN_MAP_MODE_RAW:
3106 poll_wait(file, &zr->v4l_capq, wait);
3107 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3109 spin_lock_irqsave(&zr->spinlock, flags);
3112 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3113 ZR_DEVNAME(zr), __func__,
3114 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
3115 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3116 zr->v4l_pend_tail, zr->v4l_pend_head);
3117 /* Process is the one capturing? */
3118 if (fh->buffers.active != ZORAN_FREE &&
3119 /* Buffer ready to DQBUF? */
3120 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3121 res = POLLIN | POLLRDNORM;
3122 spin_unlock_irqrestore(&zr->spinlock, flags);
3126 case ZORAN_MAP_MODE_JPG_REC:
3127 case ZORAN_MAP_MODE_JPG_PLAY:
3128 poll_wait(file, &zr->jpg_capq, wait);
3129 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3131 spin_lock_irqsave(&zr->spinlock, flags);
3134 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3135 ZR_DEVNAME(zr), __func__,
3136 "FAL"[fh->buffers.active], zr->jpg_que_tail,
3137 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3138 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3139 if (fh->buffers.active != ZORAN_FREE &&
3140 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3141 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3142 res = POLLIN | POLLRDNORM;
3144 res = POLLOUT | POLLWRNORM;
3146 spin_unlock_irqrestore(&zr->spinlock, flags);
3153 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3154 ZR_DEVNAME(zr), fh->map_mode);
3158 mutex_unlock(&zr->resource_lock);
3165 * This maps the buffers to user space.
3167 * Depending on the state of fh->map_mode
3168 * the V4L or the MJPEG buffers are mapped
3169 * per buffer or all together
3171 * Note that we need to connect to some
3172 * unmap signal event to unmap the de-allocate
3173 * the buffer accordingly (zoran_vm_close())
3177 zoran_vm_open (struct vm_area_struct *vma)
3179 struct zoran_mapping *map = vma->vm_private_data;
3185 zoran_vm_close (struct vm_area_struct *vma)
3187 struct zoran_mapping *map = vma->vm_private_data;
3188 struct file *file = map->file;
3189 struct zoran_fh *fh = file->private_data;
3190 struct zoran *zr = fh->zr;
3193 if (--map->count > 0)
3196 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3197 __func__, mode_name(fh->map_mode));
3199 for (i = 0; i < fh->buffers.num_buffers; i++) {
3200 if (fh->buffers.buffer[i].map == map)
3201 fh->buffers.buffer[i].map = NULL;
3205 /* Any buffers still mapped? */
3206 for (i = 0; i < fh->buffers.num_buffers; i++)
3207 if (fh->buffers.buffer[i].map)
3210 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3211 __func__, mode_name(fh->map_mode));
3213 mutex_lock(&zr->resource_lock);
3215 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3216 if (fh->buffers.active != ZORAN_FREE) {
3217 unsigned long flags;
3219 spin_lock_irqsave(&zr->spinlock, flags);
3220 zr36057_set_memgrab(zr, 0);
3221 zr->v4l_buffers.allocated = 0;
3222 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3223 spin_unlock_irqrestore(&zr->spinlock, flags);
3225 v4l_fbuffer_free(file);
3227 if (fh->buffers.active != ZORAN_FREE) {
3228 jpg_qbuf(file, -1, zr->codec_mode);
3229 zr->jpg_buffers.allocated = 0;
3230 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
3232 jpg_fbuffer_free(file);
3235 mutex_unlock(&zr->resource_lock);
3238 static struct vm_operations_struct zoran_vm_ops = {
3239 .open = zoran_vm_open,
3240 .close = zoran_vm_close,
3244 zoran_mmap (struct file *file,
3245 struct vm_area_struct *vma)
3247 struct zoran_fh *fh = file->private_data;
3248 struct zoran *zr = fh->zr;
3249 unsigned long size = (vma->vm_end - vma->vm_start);
3250 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3252 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3254 struct zoran_mapping *map;
3258 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3260 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
3262 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3263 !(vma->vm_flags & VM_WRITE)) {
3266 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3271 mutex_lock(&zr->resource_lock);
3273 if (!fh->buffers.allocated) {
3276 "%s: zoran_mmap(%s) - buffers not yet allocated\n",
3277 ZR_DEVNAME(zr), mode_name(fh->map_mode));
3279 goto mmap_unlock_and_return;
3282 first = offset / fh->buffers.buffer_size;
3283 last = first - 1 + size / fh->buffers.buffer_size;
3284 if (offset % fh->buffers.buffer_size != 0 ||
3285 size % fh->buffers.buffer_size != 0 || first < 0 ||
3286 last < 0 || first >= fh->buffers.num_buffers ||
3287 last >= fh->buffers.buffer_size) {
3290 "%s: mmap(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3291 ZR_DEVNAME(zr), mode_name(fh->map_mode), offset, size,
3292 fh->buffers.buffer_size,
3293 fh->buffers.num_buffers);
3295 goto mmap_unlock_and_return;
3298 /* Check if any buffers are already mapped */
3299 for (i = first; i <= last; i++) {
3300 if (fh->buffers.buffer[i].map) {
3303 "%s: mmap(%s) - buffer %d already mapped\n",
3304 ZR_DEVNAME(zr), mode_name(fh->map_mode), i);
3306 goto mmap_unlock_and_return;
3310 /* map these buffers */
3311 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3314 goto mmap_unlock_and_return;
3319 vma->vm_ops = &zoran_vm_ops;
3320 vma->vm_flags |= VM_DONTEXPAND;
3321 vma->vm_private_data = map;
3323 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3324 for (i = first; i <= last; i++) {
3326 if (todo > fh->buffers.buffer_size)
3327 todo = fh->buffers.buffer_size;
3328 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3329 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3330 todo, PAGE_SHARED)) {
3333 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3336 goto mmap_unlock_and_return;
3340 fh->buffers.buffer[i].map = map;
3345 for (i = first; i <= last; i++) {
3347 j < fh->buffers.buffer_size / PAGE_SIZE;
3350 (le32_to_cpu(fh->buffers.buffer[i].jpg.
3351 frag_tab[2 * j + 1]) & ~1) << 1;
3356 le32_to_cpu(fh->buffers.
3357 buffer[i].jpg.frag_tab[2 * j]);
3358 /* should just be pos on i386 */
3359 page = virt_to_phys(bus_to_virt(pos))
3361 if (remap_pfn_range(vma, start, page,
3362 todo, PAGE_SHARED)) {
3365 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3368 goto mmap_unlock_and_return;
3374 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
3375 frag_tab[2 * j + 1]) & 1)
3376 break; /* was last fragment */
3378 fh->buffers.buffer[i].map = map;
3385 mmap_unlock_and_return:
3386 mutex_unlock(&zr->resource_lock);
3391 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3392 .vidioc_querycap = zoran_querycap,
3393 .vidioc_cropcap = zoran_cropcap,
3394 .vidioc_s_crop = zoran_s_crop,
3395 .vidioc_g_crop = zoran_g_crop,
3396 .vidioc_enum_input = zoran_enum_input,
3397 .vidioc_g_input = zoran_g_input,
3398 .vidioc_s_input = zoran_s_input,
3399 .vidioc_enum_output = zoran_enum_output,
3400 .vidioc_g_output = zoran_g_output,
3401 .vidioc_s_output = zoran_s_output,
3402 .vidioc_g_fbuf = zoran_g_fbuf,
3403 .vidioc_s_fbuf = zoran_s_fbuf,
3404 .vidioc_g_std = zoran_g_std,
3405 .vidioc_s_std = zoran_s_std,
3406 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3407 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3408 .vidioc_overlay = zoran_overlay,
3409 .vidioc_reqbufs = zoran_reqbufs,
3410 .vidioc_querybuf = zoran_querybuf,
3411 .vidioc_qbuf = zoran_qbuf,
3412 .vidioc_dqbuf = zoran_dqbuf,
3413 .vidioc_streamon = zoran_streamon,
3414 .vidioc_streamoff = zoran_streamoff,
3415 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3416 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3417 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3418 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3419 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3420 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3421 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3422 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3423 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3424 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3425 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3426 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3427 .vidioc_queryctrl = zoran_queryctrl,
3428 .vidioc_s_ctrl = zoran_s_ctrl,
3429 .vidioc_g_ctrl = zoran_g_ctrl,
3430 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3431 .vidioc_default = zoran_default,
3432 .vidiocgmbuf = zoran_vidiocgmbuf,
3436 static const struct v4l2_file_operations zoran_fops = {
3437 .owner = THIS_MODULE,
3439 .release = zoran_close,
3440 .ioctl = video_ioctl2,
3442 .write = zoran_write,
3447 struct video_device zoran_template __devinitdata = {
3449 .fops = &zoran_fops,
3450 .ioctl_ops = &zoran_ioctl_ops,
3451 .release = &zoran_vdev_release,
3452 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,