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