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/smp_lock.h>
53 #include <linux/pci.h>
54 #include <linux/vmalloc.h>
55 #include <linux/wait.h>
57 #include <linux/interrupt.h>
58 #include <linux/i2c.h>
59 #include <linux/i2c-algo-bit.h>
61 #include <linux/spinlock.h>
63 #include <linux/videodev.h>
64 #include <media/v4l2-common.h>
65 #include <media/v4l2-ioctl.h>
66 #include "videocodec.h"
68 #include <asm/byteorder.h>
70 #include <asm/uaccess.h>
71 #include <linux/proc_fs.h>
73 #include <linux/mutex.h>
75 #include "zoran_device.h"
76 #include "zoran_card.h"
79 const struct zoran_format zoran_formats[] = {
81 .name = "15-bit RGB LE",
82 .fourcc = V4L2_PIX_FMT_RGB555,
83 .colorspace = V4L2_COLORSPACE_SRGB,
85 .flags = ZORAN_FORMAT_CAPTURE |
87 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
88 ZR36057_VFESPFR_LittleEndian,
90 .name = "15-bit RGB BE",
91 .fourcc = V4L2_PIX_FMT_RGB555X,
92 .colorspace = V4L2_COLORSPACE_SRGB,
94 .flags = ZORAN_FORMAT_CAPTURE |
96 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
98 .name = "16-bit RGB LE",
99 .fourcc = V4L2_PIX_FMT_RGB565,
100 .colorspace = V4L2_COLORSPACE_SRGB,
102 .flags = ZORAN_FORMAT_CAPTURE |
103 ZORAN_FORMAT_OVERLAY,
104 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
105 ZR36057_VFESPFR_LittleEndian,
107 .name = "16-bit RGB BE",
108 .fourcc = V4L2_PIX_FMT_RGB565X,
109 .colorspace = V4L2_COLORSPACE_SRGB,
111 .flags = ZORAN_FORMAT_CAPTURE |
112 ZORAN_FORMAT_OVERLAY,
113 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
115 .name = "24-bit RGB",
116 .fourcc = V4L2_PIX_FMT_BGR24,
117 .colorspace = V4L2_COLORSPACE_SRGB,
119 .flags = ZORAN_FORMAT_CAPTURE |
120 ZORAN_FORMAT_OVERLAY,
121 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
123 .name = "32-bit RGB LE",
124 .fourcc = V4L2_PIX_FMT_BGR32,
125 .colorspace = V4L2_COLORSPACE_SRGB,
127 .flags = ZORAN_FORMAT_CAPTURE |
128 ZORAN_FORMAT_OVERLAY,
129 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
131 .name = "32-bit RGB BE",
132 .fourcc = V4L2_PIX_FMT_RGB32,
133 .colorspace = V4L2_COLORSPACE_SRGB,
135 .flags = ZORAN_FORMAT_CAPTURE |
136 ZORAN_FORMAT_OVERLAY,
137 .vfespfr = ZR36057_VFESPFR_RGB888,
139 .name = "4:2:2, packed, YUYV",
140 .fourcc = V4L2_PIX_FMT_YUYV,
141 .colorspace = V4L2_COLORSPACE_SMPTE170M,
143 .flags = ZORAN_FORMAT_CAPTURE |
144 ZORAN_FORMAT_OVERLAY,
145 .vfespfr = ZR36057_VFESPFR_YUV422,
147 .name = "4:2:2, packed, UYVY",
148 .fourcc = V4L2_PIX_FMT_UYVY,
149 .colorspace = V4L2_COLORSPACE_SMPTE170M,
151 .flags = ZORAN_FORMAT_CAPTURE |
152 ZORAN_FORMAT_OVERLAY,
153 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
155 .name = "Hardware-encoded Motion-JPEG",
156 .fourcc = V4L2_PIX_FMT_MJPEG,
157 .colorspace = V4L2_COLORSPACE_SMPTE170M,
159 .flags = ZORAN_FORMAT_CAPTURE |
160 ZORAN_FORMAT_PLAYBACK |
161 ZORAN_FORMAT_COMPRESSED,
164 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
166 /* small helper function for calculating buffersizes for v4l2
167 * we calculate the nearest higher power-of-two, which
168 * will be the recommended buffersize */
170 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
172 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
173 __u32 num = (1024 * 512) / (div);
182 if (result > jpg_bufsize)
189 /* forward references */
190 static void v4l_fbuffer_free(struct zoran_fh *fh);
191 static void jpg_fbuffer_free(struct zoran_fh *fh);
193 /* Set mapping mode */
194 static void map_mode_raw(struct zoran_fh *fh)
196 fh->map_mode = ZORAN_MAP_MODE_RAW;
197 fh->buffers.buffer_size = v4l_bufsize;
198 fh->buffers.num_buffers = v4l_nbufs;
200 static void map_mode_jpg(struct zoran_fh *fh, int play)
202 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
203 fh->buffers.buffer_size = jpg_bufsize;
204 fh->buffers.num_buffers = jpg_nbufs;
206 static inline const char *mode_name(enum zoran_map_mode mode)
208 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
212 * Allocate the V4L grab buffers
214 * These have to be pysically contiguous.
217 static int v4l_fbuffer_alloc(struct zoran_fh *fh)
219 struct zoran *zr = fh->zr;
223 for (i = 0; i < fh->buffers.num_buffers; i++) {
224 if (fh->buffers.buffer[i].v4l.fbuffer)
227 "%s: %s - buffer %d already allocated!?\n",
228 ZR_DEVNAME(zr), __func__, i);
231 mem = kmalloc(fh->buffers.buffer_size,
232 GFP_KERNEL | __GFP_NOWARN);
236 "%s: %s - kmalloc for V4L buf %d failed\n",
237 ZR_DEVNAME(zr), __func__, i);
238 v4l_fbuffer_free(fh);
241 fh->buffers.buffer[i].v4l.fbuffer = mem;
242 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
243 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
244 for (off = 0; off < fh->buffers.buffer_size;
246 SetPageReserved(virt_to_page(mem + off));
249 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
250 ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
251 (unsigned long long)virt_to_bus(mem));
254 fh->buffers.allocated = 1;
259 /* free the V4L grab buffers */
260 static void v4l_fbuffer_free(struct zoran_fh *fh)
262 struct zoran *zr = fh->zr;
266 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
268 for (i = 0; i < fh->buffers.num_buffers; i++) {
269 if (!fh->buffers.buffer[i].v4l.fbuffer)
272 mem = fh->buffers.buffer[i].v4l.fbuffer;
273 for (off = 0; off < fh->buffers.buffer_size;
275 ClearPageReserved(virt_to_page(mem + off));
276 kfree(fh->buffers.buffer[i].v4l.fbuffer);
277 fh->buffers.buffer[i].v4l.fbuffer = NULL;
280 fh->buffers.allocated = 0;
284 * Allocate the MJPEG grab buffers.
286 * If a Natoma chipset is present and this is a revision 1 zr36057,
287 * each MJPEG buffer needs to be physically contiguous.
288 * (RJ: This statement is from Dave Perks' original driver,
289 * I could never check it because I have a zr36067)
291 * RJ: The contents grab buffers needs never be accessed in the driver.
292 * Therefore there is no need to allocate them with vmalloc in order
293 * to get a contiguous virtual memory space.
294 * I don't understand why many other drivers first allocate them with
295 * vmalloc (which uses internally also get_zeroed_page, but delivers you
296 * virtual addresses) and then again have to make a lot of efforts
297 * to get the physical address.
300 * On big-endian architectures (such as ppc) some extra steps
301 * are needed. When reading and writing to the stat_com array
302 * and fragment buffers, the device expects to see little-
303 * endian values. The use of cpu_to_le32() and le32_to_cpu()
304 * in this function (and one or two others in zoran_device.c)
305 * ensure that these values are always stored in little-endian
306 * form, regardless of architecture. The zr36057 does Very Bad
307 * Things on big endian architectures if the stat_com array
308 * and fragment buffers are not little-endian.
311 static int jpg_fbuffer_alloc(struct zoran_fh *fh)
313 struct zoran *zr = fh->zr;
317 for (i = 0; i < fh->buffers.num_buffers; i++) {
318 if (fh->buffers.buffer[i].jpg.frag_tab)
321 "%s: %s - buffer %d already allocated!?\n",
322 ZR_DEVNAME(zr), __func__, i);
324 /* Allocate fragment table for this buffer */
326 mem = (void *)get_zeroed_page(GFP_KERNEL);
330 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
331 ZR_DEVNAME(zr), __func__, i);
332 jpg_fbuffer_free(fh);
335 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
336 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
338 if (fh->buffers.need_contiguous) {
339 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
343 "%s: %s - kmalloc failed for buffer %d\n",
344 ZR_DEVNAME(zr), __func__, i);
345 jpg_fbuffer_free(fh);
348 fh->buffers.buffer[i].jpg.frag_tab[0] =
349 cpu_to_le32(virt_to_bus(mem));
350 fh->buffers.buffer[i].jpg.frag_tab[1] =
351 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
352 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
353 SetPageReserved(virt_to_page(mem + off));
355 /* jpg_bufsize is already page aligned */
356 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
357 mem = (void *)get_zeroed_page(GFP_KERNEL);
361 "%s: %s - get_zeroed_page failed for buffer %d\n",
362 ZR_DEVNAME(zr), __func__, i);
363 jpg_fbuffer_free(fh);
367 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
368 cpu_to_le32(virt_to_bus(mem));
369 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
370 cpu_to_le32((PAGE_SIZE >> 2) << 1);
371 SetPageReserved(virt_to_page(mem));
374 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
379 KERN_DEBUG "%s: %s - %d KB allocated\n",
380 ZR_DEVNAME(zr), __func__,
381 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
383 fh->buffers.allocated = 1;
388 /* free the MJPEG grab buffers */
389 static void jpg_fbuffer_free(struct zoran_fh *fh)
391 struct zoran *zr = fh->zr;
395 struct zoran_buffer *buffer;
397 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
399 for (i = 0, buffer = &fh->buffers.buffer[0];
400 i < fh->buffers.num_buffers; i++, buffer++) {
401 if (!buffer->jpg.frag_tab)
404 if (fh->buffers.need_contiguous) {
405 frag_tab = buffer->jpg.frag_tab[0];
408 mem = bus_to_virt(le32_to_cpu(frag_tab));
409 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
410 ClearPageReserved(virt_to_page(mem + off));
412 buffer->jpg.frag_tab[0] = 0;
413 buffer->jpg.frag_tab[1] = 0;
416 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
417 frag_tab = buffer->jpg.frag_tab[2 * j];
421 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
422 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
423 buffer->jpg.frag_tab[2 * j] = 0;
424 buffer->jpg.frag_tab[2 * j + 1] = 0;
428 free_page((unsigned long)buffer->jpg.frag_tab);
429 buffer->jpg.frag_tab = NULL;
432 fh->buffers.allocated = 0;
436 * V4L Buffer grabbing
440 zoran_v4l_set_format (struct zoran_fh *fh,
443 const struct zoran_format *format)
445 struct zoran *zr = fh->zr;
448 /* Check size and format of the grab wanted */
450 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
451 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
454 "%s: %s - wrong frame size (%dx%d)\n",
455 ZR_DEVNAME(zr), __func__, width, height);
459 bpp = (format->depth + 7) / 8;
461 /* Check against available buffer size */
462 if (height * width * bpp > fh->buffers.buffer_size) {
465 "%s: %s - video buffer size (%d kB) is too small\n",
466 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
470 /* The video front end needs 4-byte alinged line sizes */
472 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
475 "%s: %s - wrong frame alignment\n",
476 ZR_DEVNAME(zr), __func__);
480 fh->v4l_settings.width = width;
481 fh->v4l_settings.height = height;
482 fh->v4l_settings.format = format;
483 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
488 static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
490 struct zoran *zr = fh->zr;
494 if (!fh->buffers.allocated) {
497 "%s: %s - buffers not yet allocated\n",
498 ZR_DEVNAME(zr), __func__);
502 /* No grabbing outside the buffer range! */
503 if (num >= fh->buffers.num_buffers || num < 0) {
506 "%s: %s - buffer %d is out of range\n",
507 ZR_DEVNAME(zr), __func__, num);
511 spin_lock_irqsave(&zr->spinlock, flags);
513 if (fh->buffers.active == ZORAN_FREE) {
514 if (zr->v4l_buffers.active == ZORAN_FREE) {
515 zr->v4l_buffers = fh->buffers;
516 fh->buffers.active = ZORAN_ACTIVE;
520 "%s: %s - another session is already capturing\n",
521 ZR_DEVNAME(zr), __func__);
526 /* make sure a grab isn't going on currently with this buffer */
528 switch (zr->v4l_buffers.buffer[num].state) {
531 if (zr->v4l_buffers.active == ZORAN_FREE) {
532 fh->buffers.active = ZORAN_FREE;
533 zr->v4l_buffers.allocated = 0;
535 res = -EBUSY; /* what are you doing? */
540 "%s: %s - queueing buffer %d in state DONE!?\n",
541 ZR_DEVNAME(zr), __func__, num);
543 /* since there is at least one unused buffer there's room for at least
544 * one more pend[] entry */
545 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
546 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
547 zr->v4l_buffers.buffer[num].bs.length =
548 fh->v4l_settings.bytesperline *
549 zr->v4l_settings.height;
550 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
555 spin_unlock_irqrestore(&zr->spinlock, flags);
557 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
558 zr->v4l_buffers.active = fh->buffers.active;
564 * Sync on a V4L buffer
567 static int v4l_sync(struct zoran_fh *fh, int frame)
569 struct zoran *zr = fh->zr;
572 if (fh->buffers.active == ZORAN_FREE) {
575 "%s: %s - no grab active for this session\n",
576 ZR_DEVNAME(zr), __func__);
580 /* check passed-in frame number */
581 if (frame >= fh->buffers.num_buffers || frame < 0) {
583 KERN_ERR "%s: %s - frame %d is invalid\n",
584 ZR_DEVNAME(zr), __func__, frame);
588 /* Check if is buffer was queued at all */
589 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
592 "%s: %s - attempt to sync on a buffer which was not queued?\n",
593 ZR_DEVNAME(zr), __func__);
597 /* wait on this buffer to get ready */
598 if (!wait_event_interruptible_timeout(zr->v4l_capq,
599 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
601 if (signal_pending(current))
604 /* buffer should now be in BUZ_STATE_DONE */
605 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
607 KERN_ERR "%s: %s - internal state error\n",
608 ZR_DEVNAME(zr), __func__);
610 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
611 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
613 spin_lock_irqsave(&zr->spinlock, flags);
615 /* Check if streaming capture has finished */
616 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
617 zr36057_set_memgrab(zr, 0);
618 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
619 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
620 zr->v4l_buffers.allocated = 0;
624 spin_unlock_irqrestore(&zr->spinlock, flags);
630 * Queue a MJPEG buffer for capture/playback
633 static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
634 enum zoran_codec_mode mode)
636 struct zoran *zr = fh->zr;
640 /* Check if buffers are allocated */
641 if (!fh->buffers.allocated) {
644 "%s: %s - buffers not yet allocated\n",
645 ZR_DEVNAME(zr), __func__);
649 /* No grabbing outside the buffer range! */
650 if (num >= fh->buffers.num_buffers || num < 0) {
653 "%s: %s - buffer %d out of range\n",
654 ZR_DEVNAME(zr), __func__, num);
658 /* what is the codec mode right now? */
659 if (zr->codec_mode == BUZ_MODE_IDLE) {
660 zr->jpg_settings = fh->jpg_settings;
661 } else if (zr->codec_mode != mode) {
662 /* wrong codec mode active - invalid */
665 "%s: %s - codec in wrong mode\n",
666 ZR_DEVNAME(zr), __func__);
670 if (fh->buffers.active == ZORAN_FREE) {
671 if (zr->jpg_buffers.active == ZORAN_FREE) {
672 zr->jpg_buffers = fh->buffers;
673 fh->buffers.active = ZORAN_ACTIVE;
677 "%s: %s - another session is already capturing\n",
678 ZR_DEVNAME(zr), __func__);
683 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
684 /* Ok load up the jpeg codec */
685 zr36057_enable_jpg(zr, mode);
688 spin_lock_irqsave(&zr->spinlock, flags);
691 switch (zr->jpg_buffers.buffer[num].state) {
695 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
696 ZR_DEVNAME(zr), __func__);
698 /* since there is at least one unused buffer there's room for at
699 *least one more pend[] entry */
700 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
701 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
702 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
703 zoran_feed_stat_com(zr);
708 if (zr->jpg_buffers.active == ZORAN_FREE) {
709 fh->buffers.active = ZORAN_FREE;
710 zr->jpg_buffers.allocated = 0;
712 res = -EBUSY; /* what are you doing? */
717 spin_unlock_irqrestore(&zr->spinlock, flags);
719 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
720 zr->jpg_buffers.active = fh->buffers.active;
725 static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
727 struct zoran *zr = fh->zr;
730 /* Does the user want to stop streaming? */
732 if (zr->codec_mode == mode) {
733 if (fh->buffers.active == ZORAN_FREE) {
736 "%s: %s(-1) - session not active\n",
737 ZR_DEVNAME(zr), __func__);
740 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
741 zr->jpg_buffers.allocated = 0;
742 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
747 "%s: %s - stop streaming but not in streaming mode\n",
748 ZR_DEVNAME(zr), __func__);
753 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
756 /* Start the jpeg codec when the first frame is queued */
757 if (!res && zr->jpg_que_head == 1)
764 * Sync on a MJPEG buffer
767 static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
769 struct zoran *zr = fh->zr;
773 if (fh->buffers.active == ZORAN_FREE) {
776 "%s: %s - capture is not currently active\n",
777 ZR_DEVNAME(zr), __func__);
780 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
781 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
784 "%s: %s - codec not in streaming mode\n",
785 ZR_DEVNAME(zr), __func__);
788 if (!wait_event_interruptible_timeout(zr->jpg_capq,
789 (zr->jpg_que_tail != zr->jpg_dma_tail ||
790 zr->jpg_dma_tail == zr->jpg_dma_head),
794 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
796 zr->codec->control(zr->codec, CODEC_G_STATUS,
800 "%s: %s - timeout: codec isr=0x%02x\n",
801 ZR_DEVNAME(zr), __func__, isr);
806 if (signal_pending(current))
809 spin_lock_irqsave(&zr->spinlock, flags);
811 if (zr->jpg_dma_tail != zr->jpg_dma_head)
812 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
814 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
816 /* buffer should now be in BUZ_STATE_DONE */
817 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
819 KERN_ERR "%s: %s - internal state error\n",
820 ZR_DEVNAME(zr), __func__);
822 *bs = zr->jpg_buffers.buffer[frame].bs;
824 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
825 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
827 spin_unlock_irqrestore(&zr->spinlock, flags);
832 static void zoran_open_init_session(struct zoran_fh *fh)
835 struct zoran *zr = fh->zr;
837 /* Per default, map the V4L Buffers */
840 /* take over the card's current settings */
841 fh->overlay_settings = zr->overlay_settings;
842 fh->overlay_settings.is_set = 0;
843 fh->overlay_settings.format = zr->overlay_settings.format;
844 fh->overlay_active = ZORAN_FREE;
847 fh->v4l_settings = zr->v4l_settings;
849 fh->jpg_settings = zr->jpg_settings;
852 memset(&fh->buffers, 0, sizeof(fh->buffers));
853 for (i = 0; i < MAX_FRAME; i++) {
854 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
855 fh->buffers.buffer[i].bs.frame = i;
857 fh->buffers.allocated = 0;
858 fh->buffers.active = ZORAN_FREE;
861 static void zoran_close_end_session(struct zoran_fh *fh)
863 struct zoran *zr = fh->zr;
866 if (fh->overlay_active != ZORAN_FREE) {
867 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
868 zr->v4l_overlay_active = 0;
869 if (!zr->v4l_memgrab_active)
870 zr36057_overlay(zr, 0);
871 zr->overlay_mask = NULL;
874 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
876 if (fh->buffers.active != ZORAN_FREE) {
879 spin_lock_irqsave(&zr->spinlock, flags);
880 zr36057_set_memgrab(zr, 0);
881 zr->v4l_buffers.allocated = 0;
882 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
883 spin_unlock_irqrestore(&zr->spinlock, flags);
887 if (fh->buffers.allocated)
888 v4l_fbuffer_free(fh);
891 if (fh->buffers.active != ZORAN_FREE) {
892 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
893 zr->jpg_buffers.allocated = 0;
894 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
898 if (fh->buffers.allocated)
899 jpg_fbuffer_free(fh);
904 * Open a zoran card. Right now the flags stuff is just playing
907 static int zoran_open(struct file *file)
909 struct zoran *zr = video_drvdata(file);
911 int res, first_open = 0;
913 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
914 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
918 if (zr->user >= 2048) {
919 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
920 ZR_DEVNAME(zr), zr->user);
925 /* now, create the open()-specific file_ops struct */
926 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
930 "%s: %s - allocation of zoran_fh failed\n",
931 ZR_DEVNAME(zr), __func__);
935 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
938 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
939 if (!fh->overlay_mask) {
942 "%s: %s - allocation of overlay_mask failed\n",
943 ZR_DEVNAME(zr), __func__);
951 /*mutex_unlock(&zr->resource_lock);*/
953 /* default setup - TODO: look at flags */
954 if (first_open) { /* First device open */
956 zoran_open_init_params(zr);
957 zoran_init_hardware(zr);
959 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
962 /* set file_ops stuff */
963 file->private_data = fh;
965 zoran_open_init_session(fh);
975 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
976 ZR_DEVNAME(zr), res, zr->user);
982 zoran_close(struct file *file)
984 struct zoran_fh *fh = file->private_data;
985 struct zoran *zr = fh->zr;
987 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
988 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
990 /* kernel locks (fs/device.c), so don't do that ourselves
991 * (prevents deadlocks) */
992 /*mutex_lock(&zr->resource_lock);*/
994 zoran_close_end_session(fh);
996 if (zr->user-- == 1) { /* Last process */
997 /* Clean up JPEG process */
998 wake_up_interruptible(&zr->jpg_capq);
999 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1000 zr->jpg_buffers.allocated = 0;
1001 zr->jpg_buffers.active = ZORAN_FREE;
1003 /* disable interrupts */
1004 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1006 if (zr36067_debug > 1)
1007 print_interrupts(zr);
1010 zr->v4l_overlay_active = 0;
1011 zr36057_overlay(zr, 0);
1012 zr->overlay_mask = NULL;
1015 wake_up_interruptible(&zr->v4l_capq);
1016 zr36057_set_memgrab(zr, 0);
1017 zr->v4l_buffers.allocated = 0;
1018 zr->v4l_buffers.active = ZORAN_FREE;
1019 zoran_set_pci_master(zr, 0);
1021 if (!pass_through) { /* Switch to color bar */
1022 decoder_call(zr, video, s_stream, 0);
1023 encoder_call(zr, video, s_routing, 2, 0, 0);
1027 file->private_data = NULL;
1028 kfree(fh->overlay_mask);
1031 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
1038 zoran_read (struct file *file,
1043 /* we simply don't support read() (yet)... */
1049 zoran_write (struct file *file,
1050 const char __user *data,
1054 /* ...and the same goes for write() */
1059 static int setup_fbuffer(struct zoran_fh *fh,
1061 const struct zoran_format *fmt,
1066 struct zoran *zr = fh->zr;
1068 /* (Ronald) v4l/v4l2 guidelines */
1069 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1072 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1073 ALi Magik (that needs very low latency while the card needs a
1074 higher value always) */
1076 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1079 /* we need a bytesperline value, even if not given */
1081 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1084 if (zr->overlay_active) {
1085 /* dzjee... stupid users... don't even bother to turn off
1086 * overlay before changing the memory location...
1087 * normally, we would return errors here. However, one of
1088 * the tools that does this is... xawtv! and since xawtv
1089 * is used by +/- 99% of the users, we'd rather be user-
1090 * friendly and silently do as if nothing went wrong */
1093 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1094 ZR_DEVNAME(zr), __func__);
1095 zr36057_overlay(zr, 0);
1099 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1102 "%s: %s - no valid overlay format given\n",
1103 ZR_DEVNAME(zr), __func__);
1106 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1109 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1110 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
1113 if (bytesperline & 3) {
1116 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1117 ZR_DEVNAME(zr), __func__, bytesperline);
1121 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1122 zr->vbuf_height = height;
1123 zr->vbuf_width = width;
1124 zr->vbuf_depth = fmt->depth;
1125 zr->overlay_settings.format = fmt;
1126 zr->vbuf_bytesperline = bytesperline;
1128 /* The user should set new window parameters */
1129 zr->overlay_settings.is_set = 0;
1135 static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
1136 struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
1138 struct zoran *zr = fh->zr;
1139 struct v4l2_clip *vcp = NULL;
1143 if (!zr->vbuf_base) {
1146 "%s: %s - frame buffer has to be set first\n",
1147 ZR_DEVNAME(zr), __func__);
1151 if (!fh->overlay_settings.format) {
1154 "%s: %s - no overlay format set\n",
1155 ZR_DEVNAME(zr), __func__);
1160 * The video front end needs 4-byte alinged line sizes, we correct that
1161 * silently here if necessary
1163 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1164 end = (x + width) & ~1; /* round down */
1165 x = (x + 1) & ~1; /* round up */
1169 if (zr->vbuf_depth == 24) {
1170 end = (x + width) & ~3; /* round down */
1171 x = (x + 3) & ~3; /* round up */
1175 if (width > BUZ_MAX_WIDTH)
1176 width = BUZ_MAX_WIDTH;
1177 if (height > BUZ_MAX_HEIGHT)
1178 height = BUZ_MAX_HEIGHT;
1180 /* Check for vaild parameters */
1181 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1182 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1185 "%s: %s - width = %d or height = %d invalid\n",
1186 ZR_DEVNAME(zr), __func__, width, height);
1190 fh->overlay_settings.x = x;
1191 fh->overlay_settings.y = y;
1192 fh->overlay_settings.width = width;
1193 fh->overlay_settings.height = height;
1194 fh->overlay_settings.clipcount = clipcount;
1197 * If an overlay is running, we have to switch it off
1198 * and switch it on again in order to get the new settings in effect.
1200 * We also want to avoid that the overlay mask is written
1201 * when an overlay is running.
1204 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1205 zr->overlay_active != ZORAN_FREE &&
1206 fh->overlay_active != ZORAN_FREE;
1208 zr36057_overlay(zr, 0);
1211 * Write the overlay mask if clips are wanted.
1212 * We prefer a bitmap.
1215 /* fake value - it just means we want clips */
1216 fh->overlay_settings.clipcount = 1;
1218 if (copy_from_user(fh->overlay_mask, bitmap,
1219 (width * height + 7) / 8)) {
1222 } else if (clipcount > 0) {
1223 /* write our own bitmap from the clips */
1224 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1228 "%s: %s - Alloc of clip mask failed\n",
1229 ZR_DEVNAME(zr), __func__);
1233 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1237 write_overlay_mask(fh, vcp, clipcount);
1241 fh->overlay_settings.is_set = 1;
1242 if (fh->overlay_active != ZORAN_FREE &&
1243 zr->overlay_active != ZORAN_FREE)
1244 zr->overlay_settings = fh->overlay_settings;
1247 zr36057_overlay(zr, 1);
1249 /* Make sure the changes come into effect */
1250 return wait_grab_pending(zr);
1253 static int setup_overlay(struct zoran_fh *fh, int on)
1255 struct zoran *zr = fh->zr;
1257 /* If there is nothing to do, return immediatly */
1258 if ((on && fh->overlay_active != ZORAN_FREE) ||
1259 (!on && fh->overlay_active == ZORAN_FREE))
1262 /* check whether we're touching someone else's overlay */
1263 if (on && zr->overlay_active != ZORAN_FREE &&
1264 fh->overlay_active == ZORAN_FREE) {
1267 "%s: %s - overlay is already active for another session\n",
1268 ZR_DEVNAME(zr), __func__);
1271 if (!on && zr->overlay_active != ZORAN_FREE &&
1272 fh->overlay_active == ZORAN_FREE) {
1275 "%s: %s - you cannot cancel someone else's session\n",
1276 ZR_DEVNAME(zr), __func__);
1281 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1282 zr->v4l_overlay_active = 0;
1283 /* When a grab is running, the video simply
1284 * won't be switched on any more */
1285 if (!zr->v4l_memgrab_active)
1286 zr36057_overlay(zr, 0);
1287 zr->overlay_mask = NULL;
1289 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1292 "%s: %s - buffer or window not set\n",
1293 ZR_DEVNAME(zr), __func__);
1296 if (!fh->overlay_settings.format) {
1299 "%s: %s - no overlay format set\n",
1300 ZR_DEVNAME(zr), __func__);
1303 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1304 zr->v4l_overlay_active = 1;
1305 zr->overlay_mask = fh->overlay_mask;
1306 zr->overlay_settings = fh->overlay_settings;
1307 if (!zr->v4l_memgrab_active)
1308 zr36057_overlay(zr, 1);
1309 /* When a grab is running, the video will be
1310 * switched on when grab is finished */
1313 /* Make sure the changes come into effect */
1314 return wait_grab_pending(zr);
1317 /* get the status of a buffer in the clients buffer queue */
1318 static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1319 struct v4l2_buffer *buf, int num)
1321 struct zoran *zr = fh->zr;
1322 unsigned long flags;
1324 buf->flags = V4L2_BUF_FLAG_MAPPED;
1326 switch (fh->map_mode) {
1327 case ZORAN_MAP_MODE_RAW:
1329 if (num < 0 || num >= fh->buffers.num_buffers ||
1330 !fh->buffers.allocated) {
1333 "%s: %s - wrong number or buffers not allocated\n",
1334 ZR_DEVNAME(zr), __func__);
1338 spin_lock_irqsave(&zr->spinlock, flags);
1341 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1342 ZR_DEVNAME(zr), __func__,
1343 "FAL"[fh->buffers.active], num,
1344 "UPMD"[zr->v4l_buffers.buffer[num].state],
1345 fh->buffers.buffer[num].map ? 'Y' : 'N');
1346 spin_unlock_irqrestore(&zr->spinlock, flags);
1348 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1349 buf->length = fh->buffers.buffer_size;
1352 buf->bytesused = fh->buffers.buffer[num].bs.length;
1353 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1354 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1355 buf->sequence = fh->buffers.buffer[num].bs.seq;
1356 buf->flags |= V4L2_BUF_FLAG_DONE;
1357 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1359 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1362 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1363 buf->field = V4L2_FIELD_TOP;
1365 buf->field = V4L2_FIELD_INTERLACED;
1369 case ZORAN_MAP_MODE_JPG_REC:
1370 case ZORAN_MAP_MODE_JPG_PLAY:
1373 if (num < 0 || num >= fh->buffers.num_buffers ||
1374 !fh->buffers.allocated) {
1377 "%s: %s - wrong number or buffers not allocated\n",
1378 ZR_DEVNAME(zr), __func__);
1382 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1383 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1384 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1385 buf->length = fh->buffers.buffer_size;
1387 /* these variables are only written after frame has been captured */
1388 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1389 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1390 buf->sequence = fh->buffers.buffer[num].bs.seq;
1391 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1392 buf->bytesused = fh->buffers.buffer[num].bs.length;
1393 buf->flags |= V4L2_BUF_FLAG_DONE;
1395 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1398 /* which fields are these? */
1399 if (fh->jpg_settings.TmpDcm != 1)
1400 buf->field = fh->jpg_settings.odd_even ?
1401 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1403 buf->field = fh->jpg_settings.odd_even ?
1404 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1412 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1413 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
1417 buf->memory = V4L2_MEMORY_MMAP;
1419 buf->m.offset = buf->length * num;
1425 zoran_set_norm (struct zoran *zr,
1430 if (zr->v4l_buffers.active != ZORAN_FREE ||
1431 zr->jpg_buffers.active != ZORAN_FREE) {
1434 "%s: %s called while in playback/capture mode\n",
1435 ZR_DEVNAME(zr), __func__);
1439 if (!(norm & zr->card.norms)) {
1441 KERN_ERR "%s: %s - unsupported norm %llx\n",
1442 ZR_DEVNAME(zr), __func__, norm);
1446 if (norm == V4L2_STD_ALL) {
1448 v4l2_std_id std = 0;
1450 decoder_call(zr, video, querystd, &std);
1451 decoder_call(zr, core, s_std, std);
1453 /* let changes come into effect */
1456 decoder_call(zr, video, g_input_status, &status);
1457 if (status & V4L2_IN_ST_NO_SIGNAL) {
1460 "%s: %s - no norm detected\n",
1461 ZR_DEVNAME(zr), __func__);
1463 decoder_call(zr, core, s_std, zr->norm);
1469 if (norm & V4L2_STD_SECAM)
1470 zr->timing = zr->card.tvn[2];
1471 else if (norm & V4L2_STD_NTSC)
1472 zr->timing = zr->card.tvn[1];
1474 zr->timing = zr->card.tvn[0];
1476 /* We switch overlay off and on since a change in the
1477 * norm needs different VFE settings */
1478 on = zr->overlay_active && !zr->v4l_memgrab_active;
1480 zr36057_overlay(zr, 0);
1482 decoder_call(zr, core, s_std, norm);
1483 encoder_call(zr, video, s_std_output, norm);
1486 zr36057_overlay(zr, 1);
1488 /* Make sure the changes come into effect */
1495 zoran_set_input (struct zoran *zr,
1498 if (input == zr->input) {
1502 if (zr->v4l_buffers.active != ZORAN_FREE ||
1503 zr->jpg_buffers.active != ZORAN_FREE) {
1506 "%s: %s called while in playback/capture mode\n",
1507 ZR_DEVNAME(zr), __func__);
1511 if (input < 0 || input >= zr->card.inputs) {
1514 "%s: %s - unnsupported input %d\n",
1515 ZR_DEVNAME(zr), __func__, input);
1521 decoder_call(zr, video, s_routing,
1522 zr->card.input[input].muxsel, 0, 0);
1531 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1532 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1534 struct zoran_fh *fh = __fh;
1535 struct zoran *zr = fh->zr;
1536 struct zoran_jpg_settings settings;
1539 case BUZIOC_G_PARAMS:
1541 struct zoran_params *bparams = arg;
1543 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1545 memset(bparams, 0, sizeof(struct zoran_params));
1546 bparams->major_version = MAJOR_VERSION;
1547 bparams->minor_version = MINOR_VERSION;
1549 mutex_lock(&zr->resource_lock);
1551 if (zr->norm & V4L2_STD_NTSC)
1552 bparams->norm = VIDEO_MODE_NTSC;
1553 else if (zr->norm & V4L2_STD_PAL)
1554 bparams->norm = VIDEO_MODE_PAL;
1556 bparams->norm = VIDEO_MODE_SECAM;
1558 bparams->input = zr->input;
1560 bparams->decimation = fh->jpg_settings.decimation;
1561 bparams->HorDcm = fh->jpg_settings.HorDcm;
1562 bparams->VerDcm = fh->jpg_settings.VerDcm;
1563 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1564 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1565 bparams->img_x = fh->jpg_settings.img_x;
1566 bparams->img_y = fh->jpg_settings.img_y;
1567 bparams->img_width = fh->jpg_settings.img_width;
1568 bparams->img_height = fh->jpg_settings.img_height;
1569 bparams->odd_even = fh->jpg_settings.odd_even;
1571 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1572 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1573 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1574 memcpy(bparams->APP_data,
1575 fh->jpg_settings.jpg_comp.APP_data,
1576 sizeof(bparams->APP_data));
1577 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1578 memcpy(bparams->COM_data,
1579 fh->jpg_settings.jpg_comp.COM_data,
1580 sizeof(bparams->COM_data));
1581 bparams->jpeg_markers =
1582 fh->jpg_settings.jpg_comp.jpeg_markers;
1584 mutex_unlock(&zr->resource_lock);
1586 bparams->VFIFO_FB = 0;
1591 case BUZIOC_S_PARAMS:
1593 struct zoran_params *bparams = arg;
1596 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1598 settings.decimation = bparams->decimation;
1599 settings.HorDcm = bparams->HorDcm;
1600 settings.VerDcm = bparams->VerDcm;
1601 settings.TmpDcm = bparams->TmpDcm;
1602 settings.field_per_buff = bparams->field_per_buff;
1603 settings.img_x = bparams->img_x;
1604 settings.img_y = bparams->img_y;
1605 settings.img_width = bparams->img_width;
1606 settings.img_height = bparams->img_height;
1607 settings.odd_even = bparams->odd_even;
1609 settings.jpg_comp.quality = bparams->quality;
1610 settings.jpg_comp.APPn = bparams->APPn;
1611 settings.jpg_comp.APP_len = bparams->APP_len;
1612 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1613 sizeof(bparams->APP_data));
1614 settings.jpg_comp.COM_len = bparams->COM_len;
1615 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1616 sizeof(bparams->COM_data));
1617 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1619 mutex_lock(&zr->resource_lock);
1621 if (zr->codec_mode != BUZ_MODE_IDLE) {
1624 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1627 goto sparams_unlock_and_return;
1630 /* Check the params first before overwriting our
1632 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1634 goto sparams_unlock_and_return;
1637 fh->jpg_settings = settings;
1638 sparams_unlock_and_return:
1639 mutex_unlock(&zr->resource_lock);
1644 case BUZIOC_REQBUFS:
1646 struct zoran_requestbuffers *breq = arg;
1651 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1652 ZR_DEVNAME(zr), breq->count, breq->size);
1654 /* Enforce reasonable lower and upper limits */
1655 if (breq->count < 4)
1656 breq->count = 4; /* Could be choosen smaller */
1657 if (breq->count > jpg_nbufs)
1658 breq->count = jpg_nbufs;
1659 breq->size = PAGE_ALIGN(breq->size);
1660 if (breq->size < 8192)
1661 breq->size = 8192; /* Arbitrary */
1662 /* breq->size is limited by 1 page for the stat_com
1663 * tables to a Maximum of 2 MB */
1664 if (breq->size > jpg_bufsize)
1665 breq->size = jpg_bufsize;
1667 mutex_lock(&zr->resource_lock);
1669 if (fh->buffers.allocated) {
1672 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1675 goto jpgreqbuf_unlock_and_return;
1678 /* The next mmap will map the MJPEG buffers - could
1679 * also be *_PLAY, but it doesn't matter here */
1680 map_mode_jpg(fh, 0);
1681 fh->buffers.num_buffers = breq->count;
1682 fh->buffers.buffer_size = breq->size;
1684 if (jpg_fbuffer_alloc(fh)) {
1686 goto jpgreqbuf_unlock_and_return;
1689 jpgreqbuf_unlock_and_return:
1690 mutex_unlock(&zr->resource_lock);
1695 case BUZIOC_QBUF_CAPT:
1697 int *frame = arg, res;
1699 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1700 ZR_DEVNAME(zr), *frame);
1702 mutex_lock(&zr->resource_lock);
1703 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
1704 mutex_unlock(&zr->resource_lock);
1709 case BUZIOC_QBUF_PLAY:
1711 int *frame = arg, res;
1713 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1714 ZR_DEVNAME(zr), *frame);
1716 mutex_lock(&zr->resource_lock);
1717 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1718 mutex_unlock(&zr->resource_lock);
1725 struct zoran_sync *bsync = arg;
1728 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1730 mutex_lock(&zr->resource_lock);
1732 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1733 dprintk(2, KERN_WARNING
1734 "%s: %s - not in jpg capture mode\n",
1735 ZR_DEVNAME(zr), __func__);
1738 res = jpg_sync(fh, bsync);
1740 mutex_unlock(&zr->resource_lock);
1745 case BUZIOC_G_STATUS:
1747 struct zoran_status *bstat = arg;
1748 int status = 0, res = 0;
1751 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1753 if (zr->codec_mode != BUZ_MODE_IDLE) {
1756 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1761 mutex_lock(&zr->resource_lock);
1763 if (zr->codec_mode != BUZ_MODE_IDLE) {
1766 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1769 goto gstat_unlock_and_return;
1772 decoder_call(zr, video, s_routing,
1773 zr->card.input[bstat->input].muxsel, 0, 0);
1775 /* sleep 1 second */
1778 /* Get status of video decoder */
1779 decoder_call(zr, video, querystd, &norm);
1780 decoder_call(zr, video, g_input_status, &status);
1782 /* restore previous input and norm */
1783 decoder_call(zr, video, s_routing,
1784 zr->card.input[zr->input].muxsel, 0, 0);
1785 gstat_unlock_and_return:
1786 mutex_unlock(&zr->resource_lock);
1790 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1791 if (norm & V4L2_STD_NTSC)
1792 bstat->norm = VIDEO_MODE_NTSC;
1793 else if (norm & V4L2_STD_SECAM)
1794 bstat->norm = VIDEO_MODE_SECAM;
1796 bstat->norm = VIDEO_MODE_PAL;
1799 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1810 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1812 struct zoran_fh *fh = __fh;
1813 struct zoran *zr = fh->zr;
1817 mutex_lock(&zr->resource_lock);
1819 if (fh->buffers.allocated) {
1822 "%s: VIDIOCGMBUF - buffers already allocated\n",
1825 goto v4l1reqbuf_unlock_and_return;
1828 /* The next mmap will map the V4L buffers */
1831 if (v4l_fbuffer_alloc(fh)) {
1833 goto v4l1reqbuf_unlock_and_return;
1836 vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1837 vmbuf->frames = fh->buffers.num_buffers;
1838 for (i = 0; i < vmbuf->frames; i++)
1839 vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1841 v4l1reqbuf_unlock_and_return:
1842 mutex_unlock(&zr->resource_lock);
1848 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1850 struct zoran_fh *fh = __fh;
1851 struct zoran *zr = fh->zr;
1853 memset(cap, 0, sizeof(*cap));
1854 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1855 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1856 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1857 pci_name(zr->pci_dev));
1858 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1860 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1861 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1865 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1867 unsigned int num, i;
1869 for (num = i = 0; i < NUM_FORMATS; i++) {
1870 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1871 strncpy(fmt->description, zoran_formats[i].name,
1872 sizeof(fmt->description) - 1);
1873 /* fmt struct pre-zeroed, so adding '\0' not neeed */
1874 fmt->pixelformat = zoran_formats[i].fourcc;
1875 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1876 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1883 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1884 struct v4l2_fmtdesc *f)
1886 struct zoran_fh *fh = __fh;
1887 struct zoran *zr = fh->zr;
1889 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1892 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1893 struct v4l2_fmtdesc *f)
1895 struct zoran_fh *fh = __fh;
1896 struct zoran *zr = fh->zr;
1898 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1901 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1902 struct v4l2_fmtdesc *f)
1904 struct zoran_fh *fh = __fh;
1905 struct zoran *zr = fh->zr;
1907 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1910 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1911 struct v4l2_format *fmt)
1913 struct zoran_fh *fh = __fh;
1914 struct zoran *zr = fh->zr;
1916 mutex_lock(&zr->resource_lock);
1918 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1919 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1920 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1921 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1922 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1923 if (fh->jpg_settings.TmpDcm == 1)
1924 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1925 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1927 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1928 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1929 fmt->fmt.pix.bytesperline = 0;
1930 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1932 mutex_unlock(&zr->resource_lock);
1936 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1937 struct v4l2_format *fmt)
1939 struct zoran_fh *fh = __fh;
1940 struct zoran *zr = fh->zr;
1942 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1943 return zoran_g_fmt_vid_out(file, fh, fmt);
1945 mutex_lock(&zr->resource_lock);
1946 fmt->fmt.pix.width = fh->v4l_settings.width;
1947 fmt->fmt.pix.height = fh->v4l_settings.height;
1948 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1949 fh->v4l_settings.height;
1950 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1951 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1952 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1953 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1954 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1956 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1957 mutex_unlock(&zr->resource_lock);
1961 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1962 struct v4l2_format *fmt)
1964 struct zoran_fh *fh = __fh;
1965 struct zoran *zr = fh->zr;
1967 mutex_lock(&zr->resource_lock);
1969 fmt->fmt.win.w.left = fh->overlay_settings.x;
1970 fmt->fmt.win.w.top = fh->overlay_settings.y;
1971 fmt->fmt.win.w.width = fh->overlay_settings.width;
1972 fmt->fmt.win.w.height = fh->overlay_settings.height;
1973 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1974 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1976 fmt->fmt.win.field = V4L2_FIELD_TOP;
1978 mutex_unlock(&zr->resource_lock);
1982 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1983 struct v4l2_format *fmt)
1985 struct zoran_fh *fh = __fh;
1986 struct zoran *zr = fh->zr;
1988 mutex_lock(&zr->resource_lock);
1990 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1991 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1992 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1993 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1994 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1995 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1996 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1997 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1999 mutex_unlock(&zr->resource_lock);
2003 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2004 struct v4l2_format *fmt)
2006 struct zoran_fh *fh = __fh;
2007 struct zoran *zr = fh->zr;
2008 struct zoran_jpg_settings settings;
2011 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2014 mutex_lock(&zr->resource_lock);
2015 settings = fh->jpg_settings;
2017 /* we actually need to set 'real' parameters now */
2018 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2019 settings.TmpDcm = 1;
2021 settings.TmpDcm = 2;
2022 settings.decimation = 0;
2023 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2024 settings.VerDcm = 2;
2026 settings.VerDcm = 1;
2027 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2028 settings.HorDcm = 4;
2029 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2030 settings.HorDcm = 2;
2032 settings.HorDcm = 1;
2033 if (settings.TmpDcm == 1)
2034 settings.field_per_buff = 2;
2036 settings.field_per_buff = 1;
2038 if (settings.HorDcm > 1) {
2039 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2040 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2043 settings.img_width = BUZ_MAX_WIDTH;
2047 res = zoran_check_jpg_settings(zr, &settings, 1);
2049 goto tryfmt_unlock_and_return;
2051 /* tell the user what we actually did */
2052 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2053 fmt->fmt.pix.height = settings.img_height * 2 /
2054 (settings.TmpDcm * settings.VerDcm);
2055 if (settings.TmpDcm == 1)
2056 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2057 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2059 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2060 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2062 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2063 fmt->fmt.pix.bytesperline = 0;
2064 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2065 tryfmt_unlock_and_return:
2066 mutex_unlock(&zr->resource_lock);
2070 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2071 struct v4l2_format *fmt)
2073 struct zoran_fh *fh = __fh;
2074 struct zoran *zr = fh->zr;
2078 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2079 return zoran_try_fmt_vid_out(file, fh, fmt);
2081 mutex_lock(&zr->resource_lock);
2083 for (i = 0; i < NUM_FORMATS; i++)
2084 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2087 if (i == NUM_FORMATS) {
2088 mutex_unlock(&zr->resource_lock);
2092 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
2093 v4l_bound_align_image(
2094 &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
2095 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
2096 mutex_unlock(&zr->resource_lock);
2101 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2102 struct v4l2_format *fmt)
2104 struct zoran_fh *fh = __fh;
2105 struct zoran *zr = fh->zr;
2108 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2109 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2110 fmt->fmt.win.w.width,
2111 fmt->fmt.win.w.height,
2112 fmt->fmt.win.clipcount,
2113 fmt->fmt.win.bitmap);
2114 mutex_lock(&zr->resource_lock);
2115 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2116 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
2117 (struct v4l2_clip __user *)fmt->fmt.win.clips,
2118 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
2119 mutex_unlock(&zr->resource_lock);
2123 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2124 struct v4l2_format *fmt)
2126 struct zoran_fh *fh = __fh;
2127 struct zoran *zr = fh->zr;
2128 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2129 struct zoran_jpg_settings settings;
2132 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2133 fmt->fmt.pix.width, fmt->fmt.pix.height,
2134 fmt->fmt.pix.pixelformat,
2135 (char *) &printformat);
2136 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2139 mutex_lock(&zr->resource_lock);
2141 if (fh->buffers.allocated) {
2142 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2145 goto sfmtjpg_unlock_and_return;
2148 settings = fh->jpg_settings;
2150 /* we actually need to set 'real' parameters now */
2151 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2152 settings.TmpDcm = 1;
2154 settings.TmpDcm = 2;
2155 settings.decimation = 0;
2156 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2157 settings.VerDcm = 2;
2159 settings.VerDcm = 1;
2160 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2161 settings.HorDcm = 4;
2162 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2163 settings.HorDcm = 2;
2165 settings.HorDcm = 1;
2166 if (settings.TmpDcm == 1)
2167 settings.field_per_buff = 2;
2169 settings.field_per_buff = 1;
2171 if (settings.HorDcm > 1) {
2172 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2173 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2176 settings.img_width = BUZ_MAX_WIDTH;
2180 res = zoran_check_jpg_settings(zr, &settings, 0);
2182 goto sfmtjpg_unlock_and_return;
2184 /* it's ok, so set them */
2185 fh->jpg_settings = settings;
2187 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2188 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2190 /* tell the user what we actually did */
2191 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2192 fmt->fmt.pix.height = settings.img_height * 2 /
2193 (settings.TmpDcm * settings.VerDcm);
2194 if (settings.TmpDcm == 1)
2195 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2196 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2198 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2199 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2200 fmt->fmt.pix.bytesperline = 0;
2201 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
2202 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2204 sfmtjpg_unlock_and_return:
2205 mutex_unlock(&zr->resource_lock);
2209 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2210 struct v4l2_format *fmt)
2212 struct zoran_fh *fh = __fh;
2213 struct zoran *zr = fh->zr;
2217 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2218 return zoran_s_fmt_vid_out(file, fh, fmt);
2220 for (i = 0; i < NUM_FORMATS; i++)
2221 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2223 if (i == NUM_FORMATS) {
2224 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2225 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2229 mutex_lock(&zr->resource_lock);
2231 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2232 fh->buffers.active != ZORAN_FREE) {
2233 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2236 goto sfmtv4l_unlock_and_return;
2238 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2239 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2240 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2241 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2245 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2248 goto sfmtv4l_unlock_and_return;
2250 /* tell the user the results/missing stuff */
2251 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2252 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2253 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2254 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2255 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2257 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2259 sfmtv4l_unlock_and_return:
2260 mutex_unlock(&zr->resource_lock);
2264 static int zoran_g_fbuf(struct file *file, void *__fh,
2265 struct v4l2_framebuffer *fb)
2267 struct zoran_fh *fh = __fh;
2268 struct zoran *zr = fh->zr;
2270 memset(fb, 0, sizeof(*fb));
2271 mutex_lock(&zr->resource_lock);
2272 fb->base = zr->vbuf_base;
2273 fb->fmt.width = zr->vbuf_width;
2274 fb->fmt.height = zr->vbuf_height;
2275 if (zr->overlay_settings.format)
2276 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2277 fb->fmt.bytesperline = zr->vbuf_bytesperline;
2278 mutex_unlock(&zr->resource_lock);
2279 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2280 fb->fmt.field = V4L2_FIELD_INTERLACED;
2281 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2282 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2287 static int zoran_s_fbuf(struct file *file, void *__fh,
2288 struct v4l2_framebuffer *fb)
2290 struct zoran_fh *fh = __fh;
2291 struct zoran *zr = fh->zr;
2293 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2295 for (i = 0; i < NUM_FORMATS; i++)
2296 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2298 if (i == NUM_FORMATS) {
2299 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2300 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2301 (char *)&printformat);
2305 mutex_lock(&zr->resource_lock);
2306 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2307 fb->fmt.height, fb->fmt.bytesperline);
2308 mutex_unlock(&zr->resource_lock);
2313 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2315 struct zoran_fh *fh = __fh;
2316 struct zoran *zr = fh->zr;
2319 mutex_lock(&zr->resource_lock);
2320 res = setup_overlay(fh, on);
2321 mutex_unlock(&zr->resource_lock);
2326 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2328 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2330 struct zoran_fh *fh = __fh;
2331 struct zoran *zr = fh->zr;
2334 if (req->memory != V4L2_MEMORY_MMAP) {
2337 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2338 ZR_DEVNAME(zr), req->memory);
2342 if (req->count == 0)
2343 return zoran_streamoff(file, fh, req->type);
2345 mutex_lock(&zr->resource_lock);
2346 if (fh->buffers.allocated) {
2349 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2352 goto v4l2reqbuf_unlock_and_return;
2355 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2356 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2357 /* control user input */
2360 if (req->count > v4l_nbufs)
2361 req->count = v4l_nbufs;
2363 /* The next mmap will map the V4L buffers */
2365 fh->buffers.num_buffers = req->count;
2367 if (v4l_fbuffer_alloc(fh)) {
2369 goto v4l2reqbuf_unlock_and_return;
2371 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2372 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2373 /* we need to calculate size ourselves now */
2376 if (req->count > jpg_nbufs)
2377 req->count = jpg_nbufs;
2379 /* The next mmap will map the MJPEG buffers */
2380 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2381 fh->buffers.num_buffers = req->count;
2382 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2384 if (jpg_fbuffer_alloc(fh)) {
2386 goto v4l2reqbuf_unlock_and_return;
2391 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2392 ZR_DEVNAME(zr), req->type);
2394 goto v4l2reqbuf_unlock_and_return;
2396 v4l2reqbuf_unlock_and_return:
2397 mutex_unlock(&zr->resource_lock);
2402 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2404 struct zoran_fh *fh = __fh;
2405 struct zoran *zr = fh->zr;
2408 mutex_lock(&zr->resource_lock);
2409 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2410 mutex_unlock(&zr->resource_lock);
2415 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2417 struct zoran_fh *fh = __fh;
2418 struct zoran *zr = fh->zr;
2419 int res = 0, codec_mode, buf_type;
2421 mutex_lock(&zr->resource_lock);
2423 switch (fh->map_mode) {
2424 case ZORAN_MAP_MODE_RAW:
2425 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2427 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2428 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2430 goto qbuf_unlock_and_return;
2433 res = zoran_v4l_queue_frame(fh, buf->index);
2435 goto qbuf_unlock_and_return;
2436 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2437 zr36057_set_memgrab(zr, 1);
2440 case ZORAN_MAP_MODE_JPG_REC:
2441 case ZORAN_MAP_MODE_JPG_PLAY:
2442 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2443 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2444 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2446 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2447 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2450 if (buf->type != buf_type) {
2452 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2453 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2455 goto qbuf_unlock_and_return;
2458 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
2460 goto qbuf_unlock_and_return;
2461 if (zr->codec_mode == BUZ_MODE_IDLE &&
2462 fh->buffers.active == ZORAN_LOCKED)
2463 zr36057_enable_jpg(zr, codec_mode);
2469 "%s: VIDIOC_QBUF - unsupported type %d\n",
2470 ZR_DEVNAME(zr), buf->type);
2474 qbuf_unlock_and_return:
2475 mutex_unlock(&zr->resource_lock);
2480 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2482 struct zoran_fh *fh = __fh;
2483 struct zoran *zr = fh->zr;
2484 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2486 mutex_lock(&zr->resource_lock);
2488 switch (fh->map_mode) {
2489 case ZORAN_MAP_MODE_RAW:
2490 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2492 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2493 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2495 goto dqbuf_unlock_and_return;
2498 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2499 if (file->f_flags & O_NONBLOCK &&
2500 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2502 goto dqbuf_unlock_and_return;
2504 res = v4l_sync(fh, num);
2506 goto dqbuf_unlock_and_return;
2507 zr->v4l_sync_tail++;
2508 res = zoran_v4l2_buffer_status(fh, buf, num);
2511 case ZORAN_MAP_MODE_JPG_REC:
2512 case ZORAN_MAP_MODE_JPG_PLAY:
2514 struct zoran_sync bs;
2516 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2517 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2519 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2521 if (buf->type != buf_type) {
2523 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2524 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2526 goto dqbuf_unlock_and_return;
2529 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2531 if (file->f_flags & O_NONBLOCK &&
2532 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2534 goto dqbuf_unlock_and_return;
2536 res = jpg_sync(fh, &bs);
2538 goto dqbuf_unlock_and_return;
2539 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2545 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2546 ZR_DEVNAME(zr), buf->type);
2550 dqbuf_unlock_and_return:
2551 mutex_unlock(&zr->resource_lock);
2556 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2558 struct zoran_fh *fh = __fh;
2559 struct zoran *zr = fh->zr;
2562 mutex_lock(&zr->resource_lock);
2564 switch (fh->map_mode) {
2565 case ZORAN_MAP_MODE_RAW: /* raw capture */
2566 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2567 fh->buffers.active != ZORAN_ACTIVE) {
2569 goto strmon_unlock_and_return;
2572 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2573 zr->v4l_settings = fh->v4l_settings;
2575 zr->v4l_sync_tail = zr->v4l_pend_tail;
2576 if (!zr->v4l_memgrab_active &&
2577 zr->v4l_pend_head != zr->v4l_pend_tail) {
2578 zr36057_set_memgrab(zr, 1);
2582 case ZORAN_MAP_MODE_JPG_REC:
2583 case ZORAN_MAP_MODE_JPG_PLAY:
2584 /* what is the codec mode right now? */
2585 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2586 fh->buffers.active != ZORAN_ACTIVE) {
2588 goto strmon_unlock_and_return;
2591 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2593 if (zr->jpg_que_head != zr->jpg_que_tail) {
2594 /* Start the jpeg codec when the first frame is queued */
2602 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2603 ZR_DEVNAME(zr), fh->map_mode);
2607 strmon_unlock_and_return:
2608 mutex_unlock(&zr->resource_lock);
2613 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2615 struct zoran_fh *fh = __fh;
2616 struct zoran *zr = fh->zr;
2618 unsigned long flags;
2620 mutex_lock(&zr->resource_lock);
2622 switch (fh->map_mode) {
2623 case ZORAN_MAP_MODE_RAW: /* raw capture */
2624 if (fh->buffers.active == ZORAN_FREE &&
2625 zr->v4l_buffers.active != ZORAN_FREE) {
2626 res = -EPERM; /* stay off other's settings! */
2627 goto strmoff_unlock_and_return;
2629 if (zr->v4l_buffers.active == ZORAN_FREE)
2630 goto strmoff_unlock_and_return;
2632 spin_lock_irqsave(&zr->spinlock, flags);
2633 /* unload capture */
2634 if (zr->v4l_memgrab_active) {
2636 zr36057_set_memgrab(zr, 0);
2639 for (i = 0; i < fh->buffers.num_buffers; i++)
2640 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2641 fh->buffers = zr->v4l_buffers;
2643 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2645 zr->v4l_grab_seq = 0;
2646 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2647 zr->v4l_sync_tail = 0;
2649 spin_unlock_irqrestore(&zr->spinlock, flags);
2653 case ZORAN_MAP_MODE_JPG_REC:
2654 case ZORAN_MAP_MODE_JPG_PLAY:
2655 if (fh->buffers.active == ZORAN_FREE &&
2656 zr->jpg_buffers.active != ZORAN_FREE) {
2657 res = -EPERM; /* stay off other's settings! */
2658 goto strmoff_unlock_and_return;
2660 if (zr->jpg_buffers.active == ZORAN_FREE)
2661 goto strmoff_unlock_and_return;
2663 res = jpg_qbuf(fh, -1,
2664 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2665 BUZ_MODE_MOTION_COMPRESS :
2666 BUZ_MODE_MOTION_DECOMPRESS);
2668 goto strmoff_unlock_and_return;
2672 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2673 ZR_DEVNAME(zr), fh->map_mode);
2677 strmoff_unlock_and_return:
2678 mutex_unlock(&zr->resource_lock);
2683 static int zoran_queryctrl(struct file *file, void *__fh,
2684 struct v4l2_queryctrl *ctrl)
2686 struct zoran_fh *fh = __fh;
2687 struct zoran *zr = fh->zr;
2689 /* we only support hue/saturation/contrast/brightness */
2690 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2691 ctrl->id > V4L2_CID_HUE)
2694 decoder_call(zr, core, queryctrl, ctrl);
2699 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2701 struct zoran_fh *fh = __fh;
2702 struct zoran *zr = fh->zr;
2704 /* we only support hue/saturation/contrast/brightness */
2705 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2706 ctrl->id > V4L2_CID_HUE)
2709 mutex_lock(&zr->resource_lock);
2710 decoder_call(zr, core, g_ctrl, ctrl);
2711 mutex_unlock(&zr->resource_lock);
2716 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2718 struct zoran_fh *fh = __fh;
2719 struct zoran *zr = fh->zr;
2721 /* we only support hue/saturation/contrast/brightness */
2722 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2723 ctrl->id > V4L2_CID_HUE)
2726 mutex_lock(&zr->resource_lock);
2727 decoder_call(zr, core, s_ctrl, ctrl);
2728 mutex_unlock(&zr->resource_lock);
2733 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2735 struct zoran_fh *fh = __fh;
2736 struct zoran *zr = fh->zr;
2738 mutex_lock(&zr->resource_lock);
2740 mutex_unlock(&zr->resource_lock);
2744 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2746 struct zoran_fh *fh = __fh;
2747 struct zoran *zr = fh->zr;
2750 mutex_lock(&zr->resource_lock);
2751 res = zoran_set_norm(zr, *std);
2753 goto sstd_unlock_and_return;
2755 res = wait_grab_pending(zr);
2756 sstd_unlock_and_return:
2757 mutex_unlock(&zr->resource_lock);
2761 static int zoran_enum_input(struct file *file, void *__fh,
2762 struct v4l2_input *inp)
2764 struct zoran_fh *fh = __fh;
2765 struct zoran *zr = fh->zr;
2767 if (inp->index < 0 || inp->index >= zr->card.inputs)
2770 int id = inp->index;
2771 memset(inp, 0, sizeof(*inp));
2775 strncpy(inp->name, zr->card.input[inp->index].name,
2776 sizeof(inp->name) - 1);
2777 inp->type = V4L2_INPUT_TYPE_CAMERA;
2778 inp->std = V4L2_STD_ALL;
2780 /* Get status of video decoder */
2781 mutex_lock(&zr->resource_lock);
2782 decoder_call(zr, video, g_input_status, &inp->status);
2783 mutex_unlock(&zr->resource_lock);
2787 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2789 struct zoran_fh *fh = __fh;
2790 struct zoran *zr = fh->zr;
2792 mutex_lock(&zr->resource_lock);
2794 mutex_unlock(&zr->resource_lock);
2799 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2801 struct zoran_fh *fh = __fh;
2802 struct zoran *zr = fh->zr;
2805 mutex_lock(&zr->resource_lock);
2806 res = zoran_set_input(zr, input);
2808 goto sinput_unlock_and_return;
2810 /* Make sure the changes come into effect */
2811 res = wait_grab_pending(zr);
2812 sinput_unlock_and_return:
2813 mutex_unlock(&zr->resource_lock);
2817 static int zoran_enum_output(struct file *file, void *__fh,
2818 struct v4l2_output *outp)
2820 if (outp->index != 0)
2823 memset(outp, 0, sizeof(*outp));
2825 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2826 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2831 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2838 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2846 /* cropping (sub-frame capture) */
2847 static int zoran_cropcap(struct file *file, void *__fh,
2848 struct v4l2_cropcap *cropcap)
2850 struct zoran_fh *fh = __fh;
2851 struct zoran *zr = fh->zr;
2852 int type = cropcap->type, res = 0;
2854 memset(cropcap, 0, sizeof(*cropcap));
2855 cropcap->type = type;
2857 mutex_lock(&zr->resource_lock);
2859 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2860 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2861 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2863 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2866 goto cropcap_unlock_and_return;
2869 cropcap->bounds.top = cropcap->bounds.left = 0;
2870 cropcap->bounds.width = BUZ_MAX_WIDTH;
2871 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2872 cropcap->defrect.top = cropcap->defrect.left = 0;
2873 cropcap->defrect.width = BUZ_MIN_WIDTH;
2874 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2875 cropcap_unlock_and_return:
2876 mutex_unlock(&zr->resource_lock);
2880 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2882 struct zoran_fh *fh = __fh;
2883 struct zoran *zr = fh->zr;
2884 int type = crop->type, res = 0;
2886 memset(crop, 0, sizeof(*crop));
2889 mutex_lock(&zr->resource_lock);
2891 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2892 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2893 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2896 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2899 goto gcrop_unlock_and_return;
2902 crop->c.top = fh->jpg_settings.img_y;
2903 crop->c.left = fh->jpg_settings.img_x;
2904 crop->c.width = fh->jpg_settings.img_width;
2905 crop->c.height = fh->jpg_settings.img_height;
2907 gcrop_unlock_and_return:
2908 mutex_unlock(&zr->resource_lock);
2913 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2915 struct zoran_fh *fh = __fh;
2916 struct zoran *zr = fh->zr;
2918 struct zoran_jpg_settings settings;
2920 settings = fh->jpg_settings;
2922 mutex_lock(&zr->resource_lock);
2924 if (fh->buffers.allocated) {
2926 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2929 goto scrop_unlock_and_return;
2932 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2933 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2934 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2936 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2939 goto scrop_unlock_and_return;
2942 /* move into a form that we understand */
2943 settings.img_x = crop->c.left;
2944 settings.img_y = crop->c.top;
2945 settings.img_width = crop->c.width;
2946 settings.img_height = crop->c.height;
2948 /* check validity */
2949 res = zoran_check_jpg_settings(zr, &settings, 0);
2951 goto scrop_unlock_and_return;
2954 fh->jpg_settings = settings;
2956 scrop_unlock_and_return:
2957 mutex_unlock(&zr->resource_lock);
2961 static int zoran_g_jpegcomp(struct file *file, void *__fh,
2962 struct v4l2_jpegcompression *params)
2964 struct zoran_fh *fh = __fh;
2965 struct zoran *zr = fh->zr;
2966 memset(params, 0, sizeof(*params));
2968 mutex_lock(&zr->resource_lock);
2970 params->quality = fh->jpg_settings.jpg_comp.quality;
2971 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2972 memcpy(params->APP_data,
2973 fh->jpg_settings.jpg_comp.APP_data,
2974 fh->jpg_settings.jpg_comp.APP_len);
2975 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2976 memcpy(params->COM_data,
2977 fh->jpg_settings.jpg_comp.COM_data,
2978 fh->jpg_settings.jpg_comp.COM_len);
2979 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2980 params->jpeg_markers =
2981 fh->jpg_settings.jpg_comp.jpeg_markers;
2983 mutex_unlock(&zr->resource_lock);
2988 static int zoran_s_jpegcomp(struct file *file, void *__fh,
2989 struct v4l2_jpegcompression *params)
2991 struct zoran_fh *fh = __fh;
2992 struct zoran *zr = fh->zr;
2994 struct zoran_jpg_settings settings;
2996 settings = fh->jpg_settings;
2998 settings.jpg_comp = *params;
3000 mutex_lock(&zr->resource_lock);
3002 if (fh->buffers.active != ZORAN_FREE) {
3003 dprintk(1, KERN_WARNING
3004 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3007 goto sjpegc_unlock_and_return;
3010 res = zoran_check_jpg_settings(zr, &settings, 0);
3012 goto sjpegc_unlock_and_return;
3013 if (!fh->buffers.allocated)
3014 fh->buffers.buffer_size =
3015 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3016 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3017 sjpegc_unlock_and_return:
3018 mutex_unlock(&zr->resource_lock);
3024 zoran_poll (struct file *file,
3027 struct zoran_fh *fh = file->private_data;
3028 struct zoran *zr = fh->zr;
3030 unsigned long flags;
3032 /* we should check whether buffers are ready to be synced on
3033 * (w/o waits - O_NONBLOCK) here
3034 * if ready for read (sync), return POLLIN|POLLRDNORM,
3035 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3036 * if error, return POLLERR,
3037 * if no buffers queued or so, return POLLNVAL
3040 mutex_lock(&zr->resource_lock);
3042 switch (fh->map_mode) {
3043 case ZORAN_MAP_MODE_RAW:
3044 poll_wait(file, &zr->v4l_capq, wait);
3045 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3047 spin_lock_irqsave(&zr->spinlock, flags);
3050 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3051 ZR_DEVNAME(zr), __func__,
3052 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
3053 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3054 zr->v4l_pend_tail, zr->v4l_pend_head);
3055 /* Process is the one capturing? */
3056 if (fh->buffers.active != ZORAN_FREE &&
3057 /* Buffer ready to DQBUF? */
3058 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3059 res = POLLIN | POLLRDNORM;
3060 spin_unlock_irqrestore(&zr->spinlock, flags);
3064 case ZORAN_MAP_MODE_JPG_REC:
3065 case ZORAN_MAP_MODE_JPG_PLAY:
3066 poll_wait(file, &zr->jpg_capq, wait);
3067 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3069 spin_lock_irqsave(&zr->spinlock, flags);
3072 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3073 ZR_DEVNAME(zr), __func__,
3074 "FAL"[fh->buffers.active], zr->jpg_que_tail,
3075 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3076 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3077 if (fh->buffers.active != ZORAN_FREE &&
3078 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3079 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3080 res = POLLIN | POLLRDNORM;
3082 res = POLLOUT | POLLWRNORM;
3084 spin_unlock_irqrestore(&zr->spinlock, flags);
3091 "%s: %s - internal error, unknown map_mode=%d\n",
3092 ZR_DEVNAME(zr), __func__, fh->map_mode);
3096 mutex_unlock(&zr->resource_lock);
3103 * This maps the buffers to user space.
3105 * Depending on the state of fh->map_mode
3106 * the V4L or the MJPEG buffers are mapped
3107 * per buffer or all together
3109 * Note that we need to connect to some
3110 * unmap signal event to unmap the de-allocate
3111 * the buffer accordingly (zoran_vm_close())
3115 zoran_vm_open (struct vm_area_struct *vma)
3117 struct zoran_mapping *map = vma->vm_private_data;
3123 zoran_vm_close (struct vm_area_struct *vma)
3125 struct zoran_mapping *map = vma->vm_private_data;
3126 struct zoran_fh *fh = map->file->private_data;
3127 struct zoran *zr = fh->zr;
3130 if (--map->count > 0)
3133 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3134 __func__, mode_name(fh->map_mode));
3136 for (i = 0; i < fh->buffers.num_buffers; i++) {
3137 if (fh->buffers.buffer[i].map == map)
3138 fh->buffers.buffer[i].map = NULL;
3142 /* Any buffers still mapped? */
3143 for (i = 0; i < fh->buffers.num_buffers; i++)
3144 if (fh->buffers.buffer[i].map)
3147 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3148 __func__, mode_name(fh->map_mode));
3150 mutex_lock(&zr->resource_lock);
3152 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3153 if (fh->buffers.active != ZORAN_FREE) {
3154 unsigned long flags;
3156 spin_lock_irqsave(&zr->spinlock, flags);
3157 zr36057_set_memgrab(zr, 0);
3158 zr->v4l_buffers.allocated = 0;
3159 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3160 spin_unlock_irqrestore(&zr->spinlock, flags);
3162 v4l_fbuffer_free(fh);
3164 if (fh->buffers.active != ZORAN_FREE) {
3165 jpg_qbuf(fh, -1, zr->codec_mode);
3166 zr->jpg_buffers.allocated = 0;
3167 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
3169 jpg_fbuffer_free(fh);
3172 mutex_unlock(&zr->resource_lock);
3175 static struct vm_operations_struct zoran_vm_ops = {
3176 .open = zoran_vm_open,
3177 .close = zoran_vm_close,
3181 zoran_mmap (struct file *file,
3182 struct vm_area_struct *vma)
3184 struct zoran_fh *fh = file->private_data;
3185 struct zoran *zr = fh->zr;
3186 unsigned long size = (vma->vm_end - vma->vm_start);
3187 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3189 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3191 struct zoran_mapping *map;
3195 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3196 ZR_DEVNAME(zr), __func__,
3197 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
3199 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3200 !(vma->vm_flags & VM_WRITE)) {
3203 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3204 ZR_DEVNAME(zr), __func__);
3208 mutex_lock(&zr->resource_lock);
3210 if (!fh->buffers.allocated) {
3213 "%s: %s(%s) - buffers not yet allocated\n",
3214 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
3216 goto mmap_unlock_and_return;
3219 first = offset / fh->buffers.buffer_size;
3220 last = first - 1 + size / fh->buffers.buffer_size;
3221 if (offset % fh->buffers.buffer_size != 0 ||
3222 size % fh->buffers.buffer_size != 0 || first < 0 ||
3223 last < 0 || first >= fh->buffers.num_buffers ||
3224 last >= fh->buffers.buffer_size) {
3227 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3228 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
3229 fh->buffers.buffer_size,
3230 fh->buffers.num_buffers);
3232 goto mmap_unlock_and_return;
3235 /* Check if any buffers are already mapped */
3236 for (i = first; i <= last; i++) {
3237 if (fh->buffers.buffer[i].map) {
3240 "%s: %s(%s) - buffer %d already mapped\n",
3241 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
3243 goto mmap_unlock_and_return;
3247 /* map these buffers */
3248 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3251 goto mmap_unlock_and_return;
3256 vma->vm_ops = &zoran_vm_ops;
3257 vma->vm_flags |= VM_DONTEXPAND;
3258 vma->vm_private_data = map;
3260 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3261 for (i = first; i <= last; i++) {
3263 if (todo > fh->buffers.buffer_size)
3264 todo = fh->buffers.buffer_size;
3265 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3266 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3267 todo, PAGE_SHARED)) {
3270 "%s: %s(V4L) - remap_pfn_range failed\n",
3271 ZR_DEVNAME(zr), __func__);
3273 goto mmap_unlock_and_return;
3277 fh->buffers.buffer[i].map = map;
3282 for (i = first; i <= last; i++) {
3284 j < fh->buffers.buffer_size / PAGE_SIZE;
3287 (le32_to_cpu(fh->buffers.buffer[i].jpg.
3288 frag_tab[2 * j + 1]) & ~1) << 1;
3293 le32_to_cpu(fh->buffers.
3294 buffer[i].jpg.frag_tab[2 * j]);
3295 /* should just be pos on i386 */
3296 page = virt_to_phys(bus_to_virt(pos))
3298 if (remap_pfn_range(vma, start, page,
3299 todo, PAGE_SHARED)) {
3302 "%s: %s(V4L) - remap_pfn_range failed\n",
3303 ZR_DEVNAME(zr), __func__);
3305 goto mmap_unlock_and_return;
3311 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
3312 frag_tab[2 * j + 1]) & 1)
3313 break; /* was last fragment */
3315 fh->buffers.buffer[i].map = map;
3322 mmap_unlock_and_return:
3323 mutex_unlock(&zr->resource_lock);
3328 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3329 .vidioc_querycap = zoran_querycap,
3330 .vidioc_cropcap = zoran_cropcap,
3331 .vidioc_s_crop = zoran_s_crop,
3332 .vidioc_g_crop = zoran_g_crop,
3333 .vidioc_enum_input = zoran_enum_input,
3334 .vidioc_g_input = zoran_g_input,
3335 .vidioc_s_input = zoran_s_input,
3336 .vidioc_enum_output = zoran_enum_output,
3337 .vidioc_g_output = zoran_g_output,
3338 .vidioc_s_output = zoran_s_output,
3339 .vidioc_g_fbuf = zoran_g_fbuf,
3340 .vidioc_s_fbuf = zoran_s_fbuf,
3341 .vidioc_g_std = zoran_g_std,
3342 .vidioc_s_std = zoran_s_std,
3343 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3344 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3345 .vidioc_overlay = zoran_overlay,
3346 .vidioc_reqbufs = zoran_reqbufs,
3347 .vidioc_querybuf = zoran_querybuf,
3348 .vidioc_qbuf = zoran_qbuf,
3349 .vidioc_dqbuf = zoran_dqbuf,
3350 .vidioc_streamon = zoran_streamon,
3351 .vidioc_streamoff = zoran_streamoff,
3352 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3353 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3354 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3355 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3356 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3357 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3358 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3359 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3360 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3361 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3362 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3363 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3364 .vidioc_queryctrl = zoran_queryctrl,
3365 .vidioc_s_ctrl = zoran_s_ctrl,
3366 .vidioc_g_ctrl = zoran_g_ctrl,
3367 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3368 .vidioc_default = zoran_default,
3369 .vidiocgmbuf = zoran_vidiocgmbuf,
3373 static const struct v4l2_file_operations zoran_fops = {
3374 .owner = THIS_MODULE,
3376 .release = zoran_close,
3377 .ioctl = video_ioctl2,
3379 .write = zoran_write,
3384 struct video_device zoran_template __devinitdata = {
3386 .fops = &zoran_fops,
3387 .ioctl_ops = &zoran_ioctl_ops,
3388 .release = &zoran_vdev_release,
3389 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,