V4L/DVB (10138): v4l2-ioctl: change to long return type to match unlocked_ioctl.
[linux-2.6.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 file  *file)
1201 {
1202         unsigned int minor = video_devdata(file)->minor;
1203         struct zoran *zr = NULL;
1204         struct zoran_fh *fh;
1205         int i, res, first_open = 0, have_module_locks = 0;
1206
1207         lock_kernel();
1208         /* find the device */
1209         for (i = 0; i < zoran_num; i++) {
1210                 if (zoran[i]->video_dev->minor == minor) {
1211                         zr = zoran[i];
1212                         break;
1213                 }
1214         }
1215
1216         if (!zr) {
1217                 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1218                 res = -ENODEV;
1219                 goto open_unlock_and_return;
1220         }
1221
1222         /* see fs/device.c - the kernel already locks during open(),
1223          * so locking ourselves only causes deadlocks */
1224         /*mutex_lock(&zr->resource_lock);*/
1225
1226         if (!zr->decoder) {
1227                 dprintk(1,
1228                         KERN_ERR "%s: no TV decoder loaded for device!\n",
1229                         ZR_DEVNAME(zr));
1230                 res = -EIO;
1231                 goto open_unlock_and_return;
1232         }
1233
1234         /* try to grab a module lock */
1235         if (!try_module_get(THIS_MODULE)) {
1236                 dprintk(1,
1237                         KERN_ERR
1238                         "%s: failed to acquire my own lock! PANIC!\n",
1239                         ZR_DEVNAME(zr));
1240                 res = -ENODEV;
1241                 goto open_unlock_and_return;
1242         }
1243         if (!try_module_get(zr->decoder->driver->driver.owner)) {
1244                 dprintk(1,
1245                         KERN_ERR
1246                         "%s: failed to grab ownership of i2c decoder\n",
1247                         ZR_DEVNAME(zr));
1248                 res = -EIO;
1249                 module_put(THIS_MODULE);
1250                 goto open_unlock_and_return;
1251         }
1252         if (zr->encoder &&
1253             !try_module_get(zr->encoder->driver->driver.owner)) {
1254                 dprintk(1,
1255                         KERN_ERR
1256                         "%s: failed to grab ownership of i2c encoder\n",
1257                         ZR_DEVNAME(zr));
1258                 res = -EIO;
1259                 module_put(zr->decoder->driver->driver.owner);
1260                 module_put(THIS_MODULE);
1261                 goto open_unlock_and_return;
1262         }
1263
1264         have_module_locks = 1;
1265
1266         if (zr->user >= 2048) {
1267                 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1268                         ZR_DEVNAME(zr), zr->user);
1269                 res = -EBUSY;
1270                 goto open_unlock_and_return;
1271         }
1272
1273         dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1274                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1275
1276         /* now, create the open()-specific file_ops struct */
1277         fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1278         if (!fh) {
1279                 dprintk(1,
1280                         KERN_ERR
1281                         "%s: zoran_open() - allocation of zoran_fh failed\n",
1282                         ZR_DEVNAME(zr));
1283                 res = -ENOMEM;
1284                 goto open_unlock_and_return;
1285         }
1286         /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1287          * on norm-change! */
1288         fh->overlay_mask =
1289             kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1290         if (!fh->overlay_mask) {
1291                 dprintk(1,
1292                         KERN_ERR
1293                         "%s: zoran_open() - allocation of overlay_mask failed\n",
1294                         ZR_DEVNAME(zr));
1295                 kfree(fh);
1296                 res = -ENOMEM;
1297                 goto open_unlock_and_return;
1298         }
1299
1300         if (zr->user++ == 0)
1301                 first_open = 1;
1302
1303         /*mutex_unlock(&zr->resource_lock);*/
1304
1305         /* default setup - TODO: look at flags */
1306         if (first_open) {       /* First device open */
1307                 zr36057_restart(zr);
1308                 zoran_open_init_params(zr);
1309                 zoran_init_hardware(zr);
1310
1311                 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1312         }
1313
1314         /* set file_ops stuff */
1315         file->private_data = fh;
1316         fh->zr = zr;
1317         zoran_open_init_session(file);
1318         unlock_kernel();
1319
1320         return 0;
1321
1322 open_unlock_and_return:
1323         /* if we grabbed locks, release them accordingly */
1324         if (have_module_locks) {
1325                 module_put(zr->decoder->driver->driver.owner);
1326                 if (zr->encoder) {
1327                         module_put(zr->encoder->driver->driver.owner);
1328                 }
1329                 module_put(THIS_MODULE);
1330         }
1331
1332         /* if there's no device found, we didn't obtain the lock either */
1333         if (zr) {
1334                 /*mutex_unlock(&zr->resource_lock);*/
1335         }
1336         unlock_kernel();
1337
1338         return res;
1339 }
1340
1341 static int
1342 zoran_close(struct file  *file)
1343 {
1344         struct zoran_fh *fh = file->private_data;
1345         struct zoran *zr = fh->zr;
1346
1347         dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1348                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1349
1350         /* kernel locks (fs/device.c), so don't do that ourselves
1351          * (prevents deadlocks) */
1352         /*mutex_lock(&zr->resource_lock);*/
1353
1354         zoran_close_end_session(file);
1355
1356         if (zr->user-- == 1) {  /* Last process */
1357                 /* Clean up JPEG process */
1358                 wake_up_interruptible(&zr->jpg_capq);
1359                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1360                 zr->jpg_buffers.allocated = 0;
1361                 zr->jpg_buffers.active = ZORAN_FREE;
1362
1363                 /* disable interrupts */
1364                 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1365
1366                 if (zr36067_debug > 1)
1367                         print_interrupts(zr);
1368
1369                 /* Overlay off */
1370                 zr->v4l_overlay_active = 0;
1371                 zr36057_overlay(zr, 0);
1372                 zr->overlay_mask = NULL;
1373
1374                 /* capture off */
1375                 wake_up_interruptible(&zr->v4l_capq);
1376                 zr36057_set_memgrab(zr, 0);
1377                 zr->v4l_buffers.allocated = 0;
1378                 zr->v4l_buffers.active = ZORAN_FREE;
1379                 zoran_set_pci_master(zr, 0);
1380
1381                 if (!pass_through) {    /* Switch to color bar */
1382                         int zero = 0, two = 2;
1383                         decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1384                         encoder_command(zr, ENCODER_SET_INPUT, &two);
1385                 }
1386         }
1387
1388         file->private_data = NULL;
1389         kfree(fh->overlay_mask);
1390         kfree(fh);
1391
1392         /* release locks on the i2c modules */
1393         module_put(zr->decoder->driver->driver.owner);
1394         if (zr->encoder) {
1395                  module_put(zr->encoder->driver->driver.owner);
1396         }
1397         module_put(THIS_MODULE);
1398
1399         /*mutex_unlock(&zr->resource_lock);*/
1400
1401         dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1402
1403         return 0;
1404 }
1405
1406
1407 static ssize_t
1408 zoran_read (struct file *file,
1409             char        __user *data,
1410             size_t       count,
1411             loff_t      *ppos)
1412 {
1413         /* we simply don't support read() (yet)... */
1414
1415         return -EINVAL;
1416 }
1417
1418 static ssize_t
1419 zoran_write (struct file *file,
1420              const char  __user *data,
1421              size_t       count,
1422              loff_t      *ppos)
1423 {
1424         /* ...and the same goes for write() */
1425
1426         return -EINVAL;
1427 }
1428
1429 static int
1430 setup_fbuffer (struct file               *file,
1431                void                      *base,
1432                const struct zoran_format *fmt,
1433                int                        width,
1434                int                        height,
1435                int                        bytesperline)
1436 {
1437         struct zoran_fh *fh = file->private_data;
1438         struct zoran *zr = fh->zr;
1439
1440         /* (Ronald) v4l/v4l2 guidelines */
1441         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1442                 return -EPERM;
1443
1444         /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1445            ALi Magik (that needs very low latency while the card needs a
1446            higher value always) */
1447
1448         if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1449                 return -ENXIO;
1450
1451         /* we need a bytesperline value, even if not given */
1452         if (!bytesperline)
1453                 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1454
1455 #if 0
1456         if (zr->overlay_active) {
1457                 /* dzjee... stupid users... don't even bother to turn off
1458                  * overlay before changing the memory location...
1459                  * normally, we would return errors here. However, one of
1460                  * the tools that does this is... xawtv! and since xawtv
1461                  * is used by +/- 99% of the users, we'd rather be user-
1462                  * friendly and silently do as if nothing went wrong */
1463                 dprintk(3,
1464                         KERN_ERR
1465                         "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1466                         ZR_DEVNAME(zr));
1467                 zr36057_overlay(zr, 0);
1468         }
1469 #endif
1470
1471         if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1472                 dprintk(1,
1473                         KERN_ERR
1474                         "%s: setup_fbuffer() - no valid overlay format given\n",
1475                         ZR_DEVNAME(zr));
1476                 return -EINVAL;
1477         }
1478         if (height <= 0 || width <= 0 || bytesperline <= 0) {
1479                 dprintk(1,
1480                         KERN_ERR
1481                         "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1482                         ZR_DEVNAME(zr), width, height, bytesperline);
1483                 return -EINVAL;
1484         }
1485         if (bytesperline & 3) {
1486                 dprintk(1,
1487                         KERN_ERR
1488                         "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1489                         ZR_DEVNAME(zr), bytesperline);
1490                 return -EINVAL;
1491         }
1492
1493         zr->buffer.base = (void *) ((unsigned long) base & ~3);
1494         zr->buffer.height = height;
1495         zr->buffer.width = width;
1496         zr->buffer.depth = fmt->depth;
1497         zr->overlay_settings.format = fmt;
1498         zr->buffer.bytesperline = bytesperline;
1499
1500         /* The user should set new window parameters */
1501         zr->overlay_settings.is_set = 0;
1502
1503         return 0;
1504 }
1505
1506
1507 static int
1508 setup_window (struct file       *file,
1509               int                x,
1510               int                y,
1511               int                width,
1512               int                height,
1513               struct video_clip __user *clips,
1514               int                clipcount,
1515               void              __user *bitmap)
1516 {
1517         struct zoran_fh *fh = file->private_data;
1518         struct zoran *zr = fh->zr;
1519         struct video_clip *vcp = NULL;
1520         int on, end;
1521
1522
1523         if (!zr->buffer.base) {
1524                 dprintk(1,
1525                         KERN_ERR
1526                         "%s: setup_window() - frame buffer has to be set first\n",
1527                         ZR_DEVNAME(zr));
1528                 return -EINVAL;
1529         }
1530
1531         if (!fh->overlay_settings.format) {
1532                 dprintk(1,
1533                         KERN_ERR
1534                         "%s: setup_window() - no overlay format set\n",
1535                         ZR_DEVNAME(zr));
1536                 return -EINVAL;
1537         }
1538
1539         /*
1540          * The video front end needs 4-byte alinged line sizes, we correct that
1541          * silently here if necessary
1542          */
1543         if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1544                 end = (x + width) & ~1; /* round down */
1545                 x = (x + 1) & ~1;       /* round up */
1546                 width = end - x;
1547         }
1548
1549         if (zr->buffer.depth == 24) {
1550                 end = (x + width) & ~3; /* round down */
1551                 x = (x + 3) & ~3;       /* round up */
1552                 width = end - x;
1553         }
1554
1555         if (width > BUZ_MAX_WIDTH)
1556                 width = BUZ_MAX_WIDTH;
1557         if (height > BUZ_MAX_HEIGHT)
1558                 height = BUZ_MAX_HEIGHT;
1559
1560         /* Check for vaild parameters */
1561         if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1562             width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1563                 dprintk(1,
1564                         KERN_ERR
1565                         "%s: setup_window() - width = %d or height = %d invalid\n",
1566                         ZR_DEVNAME(zr), width, height);
1567                 return -EINVAL;
1568         }
1569
1570         fh->overlay_settings.x = x;
1571         fh->overlay_settings.y = y;
1572         fh->overlay_settings.width = width;
1573         fh->overlay_settings.height = height;
1574         fh->overlay_settings.clipcount = clipcount;
1575
1576         /*
1577          * If an overlay is running, we have to switch it off
1578          * and switch it on again in order to get the new settings in effect.
1579          *
1580          * We also want to avoid that the overlay mask is written
1581          * when an overlay is running.
1582          */
1583
1584         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1585             zr->overlay_active != ZORAN_FREE &&
1586             fh->overlay_active != ZORAN_FREE;
1587         if (on)
1588                 zr36057_overlay(zr, 0);
1589
1590         /*
1591          *   Write the overlay mask if clips are wanted.
1592          *   We prefer a bitmap.
1593          */
1594         if (bitmap) {
1595                 /* fake value - it just means we want clips */
1596                 fh->overlay_settings.clipcount = 1;
1597
1598                 if (copy_from_user(fh->overlay_mask, bitmap,
1599                                    (width * height + 7) / 8)) {
1600                         return -EFAULT;
1601                 }
1602         } else if (clipcount > 0) {
1603                 /* write our own bitmap from the clips */
1604                 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1605                 if (vcp == NULL) {
1606                         dprintk(1,
1607                                 KERN_ERR
1608                                 "%s: setup_window() - Alloc of clip mask failed\n",
1609                                 ZR_DEVNAME(zr));
1610                         return -ENOMEM;
1611                 }
1612                 if (copy_from_user
1613                     (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1614                         vfree(vcp);
1615                         return -EFAULT;
1616                 }
1617                 write_overlay_mask(file, vcp, clipcount);
1618                 vfree(vcp);
1619         }
1620
1621         fh->overlay_settings.is_set = 1;
1622         if (fh->overlay_active != ZORAN_FREE &&
1623             zr->overlay_active != ZORAN_FREE)
1624                 zr->overlay_settings = fh->overlay_settings;
1625
1626         if (on)
1627                 zr36057_overlay(zr, 1);
1628
1629         /* Make sure the changes come into effect */
1630         return wait_grab_pending(zr);
1631 }
1632
1633 static int
1634 setup_overlay (struct file *file,
1635                int          on)
1636 {
1637         struct zoran_fh *fh = file->private_data;
1638         struct zoran *zr = fh->zr;
1639
1640         /* If there is nothing to do, return immediatly */
1641         if ((on && fh->overlay_active != ZORAN_FREE) ||
1642             (!on && fh->overlay_active == ZORAN_FREE))
1643                 return 0;
1644
1645         /* check whether we're touching someone else's overlay */
1646         if (on && zr->overlay_active != ZORAN_FREE &&
1647             fh->overlay_active == ZORAN_FREE) {
1648                 dprintk(1,
1649                         KERN_ERR
1650                         "%s: setup_overlay() - overlay is already active for another session\n",
1651                         ZR_DEVNAME(zr));
1652                 return -EBUSY;
1653         }
1654         if (!on && zr->overlay_active != ZORAN_FREE &&
1655             fh->overlay_active == ZORAN_FREE) {
1656                 dprintk(1,
1657                         KERN_ERR
1658                         "%s: setup_overlay() - you cannot cancel someone else's session\n",
1659                         ZR_DEVNAME(zr));
1660                 return -EPERM;
1661         }
1662
1663         if (on == 0) {
1664                 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1665                 zr->v4l_overlay_active = 0;
1666                 /* When a grab is running, the video simply
1667                  * won't be switched on any more */
1668                 if (!zr->v4l_memgrab_active)
1669                         zr36057_overlay(zr, 0);
1670                 zr->overlay_mask = NULL;
1671         } else {
1672                 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1673                         dprintk(1,
1674                                 KERN_ERR
1675                                 "%s: setup_overlay() - buffer or window not set\n",
1676                                 ZR_DEVNAME(zr));
1677                         return -EINVAL;
1678                 }
1679                 if (!fh->overlay_settings.format) {
1680                         dprintk(1,
1681                                 KERN_ERR
1682                                 "%s: setup_overlay() - no overlay format set\n",
1683                                 ZR_DEVNAME(zr));
1684                         return -EINVAL;
1685                 }
1686                 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1687                 zr->v4l_overlay_active = 1;
1688                 zr->overlay_mask = fh->overlay_mask;
1689                 zr->overlay_settings = fh->overlay_settings;
1690                 if (!zr->v4l_memgrab_active)
1691                         zr36057_overlay(zr, 1);
1692                 /* When a grab is running, the video will be
1693                  * switched on when grab is finished */
1694         }
1695
1696         /* Make sure the changes come into effect */
1697         return wait_grab_pending(zr);
1698 }
1699
1700         /* get the status of a buffer in the clients buffer queue */
1701 static int
1702 zoran_v4l2_buffer_status (struct file        *file,
1703                           struct v4l2_buffer *buf,
1704                           int                 num)
1705 {
1706         struct zoran_fh *fh = file->private_data;
1707         struct zoran *zr = fh->zr;
1708
1709         buf->flags = V4L2_BUF_FLAG_MAPPED;
1710
1711         switch (fh->map_mode) {
1712         case ZORAN_MAP_MODE_RAW:
1713
1714                 /* check range */
1715                 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1716                     !fh->v4l_buffers.allocated) {
1717                         dprintk(1,
1718                                 KERN_ERR
1719                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1720                                 ZR_DEVNAME(zr));
1721                         return -EINVAL;
1722                 }
1723
1724                 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1725                 buf->length = fh->v4l_buffers.buffer_size;
1726
1727                 /* get buffer */
1728                 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1729                 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1730                     fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1731                         buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1732                         buf->flags |= V4L2_BUF_FLAG_DONE;
1733                         buf->timestamp =
1734                             fh->v4l_buffers.buffer[num].bs.timestamp;
1735                 } else {
1736                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1737                 }
1738
1739                 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1740                         buf->field = V4L2_FIELD_TOP;
1741                 else
1742                         buf->field = V4L2_FIELD_INTERLACED;
1743
1744                 break;
1745
1746         case ZORAN_MAP_MODE_JPG_REC:
1747         case ZORAN_MAP_MODE_JPG_PLAY:
1748
1749                 /* check range */
1750                 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1751                     !fh->jpg_buffers.allocated) {
1752                         dprintk(1,
1753                                 KERN_ERR
1754                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1755                                 ZR_DEVNAME(zr));
1756                         return -EINVAL;
1757                 }
1758
1759                 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1760                               V4L2_BUF_TYPE_VIDEO_CAPTURE :
1761                               V4L2_BUF_TYPE_VIDEO_OUTPUT;
1762                 buf->length = fh->jpg_buffers.buffer_size;
1763
1764                 /* these variables are only written after frame has been captured */
1765                 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1766                     fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1767                         buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1768                         buf->timestamp =
1769                             fh->jpg_buffers.buffer[num].bs.timestamp;
1770                         buf->bytesused =
1771                             fh->jpg_buffers.buffer[num].bs.length;
1772                         buf->flags |= V4L2_BUF_FLAG_DONE;
1773                 } else {
1774                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1775                 }
1776
1777                 /* which fields are these? */
1778                 if (fh->jpg_settings.TmpDcm != 1)
1779                         buf->field =
1780                             fh->jpg_settings.
1781                             odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1782                 else
1783                         buf->field =
1784                             fh->jpg_settings.
1785                             odd_even ? V4L2_FIELD_SEQ_TB :
1786                             V4L2_FIELD_SEQ_BT;
1787
1788                 break;
1789
1790         default:
1791
1792                 dprintk(5,
1793                         KERN_ERR
1794                         "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1795                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
1796                 return -EINVAL;
1797         }
1798
1799         buf->memory = V4L2_MEMORY_MMAP;
1800         buf->index = num;
1801         buf->m.offset = buf->length * num;
1802
1803         return 0;
1804 }
1805
1806 static int
1807 zoran_set_norm (struct zoran *zr,
1808                 int           norm) /* VIDEO_MODE_* */
1809 {
1810         int norm_encoder, on;
1811
1812         if (zr->v4l_buffers.active != ZORAN_FREE ||
1813             zr->jpg_buffers.active != ZORAN_FREE) {
1814                 dprintk(1,
1815                         KERN_WARNING
1816                         "%s: set_norm() called while in playback/capture mode\n",
1817                         ZR_DEVNAME(zr));
1818                 return -EBUSY;
1819         }
1820
1821         if (lock_norm && norm != zr->norm) {
1822                 if (lock_norm > 1) {
1823                         dprintk(1,
1824                                 KERN_WARNING
1825                                 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1826                                 ZR_DEVNAME(zr));
1827                         return -EPERM;
1828                 } else {
1829                         dprintk(1,
1830                                 KERN_WARNING
1831                                 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1832                                 ZR_DEVNAME(zr));
1833                         norm = zr->norm;
1834                 }
1835         }
1836
1837         if (norm != VIDEO_MODE_AUTO &&
1838             (norm < 0 || norm >= zr->card.norms ||
1839              !zr->card.tvn[norm])) {
1840                 dprintk(1,
1841                         KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1842                         ZR_DEVNAME(zr), norm);
1843                 return -EINVAL;
1844         }
1845
1846         if (norm == VIDEO_MODE_AUTO) {
1847                 int status;
1848
1849                 /* if we have autodetect, ... */
1850                 struct video_decoder_capability caps;
1851                 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1852                 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1853                         dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1854                                 ZR_DEVNAME(zr));
1855                         return -EINVAL;
1856                 }
1857
1858                 decoder_command(zr, DECODER_SET_NORM, &norm);
1859
1860                 /* let changes come into effect */
1861                 ssleep(2);
1862
1863                 decoder_command(zr, DECODER_GET_STATUS, &status);
1864                 if (!(status & DECODER_STATUS_GOOD)) {
1865                         dprintk(1,
1866                                 KERN_ERR
1867                                 "%s: set_norm() - no norm detected\n",
1868                                 ZR_DEVNAME(zr));
1869                         /* reset norm */
1870                         decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1871                         return -EIO;
1872                 }
1873
1874                 if (status & DECODER_STATUS_NTSC)
1875                         norm = VIDEO_MODE_NTSC;
1876                 else if (status & DECODER_STATUS_SECAM)
1877                         norm = VIDEO_MODE_SECAM;
1878                 else
1879                         norm = VIDEO_MODE_PAL;
1880         }
1881         zr->timing = zr->card.tvn[norm];
1882         norm_encoder = norm;
1883
1884         /* We switch overlay off and on since a change in the
1885          * norm needs different VFE settings */
1886         on = zr->overlay_active && !zr->v4l_memgrab_active;
1887         if (on)
1888                 zr36057_overlay(zr, 0);
1889
1890         decoder_command(zr, DECODER_SET_NORM, &norm);
1891         encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1892
1893         if (on)
1894                 zr36057_overlay(zr, 1);
1895
1896         /* Make sure the changes come into effect */
1897         zr->norm = norm;
1898
1899         return 0;
1900 }
1901
1902 static int
1903 zoran_set_input (struct zoran *zr,
1904                  int           input)
1905 {
1906         int realinput;
1907
1908         if (input == zr->input) {
1909                 return 0;
1910         }
1911
1912         if (zr->v4l_buffers.active != ZORAN_FREE ||
1913             zr->jpg_buffers.active != ZORAN_FREE) {
1914                 dprintk(1,
1915                         KERN_WARNING
1916                         "%s: set_input() called while in playback/capture mode\n",
1917                         ZR_DEVNAME(zr));
1918                 return -EBUSY;
1919         }
1920
1921         if (input < 0 || input >= zr->card.inputs) {
1922                 dprintk(1,
1923                         KERN_ERR
1924                         "%s: set_input() - unnsupported input %d\n",
1925                         ZR_DEVNAME(zr), input);
1926                 return -EINVAL;
1927         }
1928
1929         realinput = zr->card.input[input].muxsel;
1930         zr->input = input;
1931
1932         decoder_command(zr, DECODER_SET_INPUT, &realinput);
1933
1934         return 0;
1935 }
1936
1937 /*
1938  *   ioctl routine
1939  */
1940
1941 static long zoran_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1942 {
1943         struct zoran_fh *fh = file->private_data;
1944         struct zoran *zr = fh->zr;
1945         /* CAREFUL: used in multiple places here */
1946         struct zoran_jpg_settings settings;
1947
1948         /* we might have older buffers lying around... We don't want
1949          * to wait, but we do want to try cleaning them up ASAP. So
1950          * we try to obtain the lock and free them. If that fails, we
1951          * don't do anything and wait for the next turn. In the end,
1952          * zoran_close() or a new allocation will still free them...
1953          * This is just a 'the sooner the better' extra 'feature'
1954          *
1955          * We don't free the buffers right on munmap() because that
1956          * causes oopses (kfree() inside munmap() oopses for no
1957          * apparent reason - it's also not reproduceable in any way,
1958          * but moving the free code outside the munmap() handler fixes
1959          * all this... If someone knows why, please explain me (Ronald)
1960          */
1961         if (mutex_trylock(&zr->resource_lock)) {
1962                 /* we obtained it! Let's try to free some things */
1963                 if (fh->jpg_buffers.ready_to_be_freed)
1964                         jpg_fbuffer_free(file);
1965                 if (fh->v4l_buffers.ready_to_be_freed)
1966                         v4l_fbuffer_free(file);
1967
1968                 mutex_unlock(&zr->resource_lock);
1969         }
1970
1971         switch (cmd) {
1972
1973         case VIDIOCGCAP:
1974         {
1975                 struct video_capability *vcap = arg;
1976
1977                 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
1978
1979                 memset(vcap, 0, sizeof(struct video_capability));
1980                 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
1981                 vcap->type = ZORAN_VID_TYPE;
1982
1983                 vcap->channels = zr->card.inputs;
1984                 vcap->audios = 0;
1985                 mutex_lock(&zr->resource_lock);
1986                 vcap->maxwidth = BUZ_MAX_WIDTH;
1987                 vcap->maxheight = BUZ_MAX_HEIGHT;
1988                 vcap->minwidth = BUZ_MIN_WIDTH;
1989                 vcap->minheight = BUZ_MIN_HEIGHT;
1990                 mutex_unlock(&zr->resource_lock);
1991
1992                 return 0;
1993         }
1994                 break;
1995
1996         case VIDIOCGCHAN:
1997         {
1998                 struct video_channel *vchan = arg;
1999                 int channel = vchan->channel;
2000
2001                 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2002                         ZR_DEVNAME(zr), vchan->channel);
2003
2004                 memset(vchan, 0, sizeof(struct video_channel));
2005                 if (channel > zr->card.inputs || channel < 0) {
2006                         dprintk(1,
2007                                 KERN_ERR
2008                                 "%s: VIDIOCGCHAN on not existing channel %d\n",
2009                                 ZR_DEVNAME(zr), channel);
2010                         return -EINVAL;
2011                 }
2012
2013                 strcpy(vchan->name, zr->card.input[channel].name);
2014
2015                 vchan->tuners = 0;
2016                 vchan->flags = 0;
2017                 vchan->type = VIDEO_TYPE_CAMERA;
2018                 mutex_lock(&zr->resource_lock);
2019                 vchan->norm = zr->norm;
2020                 mutex_unlock(&zr->resource_lock);
2021                 vchan->channel = channel;
2022
2023                 return 0;
2024         }
2025                 break;
2026
2027                 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2028                  *
2029                  * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2030                  * *                                 ^^^^^^^
2031                  * * The famos BTTV driver has it implemented with a struct video_channel argument
2032                  * * and we follow it for compatibility reasons
2033                  * *
2034                  * * BTW: this is the only way the user can set the norm!
2035                  */
2036
2037         case VIDIOCSCHAN:
2038         {
2039                 struct video_channel *vchan = arg;
2040                 int res;
2041
2042                 dprintk(3,
2043                         KERN_DEBUG
2044                         "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2045                         ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2046
2047                 mutex_lock(&zr->resource_lock);
2048                 if ((res = zoran_set_input(zr, vchan->channel)))
2049                         goto schan_unlock_and_return;
2050                 if ((res = zoran_set_norm(zr, vchan->norm)))
2051                         goto schan_unlock_and_return;
2052
2053                 /* Make sure the changes come into effect */
2054                 res = wait_grab_pending(zr);
2055         schan_unlock_and_return:
2056                 mutex_unlock(&zr->resource_lock);
2057                 return res;
2058         }
2059                 break;
2060
2061         case VIDIOCGPICT:
2062         {
2063                 struct video_picture *vpict = arg;
2064
2065                 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2066
2067                 memset(vpict, 0, sizeof(struct video_picture));
2068                 mutex_lock(&zr->resource_lock);
2069                 vpict->hue = zr->hue;
2070                 vpict->brightness = zr->brightness;
2071                 vpict->contrast = zr->contrast;
2072                 vpict->colour = zr->saturation;
2073                 if (fh->overlay_settings.format) {
2074                         vpict->depth = fh->overlay_settings.format->depth;
2075                         vpict->palette = fh->overlay_settings.format->palette;
2076                 } else {
2077                         vpict->depth = 0;
2078                 }
2079                 mutex_unlock(&zr->resource_lock);
2080
2081                 return 0;
2082         }
2083                 break;
2084
2085         case VIDIOCSPICT:
2086         {
2087                 struct video_picture *vpict = arg;
2088                 int i;
2089
2090                 dprintk(3,
2091                         KERN_DEBUG
2092                         "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2093                         ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2094                         vpict->colour, vpict->contrast, vpict->depth,
2095                         vpict->palette);
2096
2097                 for (i = 0; i < NUM_FORMATS; i++) {
2098                         const struct zoran_format *fmt = &zoran_formats[i];
2099
2100                         if (fmt->palette != -1 &&
2101                             fmt->flags & ZORAN_FORMAT_OVERLAY &&
2102                             fmt->palette == vpict->palette &&
2103                             fmt->depth == vpict->depth)
2104                                 break;
2105                 }
2106                 if (i == NUM_FORMATS) {
2107                         dprintk(1,
2108                                 KERN_ERR
2109                                 "%s: VIDIOCSPICT - Invalid palette %d\n",
2110                                 ZR_DEVNAME(zr), vpict->palette);
2111                         return -EINVAL;
2112                 }
2113
2114                 mutex_lock(&zr->resource_lock);
2115
2116                 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2117
2118                 zr->hue = vpict->hue;
2119                 zr->contrast = vpict->contrast;
2120                 zr->saturation = vpict->colour;
2121                 zr->brightness = vpict->brightness;
2122
2123                 fh->overlay_settings.format = &zoran_formats[i];
2124
2125                 mutex_unlock(&zr->resource_lock);
2126
2127                 return 0;
2128         }
2129                 break;
2130
2131         case VIDIOCCAPTURE:
2132         {
2133                 int *on = arg, res;
2134
2135                 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2136                         ZR_DEVNAME(zr), *on);
2137
2138                 mutex_lock(&zr->resource_lock);
2139                 res = setup_overlay(file, *on);
2140                 mutex_unlock(&zr->resource_lock);
2141
2142                 return res;
2143         }
2144                 break;
2145
2146         case VIDIOCGWIN:
2147         {
2148                 struct video_window *vwin = arg;
2149
2150                 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2151
2152                 memset(vwin, 0, sizeof(struct video_window));
2153                 mutex_lock(&zr->resource_lock);
2154                 vwin->x = fh->overlay_settings.x;
2155                 vwin->y = fh->overlay_settings.y;
2156                 vwin->width = fh->overlay_settings.width;
2157                 vwin->height = fh->overlay_settings.height;
2158                 mutex_unlock(&zr->resource_lock);
2159                 vwin->clipcount = 0;
2160                 return 0;
2161         }
2162                 break;
2163
2164         case VIDIOCSWIN:
2165         {
2166                 struct video_window *vwin = arg;
2167                 int res;
2168
2169                 dprintk(3,
2170                         KERN_DEBUG
2171                         "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2172                         ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2173                         vwin->height, vwin->clipcount);
2174
2175                 mutex_lock(&zr->resource_lock);
2176                 res =
2177                     setup_window(file, vwin->x, vwin->y, vwin->width,
2178                                  vwin->height, vwin->clips,
2179                                  vwin->clipcount, NULL);
2180                 mutex_unlock(&zr->resource_lock);
2181
2182                 return res;
2183         }
2184                 break;
2185
2186         case VIDIOCGFBUF:
2187         {
2188                 struct video_buffer *vbuf = arg;
2189
2190                 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2191
2192                 mutex_lock(&zr->resource_lock);
2193                 *vbuf = zr->buffer;
2194                 mutex_unlock(&zr->resource_lock);
2195                 return 0;
2196         }
2197                 break;
2198
2199         case VIDIOCSFBUF:
2200         {
2201                 struct video_buffer *vbuf = arg;
2202                 int i, res = 0;
2203
2204                 dprintk(3,
2205                         KERN_DEBUG
2206                         "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2207                         ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2208                         vbuf->height, vbuf->depth, vbuf->bytesperline);
2209
2210                 for (i = 0; i < NUM_FORMATS; i++)
2211                         if (zoran_formats[i].depth == vbuf->depth)
2212                                 break;
2213                 if (i == NUM_FORMATS) {
2214                         dprintk(1,
2215                                 KERN_ERR
2216                                 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2217                                 ZR_DEVNAME(zr), vbuf->depth);
2218                         return -EINVAL;
2219                 }
2220
2221                 mutex_lock(&zr->resource_lock);
2222                 res =
2223                     setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2224                                   vbuf->width, vbuf->height,
2225                                   vbuf->bytesperline);
2226                 mutex_unlock(&zr->resource_lock);
2227
2228                 return res;
2229         }
2230                 break;
2231
2232         case VIDIOCSYNC:
2233         {
2234                 int *frame = arg, res;
2235
2236                 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2237                         ZR_DEVNAME(zr), *frame);
2238
2239                 mutex_lock(&zr->resource_lock);
2240                 res = v4l_sync(file, *frame);
2241                 mutex_unlock(&zr->resource_lock);
2242                 if (!res)
2243                         zr->v4l_sync_tail++;
2244                 return res;
2245         }
2246                 break;
2247
2248         case VIDIOCMCAPTURE:
2249         {
2250                 struct video_mmap *vmap = arg;
2251                 int res;
2252
2253                 dprintk(3,
2254                         KERN_DEBUG
2255                         "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2256                         ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2257                         vmap->format);
2258
2259                 mutex_lock(&zr->resource_lock);
2260                 res = v4l_grab(file, vmap);
2261                 mutex_unlock(&zr->resource_lock);
2262                 return res;
2263         }
2264                 break;
2265
2266         case VIDIOCGMBUF:
2267         {
2268                 struct video_mbuf *vmbuf = arg;
2269                 int i, res = 0;
2270
2271                 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2272
2273                 vmbuf->size =
2274                     fh->v4l_buffers.num_buffers *
2275                     fh->v4l_buffers.buffer_size;
2276                 vmbuf->frames = fh->v4l_buffers.num_buffers;
2277                 for (i = 0; i < vmbuf->frames; i++) {
2278                         vmbuf->offsets[i] =
2279                             i * fh->v4l_buffers.buffer_size;
2280                 }
2281
2282                 mutex_lock(&zr->resource_lock);
2283
2284                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2285                         dprintk(1,
2286                                 KERN_ERR
2287                                 "%s: VIDIOCGMBUF - buffers already allocated\n",
2288                                 ZR_DEVNAME(zr));
2289                         res = -EINVAL;
2290                         goto v4l1reqbuf_unlock_and_return;
2291                 }
2292
2293                 if (v4l_fbuffer_alloc(file)) {
2294                         res = -ENOMEM;
2295                         goto v4l1reqbuf_unlock_and_return;
2296                 }
2297
2298                 /* The next mmap will map the V4L buffers */
2299                 fh->map_mode = ZORAN_MAP_MODE_RAW;
2300         v4l1reqbuf_unlock_and_return:
2301                 mutex_unlock(&zr->resource_lock);
2302
2303                 return res;
2304         }
2305                 break;
2306
2307         case VIDIOCGUNIT:
2308         {
2309                 struct video_unit *vunit = arg;
2310
2311                 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2312
2313                 vunit->video = zr->video_dev->minor;
2314                 vunit->vbi = VIDEO_NO_UNIT;
2315                 vunit->radio = VIDEO_NO_UNIT;
2316                 vunit->audio = VIDEO_NO_UNIT;
2317                 vunit->teletext = VIDEO_NO_UNIT;
2318
2319                 return 0;
2320         }
2321                 break;
2322
2323                 /*
2324                  * RJ: In principal we could support subcaptures for V4L grabbing.
2325                  *     Not even the famous BTTV driver has them, however.
2326                  *     If there should be a strong demand, one could consider
2327                  *     to implement them.
2328                  */
2329         case VIDIOCGCAPTURE:
2330         {
2331                 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2332                         ZR_DEVNAME(zr));
2333                 return -EINVAL;
2334         }
2335                 break;
2336
2337         case VIDIOCSCAPTURE:
2338         {
2339                 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2340                         ZR_DEVNAME(zr));
2341                 return -EINVAL;
2342         }
2343                 break;
2344
2345         case BUZIOC_G_PARAMS:
2346         {
2347                 struct zoran_params *bparams = arg;
2348
2349                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2350
2351                 memset(bparams, 0, sizeof(struct zoran_params));
2352                 bparams->major_version = MAJOR_VERSION;
2353                 bparams->minor_version = MINOR_VERSION;
2354
2355                 mutex_lock(&zr->resource_lock);
2356
2357                 bparams->norm = zr->norm;
2358                 bparams->input = zr->input;
2359
2360                 bparams->decimation = fh->jpg_settings.decimation;
2361                 bparams->HorDcm = fh->jpg_settings.HorDcm;
2362                 bparams->VerDcm = fh->jpg_settings.VerDcm;
2363                 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2364                 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2365                 bparams->img_x = fh->jpg_settings.img_x;
2366                 bparams->img_y = fh->jpg_settings.img_y;
2367                 bparams->img_width = fh->jpg_settings.img_width;
2368                 bparams->img_height = fh->jpg_settings.img_height;
2369                 bparams->odd_even = fh->jpg_settings.odd_even;
2370
2371                 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2372                 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2373                 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2374                 memcpy(bparams->APP_data,
2375                        fh->jpg_settings.jpg_comp.APP_data,
2376                        sizeof(bparams->APP_data));
2377                 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2378                 memcpy(bparams->COM_data,
2379                        fh->jpg_settings.jpg_comp.COM_data,
2380                        sizeof(bparams->COM_data));
2381                 bparams->jpeg_markers =
2382                     fh->jpg_settings.jpg_comp.jpeg_markers;
2383
2384                 mutex_unlock(&zr->resource_lock);
2385
2386                 bparams->VFIFO_FB = 0;
2387
2388                 return 0;
2389         }
2390                 break;
2391
2392         case BUZIOC_S_PARAMS:
2393         {
2394                 struct zoran_params *bparams = arg;
2395                 int res = 0;
2396
2397                 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2398
2399                 settings.decimation = bparams->decimation;
2400                 settings.HorDcm = bparams->HorDcm;
2401                 settings.VerDcm = bparams->VerDcm;
2402                 settings.TmpDcm = bparams->TmpDcm;
2403                 settings.field_per_buff = bparams->field_per_buff;
2404                 settings.img_x = bparams->img_x;
2405                 settings.img_y = bparams->img_y;
2406                 settings.img_width = bparams->img_width;
2407                 settings.img_height = bparams->img_height;
2408                 settings.odd_even = bparams->odd_even;
2409
2410                 settings.jpg_comp.quality = bparams->quality;
2411                 settings.jpg_comp.APPn = bparams->APPn;
2412                 settings.jpg_comp.APP_len = bparams->APP_len;
2413                 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2414                        sizeof(bparams->APP_data));
2415                 settings.jpg_comp.COM_len = bparams->COM_len;
2416                 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2417                        sizeof(bparams->COM_data));
2418                 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2419
2420                 mutex_lock(&zr->resource_lock);
2421
2422                 if (zr->codec_mode != BUZ_MODE_IDLE) {
2423                         dprintk(1,
2424                                 KERN_ERR
2425                                 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2426                                 ZR_DEVNAME(zr));
2427                         res = -EINVAL;
2428                         goto sparams_unlock_and_return;
2429                 }
2430
2431                 /* Check the params first before overwriting our
2432                  * nternal values */
2433                 if (zoran_check_jpg_settings(zr, &settings)) {
2434                         res = -EINVAL;
2435                         goto sparams_unlock_and_return;
2436                 }
2437
2438                 fh->jpg_settings = settings;
2439         sparams_unlock_and_return:
2440                 mutex_unlock(&zr->resource_lock);
2441
2442                 return res;
2443         }
2444                 break;
2445
2446         case BUZIOC_REQBUFS:
2447         {
2448                 struct zoran_requestbuffers *breq = arg;
2449                 int res = 0;
2450
2451                 dprintk(3,
2452                         KERN_DEBUG
2453                         "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2454                         ZR_DEVNAME(zr), breq->count, breq->size);
2455
2456                 /* Enforce reasonable lower and upper limits */
2457                 if (breq->count < 4)
2458                         breq->count = 4;        /* Could be choosen smaller */
2459                 if (breq->count > jpg_nbufs)
2460                         breq->count = jpg_nbufs;
2461                 breq->size = PAGE_ALIGN(breq->size);
2462                 if (breq->size < 8192)
2463                         breq->size = 8192;      /* Arbitrary */
2464                 /* breq->size is limited by 1 page for the stat_com
2465                  * tables to a Maximum of 2 MB */
2466                 if (breq->size > jpg_bufsize)
2467                         breq->size = jpg_bufsize;
2468                 if (fh->jpg_buffers.need_contiguous &&
2469                     breq->size > MAX_KMALLOC_MEM)
2470                         breq->size = MAX_KMALLOC_MEM;
2471
2472                 mutex_lock(&zr->resource_lock);
2473
2474                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2475                         dprintk(1,
2476                                 KERN_ERR
2477                                 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2478                                 ZR_DEVNAME(zr));
2479                         res = -EBUSY;
2480                         goto jpgreqbuf_unlock_and_return;
2481                 }
2482
2483                 fh->jpg_buffers.num_buffers = breq->count;
2484                 fh->jpg_buffers.buffer_size = breq->size;
2485
2486                 if (jpg_fbuffer_alloc(file)) {
2487                         res = -ENOMEM;
2488                         goto jpgreqbuf_unlock_and_return;
2489                 }
2490
2491                 /* The next mmap will map the MJPEG buffers - could
2492                  * also be *_PLAY, but it doesn't matter here */
2493                 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2494         jpgreqbuf_unlock_and_return:
2495                 mutex_unlock(&zr->resource_lock);
2496
2497                 return res;
2498         }
2499                 break;
2500
2501         case BUZIOC_QBUF_CAPT:
2502         {
2503                 int *frame = arg, res;
2504
2505                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2506                         ZR_DEVNAME(zr), *frame);
2507
2508                 mutex_lock(&zr->resource_lock);
2509                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2510                 mutex_unlock(&zr->resource_lock);
2511
2512                 return res;
2513         }
2514                 break;
2515
2516         case BUZIOC_QBUF_PLAY:
2517         {
2518                 int *frame = arg, res;
2519
2520                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2521                         ZR_DEVNAME(zr), *frame);
2522
2523                 mutex_lock(&zr->resource_lock);
2524                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2525                 mutex_unlock(&zr->resource_lock);
2526
2527                 return res;
2528         }
2529                 break;
2530
2531         case BUZIOC_SYNC:
2532         {
2533                 struct zoran_sync *bsync = arg;
2534                 int res;
2535
2536                 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2537
2538                 mutex_lock(&zr->resource_lock);
2539                 res = jpg_sync(file, bsync);
2540                 mutex_unlock(&zr->resource_lock);
2541
2542                 return res;
2543         }
2544                 break;
2545
2546         case BUZIOC_G_STATUS:
2547         {
2548                 struct zoran_status *bstat = arg;
2549                 int norm, input, status, res = 0;
2550
2551                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2552
2553                 if (zr->codec_mode != BUZ_MODE_IDLE) {
2554                         dprintk(1,
2555                                 KERN_ERR
2556                                 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2557                                 ZR_DEVNAME(zr));
2558                         return -EINVAL;
2559                 }
2560
2561                 input = zr->card.input[bstat->input].muxsel;
2562                 norm = VIDEO_MODE_AUTO;
2563
2564                 mutex_lock(&zr->resource_lock);
2565
2566                 if (zr->codec_mode != BUZ_MODE_IDLE) {
2567                         dprintk(1,
2568                                 KERN_ERR
2569                                 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2570                                 ZR_DEVNAME(zr));
2571                         res = -EINVAL;
2572                         goto gstat_unlock_and_return;
2573                 }
2574
2575                 decoder_command(zr, DECODER_SET_INPUT, &input);
2576                 decoder_command(zr, DECODER_SET_NORM, &norm);
2577
2578                 /* sleep 1 second */
2579                 ssleep(1);
2580
2581                 /* Get status of video decoder */
2582                 decoder_command(zr, DECODER_GET_STATUS, &status);
2583
2584                 /* restore previous input and norm */
2585                 input = zr->card.input[zr->input].muxsel;
2586                 decoder_command(zr, DECODER_SET_INPUT, &input);
2587                 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2588         gstat_unlock_and_return:
2589                 mutex_unlock(&zr->resource_lock);
2590
2591                 if (!res) {
2592                         bstat->signal =
2593                             (status & DECODER_STATUS_GOOD) ? 1 : 0;
2594                         if (status & DECODER_STATUS_NTSC)
2595                                 bstat->norm = VIDEO_MODE_NTSC;
2596                         else if (status & DECODER_STATUS_SECAM)
2597                                 bstat->norm = VIDEO_MODE_SECAM;
2598                         else
2599                                 bstat->norm = VIDEO_MODE_PAL;
2600
2601                         bstat->color =
2602                             (status & DECODER_STATUS_COLOR) ? 1 : 0;
2603                 }
2604
2605                 return res;
2606         }
2607                 break;
2608
2609                 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2610                  * it allows for integrating the JPEG capturing calls inside standard v4l2
2611                  */
2612
2613         case VIDIOC_QUERYCAP:
2614         {
2615                 struct v4l2_capability *cap = arg;
2616
2617                 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2618
2619                 memset(cap, 0, sizeof(*cap));
2620                 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2621                 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2622                 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2623                          pci_name(zr->pci_dev));
2624                 cap->version =
2625                     KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2626                                    RELEASE_VERSION);
2627                 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2628
2629                 return 0;
2630         }
2631                 break;
2632
2633         case VIDIOC_ENUM_FMT:
2634         {
2635                 struct v4l2_fmtdesc *fmt = arg;
2636                 int index = fmt->index, num = -1, i, flag = 0, type =
2637                     fmt->type;
2638
2639                 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2640                         ZR_DEVNAME(zr), fmt->index);
2641
2642                 switch (fmt->type) {
2643                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2644                         flag = ZORAN_FORMAT_CAPTURE;
2645                         break;
2646                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2647                         flag = ZORAN_FORMAT_PLAYBACK;
2648                         break;
2649                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2650                         flag = ZORAN_FORMAT_OVERLAY;
2651                         break;
2652                 default:
2653                         dprintk(1,
2654                                 KERN_ERR
2655                                 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2656                                 ZR_DEVNAME(zr), fmt->type);
2657                         return -EINVAL;
2658                 }
2659
2660                 for (i = 0; i < NUM_FORMATS; i++) {
2661                         if (zoran_formats[i].flags & flag)
2662                                 num++;
2663                         if (num == fmt->index)
2664                                 break;
2665                 }
2666                 if (fmt->index < 0 /* late, but not too late */  ||
2667                     i == NUM_FORMATS)
2668                         return -EINVAL;
2669
2670                 memset(fmt, 0, sizeof(*fmt));
2671                 fmt->index = index;
2672                 fmt->type = type;
2673                 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2674                 fmt->pixelformat = zoran_formats[i].fourcc;
2675                 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2676                         fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2677
2678                 return 0;
2679         }
2680                 break;
2681
2682         case VIDIOC_G_FMT:
2683         {
2684                 struct v4l2_format *fmt = arg;
2685                 int type = fmt->type;
2686
2687                 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2688
2689                 memset(fmt, 0, sizeof(*fmt));
2690                 fmt->type = type;
2691
2692                 switch (fmt->type) {
2693                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2694
2695                         mutex_lock(&zr->resource_lock);
2696
2697                         fmt->fmt.win.w.left = fh->overlay_settings.x;
2698                         fmt->fmt.win.w.top = fh->overlay_settings.y;
2699                         fmt->fmt.win.w.width = fh->overlay_settings.width;
2700                         fmt->fmt.win.w.height =
2701                             fh->overlay_settings.height;
2702                         if (fh->overlay_settings.width * 2 >
2703                             BUZ_MAX_HEIGHT)
2704                                 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2705                         else
2706                                 fmt->fmt.win.field = V4L2_FIELD_TOP;
2707
2708                         mutex_unlock(&zr->resource_lock);
2709
2710                         break;
2711
2712                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2713                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2714
2715                         mutex_lock(&zr->resource_lock);
2716
2717                         if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2718                             fh->map_mode == ZORAN_MAP_MODE_RAW) {
2719
2720                                 fmt->fmt.pix.width =
2721                                     fh->v4l_settings.width;
2722                                 fmt->fmt.pix.height =
2723                                     fh->v4l_settings.height;
2724                                 fmt->fmt.pix.sizeimage =
2725                                     fh->v4l_settings.bytesperline *
2726                                     fh->v4l_settings.height;
2727                                 fmt->fmt.pix.pixelformat =
2728                                     fh->v4l_settings.format->fourcc;
2729                                 fmt->fmt.pix.colorspace =
2730                                     fh->v4l_settings.format->colorspace;
2731                                 fmt->fmt.pix.bytesperline =
2732                                     fh->v4l_settings.bytesperline;
2733                                 if (BUZ_MAX_HEIGHT <
2734                                     (fh->v4l_settings.height * 2))
2735                                         fmt->fmt.pix.field =
2736                                             V4L2_FIELD_INTERLACED;
2737                                 else
2738                                         fmt->fmt.pix.field =
2739                                             V4L2_FIELD_TOP;
2740
2741                         } else {
2742
2743                                 fmt->fmt.pix.width =
2744                                     fh->jpg_settings.img_width /
2745                                     fh->jpg_settings.HorDcm;
2746                                 fmt->fmt.pix.height =
2747                                     fh->jpg_settings.img_height /
2748                                     (fh->jpg_settings.VerDcm *
2749                                      fh->jpg_settings.TmpDcm);
2750                                 fmt->fmt.pix.sizeimage =
2751                                     zoran_v4l2_calc_bufsize(&fh->
2752                                                             jpg_settings);
2753                                 fmt->fmt.pix.pixelformat =
2754                                     V4L2_PIX_FMT_MJPEG;
2755                                 if (fh->jpg_settings.TmpDcm == 1)
2756                                         fmt->fmt.pix.field =
2757                                             (fh->jpg_settings.
2758                                              odd_even ? V4L2_FIELD_SEQ_BT :
2759                                              V4L2_FIELD_SEQ_BT);
2760                                 else
2761                                         fmt->fmt.pix.field =
2762                                             (fh->jpg_settings.
2763                                              odd_even ? V4L2_FIELD_TOP :
2764                                              V4L2_FIELD_BOTTOM);
2765
2766                                 fmt->fmt.pix.bytesperline = 0;
2767                                 fmt->fmt.pix.colorspace =
2768                                     V4L2_COLORSPACE_SMPTE170M;
2769                         }
2770
2771                         mutex_unlock(&zr->resource_lock);
2772
2773                         break;
2774
2775                 default:
2776                         dprintk(1,
2777                                 KERN_ERR
2778                                 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2779                                 ZR_DEVNAME(zr), fmt->type);
2780                         return -EINVAL;
2781                 }
2782                 return 0;
2783         }
2784                 break;
2785
2786         case VIDIOC_S_FMT:
2787         {
2788                 struct v4l2_format *fmt = arg;
2789                 int i, res = 0;
2790                 __le32 printformat;
2791
2792                 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2793                         ZR_DEVNAME(zr), fmt->type);
2794
2795                 switch (fmt->type) {
2796                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2797
2798                         dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2799                                 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2800                                 fmt->fmt.win.w.width,
2801                                 fmt->fmt.win.w.height,
2802                                 fmt->fmt.win.clipcount,
2803                                 fmt->fmt.win.bitmap);
2804                         mutex_lock(&zr->resource_lock);
2805                         res =
2806                             setup_window(file, fmt->fmt.win.w.left,
2807                                          fmt->fmt.win.w.top,
2808                                          fmt->fmt.win.w.width,
2809                                          fmt->fmt.win.w.height,
2810                                          (struct video_clip __user *)
2811                                            fmt->fmt.win.clips,
2812                                          fmt->fmt.win.clipcount,
2813                                          fmt->fmt.win.bitmap);
2814                         mutex_unlock(&zr->resource_lock);
2815                         return res;
2816                         break;
2817
2818                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2819                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2820
2821                         printformat =
2822                             __cpu_to_le32(fmt->fmt.pix.pixelformat);
2823                         dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2824                                 fmt->fmt.pix.width, fmt->fmt.pix.height,
2825                                 fmt->fmt.pix.pixelformat,
2826                                 (char *) &printformat);
2827
2828                         /* we can be requested to do JPEG/raw playback/capture */
2829                         if (!
2830                             (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2831                              (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2832                               fmt->fmt.pix.pixelformat ==
2833                               V4L2_PIX_FMT_MJPEG))) {
2834                                 dprintk(1,
2835                                         KERN_ERR
2836                                         "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2837                                         ZR_DEVNAME(zr), fmt->type,
2838                                         fmt->fmt.pix.pixelformat,
2839                                         (char *) &printformat);
2840                                 return -EINVAL;
2841                         }
2842
2843                         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2844                                 mutex_lock(&zr->resource_lock);
2845
2846                                 settings = fh->jpg_settings;
2847
2848                                 if (fh->v4l_buffers.allocated ||
2849                                     fh->jpg_buffers.allocated) {
2850                                         dprintk(1,
2851                                                 KERN_ERR
2852                                                 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2853                                                 ZR_DEVNAME(zr));
2854                                         res = -EBUSY;
2855                                         goto sfmtjpg_unlock_and_return;
2856                                 }
2857
2858                                 /* we actually need to set 'real' parameters now */
2859                                 if ((fmt->fmt.pix.height * 2) >
2860                                     BUZ_MAX_HEIGHT)
2861                                         settings.TmpDcm = 1;
2862                                 else
2863                                         settings.TmpDcm = 2;
2864                                 settings.decimation = 0;
2865                                 if (fmt->fmt.pix.height <=
2866                                     fh->jpg_settings.img_height / 2)
2867                                         settings.VerDcm = 2;
2868                                 else
2869                                         settings.VerDcm = 1;
2870                                 if (fmt->fmt.pix.width <=
2871                                     fh->jpg_settings.img_width / 4)
2872                                         settings.HorDcm = 4;
2873                                 else if (fmt->fmt.pix.width <=
2874                                          fh->jpg_settings.img_width / 2)
2875                                         settings.HorDcm = 2;
2876                                 else
2877                                         settings.HorDcm = 1;
2878                                 if (settings.TmpDcm == 1)
2879                                         settings.field_per_buff = 2;
2880                                 else
2881                                         settings.field_per_buff = 1;
2882
2883                                 /* check */
2884                                 if ((res =
2885                                      zoran_check_jpg_settings(zr,
2886                                                               &settings)))
2887                                         goto sfmtjpg_unlock_and_return;
2888
2889                                 /* it's ok, so set them */
2890                                 fh->jpg_settings = settings;
2891
2892                                 /* tell the user what we actually did */
2893                                 fmt->fmt.pix.width =
2894                                     settings.img_width / settings.HorDcm;
2895                                 fmt->fmt.pix.height =
2896                                     settings.img_height * 2 /
2897                                     (settings.TmpDcm * settings.VerDcm);
2898                                 if (settings.TmpDcm == 1)
2899                                         fmt->fmt.pix.field =
2900                                             (fh->jpg_settings.
2901                                              odd_even ? V4L2_FIELD_SEQ_TB :
2902                                              V4L2_FIELD_SEQ_BT);
2903                                 else
2904                                         fmt->fmt.pix.field =
2905                                             (fh->jpg_settings.
2906                                              odd_even ? V4L2_FIELD_TOP :
2907                                              V4L2_FIELD_BOTTOM);
2908                                 fh->jpg_buffers.buffer_size =
2909                                     zoran_v4l2_calc_bufsize(&fh->
2910                                                             jpg_settings);
2911                                 fmt->fmt.pix.bytesperline = 0;
2912                                 fmt->fmt.pix.sizeimage =
2913                                     fh->jpg_buffers.buffer_size;
2914                                 fmt->fmt.pix.colorspace =
2915                                     V4L2_COLORSPACE_SMPTE170M;
2916
2917                                 /* we hereby abuse this variable to show that
2918                                  * we're gonna do mjpeg capture */
2919                                 fh->map_mode =
2920                                     (fmt->type ==
2921                                      V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2922                                     ZORAN_MAP_MODE_JPG_REC :
2923                                     ZORAN_MAP_MODE_JPG_PLAY;
2924                         sfmtjpg_unlock_and_return:
2925                                 mutex_unlock(&zr->resource_lock);
2926                         } else {
2927                                 for (i = 0; i < NUM_FORMATS; i++)
2928                                         if (fmt->fmt.pix.pixelformat ==
2929                                             zoran_formats[i].fourcc)
2930                                                 break;
2931                                 if (i == NUM_FORMATS) {
2932                                         dprintk(1,
2933                                                 KERN_ERR
2934                                                 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2935                                                 ZR_DEVNAME(zr),
2936                                                 fmt->fmt.pix.pixelformat,
2937                                                 (char *) &printformat);
2938                                         return -EINVAL;
2939                                 }
2940                                 mutex_lock(&zr->resource_lock);
2941                                 if (fh->jpg_buffers.allocated ||
2942                                     (fh->v4l_buffers.allocated &&
2943                                      fh->v4l_buffers.active !=
2944                                      ZORAN_FREE)) {
2945                                         dprintk(1,
2946                                                 KERN_ERR
2947                                                 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2948                                                 ZR_DEVNAME(zr));
2949                                         res = -EBUSY;
2950                                         goto sfmtv4l_unlock_and_return;
2951                                 }
2952                                 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2953                                         fmt->fmt.pix.height =
2954                                             BUZ_MAX_HEIGHT;
2955                                 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2956                                         fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2957
2958                                 if ((res =
2959                                      zoran_v4l_set_format(file,
2960                                                           fmt->fmt.pix.
2961                                                           width,
2962                                                           fmt->fmt.pix.
2963                                                           height,
2964                                                           &zoran_formats
2965                                                           [i])))
2966                                         goto sfmtv4l_unlock_and_return;
2967
2968                                 /* tell the user the
2969                                  * results/missing stuff */
2970                                 fmt->fmt.pix.bytesperline =
2971                                         fh->v4l_settings.bytesperline;
2972                                 fmt->fmt.pix.sizeimage =
2973                                         fh->v4l_settings.height *
2974                                         fh->v4l_settings.bytesperline;
2975                                 fmt->fmt.pix.colorspace =
2976                                         fh->v4l_settings.format->colorspace;
2977                                 if (BUZ_MAX_HEIGHT <
2978                                     (fh->v4l_settings.height * 2))
2979                                         fmt->fmt.pix.field =
2980                                             V4L2_FIELD_INTERLACED;
2981                                 else
2982                                         fmt->fmt.pix.field =
2983                                             V4L2_FIELD_TOP;
2984
2985                                 fh->map_mode = ZORAN_MAP_MODE_RAW;
2986                         sfmtv4l_unlock_and_return:
2987                                 mutex_unlock(&zr->resource_lock);
2988                         }
2989
2990                         break;
2991
2992                 default:
2993                         dprintk(1,
2994                                 KERN_ERR
2995                                 "%s: VIDIOC_S_FMT - unsupported type %d\n",
2996                                 ZR_DEVNAME(zr), fmt->type);
2997                         return -EINVAL;
2998                 }
2999
3000                 return res;
3001         }
3002                 break;
3003
3004         case VIDIOC_G_FBUF:
3005         {
3006                 struct v4l2_framebuffer *fb = arg;
3007
3008                 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3009
3010                 memset(fb, 0, sizeof(*fb));
3011                 mutex_lock(&zr->resource_lock);
3012                 fb->base = zr->buffer.base;
3013                 fb->fmt.width = zr->buffer.width;
3014                 fb->fmt.height = zr->buffer.height;
3015                 if (zr->overlay_settings.format) {
3016                         fb->fmt.pixelformat =
3017                                 fh->overlay_settings.format->fourcc;
3018                 }
3019                 fb->fmt.bytesperline = zr->buffer.bytesperline;
3020                 mutex_unlock(&zr->resource_lock);
3021                 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3022                 fb->fmt.field = V4L2_FIELD_INTERLACED;
3023                 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3024                 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3025
3026                 return 0;
3027         }
3028                 break;
3029
3030         case VIDIOC_S_FBUF:
3031         {
3032                 int i, res = 0;
3033                 struct v4l2_framebuffer *fb = arg;
3034                 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3035
3036                 dprintk(3,
3037                         KERN_DEBUG
3038                         "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3039                         ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3040                         fb->fmt.bytesperline, fb->fmt.pixelformat,
3041                         (char *) &printformat);
3042
3043                 for (i = 0; i < NUM_FORMATS; i++)
3044                         if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3045                                 break;
3046                 if (i == NUM_FORMATS) {
3047                         dprintk(1,
3048                                 KERN_ERR
3049                                 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3050                                 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3051                                 (char *) &printformat);
3052                         return -EINVAL;
3053                 }
3054
3055                 mutex_lock(&zr->resource_lock);
3056                 res =
3057                     setup_fbuffer(file, fb->base, &zoran_formats[i],
3058                                   fb->fmt.width, fb->fmt.height,
3059                                   fb->fmt.bytesperline);
3060                 mutex_unlock(&zr->resource_lock);
3061
3062                 return res;
3063         }
3064                 break;
3065
3066         case VIDIOC_OVERLAY:
3067         {
3068                 int *on = arg, res;
3069
3070                 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3071                         ZR_DEVNAME(zr), *on);
3072
3073                 mutex_lock(&zr->resource_lock);
3074                 res = setup_overlay(file, *on);
3075                 mutex_unlock(&zr->resource_lock);
3076
3077                 return res;
3078         }
3079                 break;
3080
3081         case VIDIOC_REQBUFS:
3082         {
3083                 struct v4l2_requestbuffers *req = arg;
3084                 int res = 0;
3085
3086                 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3087                         ZR_DEVNAME(zr), req->type);
3088
3089                 if (req->memory != V4L2_MEMORY_MMAP) {
3090                         dprintk(1,
3091                                 KERN_ERR
3092                                 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3093                                 ZR_DEVNAME(zr), req->memory);
3094                         return -EINVAL;
3095                 }
3096
3097                 mutex_lock(&zr->resource_lock);
3098
3099                 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3100                         dprintk(1,
3101                                 KERN_ERR
3102                                 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3103                                 ZR_DEVNAME(zr));
3104                         res = -EBUSY;
3105                         goto v4l2reqbuf_unlock_and_return;
3106                 }
3107
3108                 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3109                     req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3110
3111                         /* control user input */
3112                         if (req->count < 2)
3113                                 req->count = 2;
3114                         if (req->count > v4l_nbufs)
3115                                 req->count = v4l_nbufs;
3116                         fh->v4l_buffers.num_buffers = req->count;
3117
3118                         if (v4l_fbuffer_alloc(file)) {
3119                                 res = -ENOMEM;
3120                                 goto v4l2reqbuf_unlock_and_return;
3121                         }
3122
3123                         /* The next mmap will map the V4L buffers */
3124                         fh->map_mode = ZORAN_MAP_MODE_RAW;
3125
3126                 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3127                            fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3128
3129                         /* we need to calculate size ourselves now */
3130                         if (req->count < 4)
3131                                 req->count = 4;
3132                         if (req->count > jpg_nbufs)
3133                                 req->count = jpg_nbufs;
3134                         fh->jpg_buffers.num_buffers = req->count;
3135                         fh->jpg_buffers.buffer_size =
3136                             zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3137
3138                         if (jpg_fbuffer_alloc(file)) {
3139                                 res = -ENOMEM;
3140                                 goto v4l2reqbuf_unlock_and_return;
3141                         }
3142
3143                         /* The next mmap will map the MJPEG buffers */
3144                         if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3145                                 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3146                         else
3147                                 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3148
3149                 } else {
3150                         dprintk(1,
3151                                 KERN_ERR
3152                                 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3153                                 ZR_DEVNAME(zr), req->type);
3154                         res = -EINVAL;
3155                         goto v4l2reqbuf_unlock_and_return;
3156                 }
3157         v4l2reqbuf_unlock_and_return:
3158                 mutex_unlock(&zr->resource_lock);
3159
3160                 return 0;
3161         }
3162                 break;
3163
3164         case VIDIOC_QUERYBUF:
3165         {
3166                 struct v4l2_buffer *buf = arg;
3167                 __u32 type = buf->type;
3168                 int index = buf->index, res;
3169
3170                 dprintk(3,
3171                         KERN_DEBUG
3172                         "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3173                         ZR_DEVNAME(zr), buf->index, buf->type);
3174
3175                 memset(buf, 0, sizeof(*buf));
3176                 buf->type = type;
3177                 buf->index = index;
3178
3179                 mutex_lock(&zr->resource_lock);
3180                 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3181                 mutex_unlock(&zr->resource_lock);
3182
3183                 return res;
3184         }
3185                 break;
3186
3187         case VIDIOC_QBUF:
3188         {
3189                 struct v4l2_buffer *buf = arg;
3190                 int res = 0, codec_mode, buf_type;
3191
3192                 dprintk(3,
3193                         KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3194                         ZR_DEVNAME(zr), buf->type, buf->index);
3195
3196                 mutex_lock(&zr->resource_lock);
3197
3198                 switch (fh->map_mode) {
3199                 case ZORAN_MAP_MODE_RAW:
3200                         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3201                                 dprintk(1,
3202                                         KERN_ERR
3203                                         "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3204                                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
3205                                 res = -EINVAL;
3206                                 goto qbuf_unlock_and_return;
3207                         }
3208
3209                         res = zoran_v4l_queue_frame(file, buf->index);
3210                         if (res)
3211                                 goto qbuf_unlock_and_return;
3212                         if (!zr->v4l_memgrab_active &&
3213                             fh->v4l_buffers.active == ZORAN_LOCKED)
3214                                 zr36057_set_memgrab(zr, 1);
3215                         break;
3216
3217                 case ZORAN_MAP_MODE_JPG_REC:
3218                 case ZORAN_MAP_MODE_JPG_PLAY:
3219                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3220                                 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3221                                 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3222                         } else {
3223                                 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3224                                 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3225                         }
3226
3227                         if (buf->type != buf_type) {
3228                                 dprintk(1,
3229                                         KERN_ERR
3230                                         "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3231                                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
3232                                 res = -EINVAL;
3233                                 goto qbuf_unlock_and_return;
3234                         }
3235
3236                         res =
3237                             zoran_jpg_queue_frame(file, buf->index,
3238                                                   codec_mode);
3239                         if (res != 0)
3240                                 goto qbuf_unlock_and_return;
3241                         if (zr->codec_mode == BUZ_MODE_IDLE &&
3242                             fh->jpg_buffers.active == ZORAN_LOCKED) {
3243                                 zr36057_enable_jpg(zr, codec_mode);
3244                         }
3245                         break;
3246
3247                 default:
3248                         dprintk(1,
3249                                 KERN_ERR
3250                                 "%s: VIDIOC_QBUF - unsupported type %d\n",
3251                                 ZR_DEVNAME(zr), buf->type);
3252                         res = -EINVAL;
3253                         goto qbuf_unlock_and_return;
3254                 }
3255         qbuf_unlock_and_return:
3256                 mutex_unlock(&zr->resource_lock);
3257
3258                 return res;
3259         }
3260                 break;
3261
3262         case VIDIOC_DQBUF:
3263         {
3264                 struct v4l2_buffer *buf = arg;
3265                 int res = 0, buf_type, num = -1;        /* compiler borks here (?) */
3266
3267                 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3268                         ZR_DEVNAME(zr), buf->type);
3269
3270                 mutex_lock(&zr->resource_lock);
3271
3272                 switch (fh->map_mode) {
3273                 case ZORAN_MAP_MODE_RAW:
3274                         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3275                                 dprintk(1,
3276                                         KERN_ERR
3277                                         "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3278                                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
3279                                 res = -EINVAL;
3280                                 goto dqbuf_unlock_and_return;
3281                         }
3282
3283                         num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3284                         if (file->f_flags & O_NONBLOCK &&
3285                             zr->v4l_buffers.buffer[num].state !=
3286                             BUZ_STATE_DONE) {
3287                                 res = -EAGAIN;
3288                                 goto dqbuf_unlock_and_return;
3289                         }
3290                         res = v4l_sync(file, num);
3291                         if (res)
3292                                 goto dqbuf_unlock_and_return;
3293                         else
3294                                 zr->v4l_sync_tail++;
3295                         res = zoran_v4l2_buffer_status(file, buf, num);
3296                         break;
3297
3298                 case ZORAN_MAP_MODE_JPG_REC:
3299                 case ZORAN_MAP_MODE_JPG_PLAY:
3300                 {
3301                         struct zoran_sync bs;
3302
3303                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3304                                 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3305                         else
3306                                 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3307
3308                         if (buf->type != buf_type) {
3309                                 dprintk(1,
3310                                         KERN_ERR
3311                                         "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3312                                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
3313                                 res = -EINVAL;
3314                                 goto dqbuf_unlock_and_return;
3315                         }
3316
3317                         num =
3318                             zr->jpg_pend[zr->
3319                                          jpg_que_tail & BUZ_MASK_FRAME];
3320
3321                         if (file->f_flags & O_NONBLOCK &&
3322                             zr->jpg_buffers.buffer[num].state !=
3323                             BUZ_STATE_DONE) {
3324                                 res = -EAGAIN;
3325                                 goto dqbuf_unlock_and_return;
3326                         }
3327                         res = jpg_sync(file, &bs);
3328                         if (res)
3329                                 goto dqbuf_unlock_and_return;
3330                         res =
3331                             zoran_v4l2_buffer_status(file, buf, bs.frame);
3332                         break;
3333                 }
3334
3335                 default:
3336                         dprintk(1,
3337                                 KERN_ERR
3338                                 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3339                                 ZR_DEVNAME(zr), buf->type);
3340                         res = -EINVAL;
3341                         goto dqbuf_unlock_and_return;
3342                 }
3343         dqbuf_unlock_and_return:
3344                 mutex_unlock(&zr->resource_lock);
3345
3346                 return res;
3347         }
3348                 break;
3349
3350         case VIDIOC_STREAMON:
3351         {
3352                 int res = 0;
3353
3354                 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3355
3356                 mutex_lock(&zr->resource_lock);
3357
3358                 switch (fh->map_mode) {
3359                 case ZORAN_MAP_MODE_RAW:        /* raw capture */
3360                         if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3361                             fh->v4l_buffers.active != ZORAN_ACTIVE) {
3362                                 res = -EBUSY;
3363                                 goto strmon_unlock_and_return;
3364                         }
3365
3366                         zr->v4l_buffers.active = fh->v4l_buffers.active =
3367                             ZORAN_LOCKED;
3368                         zr->v4l_settings = fh->v4l_settings;
3369
3370                         zr->v4l_sync_tail = zr->v4l_pend_tail;
3371                         if (!zr->v4l_memgrab_active &&
3372                             zr->v4l_pend_head != zr->v4l_pend_tail) {
3373                                 zr36057_set_memgrab(zr, 1);
3374                         }
3375                         break;
3376
3377                 case ZORAN_MAP_MODE_JPG_REC:
3378                 case ZORAN_MAP_MODE_JPG_PLAY:
3379                         /* what is the codec mode right now? */
3380                         if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3381                             fh->jpg_buffers.active != ZORAN_ACTIVE) {
3382                                 res = -EBUSY;
3383                                 goto strmon_unlock_and_return;
3384                         }
3385
3386                         zr->jpg_buffers.active = fh->jpg_buffers.active =
3387                             ZORAN_LOCKED;
3388
3389                         if (zr->jpg_que_head != zr->jpg_que_tail) {
3390                                 /* Start the jpeg codec when the first frame is queued  */
3391                                 jpeg_start(zr);
3392                         }
3393
3394                         break;
3395                 default:
3396                         dprintk(1,
3397                                 KERN_ERR
3398                                 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3399                                 ZR_DEVNAME(zr), fh->map_mode);
3400                         res = -EINVAL;
3401                         goto strmon_unlock_and_return;
3402                 }
3403         strmon_unlock_and_return:
3404                 mutex_unlock(&zr->resource_lock);
3405
3406                 return res;
3407         }
3408                 break;
3409
3410         case VIDIOC_STREAMOFF:
3411         {
3412                 int i, res = 0;
3413
3414                 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3415
3416                 mutex_lock(&zr->resource_lock);
3417
3418                 switch (fh->map_mode) {
3419                 case ZORAN_MAP_MODE_RAW:        /* raw capture */
3420                         if (fh->v4l_buffers.active == ZORAN_FREE &&
3421                             zr->v4l_buffers.active != ZORAN_FREE) {
3422                                 res = -EPERM;   /* stay off other's settings! */
3423                                 goto strmoff_unlock_and_return;
3424                         }
3425                         if (zr->v4l_buffers.active == ZORAN_FREE)
3426                                 goto strmoff_unlock_and_return;
3427
3428                         /* unload capture */
3429                         if (zr->v4l_memgrab_active) {
3430                                 unsigned long flags;
3431
3432                                 spin_lock_irqsave(&zr->spinlock, flags);
3433                                 zr36057_set_memgrab(zr, 0);
3434                                 spin_unlock_irqrestore(&zr->spinlock, flags);
3435                         }
3436
3437                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3438                                 zr->v4l_buffers.buffer[i].state =
3439                                     BUZ_STATE_USER;
3440                         fh->v4l_buffers = zr->v4l_buffers;
3441
3442                         zr->v4l_buffers.active = fh->v4l_buffers.active =
3443                             ZORAN_FREE;
3444
3445                         zr->v4l_grab_seq = 0;
3446                         zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3447                         zr->v4l_sync_tail = 0;
3448
3449                         break;
3450
3451                 case ZORAN_MAP_MODE_JPG_REC:
3452                 case ZORAN_MAP_MODE_JPG_PLAY:
3453                         if (fh->jpg_buffers.active == ZORAN_FREE &&
3454                             zr->jpg_buffers.active != ZORAN_FREE) {
3455                                 res = -EPERM;   /* stay off other's settings! */
3456                                 goto strmoff_unlock_and_return;
3457                         }
3458                         if (zr->jpg_buffers.active == ZORAN_FREE)
3459                                 goto strmoff_unlock_and_return;
3460
3461                         res =
3462                             jpg_qbuf(file, -1,
3463                                      (fh->map_mode ==
3464                                       ZORAN_MAP_MODE_JPG_REC) ?
3465                                      BUZ_MODE_MOTION_COMPRESS :
3466                                      BUZ_MODE_MOTION_DECOMPRESS);
3467                         if (res)
3468                                 goto strmoff_unlock_and_return;
3469                         break;
3470                 default:
3471                         dprintk(1,
3472                                 KERN_ERR
3473                                 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3474                                 ZR_DEVNAME(zr), fh->map_mode);
3475                         res = -EINVAL;
3476                         goto strmoff_unlock_and_return;
3477                 }
3478         strmoff_unlock_and_return:
3479                 mutex_unlock(&zr->resource_lock);
3480
3481                 return res;
3482         }
3483                 break;
3484
3485         case VIDIOC_QUERYCTRL:
3486         {
3487                 struct v4l2_queryctrl *ctrl = arg;
3488
3489                 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3490                         ZR_DEVNAME(zr), ctrl->id);
3491
3492                 /* we only support hue/saturation/contrast/brightness */
3493                 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3494                     ctrl->id > V4L2_CID_HUE)
3495                         return -EINVAL;
3496                 else {
3497                         int id = ctrl->id;
3498                         memset(ctrl, 0, sizeof(*ctrl));
3499                         ctrl->id = id;
3500                 }
3501
3502                 switch (ctrl->id) {
3503                 case V4L2_CID_BRIGHTNESS:
3504                         strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3505                         break;
3506                 case V4L2_CID_CONTRAST:
3507                         strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3508                         break;
3509                 case V4L2_CID_SATURATION:
3510                         strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3511                         break;
3512                 case V4L2_CID_HUE:
3513                         strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3514                         break;
3515                 }
3516
3517                 ctrl->minimum = 0;
3518                 ctrl->maximum = 65535;
3519                 ctrl->step = 1;
3520                 ctrl->default_value = 32768;
3521                 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3522
3523                 return 0;
3524         }
3525                 break;
3526
3527         case VIDIOC_G_CTRL:
3528         {
3529                 struct v4l2_control *ctrl = arg;
3530
3531                 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3532                         ZR_DEVNAME(zr), ctrl->id);
3533
3534                 /* we only support hue/saturation/contrast/brightness */
3535                 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3536                     ctrl->id > V4L2_CID_HUE)
3537                         return -EINVAL;
3538
3539                 mutex_lock(&zr->resource_lock);
3540                 switch (ctrl->id) {
3541                 case V4L2_CID_BRIGHTNESS:
3542                         ctrl->value = zr->brightness;
3543                         break;
3544                 case V4L2_CID_CONTRAST:
3545                         ctrl->value = zr->contrast;
3546                         break;
3547                 case V4L2_CID_SATURATION:
3548                         ctrl->value = zr->saturation;
3549                         break;
3550                 case V4L2_CID_HUE:
3551                         ctrl->value = zr->hue;
3552                         break;
3553                 }
3554                 mutex_unlock(&zr->resource_lock);
3555
3556                 return 0;
3557         }
3558                 break;
3559
3560         case VIDIOC_S_CTRL:
3561         {
3562                 struct v4l2_control *ctrl = arg;
3563                 struct video_picture pict;
3564
3565                 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3566                         ZR_DEVNAME(zr), ctrl->id);
3567
3568                 /* we only support hue/saturation/contrast/brightness */
3569                 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3570                     ctrl->id > V4L2_CID_HUE)
3571                         return -EINVAL;
3572
3573                 if (ctrl->value < 0 || ctrl->value > 65535) {
3574                         dprintk(1,
3575                                 KERN_ERR
3576                                 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3577                                 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3578                         return -EINVAL;
3579                 }
3580
3581                 mutex_lock(&zr->resource_lock);
3582                 switch (ctrl->id) {
3583                 case V4L2_CID_BRIGHTNESS:
3584                         zr->brightness = ctrl->value;
3585                         break;
3586                 case V4L2_CID_CONTRAST:
3587                         zr->contrast = ctrl->value;
3588                         break;
3589                 case V4L2_CID_SATURATION:
3590                         zr->saturation = ctrl->value;
3591                         break;
3592                 case V4L2_CID_HUE:
3593                         zr->hue = ctrl->value;
3594                         break;
3595                 }
3596                 pict.brightness = zr->brightness;
3597                 pict.contrast = zr->contrast;
3598                 pict.colour = zr->saturation;
3599                 pict.hue = zr->hue;
3600
3601                 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3602
3603                 mutex_unlock(&zr->resource_lock);
3604
3605                 return 0;
3606         }
3607                 break;
3608
3609         case VIDIOC_ENUMSTD:
3610         {
3611                 struct v4l2_standard *std = arg;
3612
3613                 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3614                         ZR_DEVNAME(zr), std->index);
3615
3616                 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3617                         return -EINVAL;
3618                 else {
3619                         int id = std->index;
3620                         memset(std, 0, sizeof(*std));
3621                         std->index = id;
3622                 }
3623
3624                 if (std->index == zr->card.norms) {
3625                         /* if we have autodetect, ... */
3626                         struct video_decoder_capability caps;
3627                         decoder_command(zr, DECODER_GET_CAPABILITIES,
3628                                         &caps);
3629                         if (caps.flags & VIDEO_DECODER_AUTO) {
3630                                 std->id = V4L2_STD_ALL;
3631                                 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3632                                 return 0;
3633                         } else
3634                                 return -EINVAL;
3635                 }
3636                 switch (std->index) {
3637                 case 0:
3638                         std->id = V4L2_STD_PAL;
3639                         strncpy(std->name, "PAL", sizeof(std->name)-1);
3640                         std->frameperiod.numerator = 1;
3641                         std->frameperiod.denominator = 25;
3642                         std->framelines = zr->card.tvn[0]->Ht;
3643                         break;
3644                 case 1:
3645                         std->id = V4L2_STD_NTSC;
3646                         strncpy(std->name, "NTSC", sizeof(std->name)-1);
3647                         std->frameperiod.numerator = 1001;
3648                         std->frameperiod.denominator = 30000;
3649                         std->framelines = zr->card.tvn[1]->Ht;
3650                         break;
3651                 case 2:
3652                         std->id = V4L2_STD_SECAM;
3653                         strncpy(std->name, "SECAM", sizeof(std->name)-1);
3654                         std->frameperiod.numerator = 1;
3655                         std->frameperiod.denominator = 25;
3656                         std->framelines = zr->card.tvn[2]->Ht;
3657                         break;
3658                 }
3659
3660                 return 0;
3661         }
3662                 break;
3663
3664         case VIDIOC_G_STD:
3665         {
3666                 v4l2_std_id *std = arg;
3667                 int norm;
3668
3669                 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3670
3671                 mutex_lock(&zr->resource_lock);
3672                 norm = zr->norm;
3673                 mutex_unlock(&zr->resource_lock);
3674
3675                 switch (norm) {
3676                 case VIDEO_MODE_PAL:
3677                         *std = V4L2_STD_PAL;
3678                         break;
3679                 case VIDEO_MODE_NTSC:
3680                         *std = V4L2_STD_NTSC;
3681                         break;
3682                 case VIDEO_MODE_SECAM:
3683                         *std = V4L2_STD_SECAM;
3684                         break;
3685                 }
3686
3687                 return 0;
3688         }
3689                 break;
3690
3691         case VIDIOC_S_STD:
3692         {
3693                 int norm = -1, res = 0;
3694                 v4l2_std_id *std = arg;
3695
3696                 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3697                         ZR_DEVNAME(zr), (unsigned long long)*std);
3698
3699                 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3700                         norm = VIDEO_MODE_PAL;
3701                 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3702                         norm = VIDEO_MODE_NTSC;
3703                 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3704                         norm = VIDEO_MODE_SECAM;
3705                 else if (*std == V4L2_STD_ALL)
3706                         norm = VIDEO_MODE_AUTO;
3707                 else {
3708                         dprintk(1,
3709                                 KERN_ERR
3710                                 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3711                                 ZR_DEVNAME(zr), (unsigned long long)*std);
3712                         return -EINVAL;
3713                 }
3714
3715                 mutex_lock(&zr->resource_lock);
3716                 if ((res = zoran_set_norm(zr, norm)))
3717                         goto sstd_unlock_and_return;
3718
3719                 res = wait_grab_pending(zr);
3720         sstd_unlock_and_return:
3721                 mutex_unlock(&zr->resource_lock);
3722                 return res;
3723         }
3724                 break;
3725
3726         case VIDIOC_ENUMINPUT:
3727         {
3728                 struct v4l2_input *inp = arg;
3729                 int status;
3730
3731                 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3732                         ZR_DEVNAME(zr), inp->index);
3733
3734                 if (inp->index < 0 || inp->index >= zr->card.inputs)
3735                         return -EINVAL;
3736                 else {
3737                         int id = inp->index;
3738                         memset(inp, 0, sizeof(*inp));
3739                         inp->index = id;
3740                 }
3741
3742                 strncpy(inp->name, zr->card.input[inp->index].name,
3743                         sizeof(inp->name) - 1);
3744                 inp->type = V4L2_INPUT_TYPE_CAMERA;
3745                 inp->std = V4L2_STD_ALL;
3746
3747                 /* Get status of video decoder */
3748                 mutex_lock(&zr->resource_lock);
3749                 decoder_command(zr, DECODER_GET_STATUS, &status);
3750                 mutex_unlock(&zr->resource_lock);
3751
3752                 if (!(status & DECODER_STATUS_GOOD)) {
3753                         inp->status |= V4L2_IN_ST_NO_POWER;
3754                         inp->status |= V4L2_IN_ST_NO_SIGNAL;
3755                 }
3756                 if (!(status & DECODER_STATUS_COLOR))
3757                         inp->status |= V4L2_IN_ST_NO_COLOR;
3758
3759                 return 0;
3760         }
3761                 break;
3762
3763         case VIDIOC_G_INPUT:
3764         {
3765                 int *input = arg;
3766
3767                 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3768
3769                 mutex_lock(&zr->resource_lock);
3770                 *input = zr->input;
3771                 mutex_unlock(&zr->resource_lock);
3772
3773                 return 0;
3774         }
3775                 break;
3776
3777         case VIDIOC_S_INPUT:
3778         {
3779                 int *input = arg, res = 0;
3780
3781                 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3782                         ZR_DEVNAME(zr), *input);
3783
3784                 mutex_lock(&zr->resource_lock);
3785                 if ((res = zoran_set_input(zr, *input)))
3786                         goto sinput_unlock_and_return;
3787
3788                 /* Make sure the changes come into effect */
3789                 res = wait_grab_pending(zr);
3790         sinput_unlock_and_return:
3791                 mutex_unlock(&zr->resource_lock);
3792                 return res;
3793         }
3794                 break;
3795
3796         case VIDIOC_ENUMOUTPUT:
3797         {
3798                 struct v4l2_output *outp = arg;
3799
3800                 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3801                         ZR_DEVNAME(zr), outp->index);
3802
3803                 if (outp->index != 0)
3804                         return -EINVAL;
3805
3806                 memset(outp, 0, sizeof(*outp));
3807                 outp->index = 0;
3808                 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3809                 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3810
3811                 return 0;
3812         }
3813                 break;
3814
3815         case VIDIOC_G_OUTPUT:
3816         {
3817                 int *output = arg;
3818
3819                 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3820
3821                 *output = 0;
3822
3823                 return 0;
3824         }
3825                 break;
3826
3827         case VIDIOC_S_OUTPUT:
3828         {
3829                 int *output = arg;
3830
3831                 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3832                         ZR_DEVNAME(zr), *output);
3833
3834                 if (*output != 0)
3835                         return -EINVAL;
3836
3837                 return 0;
3838         }
3839                 break;
3840
3841                 /* cropping (sub-frame capture) */
3842         case VIDIOC_CROPCAP:
3843         {
3844                 struct v4l2_cropcap *cropcap = arg;
3845                 int type = cropcap->type, res = 0;
3846
3847                 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3848                         ZR_DEVNAME(zr), cropcap->type);
3849
3850                 memset(cropcap, 0, sizeof(*cropcap));
3851                 cropcap->type = type;
3852
3853                 mutex_lock(&zr->resource_lock);
3854
3855                 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3856                     (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3857                      fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3858                         dprintk(1,
3859                                 KERN_ERR
3860                                 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3861                                 ZR_DEVNAME(zr));
3862                         res = -EINVAL;
3863                         goto cropcap_unlock_and_return;
3864                 }
3865
3866                 cropcap->bounds.top = cropcap->bounds.left = 0;
3867                 cropcap->bounds.width = BUZ_MAX_WIDTH;
3868                 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3869                 cropcap->defrect.top = cropcap->defrect.left = 0;
3870                 cropcap->defrect.width = BUZ_MIN_WIDTH;
3871                 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3872         cropcap_unlock_and_return:
3873                 mutex_unlock(&zr->resource_lock);
3874                 return res;
3875         }
3876                 break;
3877
3878         case VIDIOC_G_CROP:
3879         {
3880                 struct v4l2_crop *crop = arg;
3881                 int type = crop->type, res = 0;
3882
3883                 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3884                         ZR_DEVNAME(zr), crop->type);
3885
3886                 memset(crop, 0, sizeof(*crop));
3887                 crop->type = type;
3888
3889                 mutex_lock(&zr->resource_lock);
3890
3891                 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3892                     (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3893                      fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3894                         dprintk(1,
3895                                 KERN_ERR
3896                                 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3897                                 ZR_DEVNAME(zr));
3898                         res = -EINVAL;
3899                         goto gcrop_unlock_and_return;
3900                 }
3901
3902                 crop->c.top = fh->jpg_settings.img_y;
3903                 crop->c.left = fh->jpg_settings.img_x;
3904                 crop->c.width = fh->jpg_settings.img_width;
3905                 crop->c.height = fh->jpg_settings.img_height;
3906
3907         gcrop_unlock_and_return:
3908                 mutex_unlock(&zr->resource_lock);
3909
3910                 return res;
3911         }
3912                 break;
3913
3914         case VIDIOC_S_CROP:
3915         {
3916                 struct v4l2_crop *crop = arg;
3917                 int res = 0;
3918
3919                 settings = fh->jpg_settings;
3920
3921                 dprintk(3,
3922                         KERN_ERR
3923                         "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3924                         ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3925                         crop->c.width, crop->c.height);
3926
3927                 mutex_lock(&zr->resource_lock);
3928
3929                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3930                         dprintk(1,
3931                                 KERN_ERR
3932                                 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3933                                 ZR_DEVNAME(zr));
3934                         res = -EBUSY;
3935                         goto scrop_unlock_and_return;
3936                 }
3937
3938                 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3939                     (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3940                      fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3941                         dprintk(1,
3942                                 KERN_ERR
3943                                 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3944                                 ZR_DEVNAME(zr));
3945                         res = -EINVAL;
3946                         goto scrop_unlock_and_return;
3947                 }
3948
3949                 /* move into a form that we understand */
3950                 settings.img_x = crop->c.left;
3951                 settings.img_y = crop->c.top;
3952                 settings.img_width = crop->c.width;
3953                 settings.img_height = crop->c.height;
3954
3955                 /* check validity */
3956                 if ((res = zoran_check_jpg_settings(zr, &settings)))
3957                         goto scrop_unlock_and_return;
3958
3959                 /* accept */
3960                 fh->jpg_settings = settings;
3961
3962         scrop_unlock_and_return:
3963                 mutex_unlock(&zr->resource_lock);
3964                 return res;
3965         }
3966                 break;
3967
3968         case VIDIOC_G_JPEGCOMP:
3969         {
3970                 struct v4l2_jpegcompression *params = arg;
3971
3972                 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
3973                         ZR_DEVNAME(zr));
3974
3975                 memset(params, 0, sizeof(*params));
3976
3977                 mutex_lock(&zr->resource_lock);
3978
3979                 params->quality = fh->jpg_settings.jpg_comp.quality;
3980                 params->APPn = fh->jpg_settings.jpg_comp.APPn;
3981                 memcpy(params->APP_data,
3982                        fh->jpg_settings.jpg_comp.APP_data,
3983                        fh->jpg_settings.jpg_comp.APP_len);
3984                 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3985                 memcpy(params->COM_data,
3986                        fh->jpg_settings.jpg_comp.COM_data,
3987                        fh->jpg_settings.jpg_comp.COM_len);
3988                 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3989                 params->jpeg_markers =
3990                     fh->jpg_settings.jpg_comp.jpeg_markers;
3991
3992                 mutex_unlock(&zr->resource_lock);
3993
3994                 return 0;
3995         }
3996                 break;
3997
3998         case VIDIOC_S_JPEGCOMP:
3999         {
4000                 struct v4l2_jpegcompression *params = arg;
4001                 int res = 0;
4002
4003                 settings = fh->jpg_settings;
4004
4005                 dprintk(3,
4006                         KERN_DEBUG
4007                         "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4008                         ZR_DEVNAME(zr), params->quality, params->APPn,
4009                         params->APP_len, params->COM_len);
4010
4011                 settings.jpg_comp = *params;
4012
4013                 mutex_lock(&zr->resource_lock);
4014
4015                 if (fh->v4l_buffers.active != ZORAN_FREE ||
4016                     fh->jpg_buffers.active != ZORAN_FREE) {
4017                         dprintk(1,
4018                                 KERN_WARNING
4019                                 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4020                                 ZR_DEVNAME(zr));
4021                         res = -EBUSY;
4022                         goto sjpegc_unlock_and_return;
4023                 }
4024
4025                 if ((res = zoran_check_jpg_settings(zr, &settings)))
4026                         goto sjpegc_unlock_and_return;
4027                 if (!fh->jpg_buffers.allocated)
4028                         fh->jpg_buffers.buffer_size =
4029                             zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4030                 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4031         sjpegc_unlock_and_return:
4032                 mutex_unlock(&zr->resource_lock);
4033
4034                 return 0;
4035         }
4036                 break;
4037
4038         case VIDIOC_QUERYSTD:   /* why is this useful? */
4039         {
4040                 v4l2_std_id *std = arg;
4041
4042                 dprintk(3,
4043                         KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4044                         ZR_DEVNAME(zr), (unsigned long long)*std);
4045
4046                 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4047                     *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4048                                              zr->card.norms == 3)) {
4049                         return 0;
4050                 }
4051
4052                 return -EINVAL;
4053         }
4054                 break;
4055
4056         case VIDIOC_TRY_FMT:
4057         {
4058                 struct v4l2_format *fmt = arg;
4059                 int res = 0;
4060
4061                 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4062                         ZR_DEVNAME(zr), fmt->type);
4063
4064                 switch (fmt->type) {
4065                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4066                         mutex_lock(&zr->resource_lock);
4067
4068                         if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4069                                 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4070                         if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4071                                 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4072                         if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4073                                 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4074                         if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4075                                 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4076
4077                         mutex_unlock(&zr->resource_lock);
4078                         break;
4079
4080                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4081                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4082                         if (fmt->fmt.pix.bytesperline > 0)
4083                                 return -EINVAL;
4084
4085                         mutex_lock(&zr->resource_lock);
4086
4087                         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4088                                 settings = fh->jpg_settings;
4089
4090                                 /* we actually need to set 'real' parameters now */
4091                                 if ((fmt->fmt.pix.height * 2) >
4092                                     BUZ_MAX_HEIGHT)
4093                                         settings.TmpDcm = 1;
4094                                 else
4095                                         settings.TmpDcm = 2;
4096                                 settings.decimation = 0;
4097                                 if (fmt->fmt.pix.height <=
4098                                     fh->jpg_settings.img_height / 2)
4099                                         settings.VerDcm = 2;
4100                                 else
4101                                         settings.VerDcm = 1;
4102                                 if (fmt->fmt.pix.width <=
4103                                     fh->jpg_settings.img_width / 4)
4104                                         settings.HorDcm = 4;
4105                                 else if (fmt->fmt.pix.width <=
4106                                          fh->jpg_settings.img_width / 2)
4107                                         settings.HorDcm = 2;
4108                                 else
4109                                         settings.HorDcm = 1;
4110                                 if (settings.TmpDcm == 1)
4111                                         settings.field_per_buff = 2;
4112                                 else
4113                                         settings.field_per_buff = 1;
4114
4115                                 /* check */
4116                                 if ((res =
4117                                      zoran_check_jpg_settings(zr,
4118                                                               &settings)))
4119                                         goto tryfmt_unlock_and_return;
4120
4121                                 /* tell the user what we actually did */
4122                                 fmt->fmt.pix.width =
4123                                     settings.img_width / settings.HorDcm;
4124                                 fmt->fmt.pix.height =
4125                                     settings.img_height * 2 /
4126                                     (settings.TmpDcm * settings.VerDcm);
4127                                 if (settings.TmpDcm == 1)
4128                                         fmt->fmt.pix.field =
4129                                             (fh->jpg_settings.
4130                                              odd_even ? V4L2_FIELD_SEQ_TB :
4131                                              V4L2_FIELD_SEQ_BT);
4132                                 else
4133                                         fmt->fmt.pix.field =
4134                                             (fh->jpg_settings.
4135                                              odd_even ? V4L2_FIELD_TOP :
4136                                              V4L2_FIELD_BOTTOM);
4137
4138                                 fmt->fmt.pix.sizeimage =
4139                                     zoran_v4l2_calc_bufsize(&settings);
4140                         } else if (fmt->type ==
4141                                    V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4142                                 int i;
4143
4144                                 for (i = 0; i < NUM_FORMATS; i++)
4145                                         if (zoran_formats[i].fourcc ==
4146                                             fmt->fmt.pix.pixelformat)
4147                                                 break;
4148                                 if (i == NUM_FORMATS) {
4149                                         res = -EINVAL;
4150                                         goto tryfmt_unlock_and_return;
4151                                 }
4152
4153                                 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4154                                         fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4155                                 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4156                                         fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4157                                 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4158                                         fmt->fmt.pix.height =
4159                                             BUZ_MAX_HEIGHT;
4160                                 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4161                                         fmt->fmt.pix.height =
4162                                             BUZ_MIN_HEIGHT;
4163                         } else {
4164                                 res = -EINVAL;
4165                                 goto tryfmt_unlock_and_return;
4166                         }
4167                 tryfmt_unlock_and_return:
4168                         mutex_unlock(&zr->resource_lock);
4169
4170                         return res;
4171                         break;
4172
4173                 default:
4174                         return -EINVAL;
4175                 }
4176
4177                 return 0;
4178         }
4179                 break;
4180
4181         default:
4182                 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4183                         ZR_DEVNAME(zr), cmd);
4184                 return -ENOIOCTLCMD;
4185                 break;
4186
4187         }
4188         return 0;
4189 }
4190
4191
4192 static long
4193 zoran_ioctl(struct file  *file,
4194             unsigned int  cmd,
4195             unsigned long arg)
4196 {
4197         return video_usercopy(file, cmd, arg, zoran_do_ioctl);
4198 }
4199
4200 static unsigned int
4201 zoran_poll (struct file *file,
4202             poll_table  *wait)
4203 {
4204         struct zoran_fh *fh = file->private_data;
4205         struct zoran *zr = fh->zr;
4206         int res = 0, frame;
4207         unsigned long flags;
4208
4209         /* we should check whether buffers are ready to be synced on
4210          * (w/o waits - O_NONBLOCK) here
4211          * if ready for read (sync), return POLLIN|POLLRDNORM,
4212          * if ready for write (sync), return POLLOUT|POLLWRNORM,
4213          * if error, return POLLERR,
4214          * if no buffers queued or so, return POLLNVAL
4215          */
4216
4217         mutex_lock(&zr->resource_lock);
4218
4219         switch (fh->map_mode) {
4220         case ZORAN_MAP_MODE_RAW:
4221                 poll_wait(file, &zr->v4l_capq, wait);
4222                 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4223
4224                 spin_lock_irqsave(&zr->spinlock, flags);
4225                 dprintk(3,
4226                         KERN_DEBUG
4227                         "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4228                         ZR_DEVNAME(zr), __func__,
4229                         "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4230                         "UPMD"[zr->v4l_buffers.buffer[frame].state],
4231                         zr->v4l_pend_tail, zr->v4l_pend_head);
4232                 /* Process is the one capturing? */
4233                 if (fh->v4l_buffers.active != ZORAN_FREE &&
4234                     /* Buffer ready to DQBUF? */
4235                     zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4236                         res = POLLIN | POLLRDNORM;
4237                 spin_unlock_irqrestore(&zr->spinlock, flags);
4238
4239                 break;
4240
4241         case ZORAN_MAP_MODE_JPG_REC:
4242         case ZORAN_MAP_MODE_JPG_PLAY:
4243                 poll_wait(file, &zr->jpg_capq, wait);
4244                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4245
4246                 spin_lock_irqsave(&zr->spinlock, flags);
4247                 dprintk(3,
4248                         KERN_DEBUG
4249                         "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4250                         ZR_DEVNAME(zr), __func__,
4251                         "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4252                         "UPMD"[zr->jpg_buffers.buffer[frame].state],
4253                         zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4254                 if (fh->jpg_buffers.active != ZORAN_FREE &&
4255                     zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4256                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4257                                 res = POLLIN | POLLRDNORM;
4258                         else
4259                                 res = POLLOUT | POLLWRNORM;
4260                 }
4261                 spin_unlock_irqrestore(&zr->spinlock, flags);
4262
4263                 break;
4264
4265         default:
4266                 dprintk(1,
4267                         KERN_ERR
4268                         "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4269                         ZR_DEVNAME(zr), fh->map_mode);
4270                 res = POLLNVAL;
4271         }
4272
4273         mutex_unlock(&zr->resource_lock);
4274
4275         return res;
4276 }
4277
4278
4279 /*
4280  * This maps the buffers to user space.
4281  *
4282  * Depending on the state of fh->map_mode
4283  * the V4L or the MJPEG buffers are mapped
4284  * per buffer or all together
4285  *
4286  * Note that we need to connect to some
4287  * unmap signal event to unmap the de-allocate
4288  * the buffer accordingly (zoran_vm_close())
4289  */
4290
4291 static void
4292 zoran_vm_open (struct vm_area_struct *vma)
4293 {
4294         struct zoran_mapping *map = vma->vm_private_data;
4295
4296         map->count++;
4297 }
4298
4299 static void
4300 zoran_vm_close (struct vm_area_struct *vma)
4301 {
4302         struct zoran_mapping *map = vma->vm_private_data;
4303         struct file *file = map->file;
4304         struct zoran_fh *fh = file->private_data;
4305         struct zoran *zr = fh->zr;
4306         int i;
4307
4308         map->count--;
4309         if (map->count == 0) {
4310                 switch (fh->map_mode) {
4311                 case ZORAN_MAP_MODE_JPG_REC:
4312                 case ZORAN_MAP_MODE_JPG_PLAY:
4313
4314                         dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4315                                 ZR_DEVNAME(zr));
4316
4317                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4318                                 if (fh->jpg_buffers.buffer[i].map == map) {
4319                                         fh->jpg_buffers.buffer[i].map =
4320                                             NULL;
4321                                 }
4322                         }
4323                         kfree(map);
4324
4325                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4326                                 if (fh->jpg_buffers.buffer[i].map)
4327                                         break;
4328                         if (i == fh->jpg_buffers.num_buffers) {
4329                                 mutex_lock(&zr->resource_lock);
4330
4331                                 if (fh->jpg_buffers.active != ZORAN_FREE) {
4332                                         jpg_qbuf(file, -1, zr->codec_mode);
4333                                         zr->jpg_buffers.allocated = 0;
4334                                         zr->jpg_buffers.active =
4335                                             fh->jpg_buffers.active =
4336                                             ZORAN_FREE;
4337                                 }
4338                                 //jpg_fbuffer_free(file);
4339                                 fh->jpg_buffers.allocated = 0;
4340                                 fh->jpg_buffers.ready_to_be_freed = 1;
4341
4342                                 mutex_unlock(&zr->resource_lock);
4343                         }
4344
4345                         break;
4346
4347                 case ZORAN_MAP_MODE_RAW:
4348
4349                         dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4350                                 ZR_DEVNAME(zr));
4351
4352                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4353                                 if (fh->v4l_buffers.buffer[i].map == map) {
4354                                         /* unqueue/unmap */
4355                                         fh->v4l_buffers.buffer[i].map =
4356                                             NULL;
4357                                 }
4358                         }
4359                         kfree(map);
4360
4361                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4362                                 if (fh->v4l_buffers.buffer[i].map)
4363                                         break;
4364                         if (i == fh->v4l_buffers.num_buffers) {
4365                                 mutex_lock(&zr->resource_lock);
4366
4367                                 if (fh->v4l_buffers.active != ZORAN_FREE) {
4368                                         unsigned long flags;
4369
4370                                         spin_lock_irqsave(&zr->spinlock, flags);
4371                                         zr36057_set_memgrab(zr, 0);
4372                                         zr->v4l_buffers.allocated = 0;
4373                                         zr->v4l_buffers.active =
4374                                             fh->v4l_buffers.active =
4375                                             ZORAN_FREE;
4376                                         spin_unlock_irqrestore(&zr->spinlock, flags);
4377                                 }
4378                                 //v4l_fbuffer_free(file);
4379                                 fh->v4l_buffers.allocated = 0;
4380                                 fh->v4l_buffers.ready_to_be_freed = 1;
4381
4382                                 mutex_unlock(&zr->resource_lock);
4383                         }
4384
4385                         break;
4386
4387                 default:
4388                         printk(KERN_ERR
4389                                "%s: munmap() - internal error - unknown map mode %d\n",
4390                                ZR_DEVNAME(zr), fh->map_mode);
4391                         break;
4392
4393                 }
4394         }
4395 }
4396
4397 static struct vm_operations_struct zoran_vm_ops = {
4398         .open = zoran_vm_open,
4399         .close = zoran_vm_close,
4400 };
4401
4402 static int
4403 zoran_mmap (struct file           *file,
4404             struct vm_area_struct *vma)
4405 {
4406         struct zoran_fh *fh = file->private_data;
4407         struct zoran *zr = fh->zr;
4408         unsigned long size = (vma->vm_end - vma->vm_start);
4409         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4410         int i, j;
4411         unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4412         int first, last;
4413         struct zoran_mapping *map;
4414         int res = 0;
4415
4416         dprintk(3,
4417                 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4418                 ZR_DEVNAME(zr),
4419                 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4420                 vma->vm_start, vma->vm_end, size);
4421
4422         if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4423             !(vma->vm_flags & VM_WRITE)) {
4424                 dprintk(1,
4425                         KERN_ERR
4426                         "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4427                         ZR_DEVNAME(zr));
4428                 return -EINVAL;
4429         }
4430
4431         switch (fh->map_mode) {
4432
4433         case ZORAN_MAP_MODE_JPG_REC:
4434         case ZORAN_MAP_MODE_JPG_PLAY:
4435
4436                 /* lock */
4437                 mutex_lock(&zr->resource_lock);
4438
4439                 /* Map the MJPEG buffers */
4440                 if (!fh->jpg_buffers.allocated) {
4441                         dprintk(1,
4442                                 KERN_ERR
4443                                 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4444                                 ZR_DEVNAME(zr));
4445                         res = -ENOMEM;
4446                         goto jpg_mmap_unlock_and_return;
4447                 }
4448
4449                 first = offset / fh->jpg_buffers.buffer_size;
4450                 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4451                 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4452                     size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4453                     last < 0 || first >= fh->jpg_buffers.num_buffers ||
4454                     last >= fh->jpg_buffers.num_buffers) {
4455                         dprintk(1,
4456                                 KERN_ERR
4457                                 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4458                                 ZR_DEVNAME(zr), offset, size,
4459                                 fh->jpg_buffers.buffer_size,
4460                                 fh->jpg_buffers.num_buffers);
4461                         res = -EINVAL;
4462                         goto jpg_mmap_unlock_and_return;
4463                 }
4464                 for (i = first; i <= last; i++) {
4465                         if (fh->jpg_buffers.buffer[i].map) {
4466                                 dprintk(1,
4467                                         KERN_ERR
4468                                         "%s: mmap(MJPEG) - buffer %d already mapped\n",
4469                                         ZR_DEVNAME(zr), i);
4470                                 res = -EBUSY;
4471                                 goto jpg_mmap_unlock_and_return;
4472                         }
4473                 }
4474
4475                 /* map these buffers (v4l_buffers[i]) */
4476                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4477                 if (!map) {
4478                         res = -ENOMEM;
4479                         goto jpg_mmap_unlock_and_return;
4480                 }
4481                 map->file = file;
4482                 map->count = 1;
4483
4484                 vma->vm_ops = &zoran_vm_ops;
4485                 vma->vm_flags |= VM_DONTEXPAND;
4486                 vma->vm_private_data = map;
4487
4488                 for (i = first; i <= last; i++) {
4489                         for (j = 0;
4490                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4491                              j++) {
4492                                 fraglen =
4493                                     (le32_to_cpu(fh->jpg_buffers.buffer[i].
4494                                      frag_tab[2 * j + 1]) & ~1) << 1;
4495                                 todo = size;
4496                                 if (todo > fraglen)
4497                                         todo = fraglen;
4498                                 pos =
4499                                     le32_to_cpu(fh->jpg_buffers.
4500                                     buffer[i].frag_tab[2 * j]);
4501                                 /* should just be pos on i386 */
4502                                 page = virt_to_phys(bus_to_virt(pos))
4503                                                                 >> PAGE_SHIFT;
4504                                 if (remap_pfn_range(vma, start, page,
4505                                                         todo, PAGE_SHARED)) {
4506                                         dprintk(1,
4507                                                 KERN_ERR
4508                                                 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4509                                                 ZR_DEVNAME(zr));
4510                                         res = -EAGAIN;
4511                                         goto jpg_mmap_unlock_and_return;
4512                                 }
4513                                 size -= todo;
4514                                 start += todo;
4515                                 if (size == 0)
4516                                         break;
4517                                 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4518                                     frag_tab[2 * j + 1]) & 1)
4519                                         break;  /* was last fragment */
4520                         }
4521                         fh->jpg_buffers.buffer[i].map = map;
4522                         if (size == 0)
4523                                 break;
4524
4525                 }
4526         jpg_mmap_unlock_and_return:
4527                 mutex_unlock(&zr->resource_lock);
4528
4529                 break;
4530
4531         case ZORAN_MAP_MODE_RAW:
4532
4533                 mutex_lock(&zr->resource_lock);
4534
4535                 /* Map the V4L buffers */
4536                 if (!fh->v4l_buffers.allocated) {
4537                         dprintk(1,
4538                                 KERN_ERR
4539                                 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4540                                 ZR_DEVNAME(zr));
4541                         res = -ENOMEM;
4542                         goto v4l_mmap_unlock_and_return;
4543                 }
4544
4545                 first = offset / fh->v4l_buffers.buffer_size;
4546                 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4547                 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4548                     size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4549                     last < 0 || first >= fh->v4l_buffers.num_buffers ||
4550                     last >= fh->v4l_buffers.buffer_size) {
4551                         dprintk(1,
4552                                 KERN_ERR
4553                                 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4554                                 ZR_DEVNAME(zr), offset, size,
4555                                 fh->v4l_buffers.buffer_size,
4556                                 fh->v4l_buffers.num_buffers);
4557                         res = -EINVAL;
4558                         goto v4l_mmap_unlock_and_return;
4559                 }
4560                 for (i = first; i <= last; i++) {
4561                         if (fh->v4l_buffers.buffer[i].map) {
4562                                 dprintk(1,
4563                                         KERN_ERR
4564                                         "%s: mmap(V4L) - buffer %d already mapped\n",
4565                                         ZR_DEVNAME(zr), i);
4566                                 res = -EBUSY;
4567                                 goto v4l_mmap_unlock_and_return;
4568                         }
4569                 }
4570
4571                 /* map these buffers (v4l_buffers[i]) */
4572                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4573                 if (!map) {
4574                         res = -ENOMEM;
4575                         goto v4l_mmap_unlock_and_return;
4576                 }
4577                 map->file = file;
4578                 map->count = 1;
4579
4580                 vma->vm_ops = &zoran_vm_ops;
4581                 vma->vm_flags |= VM_DONTEXPAND;
4582                 vma->vm_private_data = map;
4583
4584                 for (i = first; i <= last; i++) {
4585                         todo = size;
4586                         if (todo > fh->v4l_buffers.buffer_size)
4587                                 todo = fh->v4l_buffers.buffer_size;
4588                         page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4589                         if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4590                                                         todo, PAGE_SHARED)) {
4591                                 dprintk(1,
4592                                         KERN_ERR
4593                                         "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4594                                         ZR_DEVNAME(zr));
4595                                 res = -EAGAIN;
4596                                 goto v4l_mmap_unlock_and_return;
4597                         }
4598                         size -= todo;
4599                         start += todo;
4600                         fh->v4l_buffers.buffer[i].map = map;
4601                         if (size == 0)
4602                                 break;
4603                 }
4604         v4l_mmap_unlock_and_return:
4605                 mutex_unlock(&zr->resource_lock);
4606
4607                 break;
4608
4609         default:
4610                 dprintk(1,
4611                         KERN_ERR
4612                         "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4613                         ZR_DEVNAME(zr), fh->map_mode);
4614                 break;
4615         }
4616
4617         return 0;
4618 }
4619
4620 static const struct v4l2_file_operations zoran_fops = {
4621         .owner = THIS_MODULE,
4622         .open = zoran_open,
4623         .release = zoran_close,
4624         .ioctl = zoran_ioctl,
4625         .read = zoran_read,
4626         .write = zoran_write,
4627         .mmap = zoran_mmap,
4628         .poll = zoran_poll,
4629 };
4630
4631 struct video_device zoran_template __devinitdata = {
4632         .name = ZORAN_NAME,
4633         .fops = &zoran_fops,
4634         .release = &zoran_vdev_release,
4635         .minor = -1
4636 };
4637