• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
55 
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
59 
60 #include <linux/spinlock.h>
61 #define     MAP_NR(x)       virt_to_page(x)
62 #define     ZORAN_VID_TYPE  ( \
63 				VID_TYPE_CAPTURE | \
64 				VID_TYPE_OVERLAY | \
65 				VID_TYPE_CLIPPING | \
66 				VID_TYPE_FRAMERAM | \
67 				VID_TYPE_SCALES | \
68 				VID_TYPE_MJPEG_DECODER | \
69 				VID_TYPE_MJPEG_ENCODER \
70 			     )
71 
72 #include <linux/videodev.h>
73 #include <media/v4l2-common.h>
74 #include <media/v4l2-ioctl.h>
75 #include "videocodec.h"
76 
77 #include <asm/byteorder.h>
78 #include <asm/io.h>
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
81 
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
85 #include "zoran.h"
86 #include "zoran_device.h"
87 #include "zoran_card.h"
88 
89 	/* we declare some card type definitions here, they mean
90 	 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
91 #define ZORAN_V4L2_VID_FLAGS ( \
92 				V4L2_CAP_STREAMING |\
93 				V4L2_CAP_VIDEO_CAPTURE |\
94 				V4L2_CAP_VIDEO_OUTPUT |\
95 				V4L2_CAP_VIDEO_OVERLAY \
96 			      )
97 
98 
99 #if defined(CONFIG_VIDEO_V4L1_COMPAT)
100 #define ZFMT(pal, fcc, cs) \
101 	.palette = (pal), .fourcc = (fcc), .colorspace = (cs)
102 #else
103 #define ZFMT(pal, fcc, cs) \
104 	.fourcc = (fcc), .colorspace = (cs)
105 #endif
106 
107 const struct zoran_format zoran_formats[] = {
108 	{
109 		.name = "15-bit RGB LE",
110 		ZFMT(VIDEO_PALETTE_RGB555,
111 		     V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
112 		.depth = 15,
113 		.flags = ZORAN_FORMAT_CAPTURE |
114 			 ZORAN_FORMAT_OVERLAY,
115 		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
116 			   ZR36057_VFESPFR_LittleEndian,
117 	}, {
118 		.name = "15-bit RGB BE",
119 		ZFMT(-1,
120 		     V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
121 		.depth = 15,
122 		.flags = ZORAN_FORMAT_CAPTURE |
123 			 ZORAN_FORMAT_OVERLAY,
124 		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
125 	}, {
126 		.name = "16-bit RGB LE",
127 		ZFMT(VIDEO_PALETTE_RGB565,
128 		     V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
129 		.depth = 16,
130 		.flags = ZORAN_FORMAT_CAPTURE |
131 			 ZORAN_FORMAT_OVERLAY,
132 		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
133 			   ZR36057_VFESPFR_LittleEndian,
134 	}, {
135 		.name = "16-bit RGB BE",
136 		ZFMT(-1,
137 		     V4L2_PIX_FMT_RGB565X, V4L2_COLORSPACE_SRGB),
138 		.depth = 16,
139 		.flags = ZORAN_FORMAT_CAPTURE |
140 			 ZORAN_FORMAT_OVERLAY,
141 		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
142 	}, {
143 		.name = "24-bit RGB",
144 		ZFMT(VIDEO_PALETTE_RGB24,
145 		     V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
146 		.depth = 24,
147 		.flags = ZORAN_FORMAT_CAPTURE |
148 			 ZORAN_FORMAT_OVERLAY,
149 		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
150 	}, {
151 		.name = "32-bit RGB LE",
152 		ZFMT(VIDEO_PALETTE_RGB32,
153 		     V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
154 		.depth = 32,
155 		.flags = ZORAN_FORMAT_CAPTURE |
156 			 ZORAN_FORMAT_OVERLAY,
157 		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
158 	}, {
159 		.name = "32-bit RGB BE",
160 		ZFMT(-1,
161 		     V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
162 		.depth = 32,
163 		.flags = ZORAN_FORMAT_CAPTURE |
164 			 ZORAN_FORMAT_OVERLAY,
165 		.vfespfr = ZR36057_VFESPFR_RGB888,
166 	}, {
167 		.name = "4:2:2, packed, YUYV",
168 		ZFMT(VIDEO_PALETTE_YUV422,
169 		     V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
170 		.depth = 16,
171 		.flags = ZORAN_FORMAT_CAPTURE |
172 			 ZORAN_FORMAT_OVERLAY,
173 		.vfespfr = ZR36057_VFESPFR_YUV422,
174 	}, {
175 		.name = "4:2:2, packed, UYVY",
176 		ZFMT(VIDEO_PALETTE_UYVY,
177 		     V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
178 		.depth = 16,
179 		.flags = ZORAN_FORMAT_CAPTURE |
180 			 ZORAN_FORMAT_OVERLAY,
181 		.vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
182 	}, {
183 		.name = "Hardware-encoded Motion-JPEG",
184 		ZFMT(-1,
185 		     V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
186 		.depth = 0,
187 		.flags = ZORAN_FORMAT_CAPTURE |
188 			 ZORAN_FORMAT_PLAYBACK |
189 			 ZORAN_FORMAT_COMPRESSED,
190 	}
191 };
192 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
193 
194 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
195 
196 
197 static int lock_norm;	/* 0 = default 1 = Don't change TV standard (norm) */
198 module_param(lock_norm, int, 0644);
199 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
200 
201 	/* small helper function for calculating buffersizes for v4l2
202 	 * we calculate the nearest higher power-of-two, which
203 	 * will be the recommended buffersize */
204 static __u32
zoran_v4l2_calc_bufsize(struct zoran_jpg_settings * settings)205 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
206 {
207 	__u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
208 	__u32 num = (1024 * 512) / (div);
209 	__u32 result = 2;
210 
211 	num--;
212 	while (num) {
213 		num >>= 1;
214 		result <<= 1;
215 	}
216 
217 	if (result > jpg_bufsize)
218 		return jpg_bufsize;
219 	if (result < 8192)
220 		return 8192;
221 	return result;
222 }
223 
224 /* forward references */
225 static void v4l_fbuffer_free(struct file *file);
226 static void jpg_fbuffer_free(struct file *file);
227 
228 /*
229  *   Allocate the V4L grab buffers
230  *
231  *   These have to be pysically contiguous.
232  *   If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
233  *   else we try to allocate them with bigphysarea_alloc_pages
234  *   if the bigphysarea patch is present in the kernel,
235  *   else we try to use high memory (if the user has bootet
236  *   Linux with the necessary memory left over).
237  */
238 
239 static unsigned long
get_high_mem(unsigned long size)240 get_high_mem (unsigned long size)
241 {
242 /*
243  * Check if there is usable memory at the end of Linux memory
244  * of at least size. Return the physical address of this memory,
245  * return 0 on failure.
246  *
247  * The idea is from Alexandro Rubini's book "Linux device drivers".
248  * The driver from him which is downloadable from O'Reilly's
249  * web site misses the "virt_to_phys(high_memory)" part
250  * (and therefore doesn't work at all - at least with 2.2.x kernels).
251  *
252  * It should be unnecessary to mention that THIS IS DANGEROUS,
253  * if more than one driver at a time has the idea to use this memory!!!!
254  */
255 
256 	volatile unsigned char __iomem *mem;
257 	unsigned char c;
258 	unsigned long hi_mem_ph;
259 	unsigned long i;
260 
261 	/* Map the high memory to user space */
262 
263 	hi_mem_ph = virt_to_phys(high_memory);
264 
265 	mem = ioremap(hi_mem_ph, size);
266 	if (!mem) {
267 		dprintk(1,
268 			KERN_ERR "%s: get_high_mem() - ioremap failed\n",
269 			ZORAN_NAME);
270 		return 0;
271 	}
272 
273 	for (i = 0; i < size; i++) {
274 		/* Check if it is memory */
275 		c = i & 0xff;
276 		writeb(c, mem + i);
277 		if (readb(mem + i) != c)
278 			break;
279 		c = 255 - c;
280 		writeb(c, mem + i);
281 		if (readb(mem + i) != c)
282 			break;
283 		writeb(0, mem + i);	/* zero out memory */
284 
285 		/* give the kernel air to breath */
286 		if ((i & 0x3ffff) == 0x3ffff)
287 			schedule();
288 	}
289 
290 	iounmap(mem);
291 
292 	if (i != size) {
293 		dprintk(1,
294 			KERN_ERR
295 			"%s: get_high_mem() - requested %lu, avail %lu\n",
296 			ZORAN_NAME, size, i);
297 		return 0;
298 	}
299 
300 	return hi_mem_ph;
301 }
302 
303 static int
v4l_fbuffer_alloc(struct file * file)304 v4l_fbuffer_alloc (struct file *file)
305 {
306 	struct zoran_fh *fh = file->private_data;
307 	struct zoran *zr = fh->zr;
308 	int i, off;
309 	unsigned char *mem;
310 	unsigned long pmem = 0;
311 
312 	/* we might have old buffers lying around... */
313 	if (fh->v4l_buffers.ready_to_be_freed) {
314 		v4l_fbuffer_free(file);
315 	}
316 
317 	for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
318 		if (fh->v4l_buffers.buffer[i].fbuffer)
319 			dprintk(2,
320 				KERN_WARNING
321 				"%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
322 				ZR_DEVNAME(zr), i);
323 
324 		//udelay(20);
325 		if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
326 			/* Use kmalloc */
327 
328 			mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
329 			if (!mem) {
330 				dprintk(1,
331 					KERN_ERR
332 					"%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
333 					ZR_DEVNAME(zr), i);
334 				v4l_fbuffer_free(file);
335 				return -ENOBUFS;
336 			}
337 			fh->v4l_buffers.buffer[i].fbuffer = mem;
338 			fh->v4l_buffers.buffer[i].fbuffer_phys =
339 			    virt_to_phys(mem);
340 			fh->v4l_buffers.buffer[i].fbuffer_bus =
341 			    virt_to_bus(mem);
342 			for (off = 0; off < fh->v4l_buffers.buffer_size;
343 			     off += PAGE_SIZE)
344 				SetPageReserved(MAP_NR(mem + off));
345 			dprintk(4,
346 				KERN_INFO
347 				"%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
348 				ZR_DEVNAME(zr), i, (unsigned long) mem,
349 				virt_to_bus(mem));
350 		} else {
351 
352 			/* Use high memory which has been left at boot time */
353 
354 			/* Ok., Ok. this is an evil hack - we make
355 			 * the assumption that physical addresses are
356 			 * the same as bus addresses (true at least
357 			 * for Intel processors). The whole method of
358 			 * obtaining and using this memory is not very
359 			 * nice - but I hope it saves some poor users
360 			 * from kernel hacking, which might have even
361 			 * more evil results */
362 
363 			if (i == 0) {
364 				int size =
365 				    fh->v4l_buffers.num_buffers *
366 				    fh->v4l_buffers.buffer_size;
367 
368 				pmem = get_high_mem(size);
369 				if (pmem == 0) {
370 					dprintk(1,
371 						KERN_ERR
372 						"%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
373 						ZR_DEVNAME(zr), size >> 10);
374 					return -ENOBUFS;
375 				}
376 				fh->v4l_buffers.buffer[0].fbuffer = NULL;
377 				fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
378 				fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
379 				dprintk(4,
380 					KERN_INFO
381 					"%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
382 					ZR_DEVNAME(zr), size >> 10);
383 			} else {
384 				fh->v4l_buffers.buffer[i].fbuffer = NULL;
385 				fh->v4l_buffers.buffer[i].fbuffer_phys =
386 				    pmem + i * fh->v4l_buffers.buffer_size;
387 				fh->v4l_buffers.buffer[i].fbuffer_bus =
388 				    pmem + i * fh->v4l_buffers.buffer_size;
389 			}
390 		}
391 	}
392 
393 	fh->v4l_buffers.allocated = 1;
394 
395 	return 0;
396 }
397 
398 /* free the V4L grab buffers */
399 static void
v4l_fbuffer_free(struct file * file)400 v4l_fbuffer_free (struct file *file)
401 {
402 	struct zoran_fh *fh = file->private_data;
403 	struct zoran *zr = fh->zr;
404 	int i, off;
405 	unsigned char *mem;
406 
407 	dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
408 
409 	for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
410 		if (!fh->v4l_buffers.buffer[i].fbuffer)
411 			continue;
412 
413 		if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
414 			mem = fh->v4l_buffers.buffer[i].fbuffer;
415 			for (off = 0; off < fh->v4l_buffers.buffer_size;
416 			     off += PAGE_SIZE)
417 				ClearPageReserved(MAP_NR(mem + off));
418 			kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
419 		}
420 		fh->v4l_buffers.buffer[i].fbuffer = NULL;
421 	}
422 
423 	fh->v4l_buffers.allocated = 0;
424 	fh->v4l_buffers.ready_to_be_freed = 0;
425 }
426 
427 /*
428  *   Allocate the MJPEG grab buffers.
429  *
430  *   If the requested buffer size is smaller than MAX_KMALLOC_MEM,
431  *   kmalloc is used to request a physically contiguous area,
432  *   else we allocate the memory in framgents with get_zeroed_page.
433  *
434  *   If a Natoma chipset is present and this is a revision 1 zr36057,
435  *   each MJPEG buffer needs to be physically contiguous.
436  *   (RJ: This statement is from Dave Perks' original driver,
437  *   I could never check it because I have a zr36067)
438  *   The driver cares about this because it reduces the buffer
439  *   size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
440  *
441  *   RJ: The contents grab buffers needs never be accessed in the driver.
442  *       Therefore there is no need to allocate them with vmalloc in order
443  *       to get a contiguous virtual memory space.
444  *       I don't understand why many other drivers first allocate them with
445  *       vmalloc (which uses internally also get_zeroed_page, but delivers you
446  *       virtual addresses) and then again have to make a lot of efforts
447  *       to get the physical address.
448  *
449  *   Ben Capper:
450  *       On big-endian architectures (such as ppc) some extra steps
451  *       are needed. When reading and writing to the stat_com array
452  *       and fragment buffers, the device expects to see little-
453  *       endian values. The use of cpu_to_le32() and le32_to_cpu()
454  *       in this function (and one or two others in zoran_device.c)
455  *       ensure that these values are always stored in little-endian
456  *       form, regardless of architecture. The zr36057 does Very Bad
457  *       Things on big endian architectures if the stat_com array
458  *       and fragment buffers are not little-endian.
459  */
460 
461 static int
jpg_fbuffer_alloc(struct file * file)462 jpg_fbuffer_alloc (struct file *file)
463 {
464 	struct zoran_fh *fh = file->private_data;
465 	struct zoran *zr = fh->zr;
466 	int i, j, off;
467 	unsigned long mem;
468 
469 	/* we might have old buffers lying around */
470 	if (fh->jpg_buffers.ready_to_be_freed) {
471 		jpg_fbuffer_free(file);
472 	}
473 
474 	for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
475 		if (fh->jpg_buffers.buffer[i].frag_tab)
476 			dprintk(2,
477 				KERN_WARNING
478 				"%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
479 				ZR_DEVNAME(zr), i);
480 
481 		/* Allocate fragment table for this buffer */
482 
483 		mem = get_zeroed_page(GFP_KERNEL);
484 		if (mem == 0) {
485 			dprintk(1,
486 				KERN_ERR
487 				"%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
488 				ZR_DEVNAME(zr), i);
489 			jpg_fbuffer_free(file);
490 			return -ENOBUFS;
491 		}
492 		fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
493 		fh->jpg_buffers.buffer[i].frag_tab_bus =
494 		    virt_to_bus((void *) mem);
495 
496 		//if (alloc_contig) {
497 		if (fh->jpg_buffers.need_contiguous) {
498 			mem =
499 			    (unsigned long) kmalloc(fh->jpg_buffers.
500 						    buffer_size,
501 						    GFP_KERNEL);
502 			if (mem == 0) {
503 				dprintk(1,
504 					KERN_ERR
505 					"%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
506 					ZR_DEVNAME(zr), i);
507 				jpg_fbuffer_free(file);
508 				return -ENOBUFS;
509 			}
510 			fh->jpg_buffers.buffer[i].frag_tab[0] =
511 			    cpu_to_le32(virt_to_bus((void *) mem));
512 			fh->jpg_buffers.buffer[i].frag_tab[1] =
513 			    cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
514 			for (off = 0; off < fh->jpg_buffers.buffer_size;
515 			     off += PAGE_SIZE)
516 				SetPageReserved(MAP_NR(mem + off));
517 		} else {
518 			/* jpg_bufsize is allreay page aligned */
519 			for (j = 0;
520 			     j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
521 			     j++) {
522 				mem = get_zeroed_page(GFP_KERNEL);
523 				if (mem == 0) {
524 					dprintk(1,
525 						KERN_ERR
526 						"%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
527 						ZR_DEVNAME(zr), i);
528 					jpg_fbuffer_free(file);
529 					return -ENOBUFS;
530 				}
531 
532 				fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
533 				    cpu_to_le32(virt_to_bus((void *) mem));
534 				fh->jpg_buffers.buffer[i].frag_tab[2 * j +
535 								   1] =
536 				    cpu_to_le32((PAGE_SIZE / 4) << 1);
537 				SetPageReserved(MAP_NR(mem));
538 			}
539 
540 			fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
541 		}
542 	}
543 
544 	dprintk(4,
545 		KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
546 		ZR_DEVNAME(zr),
547 		(fh->jpg_buffers.num_buffers *
548 		 fh->jpg_buffers.buffer_size) >> 10);
549 
550 	fh->jpg_buffers.allocated = 1;
551 
552 	return 0;
553 }
554 
555 /* free the MJPEG grab buffers */
556 static void
jpg_fbuffer_free(struct file * file)557 jpg_fbuffer_free (struct file *file)
558 {
559 	struct zoran_fh *fh = file->private_data;
560 	struct zoran *zr = fh->zr;
561 	int i, j, off;
562 	unsigned char *mem;
563 
564 	dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
565 
566 	for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
567 		if (!fh->jpg_buffers.buffer[i].frag_tab)
568 			continue;
569 
570 		//if (alloc_contig) {
571 		if (fh->jpg_buffers.need_contiguous) {
572 			if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
573 				mem = (unsigned char *) bus_to_virt(le32_to_cpu(
574 					fh->jpg_buffers.buffer[i].frag_tab[0]));
575 				for (off = 0;
576 				     off < fh->jpg_buffers.buffer_size;
577 				     off += PAGE_SIZE)
578 					ClearPageReserved(MAP_NR
579 							  (mem + off));
580 				kfree(mem);
581 				fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
582 				fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
583 			}
584 		} else {
585 			for (j = 0;
586 			     j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
587 			     j++) {
588 				if (!fh->jpg_buffers.buffer[i].
589 				    frag_tab[2 * j])
590 					break;
591 				ClearPageReserved(MAP_NR
592 						  (bus_to_virt
593 						   (le32_to_cpu
594 						    (fh->jpg_buffers.
595 						     buffer[i].frag_tab[2 *
596 								       j]))));
597 				free_page((unsigned long)
598 					  bus_to_virt
599 						  (le32_to_cpu
600 						   (fh->jpg_buffers.
601 						      buffer[i].
602 						      frag_tab[2 * j])));
603 				fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
604 				    0;
605 				fh->jpg_buffers.buffer[i].frag_tab[2 * j +
606 								   1] = 0;
607 			}
608 		}
609 
610 		free_page((unsigned long) fh->jpg_buffers.buffer[i].
611 			  frag_tab);
612 		fh->jpg_buffers.buffer[i].frag_tab = NULL;
613 	}
614 
615 	fh->jpg_buffers.allocated = 0;
616 	fh->jpg_buffers.ready_to_be_freed = 0;
617 }
618 
619 /*
620  *   V4L Buffer grabbing
621  */
622 
623 static int
zoran_v4l_set_format(struct file * file,int width,int height,const struct zoran_format * format)624 zoran_v4l_set_format (struct file               *file,
625 		      int                        width,
626 		      int                        height,
627 		      const struct zoran_format *format)
628 {
629 	struct zoran_fh *fh = file->private_data;
630 	struct zoran *zr = fh->zr;
631 	int bpp;
632 
633 	/* Check size and format of the grab wanted */
634 
635 	if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
636 	    height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
637 		dprintk(1,
638 			KERN_ERR
639 			"%s: v4l_set_format() - wrong frame size (%dx%d)\n",
640 			ZR_DEVNAME(zr), width, height);
641 		return -EINVAL;
642 	}
643 
644 	bpp = (format->depth + 7) / 8;
645 
646 	/* Check against available buffer size */
647 	if (height * width * bpp > fh->v4l_buffers.buffer_size) {
648 		dprintk(1,
649 			KERN_ERR
650 			"%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
651 			ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
652 		return -EINVAL;
653 	}
654 
655 	/* The video front end needs 4-byte alinged line sizes */
656 
657 	if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
658 		dprintk(1,
659 			KERN_ERR
660 			"%s: v4l_set_format() - wrong frame alingment\n",
661 			ZR_DEVNAME(zr));
662 		return -EINVAL;
663 	}
664 
665 	fh->v4l_settings.width = width;
666 	fh->v4l_settings.height = height;
667 	fh->v4l_settings.format = format;
668 	fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
669 
670 	return 0;
671 }
672 
673 static int
zoran_v4l_queue_frame(struct file * file,int num)674 zoran_v4l_queue_frame (struct file *file,
675 		       int          num)
676 {
677 	struct zoran_fh *fh = file->private_data;
678 	struct zoran *zr = fh->zr;
679 	unsigned long flags;
680 	int res = 0;
681 
682 	if (!fh->v4l_buffers.allocated) {
683 		dprintk(1,
684 			KERN_ERR
685 			"%s: v4l_queue_frame() - buffers not yet allocated\n",
686 			ZR_DEVNAME(zr));
687 		res = -ENOMEM;
688 	}
689 
690 	/* No grabbing outside the buffer range! */
691 	if (num >= fh->v4l_buffers.num_buffers || num < 0) {
692 		dprintk(1,
693 			KERN_ERR
694 			"%s: v4l_queue_frame() - buffer %d is out of range\n",
695 			ZR_DEVNAME(zr), num);
696 		res = -EINVAL;
697 	}
698 
699 	spin_lock_irqsave(&zr->spinlock, flags);
700 
701 	if (fh->v4l_buffers.active == ZORAN_FREE) {
702 		if (zr->v4l_buffers.active == ZORAN_FREE) {
703 			zr->v4l_buffers = fh->v4l_buffers;
704 			fh->v4l_buffers.active = ZORAN_ACTIVE;
705 		} else {
706 			dprintk(1,
707 				KERN_ERR
708 				"%s: v4l_queue_frame() - another session is already capturing\n",
709 				ZR_DEVNAME(zr));
710 			res = -EBUSY;
711 		}
712 	}
713 
714 	/* make sure a grab isn't going on currently with this buffer */
715 	if (!res) {
716 		switch (zr->v4l_buffers.buffer[num].state) {
717 		default:
718 		case BUZ_STATE_PEND:
719 			if (zr->v4l_buffers.active == ZORAN_FREE) {
720 				fh->v4l_buffers.active = ZORAN_FREE;
721 				zr->v4l_buffers.allocated = 0;
722 			}
723 			res = -EBUSY;	/* what are you doing? */
724 			break;
725 		case BUZ_STATE_DONE:
726 			dprintk(2,
727 				KERN_WARNING
728 				"%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
729 				ZR_DEVNAME(zr), num);
730 		case BUZ_STATE_USER:
731 			/* since there is at least one unused buffer there's room for at least
732 			 * one more pend[] entry */
733 			zr->v4l_pend[zr->v4l_pend_head++ &
734 					V4L_MASK_FRAME] = num;
735 			zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
736 			zr->v4l_buffers.buffer[num].bs.length =
737 			    fh->v4l_settings.bytesperline *
738 			    zr->v4l_settings.height;
739 			fh->v4l_buffers.buffer[num] =
740 			    zr->v4l_buffers.buffer[num];
741 			break;
742 		}
743 	}
744 
745 	spin_unlock_irqrestore(&zr->spinlock, flags);
746 
747 	if (!res && zr->v4l_buffers.active == ZORAN_FREE)
748 		zr->v4l_buffers.active = fh->v4l_buffers.active;
749 
750 	return res;
751 }
752 
753 static int
v4l_grab(struct file * file,struct video_mmap * mp)754 v4l_grab (struct file       *file,
755 	  struct video_mmap *mp)
756 {
757 	struct zoran_fh *fh = file->private_data;
758 	struct zoran *zr = fh->zr;
759 	int res = 0, i;
760 
761 	for (i = 0; i < NUM_FORMATS; i++) {
762 		if (zoran_formats[i].palette == mp->format &&
763 		    zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
764 		    !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
765 			break;
766 	}
767 	if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
768 		dprintk(1,
769 			KERN_ERR
770 			"%s: v4l_grab() - wrong bytes-per-pixel format\n",
771 			ZR_DEVNAME(zr));
772 		return -EINVAL;
773 	}
774 
775 	/*
776 	 * To minimize the time spent in the IRQ routine, we avoid setting up
777 	 * the video front end there.
778 	 * If this grab has different parameters from a running streaming capture
779 	 * we stop the streaming capture and start it over again.
780 	 */
781 	if (zr->v4l_memgrab_active &&
782 	    (zr->v4l_settings.width != mp->width ||
783 	     zr->v4l_settings.height != mp->height ||
784 	     zr->v4l_settings.format->palette != mp->format)) {
785 		res = wait_grab_pending(zr);
786 		if (res)
787 			return res;
788 	}
789 	if ((res = zoran_v4l_set_format(file,
790 					mp->width,
791 					mp->height,
792 					&zoran_formats[i])))
793 		return res;
794 	zr->v4l_settings = fh->v4l_settings;
795 
796 	/* queue the frame in the pending queue */
797 	if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
798 		fh->v4l_buffers.active = ZORAN_FREE;
799 		return res;
800 	}
801 
802 	/* put the 36057 into frame grabbing mode */
803 	if (!res && !zr->v4l_memgrab_active)
804 		zr36057_set_memgrab(zr, 1);
805 
806 	//dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
807 
808 	return res;
809 }
810 
811 /*
812  * Sync on a V4L buffer
813  */
814 
815 static int
v4l_sync(struct file * file,int frame)816 v4l_sync (struct file *file,
817 	  int          frame)
818 {
819 	struct zoran_fh *fh = file->private_data;
820 	struct zoran *zr = fh->zr;
821 	unsigned long flags;
822 
823 	if (fh->v4l_buffers.active == ZORAN_FREE) {
824 		dprintk(1,
825 			KERN_ERR
826 			"%s: v4l_sync() - no grab active for this session\n",
827 			ZR_DEVNAME(zr));
828 		return -EINVAL;
829 	}
830 
831 	/* check passed-in frame number */
832 	if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
833 		dprintk(1,
834 			KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
835 			ZR_DEVNAME(zr), frame);
836 		return -EINVAL;
837 	}
838 
839 	/* Check if is buffer was queued at all */
840 	if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
841 		dprintk(1,
842 			KERN_ERR
843 			"%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
844 			ZR_DEVNAME(zr));
845 		return -EPROTO;
846 	}
847 
848 	/* wait on this buffer to get ready */
849 	if (!wait_event_interruptible_timeout(zr->v4l_capq,
850 				(zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
851 				10*HZ))
852 		return -ETIME;
853 	if (signal_pending(current))
854 		return -ERESTARTSYS;
855 
856 	/* buffer should now be in BUZ_STATE_DONE */
857 	if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
858 		dprintk(2,
859 			KERN_ERR "%s: v4l_sync() - internal state error\n",
860 			ZR_DEVNAME(zr));
861 
862 	zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
863 	fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
864 
865 	spin_lock_irqsave(&zr->spinlock, flags);
866 
867 	/* Check if streaming capture has finished */
868 	if (zr->v4l_pend_tail == zr->v4l_pend_head) {
869 		zr36057_set_memgrab(zr, 0);
870 		if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
871 			fh->v4l_buffers.active = zr->v4l_buffers.active =
872 			    ZORAN_FREE;
873 			zr->v4l_buffers.allocated = 0;
874 		}
875 	}
876 
877 	spin_unlock_irqrestore(&zr->spinlock, flags);
878 
879 	return 0;
880 }
881 
882 /*
883  *   Queue a MJPEG buffer for capture/playback
884  */
885 
886 static int
zoran_jpg_queue_frame(struct file * file,int num,enum zoran_codec_mode mode)887 zoran_jpg_queue_frame (struct file          *file,
888 		       int                   num,
889 		       enum zoran_codec_mode mode)
890 {
891 	struct zoran_fh *fh = file->private_data;
892 	struct zoran *zr = fh->zr;
893 	unsigned long flags;
894 	int res = 0;
895 
896 	/* Check if buffers are allocated */
897 	if (!fh->jpg_buffers.allocated) {
898 		dprintk(1,
899 			KERN_ERR
900 			"%s: jpg_queue_frame() - buffers not yet allocated\n",
901 			ZR_DEVNAME(zr));
902 		return -ENOMEM;
903 	}
904 
905 	/* No grabbing outside the buffer range! */
906 	if (num >= fh->jpg_buffers.num_buffers || num < 0) {
907 		dprintk(1,
908 			KERN_ERR
909 			"%s: jpg_queue_frame() - buffer %d out of range\n",
910 			ZR_DEVNAME(zr), num);
911 		return -EINVAL;
912 	}
913 
914 	/* what is the codec mode right now? */
915 	if (zr->codec_mode == BUZ_MODE_IDLE) {
916 		zr->jpg_settings = fh->jpg_settings;
917 	} else if (zr->codec_mode != mode) {
918 		/* wrong codec mode active - invalid */
919 		dprintk(1,
920 			KERN_ERR
921 			"%s: jpg_queue_frame() - codec in wrong mode\n",
922 			ZR_DEVNAME(zr));
923 		return -EINVAL;
924 	}
925 
926 	if (fh->jpg_buffers.active == ZORAN_FREE) {
927 		if (zr->jpg_buffers.active == ZORAN_FREE) {
928 			zr->jpg_buffers = fh->jpg_buffers;
929 			fh->jpg_buffers.active = ZORAN_ACTIVE;
930 		} else {
931 			dprintk(1,
932 				KERN_ERR
933 				"%s: jpg_queue_frame() - another session is already capturing\n",
934 				ZR_DEVNAME(zr));
935 			res = -EBUSY;
936 		}
937 	}
938 
939 	if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
940 		/* Ok load up the jpeg codec */
941 		zr36057_enable_jpg(zr, mode);
942 	}
943 
944 	spin_lock_irqsave(&zr->spinlock, flags);
945 
946 	if (!res) {
947 		switch (zr->jpg_buffers.buffer[num].state) {
948 		case BUZ_STATE_DONE:
949 			dprintk(2,
950 				KERN_WARNING
951 				"%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
952 				ZR_DEVNAME(zr));
953 		case BUZ_STATE_USER:
954 			/* since there is at least one unused buffer there's room for at
955 			 *least one more pend[] entry */
956 			zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
957 			    num;
958 			zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
959 			fh->jpg_buffers.buffer[num] =
960 			    zr->jpg_buffers.buffer[num];
961 			zoran_feed_stat_com(zr);
962 			break;
963 		default:
964 		case BUZ_STATE_DMA:
965 		case BUZ_STATE_PEND:
966 			if (zr->jpg_buffers.active == ZORAN_FREE) {
967 				fh->jpg_buffers.active = ZORAN_FREE;
968 				zr->jpg_buffers.allocated = 0;
969 			}
970 			res = -EBUSY;	/* what are you doing? */
971 			break;
972 		}
973 	}
974 
975 	spin_unlock_irqrestore(&zr->spinlock, flags);
976 
977 	if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
978 		zr->jpg_buffers.active = fh->jpg_buffers.active;
979 	}
980 
981 	return res;
982 }
983 
984 static int
jpg_qbuf(struct file * file,int frame,enum zoran_codec_mode mode)985 jpg_qbuf (struct file          *file,
986 	  int                   frame,
987 	  enum zoran_codec_mode mode)
988 {
989 	struct zoran_fh *fh = file->private_data;
990 	struct zoran *zr = fh->zr;
991 	int res = 0;
992 
993 	/* Does the user want to stop streaming? */
994 	if (frame < 0) {
995 		if (zr->codec_mode == mode) {
996 			if (fh->jpg_buffers.active == ZORAN_FREE) {
997 				dprintk(1,
998 					KERN_ERR
999 					"%s: jpg_qbuf(-1) - session not active\n",
1000 					ZR_DEVNAME(zr));
1001 				return -EINVAL;
1002 			}
1003 			fh->jpg_buffers.active = zr->jpg_buffers.active =
1004 			    ZORAN_FREE;
1005 			zr->jpg_buffers.allocated = 0;
1006 			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1007 			return 0;
1008 		} else {
1009 			dprintk(1,
1010 				KERN_ERR
1011 				"%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1012 				ZR_DEVNAME(zr));
1013 			return -EINVAL;
1014 		}
1015 	}
1016 
1017 	if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1018 		return res;
1019 
1020 	/* Start the jpeg codec when the first frame is queued  */
1021 	if (!res && zr->jpg_que_head == 1)
1022 		jpeg_start(zr);
1023 
1024 	return res;
1025 }
1026 
1027 /*
1028  *   Sync on a MJPEG buffer
1029  */
1030 
1031 static int
jpg_sync(struct file * file,struct zoran_sync * bs)1032 jpg_sync (struct file       *file,
1033 	  struct zoran_sync *bs)
1034 {
1035 	struct zoran_fh *fh = file->private_data;
1036 	struct zoran *zr = fh->zr;
1037 	unsigned long flags;
1038 	int frame;
1039 
1040 	if (fh->jpg_buffers.active == ZORAN_FREE) {
1041 		dprintk(1,
1042 			KERN_ERR
1043 			"%s: jpg_sync() - capture is not currently active\n",
1044 			ZR_DEVNAME(zr));
1045 		return -EINVAL;
1046 	}
1047 	if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1048 	    zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1049 		dprintk(1,
1050 			KERN_ERR
1051 			"%s: jpg_sync() - codec not in streaming mode\n",
1052 			ZR_DEVNAME(zr));
1053 		return -EINVAL;
1054 	}
1055 	if (!wait_event_interruptible_timeout(zr->jpg_capq,
1056 			(zr->jpg_que_tail != zr->jpg_dma_tail ||
1057 			 zr->jpg_dma_tail == zr->jpg_dma_head),
1058 			10*HZ)) {
1059 		int isr;
1060 
1061 		btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1062 		udelay(1);
1063 		zr->codec->control(zr->codec, CODEC_G_STATUS,
1064 					   sizeof(isr), &isr);
1065 		dprintk(1,
1066 			KERN_ERR
1067 			"%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1068 			ZR_DEVNAME(zr), isr);
1069 
1070 		return -ETIME;
1071 
1072 	}
1073 	if (signal_pending(current))
1074 		return -ERESTARTSYS;
1075 
1076 	spin_lock_irqsave(&zr->spinlock, flags);
1077 
1078 	if (zr->jpg_dma_tail != zr->jpg_dma_head)
1079 		frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1080 	else
1081 		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1082 
1083 	/* buffer should now be in BUZ_STATE_DONE */
1084 	if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1085 		dprintk(2,
1086 			KERN_ERR "%s: jpg_sync() - internal state error\n",
1087 			ZR_DEVNAME(zr));
1088 
1089 	*bs = zr->jpg_buffers.buffer[frame].bs;
1090 	bs->frame = frame;
1091 	zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1092 	fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1093 
1094 	spin_unlock_irqrestore(&zr->spinlock, flags);
1095 
1096 	return 0;
1097 }
1098 
1099 static void
zoran_open_init_session(struct file * file)1100 zoran_open_init_session (struct file *file)
1101 {
1102 	int i;
1103 	struct zoran_fh *fh = file->private_data;
1104 	struct zoran *zr = fh->zr;
1105 
1106 	/* Per default, map the V4L Buffers */
1107 	fh->map_mode = ZORAN_MAP_MODE_RAW;
1108 
1109 	/* take over the card's current settings */
1110 	fh->overlay_settings = zr->overlay_settings;
1111 	fh->overlay_settings.is_set = 0;
1112 	fh->overlay_settings.format = zr->overlay_settings.format;
1113 	fh->overlay_active = ZORAN_FREE;
1114 
1115 	/* v4l settings */
1116 	fh->v4l_settings = zr->v4l_settings;
1117 
1118 	/* v4l_buffers */
1119 	memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1120 	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1121 		fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
1122 		fh->v4l_buffers.buffer[i].bs.frame = i;
1123 	}
1124 	fh->v4l_buffers.allocated = 0;
1125 	fh->v4l_buffers.ready_to_be_freed = 0;
1126 	fh->v4l_buffers.active = ZORAN_FREE;
1127 	fh->v4l_buffers.buffer_size = v4l_bufsize;
1128 	fh->v4l_buffers.num_buffers = v4l_nbufs;
1129 
1130 	/* jpg settings */
1131 	fh->jpg_settings = zr->jpg_settings;
1132 
1133 	/* jpg_buffers */
1134 	memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1135 	for (i = 0; i < BUZ_MAX_FRAME; i++) {
1136 		fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
1137 		fh->jpg_buffers.buffer[i].bs.frame = i;
1138 	}
1139 	fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1140 	fh->jpg_buffers.allocated = 0;
1141 	fh->jpg_buffers.ready_to_be_freed = 0;
1142 	fh->jpg_buffers.active = ZORAN_FREE;
1143 	fh->jpg_buffers.buffer_size = jpg_bufsize;
1144 	fh->jpg_buffers.num_buffers = jpg_nbufs;
1145 }
1146 
1147 static void
zoran_close_end_session(struct file * file)1148 zoran_close_end_session (struct file *file)
1149 {
1150 	struct zoran_fh *fh = file->private_data;
1151 	struct zoran *zr = fh->zr;
1152 
1153 	/* overlay */
1154 	if (fh->overlay_active != ZORAN_FREE) {
1155 		fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1156 		zr->v4l_overlay_active = 0;
1157 		if (!zr->v4l_memgrab_active)
1158 			zr36057_overlay(zr, 0);
1159 		zr->overlay_mask = NULL;
1160 	}
1161 
1162 	/* v4l capture */
1163 	if (fh->v4l_buffers.active != ZORAN_FREE) {
1164 		unsigned long flags;
1165 
1166 		spin_lock_irqsave(&zr->spinlock, flags);
1167 		zr36057_set_memgrab(zr, 0);
1168 		zr->v4l_buffers.allocated = 0;
1169 		zr->v4l_buffers.active = fh->v4l_buffers.active =
1170 		    ZORAN_FREE;
1171 		spin_unlock_irqrestore(&zr->spinlock, flags);
1172 	}
1173 
1174 	/* v4l buffers */
1175 	if (fh->v4l_buffers.allocated ||
1176 	    fh->v4l_buffers.ready_to_be_freed) {
1177 		v4l_fbuffer_free(file);
1178 	}
1179 
1180 	/* jpg capture */
1181 	if (fh->jpg_buffers.active != ZORAN_FREE) {
1182 		zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1183 		zr->jpg_buffers.allocated = 0;
1184 		zr->jpg_buffers.active = fh->jpg_buffers.active =
1185 		    ZORAN_FREE;
1186 	}
1187 
1188 	/* jpg buffers */
1189 	if (fh->jpg_buffers.allocated ||
1190 	    fh->jpg_buffers.ready_to_be_freed) {
1191 		jpg_fbuffer_free(file);
1192 	}
1193 }
1194 
1195 /*
1196  *   Open a zoran card. Right now the flags stuff is just playing
1197  */
1198 
zoran_open(struct file * file)1199 static int zoran_open(struct file *file)
1200 {
1201 	struct zoran *zr = video_drvdata(file);
1202 	struct zoran_fh *fh;
1203 	int res, first_open = 0;
1204 
1205 	dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1206 		ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
1207 
1208 	lock_kernel();
1209 
1210 	/* see fs/device.c - the kernel already locks during open(),
1211 	 * so locking ourselves only causes deadlocks */
1212 	/*mutex_lock(&zr->resource_lock);*/
1213 
1214 	if (zr->user >= 2048) {
1215 		dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1216 			ZR_DEVNAME(zr), zr->user);
1217 		res = -EBUSY;
1218 		goto fail_unlock;
1219 	}
1220 
1221 	if (!zr->decoder) {
1222 		dprintk(1,
1223 			KERN_ERR "%s: no TV decoder loaded for device!\n",
1224 			ZR_DEVNAME(zr));
1225 		res = -EIO;
1226 		goto fail_unlock;
1227 	}
1228 
1229 	if (!try_module_get(zr->decoder->driver->driver.owner)) {
1230 		dprintk(1,
1231 			KERN_ERR
1232 			"%s: failed to grab ownership of video decoder\n",
1233 			ZR_DEVNAME(zr));
1234 		res = -EIO;
1235 		goto fail_unlock;
1236 	}
1237 	if (zr->encoder &&
1238 	    !try_module_get(zr->encoder->driver->driver.owner)) {
1239 		dprintk(1,
1240 			KERN_ERR
1241 			"%s: failed to grab ownership of video encoder\n",
1242 			ZR_DEVNAME(zr));
1243 		res = -EIO;
1244 		goto fail_decoder;
1245 	}
1246 
1247 	/* now, create the open()-specific file_ops struct */
1248 	fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1249 	if (!fh) {
1250 		dprintk(1,
1251 			KERN_ERR
1252 			"%s: zoran_open() - allocation of zoran_fh failed\n",
1253 			ZR_DEVNAME(zr));
1254 		res = -ENOMEM;
1255 		goto fail_encoder;
1256 	}
1257 	/* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1258 	 * on norm-change! */
1259 	fh->overlay_mask =
1260 	    kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1261 	if (!fh->overlay_mask) {
1262 		dprintk(1,
1263 			KERN_ERR
1264 			"%s: zoran_open() - allocation of overlay_mask failed\n",
1265 			ZR_DEVNAME(zr));
1266 		res = -ENOMEM;
1267 		goto fail_fh;
1268 	}
1269 
1270 	if (zr->user++ == 0)
1271 		first_open = 1;
1272 
1273 	/*mutex_unlock(&zr->resource_lock);*/
1274 
1275 	/* default setup - TODO: look at flags */
1276 	if (first_open) {	/* First device open */
1277 		zr36057_restart(zr);
1278 		zoran_open_init_params(zr);
1279 		zoran_init_hardware(zr);
1280 
1281 		btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1282 	}
1283 
1284 	/* set file_ops stuff */
1285 	file->private_data = fh;
1286 	fh->zr = zr;
1287 	zoran_open_init_session(file);
1288 	unlock_kernel();
1289 
1290 	return 0;
1291 
1292 fail_fh:
1293 	kfree(fh);
1294 fail_encoder:
1295 	if (zr->encoder)
1296 		module_put(zr->encoder->driver->driver.owner);
1297 fail_decoder:
1298 	module_put(zr->decoder->driver->driver.owner);
1299 fail_unlock:
1300 	unlock_kernel();
1301 
1302 	dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1303 		ZR_DEVNAME(zr), res, zr->user);
1304 
1305 	return res;
1306 }
1307 
1308 static int
zoran_close(struct file * file)1309 zoran_close(struct file  *file)
1310 {
1311 	struct zoran_fh *fh = file->private_data;
1312 	struct zoran *zr = fh->zr;
1313 
1314 	dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1315 		ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1316 
1317 	/* kernel locks (fs/device.c), so don't do that ourselves
1318 	 * (prevents deadlocks) */
1319 	/*mutex_lock(&zr->resource_lock);*/
1320 
1321 	zoran_close_end_session(file);
1322 
1323 	if (zr->user-- == 1) {	/* Last process */
1324 		/* Clean up JPEG process */
1325 		wake_up_interruptible(&zr->jpg_capq);
1326 		zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1327 		zr->jpg_buffers.allocated = 0;
1328 		zr->jpg_buffers.active = ZORAN_FREE;
1329 
1330 		/* disable interrupts */
1331 		btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1332 
1333 		if (zr36067_debug > 1)
1334 			print_interrupts(zr);
1335 
1336 		/* Overlay off */
1337 		zr->v4l_overlay_active = 0;
1338 		zr36057_overlay(zr, 0);
1339 		zr->overlay_mask = NULL;
1340 
1341 		/* capture off */
1342 		wake_up_interruptible(&zr->v4l_capq);
1343 		zr36057_set_memgrab(zr, 0);
1344 		zr->v4l_buffers.allocated = 0;
1345 		zr->v4l_buffers.active = ZORAN_FREE;
1346 		zoran_set_pci_master(zr, 0);
1347 
1348 		if (!pass_through) {	/* Switch to color bar */
1349 			int zero = 0, two = 2;
1350 			decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1351 			encoder_command(zr, ENCODER_SET_INPUT, &two);
1352 		}
1353 	}
1354 
1355 	file->private_data = NULL;
1356 	kfree(fh->overlay_mask);
1357 	kfree(fh);
1358 
1359 	/* release locks on the i2c modules */
1360 	module_put(zr->decoder->driver->driver.owner);
1361 	if (zr->encoder)
1362 		module_put(zr->encoder->driver->driver.owner);
1363 
1364 	/*mutex_unlock(&zr->resource_lock);*/
1365 
1366 	dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1367 
1368 	return 0;
1369 }
1370 
1371 
1372 static ssize_t
zoran_read(struct file * file,char __user * data,size_t count,loff_t * ppos)1373 zoran_read (struct file *file,
1374 	    char        __user *data,
1375 	    size_t       count,
1376 	    loff_t      *ppos)
1377 {
1378 	/* we simply don't support read() (yet)... */
1379 
1380 	return -EINVAL;
1381 }
1382 
1383 static ssize_t
zoran_write(struct file * file,const char __user * data,size_t count,loff_t * ppos)1384 zoran_write (struct file *file,
1385 	     const char  __user *data,
1386 	     size_t       count,
1387 	     loff_t      *ppos)
1388 {
1389 	/* ...and the same goes for write() */
1390 
1391 	return -EINVAL;
1392 }
1393 
1394 static int
setup_fbuffer(struct file * file,void * base,const struct zoran_format * fmt,int width,int height,int bytesperline)1395 setup_fbuffer (struct file               *file,
1396 	       void                      *base,
1397 	       const struct zoran_format *fmt,
1398 	       int                        width,
1399 	       int                        height,
1400 	       int                        bytesperline)
1401 {
1402 	struct zoran_fh *fh = file->private_data;
1403 	struct zoran *zr = fh->zr;
1404 
1405 	/* (Ronald) v4l/v4l2 guidelines */
1406 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1407 		return -EPERM;
1408 
1409 	/* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1410 	   ALi Magik (that needs very low latency while the card needs a
1411 	   higher value always) */
1412 
1413 	if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1414 		return -ENXIO;
1415 
1416 	/* we need a bytesperline value, even if not given */
1417 	if (!bytesperline)
1418 		bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1419 
1420 #if 0
1421 	if (zr->overlay_active) {
1422 		/* dzjee... stupid users... don't even bother to turn off
1423 		 * overlay before changing the memory location...
1424 		 * normally, we would return errors here. However, one of
1425 		 * the tools that does this is... xawtv! and since xawtv
1426 		 * is used by +/- 99% of the users, we'd rather be user-
1427 		 * friendly and silently do as if nothing went wrong */
1428 		dprintk(3,
1429 			KERN_ERR
1430 			"%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1431 			ZR_DEVNAME(zr));
1432 		zr36057_overlay(zr, 0);
1433 	}
1434 #endif
1435 
1436 	if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1437 		dprintk(1,
1438 			KERN_ERR
1439 			"%s: setup_fbuffer() - no valid overlay format given\n",
1440 			ZR_DEVNAME(zr));
1441 		return -EINVAL;
1442 	}
1443 	if (height <= 0 || width <= 0 || bytesperline <= 0) {
1444 		dprintk(1,
1445 			KERN_ERR
1446 			"%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1447 			ZR_DEVNAME(zr), width, height, bytesperline);
1448 		return -EINVAL;
1449 	}
1450 	if (bytesperline & 3) {
1451 		dprintk(1,
1452 			KERN_ERR
1453 			"%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1454 			ZR_DEVNAME(zr), bytesperline);
1455 		return -EINVAL;
1456 	}
1457 
1458 	zr->buffer.base = (void *) ((unsigned long) base & ~3);
1459 	zr->buffer.height = height;
1460 	zr->buffer.width = width;
1461 	zr->buffer.depth = fmt->depth;
1462 	zr->overlay_settings.format = fmt;
1463 	zr->buffer.bytesperline = bytesperline;
1464 
1465 	/* The user should set new window parameters */
1466 	zr->overlay_settings.is_set = 0;
1467 
1468 	return 0;
1469 }
1470 
1471 
1472 static int
setup_window(struct file * file,int x,int y,int width,int height,struct video_clip __user * clips,int clipcount,void __user * bitmap)1473 setup_window (struct file       *file,
1474 	      int                x,
1475 	      int                y,
1476 	      int                width,
1477 	      int                height,
1478 	      struct video_clip __user *clips,
1479 	      int                clipcount,
1480 	      void              __user *bitmap)
1481 {
1482 	struct zoran_fh *fh = file->private_data;
1483 	struct zoran *zr = fh->zr;
1484 	struct video_clip *vcp = NULL;
1485 	int on, end;
1486 
1487 
1488 	if (!zr->buffer.base) {
1489 		dprintk(1,
1490 			KERN_ERR
1491 			"%s: setup_window() - frame buffer has to be set first\n",
1492 			ZR_DEVNAME(zr));
1493 		return -EINVAL;
1494 	}
1495 
1496 	if (!fh->overlay_settings.format) {
1497 		dprintk(1,
1498 			KERN_ERR
1499 			"%s: setup_window() - no overlay format set\n",
1500 			ZR_DEVNAME(zr));
1501 		return -EINVAL;
1502 	}
1503 
1504 	/*
1505 	 * The video front end needs 4-byte alinged line sizes, we correct that
1506 	 * silently here if necessary
1507 	 */
1508 	if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1509 		end = (x + width) & ~1;	/* round down */
1510 		x = (x + 1) & ~1;	/* round up */
1511 		width = end - x;
1512 	}
1513 
1514 	if (zr->buffer.depth == 24) {
1515 		end = (x + width) & ~3;	/* round down */
1516 		x = (x + 3) & ~3;	/* round up */
1517 		width = end - x;
1518 	}
1519 
1520 	if (width > BUZ_MAX_WIDTH)
1521 		width = BUZ_MAX_WIDTH;
1522 	if (height > BUZ_MAX_HEIGHT)
1523 		height = BUZ_MAX_HEIGHT;
1524 
1525 	/* Check for vaild parameters */
1526 	if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1527 	    width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1528 		dprintk(1,
1529 			KERN_ERR
1530 			"%s: setup_window() - width = %d or height = %d invalid\n",
1531 			ZR_DEVNAME(zr), width, height);
1532 		return -EINVAL;
1533 	}
1534 
1535 	fh->overlay_settings.x = x;
1536 	fh->overlay_settings.y = y;
1537 	fh->overlay_settings.width = width;
1538 	fh->overlay_settings.height = height;
1539 	fh->overlay_settings.clipcount = clipcount;
1540 
1541 	/*
1542 	 * If an overlay is running, we have to switch it off
1543 	 * and switch it on again in order to get the new settings in effect.
1544 	 *
1545 	 * We also want to avoid that the overlay mask is written
1546 	 * when an overlay is running.
1547 	 */
1548 
1549 	on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1550 	    zr->overlay_active != ZORAN_FREE &&
1551 	    fh->overlay_active != ZORAN_FREE;
1552 	if (on)
1553 		zr36057_overlay(zr, 0);
1554 
1555 	/*
1556 	 *   Write the overlay mask if clips are wanted.
1557 	 *   We prefer a bitmap.
1558 	 */
1559 	if (bitmap) {
1560 		/* fake value - it just means we want clips */
1561 		fh->overlay_settings.clipcount = 1;
1562 
1563 		if (copy_from_user(fh->overlay_mask, bitmap,
1564 				   (width * height + 7) / 8)) {
1565 			return -EFAULT;
1566 		}
1567 	} else if (clipcount > 0) {
1568 		/* write our own bitmap from the clips */
1569 		vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1570 		if (vcp == NULL) {
1571 			dprintk(1,
1572 				KERN_ERR
1573 				"%s: setup_window() - Alloc of clip mask failed\n",
1574 				ZR_DEVNAME(zr));
1575 			return -ENOMEM;
1576 		}
1577 		if (copy_from_user
1578 		    (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1579 			vfree(vcp);
1580 			return -EFAULT;
1581 		}
1582 		write_overlay_mask(file, vcp, clipcount);
1583 		vfree(vcp);
1584 	}
1585 
1586 	fh->overlay_settings.is_set = 1;
1587 	if (fh->overlay_active != ZORAN_FREE &&
1588 	    zr->overlay_active != ZORAN_FREE)
1589 		zr->overlay_settings = fh->overlay_settings;
1590 
1591 	if (on)
1592 		zr36057_overlay(zr, 1);
1593 
1594 	/* Make sure the changes come into effect */
1595 	return wait_grab_pending(zr);
1596 }
1597 
1598 static int
setup_overlay(struct file * file,int on)1599 setup_overlay (struct file *file,
1600 	       int          on)
1601 {
1602 	struct zoran_fh *fh = file->private_data;
1603 	struct zoran *zr = fh->zr;
1604 
1605 	/* If there is nothing to do, return immediatly */
1606 	if ((on && fh->overlay_active != ZORAN_FREE) ||
1607 	    (!on && fh->overlay_active == ZORAN_FREE))
1608 		return 0;
1609 
1610 	/* check whether we're touching someone else's overlay */
1611 	if (on && zr->overlay_active != ZORAN_FREE &&
1612 	    fh->overlay_active == ZORAN_FREE) {
1613 		dprintk(1,
1614 			KERN_ERR
1615 			"%s: setup_overlay() - overlay is already active for another session\n",
1616 			ZR_DEVNAME(zr));
1617 		return -EBUSY;
1618 	}
1619 	if (!on && zr->overlay_active != ZORAN_FREE &&
1620 	    fh->overlay_active == ZORAN_FREE) {
1621 		dprintk(1,
1622 			KERN_ERR
1623 			"%s: setup_overlay() - you cannot cancel someone else's session\n",
1624 			ZR_DEVNAME(zr));
1625 		return -EPERM;
1626 	}
1627 
1628 	if (on == 0) {
1629 		zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1630 		zr->v4l_overlay_active = 0;
1631 		/* When a grab is running, the video simply
1632 		 * won't be switched on any more */
1633 		if (!zr->v4l_memgrab_active)
1634 			zr36057_overlay(zr, 0);
1635 		zr->overlay_mask = NULL;
1636 	} else {
1637 		if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1638 			dprintk(1,
1639 				KERN_ERR
1640 				"%s: setup_overlay() - buffer or window not set\n",
1641 				ZR_DEVNAME(zr));
1642 			return -EINVAL;
1643 		}
1644 		if (!fh->overlay_settings.format) {
1645 			dprintk(1,
1646 				KERN_ERR
1647 				"%s: setup_overlay() - no overlay format set\n",
1648 				ZR_DEVNAME(zr));
1649 			return -EINVAL;
1650 		}
1651 		zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1652 		zr->v4l_overlay_active = 1;
1653 		zr->overlay_mask = fh->overlay_mask;
1654 		zr->overlay_settings = fh->overlay_settings;
1655 		if (!zr->v4l_memgrab_active)
1656 			zr36057_overlay(zr, 1);
1657 		/* When a grab is running, the video will be
1658 		 * switched on when grab is finished */
1659 	}
1660 
1661 	/* Make sure the changes come into effect */
1662 	return wait_grab_pending(zr);
1663 }
1664 
1665 	/* get the status of a buffer in the clients buffer queue */
1666 static int
zoran_v4l2_buffer_status(struct file * file,struct v4l2_buffer * buf,int num)1667 zoran_v4l2_buffer_status (struct file        *file,
1668 			  struct v4l2_buffer *buf,
1669 			  int                 num)
1670 {
1671 	struct zoran_fh *fh = file->private_data;
1672 	struct zoran *zr = fh->zr;
1673 
1674 	buf->flags = V4L2_BUF_FLAG_MAPPED;
1675 
1676 	switch (fh->map_mode) {
1677 	case ZORAN_MAP_MODE_RAW:
1678 
1679 		/* check range */
1680 		if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1681 		    !fh->v4l_buffers.allocated) {
1682 			dprintk(1,
1683 				KERN_ERR
1684 				"%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1685 				ZR_DEVNAME(zr));
1686 			return -EINVAL;
1687 		}
1688 
1689 		buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1690 		buf->length = fh->v4l_buffers.buffer_size;
1691 
1692 		/* get buffer */
1693 		buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1694 		if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1695 		    fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1696 			buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1697 			buf->flags |= V4L2_BUF_FLAG_DONE;
1698 			buf->timestamp =
1699 			    fh->v4l_buffers.buffer[num].bs.timestamp;
1700 		} else {
1701 			buf->flags |= V4L2_BUF_FLAG_QUEUED;
1702 		}
1703 
1704 		if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1705 			buf->field = V4L2_FIELD_TOP;
1706 		else
1707 			buf->field = V4L2_FIELD_INTERLACED;
1708 
1709 		break;
1710 
1711 	case ZORAN_MAP_MODE_JPG_REC:
1712 	case ZORAN_MAP_MODE_JPG_PLAY:
1713 
1714 		/* check range */
1715 		if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1716 		    !fh->jpg_buffers.allocated) {
1717 			dprintk(1,
1718 				KERN_ERR
1719 				"%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1720 				ZR_DEVNAME(zr));
1721 			return -EINVAL;
1722 		}
1723 
1724 		buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1725 			      V4L2_BUF_TYPE_VIDEO_CAPTURE :
1726 			      V4L2_BUF_TYPE_VIDEO_OUTPUT;
1727 		buf->length = fh->jpg_buffers.buffer_size;
1728 
1729 		/* these variables are only written after frame has been captured */
1730 		if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1731 		    fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1732 			buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1733 			buf->timestamp =
1734 			    fh->jpg_buffers.buffer[num].bs.timestamp;
1735 			buf->bytesused =
1736 			    fh->jpg_buffers.buffer[num].bs.length;
1737 			buf->flags |= V4L2_BUF_FLAG_DONE;
1738 		} else {
1739 			buf->flags |= V4L2_BUF_FLAG_QUEUED;
1740 		}
1741 
1742 		/* which fields are these? */
1743 		if (fh->jpg_settings.TmpDcm != 1)
1744 			buf->field =
1745 			    fh->jpg_settings.
1746 			    odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1747 		else
1748 			buf->field =
1749 			    fh->jpg_settings.
1750 			    odd_even ? V4L2_FIELD_SEQ_TB :
1751 			    V4L2_FIELD_SEQ_BT;
1752 
1753 		break;
1754 
1755 	default:
1756 
1757 		dprintk(5,
1758 			KERN_ERR
1759 			"%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1760 			ZR_DEVNAME(zr), buf->type, fh->map_mode);
1761 		return -EINVAL;
1762 	}
1763 
1764 	buf->memory = V4L2_MEMORY_MMAP;
1765 	buf->index = num;
1766 	buf->m.offset = buf->length * num;
1767 
1768 	return 0;
1769 }
1770 
1771 static int
zoran_set_norm(struct zoran * zr,int norm)1772 zoran_set_norm (struct zoran *zr,
1773 		int           norm) /* VIDEO_MODE_* */
1774 {
1775 	int norm_encoder, on;
1776 
1777 	if (zr->v4l_buffers.active != ZORAN_FREE ||
1778 	    zr->jpg_buffers.active != ZORAN_FREE) {
1779 		dprintk(1,
1780 			KERN_WARNING
1781 			"%s: set_norm() called while in playback/capture mode\n",
1782 			ZR_DEVNAME(zr));
1783 		return -EBUSY;
1784 	}
1785 
1786 	if (lock_norm && norm != zr->norm) {
1787 		if (lock_norm > 1) {
1788 			dprintk(1,
1789 				KERN_WARNING
1790 				"%s: set_norm() - TV standard is locked, can not switch norm\n",
1791 				ZR_DEVNAME(zr));
1792 			return -EPERM;
1793 		} else {
1794 			dprintk(1,
1795 				KERN_WARNING
1796 				"%s: set_norm() - TV standard is locked, norm was not changed\n",
1797 				ZR_DEVNAME(zr));
1798 			norm = zr->norm;
1799 		}
1800 	}
1801 
1802 	if (norm != VIDEO_MODE_AUTO &&
1803 	    (norm < 0 || norm >= zr->card.norms ||
1804 	     !zr->card.tvn[norm])) {
1805 		dprintk(1,
1806 			KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1807 			ZR_DEVNAME(zr), norm);
1808 		return -EINVAL;
1809 	}
1810 
1811 	if (norm == VIDEO_MODE_AUTO) {
1812 		int status;
1813 
1814 		/* if we have autodetect, ... */
1815 		struct video_decoder_capability caps;
1816 		decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1817 		if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1818 			dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1819 				ZR_DEVNAME(zr));
1820 			return -EINVAL;
1821 		}
1822 
1823 		decoder_command(zr, DECODER_SET_NORM, &norm);
1824 
1825 		/* let changes come into effect */
1826 		ssleep(2);
1827 
1828 		decoder_command(zr, DECODER_GET_STATUS, &status);
1829 		if (!(status & DECODER_STATUS_GOOD)) {
1830 			dprintk(1,
1831 				KERN_ERR
1832 				"%s: set_norm() - no norm detected\n",
1833 				ZR_DEVNAME(zr));
1834 			/* reset norm */
1835 			decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1836 			return -EIO;
1837 		}
1838 
1839 		if (status & DECODER_STATUS_NTSC)
1840 			norm = VIDEO_MODE_NTSC;
1841 		else if (status & DECODER_STATUS_SECAM)
1842 			norm = VIDEO_MODE_SECAM;
1843 		else
1844 			norm = VIDEO_MODE_PAL;
1845 	}
1846 	zr->timing = zr->card.tvn[norm];
1847 	norm_encoder = norm;
1848 
1849 	/* We switch overlay off and on since a change in the
1850 	 * norm needs different VFE settings */
1851 	on = zr->overlay_active && !zr->v4l_memgrab_active;
1852 	if (on)
1853 		zr36057_overlay(zr, 0);
1854 
1855 	decoder_command(zr, DECODER_SET_NORM, &norm);
1856 	encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1857 
1858 	if (on)
1859 		zr36057_overlay(zr, 1);
1860 
1861 	/* Make sure the changes come into effect */
1862 	zr->norm = norm;
1863 
1864 	return 0;
1865 }
1866 
1867 static int
zoran_set_input(struct zoran * zr,int input)1868 zoran_set_input (struct zoran *zr,
1869 		 int           input)
1870 {
1871 	int realinput;
1872 
1873 	if (input == zr->input) {
1874 		return 0;
1875 	}
1876 
1877 	if (zr->v4l_buffers.active != ZORAN_FREE ||
1878 	    zr->jpg_buffers.active != ZORAN_FREE) {
1879 		dprintk(1,
1880 			KERN_WARNING
1881 			"%s: set_input() called while in playback/capture mode\n",
1882 			ZR_DEVNAME(zr));
1883 		return -EBUSY;
1884 	}
1885 
1886 	if (input < 0 || input >= zr->card.inputs) {
1887 		dprintk(1,
1888 			KERN_ERR
1889 			"%s: set_input() - unnsupported input %d\n",
1890 			ZR_DEVNAME(zr), input);
1891 		return -EINVAL;
1892 	}
1893 
1894 	realinput = zr->card.input[input].muxsel;
1895 	zr->input = input;
1896 
1897 	decoder_command(zr, DECODER_SET_INPUT, &realinput);
1898 
1899 	return 0;
1900 }
1901 
1902 /*
1903  *   ioctl routine
1904  */
1905 
zoran_do_ioctl(struct file * file,unsigned int cmd,void * arg)1906 static long zoran_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1907 {
1908 	struct zoran_fh *fh = file->private_data;
1909 	struct zoran *zr = fh->zr;
1910 	/* CAREFUL: used in multiple places here */
1911 	struct zoran_jpg_settings settings;
1912 
1913 	/* we might have older buffers lying around... We don't want
1914 	 * to wait, but we do want to try cleaning them up ASAP. So
1915 	 * we try to obtain the lock and free them. If that fails, we
1916 	 * don't do anything and wait for the next turn. In the end,
1917 	 * zoran_close() or a new allocation will still free them...
1918 	 * This is just a 'the sooner the better' extra 'feature'
1919 	 *
1920 	 * We don't free the buffers right on munmap() because that
1921 	 * causes oopses (kfree() inside munmap() oopses for no
1922 	 * apparent reason - it's also not reproduceable in any way,
1923 	 * but moving the free code outside the munmap() handler fixes
1924 	 * all this... If someone knows why, please explain me (Ronald)
1925 	 */
1926 	if (mutex_trylock(&zr->resource_lock)) {
1927 		/* we obtained it! Let's try to free some things */
1928 		if (fh->jpg_buffers.ready_to_be_freed)
1929 			jpg_fbuffer_free(file);
1930 		if (fh->v4l_buffers.ready_to_be_freed)
1931 			v4l_fbuffer_free(file);
1932 
1933 		mutex_unlock(&zr->resource_lock);
1934 	}
1935 
1936 	switch (cmd) {
1937 
1938 	case VIDIOCGCAP:
1939 	{
1940 		struct video_capability *vcap = arg;
1941 
1942 		dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
1943 
1944 		memset(vcap, 0, sizeof(struct video_capability));
1945 		strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
1946 		vcap->type = ZORAN_VID_TYPE;
1947 
1948 		vcap->channels = zr->card.inputs;
1949 		vcap->audios = 0;
1950 		mutex_lock(&zr->resource_lock);
1951 		vcap->maxwidth = BUZ_MAX_WIDTH;
1952 		vcap->maxheight = BUZ_MAX_HEIGHT;
1953 		vcap->minwidth = BUZ_MIN_WIDTH;
1954 		vcap->minheight = BUZ_MIN_HEIGHT;
1955 		mutex_unlock(&zr->resource_lock);
1956 
1957 		return 0;
1958 	}
1959 		break;
1960 
1961 	case VIDIOCGCHAN:
1962 	{
1963 		struct video_channel *vchan = arg;
1964 		int channel = vchan->channel;
1965 
1966 		dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
1967 			ZR_DEVNAME(zr), vchan->channel);
1968 
1969 		memset(vchan, 0, sizeof(struct video_channel));
1970 		if (channel > zr->card.inputs || channel < 0) {
1971 			dprintk(1,
1972 				KERN_ERR
1973 				"%s: VIDIOCGCHAN on not existing channel %d\n",
1974 				ZR_DEVNAME(zr), channel);
1975 			return -EINVAL;
1976 		}
1977 
1978 		strcpy(vchan->name, zr->card.input[channel].name);
1979 
1980 		vchan->tuners = 0;
1981 		vchan->flags = 0;
1982 		vchan->type = VIDEO_TYPE_CAMERA;
1983 		mutex_lock(&zr->resource_lock);
1984 		vchan->norm = zr->norm;
1985 		mutex_unlock(&zr->resource_lock);
1986 		vchan->channel = channel;
1987 
1988 		return 0;
1989 	}
1990 		break;
1991 
1992 		/* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
1993 		 *
1994 		 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
1995 		 * *                                 ^^^^^^^
1996 		 * * The famos BTTV driver has it implemented with a struct video_channel argument
1997 		 * * and we follow it for compatibility reasons
1998 		 * *
1999 		 * * BTW: this is the only way the user can set the norm!
2000 		 */
2001 
2002 	case VIDIOCSCHAN:
2003 	{
2004 		struct video_channel *vchan = arg;
2005 		int res;
2006 
2007 		dprintk(3,
2008 			KERN_DEBUG
2009 			"%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2010 			ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2011 
2012 		mutex_lock(&zr->resource_lock);
2013 		if ((res = zoran_set_input(zr, vchan->channel)))
2014 			goto schan_unlock_and_return;
2015 		if ((res = zoran_set_norm(zr, vchan->norm)))
2016 			goto schan_unlock_and_return;
2017 
2018 		/* Make sure the changes come into effect */
2019 		res = wait_grab_pending(zr);
2020 	schan_unlock_and_return:
2021 		mutex_unlock(&zr->resource_lock);
2022 		return res;
2023 	}
2024 		break;
2025 
2026 	case VIDIOCGPICT:
2027 	{
2028 		struct video_picture *vpict = arg;
2029 
2030 		dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2031 
2032 		memset(vpict, 0, sizeof(struct video_picture));
2033 		mutex_lock(&zr->resource_lock);
2034 		vpict->hue = zr->hue;
2035 		vpict->brightness = zr->brightness;
2036 		vpict->contrast = zr->contrast;
2037 		vpict->colour = zr->saturation;
2038 		if (fh->overlay_settings.format) {
2039 			vpict->depth = fh->overlay_settings.format->depth;
2040 			vpict->palette = fh->overlay_settings.format->palette;
2041 		} else {
2042 			vpict->depth = 0;
2043 		}
2044 		mutex_unlock(&zr->resource_lock);
2045 
2046 		return 0;
2047 	}
2048 		break;
2049 
2050 	case VIDIOCSPICT:
2051 	{
2052 		struct video_picture *vpict = arg;
2053 		int i;
2054 
2055 		dprintk(3,
2056 			KERN_DEBUG
2057 			"%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2058 			ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2059 			vpict->colour, vpict->contrast, vpict->depth,
2060 			vpict->palette);
2061 
2062 		for (i = 0; i < NUM_FORMATS; i++) {
2063 			const struct zoran_format *fmt = &zoran_formats[i];
2064 
2065 			if (fmt->palette != -1 &&
2066 			    fmt->flags & ZORAN_FORMAT_OVERLAY &&
2067 			    fmt->palette == vpict->palette &&
2068 			    fmt->depth == vpict->depth)
2069 				break;
2070 		}
2071 		if (i == NUM_FORMATS) {
2072 			dprintk(1,
2073 				KERN_ERR
2074 				"%s: VIDIOCSPICT - Invalid palette %d\n",
2075 				ZR_DEVNAME(zr), vpict->palette);
2076 			return -EINVAL;
2077 		}
2078 
2079 		mutex_lock(&zr->resource_lock);
2080 
2081 		decoder_command(zr, DECODER_SET_PICTURE, vpict);
2082 
2083 		zr->hue = vpict->hue;
2084 		zr->contrast = vpict->contrast;
2085 		zr->saturation = vpict->colour;
2086 		zr->brightness = vpict->brightness;
2087 
2088 		fh->overlay_settings.format = &zoran_formats[i];
2089 
2090 		mutex_unlock(&zr->resource_lock);
2091 
2092 		return 0;
2093 	}
2094 		break;
2095 
2096 	case VIDIOCCAPTURE:
2097 	{
2098 		int *on = arg, res;
2099 
2100 		dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2101 			ZR_DEVNAME(zr), *on);
2102 
2103 		mutex_lock(&zr->resource_lock);
2104 		res = setup_overlay(file, *on);
2105 		mutex_unlock(&zr->resource_lock);
2106 
2107 		return res;
2108 	}
2109 		break;
2110 
2111 	case VIDIOCGWIN:
2112 	{
2113 		struct video_window *vwin = arg;
2114 
2115 		dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2116 
2117 		memset(vwin, 0, sizeof(struct video_window));
2118 		mutex_lock(&zr->resource_lock);
2119 		vwin->x = fh->overlay_settings.x;
2120 		vwin->y = fh->overlay_settings.y;
2121 		vwin->width = fh->overlay_settings.width;
2122 		vwin->height = fh->overlay_settings.height;
2123 		mutex_unlock(&zr->resource_lock);
2124 		vwin->clipcount = 0;
2125 		return 0;
2126 	}
2127 		break;
2128 
2129 	case VIDIOCSWIN:
2130 	{
2131 		struct video_window *vwin = arg;
2132 		int res;
2133 
2134 		dprintk(3,
2135 			KERN_DEBUG
2136 			"%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2137 			ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2138 			vwin->height, vwin->clipcount);
2139 
2140 		mutex_lock(&zr->resource_lock);
2141 		res =
2142 		    setup_window(file, vwin->x, vwin->y, vwin->width,
2143 				 vwin->height, vwin->clips,
2144 				 vwin->clipcount, NULL);
2145 		mutex_unlock(&zr->resource_lock);
2146 
2147 		return res;
2148 	}
2149 		break;
2150 
2151 	case VIDIOCGFBUF:
2152 	{
2153 		struct video_buffer *vbuf = arg;
2154 
2155 		dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2156 
2157 		mutex_lock(&zr->resource_lock);
2158 		*vbuf = zr->buffer;
2159 		mutex_unlock(&zr->resource_lock);
2160 		return 0;
2161 	}
2162 		break;
2163 
2164 	case VIDIOCSFBUF:
2165 	{
2166 		struct video_buffer *vbuf = arg;
2167 		int i, res = 0;
2168 
2169 		dprintk(3,
2170 			KERN_DEBUG
2171 			"%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2172 			ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2173 			vbuf->height, vbuf->depth, vbuf->bytesperline);
2174 
2175 		for (i = 0; i < NUM_FORMATS; i++)
2176 			if (zoran_formats[i].depth == vbuf->depth)
2177 				break;
2178 		if (i == NUM_FORMATS) {
2179 			dprintk(1,
2180 				KERN_ERR
2181 				"%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2182 				ZR_DEVNAME(zr), vbuf->depth);
2183 			return -EINVAL;
2184 		}
2185 
2186 		mutex_lock(&zr->resource_lock);
2187 		res =
2188 		    setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2189 				  vbuf->width, vbuf->height,
2190 				  vbuf->bytesperline);
2191 		mutex_unlock(&zr->resource_lock);
2192 
2193 		return res;
2194 	}
2195 		break;
2196 
2197 	case VIDIOCSYNC:
2198 	{
2199 		int *frame = arg, res;
2200 
2201 		dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2202 			ZR_DEVNAME(zr), *frame);
2203 
2204 		mutex_lock(&zr->resource_lock);
2205 		res = v4l_sync(file, *frame);
2206 		mutex_unlock(&zr->resource_lock);
2207 		if (!res)
2208 			zr->v4l_sync_tail++;
2209 		return res;
2210 	}
2211 		break;
2212 
2213 	case VIDIOCMCAPTURE:
2214 	{
2215 		struct video_mmap *vmap = arg;
2216 		int res;
2217 
2218 		dprintk(3,
2219 			KERN_DEBUG
2220 			"%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2221 			ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2222 			vmap->format);
2223 
2224 		mutex_lock(&zr->resource_lock);
2225 		res = v4l_grab(file, vmap);
2226 		mutex_unlock(&zr->resource_lock);
2227 		return res;
2228 	}
2229 		break;
2230 
2231 	case VIDIOCGMBUF:
2232 	{
2233 		struct video_mbuf *vmbuf = arg;
2234 		int i, res = 0;
2235 
2236 		dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2237 
2238 		vmbuf->size =
2239 		    fh->v4l_buffers.num_buffers *
2240 		    fh->v4l_buffers.buffer_size;
2241 		vmbuf->frames = fh->v4l_buffers.num_buffers;
2242 		for (i = 0; i < vmbuf->frames; i++) {
2243 			vmbuf->offsets[i] =
2244 			    i * fh->v4l_buffers.buffer_size;
2245 		}
2246 
2247 		mutex_lock(&zr->resource_lock);
2248 
2249 		if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2250 			dprintk(1,
2251 				KERN_ERR
2252 				"%s: VIDIOCGMBUF - buffers already allocated\n",
2253 				ZR_DEVNAME(zr));
2254 			res = -EINVAL;
2255 			goto v4l1reqbuf_unlock_and_return;
2256 		}
2257 
2258 		if (v4l_fbuffer_alloc(file)) {
2259 			res = -ENOMEM;
2260 			goto v4l1reqbuf_unlock_and_return;
2261 		}
2262 
2263 		/* The next mmap will map the V4L buffers */
2264 		fh->map_mode = ZORAN_MAP_MODE_RAW;
2265 	v4l1reqbuf_unlock_and_return:
2266 		mutex_unlock(&zr->resource_lock);
2267 
2268 		return res;
2269 	}
2270 		break;
2271 
2272 	case VIDIOCGUNIT:
2273 	{
2274 		struct video_unit *vunit = arg;
2275 
2276 		dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2277 
2278 		vunit->video = zr->video_dev->minor;
2279 		vunit->vbi = VIDEO_NO_UNIT;
2280 		vunit->radio = VIDEO_NO_UNIT;
2281 		vunit->audio = VIDEO_NO_UNIT;
2282 		vunit->teletext = VIDEO_NO_UNIT;
2283 
2284 		return 0;
2285 	}
2286 		break;
2287 
2288 		/*
2289 		 * RJ: In principal we could support subcaptures for V4L grabbing.
2290 		 *     Not even the famous BTTV driver has them, however.
2291 		 *     If there should be a strong demand, one could consider
2292 		 *     to implement them.
2293 		 */
2294 	case VIDIOCGCAPTURE:
2295 	{
2296 		dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2297 			ZR_DEVNAME(zr));
2298 		return -EINVAL;
2299 	}
2300 		break;
2301 
2302 	case VIDIOCSCAPTURE:
2303 	{
2304 		dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2305 			ZR_DEVNAME(zr));
2306 		return -EINVAL;
2307 	}
2308 		break;
2309 
2310 	case BUZIOC_G_PARAMS:
2311 	{
2312 		struct zoran_params *bparams = arg;
2313 
2314 		dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2315 
2316 		memset(bparams, 0, sizeof(struct zoran_params));
2317 		bparams->major_version = MAJOR_VERSION;
2318 		bparams->minor_version = MINOR_VERSION;
2319 
2320 		mutex_lock(&zr->resource_lock);
2321 
2322 		bparams->norm = zr->norm;
2323 		bparams->input = zr->input;
2324 
2325 		bparams->decimation = fh->jpg_settings.decimation;
2326 		bparams->HorDcm = fh->jpg_settings.HorDcm;
2327 		bparams->VerDcm = fh->jpg_settings.VerDcm;
2328 		bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2329 		bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2330 		bparams->img_x = fh->jpg_settings.img_x;
2331 		bparams->img_y = fh->jpg_settings.img_y;
2332 		bparams->img_width = fh->jpg_settings.img_width;
2333 		bparams->img_height = fh->jpg_settings.img_height;
2334 		bparams->odd_even = fh->jpg_settings.odd_even;
2335 
2336 		bparams->quality = fh->jpg_settings.jpg_comp.quality;
2337 		bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2338 		bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2339 		memcpy(bparams->APP_data,
2340 		       fh->jpg_settings.jpg_comp.APP_data,
2341 		       sizeof(bparams->APP_data));
2342 		bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2343 		memcpy(bparams->COM_data,
2344 		       fh->jpg_settings.jpg_comp.COM_data,
2345 		       sizeof(bparams->COM_data));
2346 		bparams->jpeg_markers =
2347 		    fh->jpg_settings.jpg_comp.jpeg_markers;
2348 
2349 		mutex_unlock(&zr->resource_lock);
2350 
2351 		bparams->VFIFO_FB = 0;
2352 
2353 		return 0;
2354 	}
2355 		break;
2356 
2357 	case BUZIOC_S_PARAMS:
2358 	{
2359 		struct zoran_params *bparams = arg;
2360 		int res = 0;
2361 
2362 		dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2363 
2364 		settings.decimation = bparams->decimation;
2365 		settings.HorDcm = bparams->HorDcm;
2366 		settings.VerDcm = bparams->VerDcm;
2367 		settings.TmpDcm = bparams->TmpDcm;
2368 		settings.field_per_buff = bparams->field_per_buff;
2369 		settings.img_x = bparams->img_x;
2370 		settings.img_y = bparams->img_y;
2371 		settings.img_width = bparams->img_width;
2372 		settings.img_height = bparams->img_height;
2373 		settings.odd_even = bparams->odd_even;
2374 
2375 		settings.jpg_comp.quality = bparams->quality;
2376 		settings.jpg_comp.APPn = bparams->APPn;
2377 		settings.jpg_comp.APP_len = bparams->APP_len;
2378 		memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2379 		       sizeof(bparams->APP_data));
2380 		settings.jpg_comp.COM_len = bparams->COM_len;
2381 		memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2382 		       sizeof(bparams->COM_data));
2383 		settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2384 
2385 		mutex_lock(&zr->resource_lock);
2386 
2387 		if (zr->codec_mode != BUZ_MODE_IDLE) {
2388 			dprintk(1,
2389 				KERN_ERR
2390 				"%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2391 				ZR_DEVNAME(zr));
2392 			res = -EINVAL;
2393 			goto sparams_unlock_and_return;
2394 		}
2395 
2396 		/* Check the params first before overwriting our
2397 		 * nternal values */
2398 		if (zoran_check_jpg_settings(zr, &settings)) {
2399 			res = -EINVAL;
2400 			goto sparams_unlock_and_return;
2401 		}
2402 
2403 		fh->jpg_settings = settings;
2404 	sparams_unlock_and_return:
2405 		mutex_unlock(&zr->resource_lock);
2406 
2407 		return res;
2408 	}
2409 		break;
2410 
2411 	case BUZIOC_REQBUFS:
2412 	{
2413 		struct zoran_requestbuffers *breq = arg;
2414 		int res = 0;
2415 
2416 		dprintk(3,
2417 			KERN_DEBUG
2418 			"%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2419 			ZR_DEVNAME(zr), breq->count, breq->size);
2420 
2421 		/* Enforce reasonable lower and upper limits */
2422 		if (breq->count < 4)
2423 			breq->count = 4;	/* Could be choosen smaller */
2424 		if (breq->count > jpg_nbufs)
2425 			breq->count = jpg_nbufs;
2426 		breq->size = PAGE_ALIGN(breq->size);
2427 		if (breq->size < 8192)
2428 			breq->size = 8192;	/* Arbitrary */
2429 		/* breq->size is limited by 1 page for the stat_com
2430 		 * tables to a Maximum of 2 MB */
2431 		if (breq->size > jpg_bufsize)
2432 			breq->size = jpg_bufsize;
2433 		if (fh->jpg_buffers.need_contiguous &&
2434 		    breq->size > MAX_KMALLOC_MEM)
2435 			breq->size = MAX_KMALLOC_MEM;
2436 
2437 		mutex_lock(&zr->resource_lock);
2438 
2439 		if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2440 			dprintk(1,
2441 				KERN_ERR
2442 				"%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2443 				ZR_DEVNAME(zr));
2444 			res = -EBUSY;
2445 			goto jpgreqbuf_unlock_and_return;
2446 		}
2447 
2448 		fh->jpg_buffers.num_buffers = breq->count;
2449 		fh->jpg_buffers.buffer_size = breq->size;
2450 
2451 		if (jpg_fbuffer_alloc(file)) {
2452 			res = -ENOMEM;
2453 			goto jpgreqbuf_unlock_and_return;
2454 		}
2455 
2456 		/* The next mmap will map the MJPEG buffers - could
2457 		 * also be *_PLAY, but it doesn't matter here */
2458 		fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2459 	jpgreqbuf_unlock_and_return:
2460 		mutex_unlock(&zr->resource_lock);
2461 
2462 		return res;
2463 	}
2464 		break;
2465 
2466 	case BUZIOC_QBUF_CAPT:
2467 	{
2468 		int *frame = arg, res;
2469 
2470 		dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2471 			ZR_DEVNAME(zr), *frame);
2472 
2473 		mutex_lock(&zr->resource_lock);
2474 		res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2475 		mutex_unlock(&zr->resource_lock);
2476 
2477 		return res;
2478 	}
2479 		break;
2480 
2481 	case BUZIOC_QBUF_PLAY:
2482 	{
2483 		int *frame = arg, res;
2484 
2485 		dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2486 			ZR_DEVNAME(zr), *frame);
2487 
2488 		mutex_lock(&zr->resource_lock);
2489 		res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2490 		mutex_unlock(&zr->resource_lock);
2491 
2492 		return res;
2493 	}
2494 		break;
2495 
2496 	case BUZIOC_SYNC:
2497 	{
2498 		struct zoran_sync *bsync = arg;
2499 		int res;
2500 
2501 		dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2502 
2503 		mutex_lock(&zr->resource_lock);
2504 		res = jpg_sync(file, bsync);
2505 		mutex_unlock(&zr->resource_lock);
2506 
2507 		return res;
2508 	}
2509 		break;
2510 
2511 	case BUZIOC_G_STATUS:
2512 	{
2513 		struct zoran_status *bstat = arg;
2514 		int norm, input, status, res = 0;
2515 
2516 		dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2517 
2518 		if (zr->codec_mode != BUZ_MODE_IDLE) {
2519 			dprintk(1,
2520 				KERN_ERR
2521 				"%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2522 				ZR_DEVNAME(zr));
2523 			return -EINVAL;
2524 		}
2525 
2526 		input = zr->card.input[bstat->input].muxsel;
2527 		norm = VIDEO_MODE_AUTO;
2528 
2529 		mutex_lock(&zr->resource_lock);
2530 
2531 		if (zr->codec_mode != BUZ_MODE_IDLE) {
2532 			dprintk(1,
2533 				KERN_ERR
2534 				"%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2535 				ZR_DEVNAME(zr));
2536 			res = -EINVAL;
2537 			goto gstat_unlock_and_return;
2538 		}
2539 
2540 		decoder_command(zr, DECODER_SET_INPUT, &input);
2541 		decoder_command(zr, DECODER_SET_NORM, &norm);
2542 
2543 		/* sleep 1 second */
2544 		ssleep(1);
2545 
2546 		/* Get status of video decoder */
2547 		decoder_command(zr, DECODER_GET_STATUS, &status);
2548 
2549 		/* restore previous input and norm */
2550 		input = zr->card.input[zr->input].muxsel;
2551 		decoder_command(zr, DECODER_SET_INPUT, &input);
2552 		decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2553 	gstat_unlock_and_return:
2554 		mutex_unlock(&zr->resource_lock);
2555 
2556 		if (!res) {
2557 			bstat->signal =
2558 			    (status & DECODER_STATUS_GOOD) ? 1 : 0;
2559 			if (status & DECODER_STATUS_NTSC)
2560 				bstat->norm = VIDEO_MODE_NTSC;
2561 			else if (status & DECODER_STATUS_SECAM)
2562 				bstat->norm = VIDEO_MODE_SECAM;
2563 			else
2564 				bstat->norm = VIDEO_MODE_PAL;
2565 
2566 			bstat->color =
2567 			    (status & DECODER_STATUS_COLOR) ? 1 : 0;
2568 		}
2569 
2570 		return res;
2571 	}
2572 		break;
2573 
2574 		/* The new video4linux2 capture interface - much nicer than video4linux1, since
2575 		 * it allows for integrating the JPEG capturing calls inside standard v4l2
2576 		 */
2577 
2578 	case VIDIOC_QUERYCAP:
2579 	{
2580 		struct v4l2_capability *cap = arg;
2581 
2582 		dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2583 
2584 		memset(cap, 0, sizeof(*cap));
2585 		strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2586 		strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2587 		snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2588 			 pci_name(zr->pci_dev));
2589 		cap->version =
2590 		    KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2591 				   RELEASE_VERSION);
2592 		cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2593 
2594 		return 0;
2595 	}
2596 		break;
2597 
2598 	case VIDIOC_ENUM_FMT:
2599 	{
2600 		struct v4l2_fmtdesc *fmt = arg;
2601 		int index = fmt->index, num = -1, i, flag = 0, type =
2602 		    fmt->type;
2603 
2604 		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2605 			ZR_DEVNAME(zr), fmt->index);
2606 
2607 		switch (fmt->type) {
2608 		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2609 			flag = ZORAN_FORMAT_CAPTURE;
2610 			break;
2611 		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2612 			flag = ZORAN_FORMAT_PLAYBACK;
2613 			break;
2614 		case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2615 			flag = ZORAN_FORMAT_OVERLAY;
2616 			break;
2617 		default:
2618 			dprintk(1,
2619 				KERN_ERR
2620 				"%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2621 				ZR_DEVNAME(zr), fmt->type);
2622 			return -EINVAL;
2623 		}
2624 
2625 		for (i = 0; i < NUM_FORMATS; i++) {
2626 			if (zoran_formats[i].flags & flag)
2627 				num++;
2628 			if (num == fmt->index)
2629 				break;
2630 		}
2631 		if (fmt->index < 0 /* late, but not too late */  ||
2632 		    i == NUM_FORMATS)
2633 			return -EINVAL;
2634 
2635 		memset(fmt, 0, sizeof(*fmt));
2636 		fmt->index = index;
2637 		fmt->type = type;
2638 		strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2639 		fmt->pixelformat = zoran_formats[i].fourcc;
2640 		if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2641 			fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2642 
2643 		return 0;
2644 	}
2645 		break;
2646 
2647 	case VIDIOC_G_FMT:
2648 	{
2649 		struct v4l2_format *fmt = arg;
2650 		int type = fmt->type;
2651 
2652 		dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2653 
2654 		memset(fmt, 0, sizeof(*fmt));
2655 		fmt->type = type;
2656 
2657 		switch (fmt->type) {
2658 		case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2659 
2660 			mutex_lock(&zr->resource_lock);
2661 
2662 			fmt->fmt.win.w.left = fh->overlay_settings.x;
2663 			fmt->fmt.win.w.top = fh->overlay_settings.y;
2664 			fmt->fmt.win.w.width = fh->overlay_settings.width;
2665 			fmt->fmt.win.w.height =
2666 			    fh->overlay_settings.height;
2667 			if (fh->overlay_settings.width * 2 >
2668 			    BUZ_MAX_HEIGHT)
2669 				fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2670 			else
2671 				fmt->fmt.win.field = V4L2_FIELD_TOP;
2672 
2673 			mutex_unlock(&zr->resource_lock);
2674 
2675 			break;
2676 
2677 		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2678 		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2679 
2680 			mutex_lock(&zr->resource_lock);
2681 
2682 			if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2683 			    fh->map_mode == ZORAN_MAP_MODE_RAW) {
2684 
2685 				fmt->fmt.pix.width =
2686 				    fh->v4l_settings.width;
2687 				fmt->fmt.pix.height =
2688 				    fh->v4l_settings.height;
2689 				fmt->fmt.pix.sizeimage =
2690 				    fh->v4l_settings.bytesperline *
2691 				    fh->v4l_settings.height;
2692 				fmt->fmt.pix.pixelformat =
2693 				    fh->v4l_settings.format->fourcc;
2694 				fmt->fmt.pix.colorspace =
2695 				    fh->v4l_settings.format->colorspace;
2696 				fmt->fmt.pix.bytesperline =
2697 				    fh->v4l_settings.bytesperline;
2698 				if (BUZ_MAX_HEIGHT <
2699 				    (fh->v4l_settings.height * 2))
2700 					fmt->fmt.pix.field =
2701 					    V4L2_FIELD_INTERLACED;
2702 				else
2703 					fmt->fmt.pix.field =
2704 					    V4L2_FIELD_TOP;
2705 
2706 			} else {
2707 
2708 				fmt->fmt.pix.width =
2709 				    fh->jpg_settings.img_width /
2710 				    fh->jpg_settings.HorDcm;
2711 				fmt->fmt.pix.height =
2712 				    fh->jpg_settings.img_height /
2713 				    (fh->jpg_settings.VerDcm *
2714 				     fh->jpg_settings.TmpDcm);
2715 				fmt->fmt.pix.sizeimage =
2716 				    zoran_v4l2_calc_bufsize(&fh->
2717 							    jpg_settings);
2718 				fmt->fmt.pix.pixelformat =
2719 				    V4L2_PIX_FMT_MJPEG;
2720 				if (fh->jpg_settings.TmpDcm == 1)
2721 					fmt->fmt.pix.field =
2722 					    (fh->jpg_settings.
2723 					     odd_even ? V4L2_FIELD_SEQ_BT :
2724 					     V4L2_FIELD_SEQ_BT);
2725 				else
2726 					fmt->fmt.pix.field =
2727 					    (fh->jpg_settings.
2728 					     odd_even ? V4L2_FIELD_TOP :
2729 					     V4L2_FIELD_BOTTOM);
2730 
2731 				fmt->fmt.pix.bytesperline = 0;
2732 				fmt->fmt.pix.colorspace =
2733 				    V4L2_COLORSPACE_SMPTE170M;
2734 			}
2735 
2736 			mutex_unlock(&zr->resource_lock);
2737 
2738 			break;
2739 
2740 		default:
2741 			dprintk(1,
2742 				KERN_ERR
2743 				"%s: VIDIOC_G_FMT - unsupported type %d\n",
2744 				ZR_DEVNAME(zr), fmt->type);
2745 			return -EINVAL;
2746 		}
2747 		return 0;
2748 	}
2749 		break;
2750 
2751 	case VIDIOC_S_FMT:
2752 	{
2753 		struct v4l2_format *fmt = arg;
2754 		int i, res = 0;
2755 		__le32 printformat;
2756 
2757 		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2758 			ZR_DEVNAME(zr), fmt->type);
2759 
2760 		switch (fmt->type) {
2761 		case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2762 
2763 			dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2764 				fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2765 				fmt->fmt.win.w.width,
2766 				fmt->fmt.win.w.height,
2767 				fmt->fmt.win.clipcount,
2768 				fmt->fmt.win.bitmap);
2769 			mutex_lock(&zr->resource_lock);
2770 			res =
2771 			    setup_window(file, fmt->fmt.win.w.left,
2772 					 fmt->fmt.win.w.top,
2773 					 fmt->fmt.win.w.width,
2774 					 fmt->fmt.win.w.height,
2775 					 (struct video_clip __user *)
2776 					   fmt->fmt.win.clips,
2777 					 fmt->fmt.win.clipcount,
2778 					 fmt->fmt.win.bitmap);
2779 			mutex_unlock(&zr->resource_lock);
2780 			return res;
2781 			break;
2782 
2783 		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2784 		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2785 
2786 			printformat =
2787 			    __cpu_to_le32(fmt->fmt.pix.pixelformat);
2788 			dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2789 				fmt->fmt.pix.width, fmt->fmt.pix.height,
2790 				fmt->fmt.pix.pixelformat,
2791 				(char *) &printformat);
2792 
2793 			/* we can be requested to do JPEG/raw playback/capture */
2794 			if (!
2795 			    (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2796 			     (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2797 			      fmt->fmt.pix.pixelformat ==
2798 			      V4L2_PIX_FMT_MJPEG))) {
2799 				dprintk(1,
2800 					KERN_ERR
2801 					"%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2802 					ZR_DEVNAME(zr), fmt->type,
2803 					fmt->fmt.pix.pixelformat,
2804 					(char *) &printformat);
2805 				return -EINVAL;
2806 			}
2807 
2808 			if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2809 				mutex_lock(&zr->resource_lock);
2810 
2811 				settings = fh->jpg_settings;
2812 
2813 				if (fh->v4l_buffers.allocated ||
2814 				    fh->jpg_buffers.allocated) {
2815 					dprintk(1,
2816 						KERN_ERR
2817 						"%s: VIDIOC_S_FMT - cannot change capture mode\n",
2818 						ZR_DEVNAME(zr));
2819 					res = -EBUSY;
2820 					goto sfmtjpg_unlock_and_return;
2821 				}
2822 
2823 				/* we actually need to set 'real' parameters now */
2824 				if ((fmt->fmt.pix.height * 2) >
2825 				    BUZ_MAX_HEIGHT)
2826 					settings.TmpDcm = 1;
2827 				else
2828 					settings.TmpDcm = 2;
2829 				settings.decimation = 0;
2830 				if (fmt->fmt.pix.height <=
2831 				    fh->jpg_settings.img_height / 2)
2832 					settings.VerDcm = 2;
2833 				else
2834 					settings.VerDcm = 1;
2835 				if (fmt->fmt.pix.width <=
2836 				    fh->jpg_settings.img_width / 4)
2837 					settings.HorDcm = 4;
2838 				else if (fmt->fmt.pix.width <=
2839 					 fh->jpg_settings.img_width / 2)
2840 					settings.HorDcm = 2;
2841 				else
2842 					settings.HorDcm = 1;
2843 				if (settings.TmpDcm == 1)
2844 					settings.field_per_buff = 2;
2845 				else
2846 					settings.field_per_buff = 1;
2847 
2848 				/* check */
2849 				if ((res =
2850 				     zoran_check_jpg_settings(zr,
2851 							      &settings)))
2852 					goto sfmtjpg_unlock_and_return;
2853 
2854 				/* it's ok, so set them */
2855 				fh->jpg_settings = settings;
2856 
2857 				/* tell the user what we actually did */
2858 				fmt->fmt.pix.width =
2859 				    settings.img_width / settings.HorDcm;
2860 				fmt->fmt.pix.height =
2861 				    settings.img_height * 2 /
2862 				    (settings.TmpDcm * settings.VerDcm);
2863 				if (settings.TmpDcm == 1)
2864 					fmt->fmt.pix.field =
2865 					    (fh->jpg_settings.
2866 					     odd_even ? V4L2_FIELD_SEQ_TB :
2867 					     V4L2_FIELD_SEQ_BT);
2868 				else
2869 					fmt->fmt.pix.field =
2870 					    (fh->jpg_settings.
2871 					     odd_even ? V4L2_FIELD_TOP :
2872 					     V4L2_FIELD_BOTTOM);
2873 				fh->jpg_buffers.buffer_size =
2874 				    zoran_v4l2_calc_bufsize(&fh->
2875 							    jpg_settings);
2876 				fmt->fmt.pix.bytesperline = 0;
2877 				fmt->fmt.pix.sizeimage =
2878 				    fh->jpg_buffers.buffer_size;
2879 				fmt->fmt.pix.colorspace =
2880 				    V4L2_COLORSPACE_SMPTE170M;
2881 
2882 				/* we hereby abuse this variable to show that
2883 				 * we're gonna do mjpeg capture */
2884 				fh->map_mode =
2885 				    (fmt->type ==
2886 				     V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2887 				    ZORAN_MAP_MODE_JPG_REC :
2888 				    ZORAN_MAP_MODE_JPG_PLAY;
2889 			sfmtjpg_unlock_and_return:
2890 				mutex_unlock(&zr->resource_lock);
2891 			} else {
2892 				for (i = 0; i < NUM_FORMATS; i++)
2893 					if (fmt->fmt.pix.pixelformat ==
2894 					    zoran_formats[i].fourcc)
2895 						break;
2896 				if (i == NUM_FORMATS) {
2897 					dprintk(1,
2898 						KERN_ERR
2899 						"%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2900 						ZR_DEVNAME(zr),
2901 						fmt->fmt.pix.pixelformat,
2902 						(char *) &printformat);
2903 					return -EINVAL;
2904 				}
2905 				mutex_lock(&zr->resource_lock);
2906 				if (fh->jpg_buffers.allocated ||
2907 				    (fh->v4l_buffers.allocated &&
2908 				     fh->v4l_buffers.active !=
2909 				     ZORAN_FREE)) {
2910 					dprintk(1,
2911 						KERN_ERR
2912 						"%s: VIDIOC_S_FMT - cannot change capture mode\n",
2913 						ZR_DEVNAME(zr));
2914 					res = -EBUSY;
2915 					goto sfmtv4l_unlock_and_return;
2916 				}
2917 				if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2918 					fmt->fmt.pix.height =
2919 					    BUZ_MAX_HEIGHT;
2920 				if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2921 					fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2922 
2923 				if ((res =
2924 				     zoran_v4l_set_format(file,
2925 							  fmt->fmt.pix.
2926 							  width,
2927 							  fmt->fmt.pix.
2928 							  height,
2929 							  &zoran_formats
2930 							  [i])))
2931 					goto sfmtv4l_unlock_and_return;
2932 
2933 				/* tell the user the
2934 				 * results/missing stuff */
2935 				fmt->fmt.pix.bytesperline =
2936 					fh->v4l_settings.bytesperline;
2937 				fmt->fmt.pix.sizeimage =
2938 					fh->v4l_settings.height *
2939 					fh->v4l_settings.bytesperline;
2940 				fmt->fmt.pix.colorspace =
2941 					fh->v4l_settings.format->colorspace;
2942 				if (BUZ_MAX_HEIGHT <
2943 				    (fh->v4l_settings.height * 2))
2944 					fmt->fmt.pix.field =
2945 					    V4L2_FIELD_INTERLACED;
2946 				else
2947 					fmt->fmt.pix.field =
2948 					    V4L2_FIELD_TOP;
2949 
2950 				fh->map_mode = ZORAN_MAP_MODE_RAW;
2951 			sfmtv4l_unlock_and_return:
2952 				mutex_unlock(&zr->resource_lock);
2953 			}
2954 
2955 			break;
2956 
2957 		default:
2958 			dprintk(1,
2959 				KERN_ERR
2960 				"%s: VIDIOC_S_FMT - unsupported type %d\n",
2961 				ZR_DEVNAME(zr), fmt->type);
2962 			return -EINVAL;
2963 		}
2964 
2965 		return res;
2966 	}
2967 		break;
2968 
2969 	case VIDIOC_G_FBUF:
2970 	{
2971 		struct v4l2_framebuffer *fb = arg;
2972 
2973 		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
2974 
2975 		memset(fb, 0, sizeof(*fb));
2976 		mutex_lock(&zr->resource_lock);
2977 		fb->base = zr->buffer.base;
2978 		fb->fmt.width = zr->buffer.width;
2979 		fb->fmt.height = zr->buffer.height;
2980 		if (zr->overlay_settings.format) {
2981 			fb->fmt.pixelformat =
2982 				fh->overlay_settings.format->fourcc;
2983 		}
2984 		fb->fmt.bytesperline = zr->buffer.bytesperline;
2985 		mutex_unlock(&zr->resource_lock);
2986 		fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2987 		fb->fmt.field = V4L2_FIELD_INTERLACED;
2988 		fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2989 		fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2990 
2991 		return 0;
2992 	}
2993 		break;
2994 
2995 	case VIDIOC_S_FBUF:
2996 	{
2997 		int i, res = 0;
2998 		struct v4l2_framebuffer *fb = arg;
2999 		__le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3000 
3001 		dprintk(3,
3002 			KERN_DEBUG
3003 			"%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3004 			ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3005 			fb->fmt.bytesperline, fb->fmt.pixelformat,
3006 			(char *) &printformat);
3007 
3008 		for (i = 0; i < NUM_FORMATS; i++)
3009 			if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3010 				break;
3011 		if (i == NUM_FORMATS) {
3012 			dprintk(1,
3013 				KERN_ERR
3014 				"%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3015 				ZR_DEVNAME(zr), fb->fmt.pixelformat,
3016 				(char *) &printformat);
3017 			return -EINVAL;
3018 		}
3019 
3020 		mutex_lock(&zr->resource_lock);
3021 		res =
3022 		    setup_fbuffer(file, fb->base, &zoran_formats[i],
3023 				  fb->fmt.width, fb->fmt.height,
3024 				  fb->fmt.bytesperline);
3025 		mutex_unlock(&zr->resource_lock);
3026 
3027 		return res;
3028 	}
3029 		break;
3030 
3031 	case VIDIOC_OVERLAY:
3032 	{
3033 		int *on = arg, res;
3034 
3035 		dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3036 			ZR_DEVNAME(zr), *on);
3037 
3038 		mutex_lock(&zr->resource_lock);
3039 		res = setup_overlay(file, *on);
3040 		mutex_unlock(&zr->resource_lock);
3041 
3042 		return res;
3043 	}
3044 		break;
3045 
3046 	case VIDIOC_REQBUFS:
3047 	{
3048 		struct v4l2_requestbuffers *req = arg;
3049 		int res = 0;
3050 
3051 		dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3052 			ZR_DEVNAME(zr), req->type);
3053 
3054 		if (req->memory != V4L2_MEMORY_MMAP) {
3055 			dprintk(1,
3056 				KERN_ERR
3057 				"%s: only MEMORY_MMAP capture is supported, not %d\n",
3058 				ZR_DEVNAME(zr), req->memory);
3059 			return -EINVAL;
3060 		}
3061 
3062 		mutex_lock(&zr->resource_lock);
3063 
3064 		if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3065 			dprintk(1,
3066 				KERN_ERR
3067 				"%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3068 				ZR_DEVNAME(zr));
3069 			res = -EBUSY;
3070 			goto v4l2reqbuf_unlock_and_return;
3071 		}
3072 
3073 		if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3074 		    req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3075 
3076 			/* control user input */
3077 			if (req->count < 2)
3078 				req->count = 2;
3079 			if (req->count > v4l_nbufs)
3080 				req->count = v4l_nbufs;
3081 			fh->v4l_buffers.num_buffers = req->count;
3082 
3083 			if (v4l_fbuffer_alloc(file)) {
3084 				res = -ENOMEM;
3085 				goto v4l2reqbuf_unlock_and_return;
3086 			}
3087 
3088 			/* The next mmap will map the V4L buffers */
3089 			fh->map_mode = ZORAN_MAP_MODE_RAW;
3090 
3091 		} else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3092 			   fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3093 
3094 			/* we need to calculate size ourselves now */
3095 			if (req->count < 4)
3096 				req->count = 4;
3097 			if (req->count > jpg_nbufs)
3098 				req->count = jpg_nbufs;
3099 			fh->jpg_buffers.num_buffers = req->count;
3100 			fh->jpg_buffers.buffer_size =
3101 			    zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3102 
3103 			if (jpg_fbuffer_alloc(file)) {
3104 				res = -ENOMEM;
3105 				goto v4l2reqbuf_unlock_and_return;
3106 			}
3107 
3108 			/* The next mmap will map the MJPEG buffers */
3109 			if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3110 				fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3111 			else
3112 				fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3113 
3114 		} else {
3115 			dprintk(1,
3116 				KERN_ERR
3117 				"%s: VIDIOC_REQBUFS - unknown type %d\n",
3118 				ZR_DEVNAME(zr), req->type);
3119 			res = -EINVAL;
3120 			goto v4l2reqbuf_unlock_and_return;
3121 		}
3122 	v4l2reqbuf_unlock_and_return:
3123 		mutex_unlock(&zr->resource_lock);
3124 
3125 		return 0;
3126 	}
3127 		break;
3128 
3129 	case VIDIOC_QUERYBUF:
3130 	{
3131 		struct v4l2_buffer *buf = arg;
3132 		__u32 type = buf->type;
3133 		int index = buf->index, res;
3134 
3135 		dprintk(3,
3136 			KERN_DEBUG
3137 			"%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3138 			ZR_DEVNAME(zr), buf->index, buf->type);
3139 
3140 		memset(buf, 0, sizeof(*buf));
3141 		buf->type = type;
3142 		buf->index = index;
3143 
3144 		mutex_lock(&zr->resource_lock);
3145 		res = zoran_v4l2_buffer_status(file, buf, buf->index);
3146 		mutex_unlock(&zr->resource_lock);
3147 
3148 		return res;
3149 	}
3150 		break;
3151 
3152 	case VIDIOC_QBUF:
3153 	{
3154 		struct v4l2_buffer *buf = arg;
3155 		int res = 0, codec_mode, buf_type;
3156 
3157 		dprintk(3,
3158 			KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3159 			ZR_DEVNAME(zr), buf->type, buf->index);
3160 
3161 		mutex_lock(&zr->resource_lock);
3162 
3163 		switch (fh->map_mode) {
3164 		case ZORAN_MAP_MODE_RAW:
3165 			if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3166 				dprintk(1,
3167 					KERN_ERR
3168 					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3169 					ZR_DEVNAME(zr), buf->type, fh->map_mode);
3170 				res = -EINVAL;
3171 				goto qbuf_unlock_and_return;
3172 			}
3173 
3174 			res = zoran_v4l_queue_frame(file, buf->index);
3175 			if (res)
3176 				goto qbuf_unlock_and_return;
3177 			if (!zr->v4l_memgrab_active &&
3178 			    fh->v4l_buffers.active == ZORAN_LOCKED)
3179 				zr36057_set_memgrab(zr, 1);
3180 			break;
3181 
3182 		case ZORAN_MAP_MODE_JPG_REC:
3183 		case ZORAN_MAP_MODE_JPG_PLAY:
3184 			if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3185 				buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3186 				codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3187 			} else {
3188 				buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3189 				codec_mode = BUZ_MODE_MOTION_COMPRESS;
3190 			}
3191 
3192 			if (buf->type != buf_type) {
3193 				dprintk(1,
3194 					KERN_ERR
3195 					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3196 					ZR_DEVNAME(zr), buf->type, fh->map_mode);
3197 				res = -EINVAL;
3198 				goto qbuf_unlock_and_return;
3199 			}
3200 
3201 			res =
3202 			    zoran_jpg_queue_frame(file, buf->index,
3203 						  codec_mode);
3204 			if (res != 0)
3205 				goto qbuf_unlock_and_return;
3206 			if (zr->codec_mode == BUZ_MODE_IDLE &&
3207 			    fh->jpg_buffers.active == ZORAN_LOCKED) {
3208 				zr36057_enable_jpg(zr, codec_mode);
3209 			}
3210 			break;
3211 
3212 		default:
3213 			dprintk(1,
3214 				KERN_ERR
3215 				"%s: VIDIOC_QBUF - unsupported type %d\n",
3216 				ZR_DEVNAME(zr), buf->type);
3217 			res = -EINVAL;
3218 			goto qbuf_unlock_and_return;
3219 		}
3220 	qbuf_unlock_and_return:
3221 		mutex_unlock(&zr->resource_lock);
3222 
3223 		return res;
3224 	}
3225 		break;
3226 
3227 	case VIDIOC_DQBUF:
3228 	{
3229 		struct v4l2_buffer *buf = arg;
3230 		int res = 0, buf_type, num = -1;	/* compiler borks here (?) */
3231 
3232 		dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3233 			ZR_DEVNAME(zr), buf->type);
3234 
3235 		mutex_lock(&zr->resource_lock);
3236 
3237 		switch (fh->map_mode) {
3238 		case ZORAN_MAP_MODE_RAW:
3239 			if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3240 				dprintk(1,
3241 					KERN_ERR
3242 					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3243 					ZR_DEVNAME(zr), buf->type, fh->map_mode);
3244 				res = -EINVAL;
3245 				goto dqbuf_unlock_and_return;
3246 			}
3247 
3248 			num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3249 			if (file->f_flags & O_NONBLOCK &&
3250 			    zr->v4l_buffers.buffer[num].state !=
3251 			    BUZ_STATE_DONE) {
3252 				res = -EAGAIN;
3253 				goto dqbuf_unlock_and_return;
3254 			}
3255 			res = v4l_sync(file, num);
3256 			if (res)
3257 				goto dqbuf_unlock_and_return;
3258 			else
3259 				zr->v4l_sync_tail++;
3260 			res = zoran_v4l2_buffer_status(file, buf, num);
3261 			break;
3262 
3263 		case ZORAN_MAP_MODE_JPG_REC:
3264 		case ZORAN_MAP_MODE_JPG_PLAY:
3265 		{
3266 			struct zoran_sync bs;
3267 
3268 			if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3269 				buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3270 			else
3271 				buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3272 
3273 			if (buf->type != buf_type) {
3274 				dprintk(1,
3275 					KERN_ERR
3276 					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3277 					ZR_DEVNAME(zr), buf->type, fh->map_mode);
3278 				res = -EINVAL;
3279 				goto dqbuf_unlock_and_return;
3280 			}
3281 
3282 			num =
3283 			    zr->jpg_pend[zr->
3284 					 jpg_que_tail & BUZ_MASK_FRAME];
3285 
3286 			if (file->f_flags & O_NONBLOCK &&
3287 			    zr->jpg_buffers.buffer[num].state !=
3288 			    BUZ_STATE_DONE) {
3289 				res = -EAGAIN;
3290 				goto dqbuf_unlock_and_return;
3291 			}
3292 			res = jpg_sync(file, &bs);
3293 			if (res)
3294 				goto dqbuf_unlock_and_return;
3295 			res =
3296 			    zoran_v4l2_buffer_status(file, buf, bs.frame);
3297 			break;
3298 		}
3299 
3300 		default:
3301 			dprintk(1,
3302 				KERN_ERR
3303 				"%s: VIDIOC_DQBUF - unsupported type %d\n",
3304 				ZR_DEVNAME(zr), buf->type);
3305 			res = -EINVAL;
3306 			goto dqbuf_unlock_and_return;
3307 		}
3308 	dqbuf_unlock_and_return:
3309 		mutex_unlock(&zr->resource_lock);
3310 
3311 		return res;
3312 	}
3313 		break;
3314 
3315 	case VIDIOC_STREAMON:
3316 	{
3317 		int res = 0;
3318 
3319 		dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3320 
3321 		mutex_lock(&zr->resource_lock);
3322 
3323 		switch (fh->map_mode) {
3324 		case ZORAN_MAP_MODE_RAW:	/* raw capture */
3325 			if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3326 			    fh->v4l_buffers.active != ZORAN_ACTIVE) {
3327 				res = -EBUSY;
3328 				goto strmon_unlock_and_return;
3329 			}
3330 
3331 			zr->v4l_buffers.active = fh->v4l_buffers.active =
3332 			    ZORAN_LOCKED;
3333 			zr->v4l_settings = fh->v4l_settings;
3334 
3335 			zr->v4l_sync_tail = zr->v4l_pend_tail;
3336 			if (!zr->v4l_memgrab_active &&
3337 			    zr->v4l_pend_head != zr->v4l_pend_tail) {
3338 				zr36057_set_memgrab(zr, 1);
3339 			}
3340 			break;
3341 
3342 		case ZORAN_MAP_MODE_JPG_REC:
3343 		case ZORAN_MAP_MODE_JPG_PLAY:
3344 			/* what is the codec mode right now? */
3345 			if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3346 			    fh->jpg_buffers.active != ZORAN_ACTIVE) {
3347 				res = -EBUSY;
3348 				goto strmon_unlock_and_return;
3349 			}
3350 
3351 			zr->jpg_buffers.active = fh->jpg_buffers.active =
3352 			    ZORAN_LOCKED;
3353 
3354 			if (zr->jpg_que_head != zr->jpg_que_tail) {
3355 				/* Start the jpeg codec when the first frame is queued  */
3356 				jpeg_start(zr);
3357 			}
3358 
3359 			break;
3360 		default:
3361 			dprintk(1,
3362 				KERN_ERR
3363 				"%s: VIDIOC_STREAMON - invalid map mode %d\n",
3364 				ZR_DEVNAME(zr), fh->map_mode);
3365 			res = -EINVAL;
3366 			goto strmon_unlock_and_return;
3367 		}
3368 	strmon_unlock_and_return:
3369 		mutex_unlock(&zr->resource_lock);
3370 
3371 		return res;
3372 	}
3373 		break;
3374 
3375 	case VIDIOC_STREAMOFF:
3376 	{
3377 		int i, res = 0;
3378 
3379 		dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3380 
3381 		mutex_lock(&zr->resource_lock);
3382 
3383 		switch (fh->map_mode) {
3384 		case ZORAN_MAP_MODE_RAW:	/* raw capture */
3385 			if (fh->v4l_buffers.active == ZORAN_FREE &&
3386 			    zr->v4l_buffers.active != ZORAN_FREE) {
3387 				res = -EPERM;	/* stay off other's settings! */
3388 				goto strmoff_unlock_and_return;
3389 			}
3390 			if (zr->v4l_buffers.active == ZORAN_FREE)
3391 				goto strmoff_unlock_and_return;
3392 
3393 			/* unload capture */
3394 			if (zr->v4l_memgrab_active) {
3395 				unsigned long flags;
3396 
3397 				spin_lock_irqsave(&zr->spinlock, flags);
3398 				zr36057_set_memgrab(zr, 0);
3399 				spin_unlock_irqrestore(&zr->spinlock, flags);
3400 			}
3401 
3402 			for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3403 				zr->v4l_buffers.buffer[i].state =
3404 				    BUZ_STATE_USER;
3405 			fh->v4l_buffers = zr->v4l_buffers;
3406 
3407 			zr->v4l_buffers.active = fh->v4l_buffers.active =
3408 			    ZORAN_FREE;
3409 
3410 			zr->v4l_grab_seq = 0;
3411 			zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3412 			zr->v4l_sync_tail = 0;
3413 
3414 			break;
3415 
3416 		case ZORAN_MAP_MODE_JPG_REC:
3417 		case ZORAN_MAP_MODE_JPG_PLAY:
3418 			if (fh->jpg_buffers.active == ZORAN_FREE &&
3419 			    zr->jpg_buffers.active != ZORAN_FREE) {
3420 				res = -EPERM;	/* stay off other's settings! */
3421 				goto strmoff_unlock_and_return;
3422 			}
3423 			if (zr->jpg_buffers.active == ZORAN_FREE)
3424 				goto strmoff_unlock_and_return;
3425 
3426 			res =
3427 			    jpg_qbuf(file, -1,
3428 				     (fh->map_mode ==
3429 				      ZORAN_MAP_MODE_JPG_REC) ?
3430 				     BUZ_MODE_MOTION_COMPRESS :
3431 				     BUZ_MODE_MOTION_DECOMPRESS);
3432 			if (res)
3433 				goto strmoff_unlock_and_return;
3434 			break;
3435 		default:
3436 			dprintk(1,
3437 				KERN_ERR
3438 				"%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3439 				ZR_DEVNAME(zr), fh->map_mode);
3440 			res = -EINVAL;
3441 			goto strmoff_unlock_and_return;
3442 		}
3443 	strmoff_unlock_and_return:
3444 		mutex_unlock(&zr->resource_lock);
3445 
3446 		return res;
3447 	}
3448 		break;
3449 
3450 	case VIDIOC_QUERYCTRL:
3451 	{
3452 		struct v4l2_queryctrl *ctrl = arg;
3453 
3454 		dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3455 			ZR_DEVNAME(zr), ctrl->id);
3456 
3457 		/* we only support hue/saturation/contrast/brightness */
3458 		if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3459 		    ctrl->id > V4L2_CID_HUE)
3460 			return -EINVAL;
3461 		else {
3462 			int id = ctrl->id;
3463 			memset(ctrl, 0, sizeof(*ctrl));
3464 			ctrl->id = id;
3465 		}
3466 
3467 		switch (ctrl->id) {
3468 		case V4L2_CID_BRIGHTNESS:
3469 			strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3470 			break;
3471 		case V4L2_CID_CONTRAST:
3472 			strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3473 			break;
3474 		case V4L2_CID_SATURATION:
3475 			strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3476 			break;
3477 		case V4L2_CID_HUE:
3478 			strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3479 			break;
3480 		}
3481 
3482 		ctrl->minimum = 0;
3483 		ctrl->maximum = 65535;
3484 		ctrl->step = 1;
3485 		ctrl->default_value = 32768;
3486 		ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3487 
3488 		return 0;
3489 	}
3490 		break;
3491 
3492 	case VIDIOC_G_CTRL:
3493 	{
3494 		struct v4l2_control *ctrl = arg;
3495 
3496 		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3497 			ZR_DEVNAME(zr), ctrl->id);
3498 
3499 		/* we only support hue/saturation/contrast/brightness */
3500 		if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3501 		    ctrl->id > V4L2_CID_HUE)
3502 			return -EINVAL;
3503 
3504 		mutex_lock(&zr->resource_lock);
3505 		switch (ctrl->id) {
3506 		case V4L2_CID_BRIGHTNESS:
3507 			ctrl->value = zr->brightness;
3508 			break;
3509 		case V4L2_CID_CONTRAST:
3510 			ctrl->value = zr->contrast;
3511 			break;
3512 		case V4L2_CID_SATURATION:
3513 			ctrl->value = zr->saturation;
3514 			break;
3515 		case V4L2_CID_HUE:
3516 			ctrl->value = zr->hue;
3517 			break;
3518 		}
3519 		mutex_unlock(&zr->resource_lock);
3520 
3521 		return 0;
3522 	}
3523 		break;
3524 
3525 	case VIDIOC_S_CTRL:
3526 	{
3527 		struct v4l2_control *ctrl = arg;
3528 		struct video_picture pict;
3529 
3530 		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3531 			ZR_DEVNAME(zr), ctrl->id);
3532 
3533 		/* we only support hue/saturation/contrast/brightness */
3534 		if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3535 		    ctrl->id > V4L2_CID_HUE)
3536 			return -EINVAL;
3537 
3538 		if (ctrl->value < 0 || ctrl->value > 65535) {
3539 			dprintk(1,
3540 				KERN_ERR
3541 				"%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3542 				ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3543 			return -EINVAL;
3544 		}
3545 
3546 		mutex_lock(&zr->resource_lock);
3547 		switch (ctrl->id) {
3548 		case V4L2_CID_BRIGHTNESS:
3549 			zr->brightness = ctrl->value;
3550 			break;
3551 		case V4L2_CID_CONTRAST:
3552 			zr->contrast = ctrl->value;
3553 			break;
3554 		case V4L2_CID_SATURATION:
3555 			zr->saturation = ctrl->value;
3556 			break;
3557 		case V4L2_CID_HUE:
3558 			zr->hue = ctrl->value;
3559 			break;
3560 		}
3561 		pict.brightness = zr->brightness;
3562 		pict.contrast = zr->contrast;
3563 		pict.colour = zr->saturation;
3564 		pict.hue = zr->hue;
3565 
3566 		decoder_command(zr, DECODER_SET_PICTURE, &pict);
3567 
3568 		mutex_unlock(&zr->resource_lock);
3569 
3570 		return 0;
3571 	}
3572 		break;
3573 
3574 	case VIDIOC_ENUMSTD:
3575 	{
3576 		struct v4l2_standard *std = arg;
3577 
3578 		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3579 			ZR_DEVNAME(zr), std->index);
3580 
3581 		if (std->index < 0 || std->index >= (zr->card.norms + 1))
3582 			return -EINVAL;
3583 		else {
3584 			int id = std->index;
3585 			memset(std, 0, sizeof(*std));
3586 			std->index = id;
3587 		}
3588 
3589 		if (std->index == zr->card.norms) {
3590 			/* if we have autodetect, ... */
3591 			struct video_decoder_capability caps;
3592 			decoder_command(zr, DECODER_GET_CAPABILITIES,
3593 					&caps);
3594 			if (caps.flags & VIDEO_DECODER_AUTO) {
3595 				std->id = V4L2_STD_ALL;
3596 				strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3597 				return 0;
3598 			} else
3599 				return -EINVAL;
3600 		}
3601 		switch (std->index) {
3602 		case 0:
3603 			std->id = V4L2_STD_PAL;
3604 			strncpy(std->name, "PAL", sizeof(std->name)-1);
3605 			std->frameperiod.numerator = 1;
3606 			std->frameperiod.denominator = 25;
3607 			std->framelines = zr->card.tvn[0]->Ht;
3608 			break;
3609 		case 1:
3610 			std->id = V4L2_STD_NTSC;
3611 			strncpy(std->name, "NTSC", sizeof(std->name)-1);
3612 			std->frameperiod.numerator = 1001;
3613 			std->frameperiod.denominator = 30000;
3614 			std->framelines = zr->card.tvn[1]->Ht;
3615 			break;
3616 		case 2:
3617 			std->id = V4L2_STD_SECAM;
3618 			strncpy(std->name, "SECAM", sizeof(std->name)-1);
3619 			std->frameperiod.numerator = 1;
3620 			std->frameperiod.denominator = 25;
3621 			std->framelines = zr->card.tvn[2]->Ht;
3622 			break;
3623 		}
3624 
3625 		return 0;
3626 	}
3627 		break;
3628 
3629 	case VIDIOC_G_STD:
3630 	{
3631 		v4l2_std_id *std = arg;
3632 		int norm;
3633 
3634 		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3635 
3636 		mutex_lock(&zr->resource_lock);
3637 		norm = zr->norm;
3638 		mutex_unlock(&zr->resource_lock);
3639 
3640 		switch (norm) {
3641 		case VIDEO_MODE_PAL:
3642 			*std = V4L2_STD_PAL;
3643 			break;
3644 		case VIDEO_MODE_NTSC:
3645 			*std = V4L2_STD_NTSC;
3646 			break;
3647 		case VIDEO_MODE_SECAM:
3648 			*std = V4L2_STD_SECAM;
3649 			break;
3650 		}
3651 
3652 		return 0;
3653 	}
3654 		break;
3655 
3656 	case VIDIOC_S_STD:
3657 	{
3658 		int norm = -1, res = 0;
3659 		v4l2_std_id *std = arg;
3660 
3661 		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3662 			ZR_DEVNAME(zr), (unsigned long long)*std);
3663 
3664 		if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3665 			norm = VIDEO_MODE_PAL;
3666 		else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3667 			norm = VIDEO_MODE_NTSC;
3668 		else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3669 			norm = VIDEO_MODE_SECAM;
3670 		else if (*std == V4L2_STD_ALL)
3671 			norm = VIDEO_MODE_AUTO;
3672 		else {
3673 			dprintk(1,
3674 				KERN_ERR
3675 				"%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3676 				ZR_DEVNAME(zr), (unsigned long long)*std);
3677 			return -EINVAL;
3678 		}
3679 
3680 		mutex_lock(&zr->resource_lock);
3681 		if ((res = zoran_set_norm(zr, norm)))
3682 			goto sstd_unlock_and_return;
3683 
3684 		res = wait_grab_pending(zr);
3685 	sstd_unlock_and_return:
3686 		mutex_unlock(&zr->resource_lock);
3687 		return res;
3688 	}
3689 		break;
3690 
3691 	case VIDIOC_ENUMINPUT:
3692 	{
3693 		struct v4l2_input *inp = arg;
3694 		int status;
3695 
3696 		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3697 			ZR_DEVNAME(zr), inp->index);
3698 
3699 		if (inp->index < 0 || inp->index >= zr->card.inputs)
3700 			return -EINVAL;
3701 		else {
3702 			int id = inp->index;
3703 			memset(inp, 0, sizeof(*inp));
3704 			inp->index = id;
3705 		}
3706 
3707 		strncpy(inp->name, zr->card.input[inp->index].name,
3708 			sizeof(inp->name) - 1);
3709 		inp->type = V4L2_INPUT_TYPE_CAMERA;
3710 		inp->std = V4L2_STD_ALL;
3711 
3712 		/* Get status of video decoder */
3713 		mutex_lock(&zr->resource_lock);
3714 		decoder_command(zr, DECODER_GET_STATUS, &status);
3715 		mutex_unlock(&zr->resource_lock);
3716 
3717 		if (!(status & DECODER_STATUS_GOOD)) {
3718 			inp->status |= V4L2_IN_ST_NO_POWER;
3719 			inp->status |= V4L2_IN_ST_NO_SIGNAL;
3720 		}
3721 		if (!(status & DECODER_STATUS_COLOR))
3722 			inp->status |= V4L2_IN_ST_NO_COLOR;
3723 
3724 		return 0;
3725 	}
3726 		break;
3727 
3728 	case VIDIOC_G_INPUT:
3729 	{
3730 		int *input = arg;
3731 
3732 		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3733 
3734 		mutex_lock(&zr->resource_lock);
3735 		*input = zr->input;
3736 		mutex_unlock(&zr->resource_lock);
3737 
3738 		return 0;
3739 	}
3740 		break;
3741 
3742 	case VIDIOC_S_INPUT:
3743 	{
3744 		int *input = arg, res = 0;
3745 
3746 		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3747 			ZR_DEVNAME(zr), *input);
3748 
3749 		mutex_lock(&zr->resource_lock);
3750 		if ((res = zoran_set_input(zr, *input)))
3751 			goto sinput_unlock_and_return;
3752 
3753 		/* Make sure the changes come into effect */
3754 		res = wait_grab_pending(zr);
3755 	sinput_unlock_and_return:
3756 		mutex_unlock(&zr->resource_lock);
3757 		return res;
3758 	}
3759 		break;
3760 
3761 	case VIDIOC_ENUMOUTPUT:
3762 	{
3763 		struct v4l2_output *outp = arg;
3764 
3765 		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3766 			ZR_DEVNAME(zr), outp->index);
3767 
3768 		if (outp->index != 0)
3769 			return -EINVAL;
3770 
3771 		memset(outp, 0, sizeof(*outp));
3772 		outp->index = 0;
3773 		outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3774 		strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3775 
3776 		return 0;
3777 	}
3778 		break;
3779 
3780 	case VIDIOC_G_OUTPUT:
3781 	{
3782 		int *output = arg;
3783 
3784 		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3785 
3786 		*output = 0;
3787 
3788 		return 0;
3789 	}
3790 		break;
3791 
3792 	case VIDIOC_S_OUTPUT:
3793 	{
3794 		int *output = arg;
3795 
3796 		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3797 			ZR_DEVNAME(zr), *output);
3798 
3799 		if (*output != 0)
3800 			return -EINVAL;
3801 
3802 		return 0;
3803 	}
3804 		break;
3805 
3806 		/* cropping (sub-frame capture) */
3807 	case VIDIOC_CROPCAP:
3808 	{
3809 		struct v4l2_cropcap *cropcap = arg;
3810 		int type = cropcap->type, res = 0;
3811 
3812 		dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3813 			ZR_DEVNAME(zr), cropcap->type);
3814 
3815 		memset(cropcap, 0, sizeof(*cropcap));
3816 		cropcap->type = type;
3817 
3818 		mutex_lock(&zr->resource_lock);
3819 
3820 		if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3821 		    (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3822 		     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3823 			dprintk(1,
3824 				KERN_ERR
3825 				"%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3826 				ZR_DEVNAME(zr));
3827 			res = -EINVAL;
3828 			goto cropcap_unlock_and_return;
3829 		}
3830 
3831 		cropcap->bounds.top = cropcap->bounds.left = 0;
3832 		cropcap->bounds.width = BUZ_MAX_WIDTH;
3833 		cropcap->bounds.height = BUZ_MAX_HEIGHT;
3834 		cropcap->defrect.top = cropcap->defrect.left = 0;
3835 		cropcap->defrect.width = BUZ_MIN_WIDTH;
3836 		cropcap->defrect.height = BUZ_MIN_HEIGHT;
3837 	cropcap_unlock_and_return:
3838 		mutex_unlock(&zr->resource_lock);
3839 		return res;
3840 	}
3841 		break;
3842 
3843 	case VIDIOC_G_CROP:
3844 	{
3845 		struct v4l2_crop *crop = arg;
3846 		int type = crop->type, res = 0;
3847 
3848 		dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3849 			ZR_DEVNAME(zr), crop->type);
3850 
3851 		memset(crop, 0, sizeof(*crop));
3852 		crop->type = type;
3853 
3854 		mutex_lock(&zr->resource_lock);
3855 
3856 		if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3857 		    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3858 		     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3859 			dprintk(1,
3860 				KERN_ERR
3861 				"%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3862 				ZR_DEVNAME(zr));
3863 			res = -EINVAL;
3864 			goto gcrop_unlock_and_return;
3865 		}
3866 
3867 		crop->c.top = fh->jpg_settings.img_y;
3868 		crop->c.left = fh->jpg_settings.img_x;
3869 		crop->c.width = fh->jpg_settings.img_width;
3870 		crop->c.height = fh->jpg_settings.img_height;
3871 
3872 	gcrop_unlock_and_return:
3873 		mutex_unlock(&zr->resource_lock);
3874 
3875 		return res;
3876 	}
3877 		break;
3878 
3879 	case VIDIOC_S_CROP:
3880 	{
3881 		struct v4l2_crop *crop = arg;
3882 		int res = 0;
3883 
3884 		settings = fh->jpg_settings;
3885 
3886 		dprintk(3,
3887 			KERN_ERR
3888 			"%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3889 			ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3890 			crop->c.width, crop->c.height);
3891 
3892 		mutex_lock(&zr->resource_lock);
3893 
3894 		if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3895 			dprintk(1,
3896 				KERN_ERR
3897 				"%s: VIDIOC_S_CROP - cannot change settings while active\n",
3898 				ZR_DEVNAME(zr));
3899 			res = -EBUSY;
3900 			goto scrop_unlock_and_return;
3901 		}
3902 
3903 		if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3904 		    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3905 		     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3906 			dprintk(1,
3907 				KERN_ERR
3908 				"%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3909 				ZR_DEVNAME(zr));
3910 			res = -EINVAL;
3911 			goto scrop_unlock_and_return;
3912 		}
3913 
3914 		/* move into a form that we understand */
3915 		settings.img_x = crop->c.left;
3916 		settings.img_y = crop->c.top;
3917 		settings.img_width = crop->c.width;
3918 		settings.img_height = crop->c.height;
3919 
3920 		/* check validity */
3921 		if ((res = zoran_check_jpg_settings(zr, &settings)))
3922 			goto scrop_unlock_and_return;
3923 
3924 		/* accept */
3925 		fh->jpg_settings = settings;
3926 
3927 	scrop_unlock_and_return:
3928 		mutex_unlock(&zr->resource_lock);
3929 		return res;
3930 	}
3931 		break;
3932 
3933 	case VIDIOC_G_JPEGCOMP:
3934 	{
3935 		struct v4l2_jpegcompression *params = arg;
3936 
3937 		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
3938 			ZR_DEVNAME(zr));
3939 
3940 		memset(params, 0, sizeof(*params));
3941 
3942 		mutex_lock(&zr->resource_lock);
3943 
3944 		params->quality = fh->jpg_settings.jpg_comp.quality;
3945 		params->APPn = fh->jpg_settings.jpg_comp.APPn;
3946 		memcpy(params->APP_data,
3947 		       fh->jpg_settings.jpg_comp.APP_data,
3948 		       fh->jpg_settings.jpg_comp.APP_len);
3949 		params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3950 		memcpy(params->COM_data,
3951 		       fh->jpg_settings.jpg_comp.COM_data,
3952 		       fh->jpg_settings.jpg_comp.COM_len);
3953 		params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3954 		params->jpeg_markers =
3955 		    fh->jpg_settings.jpg_comp.jpeg_markers;
3956 
3957 		mutex_unlock(&zr->resource_lock);
3958 
3959 		return 0;
3960 	}
3961 		break;
3962 
3963 	case VIDIOC_S_JPEGCOMP:
3964 	{
3965 		struct v4l2_jpegcompression *params = arg;
3966 		int res = 0;
3967 
3968 		settings = fh->jpg_settings;
3969 
3970 		dprintk(3,
3971 			KERN_DEBUG
3972 			"%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
3973 			ZR_DEVNAME(zr), params->quality, params->APPn,
3974 			params->APP_len, params->COM_len);
3975 
3976 		settings.jpg_comp = *params;
3977 
3978 		mutex_lock(&zr->resource_lock);
3979 
3980 		if (fh->v4l_buffers.active != ZORAN_FREE ||
3981 		    fh->jpg_buffers.active != ZORAN_FREE) {
3982 			dprintk(1,
3983 				KERN_WARNING
3984 				"%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3985 				ZR_DEVNAME(zr));
3986 			res = -EBUSY;
3987 			goto sjpegc_unlock_and_return;
3988 		}
3989 
3990 		if ((res = zoran_check_jpg_settings(zr, &settings)))
3991 			goto sjpegc_unlock_and_return;
3992 		if (!fh->jpg_buffers.allocated)
3993 			fh->jpg_buffers.buffer_size =
3994 			    zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3995 		fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3996 	sjpegc_unlock_and_return:
3997 		mutex_unlock(&zr->resource_lock);
3998 
3999 		return 0;
4000 	}
4001 		break;
4002 
4003 	case VIDIOC_QUERYSTD:	/* why is this useful? */
4004 	{
4005 		v4l2_std_id *std = arg;
4006 
4007 		dprintk(3,
4008 			KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4009 			ZR_DEVNAME(zr), (unsigned long long)*std);
4010 
4011 		if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4012 		    *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4013 					     zr->card.norms == 3)) {
4014 			return 0;
4015 		}
4016 
4017 		return -EINVAL;
4018 	}
4019 		break;
4020 
4021 	case VIDIOC_TRY_FMT:
4022 	{
4023 		struct v4l2_format *fmt = arg;
4024 		int res = 0;
4025 
4026 		dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4027 			ZR_DEVNAME(zr), fmt->type);
4028 
4029 		switch (fmt->type) {
4030 		case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4031 			mutex_lock(&zr->resource_lock);
4032 
4033 			if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4034 				fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4035 			if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4036 				fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4037 			if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4038 				fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4039 			if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4040 				fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4041 
4042 			mutex_unlock(&zr->resource_lock);
4043 			break;
4044 
4045 		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4046 		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4047 			if (fmt->fmt.pix.bytesperline > 0)
4048 				return -EINVAL;
4049 
4050 			mutex_lock(&zr->resource_lock);
4051 
4052 			if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4053 				settings = fh->jpg_settings;
4054 
4055 				/* we actually need to set 'real' parameters now */
4056 				if ((fmt->fmt.pix.height * 2) >
4057 				    BUZ_MAX_HEIGHT)
4058 					settings.TmpDcm = 1;
4059 				else
4060 					settings.TmpDcm = 2;
4061 				settings.decimation = 0;
4062 				if (fmt->fmt.pix.height <=
4063 				    fh->jpg_settings.img_height / 2)
4064 					settings.VerDcm = 2;
4065 				else
4066 					settings.VerDcm = 1;
4067 				if (fmt->fmt.pix.width <=
4068 				    fh->jpg_settings.img_width / 4)
4069 					settings.HorDcm = 4;
4070 				else if (fmt->fmt.pix.width <=
4071 					 fh->jpg_settings.img_width / 2)
4072 					settings.HorDcm = 2;
4073 				else
4074 					settings.HorDcm = 1;
4075 				if (settings.TmpDcm == 1)
4076 					settings.field_per_buff = 2;
4077 				else
4078 					settings.field_per_buff = 1;
4079 
4080 				/* check */
4081 				if ((res =
4082 				     zoran_check_jpg_settings(zr,
4083 							      &settings)))
4084 					goto tryfmt_unlock_and_return;
4085 
4086 				/* tell the user what we actually did */
4087 				fmt->fmt.pix.width =
4088 				    settings.img_width / settings.HorDcm;
4089 				fmt->fmt.pix.height =
4090 				    settings.img_height * 2 /
4091 				    (settings.TmpDcm * settings.VerDcm);
4092 				if (settings.TmpDcm == 1)
4093 					fmt->fmt.pix.field =
4094 					    (fh->jpg_settings.
4095 					     odd_even ? V4L2_FIELD_SEQ_TB :
4096 					     V4L2_FIELD_SEQ_BT);
4097 				else
4098 					fmt->fmt.pix.field =
4099 					    (fh->jpg_settings.
4100 					     odd_even ? V4L2_FIELD_TOP :
4101 					     V4L2_FIELD_BOTTOM);
4102 
4103 				fmt->fmt.pix.sizeimage =
4104 				    zoran_v4l2_calc_bufsize(&settings);
4105 			} else if (fmt->type ==
4106 				   V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4107 				int i;
4108 
4109 				for (i = 0; i < NUM_FORMATS; i++)
4110 					if (zoran_formats[i].fourcc ==
4111 					    fmt->fmt.pix.pixelformat)
4112 						break;
4113 				if (i == NUM_FORMATS) {
4114 					res = -EINVAL;
4115 					goto tryfmt_unlock_and_return;
4116 				}
4117 
4118 				if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4119 					fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4120 				if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4121 					fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4122 				if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4123 					fmt->fmt.pix.height =
4124 					    BUZ_MAX_HEIGHT;
4125 				if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4126 					fmt->fmt.pix.height =
4127 					    BUZ_MIN_HEIGHT;
4128 			} else {
4129 				res = -EINVAL;
4130 				goto tryfmt_unlock_and_return;
4131 			}
4132 		tryfmt_unlock_and_return:
4133 			mutex_unlock(&zr->resource_lock);
4134 
4135 			return res;
4136 			break;
4137 
4138 		default:
4139 			return -EINVAL;
4140 		}
4141 
4142 		return 0;
4143 	}
4144 		break;
4145 
4146 	default:
4147 		dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4148 			ZR_DEVNAME(zr), cmd);
4149 		return -ENOIOCTLCMD;
4150 		break;
4151 
4152 	}
4153 	return 0;
4154 }
4155 
4156 
4157 static long
zoran_ioctl(struct file * file,unsigned int cmd,unsigned long arg)4158 zoran_ioctl(struct file  *file,
4159 	    unsigned int  cmd,
4160 	    unsigned long arg)
4161 {
4162 	return video_usercopy(file, cmd, arg, zoran_do_ioctl);
4163 }
4164 
4165 static unsigned int
zoran_poll(struct file * file,poll_table * wait)4166 zoran_poll (struct file *file,
4167 	    poll_table  *wait)
4168 {
4169 	struct zoran_fh *fh = file->private_data;
4170 	struct zoran *zr = fh->zr;
4171 	int res = 0, frame;
4172 	unsigned long flags;
4173 
4174 	/* we should check whether buffers are ready to be synced on
4175 	 * (w/o waits - O_NONBLOCK) here
4176 	 * if ready for read (sync), return POLLIN|POLLRDNORM,
4177 	 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4178 	 * if error, return POLLERR,
4179 	 * if no buffers queued or so, return POLLNVAL
4180 	 */
4181 
4182 	mutex_lock(&zr->resource_lock);
4183 
4184 	switch (fh->map_mode) {
4185 	case ZORAN_MAP_MODE_RAW:
4186 		poll_wait(file, &zr->v4l_capq, wait);
4187 		frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4188 
4189 		spin_lock_irqsave(&zr->spinlock, flags);
4190 		dprintk(3,
4191 			KERN_DEBUG
4192 			"%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4193 			ZR_DEVNAME(zr), __func__,
4194 			"FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4195 			"UPMD"[zr->v4l_buffers.buffer[frame].state],
4196 			zr->v4l_pend_tail, zr->v4l_pend_head);
4197 		/* Process is the one capturing? */
4198 		if (fh->v4l_buffers.active != ZORAN_FREE &&
4199 		    /* Buffer ready to DQBUF? */
4200 		    zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4201 			res = POLLIN | POLLRDNORM;
4202 		spin_unlock_irqrestore(&zr->spinlock, flags);
4203 
4204 		break;
4205 
4206 	case ZORAN_MAP_MODE_JPG_REC:
4207 	case ZORAN_MAP_MODE_JPG_PLAY:
4208 		poll_wait(file, &zr->jpg_capq, wait);
4209 		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4210 
4211 		spin_lock_irqsave(&zr->spinlock, flags);
4212 		dprintk(3,
4213 			KERN_DEBUG
4214 			"%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4215 			ZR_DEVNAME(zr), __func__,
4216 			"FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4217 			"UPMD"[zr->jpg_buffers.buffer[frame].state],
4218 			zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4219 		if (fh->jpg_buffers.active != ZORAN_FREE &&
4220 		    zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4221 			if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4222 				res = POLLIN | POLLRDNORM;
4223 			else
4224 				res = POLLOUT | POLLWRNORM;
4225 		}
4226 		spin_unlock_irqrestore(&zr->spinlock, flags);
4227 
4228 		break;
4229 
4230 	default:
4231 		dprintk(1,
4232 			KERN_ERR
4233 			"%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4234 			ZR_DEVNAME(zr), fh->map_mode);
4235 		res = POLLNVAL;
4236 	}
4237 
4238 	mutex_unlock(&zr->resource_lock);
4239 
4240 	return res;
4241 }
4242 
4243 
4244 /*
4245  * This maps the buffers to user space.
4246  *
4247  * Depending on the state of fh->map_mode
4248  * the V4L or the MJPEG buffers are mapped
4249  * per buffer or all together
4250  *
4251  * Note that we need to connect to some
4252  * unmap signal event to unmap the de-allocate
4253  * the buffer accordingly (zoran_vm_close())
4254  */
4255 
4256 static void
zoran_vm_open(struct vm_area_struct * vma)4257 zoran_vm_open (struct vm_area_struct *vma)
4258 {
4259 	struct zoran_mapping *map = vma->vm_private_data;
4260 
4261 	map->count++;
4262 }
4263 
4264 static void
zoran_vm_close(struct vm_area_struct * vma)4265 zoran_vm_close (struct vm_area_struct *vma)
4266 {
4267 	struct zoran_mapping *map = vma->vm_private_data;
4268 	struct file *file = map->file;
4269 	struct zoran_fh *fh = file->private_data;
4270 	struct zoran *zr = fh->zr;
4271 	int i;
4272 
4273 	map->count--;
4274 	if (map->count == 0) {
4275 		switch (fh->map_mode) {
4276 		case ZORAN_MAP_MODE_JPG_REC:
4277 		case ZORAN_MAP_MODE_JPG_PLAY:
4278 
4279 			dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4280 				ZR_DEVNAME(zr));
4281 
4282 			for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4283 				if (fh->jpg_buffers.buffer[i].map == map) {
4284 					fh->jpg_buffers.buffer[i].map =
4285 					    NULL;
4286 				}
4287 			}
4288 			kfree(map);
4289 
4290 			for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4291 				if (fh->jpg_buffers.buffer[i].map)
4292 					break;
4293 			if (i == fh->jpg_buffers.num_buffers) {
4294 				mutex_lock(&zr->resource_lock);
4295 
4296 				if (fh->jpg_buffers.active != ZORAN_FREE) {
4297 					jpg_qbuf(file, -1, zr->codec_mode);
4298 					zr->jpg_buffers.allocated = 0;
4299 					zr->jpg_buffers.active =
4300 					    fh->jpg_buffers.active =
4301 					    ZORAN_FREE;
4302 				}
4303 				//jpg_fbuffer_free(file);
4304 				fh->jpg_buffers.allocated = 0;
4305 				fh->jpg_buffers.ready_to_be_freed = 1;
4306 
4307 				mutex_unlock(&zr->resource_lock);
4308 			}
4309 
4310 			break;
4311 
4312 		case ZORAN_MAP_MODE_RAW:
4313 
4314 			dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4315 				ZR_DEVNAME(zr));
4316 
4317 			for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4318 				if (fh->v4l_buffers.buffer[i].map == map) {
4319 					/* unqueue/unmap */
4320 					fh->v4l_buffers.buffer[i].map =
4321 					    NULL;
4322 				}
4323 			}
4324 			kfree(map);
4325 
4326 			for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4327 				if (fh->v4l_buffers.buffer[i].map)
4328 					break;
4329 			if (i == fh->v4l_buffers.num_buffers) {
4330 				mutex_lock(&zr->resource_lock);
4331 
4332 				if (fh->v4l_buffers.active != ZORAN_FREE) {
4333 					unsigned long flags;
4334 
4335 					spin_lock_irqsave(&zr->spinlock, flags);
4336 					zr36057_set_memgrab(zr, 0);
4337 					zr->v4l_buffers.allocated = 0;
4338 					zr->v4l_buffers.active =
4339 					    fh->v4l_buffers.active =
4340 					    ZORAN_FREE;
4341 					spin_unlock_irqrestore(&zr->spinlock, flags);
4342 				}
4343 				//v4l_fbuffer_free(file);
4344 				fh->v4l_buffers.allocated = 0;
4345 				fh->v4l_buffers.ready_to_be_freed = 1;
4346 
4347 				mutex_unlock(&zr->resource_lock);
4348 			}
4349 
4350 			break;
4351 
4352 		default:
4353 			printk(KERN_ERR
4354 			       "%s: munmap() - internal error - unknown map mode %d\n",
4355 			       ZR_DEVNAME(zr), fh->map_mode);
4356 			break;
4357 
4358 		}
4359 	}
4360 }
4361 
4362 static struct vm_operations_struct zoran_vm_ops = {
4363 	.open = zoran_vm_open,
4364 	.close = zoran_vm_close,
4365 };
4366 
4367 static int
zoran_mmap(struct file * file,struct vm_area_struct * vma)4368 zoran_mmap (struct file           *file,
4369 	    struct vm_area_struct *vma)
4370 {
4371 	struct zoran_fh *fh = file->private_data;
4372 	struct zoran *zr = fh->zr;
4373 	unsigned long size = (vma->vm_end - vma->vm_start);
4374 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4375 	int i, j;
4376 	unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4377 	int first, last;
4378 	struct zoran_mapping *map;
4379 	int res = 0;
4380 
4381 	dprintk(3,
4382 		KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4383 		ZR_DEVNAME(zr),
4384 		fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4385 		vma->vm_start, vma->vm_end, size);
4386 
4387 	if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4388 	    !(vma->vm_flags & VM_WRITE)) {
4389 		dprintk(1,
4390 			KERN_ERR
4391 			"%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4392 			ZR_DEVNAME(zr));
4393 		return -EINVAL;
4394 	}
4395 
4396 	switch (fh->map_mode) {
4397 
4398 	case ZORAN_MAP_MODE_JPG_REC:
4399 	case ZORAN_MAP_MODE_JPG_PLAY:
4400 
4401 		/* lock */
4402 		mutex_lock(&zr->resource_lock);
4403 
4404 		/* Map the MJPEG buffers */
4405 		if (!fh->jpg_buffers.allocated) {
4406 			dprintk(1,
4407 				KERN_ERR
4408 				"%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4409 				ZR_DEVNAME(zr));
4410 			res = -ENOMEM;
4411 			goto jpg_mmap_unlock_and_return;
4412 		}
4413 
4414 		first = offset / fh->jpg_buffers.buffer_size;
4415 		last = first - 1 + size / fh->jpg_buffers.buffer_size;
4416 		if (offset % fh->jpg_buffers.buffer_size != 0 ||
4417 		    size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4418 		    last < 0 || first >= fh->jpg_buffers.num_buffers ||
4419 		    last >= fh->jpg_buffers.num_buffers) {
4420 			dprintk(1,
4421 				KERN_ERR
4422 				"%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4423 				ZR_DEVNAME(zr), offset, size,
4424 				fh->jpg_buffers.buffer_size,
4425 				fh->jpg_buffers.num_buffers);
4426 			res = -EINVAL;
4427 			goto jpg_mmap_unlock_and_return;
4428 		}
4429 		for (i = first; i <= last; i++) {
4430 			if (fh->jpg_buffers.buffer[i].map) {
4431 				dprintk(1,
4432 					KERN_ERR
4433 					"%s: mmap(MJPEG) - buffer %d already mapped\n",
4434 					ZR_DEVNAME(zr), i);
4435 				res = -EBUSY;
4436 				goto jpg_mmap_unlock_and_return;
4437 			}
4438 		}
4439 
4440 		/* map these buffers (v4l_buffers[i]) */
4441 		map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4442 		if (!map) {
4443 			res = -ENOMEM;
4444 			goto jpg_mmap_unlock_and_return;
4445 		}
4446 		map->file = file;
4447 		map->count = 1;
4448 
4449 		vma->vm_ops = &zoran_vm_ops;
4450 		vma->vm_flags |= VM_DONTEXPAND;
4451 		vma->vm_private_data = map;
4452 
4453 		for (i = first; i <= last; i++) {
4454 			for (j = 0;
4455 			     j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4456 			     j++) {
4457 				fraglen =
4458 				    (le32_to_cpu(fh->jpg_buffers.buffer[i].
4459 				     frag_tab[2 * j + 1]) & ~1) << 1;
4460 				todo = size;
4461 				if (todo > fraglen)
4462 					todo = fraglen;
4463 				pos =
4464 				    le32_to_cpu(fh->jpg_buffers.
4465 				    buffer[i].frag_tab[2 * j]);
4466 				/* should just be pos on i386 */
4467 				page = virt_to_phys(bus_to_virt(pos))
4468 								>> PAGE_SHIFT;
4469 				if (remap_pfn_range(vma, start, page,
4470 							todo, PAGE_SHARED)) {
4471 					dprintk(1,
4472 						KERN_ERR
4473 						"%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4474 						ZR_DEVNAME(zr));
4475 					res = -EAGAIN;
4476 					goto jpg_mmap_unlock_and_return;
4477 				}
4478 				size -= todo;
4479 				start += todo;
4480 				if (size == 0)
4481 					break;
4482 				if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4483 				    frag_tab[2 * j + 1]) & 1)
4484 					break;	/* was last fragment */
4485 			}
4486 			fh->jpg_buffers.buffer[i].map = map;
4487 			if (size == 0)
4488 				break;
4489 
4490 		}
4491 	jpg_mmap_unlock_and_return:
4492 		mutex_unlock(&zr->resource_lock);
4493 
4494 		break;
4495 
4496 	case ZORAN_MAP_MODE_RAW:
4497 
4498 		mutex_lock(&zr->resource_lock);
4499 
4500 		/* Map the V4L buffers */
4501 		if (!fh->v4l_buffers.allocated) {
4502 			dprintk(1,
4503 				KERN_ERR
4504 				"%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4505 				ZR_DEVNAME(zr));
4506 			res = -ENOMEM;
4507 			goto v4l_mmap_unlock_and_return;
4508 		}
4509 
4510 		first = offset / fh->v4l_buffers.buffer_size;
4511 		last = first - 1 + size / fh->v4l_buffers.buffer_size;
4512 		if (offset % fh->v4l_buffers.buffer_size != 0 ||
4513 		    size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4514 		    last < 0 || first >= fh->v4l_buffers.num_buffers ||
4515 		    last >= fh->v4l_buffers.buffer_size) {
4516 			dprintk(1,
4517 				KERN_ERR
4518 				"%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4519 				ZR_DEVNAME(zr), offset, size,
4520 				fh->v4l_buffers.buffer_size,
4521 				fh->v4l_buffers.num_buffers);
4522 			res = -EINVAL;
4523 			goto v4l_mmap_unlock_and_return;
4524 		}
4525 		for (i = first; i <= last; i++) {
4526 			if (fh->v4l_buffers.buffer[i].map) {
4527 				dprintk(1,
4528 					KERN_ERR
4529 					"%s: mmap(V4L) - buffer %d already mapped\n",
4530 					ZR_DEVNAME(zr), i);
4531 				res = -EBUSY;
4532 				goto v4l_mmap_unlock_and_return;
4533 			}
4534 		}
4535 
4536 		/* map these buffers (v4l_buffers[i]) */
4537 		map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4538 		if (!map) {
4539 			res = -ENOMEM;
4540 			goto v4l_mmap_unlock_and_return;
4541 		}
4542 		map->file = file;
4543 		map->count = 1;
4544 
4545 		vma->vm_ops = &zoran_vm_ops;
4546 		vma->vm_flags |= VM_DONTEXPAND;
4547 		vma->vm_private_data = map;
4548 
4549 		for (i = first; i <= last; i++) {
4550 			todo = size;
4551 			if (todo > fh->v4l_buffers.buffer_size)
4552 				todo = fh->v4l_buffers.buffer_size;
4553 			page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4554 			if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4555 							todo, PAGE_SHARED)) {
4556 				dprintk(1,
4557 					KERN_ERR
4558 					"%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4559 					ZR_DEVNAME(zr));
4560 				res = -EAGAIN;
4561 				goto v4l_mmap_unlock_and_return;
4562 			}
4563 			size -= todo;
4564 			start += todo;
4565 			fh->v4l_buffers.buffer[i].map = map;
4566 			if (size == 0)
4567 				break;
4568 		}
4569 	v4l_mmap_unlock_and_return:
4570 		mutex_unlock(&zr->resource_lock);
4571 
4572 		break;
4573 
4574 	default:
4575 		dprintk(1,
4576 			KERN_ERR
4577 			"%s: zoran_mmap() - internal error - unknown map mode %d\n",
4578 			ZR_DEVNAME(zr), fh->map_mode);
4579 		break;
4580 	}
4581 
4582 	return 0;
4583 }
4584 
4585 static const struct v4l2_file_operations zoran_fops = {
4586 	.owner = THIS_MODULE,
4587 	.open = zoran_open,
4588 	.release = zoran_close,
4589 	.ioctl = zoran_ioctl,
4590 	.read = zoran_read,
4591 	.write = zoran_write,
4592 	.mmap = zoran_mmap,
4593 	.poll = zoran_poll,
4594 };
4595 
4596 struct video_device zoran_template __devinitdata = {
4597 	.name = ZORAN_NAME,
4598 	.fops = &zoran_fops,
4599 	.release = &zoran_vdev_release,
4600 	.minor = -1
4601 };
4602 
4603