• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/media/video/zoran/
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 *
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9 *
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11 *
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13 *
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 *
16 * Based on
17 *
18 * Miro DC10 driver
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20 *
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23 *
24 * buz.0.0.3
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26 *
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)
30 *
31 *
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.
36 *
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.
41 *
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.
45 */
46
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>
56
57#include <linux/interrupt.h>
58#include <linux/i2c.h>
59#include <linux/i2c-algo-bit.h>
60
61#include <linux/spinlock.h>
62
63#include <linux/videodev2.h>
64#include <media/v4l2-common.h>
65#include <media/v4l2-ioctl.h>
66#include "videocodec.h"
67
68#include <asm/byteorder.h>
69#include <asm/io.h>
70#include <asm/uaccess.h>
71#include <linux/proc_fs.h>
72
73#include <linux/mutex.h>
74#include "zoran.h"
75#include "zoran_device.h"
76#include "zoran_card.h"
77
78
79const struct zoran_format zoran_formats[] = {
80	{
81		.name = "15-bit RGB LE",
82		.fourcc = V4L2_PIX_FMT_RGB555,
83		.colorspace = V4L2_COLORSPACE_SRGB,
84		.depth = 15,
85		.flags = ZORAN_FORMAT_CAPTURE |
86			 ZORAN_FORMAT_OVERLAY,
87		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
88			   ZR36057_VFESPFR_LittleEndian,
89	}, {
90		.name = "15-bit RGB BE",
91		.fourcc = V4L2_PIX_FMT_RGB555X,
92		.colorspace = V4L2_COLORSPACE_SRGB,
93		.depth = 15,
94		.flags = ZORAN_FORMAT_CAPTURE |
95			 ZORAN_FORMAT_OVERLAY,
96		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
97	}, {
98		.name = "16-bit RGB LE",
99		.fourcc = V4L2_PIX_FMT_RGB565,
100		.colorspace = V4L2_COLORSPACE_SRGB,
101		.depth = 16,
102		.flags = ZORAN_FORMAT_CAPTURE |
103			 ZORAN_FORMAT_OVERLAY,
104		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
105			   ZR36057_VFESPFR_LittleEndian,
106	}, {
107		.name = "16-bit RGB BE",
108		.fourcc = V4L2_PIX_FMT_RGB565X,
109		.colorspace = V4L2_COLORSPACE_SRGB,
110		.depth = 16,
111		.flags = ZORAN_FORMAT_CAPTURE |
112			 ZORAN_FORMAT_OVERLAY,
113		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
114	}, {
115		.name = "24-bit RGB",
116		.fourcc = V4L2_PIX_FMT_BGR24,
117		.colorspace = V4L2_COLORSPACE_SRGB,
118		.depth = 24,
119		.flags = ZORAN_FORMAT_CAPTURE |
120			 ZORAN_FORMAT_OVERLAY,
121		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
122	}, {
123		.name = "32-bit RGB LE",
124		.fourcc = V4L2_PIX_FMT_BGR32,
125		.colorspace = V4L2_COLORSPACE_SRGB,
126		.depth = 32,
127		.flags = ZORAN_FORMAT_CAPTURE |
128			 ZORAN_FORMAT_OVERLAY,
129		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
130	}, {
131		.name = "32-bit RGB BE",
132		.fourcc = V4L2_PIX_FMT_RGB32,
133		.colorspace = V4L2_COLORSPACE_SRGB,
134		.depth = 32,
135		.flags = ZORAN_FORMAT_CAPTURE |
136			 ZORAN_FORMAT_OVERLAY,
137		.vfespfr = ZR36057_VFESPFR_RGB888,
138	}, {
139		.name = "4:2:2, packed, YUYV",
140		.fourcc = V4L2_PIX_FMT_YUYV,
141		.colorspace = V4L2_COLORSPACE_SMPTE170M,
142		.depth = 16,
143		.flags = ZORAN_FORMAT_CAPTURE |
144			 ZORAN_FORMAT_OVERLAY,
145		.vfespfr = ZR36057_VFESPFR_YUV422,
146	}, {
147		.name = "4:2:2, packed, UYVY",
148		.fourcc = V4L2_PIX_FMT_UYVY,
149		.colorspace = V4L2_COLORSPACE_SMPTE170M,
150		.depth = 16,
151		.flags = ZORAN_FORMAT_CAPTURE |
152			 ZORAN_FORMAT_OVERLAY,
153		.vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
154	}, {
155		.name = "Hardware-encoded Motion-JPEG",
156		.fourcc = V4L2_PIX_FMT_MJPEG,
157		.colorspace = V4L2_COLORSPACE_SMPTE170M,
158		.depth = 0,
159		.flags = ZORAN_FORMAT_CAPTURE |
160			 ZORAN_FORMAT_PLAYBACK |
161			 ZORAN_FORMAT_COMPRESSED,
162	}
163};
164#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
165
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 */
169static __u32
170zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
171{
172	__u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
173	__u32 num = (1024 * 512) / (div);
174	__u32 result = 2;
175
176	num--;
177	while (num) {
178		num >>= 1;
179		result <<= 1;
180	}
181
182	if (result > jpg_bufsize)
183		return jpg_bufsize;
184	if (result < 8192)
185		return 8192;
186	return result;
187}
188
189/* forward references */
190static void v4l_fbuffer_free(struct zoran_fh *fh);
191static void jpg_fbuffer_free(struct zoran_fh *fh);
192
193/* Set mapping mode */
194static void map_mode_raw(struct zoran_fh *fh)
195{
196	fh->map_mode = ZORAN_MAP_MODE_RAW;
197	fh->buffers.buffer_size = v4l_bufsize;
198	fh->buffers.num_buffers = v4l_nbufs;
199}
200static void map_mode_jpg(struct zoran_fh *fh, int play)
201{
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;
205}
206static inline const char *mode_name(enum zoran_map_mode mode)
207{
208	return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
209}
210
211/*
212 *   Allocate the V4L grab buffers
213 *
214 *   These have to be pysically contiguous.
215 */
216
217static int v4l_fbuffer_alloc(struct zoran_fh *fh)
218{
219	struct zoran *zr = fh->zr;
220	int i, off;
221	unsigned char *mem;
222
223	for (i = 0; i < fh->buffers.num_buffers; i++) {
224		if (fh->buffers.buffer[i].v4l.fbuffer)
225			dprintk(2,
226				KERN_WARNING
227				"%s: %s - buffer %d already allocated!?\n",
228				ZR_DEVNAME(zr), __func__, i);
229
230		//udelay(20);
231		mem = kmalloc(fh->buffers.buffer_size,
232			      GFP_KERNEL | __GFP_NOWARN);
233		if (!mem) {
234			dprintk(1,
235				KERN_ERR
236				"%s: %s - kmalloc for V4L buf %d failed\n",
237				ZR_DEVNAME(zr), __func__, i);
238			v4l_fbuffer_free(fh);
239			return -ENOBUFS;
240		}
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;
245		     off += PAGE_SIZE)
246			SetPageReserved(virt_to_page(mem + off));
247		dprintk(4,
248			KERN_INFO
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));
252	}
253
254	fh->buffers.allocated = 1;
255
256	return 0;
257}
258
259/* free the V4L grab buffers */
260static void v4l_fbuffer_free(struct zoran_fh *fh)
261{
262	struct zoran *zr = fh->zr;
263	int i, off;
264	unsigned char *mem;
265
266	dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
267
268	for (i = 0; i < fh->buffers.num_buffers; i++) {
269		if (!fh->buffers.buffer[i].v4l.fbuffer)
270			continue;
271
272		mem = fh->buffers.buffer[i].v4l.fbuffer;
273		for (off = 0; off < fh->buffers.buffer_size;
274		     off += PAGE_SIZE)
275			ClearPageReserved(virt_to_page(mem + off));
276		kfree(fh->buffers.buffer[i].v4l.fbuffer);
277		fh->buffers.buffer[i].v4l.fbuffer = NULL;
278	}
279
280	fh->buffers.allocated = 0;
281}
282
283/*
284 *   Allocate the MJPEG grab buffers.
285 *
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)
290 *
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.
298 *
299 *   Ben Capper:
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.
309 */
310
311static int jpg_fbuffer_alloc(struct zoran_fh *fh)
312{
313	struct zoran *zr = fh->zr;
314	int i, j, off;
315	u8 *mem;
316
317	for (i = 0; i < fh->buffers.num_buffers; i++) {
318		if (fh->buffers.buffer[i].jpg.frag_tab)
319			dprintk(2,
320				KERN_WARNING
321				"%s: %s - buffer %d already allocated!?\n",
322				ZR_DEVNAME(zr), __func__, i);
323
324		/* Allocate fragment table for this buffer */
325
326		mem = (void *)get_zeroed_page(GFP_KERNEL);
327		if (!mem) {
328			dprintk(1,
329				KERN_ERR
330				"%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
331				ZR_DEVNAME(zr), __func__, i);
332			jpg_fbuffer_free(fh);
333			return -ENOBUFS;
334		}
335		fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
336		fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
337
338		if (fh->buffers.need_contiguous) {
339			mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
340			if (mem == NULL) {
341				dprintk(1,
342					KERN_ERR
343					"%s: %s - kmalloc failed for buffer %d\n",
344					ZR_DEVNAME(zr), __func__, i);
345				jpg_fbuffer_free(fh);
346				return -ENOBUFS;
347			}
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));
354		} else {
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);
358				if (mem == NULL) {
359					dprintk(1,
360						KERN_ERR
361						"%s: %s - get_zeroed_page failed for buffer %d\n",
362						ZR_DEVNAME(zr), __func__, i);
363					jpg_fbuffer_free(fh);
364					return -ENOBUFS;
365				}
366
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));
372			}
373
374			fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
375		}
376	}
377
378	dprintk(4,
379		KERN_DEBUG "%s: %s - %d KB allocated\n",
380		ZR_DEVNAME(zr), __func__,
381		(fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
382
383	fh->buffers.allocated = 1;
384
385	return 0;
386}
387
388/* free the MJPEG grab buffers */
389static void jpg_fbuffer_free(struct zoran_fh *fh)
390{
391	struct zoran *zr = fh->zr;
392	int i, j, off;
393	unsigned char *mem;
394	__le32 frag_tab;
395	struct zoran_buffer *buffer;
396
397	dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
398
399	for (i = 0, buffer = &fh->buffers.buffer[0];
400	     i < fh->buffers.num_buffers; i++, buffer++) {
401		if (!buffer->jpg.frag_tab)
402			continue;
403
404		if (fh->buffers.need_contiguous) {
405			frag_tab = buffer->jpg.frag_tab[0];
406
407			if (frag_tab) {
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));
411				kfree(mem);
412				buffer->jpg.frag_tab[0] = 0;
413				buffer->jpg.frag_tab[1] = 0;
414			}
415		} else {
416			for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
417				frag_tab = buffer->jpg.frag_tab[2 * j];
418
419				if (!frag_tab)
420					break;
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;
425			}
426		}
427
428		free_page((unsigned long)buffer->jpg.frag_tab);
429		buffer->jpg.frag_tab = NULL;
430	}
431
432	fh->buffers.allocated = 0;
433}
434
435/*
436 *   V4L Buffer grabbing
437 */
438
439static int
440zoran_v4l_set_format (struct zoran_fh           *fh,
441		      int                        width,
442		      int                        height,
443		      const struct zoran_format *format)
444{
445	struct zoran *zr = fh->zr;
446	int bpp;
447
448	/* Check size and format of the grab wanted */
449
450	if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
451	    height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
452		dprintk(1,
453			KERN_ERR
454			"%s: %s - wrong frame size (%dx%d)\n",
455			ZR_DEVNAME(zr), __func__, width, height);
456		return -EINVAL;
457	}
458
459	bpp = (format->depth + 7) / 8;
460
461	/* Check against available buffer size */
462	if (height * width * bpp > fh->buffers.buffer_size) {
463		dprintk(1,
464			KERN_ERR
465			"%s: %s - video buffer size (%d kB) is too small\n",
466			ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
467		return -EINVAL;
468	}
469
470	/* The video front end needs 4-byte alinged line sizes */
471
472	if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
473		dprintk(1,
474			KERN_ERR
475			"%s: %s - wrong frame alignment\n",
476			ZR_DEVNAME(zr), __func__);
477		return -EINVAL;
478	}
479
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;
484
485	return 0;
486}
487
488static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
489{
490	struct zoran *zr = fh->zr;
491	unsigned long flags;
492	int res = 0;
493
494	if (!fh->buffers.allocated) {
495		dprintk(1,
496			KERN_ERR
497			"%s: %s - buffers not yet allocated\n",
498			ZR_DEVNAME(zr), __func__);
499		res = -ENOMEM;
500	}
501
502	/* No grabbing outside the buffer range! */
503	if (num >= fh->buffers.num_buffers || num < 0) {
504		dprintk(1,
505			KERN_ERR
506			"%s: %s - buffer %d is out of range\n",
507			ZR_DEVNAME(zr), __func__, num);
508		res = -EINVAL;
509	}
510
511	spin_lock_irqsave(&zr->spinlock, flags);
512
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;
517		} else {
518			dprintk(1,
519				KERN_ERR
520				"%s: %s - another session is already capturing\n",
521				ZR_DEVNAME(zr), __func__);
522			res = -EBUSY;
523		}
524	}
525
526	/* make sure a grab isn't going on currently with this buffer */
527	if (!res) {
528		switch (zr->v4l_buffers.buffer[num].state) {
529		default:
530		case BUZ_STATE_PEND:
531			if (zr->v4l_buffers.active == ZORAN_FREE) {
532				fh->buffers.active = ZORAN_FREE;
533				zr->v4l_buffers.allocated = 0;
534			}
535			res = -EBUSY;	/* what are you doing? */
536			break;
537		case BUZ_STATE_DONE:
538			dprintk(2,
539				KERN_WARNING
540				"%s: %s - queueing buffer %d in state DONE!?\n",
541				ZR_DEVNAME(zr), __func__, num);
542		case BUZ_STATE_USER:
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];
551			break;
552		}
553	}
554
555	spin_unlock_irqrestore(&zr->spinlock, flags);
556
557	if (!res && zr->v4l_buffers.active == ZORAN_FREE)
558		zr->v4l_buffers.active = fh->buffers.active;
559
560	return res;
561}
562
563/*
564 * Sync on a V4L buffer
565 */
566
567static int v4l_sync(struct zoran_fh *fh, int frame)
568{
569	struct zoran *zr = fh->zr;
570	unsigned long flags;
571
572	if (fh->buffers.active == ZORAN_FREE) {
573		dprintk(1,
574			KERN_ERR
575			"%s: %s - no grab active for this session\n",
576			ZR_DEVNAME(zr), __func__);
577		return -EINVAL;
578	}
579
580	/* check passed-in frame number */
581	if (frame >= fh->buffers.num_buffers || frame < 0) {
582		dprintk(1,
583			KERN_ERR "%s: %s - frame %d is invalid\n",
584			ZR_DEVNAME(zr), __func__, frame);
585		return -EINVAL;
586	}
587
588	/* Check if is buffer was queued at all */
589	if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
590		dprintk(1,
591			KERN_ERR
592			"%s: %s - attempt to sync on a buffer which was not queued?\n",
593			ZR_DEVNAME(zr), __func__);
594		return -EPROTO;
595	}
596
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))
600		return -ETIME;
601	if (signal_pending(current))
602		return -ERESTARTSYS;
603
604	/* buffer should now be in BUZ_STATE_DONE */
605	if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
606		dprintk(2,
607			KERN_ERR "%s: %s - internal state error\n",
608			ZR_DEVNAME(zr), __func__);
609
610	zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
611	fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
612
613	spin_lock_irqsave(&zr->spinlock, flags);
614
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;
621		}
622	}
623
624	spin_unlock_irqrestore(&zr->spinlock, flags);
625
626	return 0;
627}
628
629/*
630 *   Queue a MJPEG buffer for capture/playback
631 */
632
633static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
634				 enum zoran_codec_mode mode)
635{
636	struct zoran *zr = fh->zr;
637	unsigned long flags;
638	int res = 0;
639
640	/* Check if buffers are allocated */
641	if (!fh->buffers.allocated) {
642		dprintk(1,
643			KERN_ERR
644			"%s: %s - buffers not yet allocated\n",
645			ZR_DEVNAME(zr), __func__);
646		return -ENOMEM;
647	}
648
649	/* No grabbing outside the buffer range! */
650	if (num >= fh->buffers.num_buffers || num < 0) {
651		dprintk(1,
652			KERN_ERR
653			"%s: %s - buffer %d out of range\n",
654			ZR_DEVNAME(zr), __func__, num);
655		return -EINVAL;
656	}
657
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 */
663		dprintk(1,
664			KERN_ERR
665			"%s: %s - codec in wrong mode\n",
666			ZR_DEVNAME(zr), __func__);
667		return -EINVAL;
668	}
669
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;
674		} else {
675			dprintk(1,
676				KERN_ERR
677				"%s: %s - another session is already capturing\n",
678				ZR_DEVNAME(zr), __func__);
679			res = -EBUSY;
680		}
681	}
682
683	if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
684		/* Ok load up the jpeg codec */
685		zr36057_enable_jpg(zr, mode);
686	}
687
688	spin_lock_irqsave(&zr->spinlock, flags);
689
690	if (!res) {
691		switch (zr->jpg_buffers.buffer[num].state) {
692		case BUZ_STATE_DONE:
693			dprintk(2,
694				KERN_WARNING
695				"%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
696				ZR_DEVNAME(zr), __func__);
697		case BUZ_STATE_USER:
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);
704			break;
705		default:
706		case BUZ_STATE_DMA:
707		case BUZ_STATE_PEND:
708			if (zr->jpg_buffers.active == ZORAN_FREE) {
709				fh->buffers.active = ZORAN_FREE;
710				zr->jpg_buffers.allocated = 0;
711			}
712			res = -EBUSY;	/* what are you doing? */
713			break;
714		}
715	}
716
717	spin_unlock_irqrestore(&zr->spinlock, flags);
718
719	if (!res && zr->jpg_buffers.active == ZORAN_FREE)
720		zr->jpg_buffers.active = fh->buffers.active;
721
722	return res;
723}
724
725static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
726{
727	struct zoran *zr = fh->zr;
728	int res = 0;
729
730	/* Does the user want to stop streaming? */
731	if (frame < 0) {
732		if (zr->codec_mode == mode) {
733			if (fh->buffers.active == ZORAN_FREE) {
734				dprintk(1,
735					KERN_ERR
736					"%s: %s(-1) - session not active\n",
737					ZR_DEVNAME(zr), __func__);
738				return -EINVAL;
739			}
740			fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
741			zr->jpg_buffers.allocated = 0;
742			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
743			return 0;
744		} else {
745			dprintk(1,
746				KERN_ERR
747				"%s: %s - stop streaming but not in streaming mode\n",
748				ZR_DEVNAME(zr), __func__);
749			return -EINVAL;
750		}
751	}
752
753	if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
754		return res;
755
756	/* Start the jpeg codec when the first frame is queued  */
757	if (!res && zr->jpg_que_head == 1)
758		jpeg_start(zr);
759
760	return res;
761}
762
763/*
764 *   Sync on a MJPEG buffer
765 */
766
767static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
768{
769	struct zoran *zr = fh->zr;
770	unsigned long flags;
771	int frame;
772
773	if (fh->buffers.active == ZORAN_FREE) {
774		dprintk(1,
775			KERN_ERR
776			"%s: %s - capture is not currently active\n",
777			ZR_DEVNAME(zr), __func__);
778		return -EINVAL;
779	}
780	if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
781	    zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
782		dprintk(1,
783			KERN_ERR
784			"%s: %s - codec not in streaming mode\n",
785			ZR_DEVNAME(zr), __func__);
786		return -EINVAL;
787	}
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),
791			10*HZ)) {
792		int isr;
793
794		btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
795		udelay(1);
796		zr->codec->control(zr->codec, CODEC_G_STATUS,
797					   sizeof(isr), &isr);
798		dprintk(1,
799			KERN_ERR
800			"%s: %s - timeout: codec isr=0x%02x\n",
801			ZR_DEVNAME(zr), __func__, isr);
802
803		return -ETIME;
804
805	}
806	if (signal_pending(current))
807		return -ERESTARTSYS;
808
809	spin_lock_irqsave(&zr->spinlock, flags);
810
811	if (zr->jpg_dma_tail != zr->jpg_dma_head)
812		frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
813	else
814		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
815
816	/* buffer should now be in BUZ_STATE_DONE */
817	if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
818		dprintk(2,
819			KERN_ERR "%s: %s - internal state error\n",
820			ZR_DEVNAME(zr), __func__);
821
822	*bs = zr->jpg_buffers.buffer[frame].bs;
823	bs->frame = frame;
824	zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
825	fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
826
827	spin_unlock_irqrestore(&zr->spinlock, flags);
828
829	return 0;
830}
831
832static void zoran_open_init_session(struct zoran_fh *fh)
833{
834	int i;
835	struct zoran *zr = fh->zr;
836
837	/* Per default, map the V4L Buffers */
838	map_mode_raw(fh);
839
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;
845
846	/* v4l settings */
847	fh->v4l_settings = zr->v4l_settings;
848	/* jpg settings */
849	fh->jpg_settings = zr->jpg_settings;
850
851	/* buffers */
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;
856	}
857	fh->buffers.allocated = 0;
858	fh->buffers.active = ZORAN_FREE;
859}
860
861static void zoran_close_end_session(struct zoran_fh *fh)
862{
863	struct zoran *zr = fh->zr;
864
865	/* overlay */
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;
872	}
873
874	if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
875		/* v4l capture */
876		if (fh->buffers.active != ZORAN_FREE) {
877			unsigned long flags;
878
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);
884		}
885
886		/* v4l buffers */
887		if (fh->buffers.allocated)
888			v4l_fbuffer_free(fh);
889	} else {
890		/* jpg capture */
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;
895		}
896
897		/* jpg buffers */
898		if (fh->buffers.allocated)
899			jpg_fbuffer_free(fh);
900	}
901}
902
903/*
904 *   Open a zoran card. Right now the flags stuff is just playing
905 */
906
907static int zoran_open(struct file *file)
908{
909	struct zoran *zr = video_drvdata(file);
910	struct zoran_fh *fh;
911	int res, first_open = 0;
912
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);
915
916	lock_kernel();
917
918	if (zr->user >= 2048) {
919		dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
920			ZR_DEVNAME(zr), zr->user);
921		res = -EBUSY;
922		goto fail_unlock;
923	}
924
925	/* now, create the open()-specific file_ops struct */
926	fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
927	if (!fh) {
928		dprintk(1,
929			KERN_ERR
930			"%s: %s - allocation of zoran_fh failed\n",
931			ZR_DEVNAME(zr), __func__);
932		res = -ENOMEM;
933		goto fail_unlock;
934	}
935	/* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
936	 * on norm-change! */
937	fh->overlay_mask =
938	    kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
939	if (!fh->overlay_mask) {
940		dprintk(1,
941			KERN_ERR
942			"%s: %s - allocation of overlay_mask failed\n",
943			ZR_DEVNAME(zr), __func__);
944		res = -ENOMEM;
945		goto fail_fh;
946	}
947
948	if (zr->user++ == 0)
949		first_open = 1;
950
951	/*mutex_unlock(&zr->resource_lock);*/
952
953	/* default setup - TODO: look at flags */
954	if (first_open) {	/* First device open */
955		zr36057_restart(zr);
956		zoran_open_init_params(zr);
957		zoran_init_hardware(zr);
958
959		btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
960	}
961
962	/* set file_ops stuff */
963	file->private_data = fh;
964	fh->zr = zr;
965	zoran_open_init_session(fh);
966	unlock_kernel();
967
968	return 0;
969
970fail_fh:
971	kfree(fh);
972fail_unlock:
973	unlock_kernel();
974
975	dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
976		ZR_DEVNAME(zr), res, zr->user);
977
978	return res;
979}
980
981static int
982zoran_close(struct file  *file)
983{
984	struct zoran_fh *fh = file->private_data;
985	struct zoran *zr = fh->zr;
986
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);
989
990	/* kernel locks (fs/device.c), so don't do that ourselves
991	 * (prevents deadlocks) */
992	/*mutex_lock(&zr->resource_lock);*/
993
994	zoran_close_end_session(fh);
995
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;
1002
1003		/* disable interrupts */
1004		btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1005
1006		if (zr36067_debug > 1)
1007			print_interrupts(zr);
1008
1009		/* Overlay off */
1010		zr->v4l_overlay_active = 0;
1011		zr36057_overlay(zr, 0);
1012		zr->overlay_mask = NULL;
1013
1014		/* capture off */
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);
1020
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);
1024		}
1025	}
1026
1027	file->private_data = NULL;
1028	kfree(fh->overlay_mask);
1029	kfree(fh);
1030
1031	dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
1032
1033	return 0;
1034}
1035
1036
1037static ssize_t
1038zoran_read (struct file *file,
1039	    char        __user *data,
1040	    size_t       count,
1041	    loff_t      *ppos)
1042{
1043	/* we simply don't support read() (yet)... */
1044
1045	return -EINVAL;
1046}
1047
1048static ssize_t
1049zoran_write (struct file *file,
1050	     const char  __user *data,
1051	     size_t       count,
1052	     loff_t      *ppos)
1053{
1054	/* ...and the same goes for write() */
1055
1056	return -EINVAL;
1057}
1058
1059static int setup_fbuffer(struct zoran_fh *fh,
1060	       void                      *base,
1061	       const struct zoran_format *fmt,
1062	       int                        width,
1063	       int                        height,
1064	       int                        bytesperline)
1065{
1066	struct zoran *zr = fh->zr;
1067
1068	/* (Ronald) v4l/v4l2 guidelines */
1069	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1070		return -EPERM;
1071
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) */
1075
1076	if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1077		return -ENXIO;
1078
1079	/* we need a bytesperline value, even if not given */
1080	if (!bytesperline)
1081		bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1082
1083
1084	if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1085		dprintk(1,
1086			KERN_ERR
1087			"%s: %s - no valid overlay format given\n",
1088			ZR_DEVNAME(zr), __func__);
1089		return -EINVAL;
1090	}
1091	if (height <= 0 || width <= 0 || bytesperline <= 0) {
1092		dprintk(1,
1093			KERN_ERR
1094			"%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1095			ZR_DEVNAME(zr), __func__, width, height, bytesperline);
1096		return -EINVAL;
1097	}
1098	if (bytesperline & 3) {
1099		dprintk(1,
1100			KERN_ERR
1101			"%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1102			ZR_DEVNAME(zr), __func__, bytesperline);
1103		return -EINVAL;
1104	}
1105
1106	zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1107	zr->vbuf_height = height;
1108	zr->vbuf_width = width;
1109	zr->vbuf_depth = fmt->depth;
1110	zr->overlay_settings.format = fmt;
1111	zr->vbuf_bytesperline = bytesperline;
1112
1113	/* The user should set new window parameters */
1114	zr->overlay_settings.is_set = 0;
1115
1116	return 0;
1117}
1118
1119
1120static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
1121	struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
1122{
1123	struct zoran *zr = fh->zr;
1124	struct v4l2_clip *vcp = NULL;
1125	int on, end;
1126
1127
1128	if (!zr->vbuf_base) {
1129		dprintk(1,
1130			KERN_ERR
1131			"%s: %s - frame buffer has to be set first\n",
1132			ZR_DEVNAME(zr), __func__);
1133		return -EINVAL;
1134	}
1135
1136	if (!fh->overlay_settings.format) {
1137		dprintk(1,
1138			KERN_ERR
1139			"%s: %s - no overlay format set\n",
1140			ZR_DEVNAME(zr), __func__);
1141		return -EINVAL;
1142	}
1143
1144	/*
1145	 * The video front end needs 4-byte alinged line sizes, we correct that
1146	 * silently here if necessary
1147	 */
1148	if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1149		end = (x + width) & ~1;	/* round down */
1150		x = (x + 1) & ~1;	/* round up */
1151		width = end - x;
1152	}
1153
1154	if (zr->vbuf_depth == 24) {
1155		end = (x + width) & ~3;	/* round down */
1156		x = (x + 3) & ~3;	/* round up */
1157		width = end - x;
1158	}
1159
1160	if (width > BUZ_MAX_WIDTH)
1161		width = BUZ_MAX_WIDTH;
1162	if (height > BUZ_MAX_HEIGHT)
1163		height = BUZ_MAX_HEIGHT;
1164
1165	/* Check for vaild parameters */
1166	if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1167	    width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1168		dprintk(1,
1169			KERN_ERR
1170			"%s: %s - width = %d or height = %d invalid\n",
1171			ZR_DEVNAME(zr), __func__, width, height);
1172		return -EINVAL;
1173	}
1174
1175	fh->overlay_settings.x = x;
1176	fh->overlay_settings.y = y;
1177	fh->overlay_settings.width = width;
1178	fh->overlay_settings.height = height;
1179	fh->overlay_settings.clipcount = clipcount;
1180
1181	/*
1182	 * If an overlay is running, we have to switch it off
1183	 * and switch it on again in order to get the new settings in effect.
1184	 *
1185	 * We also want to avoid that the overlay mask is written
1186	 * when an overlay is running.
1187	 */
1188
1189	on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1190	    zr->overlay_active != ZORAN_FREE &&
1191	    fh->overlay_active != ZORAN_FREE;
1192	if (on)
1193		zr36057_overlay(zr, 0);
1194
1195	/*
1196	 *   Write the overlay mask if clips are wanted.
1197	 *   We prefer a bitmap.
1198	 */
1199	if (bitmap) {
1200		/* fake value - it just means we want clips */
1201		fh->overlay_settings.clipcount = 1;
1202
1203		if (copy_from_user(fh->overlay_mask, bitmap,
1204				   (width * height + 7) / 8)) {
1205			return -EFAULT;
1206		}
1207	} else if (clipcount > 0) {
1208		/* write our own bitmap from the clips */
1209		vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1210		if (vcp == NULL) {
1211			dprintk(1,
1212				KERN_ERR
1213				"%s: %s - Alloc of clip mask failed\n",
1214				ZR_DEVNAME(zr), __func__);
1215			return -ENOMEM;
1216		}
1217		if (copy_from_user
1218		    (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1219			vfree(vcp);
1220			return -EFAULT;
1221		}
1222		write_overlay_mask(fh, vcp, clipcount);
1223		vfree(vcp);
1224	}
1225
1226	fh->overlay_settings.is_set = 1;
1227	if (fh->overlay_active != ZORAN_FREE &&
1228	    zr->overlay_active != ZORAN_FREE)
1229		zr->overlay_settings = fh->overlay_settings;
1230
1231	if (on)
1232		zr36057_overlay(zr, 1);
1233
1234	/* Make sure the changes come into effect */
1235	return wait_grab_pending(zr);
1236}
1237
1238static int setup_overlay(struct zoran_fh *fh, int on)
1239{
1240	struct zoran *zr = fh->zr;
1241
1242	/* If there is nothing to do, return immediatly */
1243	if ((on && fh->overlay_active != ZORAN_FREE) ||
1244	    (!on && fh->overlay_active == ZORAN_FREE))
1245		return 0;
1246
1247	/* check whether we're touching someone else's overlay */
1248	if (on && zr->overlay_active != ZORAN_FREE &&
1249	    fh->overlay_active == ZORAN_FREE) {
1250		dprintk(1,
1251			KERN_ERR
1252			"%s: %s - overlay is already active for another session\n",
1253			ZR_DEVNAME(zr), __func__);
1254		return -EBUSY;
1255	}
1256	if (!on && zr->overlay_active != ZORAN_FREE &&
1257	    fh->overlay_active == ZORAN_FREE) {
1258		dprintk(1,
1259			KERN_ERR
1260			"%s: %s - you cannot cancel someone else's session\n",
1261			ZR_DEVNAME(zr), __func__);
1262		return -EPERM;
1263	}
1264
1265	if (on == 0) {
1266		zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1267		zr->v4l_overlay_active = 0;
1268		/* When a grab is running, the video simply
1269		 * won't be switched on any more */
1270		if (!zr->v4l_memgrab_active)
1271			zr36057_overlay(zr, 0);
1272		zr->overlay_mask = NULL;
1273	} else {
1274		if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1275			dprintk(1,
1276				KERN_ERR
1277				"%s: %s - buffer or window not set\n",
1278				ZR_DEVNAME(zr), __func__);
1279			return -EINVAL;
1280		}
1281		if (!fh->overlay_settings.format) {
1282			dprintk(1,
1283				KERN_ERR
1284				"%s: %s - no overlay format set\n",
1285				ZR_DEVNAME(zr), __func__);
1286			return -EINVAL;
1287		}
1288		zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1289		zr->v4l_overlay_active = 1;
1290		zr->overlay_mask = fh->overlay_mask;
1291		zr->overlay_settings = fh->overlay_settings;
1292		if (!zr->v4l_memgrab_active)
1293			zr36057_overlay(zr, 1);
1294		/* When a grab is running, the video will be
1295		 * switched on when grab is finished */
1296	}
1297
1298	/* Make sure the changes come into effect */
1299	return wait_grab_pending(zr);
1300}
1301
1302/* get the status of a buffer in the clients buffer queue */
1303static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1304				    struct v4l2_buffer *buf, int num)
1305{
1306	struct zoran *zr = fh->zr;
1307	unsigned long flags;
1308
1309	buf->flags = V4L2_BUF_FLAG_MAPPED;
1310
1311	switch (fh->map_mode) {
1312	case ZORAN_MAP_MODE_RAW:
1313		/* check range */
1314		if (num < 0 || num >= fh->buffers.num_buffers ||
1315		    !fh->buffers.allocated) {
1316			dprintk(1,
1317				KERN_ERR
1318				"%s: %s - wrong number or buffers not allocated\n",
1319				ZR_DEVNAME(zr), __func__);
1320			return -EINVAL;
1321		}
1322
1323		spin_lock_irqsave(&zr->spinlock, flags);
1324		dprintk(3,
1325			KERN_DEBUG
1326			"%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1327			ZR_DEVNAME(zr), __func__,
1328			"FAL"[fh->buffers.active], num,
1329			"UPMD"[zr->v4l_buffers.buffer[num].state],
1330			fh->buffers.buffer[num].map ? 'Y' : 'N');
1331		spin_unlock_irqrestore(&zr->spinlock, flags);
1332
1333		buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1334		buf->length = fh->buffers.buffer_size;
1335
1336		/* get buffer */
1337		buf->bytesused = fh->buffers.buffer[num].bs.length;
1338		if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1339		    fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1340			buf->sequence = fh->buffers.buffer[num].bs.seq;
1341			buf->flags |= V4L2_BUF_FLAG_DONE;
1342			buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1343		} else {
1344			buf->flags |= V4L2_BUF_FLAG_QUEUED;
1345		}
1346
1347		if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1348			buf->field = V4L2_FIELD_TOP;
1349		else
1350			buf->field = V4L2_FIELD_INTERLACED;
1351
1352		break;
1353
1354	case ZORAN_MAP_MODE_JPG_REC:
1355	case ZORAN_MAP_MODE_JPG_PLAY:
1356
1357		/* check range */
1358		if (num < 0 || num >= fh->buffers.num_buffers ||
1359		    !fh->buffers.allocated) {
1360			dprintk(1,
1361				KERN_ERR
1362				"%s: %s - wrong number or buffers not allocated\n",
1363				ZR_DEVNAME(zr), __func__);
1364			return -EINVAL;
1365		}
1366
1367		buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1368			      V4L2_BUF_TYPE_VIDEO_CAPTURE :
1369			      V4L2_BUF_TYPE_VIDEO_OUTPUT;
1370		buf->length = fh->buffers.buffer_size;
1371
1372		/* these variables are only written after frame has been captured */
1373		if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1374		    fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1375			buf->sequence = fh->buffers.buffer[num].bs.seq;
1376			buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1377			buf->bytesused = fh->buffers.buffer[num].bs.length;
1378			buf->flags |= V4L2_BUF_FLAG_DONE;
1379		} else {
1380			buf->flags |= V4L2_BUF_FLAG_QUEUED;
1381		}
1382
1383		/* which fields are these? */
1384		if (fh->jpg_settings.TmpDcm != 1)
1385			buf->field = fh->jpg_settings.odd_even ?
1386				V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1387		else
1388			buf->field = fh->jpg_settings.odd_even ?
1389				V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1390
1391		break;
1392
1393	default:
1394
1395		dprintk(5,
1396			KERN_ERR
1397			"%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1398			ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
1399		return -EINVAL;
1400	}
1401
1402	buf->memory = V4L2_MEMORY_MMAP;
1403	buf->index = num;
1404	buf->m.offset = buf->length * num;
1405
1406	return 0;
1407}
1408
1409static int
1410zoran_set_norm (struct zoran *zr,
1411		v4l2_std_id norm)
1412{
1413	int on;
1414
1415	if (zr->v4l_buffers.active != ZORAN_FREE ||
1416	    zr->jpg_buffers.active != ZORAN_FREE) {
1417		dprintk(1,
1418			KERN_WARNING
1419			"%s: %s called while in playback/capture mode\n",
1420			ZR_DEVNAME(zr), __func__);
1421		return -EBUSY;
1422	}
1423
1424	if (!(norm & zr->card.norms)) {
1425		dprintk(1,
1426			KERN_ERR "%s: %s - unsupported norm %llx\n",
1427			ZR_DEVNAME(zr), __func__, norm);
1428		return -EINVAL;
1429	}
1430
1431	if (norm == V4L2_STD_ALL) {
1432		unsigned int status = 0;
1433		v4l2_std_id std = 0;
1434
1435		decoder_call(zr, video, querystd, &std);
1436		decoder_call(zr, core, s_std, std);
1437
1438		/* let changes come into effect */
1439		ssleep(2);
1440
1441		decoder_call(zr, video, g_input_status, &status);
1442		if (status & V4L2_IN_ST_NO_SIGNAL) {
1443			dprintk(1,
1444				KERN_ERR
1445				"%s: %s - no norm detected\n",
1446				ZR_DEVNAME(zr), __func__);
1447			/* reset norm */
1448			decoder_call(zr, core, s_std, zr->norm);
1449			return -EIO;
1450		}
1451
1452		norm = std;
1453	}
1454	if (norm & V4L2_STD_SECAM)
1455		zr->timing = zr->card.tvn[2];
1456	else if (norm & V4L2_STD_NTSC)
1457		zr->timing = zr->card.tvn[1];
1458	else
1459		zr->timing = zr->card.tvn[0];
1460
1461	/* We switch overlay off and on since a change in the
1462	 * norm needs different VFE settings */
1463	on = zr->overlay_active && !zr->v4l_memgrab_active;
1464	if (on)
1465		zr36057_overlay(zr, 0);
1466
1467	decoder_call(zr, core, s_std, norm);
1468	encoder_call(zr, video, s_std_output, norm);
1469
1470	if (on)
1471		zr36057_overlay(zr, 1);
1472
1473	/* Make sure the changes come into effect */
1474	zr->norm = norm;
1475
1476	return 0;
1477}
1478
1479static int
1480zoran_set_input (struct zoran *zr,
1481		 int           input)
1482{
1483	if (input == zr->input) {
1484		return 0;
1485	}
1486
1487	if (zr->v4l_buffers.active != ZORAN_FREE ||
1488	    zr->jpg_buffers.active != ZORAN_FREE) {
1489		dprintk(1,
1490			KERN_WARNING
1491			"%s: %s called while in playback/capture mode\n",
1492			ZR_DEVNAME(zr), __func__);
1493		return -EBUSY;
1494	}
1495
1496	if (input < 0 || input >= zr->card.inputs) {
1497		dprintk(1,
1498			KERN_ERR
1499			"%s: %s - unnsupported input %d\n",
1500			ZR_DEVNAME(zr), __func__, input);
1501		return -EINVAL;
1502	}
1503
1504	zr->input = input;
1505
1506	decoder_call(zr, video, s_routing,
1507			zr->card.input[input].muxsel, 0, 0);
1508
1509	return 0;
1510}
1511
1512/*
1513 *   ioctl routine
1514 */
1515
1516#ifdef CONFIG_VIDEO_V4L1_COMPAT
1517static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1518{
1519	struct zoran_fh *fh = __fh;
1520	struct zoran *zr = fh->zr;
1521	struct zoran_jpg_settings settings;
1522
1523	switch (cmd) {
1524	case BUZIOC_G_PARAMS:
1525	{
1526		struct zoran_params *bparams = arg;
1527
1528		dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1529
1530		memset(bparams, 0, sizeof(struct zoran_params));
1531		bparams->major_version = MAJOR_VERSION;
1532		bparams->minor_version = MINOR_VERSION;
1533
1534		mutex_lock(&zr->resource_lock);
1535
1536		if (zr->norm & V4L2_STD_NTSC)
1537			bparams->norm = ZORAN_VIDMODE_NTSC;
1538		else if (zr->norm & V4L2_STD_SECAM)
1539			bparams->norm = ZORAN_VIDMODE_SECAM;
1540		else
1541			bparams->norm = ZORAN_VIDMODE_PAL;
1542
1543		bparams->input = zr->input;
1544
1545		bparams->decimation = fh->jpg_settings.decimation;
1546		bparams->HorDcm = fh->jpg_settings.HorDcm;
1547		bparams->VerDcm = fh->jpg_settings.VerDcm;
1548		bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1549		bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1550		bparams->img_x = fh->jpg_settings.img_x;
1551		bparams->img_y = fh->jpg_settings.img_y;
1552		bparams->img_width = fh->jpg_settings.img_width;
1553		bparams->img_height = fh->jpg_settings.img_height;
1554		bparams->odd_even = fh->jpg_settings.odd_even;
1555
1556		bparams->quality = fh->jpg_settings.jpg_comp.quality;
1557		bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1558		bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1559		memcpy(bparams->APP_data,
1560		       fh->jpg_settings.jpg_comp.APP_data,
1561		       sizeof(bparams->APP_data));
1562		bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1563		memcpy(bparams->COM_data,
1564		       fh->jpg_settings.jpg_comp.COM_data,
1565		       sizeof(bparams->COM_data));
1566		bparams->jpeg_markers =
1567		    fh->jpg_settings.jpg_comp.jpeg_markers;
1568
1569		mutex_unlock(&zr->resource_lock);
1570
1571		bparams->VFIFO_FB = 0;
1572
1573		return 0;
1574	}
1575
1576	case BUZIOC_S_PARAMS:
1577	{
1578		struct zoran_params *bparams = arg;
1579		int res = 0;
1580
1581		dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1582
1583		settings.decimation = bparams->decimation;
1584		settings.HorDcm = bparams->HorDcm;
1585		settings.VerDcm = bparams->VerDcm;
1586		settings.TmpDcm = bparams->TmpDcm;
1587		settings.field_per_buff = bparams->field_per_buff;
1588		settings.img_x = bparams->img_x;
1589		settings.img_y = bparams->img_y;
1590		settings.img_width = bparams->img_width;
1591		settings.img_height = bparams->img_height;
1592		settings.odd_even = bparams->odd_even;
1593
1594		settings.jpg_comp.quality = bparams->quality;
1595		settings.jpg_comp.APPn = bparams->APPn;
1596		settings.jpg_comp.APP_len = bparams->APP_len;
1597		memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1598		       sizeof(bparams->APP_data));
1599		settings.jpg_comp.COM_len = bparams->COM_len;
1600		memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1601		       sizeof(bparams->COM_data));
1602		settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1603
1604		mutex_lock(&zr->resource_lock);
1605
1606		if (zr->codec_mode != BUZ_MODE_IDLE) {
1607			dprintk(1,
1608				KERN_ERR
1609				"%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1610				ZR_DEVNAME(zr));
1611			res = -EINVAL;
1612			goto sparams_unlock_and_return;
1613		}
1614
1615		/* Check the params first before overwriting our
1616		 * nternal values */
1617		if (zoran_check_jpg_settings(zr, &settings, 0)) {
1618			res = -EINVAL;
1619			goto sparams_unlock_and_return;
1620		}
1621
1622		fh->jpg_settings = settings;
1623sparams_unlock_and_return:
1624		mutex_unlock(&zr->resource_lock);
1625
1626		return res;
1627	}
1628
1629	case BUZIOC_REQBUFS:
1630	{
1631		struct zoran_requestbuffers *breq = arg;
1632		int res = 0;
1633
1634		dprintk(3,
1635			KERN_DEBUG
1636			"%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1637			ZR_DEVNAME(zr), breq->count, breq->size);
1638
1639		/* Enforce reasonable lower and upper limits */
1640		if (breq->count < 4)
1641			breq->count = 4;	/* Could be choosen smaller */
1642		if (breq->count > jpg_nbufs)
1643			breq->count = jpg_nbufs;
1644		breq->size = PAGE_ALIGN(breq->size);
1645		if (breq->size < 8192)
1646			breq->size = 8192;	/* Arbitrary */
1647		/* breq->size is limited by 1 page for the stat_com
1648		 * tables to a Maximum of 2 MB */
1649		if (breq->size > jpg_bufsize)
1650			breq->size = jpg_bufsize;
1651
1652		mutex_lock(&zr->resource_lock);
1653
1654		if (fh->buffers.allocated) {
1655			dprintk(1,
1656				KERN_ERR
1657				"%s: BUZIOC_REQBUFS - buffers already allocated\n",
1658				ZR_DEVNAME(zr));
1659			res = -EBUSY;
1660			goto jpgreqbuf_unlock_and_return;
1661		}
1662
1663		/* The next mmap will map the MJPEG buffers - could
1664		 * also be *_PLAY, but it doesn't matter here */
1665		map_mode_jpg(fh, 0);
1666		fh->buffers.num_buffers = breq->count;
1667		fh->buffers.buffer_size = breq->size;
1668
1669		if (jpg_fbuffer_alloc(fh)) {
1670			res = -ENOMEM;
1671			goto jpgreqbuf_unlock_and_return;
1672		}
1673
1674jpgreqbuf_unlock_and_return:
1675		mutex_unlock(&zr->resource_lock);
1676
1677		return res;
1678	}
1679
1680	case BUZIOC_QBUF_CAPT:
1681	{
1682		int *frame = arg, res;
1683
1684		dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1685			ZR_DEVNAME(zr), *frame);
1686
1687		mutex_lock(&zr->resource_lock);
1688		res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
1689		mutex_unlock(&zr->resource_lock);
1690
1691		return res;
1692	}
1693
1694	case BUZIOC_QBUF_PLAY:
1695	{
1696		int *frame = arg, res;
1697
1698		dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1699			ZR_DEVNAME(zr), *frame);
1700
1701		mutex_lock(&zr->resource_lock);
1702		res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1703		mutex_unlock(&zr->resource_lock);
1704
1705		return res;
1706	}
1707
1708	case BUZIOC_SYNC:
1709	{
1710		struct zoran_sync *bsync = arg;
1711		int res;
1712
1713		dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1714
1715		mutex_lock(&zr->resource_lock);
1716
1717		if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1718			dprintk(2, KERN_WARNING
1719				"%s: %s - not in jpg capture mode\n",
1720				ZR_DEVNAME(zr), __func__);
1721			res = -EINVAL;
1722		} else {
1723			res = jpg_sync(fh, bsync);
1724		}
1725		mutex_unlock(&zr->resource_lock);
1726
1727		return res;
1728	}
1729
1730	case BUZIOC_G_STATUS:
1731	{
1732		struct zoran_status *bstat = arg;
1733		int status = 0, res = 0;
1734		v4l2_std_id norm;
1735
1736		dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1737
1738		if (zr->codec_mode != BUZ_MODE_IDLE) {
1739			dprintk(1,
1740				KERN_ERR
1741				"%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1742				ZR_DEVNAME(zr));
1743			return -EINVAL;
1744		}
1745
1746		mutex_lock(&zr->resource_lock);
1747
1748		if (zr->codec_mode != BUZ_MODE_IDLE) {
1749			dprintk(1,
1750				KERN_ERR
1751				"%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1752				ZR_DEVNAME(zr));
1753			res = -EINVAL;
1754			goto gstat_unlock_and_return;
1755		}
1756
1757		decoder_call(zr, video, s_routing,
1758				zr->card.input[bstat->input].muxsel, 0, 0);
1759
1760		/* sleep 1 second */
1761		ssleep(1);
1762
1763		/* Get status of video decoder */
1764		decoder_call(zr, video, querystd, &norm);
1765		decoder_call(zr, video, g_input_status, &status);
1766
1767		/* restore previous input and norm */
1768		decoder_call(zr, video, s_routing,
1769				zr->card.input[zr->input].muxsel, 0, 0);
1770gstat_unlock_and_return:
1771		mutex_unlock(&zr->resource_lock);
1772
1773		if (!res) {
1774			bstat->signal =
1775			    (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1776			if (norm & V4L2_STD_NTSC)
1777				bstat->norm = ZORAN_VIDMODE_NTSC;
1778			else if (norm & V4L2_STD_SECAM)
1779				bstat->norm = ZORAN_VIDMODE_SECAM;
1780			else
1781				bstat->norm = ZORAN_VIDMODE_PAL;
1782
1783			bstat->color =
1784			    (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1785		}
1786
1787		return res;
1788	}
1789
1790	default:
1791		return -EINVAL;
1792	}
1793}
1794
1795static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1796{
1797	struct zoran_fh *fh = __fh;
1798	struct zoran *zr = fh->zr;
1799	int i, res = 0;
1800
1801
1802	mutex_lock(&zr->resource_lock);
1803
1804	if (fh->buffers.allocated) {
1805		dprintk(1,
1806			KERN_ERR
1807			"%s: VIDIOCGMBUF - buffers already allocated\n",
1808			ZR_DEVNAME(zr));
1809		res = -EINVAL;
1810		goto v4l1reqbuf_unlock_and_return;
1811	}
1812
1813	/* The next mmap will map the V4L buffers */
1814	map_mode_raw(fh);
1815
1816	if (v4l_fbuffer_alloc(fh)) {
1817		res = -ENOMEM;
1818		goto v4l1reqbuf_unlock_and_return;
1819	}
1820
1821	vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1822	vmbuf->frames = fh->buffers.num_buffers;
1823	for (i = 0; i < vmbuf->frames; i++)
1824		vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1825
1826v4l1reqbuf_unlock_and_return:
1827	mutex_unlock(&zr->resource_lock);
1828
1829	return res;
1830}
1831#endif
1832
1833static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1834{
1835	struct zoran_fh *fh = __fh;
1836	struct zoran *zr = fh->zr;
1837
1838	memset(cap, 0, sizeof(*cap));
1839	strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1840	strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1841	snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1842		 pci_name(zr->pci_dev));
1843	cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1844			   RELEASE_VERSION);
1845	cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1846			    V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1847	return 0;
1848}
1849
1850static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1851{
1852	unsigned int num, i;
1853
1854	for (num = i = 0; i < NUM_FORMATS; i++) {
1855		if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1856			strncpy(fmt->description, zoran_formats[i].name,
1857				sizeof(fmt->description) - 1);
1858			/* fmt struct pre-zeroed, so adding '\0' not neeed */
1859			fmt->pixelformat = zoran_formats[i].fourcc;
1860			if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1861				fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1862			return 0;
1863		}
1864	}
1865	return -EINVAL;
1866}
1867
1868static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1869					    struct v4l2_fmtdesc *f)
1870{
1871	struct zoran_fh *fh = __fh;
1872	struct zoran *zr = fh->zr;
1873
1874	return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1875}
1876
1877static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1878					    struct v4l2_fmtdesc *f)
1879{
1880	struct zoran_fh *fh = __fh;
1881	struct zoran *zr = fh->zr;
1882
1883	return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1884}
1885
1886static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1887					    struct v4l2_fmtdesc *f)
1888{
1889	struct zoran_fh *fh = __fh;
1890	struct zoran *zr = fh->zr;
1891
1892	return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1893}
1894
1895static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1896					struct v4l2_format *fmt)
1897{
1898	struct zoran_fh *fh = __fh;
1899	struct zoran *zr = fh->zr;
1900
1901	mutex_lock(&zr->resource_lock);
1902
1903	fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1904	fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1905		(fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1906	fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1907	fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1908	if (fh->jpg_settings.TmpDcm == 1)
1909		fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1910				V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1911	else
1912		fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1913				V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1914	fmt->fmt.pix.bytesperline = 0;
1915	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1916
1917	mutex_unlock(&zr->resource_lock);
1918	return 0;
1919}
1920
1921static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1922					struct v4l2_format *fmt)
1923{
1924	struct zoran_fh *fh = __fh;
1925	struct zoran *zr = fh->zr;
1926
1927	if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1928		return zoran_g_fmt_vid_out(file, fh, fmt);
1929
1930	mutex_lock(&zr->resource_lock);
1931	fmt->fmt.pix.width = fh->v4l_settings.width;
1932	fmt->fmt.pix.height = fh->v4l_settings.height;
1933	fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1934					fh->v4l_settings.height;
1935	fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1936	fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1937	fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1938	if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1939		fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1940	else
1941		fmt->fmt.pix.field = V4L2_FIELD_TOP;
1942	mutex_unlock(&zr->resource_lock);
1943	return 0;
1944}
1945
1946static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1947					struct v4l2_format *fmt)
1948{
1949	struct zoran_fh *fh = __fh;
1950	struct zoran *zr = fh->zr;
1951
1952	mutex_lock(&zr->resource_lock);
1953
1954	fmt->fmt.win.w.left = fh->overlay_settings.x;
1955	fmt->fmt.win.w.top = fh->overlay_settings.y;
1956	fmt->fmt.win.w.width = fh->overlay_settings.width;
1957	fmt->fmt.win.w.height = fh->overlay_settings.height;
1958	if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1959		fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1960	else
1961		fmt->fmt.win.field = V4L2_FIELD_TOP;
1962
1963	mutex_unlock(&zr->resource_lock);
1964	return 0;
1965}
1966
1967static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1968					struct v4l2_format *fmt)
1969{
1970	struct zoran_fh *fh = __fh;
1971	struct zoran *zr = fh->zr;
1972
1973	mutex_lock(&zr->resource_lock);
1974
1975	if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1976		fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1977	if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1978		fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1979	if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1980		fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1981	if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1982		fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1983
1984	mutex_unlock(&zr->resource_lock);
1985	return 0;
1986}
1987
1988static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
1989					struct v4l2_format *fmt)
1990{
1991	struct zoran_fh *fh = __fh;
1992	struct zoran *zr = fh->zr;
1993	struct zoran_jpg_settings settings;
1994	int res = 0;
1995
1996	if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1997		return -EINVAL;
1998
1999	mutex_lock(&zr->resource_lock);
2000	settings = fh->jpg_settings;
2001
2002	/* we actually need to set 'real' parameters now */
2003	if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2004		settings.TmpDcm = 1;
2005	else
2006		settings.TmpDcm = 2;
2007	settings.decimation = 0;
2008	if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2009		settings.VerDcm = 2;
2010	else
2011		settings.VerDcm = 1;
2012	if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2013		settings.HorDcm = 4;
2014	else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2015		settings.HorDcm = 2;
2016	else
2017		settings.HorDcm = 1;
2018	if (settings.TmpDcm == 1)
2019		settings.field_per_buff = 2;
2020	else
2021		settings.field_per_buff = 1;
2022
2023	if (settings.HorDcm > 1) {
2024		settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2025		settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2026	} else {
2027		settings.img_x = 0;
2028		settings.img_width = BUZ_MAX_WIDTH;
2029	}
2030
2031	/* check */
2032	res = zoran_check_jpg_settings(zr, &settings, 1);
2033	if (res)
2034		goto tryfmt_unlock_and_return;
2035
2036	/* tell the user what we actually did */
2037	fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2038	fmt->fmt.pix.height = settings.img_height * 2 /
2039		(settings.TmpDcm * settings.VerDcm);
2040	if (settings.TmpDcm == 1)
2041		fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2042				V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2043	else
2044		fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2045				V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2046
2047	fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2048	fmt->fmt.pix.bytesperline = 0;
2049	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2050tryfmt_unlock_and_return:
2051	mutex_unlock(&zr->resource_lock);
2052	return res;
2053}
2054
2055static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2056					struct v4l2_format *fmt)
2057{
2058	struct zoran_fh *fh = __fh;
2059	struct zoran *zr = fh->zr;
2060	int bpp;
2061	int i;
2062
2063	if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2064		return zoran_try_fmt_vid_out(file, fh, fmt);
2065
2066	mutex_lock(&zr->resource_lock);
2067
2068	for (i = 0; i < NUM_FORMATS; i++)
2069		if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2070			break;
2071
2072	if (i == NUM_FORMATS) {
2073		mutex_unlock(&zr->resource_lock);
2074		return -EINVAL;
2075	}
2076
2077	bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
2078	v4l_bound_align_image(
2079		&fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
2080		&fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
2081	mutex_unlock(&zr->resource_lock);
2082
2083	return 0;
2084}
2085
2086static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2087					struct v4l2_format *fmt)
2088{
2089	struct zoran_fh *fh = __fh;
2090	struct zoran *zr = fh->zr;
2091	int res;
2092
2093	dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2094			fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2095			fmt->fmt.win.w.width,
2096			fmt->fmt.win.w.height,
2097			fmt->fmt.win.clipcount,
2098			fmt->fmt.win.bitmap);
2099	mutex_lock(&zr->resource_lock);
2100	res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2101			   fmt->fmt.win.w.width, fmt->fmt.win.w.height,
2102			   (struct v4l2_clip __user *)fmt->fmt.win.clips,
2103			   fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
2104	mutex_unlock(&zr->resource_lock);
2105	return res;
2106}
2107
2108static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2109					struct v4l2_format *fmt)
2110{
2111	struct zoran_fh *fh = __fh;
2112	struct zoran *zr = fh->zr;
2113	__le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2114	struct zoran_jpg_settings settings;
2115	int res = 0;
2116
2117	dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2118			fmt->fmt.pix.width, fmt->fmt.pix.height,
2119			fmt->fmt.pix.pixelformat,
2120			(char *) &printformat);
2121	if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2122		return -EINVAL;
2123
2124	mutex_lock(&zr->resource_lock);
2125
2126	if (fh->buffers.allocated) {
2127		dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2128			ZR_DEVNAME(zr));
2129		res = -EBUSY;
2130		goto sfmtjpg_unlock_and_return;
2131	}
2132
2133	settings = fh->jpg_settings;
2134
2135	/* we actually need to set 'real' parameters now */
2136	if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2137		settings.TmpDcm = 1;
2138	else
2139		settings.TmpDcm = 2;
2140	settings.decimation = 0;
2141	if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2142		settings.VerDcm = 2;
2143	else
2144		settings.VerDcm = 1;
2145	if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2146		settings.HorDcm = 4;
2147	else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2148		settings.HorDcm = 2;
2149	else
2150		settings.HorDcm = 1;
2151	if (settings.TmpDcm == 1)
2152		settings.field_per_buff = 2;
2153	else
2154		settings.field_per_buff = 1;
2155
2156	if (settings.HorDcm > 1) {
2157		settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2158		settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2159	} else {
2160		settings.img_x = 0;
2161		settings.img_width = BUZ_MAX_WIDTH;
2162	}
2163
2164	/* check */
2165	res = zoran_check_jpg_settings(zr, &settings, 0);
2166	if (res)
2167		goto sfmtjpg_unlock_and_return;
2168
2169	/* it's ok, so set them */
2170	fh->jpg_settings = settings;
2171
2172	map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2173	fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2174
2175	/* tell the user what we actually did */
2176	fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2177	fmt->fmt.pix.height = settings.img_height * 2 /
2178		(settings.TmpDcm * settings.VerDcm);
2179	if (settings.TmpDcm == 1)
2180		fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2181				V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2182	else
2183		fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2184				V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2185	fmt->fmt.pix.bytesperline = 0;
2186	fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
2187	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2188
2189sfmtjpg_unlock_and_return:
2190	mutex_unlock(&zr->resource_lock);
2191	return res;
2192}
2193
2194static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2195					struct v4l2_format *fmt)
2196{
2197	struct zoran_fh *fh = __fh;
2198	struct zoran *zr = fh->zr;
2199	int i;
2200	int res = 0;
2201
2202	if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2203		return zoran_s_fmt_vid_out(file, fh, fmt);
2204
2205	for (i = 0; i < NUM_FORMATS; i++)
2206		if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2207			break;
2208	if (i == NUM_FORMATS) {
2209		dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2210			ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2211		return -EINVAL;
2212	}
2213
2214	mutex_lock(&zr->resource_lock);
2215
2216	if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2217	    fh->buffers.active != ZORAN_FREE) {
2218		dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2219				ZR_DEVNAME(zr));
2220		res = -EBUSY;
2221		goto sfmtv4l_unlock_and_return;
2222	}
2223	if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2224		fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2225	if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2226		fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2227
2228	map_mode_raw(fh);
2229
2230	res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2231				   &zoran_formats[i]);
2232	if (res)
2233		goto sfmtv4l_unlock_and_return;
2234
2235	/* tell the user the results/missing stuff */
2236	fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2237	fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2238	fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2239	if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2240		fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2241	else
2242		fmt->fmt.pix.field = V4L2_FIELD_TOP;
2243
2244sfmtv4l_unlock_and_return:
2245	mutex_unlock(&zr->resource_lock);
2246	return res;
2247}
2248
2249static int zoran_g_fbuf(struct file *file, void *__fh,
2250		struct v4l2_framebuffer *fb)
2251{
2252	struct zoran_fh *fh = __fh;
2253	struct zoran *zr = fh->zr;
2254
2255	memset(fb, 0, sizeof(*fb));
2256	mutex_lock(&zr->resource_lock);
2257	fb->base = zr->vbuf_base;
2258	fb->fmt.width = zr->vbuf_width;
2259	fb->fmt.height = zr->vbuf_height;
2260	if (zr->overlay_settings.format)
2261		fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2262	fb->fmt.bytesperline = zr->vbuf_bytesperline;
2263	mutex_unlock(&zr->resource_lock);
2264	fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2265	fb->fmt.field = V4L2_FIELD_INTERLACED;
2266	fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2267	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2268
2269	return 0;
2270}
2271
2272static int zoran_s_fbuf(struct file *file, void *__fh,
2273		struct v4l2_framebuffer *fb)
2274{
2275	struct zoran_fh *fh = __fh;
2276	struct zoran *zr = fh->zr;
2277	int i, res = 0;
2278	__le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2279
2280	for (i = 0; i < NUM_FORMATS; i++)
2281		if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2282			break;
2283	if (i == NUM_FORMATS) {
2284		dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2285			ZR_DEVNAME(zr), fb->fmt.pixelformat,
2286			(char *)&printformat);
2287		return -EINVAL;
2288	}
2289
2290	mutex_lock(&zr->resource_lock);
2291	res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2292			    fb->fmt.height, fb->fmt.bytesperline);
2293	mutex_unlock(&zr->resource_lock);
2294
2295	return res;
2296}
2297
2298static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2299{
2300	struct zoran_fh *fh = __fh;
2301	struct zoran *zr = fh->zr;
2302	int res;
2303
2304	mutex_lock(&zr->resource_lock);
2305	res = setup_overlay(fh, on);
2306	mutex_unlock(&zr->resource_lock);
2307
2308	return res;
2309}
2310
2311static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2312
2313static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2314{
2315	struct zoran_fh *fh = __fh;
2316	struct zoran *zr = fh->zr;
2317	int res = 0;
2318
2319	if (req->memory != V4L2_MEMORY_MMAP) {
2320		dprintk(2,
2321				KERN_ERR
2322				"%s: only MEMORY_MMAP capture is supported, not %d\n",
2323				ZR_DEVNAME(zr), req->memory);
2324		return -EINVAL;
2325	}
2326
2327	if (req->count == 0)
2328		return zoran_streamoff(file, fh, req->type);
2329
2330	mutex_lock(&zr->resource_lock);
2331	if (fh->buffers.allocated) {
2332		dprintk(2,
2333				KERN_ERR
2334				"%s: VIDIOC_REQBUFS - buffers already allocated\n",
2335				ZR_DEVNAME(zr));
2336		res = -EBUSY;
2337		goto v4l2reqbuf_unlock_and_return;
2338	}
2339
2340	if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2341	    req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2342		/* control user input */
2343		if (req->count < 2)
2344			req->count = 2;
2345		if (req->count > v4l_nbufs)
2346			req->count = v4l_nbufs;
2347
2348		/* The next mmap will map the V4L buffers */
2349		map_mode_raw(fh);
2350		fh->buffers.num_buffers = req->count;
2351
2352		if (v4l_fbuffer_alloc(fh)) {
2353			res = -ENOMEM;
2354			goto v4l2reqbuf_unlock_and_return;
2355		}
2356	} else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2357		   fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2358		/* we need to calculate size ourselves now */
2359		if (req->count < 4)
2360			req->count = 4;
2361		if (req->count > jpg_nbufs)
2362			req->count = jpg_nbufs;
2363
2364		/* The next mmap will map the MJPEG buffers */
2365		map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2366		fh->buffers.num_buffers = req->count;
2367		fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2368
2369		if (jpg_fbuffer_alloc(fh)) {
2370			res = -ENOMEM;
2371			goto v4l2reqbuf_unlock_and_return;
2372		}
2373	} else {
2374		dprintk(1,
2375				KERN_ERR
2376				"%s: VIDIOC_REQBUFS - unknown type %d\n",
2377				ZR_DEVNAME(zr), req->type);
2378		res = -EINVAL;
2379		goto v4l2reqbuf_unlock_and_return;
2380	}
2381v4l2reqbuf_unlock_and_return:
2382	mutex_unlock(&zr->resource_lock);
2383
2384	return res;
2385}
2386
2387static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2388{
2389	struct zoran_fh *fh = __fh;
2390	struct zoran *zr = fh->zr;
2391	int res;
2392
2393	mutex_lock(&zr->resource_lock);
2394	res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2395	mutex_unlock(&zr->resource_lock);
2396
2397	return res;
2398}
2399
2400static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2401{
2402	struct zoran_fh *fh = __fh;
2403	struct zoran *zr = fh->zr;
2404	int res = 0, codec_mode, buf_type;
2405
2406	mutex_lock(&zr->resource_lock);
2407
2408	switch (fh->map_mode) {
2409	case ZORAN_MAP_MODE_RAW:
2410		if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2411			dprintk(1, KERN_ERR
2412				"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2413				ZR_DEVNAME(zr), buf->type, fh->map_mode);
2414			res = -EINVAL;
2415			goto qbuf_unlock_and_return;
2416		}
2417
2418		res = zoran_v4l_queue_frame(fh, buf->index);
2419		if (res)
2420			goto qbuf_unlock_and_return;
2421		if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2422			zr36057_set_memgrab(zr, 1);
2423		break;
2424
2425	case ZORAN_MAP_MODE_JPG_REC:
2426	case ZORAN_MAP_MODE_JPG_PLAY:
2427		if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2428			buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2429			codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2430		} else {
2431			buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2432			codec_mode = BUZ_MODE_MOTION_COMPRESS;
2433		}
2434
2435		if (buf->type != buf_type) {
2436			dprintk(1, KERN_ERR
2437				"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2438				ZR_DEVNAME(zr), buf->type, fh->map_mode);
2439			res = -EINVAL;
2440			goto qbuf_unlock_and_return;
2441		}
2442
2443		res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
2444		if (res != 0)
2445			goto qbuf_unlock_and_return;
2446		if (zr->codec_mode == BUZ_MODE_IDLE &&
2447		    fh->buffers.active == ZORAN_LOCKED)
2448			zr36057_enable_jpg(zr, codec_mode);
2449
2450		break;
2451
2452	default:
2453		dprintk(1, KERN_ERR
2454			"%s: VIDIOC_QBUF - unsupported type %d\n",
2455			ZR_DEVNAME(zr), buf->type);
2456		res = -EINVAL;
2457		break;
2458	}
2459qbuf_unlock_and_return:
2460	mutex_unlock(&zr->resource_lock);
2461
2462	return res;
2463}
2464
2465static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2466{
2467	struct zoran_fh *fh = __fh;
2468	struct zoran *zr = fh->zr;
2469	int res = 0, buf_type, num = -1;	/* compiler borks here (?) */
2470
2471	mutex_lock(&zr->resource_lock);
2472
2473	switch (fh->map_mode) {
2474	case ZORAN_MAP_MODE_RAW:
2475		if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2476			dprintk(1, KERN_ERR
2477				"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2478				ZR_DEVNAME(zr), buf->type, fh->map_mode);
2479			res = -EINVAL;
2480			goto dqbuf_unlock_and_return;
2481		}
2482
2483		num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2484		if (file->f_flags & O_NONBLOCK &&
2485		    zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2486			res = -EAGAIN;
2487			goto dqbuf_unlock_and_return;
2488		}
2489		res = v4l_sync(fh, num);
2490		if (res)
2491			goto dqbuf_unlock_and_return;
2492		zr->v4l_sync_tail++;
2493		res = zoran_v4l2_buffer_status(fh, buf, num);
2494		break;
2495
2496	case ZORAN_MAP_MODE_JPG_REC:
2497	case ZORAN_MAP_MODE_JPG_PLAY:
2498	{
2499		struct zoran_sync bs;
2500
2501		if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2502			buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2503		else
2504			buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2505
2506		if (buf->type != buf_type) {
2507			dprintk(1, KERN_ERR
2508				"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2509				ZR_DEVNAME(zr), buf->type, fh->map_mode);
2510			res = -EINVAL;
2511			goto dqbuf_unlock_and_return;
2512		}
2513
2514		num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2515
2516		if (file->f_flags & O_NONBLOCK &&
2517		    zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2518			res = -EAGAIN;
2519			goto dqbuf_unlock_and_return;
2520		}
2521		res = jpg_sync(fh, &bs);
2522		if (res)
2523			goto dqbuf_unlock_and_return;
2524		res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2525		break;
2526	}
2527
2528	default:
2529		dprintk(1, KERN_ERR
2530			"%s: VIDIOC_DQBUF - unsupported type %d\n",
2531			ZR_DEVNAME(zr), buf->type);
2532		res = -EINVAL;
2533		break;
2534	}
2535dqbuf_unlock_and_return:
2536	mutex_unlock(&zr->resource_lock);
2537
2538	return res;
2539}
2540
2541static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2542{
2543	struct zoran_fh *fh = __fh;
2544	struct zoran *zr = fh->zr;
2545	int res = 0;
2546
2547	mutex_lock(&zr->resource_lock);
2548
2549	switch (fh->map_mode) {
2550	case ZORAN_MAP_MODE_RAW:	/* raw capture */
2551		if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2552		    fh->buffers.active != ZORAN_ACTIVE) {
2553			res = -EBUSY;
2554			goto strmon_unlock_and_return;
2555		}
2556
2557		zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2558		zr->v4l_settings = fh->v4l_settings;
2559
2560		zr->v4l_sync_tail = zr->v4l_pend_tail;
2561		if (!zr->v4l_memgrab_active &&
2562		    zr->v4l_pend_head != zr->v4l_pend_tail) {
2563			zr36057_set_memgrab(zr, 1);
2564		}
2565		break;
2566
2567	case ZORAN_MAP_MODE_JPG_REC:
2568	case ZORAN_MAP_MODE_JPG_PLAY:
2569		/* what is the codec mode right now? */
2570		if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2571		    fh->buffers.active != ZORAN_ACTIVE) {
2572			res = -EBUSY;
2573			goto strmon_unlock_and_return;
2574		}
2575
2576		zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2577
2578		if (zr->jpg_que_head != zr->jpg_que_tail) {
2579			/* Start the jpeg codec when the first frame is queued  */
2580			jpeg_start(zr);
2581		}
2582		break;
2583
2584	default:
2585		dprintk(1,
2586			KERN_ERR
2587			"%s: VIDIOC_STREAMON - invalid map mode %d\n",
2588			ZR_DEVNAME(zr), fh->map_mode);
2589		res = -EINVAL;
2590		break;
2591	}
2592strmon_unlock_and_return:
2593	mutex_unlock(&zr->resource_lock);
2594
2595	return res;
2596}
2597
2598static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2599{
2600	struct zoran_fh *fh = __fh;
2601	struct zoran *zr = fh->zr;
2602	int i, res = 0;
2603	unsigned long flags;
2604
2605	mutex_lock(&zr->resource_lock);
2606
2607	switch (fh->map_mode) {
2608	case ZORAN_MAP_MODE_RAW:	/* raw capture */
2609		if (fh->buffers.active == ZORAN_FREE &&
2610		    zr->v4l_buffers.active != ZORAN_FREE) {
2611			res = -EPERM;	/* stay off other's settings! */
2612			goto strmoff_unlock_and_return;
2613		}
2614		if (zr->v4l_buffers.active == ZORAN_FREE)
2615			goto strmoff_unlock_and_return;
2616
2617		spin_lock_irqsave(&zr->spinlock, flags);
2618		/* unload capture */
2619		if (zr->v4l_memgrab_active) {
2620
2621			zr36057_set_memgrab(zr, 0);
2622		}
2623
2624		for (i = 0; i < fh->buffers.num_buffers; i++)
2625			zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2626		fh->buffers = zr->v4l_buffers;
2627
2628		zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2629
2630		zr->v4l_grab_seq = 0;
2631		zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2632		zr->v4l_sync_tail = 0;
2633
2634		spin_unlock_irqrestore(&zr->spinlock, flags);
2635
2636		break;
2637
2638	case ZORAN_MAP_MODE_JPG_REC:
2639	case ZORAN_MAP_MODE_JPG_PLAY:
2640		if (fh->buffers.active == ZORAN_FREE &&
2641		    zr->jpg_buffers.active != ZORAN_FREE) {
2642			res = -EPERM;	/* stay off other's settings! */
2643			goto strmoff_unlock_and_return;
2644		}
2645		if (zr->jpg_buffers.active == ZORAN_FREE)
2646			goto strmoff_unlock_and_return;
2647
2648		res = jpg_qbuf(fh, -1,
2649			     (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2650			     BUZ_MODE_MOTION_COMPRESS :
2651			     BUZ_MODE_MOTION_DECOMPRESS);
2652		if (res)
2653			goto strmoff_unlock_and_return;
2654		break;
2655	default:
2656		dprintk(1, KERN_ERR
2657			"%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2658			ZR_DEVNAME(zr), fh->map_mode);
2659		res = -EINVAL;
2660		break;
2661	}
2662strmoff_unlock_and_return:
2663	mutex_unlock(&zr->resource_lock);
2664
2665	return res;
2666}
2667
2668static int zoran_queryctrl(struct file *file, void *__fh,
2669					struct v4l2_queryctrl *ctrl)
2670{
2671	struct zoran_fh *fh = __fh;
2672	struct zoran *zr = fh->zr;
2673
2674	/* we only support hue/saturation/contrast/brightness */
2675	if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2676	    ctrl->id > V4L2_CID_HUE)
2677		return -EINVAL;
2678
2679	decoder_call(zr, core, queryctrl, ctrl);
2680
2681	return 0;
2682}
2683
2684static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2685{
2686	struct zoran_fh *fh = __fh;
2687	struct zoran *zr = fh->zr;
2688
2689	/* we only support hue/saturation/contrast/brightness */
2690	if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2691	    ctrl->id > V4L2_CID_HUE)
2692		return -EINVAL;
2693
2694	mutex_lock(&zr->resource_lock);
2695	decoder_call(zr, core, g_ctrl, ctrl);
2696	mutex_unlock(&zr->resource_lock);
2697
2698	return 0;
2699}
2700
2701static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2702{
2703	struct zoran_fh *fh = __fh;
2704	struct zoran *zr = fh->zr;
2705
2706	/* we only support hue/saturation/contrast/brightness */
2707	if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2708	    ctrl->id > V4L2_CID_HUE)
2709		return -EINVAL;
2710
2711	mutex_lock(&zr->resource_lock);
2712	decoder_call(zr, core, s_ctrl, ctrl);
2713	mutex_unlock(&zr->resource_lock);
2714
2715	return 0;
2716}
2717
2718static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2719{
2720	struct zoran_fh *fh = __fh;
2721	struct zoran *zr = fh->zr;
2722
2723	mutex_lock(&zr->resource_lock);
2724	*std = zr->norm;
2725	mutex_unlock(&zr->resource_lock);
2726	return 0;
2727}
2728
2729static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2730{
2731	struct zoran_fh *fh = __fh;
2732	struct zoran *zr = fh->zr;
2733	int res = 0;
2734
2735	mutex_lock(&zr->resource_lock);
2736	res = zoran_set_norm(zr, *std);
2737	if (res)
2738		goto sstd_unlock_and_return;
2739
2740	res = wait_grab_pending(zr);
2741sstd_unlock_and_return:
2742	mutex_unlock(&zr->resource_lock);
2743	return res;
2744}
2745
2746static int zoran_enum_input(struct file *file, void *__fh,
2747				 struct v4l2_input *inp)
2748{
2749	struct zoran_fh *fh = __fh;
2750	struct zoran *zr = fh->zr;
2751
2752	if (inp->index >= zr->card.inputs)
2753		return -EINVAL;
2754	else {
2755		int id = inp->index;
2756		memset(inp, 0, sizeof(*inp));
2757		inp->index = id;
2758	}
2759
2760	strncpy(inp->name, zr->card.input[inp->index].name,
2761		sizeof(inp->name) - 1);
2762	inp->type = V4L2_INPUT_TYPE_CAMERA;
2763	inp->std = V4L2_STD_ALL;
2764
2765	/* Get status of video decoder */
2766	mutex_lock(&zr->resource_lock);
2767	decoder_call(zr, video, g_input_status, &inp->status);
2768	mutex_unlock(&zr->resource_lock);
2769	return 0;
2770}
2771
2772static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2773{
2774	struct zoran_fh *fh = __fh;
2775	struct zoran *zr = fh->zr;
2776
2777	mutex_lock(&zr->resource_lock);
2778	*input = zr->input;
2779	mutex_unlock(&zr->resource_lock);
2780
2781	return 0;
2782}
2783
2784static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2785{
2786	struct zoran_fh *fh = __fh;
2787	struct zoran *zr = fh->zr;
2788	int res;
2789
2790	mutex_lock(&zr->resource_lock);
2791	res = zoran_set_input(zr, input);
2792	if (res)
2793		goto sinput_unlock_and_return;
2794
2795	/* Make sure the changes come into effect */
2796	res = wait_grab_pending(zr);
2797sinput_unlock_and_return:
2798	mutex_unlock(&zr->resource_lock);
2799	return res;
2800}
2801
2802static int zoran_enum_output(struct file *file, void *__fh,
2803				  struct v4l2_output *outp)
2804{
2805	if (outp->index != 0)
2806		return -EINVAL;
2807
2808	memset(outp, 0, sizeof(*outp));
2809	outp->index = 0;
2810	outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2811	strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2812
2813	return 0;
2814}
2815
2816static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2817{
2818	*output = 0;
2819
2820	return 0;
2821}
2822
2823static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2824{
2825	if (output != 0)
2826		return -EINVAL;
2827
2828	return 0;
2829}
2830
2831/* cropping (sub-frame capture) */
2832static int zoran_cropcap(struct file *file, void *__fh,
2833					struct v4l2_cropcap *cropcap)
2834{
2835	struct zoran_fh *fh = __fh;
2836	struct zoran *zr = fh->zr;
2837	int type = cropcap->type, res = 0;
2838
2839	memset(cropcap, 0, sizeof(*cropcap));
2840	cropcap->type = type;
2841
2842	mutex_lock(&zr->resource_lock);
2843
2844	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2845	    (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2846	     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2847		dprintk(1, KERN_ERR
2848			"%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2849			ZR_DEVNAME(zr));
2850		res = -EINVAL;
2851		goto cropcap_unlock_and_return;
2852	}
2853
2854	cropcap->bounds.top = cropcap->bounds.left = 0;
2855	cropcap->bounds.width = BUZ_MAX_WIDTH;
2856	cropcap->bounds.height = BUZ_MAX_HEIGHT;
2857	cropcap->defrect.top = cropcap->defrect.left = 0;
2858	cropcap->defrect.width = BUZ_MIN_WIDTH;
2859	cropcap->defrect.height = BUZ_MIN_HEIGHT;
2860cropcap_unlock_and_return:
2861	mutex_unlock(&zr->resource_lock);
2862	return res;
2863}
2864
2865static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2866{
2867	struct zoran_fh *fh = __fh;
2868	struct zoran *zr = fh->zr;
2869	int type = crop->type, res = 0;
2870
2871	memset(crop, 0, sizeof(*crop));
2872	crop->type = type;
2873
2874	mutex_lock(&zr->resource_lock);
2875
2876	if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2877	    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2878	     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2879		dprintk(1,
2880			KERN_ERR
2881			"%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2882			ZR_DEVNAME(zr));
2883		res = -EINVAL;
2884		goto gcrop_unlock_and_return;
2885	}
2886
2887	crop->c.top = fh->jpg_settings.img_y;
2888	crop->c.left = fh->jpg_settings.img_x;
2889	crop->c.width = fh->jpg_settings.img_width;
2890	crop->c.height = fh->jpg_settings.img_height;
2891
2892gcrop_unlock_and_return:
2893	mutex_unlock(&zr->resource_lock);
2894
2895	return res;
2896}
2897
2898static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2899{
2900	struct zoran_fh *fh = __fh;
2901	struct zoran *zr = fh->zr;
2902	int res = 0;
2903	struct zoran_jpg_settings settings;
2904
2905	settings = fh->jpg_settings;
2906
2907	mutex_lock(&zr->resource_lock);
2908
2909	if (fh->buffers.allocated) {
2910		dprintk(1, KERN_ERR
2911			"%s: VIDIOC_S_CROP - cannot change settings while active\n",
2912			ZR_DEVNAME(zr));
2913		res = -EBUSY;
2914		goto scrop_unlock_and_return;
2915	}
2916
2917	if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2918	    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2919	     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2920		dprintk(1, KERN_ERR
2921			"%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2922			ZR_DEVNAME(zr));
2923		res = -EINVAL;
2924		goto scrop_unlock_and_return;
2925	}
2926
2927	/* move into a form that we understand */
2928	settings.img_x = crop->c.left;
2929	settings.img_y = crop->c.top;
2930	settings.img_width = crop->c.width;
2931	settings.img_height = crop->c.height;
2932
2933	/* check validity */
2934	res = zoran_check_jpg_settings(zr, &settings, 0);
2935	if (res)
2936		goto scrop_unlock_and_return;
2937
2938	/* accept */
2939	fh->jpg_settings = settings;
2940
2941scrop_unlock_and_return:
2942	mutex_unlock(&zr->resource_lock);
2943	return res;
2944}
2945
2946static int zoran_g_jpegcomp(struct file *file, void *__fh,
2947					struct v4l2_jpegcompression *params)
2948{
2949	struct zoran_fh *fh = __fh;
2950	struct zoran *zr = fh->zr;
2951	memset(params, 0, sizeof(*params));
2952
2953	mutex_lock(&zr->resource_lock);
2954
2955	params->quality = fh->jpg_settings.jpg_comp.quality;
2956	params->APPn = fh->jpg_settings.jpg_comp.APPn;
2957	memcpy(params->APP_data,
2958	       fh->jpg_settings.jpg_comp.APP_data,
2959	       fh->jpg_settings.jpg_comp.APP_len);
2960	params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2961	memcpy(params->COM_data,
2962	       fh->jpg_settings.jpg_comp.COM_data,
2963	       fh->jpg_settings.jpg_comp.COM_len);
2964	params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2965	params->jpeg_markers =
2966	    fh->jpg_settings.jpg_comp.jpeg_markers;
2967
2968	mutex_unlock(&zr->resource_lock);
2969
2970	return 0;
2971}
2972
2973static int zoran_s_jpegcomp(struct file *file, void *__fh,
2974					struct v4l2_jpegcompression *params)
2975{
2976	struct zoran_fh *fh = __fh;
2977	struct zoran *zr = fh->zr;
2978	int res = 0;
2979	struct zoran_jpg_settings settings;
2980
2981	settings = fh->jpg_settings;
2982
2983	settings.jpg_comp = *params;
2984
2985	mutex_lock(&zr->resource_lock);
2986
2987	if (fh->buffers.active != ZORAN_FREE) {
2988		dprintk(1, KERN_WARNING
2989			"%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
2990			ZR_DEVNAME(zr));
2991		res = -EBUSY;
2992		goto sjpegc_unlock_and_return;
2993	}
2994
2995	res = zoran_check_jpg_settings(zr, &settings, 0);
2996	if (res)
2997		goto sjpegc_unlock_and_return;
2998	if (!fh->buffers.allocated)
2999		fh->buffers.buffer_size =
3000			zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3001	fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3002sjpegc_unlock_and_return:
3003	mutex_unlock(&zr->resource_lock);
3004
3005	return res;
3006}
3007
3008static unsigned int
3009zoran_poll (struct file *file,
3010	    poll_table  *wait)
3011{
3012	struct zoran_fh *fh = file->private_data;
3013	struct zoran *zr = fh->zr;
3014	int res = 0, frame;
3015	unsigned long flags;
3016
3017	/* we should check whether buffers are ready to be synced on
3018	 * (w/o waits - O_NONBLOCK) here
3019	 * if ready for read (sync), return POLLIN|POLLRDNORM,
3020	 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3021	 * if error, return POLLERR,
3022	 * if no buffers queued or so, return POLLNVAL
3023	 */
3024
3025	mutex_lock(&zr->resource_lock);
3026
3027	switch (fh->map_mode) {
3028	case ZORAN_MAP_MODE_RAW:
3029		poll_wait(file, &zr->v4l_capq, wait);
3030		frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3031
3032		spin_lock_irqsave(&zr->spinlock, flags);
3033		dprintk(3,
3034			KERN_DEBUG
3035			"%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3036			ZR_DEVNAME(zr), __func__,
3037			"FAL"[fh->buffers.active], zr->v4l_sync_tail,
3038			"UPMD"[zr->v4l_buffers.buffer[frame].state],
3039			zr->v4l_pend_tail, zr->v4l_pend_head);
3040		/* Process is the one capturing? */
3041		if (fh->buffers.active != ZORAN_FREE &&
3042		    /* Buffer ready to DQBUF? */
3043		    zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3044			res = POLLIN | POLLRDNORM;
3045		spin_unlock_irqrestore(&zr->spinlock, flags);
3046
3047		break;
3048
3049	case ZORAN_MAP_MODE_JPG_REC:
3050	case ZORAN_MAP_MODE_JPG_PLAY:
3051		poll_wait(file, &zr->jpg_capq, wait);
3052		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3053
3054		spin_lock_irqsave(&zr->spinlock, flags);
3055		dprintk(3,
3056			KERN_DEBUG
3057			"%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3058			ZR_DEVNAME(zr), __func__,
3059			"FAL"[fh->buffers.active], zr->jpg_que_tail,
3060			"UPMD"[zr->jpg_buffers.buffer[frame].state],
3061			zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3062		if (fh->buffers.active != ZORAN_FREE &&
3063		    zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3064			if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3065				res = POLLIN | POLLRDNORM;
3066			else
3067				res = POLLOUT | POLLWRNORM;
3068		}
3069		spin_unlock_irqrestore(&zr->spinlock, flags);
3070
3071		break;
3072
3073	default:
3074		dprintk(1,
3075			KERN_ERR
3076			"%s: %s - internal error, unknown map_mode=%d\n",
3077			ZR_DEVNAME(zr), __func__, fh->map_mode);
3078		res = POLLNVAL;
3079	}
3080
3081	mutex_unlock(&zr->resource_lock);
3082
3083	return res;
3084}
3085
3086
3087/*
3088 * This maps the buffers to user space.
3089 *
3090 * Depending on the state of fh->map_mode
3091 * the V4L or the MJPEG buffers are mapped
3092 * per buffer or all together
3093 *
3094 * Note that we need to connect to some
3095 * unmap signal event to unmap the de-allocate
3096 * the buffer accordingly (zoran_vm_close())
3097 */
3098
3099static void
3100zoran_vm_open (struct vm_area_struct *vma)
3101{
3102	struct zoran_mapping *map = vma->vm_private_data;
3103
3104	map->count++;
3105}
3106
3107static void
3108zoran_vm_close (struct vm_area_struct *vma)
3109{
3110	struct zoran_mapping *map = vma->vm_private_data;
3111	struct zoran_fh *fh = map->file->private_data;
3112	struct zoran *zr = fh->zr;
3113	int i;
3114
3115	if (--map->count > 0)
3116		return;
3117
3118	dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3119		__func__, mode_name(fh->map_mode));
3120
3121	for (i = 0; i < fh->buffers.num_buffers; i++) {
3122		if (fh->buffers.buffer[i].map == map)
3123			fh->buffers.buffer[i].map = NULL;
3124	}
3125	kfree(map);
3126
3127	/* Any buffers still mapped? */
3128	for (i = 0; i < fh->buffers.num_buffers; i++)
3129		if (fh->buffers.buffer[i].map)
3130			return;
3131
3132	dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3133		__func__, mode_name(fh->map_mode));
3134
3135	mutex_lock(&zr->resource_lock);
3136
3137	if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3138		if (fh->buffers.active != ZORAN_FREE) {
3139			unsigned long flags;
3140
3141			spin_lock_irqsave(&zr->spinlock, flags);
3142			zr36057_set_memgrab(zr, 0);
3143			zr->v4l_buffers.allocated = 0;
3144			zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3145			spin_unlock_irqrestore(&zr->spinlock, flags);
3146		}
3147		v4l_fbuffer_free(fh);
3148	} else {
3149		if (fh->buffers.active != ZORAN_FREE) {
3150			jpg_qbuf(fh, -1, zr->codec_mode);
3151			zr->jpg_buffers.allocated = 0;
3152			zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
3153		}
3154		jpg_fbuffer_free(fh);
3155	}
3156
3157	mutex_unlock(&zr->resource_lock);
3158}
3159
3160static const struct vm_operations_struct zoran_vm_ops = {
3161	.open = zoran_vm_open,
3162	.close = zoran_vm_close,
3163};
3164
3165static int
3166zoran_mmap (struct file           *file,
3167	    struct vm_area_struct *vma)
3168{
3169	struct zoran_fh *fh = file->private_data;
3170	struct zoran *zr = fh->zr;
3171	unsigned long size = (vma->vm_end - vma->vm_start);
3172	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3173	int i, j;
3174	unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3175	int first, last;
3176	struct zoran_mapping *map;
3177	int res = 0;
3178
3179	dprintk(3,
3180		KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3181		ZR_DEVNAME(zr), __func__,
3182		mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
3183
3184	if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3185	    !(vma->vm_flags & VM_WRITE)) {
3186		dprintk(1,
3187			KERN_ERR
3188			"%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3189			ZR_DEVNAME(zr), __func__);
3190		return -EINVAL;
3191	}
3192
3193	mutex_lock(&zr->resource_lock);
3194
3195	if (!fh->buffers.allocated) {
3196		dprintk(1,
3197			KERN_ERR
3198			"%s: %s(%s) - buffers not yet allocated\n",
3199			ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
3200		res = -ENOMEM;
3201		goto mmap_unlock_and_return;
3202	}
3203
3204	first = offset / fh->buffers.buffer_size;
3205	last = first - 1 + size / fh->buffers.buffer_size;
3206	if (offset % fh->buffers.buffer_size != 0 ||
3207	    size % fh->buffers.buffer_size != 0 || first < 0 ||
3208	    last < 0 || first >= fh->buffers.num_buffers ||
3209	    last >= fh->buffers.buffer_size) {
3210		dprintk(1,
3211			KERN_ERR
3212			"%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3213			ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
3214			fh->buffers.buffer_size,
3215			fh->buffers.num_buffers);
3216		res = -EINVAL;
3217		goto mmap_unlock_and_return;
3218	}
3219
3220	/* Check if any buffers are already mapped */
3221	for (i = first; i <= last; i++) {
3222		if (fh->buffers.buffer[i].map) {
3223			dprintk(1,
3224				KERN_ERR
3225				"%s: %s(%s) - buffer %d already mapped\n",
3226				ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
3227			res = -EBUSY;
3228			goto mmap_unlock_and_return;
3229		}
3230	}
3231
3232	/* map these buffers */
3233	map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3234	if (!map) {
3235		res = -ENOMEM;
3236		goto mmap_unlock_and_return;
3237	}
3238	map->file = file;
3239	map->count = 1;
3240
3241	vma->vm_ops = &zoran_vm_ops;
3242	vma->vm_flags |= VM_DONTEXPAND;
3243	vma->vm_private_data = map;
3244
3245	if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3246		for (i = first; i <= last; i++) {
3247			todo = size;
3248			if (todo > fh->buffers.buffer_size)
3249				todo = fh->buffers.buffer_size;
3250			page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3251			if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3252							todo, PAGE_SHARED)) {
3253				dprintk(1,
3254					KERN_ERR
3255					"%s: %s(V4L) - remap_pfn_range failed\n",
3256					ZR_DEVNAME(zr), __func__);
3257				res = -EAGAIN;
3258				goto mmap_unlock_and_return;
3259			}
3260			size -= todo;
3261			start += todo;
3262			fh->buffers.buffer[i].map = map;
3263			if (size == 0)
3264				break;
3265		}
3266	} else {
3267		for (i = first; i <= last; i++) {
3268			for (j = 0;
3269			     j < fh->buffers.buffer_size / PAGE_SIZE;
3270			     j++) {
3271				fraglen =
3272				    (le32_to_cpu(fh->buffers.buffer[i].jpg.
3273				     frag_tab[2 * j + 1]) & ~1) << 1;
3274				todo = size;
3275				if (todo > fraglen)
3276					todo = fraglen;
3277				pos =
3278				    le32_to_cpu(fh->buffers.
3279				    buffer[i].jpg.frag_tab[2 * j]);
3280				/* should just be pos on i386 */
3281				page = virt_to_phys(bus_to_virt(pos))
3282								>> PAGE_SHIFT;
3283				if (remap_pfn_range(vma, start, page,
3284							todo, PAGE_SHARED)) {
3285					dprintk(1,
3286						KERN_ERR
3287						"%s: %s(V4L) - remap_pfn_range failed\n",
3288						ZR_DEVNAME(zr), __func__);
3289					res = -EAGAIN;
3290					goto mmap_unlock_and_return;
3291				}
3292				size -= todo;
3293				start += todo;
3294				if (size == 0)
3295					break;
3296				if (le32_to_cpu(fh->buffers.buffer[i].jpg.
3297				    frag_tab[2 * j + 1]) & 1)
3298					break;	/* was last fragment */
3299			}
3300			fh->buffers.buffer[i].map = map;
3301			if (size == 0)
3302				break;
3303
3304		}
3305	}
3306
3307mmap_unlock_and_return:
3308	mutex_unlock(&zr->resource_lock);
3309
3310	return 0;
3311}
3312
3313static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3314	.vidioc_querycap    		    = zoran_querycap,
3315	.vidioc_cropcap       		    = zoran_cropcap,
3316	.vidioc_s_crop       		    = zoran_s_crop,
3317	.vidioc_g_crop       		    = zoran_g_crop,
3318	.vidioc_enum_input     		    = zoran_enum_input,
3319	.vidioc_g_input      		    = zoran_g_input,
3320	.vidioc_s_input      		    = zoran_s_input,
3321	.vidioc_enum_output    		    = zoran_enum_output,
3322	.vidioc_g_output     		    = zoran_g_output,
3323	.vidioc_s_output     		    = zoran_s_output,
3324	.vidioc_g_fbuf			    = zoran_g_fbuf,
3325	.vidioc_s_fbuf			    = zoran_s_fbuf,
3326	.vidioc_g_std 			    = zoran_g_std,
3327	.vidioc_s_std 			    = zoran_s_std,
3328	.vidioc_g_jpegcomp 		    = zoran_g_jpegcomp,
3329	.vidioc_s_jpegcomp 		    = zoran_s_jpegcomp,
3330	.vidioc_overlay			    = zoran_overlay,
3331	.vidioc_reqbufs			    = zoran_reqbufs,
3332	.vidioc_querybuf		    = zoran_querybuf,
3333	.vidioc_qbuf			    = zoran_qbuf,
3334	.vidioc_dqbuf			    = zoran_dqbuf,
3335	.vidioc_streamon		    = zoran_streamon,
3336	.vidioc_streamoff		    = zoran_streamoff,
3337	.vidioc_enum_fmt_vid_cap 	    = zoran_enum_fmt_vid_cap,
3338	.vidioc_enum_fmt_vid_out 	    = zoran_enum_fmt_vid_out,
3339	.vidioc_enum_fmt_vid_overlay 	    = zoran_enum_fmt_vid_overlay,
3340	.vidioc_g_fmt_vid_cap 		    = zoran_g_fmt_vid_cap,
3341	.vidioc_g_fmt_vid_out               = zoran_g_fmt_vid_out,
3342	.vidioc_g_fmt_vid_overlay           = zoran_g_fmt_vid_overlay,
3343	.vidioc_s_fmt_vid_cap  		    = zoran_s_fmt_vid_cap,
3344	.vidioc_s_fmt_vid_out               = zoran_s_fmt_vid_out,
3345	.vidioc_s_fmt_vid_overlay           = zoran_s_fmt_vid_overlay,
3346	.vidioc_try_fmt_vid_cap  	    = zoran_try_fmt_vid_cap,
3347	.vidioc_try_fmt_vid_out 	    = zoran_try_fmt_vid_out,
3348	.vidioc_try_fmt_vid_overlay 	    = zoran_try_fmt_vid_overlay,
3349	.vidioc_queryctrl 		    = zoran_queryctrl,
3350	.vidioc_s_ctrl       		    = zoran_s_ctrl,
3351	.vidioc_g_ctrl       		    = zoran_g_ctrl,
3352#ifdef CONFIG_VIDEO_V4L1_COMPAT
3353	.vidioc_default 		    = zoran_default,
3354	.vidiocgmbuf 			    = zoran_vidiocgmbuf,
3355#endif
3356};
3357
3358static const struct v4l2_file_operations zoran_fops = {
3359	.owner = THIS_MODULE,
3360	.open = zoran_open,
3361	.release = zoran_close,
3362	.ioctl = video_ioctl2,
3363	.read = zoran_read,
3364	.write = zoran_write,
3365	.mmap = zoran_mmap,
3366	.poll = zoran_poll,
3367};
3368
3369struct video_device zoran_template __devinitdata = {
3370	.name = ZORAN_NAME,
3371	.fops = &zoran_fops,
3372	.ioctl_ops = &zoran_ioctl_ops,
3373	.release = &zoran_vdev_release,
3374	.tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3375};
3376