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