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