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