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