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