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 zoran_fh *fh);
190 static void jpg_fbuffer_free(struct zoran_fh *fh);
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.
216 static int v4l_fbuffer_alloc(struct zoran_fh *fh)
218 struct zoran *zr = fh->zr;
222 for (i = 0; i < fh->buffers.num_buffers; i++) {
223 if (fh->buffers.buffer[i].v4l.fbuffer)
226 "%s: %s - buffer %d already allocated!?\n",
227 ZR_DEVNAME(zr), __func__, i);
230 mem = kmalloc(fh->buffers.buffer_size,
231 GFP_KERNEL | __GFP_NOWARN);
235 "%s: %s - kmalloc for V4L buf %d failed\n",
236 ZR_DEVNAME(zr), __func__, i);
237 v4l_fbuffer_free(fh);
240 fh->buffers.buffer[i].v4l.fbuffer = mem;
241 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
242 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
243 for (off = 0; off < fh->buffers.buffer_size;
245 SetPageReserved(virt_to_page(mem + off));
248 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
249 ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
250 (unsigned long long)virt_to_bus(mem));
253 fh->buffers.allocated = 1;
258 /* free the V4L grab buffers */
259 static void v4l_fbuffer_free(struct zoran_fh *fh)
261 struct zoran *zr = fh->zr;
265 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
267 for (i = 0; i < fh->buffers.num_buffers; i++) {
268 if (!fh->buffers.buffer[i].v4l.fbuffer)
271 mem = fh->buffers.buffer[i].v4l.fbuffer;
272 for (off = 0; off < fh->buffers.buffer_size;
274 ClearPageReserved(virt_to_page(mem + off));
275 kfree(fh->buffers.buffer[i].v4l.fbuffer);
276 fh->buffers.buffer[i].v4l.fbuffer = NULL;
279 fh->buffers.allocated = 0;
283 * Allocate the MJPEG grab buffers.
285 * If a Natoma chipset is present and this is a revision 1 zr36057,
286 * each MJPEG buffer needs to be physically contiguous.
287 * (RJ: This statement is from Dave Perks' original driver,
288 * I could never check it because I have a zr36067)
290 * RJ: The contents grab buffers needs never be accessed in the driver.
291 * Therefore there is no need to allocate them with vmalloc in order
292 * to get a contiguous virtual memory space.
293 * I don't understand why many other drivers first allocate them with
294 * vmalloc (which uses internally also get_zeroed_page, but delivers you
295 * virtual addresses) and then again have to make a lot of efforts
296 * to get the physical address.
299 * On big-endian architectures (such as ppc) some extra steps
300 * are needed. When reading and writing to the stat_com array
301 * and fragment buffers, the device expects to see little-
302 * endian values. The use of cpu_to_le32() and le32_to_cpu()
303 * in this function (and one or two others in zoran_device.c)
304 * ensure that these values are always stored in little-endian
305 * form, regardless of architecture. The zr36057 does Very Bad
306 * Things on big endian architectures if the stat_com array
307 * and fragment buffers are not little-endian.
310 static int jpg_fbuffer_alloc(struct zoran_fh *fh)
312 struct zoran *zr = fh->zr;
316 for (i = 0; i < fh->buffers.num_buffers; i++) {
317 if (fh->buffers.buffer[i].jpg.frag_tab)
320 "%s: %s - buffer %d already allocated!?\n",
321 ZR_DEVNAME(zr), __func__, i);
323 /* Allocate fragment table for this buffer */
325 mem = (void *)get_zeroed_page(GFP_KERNEL);
329 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
330 ZR_DEVNAME(zr), __func__, i);
331 jpg_fbuffer_free(fh);
334 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
335 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
337 if (fh->buffers.need_contiguous) {
338 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
342 "%s: %s - kmalloc failed for buffer %d\n",
343 ZR_DEVNAME(zr), __func__, i);
344 jpg_fbuffer_free(fh);
347 fh->buffers.buffer[i].jpg.frag_tab[0] =
348 cpu_to_le32(virt_to_bus(mem));
349 fh->buffers.buffer[i].jpg.frag_tab[1] =
350 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
351 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
352 SetPageReserved(virt_to_page(mem + off));
354 /* jpg_bufsize is already page aligned */
355 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
356 mem = (void *)get_zeroed_page(GFP_KERNEL);
360 "%s: %s - get_zeroed_page failed for buffer %d\n",
361 ZR_DEVNAME(zr), __func__, i);
362 jpg_fbuffer_free(fh);
366 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
367 cpu_to_le32(virt_to_bus(mem));
368 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
369 cpu_to_le32((PAGE_SIZE >> 2) << 1);
370 SetPageReserved(virt_to_page(mem));
373 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
378 KERN_DEBUG "%s: %s - %d KB allocated\n",
379 ZR_DEVNAME(zr), __func__,
380 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
382 fh->buffers.allocated = 1;
387 /* free the MJPEG grab buffers */
388 static void jpg_fbuffer_free(struct zoran_fh *fh)
390 struct zoran *zr = fh->zr;
394 struct zoran_buffer *buffer;
396 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
398 for (i = 0, buffer = &fh->buffers.buffer[0];
399 i < fh->buffers.num_buffers; i++, buffer++) {
400 if (!buffer->jpg.frag_tab)
403 if (fh->buffers.need_contiguous) {
404 frag_tab = buffer->jpg.frag_tab[0];
407 mem = bus_to_virt(le32_to_cpu(frag_tab));
408 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
409 ClearPageReserved(virt_to_page(mem + off));
411 buffer->jpg.frag_tab[0] = 0;
412 buffer->jpg.frag_tab[1] = 0;
415 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
416 frag_tab = buffer->jpg.frag_tab[2 * j];
420 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
421 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
422 buffer->jpg.frag_tab[2 * j] = 0;
423 buffer->jpg.frag_tab[2 * j + 1] = 0;
427 free_page((unsigned long)buffer->jpg.frag_tab);
428 buffer->jpg.frag_tab = NULL;
431 fh->buffers.allocated = 0;
435 * V4L Buffer grabbing
439 zoran_v4l_set_format (struct zoran_fh *fh,
442 const struct zoran_format *format)
444 struct zoran *zr = fh->zr;
447 /* Check size and format of the grab wanted */
449 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
450 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
453 "%s: %s - wrong frame size (%dx%d)\n",
454 ZR_DEVNAME(zr), __func__, width, height);
458 bpp = (format->depth + 7) / 8;
460 /* Check against available buffer size */
461 if (height * width * bpp > fh->buffers.buffer_size) {
464 "%s: %s - video buffer size (%d kB) is too small\n",
465 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
469 /* The video front end needs 4-byte alinged line sizes */
471 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
474 "%s: %s - wrong frame alignment\n",
475 ZR_DEVNAME(zr), __func__);
479 fh->v4l_settings.width = width;
480 fh->v4l_settings.height = height;
481 fh->v4l_settings.format = format;
482 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
487 static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
489 struct zoran *zr = fh->zr;
493 if (!fh->buffers.allocated) {
496 "%s: %s - buffers not yet allocated\n",
497 ZR_DEVNAME(zr), __func__);
501 /* No grabbing outside the buffer range! */
502 if (num >= fh->buffers.num_buffers || num < 0) {
505 "%s: %s - buffer %d is out of range\n",
506 ZR_DEVNAME(zr), __func__, num);
510 spin_lock_irqsave(&zr->spinlock, flags);
512 if (fh->buffers.active == ZORAN_FREE) {
513 if (zr->v4l_buffers.active == ZORAN_FREE) {
514 zr->v4l_buffers = fh->buffers;
515 fh->buffers.active = ZORAN_ACTIVE;
519 "%s: %s - another session is already capturing\n",
520 ZR_DEVNAME(zr), __func__);
525 /* make sure a grab isn't going on currently with this buffer */
527 switch (zr->v4l_buffers.buffer[num].state) {
530 if (zr->v4l_buffers.active == ZORAN_FREE) {
531 fh->buffers.active = ZORAN_FREE;
532 zr->v4l_buffers.allocated = 0;
534 res = -EBUSY; /* what are you doing? */
539 "%s: %s - queueing buffer %d in state DONE!?\n",
540 ZR_DEVNAME(zr), __func__, num);
542 /* since there is at least one unused buffer there's room for at least
543 * one more pend[] entry */
544 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
545 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
546 zr->v4l_buffers.buffer[num].bs.length =
547 fh->v4l_settings.bytesperline *
548 zr->v4l_settings.height;
549 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
554 spin_unlock_irqrestore(&zr->spinlock, flags);
556 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
557 zr->v4l_buffers.active = fh->buffers.active;
563 * Sync on a V4L buffer
566 static int v4l_sync(struct zoran_fh *fh, int frame)
568 struct zoran *zr = fh->zr;
571 if (fh->buffers.active == ZORAN_FREE) {
574 "%s: %s - no grab active for this session\n",
575 ZR_DEVNAME(zr), __func__);
579 /* check passed-in frame number */
580 if (frame >= fh->buffers.num_buffers || frame < 0) {
582 KERN_ERR "%s: %s - frame %d is invalid\n",
583 ZR_DEVNAME(zr), __func__, frame);
587 /* Check if is buffer was queued at all */
588 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
591 "%s: %s - attempt to sync on a buffer which was not queued?\n",
592 ZR_DEVNAME(zr), __func__);
596 /* wait on this buffer to get ready */
597 if (!wait_event_interruptible_timeout(zr->v4l_capq,
598 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
600 if (signal_pending(current))
603 /* buffer should now be in BUZ_STATE_DONE */
604 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
606 KERN_ERR "%s: %s - internal state error\n",
607 ZR_DEVNAME(zr), __func__);
609 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
610 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
612 spin_lock_irqsave(&zr->spinlock, flags);
614 /* Check if streaming capture has finished */
615 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
616 zr36057_set_memgrab(zr, 0);
617 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
618 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
619 zr->v4l_buffers.allocated = 0;
623 spin_unlock_irqrestore(&zr->spinlock, flags);
629 * Queue a MJPEG buffer for capture/playback
632 static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
633 enum zoran_codec_mode mode)
635 struct zoran *zr = fh->zr;
639 /* Check if buffers are allocated */
640 if (!fh->buffers.allocated) {
643 "%s: %s - buffers not yet allocated\n",
644 ZR_DEVNAME(zr), __func__);
648 /* No grabbing outside the buffer range! */
649 if (num >= fh->buffers.num_buffers || num < 0) {
652 "%s: %s - buffer %d out of range\n",
653 ZR_DEVNAME(zr), __func__, num);
657 /* what is the codec mode right now? */
658 if (zr->codec_mode == BUZ_MODE_IDLE) {
659 zr->jpg_settings = fh->jpg_settings;
660 } else if (zr->codec_mode != mode) {
661 /* wrong codec mode active - invalid */
664 "%s: %s - codec in wrong mode\n",
665 ZR_DEVNAME(zr), __func__);
669 if (fh->buffers.active == ZORAN_FREE) {
670 if (zr->jpg_buffers.active == ZORAN_FREE) {
671 zr->jpg_buffers = fh->buffers;
672 fh->buffers.active = ZORAN_ACTIVE;
676 "%s: %s - another session is already capturing\n",
677 ZR_DEVNAME(zr), __func__);
682 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
683 /* Ok load up the jpeg codec */
684 zr36057_enable_jpg(zr, mode);
687 spin_lock_irqsave(&zr->spinlock, flags);
690 switch (zr->jpg_buffers.buffer[num].state) {
694 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
695 ZR_DEVNAME(zr), __func__);
697 /* since there is at least one unused buffer there's room for at
698 *least one more pend[] entry */
699 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
700 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
701 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
702 zoran_feed_stat_com(zr);
707 if (zr->jpg_buffers.active == ZORAN_FREE) {
708 fh->buffers.active = ZORAN_FREE;
709 zr->jpg_buffers.allocated = 0;
711 res = -EBUSY; /* what are you doing? */
716 spin_unlock_irqrestore(&zr->spinlock, flags);
718 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
719 zr->jpg_buffers.active = fh->buffers.active;
724 static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
726 struct zoran *zr = fh->zr;
729 /* Does the user want to stop streaming? */
731 if (zr->codec_mode == mode) {
732 if (fh->buffers.active == ZORAN_FREE) {
735 "%s: %s(-1) - session not active\n",
736 ZR_DEVNAME(zr), __func__);
739 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
740 zr->jpg_buffers.allocated = 0;
741 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
746 "%s: %s - stop streaming but not in streaming mode\n",
747 ZR_DEVNAME(zr), __func__);
752 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
755 /* Start the jpeg codec when the first frame is queued */
756 if (!res && zr->jpg_que_head == 1)
763 * Sync on a MJPEG buffer
766 static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
768 struct zoran *zr = fh->zr;
772 if (fh->buffers.active == ZORAN_FREE) {
775 "%s: %s - capture is not currently active\n",
776 ZR_DEVNAME(zr), __func__);
779 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
780 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
783 "%s: %s - codec not in streaming mode\n",
784 ZR_DEVNAME(zr), __func__);
787 if (!wait_event_interruptible_timeout(zr->jpg_capq,
788 (zr->jpg_que_tail != zr->jpg_dma_tail ||
789 zr->jpg_dma_tail == zr->jpg_dma_head),
793 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
795 zr->codec->control(zr->codec, CODEC_G_STATUS,
799 "%s: %s - timeout: codec isr=0x%02x\n",
800 ZR_DEVNAME(zr), __func__, isr);
805 if (signal_pending(current))
808 spin_lock_irqsave(&zr->spinlock, flags);
810 if (zr->jpg_dma_tail != zr->jpg_dma_head)
811 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
813 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
815 /* buffer should now be in BUZ_STATE_DONE */
816 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
818 KERN_ERR "%s: %s - internal state error\n",
819 ZR_DEVNAME(zr), __func__);
821 *bs = zr->jpg_buffers.buffer[frame].bs;
823 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
824 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
826 spin_unlock_irqrestore(&zr->spinlock, flags);
831 static void zoran_open_init_session(struct zoran_fh *fh)
834 struct zoran *zr = fh->zr;
836 /* Per default, map the V4L Buffers */
839 /* take over the card's current settings */
840 fh->overlay_settings = zr->overlay_settings;
841 fh->overlay_settings.is_set = 0;
842 fh->overlay_settings.format = zr->overlay_settings.format;
843 fh->overlay_active = ZORAN_FREE;
846 fh->v4l_settings = zr->v4l_settings;
848 fh->jpg_settings = zr->jpg_settings;
851 memset(&fh->buffers, 0, sizeof(fh->buffers));
852 for (i = 0; i < MAX_FRAME; i++) {
853 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
854 fh->buffers.buffer[i].bs.frame = i;
856 fh->buffers.allocated = 0;
857 fh->buffers.active = ZORAN_FREE;
860 static void zoran_close_end_session(struct zoran_fh *fh)
862 struct zoran *zr = fh->zr;
865 if (fh->overlay_active != ZORAN_FREE) {
866 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
867 zr->v4l_overlay_active = 0;
868 if (!zr->v4l_memgrab_active)
869 zr36057_overlay(zr, 0);
870 zr->overlay_mask = NULL;
873 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
875 if (fh->buffers.active != ZORAN_FREE) {
878 spin_lock_irqsave(&zr->spinlock, flags);
879 zr36057_set_memgrab(zr, 0);
880 zr->v4l_buffers.allocated = 0;
881 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
882 spin_unlock_irqrestore(&zr->spinlock, flags);
886 if (fh->buffers.allocated)
887 v4l_fbuffer_free(fh);
890 if (fh->buffers.active != ZORAN_FREE) {
891 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
892 zr->jpg_buffers.allocated = 0;
893 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
897 if (fh->buffers.allocated)
898 jpg_fbuffer_free(fh);
903 * Open a zoran card. Right now the flags stuff is just playing
906 static int zoran_open(struct file *file)
908 struct zoran *zr = video_drvdata(file);
910 int res, first_open = 0;
912 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
913 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
917 if (zr->user >= 2048) {
918 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
919 ZR_DEVNAME(zr), zr->user);
924 /* now, create the open()-specific file_ops struct */
925 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
929 "%s: %s - allocation of zoran_fh failed\n",
930 ZR_DEVNAME(zr), __func__);
934 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
937 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
938 if (!fh->overlay_mask) {
941 "%s: %s - allocation of overlay_mask failed\n",
942 ZR_DEVNAME(zr), __func__);
950 /*mutex_unlock(&zr->resource_lock);*/
952 /* default setup - TODO: look at flags */
953 if (first_open) { /* First device open */
955 zoran_open_init_params(zr);
956 zoran_init_hardware(zr);
958 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
961 /* set file_ops stuff */
962 file->private_data = fh;
964 zoran_open_init_session(fh);
974 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
975 ZR_DEVNAME(zr), res, zr->user);
981 zoran_close(struct file *file)
983 struct zoran_fh *fh = file->private_data;
984 struct zoran *zr = fh->zr;
986 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
987 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
989 /* kernel locks (fs/device.c), so don't do that ourselves
990 * (prevents deadlocks) */
991 /*mutex_lock(&zr->resource_lock);*/
993 zoran_close_end_session(fh);
995 if (zr->user-- == 1) { /* Last process */
996 /* Clean up JPEG process */
997 wake_up_interruptible(&zr->jpg_capq);
998 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
999 zr->jpg_buffers.allocated = 0;
1000 zr->jpg_buffers.active = ZORAN_FREE;
1002 /* disable interrupts */
1003 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1005 if (zr36067_debug > 1)
1006 print_interrupts(zr);
1009 zr->v4l_overlay_active = 0;
1010 zr36057_overlay(zr, 0);
1011 zr->overlay_mask = NULL;
1014 wake_up_interruptible(&zr->v4l_capq);
1015 zr36057_set_memgrab(zr, 0);
1016 zr->v4l_buffers.allocated = 0;
1017 zr->v4l_buffers.active = ZORAN_FREE;
1018 zoran_set_pci_master(zr, 0);
1020 if (!pass_through) { /* Switch to color bar */
1021 decoder_call(zr, video, s_stream, 0);
1022 encoder_call(zr, video, s_routing, 2, 0, 0);
1026 file->private_data = NULL;
1027 kfree(fh->overlay_mask);
1030 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
1037 zoran_read (struct file *file,
1042 /* we simply don't support read() (yet)... */
1048 zoran_write (struct file *file,
1049 const char __user *data,
1053 /* ...and the same goes for write() */
1058 static int setup_fbuffer(struct zoran_fh *fh,
1060 const struct zoran_format *fmt,
1065 struct zoran *zr = fh->zr;
1067 /* (Ronald) v4l/v4l2 guidelines */
1068 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1071 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1072 ALi Magik (that needs very low latency while the card needs a
1073 higher value always) */
1075 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1078 /* we need a bytesperline value, even if not given */
1080 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1083 if (zr->overlay_active) {
1084 /* dzjee... stupid users... don't even bother to turn off
1085 * overlay before changing the memory location...
1086 * normally, we would return errors here. However, one of
1087 * the tools that does this is... xawtv! and since xawtv
1088 * is used by +/- 99% of the users, we'd rather be user-
1089 * friendly and silently do as if nothing went wrong */
1092 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1093 ZR_DEVNAME(zr), __func__);
1094 zr36057_overlay(zr, 0);
1098 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1101 "%s: %s - no valid overlay format given\n",
1102 ZR_DEVNAME(zr), __func__);
1105 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1108 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1109 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
1112 if (bytesperline & 3) {
1115 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1116 ZR_DEVNAME(zr), __func__, bytesperline);
1120 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1121 zr->vbuf_height = height;
1122 zr->vbuf_width = width;
1123 zr->vbuf_depth = fmt->depth;
1124 zr->overlay_settings.format = fmt;
1125 zr->vbuf_bytesperline = bytesperline;
1127 /* The user should set new window parameters */
1128 zr->overlay_settings.is_set = 0;
1134 static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
1135 struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
1137 struct zoran *zr = fh->zr;
1138 struct v4l2_clip *vcp = NULL;
1142 if (!zr->vbuf_base) {
1145 "%s: %s - frame buffer has to be set first\n",
1146 ZR_DEVNAME(zr), __func__);
1150 if (!fh->overlay_settings.format) {
1153 "%s: %s - no overlay format set\n",
1154 ZR_DEVNAME(zr), __func__);
1159 * The video front end needs 4-byte alinged line sizes, we correct that
1160 * silently here if necessary
1162 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1163 end = (x + width) & ~1; /* round down */
1164 x = (x + 1) & ~1; /* round up */
1168 if (zr->vbuf_depth == 24) {
1169 end = (x + width) & ~3; /* round down */
1170 x = (x + 3) & ~3; /* round up */
1174 if (width > BUZ_MAX_WIDTH)
1175 width = BUZ_MAX_WIDTH;
1176 if (height > BUZ_MAX_HEIGHT)
1177 height = BUZ_MAX_HEIGHT;
1179 /* Check for vaild parameters */
1180 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1181 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1184 "%s: %s - width = %d or height = %d invalid\n",
1185 ZR_DEVNAME(zr), __func__, width, height);
1189 fh->overlay_settings.x = x;
1190 fh->overlay_settings.y = y;
1191 fh->overlay_settings.width = width;
1192 fh->overlay_settings.height = height;
1193 fh->overlay_settings.clipcount = clipcount;
1196 * If an overlay is running, we have to switch it off
1197 * and switch it on again in order to get the new settings in effect.
1199 * We also want to avoid that the overlay mask is written
1200 * when an overlay is running.
1203 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1204 zr->overlay_active != ZORAN_FREE &&
1205 fh->overlay_active != ZORAN_FREE;
1207 zr36057_overlay(zr, 0);
1210 * Write the overlay mask if clips are wanted.
1211 * We prefer a bitmap.
1214 /* fake value - it just means we want clips */
1215 fh->overlay_settings.clipcount = 1;
1217 if (copy_from_user(fh->overlay_mask, bitmap,
1218 (width * height + 7) / 8)) {
1221 } else if (clipcount > 0) {
1222 /* write our own bitmap from the clips */
1223 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1227 "%s: %s - Alloc of clip mask failed\n",
1228 ZR_DEVNAME(zr), __func__);
1232 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1236 write_overlay_mask(fh, vcp, clipcount);
1240 fh->overlay_settings.is_set = 1;
1241 if (fh->overlay_active != ZORAN_FREE &&
1242 zr->overlay_active != ZORAN_FREE)
1243 zr->overlay_settings = fh->overlay_settings;
1246 zr36057_overlay(zr, 1);
1248 /* Make sure the changes come into effect */
1249 return wait_grab_pending(zr);
1252 static int setup_overlay(struct zoran_fh *fh, int on)
1254 struct zoran *zr = fh->zr;
1256 /* If there is nothing to do, return immediatly */
1257 if ((on && fh->overlay_active != ZORAN_FREE) ||
1258 (!on && fh->overlay_active == ZORAN_FREE))
1261 /* check whether we're touching someone else's overlay */
1262 if (on && zr->overlay_active != ZORAN_FREE &&
1263 fh->overlay_active == ZORAN_FREE) {
1266 "%s: %s - overlay is already active for another session\n",
1267 ZR_DEVNAME(zr), __func__);
1270 if (!on && zr->overlay_active != ZORAN_FREE &&
1271 fh->overlay_active == ZORAN_FREE) {
1274 "%s: %s - you cannot cancel someone else's session\n",
1275 ZR_DEVNAME(zr), __func__);
1280 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1281 zr->v4l_overlay_active = 0;
1282 /* When a grab is running, the video simply
1283 * won't be switched on any more */
1284 if (!zr->v4l_memgrab_active)
1285 zr36057_overlay(zr, 0);
1286 zr->overlay_mask = NULL;
1288 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1291 "%s: %s - buffer or window not set\n",
1292 ZR_DEVNAME(zr), __func__);
1295 if (!fh->overlay_settings.format) {
1298 "%s: %s - no overlay format set\n",
1299 ZR_DEVNAME(zr), __func__);
1302 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1303 zr->v4l_overlay_active = 1;
1304 zr->overlay_mask = fh->overlay_mask;
1305 zr->overlay_settings = fh->overlay_settings;
1306 if (!zr->v4l_memgrab_active)
1307 zr36057_overlay(zr, 1);
1308 /* When a grab is running, the video will be
1309 * switched on when grab is finished */
1312 /* Make sure the changes come into effect */
1313 return wait_grab_pending(zr);
1316 /* get the status of a buffer in the clients buffer queue */
1317 static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1318 struct v4l2_buffer *buf, int num)
1320 struct zoran *zr = fh->zr;
1321 unsigned long flags;
1323 buf->flags = V4L2_BUF_FLAG_MAPPED;
1325 switch (fh->map_mode) {
1326 case ZORAN_MAP_MODE_RAW:
1328 if (num < 0 || num >= fh->buffers.num_buffers ||
1329 !fh->buffers.allocated) {
1332 "%s: %s - wrong number or buffers not allocated\n",
1333 ZR_DEVNAME(zr), __func__);
1337 spin_lock_irqsave(&zr->spinlock, flags);
1340 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1341 ZR_DEVNAME(zr), __func__,
1342 "FAL"[fh->buffers.active], num,
1343 "UPMD"[zr->v4l_buffers.buffer[num].state],
1344 fh->buffers.buffer[num].map ? 'Y' : 'N');
1345 spin_unlock_irqrestore(&zr->spinlock, flags);
1347 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1348 buf->length = fh->buffers.buffer_size;
1351 buf->bytesused = fh->buffers.buffer[num].bs.length;
1352 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1353 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1354 buf->sequence = fh->buffers.buffer[num].bs.seq;
1355 buf->flags |= V4L2_BUF_FLAG_DONE;
1356 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1358 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1361 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1362 buf->field = V4L2_FIELD_TOP;
1364 buf->field = V4L2_FIELD_INTERLACED;
1368 case ZORAN_MAP_MODE_JPG_REC:
1369 case ZORAN_MAP_MODE_JPG_PLAY:
1372 if (num < 0 || num >= fh->buffers.num_buffers ||
1373 !fh->buffers.allocated) {
1376 "%s: %s - wrong number or buffers not allocated\n",
1377 ZR_DEVNAME(zr), __func__);
1381 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1382 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1383 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1384 buf->length = fh->buffers.buffer_size;
1386 /* these variables are only written after frame has been captured */
1387 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1388 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1389 buf->sequence = fh->buffers.buffer[num].bs.seq;
1390 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1391 buf->bytesused = fh->buffers.buffer[num].bs.length;
1392 buf->flags |= V4L2_BUF_FLAG_DONE;
1394 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1397 /* which fields are these? */
1398 if (fh->jpg_settings.TmpDcm != 1)
1399 buf->field = fh->jpg_settings.odd_even ?
1400 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1402 buf->field = fh->jpg_settings.odd_even ?
1403 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1411 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1412 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
1416 buf->memory = V4L2_MEMORY_MMAP;
1418 buf->m.offset = buf->length * num;
1424 zoran_set_norm (struct zoran *zr,
1429 if (zr->v4l_buffers.active != ZORAN_FREE ||
1430 zr->jpg_buffers.active != ZORAN_FREE) {
1433 "%s: %s called while in playback/capture mode\n",
1434 ZR_DEVNAME(zr), __func__);
1438 if (!(norm & zr->card.norms)) {
1440 KERN_ERR "%s: %s - unsupported norm %llx\n",
1441 ZR_DEVNAME(zr), __func__, norm);
1445 if (norm == V4L2_STD_ALL) {
1447 v4l2_std_id std = 0;
1449 decoder_call(zr, video, querystd, &std);
1450 decoder_call(zr, core, s_std, std);
1452 /* let changes come into effect */
1455 decoder_call(zr, video, g_input_status, &status);
1456 if (status & V4L2_IN_ST_NO_SIGNAL) {
1459 "%s: %s - no norm detected\n",
1460 ZR_DEVNAME(zr), __func__);
1462 decoder_call(zr, core, s_std, zr->norm);
1468 if (norm & V4L2_STD_SECAM)
1469 zr->timing = zr->card.tvn[2];
1470 else if (norm & V4L2_STD_NTSC)
1471 zr->timing = zr->card.tvn[1];
1473 zr->timing = zr->card.tvn[0];
1475 /* We switch overlay off and on since a change in the
1476 * norm needs different VFE settings */
1477 on = zr->overlay_active && !zr->v4l_memgrab_active;
1479 zr36057_overlay(zr, 0);
1481 decoder_call(zr, core, s_std, norm);
1482 encoder_call(zr, video, s_std_output, norm);
1485 zr36057_overlay(zr, 1);
1487 /* Make sure the changes come into effect */
1494 zoran_set_input (struct zoran *zr,
1497 if (input == zr->input) {
1501 if (zr->v4l_buffers.active != ZORAN_FREE ||
1502 zr->jpg_buffers.active != ZORAN_FREE) {
1505 "%s: %s called while in playback/capture mode\n",
1506 ZR_DEVNAME(zr), __func__);
1510 if (input < 0 || input >= zr->card.inputs) {
1513 "%s: %s - unnsupported input %d\n",
1514 ZR_DEVNAME(zr), __func__, input);
1520 decoder_call(zr, video, s_routing,
1521 zr->card.input[input].muxsel, 0, 0);
1530 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1531 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1533 struct zoran_fh *fh = __fh;
1534 struct zoran *zr = fh->zr;
1535 struct zoran_jpg_settings settings;
1538 case BUZIOC_G_PARAMS:
1540 struct zoran_params *bparams = arg;
1542 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1544 memset(bparams, 0, sizeof(struct zoran_params));
1545 bparams->major_version = MAJOR_VERSION;
1546 bparams->minor_version = MINOR_VERSION;
1548 mutex_lock(&zr->resource_lock);
1550 if (zr->norm & V4L2_STD_NTSC)
1551 bparams->norm = VIDEO_MODE_NTSC;
1552 else if (zr->norm & V4L2_STD_PAL)
1553 bparams->norm = VIDEO_MODE_PAL;
1555 bparams->norm = VIDEO_MODE_SECAM;
1557 bparams->input = zr->input;
1559 bparams->decimation = fh->jpg_settings.decimation;
1560 bparams->HorDcm = fh->jpg_settings.HorDcm;
1561 bparams->VerDcm = fh->jpg_settings.VerDcm;
1562 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1563 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1564 bparams->img_x = fh->jpg_settings.img_x;
1565 bparams->img_y = fh->jpg_settings.img_y;
1566 bparams->img_width = fh->jpg_settings.img_width;
1567 bparams->img_height = fh->jpg_settings.img_height;
1568 bparams->odd_even = fh->jpg_settings.odd_even;
1570 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1571 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1572 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1573 memcpy(bparams->APP_data,
1574 fh->jpg_settings.jpg_comp.APP_data,
1575 sizeof(bparams->APP_data));
1576 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1577 memcpy(bparams->COM_data,
1578 fh->jpg_settings.jpg_comp.COM_data,
1579 sizeof(bparams->COM_data));
1580 bparams->jpeg_markers =
1581 fh->jpg_settings.jpg_comp.jpeg_markers;
1583 mutex_unlock(&zr->resource_lock);
1585 bparams->VFIFO_FB = 0;
1590 case BUZIOC_S_PARAMS:
1592 struct zoran_params *bparams = arg;
1595 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1597 settings.decimation = bparams->decimation;
1598 settings.HorDcm = bparams->HorDcm;
1599 settings.VerDcm = bparams->VerDcm;
1600 settings.TmpDcm = bparams->TmpDcm;
1601 settings.field_per_buff = bparams->field_per_buff;
1602 settings.img_x = bparams->img_x;
1603 settings.img_y = bparams->img_y;
1604 settings.img_width = bparams->img_width;
1605 settings.img_height = bparams->img_height;
1606 settings.odd_even = bparams->odd_even;
1608 settings.jpg_comp.quality = bparams->quality;
1609 settings.jpg_comp.APPn = bparams->APPn;
1610 settings.jpg_comp.APP_len = bparams->APP_len;
1611 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1612 sizeof(bparams->APP_data));
1613 settings.jpg_comp.COM_len = bparams->COM_len;
1614 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1615 sizeof(bparams->COM_data));
1616 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1618 mutex_lock(&zr->resource_lock);
1620 if (zr->codec_mode != BUZ_MODE_IDLE) {
1623 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1626 goto sparams_unlock_and_return;
1629 /* Check the params first before overwriting our
1631 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1633 goto sparams_unlock_and_return;
1636 fh->jpg_settings = settings;
1637 sparams_unlock_and_return:
1638 mutex_unlock(&zr->resource_lock);
1643 case BUZIOC_REQBUFS:
1645 struct zoran_requestbuffers *breq = arg;
1650 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1651 ZR_DEVNAME(zr), breq->count, breq->size);
1653 /* Enforce reasonable lower and upper limits */
1654 if (breq->count < 4)
1655 breq->count = 4; /* Could be choosen smaller */
1656 if (breq->count > jpg_nbufs)
1657 breq->count = jpg_nbufs;
1658 breq->size = PAGE_ALIGN(breq->size);
1659 if (breq->size < 8192)
1660 breq->size = 8192; /* Arbitrary */
1661 /* breq->size is limited by 1 page for the stat_com
1662 * tables to a Maximum of 2 MB */
1663 if (breq->size > jpg_bufsize)
1664 breq->size = jpg_bufsize;
1666 mutex_lock(&zr->resource_lock);
1668 if (fh->buffers.allocated) {
1671 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1674 goto jpgreqbuf_unlock_and_return;
1677 /* The next mmap will map the MJPEG buffers - could
1678 * also be *_PLAY, but it doesn't matter here */
1679 map_mode_jpg(fh, 0);
1680 fh->buffers.num_buffers = breq->count;
1681 fh->buffers.buffer_size = breq->size;
1683 if (jpg_fbuffer_alloc(fh)) {
1685 goto jpgreqbuf_unlock_and_return;
1688 jpgreqbuf_unlock_and_return:
1689 mutex_unlock(&zr->resource_lock);
1694 case BUZIOC_QBUF_CAPT:
1696 int *frame = arg, res;
1698 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1699 ZR_DEVNAME(zr), *frame);
1701 mutex_lock(&zr->resource_lock);
1702 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
1703 mutex_unlock(&zr->resource_lock);
1708 case BUZIOC_QBUF_PLAY:
1710 int *frame = arg, res;
1712 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1713 ZR_DEVNAME(zr), *frame);
1715 mutex_lock(&zr->resource_lock);
1716 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1717 mutex_unlock(&zr->resource_lock);
1724 struct zoran_sync *bsync = arg;
1727 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1729 mutex_lock(&zr->resource_lock);
1731 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1732 dprintk(2, KERN_WARNING
1733 "%s: %s - not in jpg capture mode\n",
1734 ZR_DEVNAME(zr), __func__);
1737 res = jpg_sync(fh, bsync);
1739 mutex_unlock(&zr->resource_lock);
1744 case BUZIOC_G_STATUS:
1746 struct zoran_status *bstat = arg;
1747 int status = 0, res = 0;
1750 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1752 if (zr->codec_mode != BUZ_MODE_IDLE) {
1755 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1760 mutex_lock(&zr->resource_lock);
1762 if (zr->codec_mode != BUZ_MODE_IDLE) {
1765 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1768 goto gstat_unlock_and_return;
1771 decoder_call(zr, video, s_routing,
1772 zr->card.input[bstat->input].muxsel, 0, 0);
1774 /* sleep 1 second */
1777 /* Get status of video decoder */
1778 decoder_call(zr, video, querystd, &norm);
1779 decoder_call(zr, video, g_input_status, &status);
1781 /* restore previous input and norm */
1782 decoder_call(zr, video, s_routing,
1783 zr->card.input[zr->input].muxsel, 0, 0);
1784 gstat_unlock_and_return:
1785 mutex_unlock(&zr->resource_lock);
1789 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1790 if (norm & V4L2_STD_NTSC)
1791 bstat->norm = VIDEO_MODE_NTSC;
1792 else if (norm & V4L2_STD_SECAM)
1793 bstat->norm = VIDEO_MODE_SECAM;
1795 bstat->norm = VIDEO_MODE_PAL;
1798 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1809 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1811 struct zoran_fh *fh = __fh;
1812 struct zoran *zr = fh->zr;
1816 mutex_lock(&zr->resource_lock);
1818 if (fh->buffers.allocated) {
1821 "%s: VIDIOCGMBUF - buffers already allocated\n",
1824 goto v4l1reqbuf_unlock_and_return;
1827 /* The next mmap will map the V4L buffers */
1830 if (v4l_fbuffer_alloc(fh)) {
1832 goto v4l1reqbuf_unlock_and_return;
1835 vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1836 vmbuf->frames = fh->buffers.num_buffers;
1837 for (i = 0; i < vmbuf->frames; i++)
1838 vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1840 v4l1reqbuf_unlock_and_return:
1841 mutex_unlock(&zr->resource_lock);
1847 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1849 struct zoran_fh *fh = __fh;
1850 struct zoran *zr = fh->zr;
1852 memset(cap, 0, sizeof(*cap));
1853 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1854 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1855 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1856 pci_name(zr->pci_dev));
1857 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1859 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1860 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1864 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1868 for (i = 0; i < NUM_FORMATS; i++) {
1869 if (zoran_formats[i].flags & flag)
1871 if (num == fmt->index)
1874 if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
1877 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1878 fmt->pixelformat = zoran_formats[i].fourcc;
1879 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1880 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1884 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1885 struct v4l2_fmtdesc *f)
1887 struct zoran_fh *fh = __fh;
1888 struct zoran *zr = fh->zr;
1890 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1893 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1894 struct v4l2_fmtdesc *f)
1896 struct zoran_fh *fh = __fh;
1897 struct zoran *zr = fh->zr;
1899 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1902 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1903 struct v4l2_fmtdesc *f)
1905 struct zoran_fh *fh = __fh;
1906 struct zoran *zr = fh->zr;
1908 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1911 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1912 struct v4l2_format *fmt)
1914 struct zoran_fh *fh = __fh;
1915 struct zoran *zr = fh->zr;
1917 mutex_lock(&zr->resource_lock);
1919 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1920 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1921 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1922 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1923 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1924 if (fh->jpg_settings.TmpDcm == 1)
1925 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1926 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1928 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1929 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1930 fmt->fmt.pix.bytesperline = 0;
1931 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1933 mutex_unlock(&zr->resource_lock);
1937 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1938 struct v4l2_format *fmt)
1940 struct zoran_fh *fh = __fh;
1941 struct zoran *zr = fh->zr;
1943 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1944 return zoran_g_fmt_vid_out(file, fh, fmt);
1946 mutex_lock(&zr->resource_lock);
1947 fmt->fmt.pix.width = fh->v4l_settings.width;
1948 fmt->fmt.pix.height = fh->v4l_settings.height;
1949 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1950 fh->v4l_settings.height;
1951 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1952 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1953 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1954 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1955 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1957 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1958 mutex_unlock(&zr->resource_lock);
1962 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1963 struct v4l2_format *fmt)
1965 struct zoran_fh *fh = __fh;
1966 struct zoran *zr = fh->zr;
1968 mutex_lock(&zr->resource_lock);
1970 fmt->fmt.win.w.left = fh->overlay_settings.x;
1971 fmt->fmt.win.w.top = fh->overlay_settings.y;
1972 fmt->fmt.win.w.width = fh->overlay_settings.width;
1973 fmt->fmt.win.w.height = fh->overlay_settings.height;
1974 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1975 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1977 fmt->fmt.win.field = V4L2_FIELD_TOP;
1979 mutex_unlock(&zr->resource_lock);
1983 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1984 struct v4l2_format *fmt)
1986 struct zoran_fh *fh = __fh;
1987 struct zoran *zr = fh->zr;
1989 mutex_lock(&zr->resource_lock);
1991 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1992 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1993 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1994 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1995 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1996 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1997 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1998 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2000 mutex_unlock(&zr->resource_lock);
2004 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2005 struct v4l2_format *fmt)
2007 struct zoran_fh *fh = __fh;
2008 struct zoran *zr = fh->zr;
2009 struct zoran_jpg_settings settings;
2012 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2015 mutex_lock(&zr->resource_lock);
2016 settings = fh->jpg_settings;
2018 /* we actually need to set 'real' parameters now */
2019 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2020 settings.TmpDcm = 1;
2022 settings.TmpDcm = 2;
2023 settings.decimation = 0;
2024 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2025 settings.VerDcm = 2;
2027 settings.VerDcm = 1;
2028 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2029 settings.HorDcm = 4;
2030 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2031 settings.HorDcm = 2;
2033 settings.HorDcm = 1;
2034 if (settings.TmpDcm == 1)
2035 settings.field_per_buff = 2;
2037 settings.field_per_buff = 1;
2039 if (settings.HorDcm > 1) {
2040 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2041 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2044 settings.img_width = BUZ_MAX_WIDTH;
2048 res = zoran_check_jpg_settings(zr, &settings, 1);
2050 goto tryfmt_unlock_and_return;
2052 /* tell the user what we actually did */
2053 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2054 fmt->fmt.pix.height = settings.img_height * 2 /
2055 (settings.TmpDcm * settings.VerDcm);
2056 if (settings.TmpDcm == 1)
2057 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2058 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2060 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2061 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2063 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2064 fmt->fmt.pix.bytesperline = 0;
2065 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2066 tryfmt_unlock_and_return:
2067 mutex_unlock(&zr->resource_lock);
2071 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2072 struct v4l2_format *fmt)
2074 struct zoran_fh *fh = __fh;
2075 struct zoran *zr = fh->zr;
2079 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2080 return zoran_try_fmt_vid_out(file, fh, fmt);
2082 mutex_lock(&zr->resource_lock);
2084 for (i = 0; i < NUM_FORMATS; i++)
2085 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2088 if (i == NUM_FORMATS) {
2089 mutex_unlock(&zr->resource_lock);
2093 bpp = (zoran_formats[i].depth + 7) / 8;
2094 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2095 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2096 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2097 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2098 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2099 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2100 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2101 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2102 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2103 mutex_unlock(&zr->resource_lock);
2108 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2109 struct v4l2_format *fmt)
2111 struct zoran_fh *fh = __fh;
2112 struct zoran *zr = fh->zr;
2115 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2116 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2117 fmt->fmt.win.w.width,
2118 fmt->fmt.win.w.height,
2119 fmt->fmt.win.clipcount,
2120 fmt->fmt.win.bitmap);
2121 mutex_lock(&zr->resource_lock);
2122 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2123 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
2124 (struct v4l2_clip __user *)fmt->fmt.win.clips,
2125 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
2126 mutex_unlock(&zr->resource_lock);
2130 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2131 struct v4l2_format *fmt)
2133 struct zoran_fh *fh = __fh;
2134 struct zoran *zr = fh->zr;
2135 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2136 struct zoran_jpg_settings settings;
2139 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2140 fmt->fmt.pix.width, fmt->fmt.pix.height,
2141 fmt->fmt.pix.pixelformat,
2142 (char *) &printformat);
2143 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2146 mutex_lock(&zr->resource_lock);
2148 if (fh->buffers.allocated) {
2149 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2152 goto sfmtjpg_unlock_and_return;
2155 settings = fh->jpg_settings;
2157 /* we actually need to set 'real' parameters now */
2158 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2159 settings.TmpDcm = 1;
2161 settings.TmpDcm = 2;
2162 settings.decimation = 0;
2163 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2164 settings.VerDcm = 2;
2166 settings.VerDcm = 1;
2167 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2168 settings.HorDcm = 4;
2169 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2170 settings.HorDcm = 2;
2172 settings.HorDcm = 1;
2173 if (settings.TmpDcm == 1)
2174 settings.field_per_buff = 2;
2176 settings.field_per_buff = 1;
2178 if (settings.HorDcm > 1) {
2179 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2180 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2183 settings.img_width = BUZ_MAX_WIDTH;
2187 res = zoran_check_jpg_settings(zr, &settings, 0);
2189 goto sfmtjpg_unlock_and_return;
2191 /* it's ok, so set them */
2192 fh->jpg_settings = settings;
2194 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2195 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2197 /* tell the user what we actually did */
2198 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2199 fmt->fmt.pix.height = settings.img_height * 2 /
2200 (settings.TmpDcm * settings.VerDcm);
2201 if (settings.TmpDcm == 1)
2202 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2203 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2205 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2206 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2207 fmt->fmt.pix.bytesperline = 0;
2208 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
2209 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2211 sfmtjpg_unlock_and_return:
2212 mutex_unlock(&zr->resource_lock);
2216 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2217 struct v4l2_format *fmt)
2219 struct zoran_fh *fh = __fh;
2220 struct zoran *zr = fh->zr;
2224 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2225 return zoran_s_fmt_vid_out(file, fh, fmt);
2227 for (i = 0; i < NUM_FORMATS; i++)
2228 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2230 if (i == NUM_FORMATS) {
2231 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2232 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2236 mutex_lock(&zr->resource_lock);
2238 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2239 fh->buffers.active != ZORAN_FREE) {
2240 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2243 goto sfmtv4l_unlock_and_return;
2245 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2246 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2247 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2248 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2252 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2255 goto sfmtv4l_unlock_and_return;
2257 /* tell the user the results/missing stuff */
2258 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2259 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2260 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2261 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2262 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2264 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2266 sfmtv4l_unlock_and_return:
2267 mutex_unlock(&zr->resource_lock);
2271 static int zoran_g_fbuf(struct file *file, void *__fh,
2272 struct v4l2_framebuffer *fb)
2274 struct zoran_fh *fh = __fh;
2275 struct zoran *zr = fh->zr;
2277 memset(fb, 0, sizeof(*fb));
2278 mutex_lock(&zr->resource_lock);
2279 fb->base = zr->vbuf_base;
2280 fb->fmt.width = zr->vbuf_width;
2281 fb->fmt.height = zr->vbuf_height;
2282 if (zr->overlay_settings.format)
2283 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2284 fb->fmt.bytesperline = zr->vbuf_bytesperline;
2285 mutex_unlock(&zr->resource_lock);
2286 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2287 fb->fmt.field = V4L2_FIELD_INTERLACED;
2288 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2289 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2294 static int zoran_s_fbuf(struct file *file, void *__fh,
2295 struct v4l2_framebuffer *fb)
2297 struct zoran_fh *fh = __fh;
2298 struct zoran *zr = fh->zr;
2300 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2302 for (i = 0; i < NUM_FORMATS; i++)
2303 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2305 if (i == NUM_FORMATS) {
2306 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2307 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2308 (char *)&printformat);
2312 mutex_lock(&zr->resource_lock);
2313 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2314 fb->fmt.height, fb->fmt.bytesperline);
2315 mutex_unlock(&zr->resource_lock);
2320 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2322 struct zoran_fh *fh = __fh;
2323 struct zoran *zr = fh->zr;
2326 mutex_lock(&zr->resource_lock);
2327 res = setup_overlay(fh, on);
2328 mutex_unlock(&zr->resource_lock);
2333 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2335 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2337 struct zoran_fh *fh = __fh;
2338 struct zoran *zr = fh->zr;
2341 if (req->memory != V4L2_MEMORY_MMAP) {
2344 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2345 ZR_DEVNAME(zr), req->memory);
2349 if (req->count == 0)
2350 return zoran_streamoff(file, fh, req->type);
2352 mutex_lock(&zr->resource_lock);
2353 if (fh->buffers.allocated) {
2356 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2359 goto v4l2reqbuf_unlock_and_return;
2362 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2363 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2364 /* control user input */
2367 if (req->count > v4l_nbufs)
2368 req->count = v4l_nbufs;
2370 /* The next mmap will map the V4L buffers */
2372 fh->buffers.num_buffers = req->count;
2374 if (v4l_fbuffer_alloc(fh)) {
2376 goto v4l2reqbuf_unlock_and_return;
2378 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2379 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2380 /* we need to calculate size ourselves now */
2383 if (req->count > jpg_nbufs)
2384 req->count = jpg_nbufs;
2386 /* The next mmap will map the MJPEG buffers */
2387 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2388 fh->buffers.num_buffers = req->count;
2389 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2391 if (jpg_fbuffer_alloc(fh)) {
2393 goto v4l2reqbuf_unlock_and_return;
2398 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2399 ZR_DEVNAME(zr), req->type);
2401 goto v4l2reqbuf_unlock_and_return;
2403 v4l2reqbuf_unlock_and_return:
2404 mutex_unlock(&zr->resource_lock);
2409 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2411 struct zoran_fh *fh = __fh;
2412 struct zoran *zr = fh->zr;
2415 mutex_lock(&zr->resource_lock);
2416 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2417 mutex_unlock(&zr->resource_lock);
2422 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2424 struct zoran_fh *fh = __fh;
2425 struct zoran *zr = fh->zr;
2426 int res = 0, codec_mode, buf_type;
2428 mutex_lock(&zr->resource_lock);
2430 switch (fh->map_mode) {
2431 case ZORAN_MAP_MODE_RAW:
2432 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2434 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2435 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2437 goto qbuf_unlock_and_return;
2440 res = zoran_v4l_queue_frame(fh, buf->index);
2442 goto qbuf_unlock_and_return;
2443 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2444 zr36057_set_memgrab(zr, 1);
2447 case ZORAN_MAP_MODE_JPG_REC:
2448 case ZORAN_MAP_MODE_JPG_PLAY:
2449 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2450 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2451 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2453 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2454 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2457 if (buf->type != buf_type) {
2459 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2460 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2462 goto qbuf_unlock_and_return;
2465 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
2467 goto qbuf_unlock_and_return;
2468 if (zr->codec_mode == BUZ_MODE_IDLE &&
2469 fh->buffers.active == ZORAN_LOCKED)
2470 zr36057_enable_jpg(zr, codec_mode);
2476 "%s: VIDIOC_QBUF - unsupported type %d\n",
2477 ZR_DEVNAME(zr), buf->type);
2481 qbuf_unlock_and_return:
2482 mutex_unlock(&zr->resource_lock);
2487 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2489 struct zoran_fh *fh = __fh;
2490 struct zoran *zr = fh->zr;
2491 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2493 mutex_lock(&zr->resource_lock);
2495 switch (fh->map_mode) {
2496 case ZORAN_MAP_MODE_RAW:
2497 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2499 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2500 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2502 goto dqbuf_unlock_and_return;
2505 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2506 if (file->f_flags & O_NONBLOCK &&
2507 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2509 goto dqbuf_unlock_and_return;
2511 res = v4l_sync(fh, num);
2513 goto dqbuf_unlock_and_return;
2514 zr->v4l_sync_tail++;
2515 res = zoran_v4l2_buffer_status(fh, buf, num);
2518 case ZORAN_MAP_MODE_JPG_REC:
2519 case ZORAN_MAP_MODE_JPG_PLAY:
2521 struct zoran_sync bs;
2523 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2524 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2526 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2528 if (buf->type != buf_type) {
2530 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2531 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2533 goto dqbuf_unlock_and_return;
2536 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2538 if (file->f_flags & O_NONBLOCK &&
2539 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2541 goto dqbuf_unlock_and_return;
2543 res = jpg_sync(fh, &bs);
2545 goto dqbuf_unlock_and_return;
2546 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2552 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2553 ZR_DEVNAME(zr), buf->type);
2557 dqbuf_unlock_and_return:
2558 mutex_unlock(&zr->resource_lock);
2563 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2565 struct zoran_fh *fh = __fh;
2566 struct zoran *zr = fh->zr;
2569 mutex_lock(&zr->resource_lock);
2571 switch (fh->map_mode) {
2572 case ZORAN_MAP_MODE_RAW: /* raw capture */
2573 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2574 fh->buffers.active != ZORAN_ACTIVE) {
2576 goto strmon_unlock_and_return;
2579 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2580 zr->v4l_settings = fh->v4l_settings;
2582 zr->v4l_sync_tail = zr->v4l_pend_tail;
2583 if (!zr->v4l_memgrab_active &&
2584 zr->v4l_pend_head != zr->v4l_pend_tail) {
2585 zr36057_set_memgrab(zr, 1);
2589 case ZORAN_MAP_MODE_JPG_REC:
2590 case ZORAN_MAP_MODE_JPG_PLAY:
2591 /* what is the codec mode right now? */
2592 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2593 fh->buffers.active != ZORAN_ACTIVE) {
2595 goto strmon_unlock_and_return;
2598 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2600 if (zr->jpg_que_head != zr->jpg_que_tail) {
2601 /* Start the jpeg codec when the first frame is queued */
2609 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2610 ZR_DEVNAME(zr), fh->map_mode);
2614 strmon_unlock_and_return:
2615 mutex_unlock(&zr->resource_lock);
2620 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2622 struct zoran_fh *fh = __fh;
2623 struct zoran *zr = fh->zr;
2625 unsigned long flags;
2627 mutex_lock(&zr->resource_lock);
2629 switch (fh->map_mode) {
2630 case ZORAN_MAP_MODE_RAW: /* raw capture */
2631 if (fh->buffers.active == ZORAN_FREE &&
2632 zr->v4l_buffers.active != ZORAN_FREE) {
2633 res = -EPERM; /* stay off other's settings! */
2634 goto strmoff_unlock_and_return;
2636 if (zr->v4l_buffers.active == ZORAN_FREE)
2637 goto strmoff_unlock_and_return;
2639 spin_lock_irqsave(&zr->spinlock, flags);
2640 /* unload capture */
2641 if (zr->v4l_memgrab_active) {
2643 zr36057_set_memgrab(zr, 0);
2646 for (i = 0; i < fh->buffers.num_buffers; i++)
2647 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2648 fh->buffers = zr->v4l_buffers;
2650 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2652 zr->v4l_grab_seq = 0;
2653 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2654 zr->v4l_sync_tail = 0;
2656 spin_unlock_irqrestore(&zr->spinlock, flags);
2660 case ZORAN_MAP_MODE_JPG_REC:
2661 case ZORAN_MAP_MODE_JPG_PLAY:
2662 if (fh->buffers.active == ZORAN_FREE &&
2663 zr->jpg_buffers.active != ZORAN_FREE) {
2664 res = -EPERM; /* stay off other's settings! */
2665 goto strmoff_unlock_and_return;
2667 if (zr->jpg_buffers.active == ZORAN_FREE)
2668 goto strmoff_unlock_and_return;
2670 res = jpg_qbuf(fh, -1,
2671 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2672 BUZ_MODE_MOTION_COMPRESS :
2673 BUZ_MODE_MOTION_DECOMPRESS);
2675 goto strmoff_unlock_and_return;
2679 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2680 ZR_DEVNAME(zr), fh->map_mode);
2684 strmoff_unlock_and_return:
2685 mutex_unlock(&zr->resource_lock);
2690 static int zoran_queryctrl(struct file *file, void *__fh,
2691 struct v4l2_queryctrl *ctrl)
2693 struct zoran_fh *fh = __fh;
2694 struct zoran *zr = fh->zr;
2696 /* we only support hue/saturation/contrast/brightness */
2697 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2698 ctrl->id > V4L2_CID_HUE)
2701 decoder_call(zr, core, queryctrl, ctrl);
2706 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2708 struct zoran_fh *fh = __fh;
2709 struct zoran *zr = fh->zr;
2711 /* we only support hue/saturation/contrast/brightness */
2712 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2713 ctrl->id > V4L2_CID_HUE)
2716 mutex_lock(&zr->resource_lock);
2717 decoder_call(zr, core, g_ctrl, ctrl);
2718 mutex_unlock(&zr->resource_lock);
2723 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2725 struct zoran_fh *fh = __fh;
2726 struct zoran *zr = fh->zr;
2728 /* we only support hue/saturation/contrast/brightness */
2729 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2730 ctrl->id > V4L2_CID_HUE)
2733 mutex_lock(&zr->resource_lock);
2734 decoder_call(zr, core, s_ctrl, ctrl);
2735 mutex_unlock(&zr->resource_lock);
2740 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2742 struct zoran_fh *fh = __fh;
2743 struct zoran *zr = fh->zr;
2745 mutex_lock(&zr->resource_lock);
2747 mutex_unlock(&zr->resource_lock);
2751 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2753 struct zoran_fh *fh = __fh;
2754 struct zoran *zr = fh->zr;
2757 mutex_lock(&zr->resource_lock);
2758 res = zoran_set_norm(zr, *std);
2760 goto sstd_unlock_and_return;
2762 res = wait_grab_pending(zr);
2763 sstd_unlock_and_return:
2764 mutex_unlock(&zr->resource_lock);
2768 static int zoran_enum_input(struct file *file, void *__fh,
2769 struct v4l2_input *inp)
2771 struct zoran_fh *fh = __fh;
2772 struct zoran *zr = fh->zr;
2774 if (inp->index < 0 || inp->index >= zr->card.inputs)
2777 int id = inp->index;
2778 memset(inp, 0, sizeof(*inp));
2782 strncpy(inp->name, zr->card.input[inp->index].name,
2783 sizeof(inp->name) - 1);
2784 inp->type = V4L2_INPUT_TYPE_CAMERA;
2785 inp->std = V4L2_STD_ALL;
2787 /* Get status of video decoder */
2788 mutex_lock(&zr->resource_lock);
2789 decoder_call(zr, video, g_input_status, &inp->status);
2790 mutex_unlock(&zr->resource_lock);
2794 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2796 struct zoran_fh *fh = __fh;
2797 struct zoran *zr = fh->zr;
2799 mutex_lock(&zr->resource_lock);
2801 mutex_unlock(&zr->resource_lock);
2806 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2808 struct zoran_fh *fh = __fh;
2809 struct zoran *zr = fh->zr;
2812 mutex_lock(&zr->resource_lock);
2813 res = zoran_set_input(zr, input);
2815 goto sinput_unlock_and_return;
2817 /* Make sure the changes come into effect */
2818 res = wait_grab_pending(zr);
2819 sinput_unlock_and_return:
2820 mutex_unlock(&zr->resource_lock);
2824 static int zoran_enum_output(struct file *file, void *__fh,
2825 struct v4l2_output *outp)
2827 if (outp->index != 0)
2830 memset(outp, 0, sizeof(*outp));
2832 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2833 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2838 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2845 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2853 /* cropping (sub-frame capture) */
2854 static int zoran_cropcap(struct file *file, void *__fh,
2855 struct v4l2_cropcap *cropcap)
2857 struct zoran_fh *fh = __fh;
2858 struct zoran *zr = fh->zr;
2859 int type = cropcap->type, res = 0;
2861 memset(cropcap, 0, sizeof(*cropcap));
2862 cropcap->type = type;
2864 mutex_lock(&zr->resource_lock);
2866 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2867 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2868 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2870 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2873 goto cropcap_unlock_and_return;
2876 cropcap->bounds.top = cropcap->bounds.left = 0;
2877 cropcap->bounds.width = BUZ_MAX_WIDTH;
2878 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2879 cropcap->defrect.top = cropcap->defrect.left = 0;
2880 cropcap->defrect.width = BUZ_MIN_WIDTH;
2881 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2882 cropcap_unlock_and_return:
2883 mutex_unlock(&zr->resource_lock);
2887 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2889 struct zoran_fh *fh = __fh;
2890 struct zoran *zr = fh->zr;
2891 int type = crop->type, res = 0;
2893 memset(crop, 0, sizeof(*crop));
2896 mutex_lock(&zr->resource_lock);
2898 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2899 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2900 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2903 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2906 goto gcrop_unlock_and_return;
2909 crop->c.top = fh->jpg_settings.img_y;
2910 crop->c.left = fh->jpg_settings.img_x;
2911 crop->c.width = fh->jpg_settings.img_width;
2912 crop->c.height = fh->jpg_settings.img_height;
2914 gcrop_unlock_and_return:
2915 mutex_unlock(&zr->resource_lock);
2920 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2922 struct zoran_fh *fh = __fh;
2923 struct zoran *zr = fh->zr;
2925 struct zoran_jpg_settings settings;
2927 settings = fh->jpg_settings;
2929 mutex_lock(&zr->resource_lock);
2931 if (fh->buffers.allocated) {
2933 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2936 goto scrop_unlock_and_return;
2939 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2940 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2941 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2943 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2946 goto scrop_unlock_and_return;
2949 /* move into a form that we understand */
2950 settings.img_x = crop->c.left;
2951 settings.img_y = crop->c.top;
2952 settings.img_width = crop->c.width;
2953 settings.img_height = crop->c.height;
2955 /* check validity */
2956 res = zoran_check_jpg_settings(zr, &settings, 0);
2958 goto scrop_unlock_and_return;
2961 fh->jpg_settings = settings;
2963 scrop_unlock_and_return:
2964 mutex_unlock(&zr->resource_lock);
2968 static int zoran_g_jpegcomp(struct file *file, void *__fh,
2969 struct v4l2_jpegcompression *params)
2971 struct zoran_fh *fh = __fh;
2972 struct zoran *zr = fh->zr;
2973 memset(params, 0, sizeof(*params));
2975 mutex_lock(&zr->resource_lock);
2977 params->quality = fh->jpg_settings.jpg_comp.quality;
2978 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2979 memcpy(params->APP_data,
2980 fh->jpg_settings.jpg_comp.APP_data,
2981 fh->jpg_settings.jpg_comp.APP_len);
2982 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2983 memcpy(params->COM_data,
2984 fh->jpg_settings.jpg_comp.COM_data,
2985 fh->jpg_settings.jpg_comp.COM_len);
2986 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2987 params->jpeg_markers =
2988 fh->jpg_settings.jpg_comp.jpeg_markers;
2990 mutex_unlock(&zr->resource_lock);
2995 static int zoran_s_jpegcomp(struct file *file, void *__fh,
2996 struct v4l2_jpegcompression *params)
2998 struct zoran_fh *fh = __fh;
2999 struct zoran *zr = fh->zr;
3001 struct zoran_jpg_settings settings;
3003 settings = fh->jpg_settings;
3005 settings.jpg_comp = *params;
3007 mutex_lock(&zr->resource_lock);
3009 if (fh->buffers.active != ZORAN_FREE) {
3010 dprintk(1, KERN_WARNING
3011 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3014 goto sjpegc_unlock_and_return;
3017 res = zoran_check_jpg_settings(zr, &settings, 0);
3019 goto sjpegc_unlock_and_return;
3020 if (!fh->buffers.allocated)
3021 fh->buffers.buffer_size =
3022 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3023 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3024 sjpegc_unlock_and_return:
3025 mutex_unlock(&zr->resource_lock);
3031 zoran_poll (struct file *file,
3034 struct zoran_fh *fh = file->private_data;
3035 struct zoran *zr = fh->zr;
3037 unsigned long flags;
3039 /* we should check whether buffers are ready to be synced on
3040 * (w/o waits - O_NONBLOCK) here
3041 * if ready for read (sync), return POLLIN|POLLRDNORM,
3042 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3043 * if error, return POLLERR,
3044 * if no buffers queued or so, return POLLNVAL
3047 mutex_lock(&zr->resource_lock);
3049 switch (fh->map_mode) {
3050 case ZORAN_MAP_MODE_RAW:
3051 poll_wait(file, &zr->v4l_capq, wait);
3052 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3054 spin_lock_irqsave(&zr->spinlock, flags);
3057 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3058 ZR_DEVNAME(zr), __func__,
3059 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
3060 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3061 zr->v4l_pend_tail, zr->v4l_pend_head);
3062 /* Process is the one capturing? */
3063 if (fh->buffers.active != ZORAN_FREE &&
3064 /* Buffer ready to DQBUF? */
3065 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3066 res = POLLIN | POLLRDNORM;
3067 spin_unlock_irqrestore(&zr->spinlock, flags);
3071 case ZORAN_MAP_MODE_JPG_REC:
3072 case ZORAN_MAP_MODE_JPG_PLAY:
3073 poll_wait(file, &zr->jpg_capq, wait);
3074 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3076 spin_lock_irqsave(&zr->spinlock, flags);
3079 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3080 ZR_DEVNAME(zr), __func__,
3081 "FAL"[fh->buffers.active], zr->jpg_que_tail,
3082 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3083 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3084 if (fh->buffers.active != ZORAN_FREE &&
3085 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3086 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3087 res = POLLIN | POLLRDNORM;
3089 res = POLLOUT | POLLWRNORM;
3091 spin_unlock_irqrestore(&zr->spinlock, flags);
3098 "%s: %s - internal error, unknown map_mode=%d\n",
3099 ZR_DEVNAME(zr), __func__, fh->map_mode);
3103 mutex_unlock(&zr->resource_lock);
3110 * This maps the buffers to user space.
3112 * Depending on the state of fh->map_mode
3113 * the V4L or the MJPEG buffers are mapped
3114 * per buffer or all together
3116 * Note that we need to connect to some
3117 * unmap signal event to unmap the de-allocate
3118 * the buffer accordingly (zoran_vm_close())
3122 zoran_vm_open (struct vm_area_struct *vma)
3124 struct zoran_mapping *map = vma->vm_private_data;
3130 zoran_vm_close (struct vm_area_struct *vma)
3132 struct zoran_mapping *map = vma->vm_private_data;
3133 struct zoran_fh *fh = map->file->private_data;
3134 struct zoran *zr = fh->zr;
3137 if (--map->count > 0)
3140 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3141 __func__, mode_name(fh->map_mode));
3143 for (i = 0; i < fh->buffers.num_buffers; i++) {
3144 if (fh->buffers.buffer[i].map == map)
3145 fh->buffers.buffer[i].map = NULL;
3149 /* Any buffers still mapped? */
3150 for (i = 0; i < fh->buffers.num_buffers; i++)
3151 if (fh->buffers.buffer[i].map)
3154 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3155 __func__, mode_name(fh->map_mode));
3157 mutex_lock(&zr->resource_lock);
3159 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3160 if (fh->buffers.active != ZORAN_FREE) {
3161 unsigned long flags;
3163 spin_lock_irqsave(&zr->spinlock, flags);
3164 zr36057_set_memgrab(zr, 0);
3165 zr->v4l_buffers.allocated = 0;
3166 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3167 spin_unlock_irqrestore(&zr->spinlock, flags);
3169 v4l_fbuffer_free(fh);
3171 if (fh->buffers.active != ZORAN_FREE) {
3172 jpg_qbuf(fh, -1, zr->codec_mode);
3173 zr->jpg_buffers.allocated = 0;
3174 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
3176 jpg_fbuffer_free(fh);
3179 mutex_unlock(&zr->resource_lock);
3182 static struct vm_operations_struct zoran_vm_ops = {
3183 .open = zoran_vm_open,
3184 .close = zoran_vm_close,
3188 zoran_mmap (struct file *file,
3189 struct vm_area_struct *vma)
3191 struct zoran_fh *fh = file->private_data;
3192 struct zoran *zr = fh->zr;
3193 unsigned long size = (vma->vm_end - vma->vm_start);
3194 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3196 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3198 struct zoran_mapping *map;
3202 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3203 ZR_DEVNAME(zr), __func__,
3204 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
3206 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3207 !(vma->vm_flags & VM_WRITE)) {
3210 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3211 ZR_DEVNAME(zr), __func__);
3215 mutex_lock(&zr->resource_lock);
3217 if (!fh->buffers.allocated) {
3220 "%s: %s(%s) - buffers not yet allocated\n",
3221 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
3223 goto mmap_unlock_and_return;
3226 first = offset / fh->buffers.buffer_size;
3227 last = first - 1 + size / fh->buffers.buffer_size;
3228 if (offset % fh->buffers.buffer_size != 0 ||
3229 size % fh->buffers.buffer_size != 0 || first < 0 ||
3230 last < 0 || first >= fh->buffers.num_buffers ||
3231 last >= fh->buffers.buffer_size) {
3234 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3235 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
3236 fh->buffers.buffer_size,
3237 fh->buffers.num_buffers);
3239 goto mmap_unlock_and_return;
3242 /* Check if any buffers are already mapped */
3243 for (i = first; i <= last; i++) {
3244 if (fh->buffers.buffer[i].map) {
3247 "%s: %s(%s) - buffer %d already mapped\n",
3248 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
3250 goto mmap_unlock_and_return;
3254 /* map these buffers */
3255 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3258 goto mmap_unlock_and_return;
3263 vma->vm_ops = &zoran_vm_ops;
3264 vma->vm_flags |= VM_DONTEXPAND;
3265 vma->vm_private_data = map;
3267 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3268 for (i = first; i <= last; i++) {
3270 if (todo > fh->buffers.buffer_size)
3271 todo = fh->buffers.buffer_size;
3272 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3273 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3274 todo, PAGE_SHARED)) {
3277 "%s: %s(V4L) - remap_pfn_range failed\n",
3278 ZR_DEVNAME(zr), __func__);
3280 goto mmap_unlock_and_return;
3284 fh->buffers.buffer[i].map = map;
3289 for (i = first; i <= last; i++) {
3291 j < fh->buffers.buffer_size / PAGE_SIZE;
3294 (le32_to_cpu(fh->buffers.buffer[i].jpg.
3295 frag_tab[2 * j + 1]) & ~1) << 1;
3300 le32_to_cpu(fh->buffers.
3301 buffer[i].jpg.frag_tab[2 * j]);
3302 /* should just be pos on i386 */
3303 page = virt_to_phys(bus_to_virt(pos))
3305 if (remap_pfn_range(vma, start, page,
3306 todo, PAGE_SHARED)) {
3309 "%s: %s(V4L) - remap_pfn_range failed\n",
3310 ZR_DEVNAME(zr), __func__);
3312 goto mmap_unlock_and_return;
3318 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
3319 frag_tab[2 * j + 1]) & 1)
3320 break; /* was last fragment */
3322 fh->buffers.buffer[i].map = map;
3329 mmap_unlock_and_return:
3330 mutex_unlock(&zr->resource_lock);
3335 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3336 .vidioc_querycap = zoran_querycap,
3337 .vidioc_cropcap = zoran_cropcap,
3338 .vidioc_s_crop = zoran_s_crop,
3339 .vidioc_g_crop = zoran_g_crop,
3340 .vidioc_enum_input = zoran_enum_input,
3341 .vidioc_g_input = zoran_g_input,
3342 .vidioc_s_input = zoran_s_input,
3343 .vidioc_enum_output = zoran_enum_output,
3344 .vidioc_g_output = zoran_g_output,
3345 .vidioc_s_output = zoran_s_output,
3346 .vidioc_g_fbuf = zoran_g_fbuf,
3347 .vidioc_s_fbuf = zoran_s_fbuf,
3348 .vidioc_g_std = zoran_g_std,
3349 .vidioc_s_std = zoran_s_std,
3350 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3351 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3352 .vidioc_overlay = zoran_overlay,
3353 .vidioc_reqbufs = zoran_reqbufs,
3354 .vidioc_querybuf = zoran_querybuf,
3355 .vidioc_qbuf = zoran_qbuf,
3356 .vidioc_dqbuf = zoran_dqbuf,
3357 .vidioc_streamon = zoran_streamon,
3358 .vidioc_streamoff = zoran_streamoff,
3359 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3360 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3361 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3362 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3363 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3364 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3365 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3366 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3367 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3368 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3369 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3370 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3371 .vidioc_queryctrl = zoran_queryctrl,
3372 .vidioc_s_ctrl = zoran_s_ctrl,
3373 .vidioc_g_ctrl = zoran_g_ctrl,
3374 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3375 .vidioc_default = zoran_default,
3376 .vidiocgmbuf = zoran_vidiocgmbuf,
3380 static const struct v4l2_file_operations zoran_fops = {
3381 .owner = THIS_MODULE,
3383 .release = zoran_close,
3384 .ioctl = video_ioctl2,
3386 .write = zoran_write,
3391 struct video_device zoran_template __devinitdata = {
3393 .fops = &zoran_fops,
3394 .ioctl_ops = &zoran_ioctl_ops,
3395 .release = &zoran_vdev_release,
3396 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,