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