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