]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/zoran/zoran_driver.c
V4L/DVB (10907): avoid loading the entire videodev.h header on V4L2 drivers
[linux-2.6-omap-h63xx.git] / drivers / media / video / zoran / zoran_driver.c
1 /*
2  * Zoran zr36057/zr36067 PCI controller driver, for the
3  * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4  * Media Labs LML33/LML33R10.
5  *
6  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7  *
8  * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9  *
10  * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11  *
12  * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13  *
14  * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15  *
16  * Based on
17  *
18  * Miro DC10 driver
19  * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20  *
21  * Iomega Buz driver version 1.0
22  * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23  *
24  * buz.0.0.3
25  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26  *
27  * bttv - Bt848 frame grabber driver
28  * Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
29  *                        & Marcus Metzler (mocm@thp.uni-koeln.de)
30  *
31  *
32  * This program is free software; you can redistribute it and/or modify
33  * it under the terms of the GNU General Public License as published by
34  * the Free Software Foundation; either version 2 of the License, or
35  * (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this program; if not, write to the Free Software
44  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45  */
46
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
55
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
59
60 #include <linux/spinlock.h>
61
62 #include <linux/videodev.h>
63 #include <media/v4l2-common.h>
64 #include <media/v4l2-ioctl.h>
65 #include "videocodec.h"
66
67 #include <asm/byteorder.h>
68 #include <asm/io.h>
69 #include <asm/uaccess.h>
70 #include <linux/proc_fs.h>
71
72 #include <linux/mutex.h>
73 #include "zoran.h"
74 #include "zoran_device.h"
75 #include "zoran_card.h"
76
77
78 const struct zoran_format zoran_formats[] = {
79         {
80                 .name = "15-bit RGB LE",
81                 .fourcc = V4L2_PIX_FMT_RGB555,
82                 .colorspace = V4L2_COLORSPACE_SRGB,
83                 .depth = 15,
84                 .flags = ZORAN_FORMAT_CAPTURE |
85                          ZORAN_FORMAT_OVERLAY,
86                 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
87                            ZR36057_VFESPFR_LittleEndian,
88         }, {
89                 .name = "15-bit RGB BE",
90                 .fourcc = V4L2_PIX_FMT_RGB555X,
91                 .colorspace = V4L2_COLORSPACE_SRGB,
92                 .depth = 15,
93                 .flags = ZORAN_FORMAT_CAPTURE |
94                          ZORAN_FORMAT_OVERLAY,
95                 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
96         }, {
97                 .name = "16-bit RGB LE",
98                 .fourcc = V4L2_PIX_FMT_RGB565,
99                 .colorspace = V4L2_COLORSPACE_SRGB,
100                 .depth = 16,
101                 .flags = ZORAN_FORMAT_CAPTURE |
102                          ZORAN_FORMAT_OVERLAY,
103                 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
104                            ZR36057_VFESPFR_LittleEndian,
105         }, {
106                 .name = "16-bit RGB BE",
107                 .fourcc = V4L2_PIX_FMT_RGB565X,
108                 .colorspace = V4L2_COLORSPACE_SRGB,
109                 .depth = 16,
110                 .flags = ZORAN_FORMAT_CAPTURE |
111                          ZORAN_FORMAT_OVERLAY,
112                 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
113         }, {
114                 .name = "24-bit RGB",
115                 .fourcc = V4L2_PIX_FMT_BGR24,
116                 .colorspace = V4L2_COLORSPACE_SRGB,
117                 .depth = 24,
118                 .flags = ZORAN_FORMAT_CAPTURE |
119                          ZORAN_FORMAT_OVERLAY,
120                 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
121         }, {
122                 .name = "32-bit RGB LE",
123                 .fourcc = V4L2_PIX_FMT_BGR32,
124                 .colorspace = V4L2_COLORSPACE_SRGB,
125                 .depth = 32,
126                 .flags = ZORAN_FORMAT_CAPTURE |
127                          ZORAN_FORMAT_OVERLAY,
128                 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
129         }, {
130                 .name = "32-bit RGB BE",
131                 .fourcc = V4L2_PIX_FMT_RGB32,
132                 .colorspace = V4L2_COLORSPACE_SRGB,
133                 .depth = 32,
134                 .flags = ZORAN_FORMAT_CAPTURE |
135                          ZORAN_FORMAT_OVERLAY,
136                 .vfespfr = ZR36057_VFESPFR_RGB888,
137         }, {
138                 .name = "4:2:2, packed, YUYV",
139                 .fourcc = V4L2_PIX_FMT_YUYV,
140                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
141                 .depth = 16,
142                 .flags = ZORAN_FORMAT_CAPTURE |
143                          ZORAN_FORMAT_OVERLAY,
144                 .vfespfr = ZR36057_VFESPFR_YUV422,
145         }, {
146                 .name = "4:2:2, packed, UYVY",
147                 .fourcc = V4L2_PIX_FMT_UYVY,
148                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
149                 .depth = 16,
150                 .flags = ZORAN_FORMAT_CAPTURE |
151                          ZORAN_FORMAT_OVERLAY,
152                 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
153         }, {
154                 .name = "Hardware-encoded Motion-JPEG",
155                 .fourcc = V4L2_PIX_FMT_MJPEG,
156                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
157                 .depth = 0,
158                 .flags = ZORAN_FORMAT_CAPTURE |
159                          ZORAN_FORMAT_PLAYBACK |
160                          ZORAN_FORMAT_COMPRESSED,
161         }
162 };
163 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
164
165 static int lock_norm;   /* 0 = default 1 = Don't change TV standard (norm) */
166 module_param(lock_norm, int, 0644);
167 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
168
169         /* small helper function for calculating buffersizes for v4l2
170          * we calculate the nearest higher power-of-two, which
171          * will be the recommended buffersize */
172 static __u32
173 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
174 {
175         __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
176         __u32 num = (1024 * 512) / (div);
177         __u32 result = 2;
178
179         num--;
180         while (num) {
181                 num >>= 1;
182                 result <<= 1;
183         }
184
185         if (result > jpg_bufsize)
186                 return jpg_bufsize;
187         if (result < 8192)
188                 return 8192;
189         return result;
190 }
191
192 /* forward references */
193 static void v4l_fbuffer_free(struct file *file);
194 static void jpg_fbuffer_free(struct file *file);
195
196 /*
197  *   Allocate the V4L grab buffers
198  *
199  *   These have to be pysically contiguous.
200  */
201
202 static int
203 v4l_fbuffer_alloc (struct file *file)
204 {
205         struct zoran_fh *fh = file->private_data;
206         struct zoran *zr = fh->zr;
207         int i, off;
208         unsigned char *mem;
209
210         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
211                 if (fh->v4l_buffers.buffer[i].fbuffer)
212                         dprintk(2,
213                                 KERN_WARNING
214                                 "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
215                                 ZR_DEVNAME(zr), i);
216
217                 //udelay(20);
218                 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
219                 if (!mem) {
220                         dprintk(1,
221                                 KERN_ERR
222                                 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
223                                 ZR_DEVNAME(zr), i);
224                         v4l_fbuffer_free(file);
225                         return -ENOBUFS;
226                 }
227                 fh->v4l_buffers.buffer[i].fbuffer = mem;
228                 fh->v4l_buffers.buffer[i].fbuffer_phys =
229                     virt_to_phys(mem);
230                 fh->v4l_buffers.buffer[i].fbuffer_bus =
231                     virt_to_bus(mem);
232                 for (off = 0; off < fh->v4l_buffers.buffer_size;
233                      off += PAGE_SIZE)
234                         SetPageReserved(virt_to_page(mem + off));
235                 dprintk(4,
236                         KERN_INFO
237                         "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
238                         ZR_DEVNAME(zr), i, (unsigned long) mem,
239                         virt_to_bus(mem));
240         }
241
242         fh->v4l_buffers.allocated = 1;
243
244         return 0;
245 }
246
247 /* free the V4L grab buffers */
248 static void
249 v4l_fbuffer_free (struct file *file)
250 {
251         struct zoran_fh *fh = file->private_data;
252         struct zoran *zr = fh->zr;
253         int i, off;
254         unsigned char *mem;
255
256         dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
257
258         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
259                 if (!fh->v4l_buffers.buffer[i].fbuffer)
260                         continue;
261
262                 mem = fh->v4l_buffers.buffer[i].fbuffer;
263                 for (off = 0; off < fh->v4l_buffers.buffer_size;
264                      off += PAGE_SIZE)
265                         ClearPageReserved(virt_to_page(mem + off));
266                 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
267                 fh->v4l_buffers.buffer[i].fbuffer = NULL;
268         }
269
270         fh->v4l_buffers.allocated = 0;
271 }
272
273 /*
274  *   Allocate the MJPEG grab buffers.
275  *
276  *   If a Natoma chipset is present and this is a revision 1 zr36057,
277  *   each MJPEG buffer needs to be physically contiguous.
278  *   (RJ: This statement is from Dave Perks' original driver,
279  *   I could never check it because I have a zr36067)
280  *
281  *   RJ: The contents grab buffers needs never be accessed in the driver.
282  *       Therefore there is no need to allocate them with vmalloc in order
283  *       to get a contiguous virtual memory space.
284  *       I don't understand why many other drivers first allocate them with
285  *       vmalloc (which uses internally also get_zeroed_page, but delivers you
286  *       virtual addresses) and then again have to make a lot of efforts
287  *       to get the physical address.
288  *
289  *   Ben Capper:
290  *       On big-endian architectures (such as ppc) some extra steps
291  *       are needed. When reading and writing to the stat_com array
292  *       and fragment buffers, the device expects to see little-
293  *       endian values. The use of cpu_to_le32() and le32_to_cpu()
294  *       in this function (and one or two others in zoran_device.c)
295  *       ensure that these values are always stored in little-endian
296  *       form, regardless of architecture. The zr36057 does Very Bad
297  *       Things on big endian architectures if the stat_com array
298  *       and fragment buffers are not little-endian.
299  */
300
301 static int
302 jpg_fbuffer_alloc (struct file *file)
303 {
304         struct zoran_fh *fh = file->private_data;
305         struct zoran *zr = fh->zr;
306         int i, j, off;
307         unsigned long mem;
308
309         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
310                 if (fh->jpg_buffers.buffer[i].frag_tab)
311                         dprintk(2,
312                                 KERN_WARNING
313                                 "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
314                                 ZR_DEVNAME(zr), i);
315
316                 /* Allocate fragment table for this buffer */
317
318                 mem = get_zeroed_page(GFP_KERNEL);
319                 if (mem == 0) {
320                         dprintk(1,
321                                 KERN_ERR
322                                 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
323                                 ZR_DEVNAME(zr), i);
324                         jpg_fbuffer_free(file);
325                         return -ENOBUFS;
326                 }
327                 fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
328                 fh->jpg_buffers.buffer[i].frag_tab_bus =
329                     virt_to_bus((void *) mem);
330
331                 //if (alloc_contig) {
332                 if (fh->jpg_buffers.need_contiguous) {
333                         mem =
334                             (unsigned long) kmalloc(fh->jpg_buffers.
335                                                     buffer_size,
336                                                     GFP_KERNEL);
337                         if (mem == 0) {
338                                 dprintk(1,
339                                         KERN_ERR
340                                         "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
341                                         ZR_DEVNAME(zr), i);
342                                 jpg_fbuffer_free(file);
343                                 return -ENOBUFS;
344                         }
345                         fh->jpg_buffers.buffer[i].frag_tab[0] =
346                             cpu_to_le32(virt_to_bus((void *) mem));
347                         fh->jpg_buffers.buffer[i].frag_tab[1] =
348                             cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
349                         for (off = 0; off < fh->jpg_buffers.buffer_size;
350                              off += PAGE_SIZE)
351                                 SetPageReserved(virt_to_page(mem + off));
352                 } else {
353                         /* jpg_bufsize is already page aligned */
354                         for (j = 0;
355                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
356                              j++) {
357                                 mem = get_zeroed_page(GFP_KERNEL);
358                                 if (mem == 0) {
359                                         dprintk(1,
360                                                 KERN_ERR
361                                                 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
362                                                 ZR_DEVNAME(zr), i);
363                                         jpg_fbuffer_free(file);
364                                         return -ENOBUFS;
365                                 }
366
367                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
368                                     cpu_to_le32(virt_to_bus((void *) mem));
369                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
370                                                                    1] =
371                                     cpu_to_le32((PAGE_SIZE / 4) << 1);
372                                 SetPageReserved(virt_to_page(mem));
373                         }
374
375                         fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
376                 }
377         }
378
379         dprintk(4,
380                 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
381                 ZR_DEVNAME(zr),
382                 (fh->jpg_buffers.num_buffers *
383                  fh->jpg_buffers.buffer_size) >> 10);
384
385         fh->jpg_buffers.allocated = 1;
386
387         return 0;
388 }
389
390 /* free the MJPEG grab buffers */
391 static void
392 jpg_fbuffer_free (struct file *file)
393 {
394         struct zoran_fh *fh = file->private_data;
395         struct zoran *zr = fh->zr;
396         int i, j, off;
397         unsigned char *mem;
398         __le32 frag_tab;
399
400         dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
401
402         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
403                 if (!fh->jpg_buffers.buffer[i].frag_tab)
404                         continue;
405
406                 if (fh->jpg_buffers.need_contiguous) {
407                         frag_tab = fh->jpg_buffers.buffer[i].frag_tab[0];
408
409                         if (frag_tab) {
410                                 mem = (unsigned char *)bus_to_virt(le32_to_cpu(frag_tab));
411                                 for (off = 0; off < fh->jpg_buffers.buffer_size; off += PAGE_SIZE)
412                                         ClearPageReserved(virt_to_page(mem + off));
413                                 kfree(mem);
414                                 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
415                                 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
416                         }
417                 } else {
418                         for (j = 0; j < fh->jpg_buffers.buffer_size / PAGE_SIZE; j++) {
419                                 frag_tab = fh->jpg_buffers.buffer[i].frag_tab[2 * j];
420
421                                 if (!frag_tab)
422                                         break;
423                                 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
424                                 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
425                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j] = 0;
426                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j + 1] = 0;
427                         }
428                 }
429
430                 free_page((unsigned long)fh->jpg_buffers.buffer[i].frag_tab);
431                 fh->jpg_buffers.buffer[i].frag_tab = NULL;
432         }
433
434         fh->jpg_buffers.allocated = 0;
435 }
436
437 /*
438  *   V4L Buffer grabbing
439  */
440
441 static int
442 zoran_v4l_set_format (struct file               *file,
443                       int                        width,
444                       int                        height,
445                       const struct zoran_format *format)
446 {
447         struct zoran_fh *fh = file->private_data;
448         struct zoran *zr = fh->zr;
449         int bpp;
450
451         /* Check size and format of the grab wanted */
452
453         if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
454             height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
455                 dprintk(1,
456                         KERN_ERR
457                         "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
458                         ZR_DEVNAME(zr), width, height);
459                 return -EINVAL;
460         }
461
462         bpp = (format->depth + 7) / 8;
463
464         /* Check against available buffer size */
465         if (height * width * bpp > fh->v4l_buffers.buffer_size) {
466                 dprintk(1,
467                         KERN_ERR
468                         "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
469                         ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
470                 return -EINVAL;
471         }
472
473         /* The video front end needs 4-byte alinged line sizes */
474
475         if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
476                 dprintk(1,
477                         KERN_ERR
478                         "%s: v4l_set_format() - wrong frame alignment\n",
479                         ZR_DEVNAME(zr));
480                 return -EINVAL;
481         }
482
483         fh->v4l_settings.width = width;
484         fh->v4l_settings.height = height;
485         fh->v4l_settings.format = format;
486         fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
487
488         return 0;
489 }
490
491 static int
492 zoran_v4l_queue_frame (struct file *file,
493                        int          num)
494 {
495         struct zoran_fh *fh = file->private_data;
496         struct zoran *zr = fh->zr;
497         unsigned long flags;
498         int res = 0;
499
500         if (!fh->v4l_buffers.allocated) {
501                 dprintk(1,
502                         KERN_ERR
503                         "%s: v4l_queue_frame() - buffers not yet allocated\n",
504                         ZR_DEVNAME(zr));
505                 res = -ENOMEM;
506         }
507
508         /* No grabbing outside the buffer range! */
509         if (num >= fh->v4l_buffers.num_buffers || num < 0) {
510                 dprintk(1,
511                         KERN_ERR
512                         "%s: v4l_queue_frame() - buffer %d is out of range\n",
513                         ZR_DEVNAME(zr), num);
514                 res = -EINVAL;
515         }
516
517         spin_lock_irqsave(&zr->spinlock, flags);
518
519         if (fh->v4l_buffers.active == ZORAN_FREE) {
520                 if (zr->v4l_buffers.active == ZORAN_FREE) {
521                         zr->v4l_buffers = fh->v4l_buffers;
522                         fh->v4l_buffers.active = ZORAN_ACTIVE;
523                 } else {
524                         dprintk(1,
525                                 KERN_ERR
526                                 "%s: v4l_queue_frame() - another session is already capturing\n",
527                                 ZR_DEVNAME(zr));
528                         res = -EBUSY;
529                 }
530         }
531
532         /* make sure a grab isn't going on currently with this buffer */
533         if (!res) {
534                 switch (zr->v4l_buffers.buffer[num].state) {
535                 default:
536                 case BUZ_STATE_PEND:
537                         if (zr->v4l_buffers.active == ZORAN_FREE) {
538                                 fh->v4l_buffers.active = ZORAN_FREE;
539                                 zr->v4l_buffers.allocated = 0;
540                         }
541                         res = -EBUSY;   /* what are you doing? */
542                         break;
543                 case BUZ_STATE_DONE:
544                         dprintk(2,
545                                 KERN_WARNING
546                                 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
547                                 ZR_DEVNAME(zr), num);
548                 case BUZ_STATE_USER:
549                         /* since there is at least one unused buffer there's room for at least
550                          * one more pend[] entry */
551                         zr->v4l_pend[zr->v4l_pend_head++ &
552                                         V4L_MASK_FRAME] = num;
553                         zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
554                         zr->v4l_buffers.buffer[num].bs.length =
555                             fh->v4l_settings.bytesperline *
556                             zr->v4l_settings.height;
557                         fh->v4l_buffers.buffer[num] =
558                             zr->v4l_buffers.buffer[num];
559                         break;
560                 }
561         }
562
563         spin_unlock_irqrestore(&zr->spinlock, flags);
564
565         if (!res && zr->v4l_buffers.active == ZORAN_FREE)
566                 zr->v4l_buffers.active = fh->v4l_buffers.active;
567
568         return res;
569 }
570
571 /*
572  * Sync on a V4L buffer
573  */
574
575 static int
576 v4l_sync (struct file *file,
577           int          frame)
578 {
579         struct zoran_fh *fh = file->private_data;
580         struct zoran *zr = fh->zr;
581         unsigned long flags;
582
583         if (fh->v4l_buffers.active == ZORAN_FREE) {
584                 dprintk(1,
585                         KERN_ERR
586                         "%s: v4l_sync() - no grab active for this session\n",
587                         ZR_DEVNAME(zr));
588                 return -EINVAL;
589         }
590
591         /* check passed-in frame number */
592         if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
593                 dprintk(1,
594                         KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
595                         ZR_DEVNAME(zr), frame);
596                 return -EINVAL;
597         }
598
599         /* Check if is buffer was queued at all */
600         if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
601                 dprintk(1,
602                         KERN_ERR
603                         "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
604                         ZR_DEVNAME(zr));
605                 return -EPROTO;
606         }
607
608         /* wait on this buffer to get ready */
609         if (!wait_event_interruptible_timeout(zr->v4l_capq,
610                                 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
611                                 10*HZ))
612                 return -ETIME;
613         if (signal_pending(current))
614                 return -ERESTARTSYS;
615
616         /* buffer should now be in BUZ_STATE_DONE */
617         if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
618                 dprintk(2,
619                         KERN_ERR "%s: v4l_sync() - internal state error\n",
620                         ZR_DEVNAME(zr));
621
622         zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
623         fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
624
625         spin_lock_irqsave(&zr->spinlock, flags);
626
627         /* Check if streaming capture has finished */
628         if (zr->v4l_pend_tail == zr->v4l_pend_head) {
629                 zr36057_set_memgrab(zr, 0);
630                 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
631                         fh->v4l_buffers.active = zr->v4l_buffers.active =
632                             ZORAN_FREE;
633                         zr->v4l_buffers.allocated = 0;
634                 }
635         }
636
637         spin_unlock_irqrestore(&zr->spinlock, flags);
638
639         return 0;
640 }
641
642 /*
643  *   Queue a MJPEG buffer for capture/playback
644  */
645
646 static int
647 zoran_jpg_queue_frame (struct file          *file,
648                        int                   num,
649                        enum zoran_codec_mode mode)
650 {
651         struct zoran_fh *fh = file->private_data;
652         struct zoran *zr = fh->zr;
653         unsigned long flags;
654         int res = 0;
655
656         /* Check if buffers are allocated */
657         if (!fh->jpg_buffers.allocated) {
658                 dprintk(1,
659                         KERN_ERR
660                         "%s: jpg_queue_frame() - buffers not yet allocated\n",
661                         ZR_DEVNAME(zr));
662                 return -ENOMEM;
663         }
664
665         /* No grabbing outside the buffer range! */
666         if (num >= fh->jpg_buffers.num_buffers || num < 0) {
667                 dprintk(1,
668                         KERN_ERR
669                         "%s: jpg_queue_frame() - buffer %d out of range\n",
670                         ZR_DEVNAME(zr), num);
671                 return -EINVAL;
672         }
673
674         /* what is the codec mode right now? */
675         if (zr->codec_mode == BUZ_MODE_IDLE) {
676                 zr->jpg_settings = fh->jpg_settings;
677         } else if (zr->codec_mode != mode) {
678                 /* wrong codec mode active - invalid */
679                 dprintk(1,
680                         KERN_ERR
681                         "%s: jpg_queue_frame() - codec in wrong mode\n",
682                         ZR_DEVNAME(zr));
683                 return -EINVAL;
684         }
685
686         if (fh->jpg_buffers.active == ZORAN_FREE) {
687                 if (zr->jpg_buffers.active == ZORAN_FREE) {
688                         zr->jpg_buffers = fh->jpg_buffers;
689                         fh->jpg_buffers.active = ZORAN_ACTIVE;
690                 } else {
691                         dprintk(1,
692                                 KERN_ERR
693                                 "%s: jpg_queue_frame() - another session is already capturing\n",
694                                 ZR_DEVNAME(zr));
695                         res = -EBUSY;
696                 }
697         }
698
699         if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
700                 /* Ok load up the jpeg codec */
701                 zr36057_enable_jpg(zr, mode);
702         }
703
704         spin_lock_irqsave(&zr->spinlock, flags);
705
706         if (!res) {
707                 switch (zr->jpg_buffers.buffer[num].state) {
708                 case BUZ_STATE_DONE:
709                         dprintk(2,
710                                 KERN_WARNING
711                                 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
712                                 ZR_DEVNAME(zr));
713                 case BUZ_STATE_USER:
714                         /* since there is at least one unused buffer there's room for at
715                          *least one more pend[] entry */
716                         zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
717                             num;
718                         zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
719                         fh->jpg_buffers.buffer[num] =
720                             zr->jpg_buffers.buffer[num];
721                         zoran_feed_stat_com(zr);
722                         break;
723                 default:
724                 case BUZ_STATE_DMA:
725                 case BUZ_STATE_PEND:
726                         if (zr->jpg_buffers.active == ZORAN_FREE) {
727                                 fh->jpg_buffers.active = ZORAN_FREE;
728                                 zr->jpg_buffers.allocated = 0;
729                         }
730                         res = -EBUSY;   /* what are you doing? */
731                         break;
732                 }
733         }
734
735         spin_unlock_irqrestore(&zr->spinlock, flags);
736
737         if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
738                 zr->jpg_buffers.active = fh->jpg_buffers.active;
739         }
740
741         return res;
742 }
743
744 static int
745 jpg_qbuf (struct file          *file,
746           int                   frame,
747           enum zoran_codec_mode mode)
748 {
749         struct zoran_fh *fh = file->private_data;
750         struct zoran *zr = fh->zr;
751         int res = 0;
752
753         /* Does the user want to stop streaming? */
754         if (frame < 0) {
755                 if (zr->codec_mode == mode) {
756                         if (fh->jpg_buffers.active == ZORAN_FREE) {
757                                 dprintk(1,
758                                         KERN_ERR
759                                         "%s: jpg_qbuf(-1) - session not active\n",
760                                         ZR_DEVNAME(zr));
761                                 return -EINVAL;
762                         }
763                         fh->jpg_buffers.active = zr->jpg_buffers.active =
764                             ZORAN_FREE;
765                         zr->jpg_buffers.allocated = 0;
766                         zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
767                         return 0;
768                 } else {
769                         dprintk(1,
770                                 KERN_ERR
771                                 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
772                                 ZR_DEVNAME(zr));
773                         return -EINVAL;
774                 }
775         }
776
777         if ((res = zoran_jpg_queue_frame(file, frame, mode)))
778                 return res;
779
780         /* Start the jpeg codec when the first frame is queued  */
781         if (!res && zr->jpg_que_head == 1)
782                 jpeg_start(zr);
783
784         return res;
785 }
786
787 /*
788  *   Sync on a MJPEG buffer
789  */
790
791 static int
792 jpg_sync (struct file       *file,
793           struct zoran_sync *bs)
794 {
795         struct zoran_fh *fh = file->private_data;
796         struct zoran *zr = fh->zr;
797         unsigned long flags;
798         int frame;
799
800         if (fh->jpg_buffers.active == ZORAN_FREE) {
801                 dprintk(1,
802                         KERN_ERR
803                         "%s: jpg_sync() - capture is not currently active\n",
804                         ZR_DEVNAME(zr));
805                 return -EINVAL;
806         }
807         if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
808             zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
809                 dprintk(1,
810                         KERN_ERR
811                         "%s: jpg_sync() - codec not in streaming mode\n",
812                         ZR_DEVNAME(zr));
813                 return -EINVAL;
814         }
815         if (!wait_event_interruptible_timeout(zr->jpg_capq,
816                         (zr->jpg_que_tail != zr->jpg_dma_tail ||
817                          zr->jpg_dma_tail == zr->jpg_dma_head),
818                         10*HZ)) {
819                 int isr;
820
821                 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
822                 udelay(1);
823                 zr->codec->control(zr->codec, CODEC_G_STATUS,
824                                            sizeof(isr), &isr);
825                 dprintk(1,
826                         KERN_ERR
827                         "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
828                         ZR_DEVNAME(zr), isr);
829
830                 return -ETIME;
831
832         }
833         if (signal_pending(current))
834                 return -ERESTARTSYS;
835
836         spin_lock_irqsave(&zr->spinlock, flags);
837
838         if (zr->jpg_dma_tail != zr->jpg_dma_head)
839                 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
840         else
841                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
842
843         /* buffer should now be in BUZ_STATE_DONE */
844         if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
845                 dprintk(2,
846                         KERN_ERR "%s: jpg_sync() - internal state error\n",
847                         ZR_DEVNAME(zr));
848
849         *bs = zr->jpg_buffers.buffer[frame].bs;
850         bs->frame = frame;
851         zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
852         fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
853
854         spin_unlock_irqrestore(&zr->spinlock, flags);
855
856         return 0;
857 }
858
859 static void
860 zoran_open_init_session (struct file *file)
861 {
862         int i;
863         struct zoran_fh *fh = file->private_data;
864         struct zoran *zr = fh->zr;
865
866         /* Per default, map the V4L Buffers */
867         fh->map_mode = ZORAN_MAP_MODE_RAW;
868
869         /* take over the card's current settings */
870         fh->overlay_settings = zr->overlay_settings;
871         fh->overlay_settings.is_set = 0;
872         fh->overlay_settings.format = zr->overlay_settings.format;
873         fh->overlay_active = ZORAN_FREE;
874
875         /* v4l settings */
876         fh->v4l_settings = zr->v4l_settings;
877
878         /* v4l_buffers */
879         memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
880         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
881                 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
882                 fh->v4l_buffers.buffer[i].bs.frame = i;
883         }
884         fh->v4l_buffers.allocated = 0;
885         fh->v4l_buffers.active = ZORAN_FREE;
886         fh->v4l_buffers.buffer_size = v4l_bufsize;
887         fh->v4l_buffers.num_buffers = v4l_nbufs;
888
889         /* jpg settings */
890         fh->jpg_settings = zr->jpg_settings;
891
892         /* jpg_buffers */
893         memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
894         for (i = 0; i < BUZ_MAX_FRAME; i++) {
895                 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
896                 fh->jpg_buffers.buffer[i].bs.frame = i;
897         }
898         fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
899         fh->jpg_buffers.allocated = 0;
900         fh->jpg_buffers.active = ZORAN_FREE;
901         fh->jpg_buffers.buffer_size = jpg_bufsize;
902         fh->jpg_buffers.num_buffers = jpg_nbufs;
903 }
904
905 static void
906 zoran_close_end_session (struct file *file)
907 {
908         struct zoran_fh *fh = file->private_data;
909         struct zoran *zr = fh->zr;
910
911         /* overlay */
912         if (fh->overlay_active != ZORAN_FREE) {
913                 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
914                 zr->v4l_overlay_active = 0;
915                 if (!zr->v4l_memgrab_active)
916                         zr36057_overlay(zr, 0);
917                 zr->overlay_mask = NULL;
918         }
919
920         /* v4l capture */
921         if (fh->v4l_buffers.active != ZORAN_FREE) {
922                 unsigned long flags;
923
924                 spin_lock_irqsave(&zr->spinlock, flags);
925                 zr36057_set_memgrab(zr, 0);
926                 zr->v4l_buffers.allocated = 0;
927                 zr->v4l_buffers.active = fh->v4l_buffers.active =
928                     ZORAN_FREE;
929                 spin_unlock_irqrestore(&zr->spinlock, flags);
930         }
931
932         /* v4l buffers */
933         if (fh->v4l_buffers.allocated)
934                 v4l_fbuffer_free(file);
935
936         /* jpg capture */
937         if (fh->jpg_buffers.active != ZORAN_FREE) {
938                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
939                 zr->jpg_buffers.allocated = 0;
940                 zr->jpg_buffers.active = fh->jpg_buffers.active =
941                     ZORAN_FREE;
942         }
943
944         /* jpg buffers */
945         if (fh->jpg_buffers.allocated)
946                 jpg_fbuffer_free(file);
947 }
948
949 /*
950  *   Open a zoran card. Right now the flags stuff is just playing
951  */
952
953 static int zoran_open(struct file *file)
954 {
955         struct zoran *zr = video_drvdata(file);
956         struct zoran_fh *fh;
957         int res, first_open = 0;
958
959         dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
960                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
961
962         lock_kernel();
963
964         if (zr->user >= 2048) {
965                 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
966                         ZR_DEVNAME(zr), zr->user);
967                 res = -EBUSY;
968                 goto fail_unlock;
969         }
970
971         /* now, create the open()-specific file_ops struct */
972         fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
973         if (!fh) {
974                 dprintk(1,
975                         KERN_ERR
976                         "%s: zoran_open() - allocation of zoran_fh failed\n",
977                         ZR_DEVNAME(zr));
978                 res = -ENOMEM;
979                 goto fail_unlock;
980         }
981         /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
982          * on norm-change! */
983         fh->overlay_mask =
984             kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
985         if (!fh->overlay_mask) {
986                 dprintk(1,
987                         KERN_ERR
988                         "%s: zoran_open() - allocation of overlay_mask failed\n",
989                         ZR_DEVNAME(zr));
990                 res = -ENOMEM;
991                 goto fail_fh;
992         }
993
994         if (zr->user++ == 0)
995                 first_open = 1;
996
997         /*mutex_unlock(&zr->resource_lock);*/
998
999         /* default setup - TODO: look at flags */
1000         if (first_open) {       /* First device open */
1001                 zr36057_restart(zr);
1002                 zoran_open_init_params(zr);
1003                 zoran_init_hardware(zr);
1004
1005                 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1006         }
1007
1008         /* set file_ops stuff */
1009         file->private_data = fh;
1010         fh->zr = zr;
1011         zoran_open_init_session(file);
1012         unlock_kernel();
1013
1014         return 0;
1015
1016 fail_fh:
1017         kfree(fh);
1018 fail_unlock:
1019         unlock_kernel();
1020
1021         dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1022                 ZR_DEVNAME(zr), res, zr->user);
1023
1024         return res;
1025 }
1026
1027 static int
1028 zoran_close(struct file  *file)
1029 {
1030         struct zoran_fh *fh = file->private_data;
1031         struct zoran *zr = fh->zr;
1032
1033         dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1034                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1035
1036         /* kernel locks (fs/device.c), so don't do that ourselves
1037          * (prevents deadlocks) */
1038         /*mutex_lock(&zr->resource_lock);*/
1039
1040         zoran_close_end_session(file);
1041
1042         if (zr->user-- == 1) {  /* Last process */
1043                 /* Clean up JPEG process */
1044                 wake_up_interruptible(&zr->jpg_capq);
1045                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1046                 zr->jpg_buffers.allocated = 0;
1047                 zr->jpg_buffers.active = ZORAN_FREE;
1048
1049                 /* disable interrupts */
1050                 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1051
1052                 if (zr36067_debug > 1)
1053                         print_interrupts(zr);
1054
1055                 /* Overlay off */
1056                 zr->v4l_overlay_active = 0;
1057                 zr36057_overlay(zr, 0);
1058                 zr->overlay_mask = NULL;
1059
1060                 /* capture off */
1061                 wake_up_interruptible(&zr->v4l_capq);
1062                 zr36057_set_memgrab(zr, 0);
1063                 zr->v4l_buffers.allocated = 0;
1064                 zr->v4l_buffers.active = ZORAN_FREE;
1065                 zoran_set_pci_master(zr, 0);
1066
1067                 if (!pass_through) {    /* Switch to color bar */
1068                         struct v4l2_routing route = { 2, 0 };
1069
1070                         decoder_call(zr, video, s_stream, 0);
1071                         encoder_call(zr, video, s_routing, &route);
1072                 }
1073         }
1074
1075         file->private_data = NULL;
1076         kfree(fh->overlay_mask);
1077         kfree(fh);
1078
1079         dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1080
1081         return 0;
1082 }
1083
1084
1085 static ssize_t
1086 zoran_read (struct file *file,
1087             char        __user *data,
1088             size_t       count,
1089             loff_t      *ppos)
1090 {
1091         /* we simply don't support read() (yet)... */
1092
1093         return -EINVAL;
1094 }
1095
1096 static ssize_t
1097 zoran_write (struct file *file,
1098              const char  __user *data,
1099              size_t       count,
1100              loff_t      *ppos)
1101 {
1102         /* ...and the same goes for write() */
1103
1104         return -EINVAL;
1105 }
1106
1107 static int
1108 setup_fbuffer (struct file               *file,
1109                void                      *base,
1110                const struct zoran_format *fmt,
1111                int                        width,
1112                int                        height,
1113                int                        bytesperline)
1114 {
1115         struct zoran_fh *fh = file->private_data;
1116         struct zoran *zr = fh->zr;
1117
1118         /* (Ronald) v4l/v4l2 guidelines */
1119         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1120                 return -EPERM;
1121
1122         /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1123            ALi Magik (that needs very low latency while the card needs a
1124            higher value always) */
1125
1126         if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1127                 return -ENXIO;
1128
1129         /* we need a bytesperline value, even if not given */
1130         if (!bytesperline)
1131                 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1132
1133 #if 0
1134         if (zr->overlay_active) {
1135                 /* dzjee... stupid users... don't even bother to turn off
1136                  * overlay before changing the memory location...
1137                  * normally, we would return errors here. However, one of
1138                  * the tools that does this is... xawtv! and since xawtv
1139                  * is used by +/- 99% of the users, we'd rather be user-
1140                  * friendly and silently do as if nothing went wrong */
1141                 dprintk(3,
1142                         KERN_ERR
1143                         "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1144                         ZR_DEVNAME(zr));
1145                 zr36057_overlay(zr, 0);
1146         }
1147 #endif
1148
1149         if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1150                 dprintk(1,
1151                         KERN_ERR
1152                         "%s: setup_fbuffer() - no valid overlay format given\n",
1153                         ZR_DEVNAME(zr));
1154                 return -EINVAL;
1155         }
1156         if (height <= 0 || width <= 0 || bytesperline <= 0) {
1157                 dprintk(1,
1158                         KERN_ERR
1159                         "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1160                         ZR_DEVNAME(zr), width, height, bytesperline);
1161                 return -EINVAL;
1162         }
1163         if (bytesperline & 3) {
1164                 dprintk(1,
1165                         KERN_ERR
1166                         "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1167                         ZR_DEVNAME(zr), bytesperline);
1168                 return -EINVAL;
1169         }
1170
1171         zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1172         zr->vbuf_height = height;
1173         zr->vbuf_width = width;
1174         zr->vbuf_depth = fmt->depth;
1175         zr->overlay_settings.format = fmt;
1176         zr->vbuf_bytesperline = bytesperline;
1177
1178         /* The user should set new window parameters */
1179         zr->overlay_settings.is_set = 0;
1180
1181         return 0;
1182 }
1183
1184
1185 static int
1186 setup_window (struct file       *file,
1187               int                x,
1188               int                y,
1189               int                width,
1190               int                height,
1191               struct v4l2_clip __user *clips,
1192               int                clipcount,
1193               void              __user *bitmap)
1194 {
1195         struct zoran_fh *fh = file->private_data;
1196         struct zoran *zr = fh->zr;
1197         struct v4l2_clip *vcp = NULL;
1198         int on, end;
1199
1200
1201         if (!zr->vbuf_base) {
1202                 dprintk(1,
1203                         KERN_ERR
1204                         "%s: setup_window() - frame buffer has to be set first\n",
1205                         ZR_DEVNAME(zr));
1206                 return -EINVAL;
1207         }
1208
1209         if (!fh->overlay_settings.format) {
1210                 dprintk(1,
1211                         KERN_ERR
1212                         "%s: setup_window() - no overlay format set\n",
1213                         ZR_DEVNAME(zr));
1214                 return -EINVAL;
1215         }
1216
1217         /*
1218          * The video front end needs 4-byte alinged line sizes, we correct that
1219          * silently here if necessary
1220          */
1221         if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1222                 end = (x + width) & ~1; /* round down */
1223                 x = (x + 1) & ~1;       /* round up */
1224                 width = end - x;
1225         }
1226
1227         if (zr->vbuf_depth == 24) {
1228                 end = (x + width) & ~3; /* round down */
1229                 x = (x + 3) & ~3;       /* round up */
1230                 width = end - x;
1231         }
1232
1233         if (width > BUZ_MAX_WIDTH)
1234                 width = BUZ_MAX_WIDTH;
1235         if (height > BUZ_MAX_HEIGHT)
1236                 height = BUZ_MAX_HEIGHT;
1237
1238         /* Check for vaild parameters */
1239         if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1240             width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1241                 dprintk(1,
1242                         KERN_ERR
1243                         "%s: setup_window() - width = %d or height = %d invalid\n",
1244                         ZR_DEVNAME(zr), width, height);
1245                 return -EINVAL;
1246         }
1247
1248         fh->overlay_settings.x = x;
1249         fh->overlay_settings.y = y;
1250         fh->overlay_settings.width = width;
1251         fh->overlay_settings.height = height;
1252         fh->overlay_settings.clipcount = clipcount;
1253
1254         /*
1255          * If an overlay is running, we have to switch it off
1256          * and switch it on again in order to get the new settings in effect.
1257          *
1258          * We also want to avoid that the overlay mask is written
1259          * when an overlay is running.
1260          */
1261
1262         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1263             zr->overlay_active != ZORAN_FREE &&
1264             fh->overlay_active != ZORAN_FREE;
1265         if (on)
1266                 zr36057_overlay(zr, 0);
1267
1268         /*
1269          *   Write the overlay mask if clips are wanted.
1270          *   We prefer a bitmap.
1271          */
1272         if (bitmap) {
1273                 /* fake value - it just means we want clips */
1274                 fh->overlay_settings.clipcount = 1;
1275
1276                 if (copy_from_user(fh->overlay_mask, bitmap,
1277                                    (width * height + 7) / 8)) {
1278                         return -EFAULT;
1279                 }
1280         } else if (clipcount > 0) {
1281                 /* write our own bitmap from the clips */
1282                 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1283                 if (vcp == NULL) {
1284                         dprintk(1,
1285                                 KERN_ERR
1286                                 "%s: setup_window() - Alloc of clip mask failed\n",
1287                                 ZR_DEVNAME(zr));
1288                         return -ENOMEM;
1289                 }
1290                 if (copy_from_user
1291                     (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1292                         vfree(vcp);
1293                         return -EFAULT;
1294                 }
1295                 write_overlay_mask(file, vcp, clipcount);
1296                 vfree(vcp);
1297         }
1298
1299         fh->overlay_settings.is_set = 1;
1300         if (fh->overlay_active != ZORAN_FREE &&
1301             zr->overlay_active != ZORAN_FREE)
1302                 zr->overlay_settings = fh->overlay_settings;
1303
1304         if (on)
1305                 zr36057_overlay(zr, 1);
1306
1307         /* Make sure the changes come into effect */
1308         return wait_grab_pending(zr);
1309 }
1310
1311 static int
1312 setup_overlay (struct file *file,
1313                int          on)
1314 {
1315         struct zoran_fh *fh = file->private_data;
1316         struct zoran *zr = fh->zr;
1317
1318         /* If there is nothing to do, return immediatly */
1319         if ((on && fh->overlay_active != ZORAN_FREE) ||
1320             (!on && fh->overlay_active == ZORAN_FREE))
1321                 return 0;
1322
1323         /* check whether we're touching someone else's overlay */
1324         if (on && zr->overlay_active != ZORAN_FREE &&
1325             fh->overlay_active == ZORAN_FREE) {
1326                 dprintk(1,
1327                         KERN_ERR
1328                         "%s: setup_overlay() - overlay is already active for another session\n",
1329                         ZR_DEVNAME(zr));
1330                 return -EBUSY;
1331         }
1332         if (!on && zr->overlay_active != ZORAN_FREE &&
1333             fh->overlay_active == ZORAN_FREE) {
1334                 dprintk(1,
1335                         KERN_ERR
1336                         "%s: setup_overlay() - you cannot cancel someone else's session\n",
1337                         ZR_DEVNAME(zr));
1338                 return -EPERM;
1339         }
1340
1341         if (on == 0) {
1342                 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1343                 zr->v4l_overlay_active = 0;
1344                 /* When a grab is running, the video simply
1345                  * won't be switched on any more */
1346                 if (!zr->v4l_memgrab_active)
1347                         zr36057_overlay(zr, 0);
1348                 zr->overlay_mask = NULL;
1349         } else {
1350                 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1351                         dprintk(1,
1352                                 KERN_ERR
1353                                 "%s: setup_overlay() - buffer or window not set\n",
1354                                 ZR_DEVNAME(zr));
1355                         return -EINVAL;
1356                 }
1357                 if (!fh->overlay_settings.format) {
1358                         dprintk(1,
1359                                 KERN_ERR
1360                                 "%s: setup_overlay() - no overlay format set\n",
1361                                 ZR_DEVNAME(zr));
1362                         return -EINVAL;
1363                 }
1364                 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1365                 zr->v4l_overlay_active = 1;
1366                 zr->overlay_mask = fh->overlay_mask;
1367                 zr->overlay_settings = fh->overlay_settings;
1368                 if (!zr->v4l_memgrab_active)
1369                         zr36057_overlay(zr, 1);
1370                 /* When a grab is running, the video will be
1371                  * switched on when grab is finished */
1372         }
1373
1374         /* Make sure the changes come into effect */
1375         return wait_grab_pending(zr);
1376 }
1377
1378         /* get the status of a buffer in the clients buffer queue */
1379 static int
1380 zoran_v4l2_buffer_status (struct zoran_fh    *fh,
1381                           struct v4l2_buffer *buf,
1382                           int                 num)
1383 {
1384         struct zoran *zr = fh->zr;
1385
1386         buf->flags = V4L2_BUF_FLAG_MAPPED;
1387
1388         switch (fh->map_mode) {
1389         case ZORAN_MAP_MODE_RAW:
1390
1391                 /* check range */
1392                 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1393                     !fh->v4l_buffers.allocated) {
1394                         dprintk(1,
1395                                 KERN_ERR
1396                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1397                                 ZR_DEVNAME(zr));
1398                         return -EINVAL;
1399                 }
1400
1401                 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1402                 buf->length = fh->v4l_buffers.buffer_size;
1403
1404                 /* get buffer */
1405                 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1406                 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1407                     fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1408                         buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1409                         buf->flags |= V4L2_BUF_FLAG_DONE;
1410                         buf->timestamp =
1411                             fh->v4l_buffers.buffer[num].bs.timestamp;
1412                 } else {
1413                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1414                 }
1415
1416                 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1417                         buf->field = V4L2_FIELD_TOP;
1418                 else
1419                         buf->field = V4L2_FIELD_INTERLACED;
1420
1421                 break;
1422
1423         case ZORAN_MAP_MODE_JPG_REC:
1424         case ZORAN_MAP_MODE_JPG_PLAY:
1425
1426                 /* check range */
1427                 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1428                     !fh->jpg_buffers.allocated) {
1429                         dprintk(1,
1430                                 KERN_ERR
1431                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1432                                 ZR_DEVNAME(zr));
1433                         return -EINVAL;
1434                 }
1435
1436                 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1437                               V4L2_BUF_TYPE_VIDEO_CAPTURE :
1438                               V4L2_BUF_TYPE_VIDEO_OUTPUT;
1439                 buf->length = fh->jpg_buffers.buffer_size;
1440
1441                 /* these variables are only written after frame has been captured */
1442                 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1443                     fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1444                         buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1445                         buf->timestamp =
1446                             fh->jpg_buffers.buffer[num].bs.timestamp;
1447                         buf->bytesused =
1448                             fh->jpg_buffers.buffer[num].bs.length;
1449                         buf->flags |= V4L2_BUF_FLAG_DONE;
1450                 } else {
1451                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1452                 }
1453
1454                 /* which fields are these? */
1455                 if (fh->jpg_settings.TmpDcm != 1)
1456                         buf->field =
1457                             fh->jpg_settings.
1458                             odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1459                 else
1460                         buf->field =
1461                             fh->jpg_settings.
1462                             odd_even ? V4L2_FIELD_SEQ_TB :
1463                             V4L2_FIELD_SEQ_BT;
1464
1465                 break;
1466
1467         default:
1468
1469                 dprintk(5,
1470                         KERN_ERR
1471                         "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1472                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
1473                 return -EINVAL;
1474         }
1475
1476         buf->memory = V4L2_MEMORY_MMAP;
1477         buf->index = num;
1478         buf->m.offset = buf->length * num;
1479
1480         return 0;
1481 }
1482
1483 static int
1484 zoran_set_norm (struct zoran *zr,
1485                 v4l2_std_id norm)
1486 {
1487         int on;
1488
1489         if (zr->v4l_buffers.active != ZORAN_FREE ||
1490             zr->jpg_buffers.active != ZORAN_FREE) {
1491                 dprintk(1,
1492                         KERN_WARNING
1493                         "%s: set_norm() called while in playback/capture mode\n",
1494                         ZR_DEVNAME(zr));
1495                 return -EBUSY;
1496         }
1497
1498         if (lock_norm && norm != zr->norm) {
1499                 if (lock_norm > 1) {
1500                         dprintk(1,
1501                                 KERN_WARNING
1502                                 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1503                                 ZR_DEVNAME(zr));
1504                         return -EPERM;
1505                 } else {
1506                         dprintk(1,
1507                                 KERN_WARNING
1508                                 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1509                                 ZR_DEVNAME(zr));
1510                         norm = zr->norm;
1511                 }
1512         }
1513
1514         if (!(norm & zr->card.norms)) {
1515                 dprintk(1,
1516                         KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1517                         ZR_DEVNAME(zr), norm);
1518                 return -EINVAL;
1519         }
1520
1521         if (norm == V4L2_STD_ALL) {
1522                 int status = 0;
1523                 v4l2_std_id std = 0;
1524
1525                 decoder_call(zr, video, querystd, &std);
1526                 decoder_s_std(zr, std);
1527
1528                 /* let changes come into effect */
1529                 ssleep(2);
1530
1531                 decoder_call(zr, video, g_input_status, &status);
1532                 if (status & V4L2_IN_ST_NO_SIGNAL) {
1533                         dprintk(1,
1534                                 KERN_ERR
1535                                 "%s: set_norm() - no norm detected\n",
1536                                 ZR_DEVNAME(zr));
1537                         /* reset norm */
1538                         decoder_s_std(zr, zr->norm);
1539                         return -EIO;
1540                 }
1541
1542                 norm = std;
1543         }
1544         if (norm & V4L2_STD_SECAM)
1545                 zr->timing = zr->card.tvn[2];
1546         else if (norm & V4L2_STD_NTSC)
1547                 zr->timing = zr->card.tvn[1];
1548         else
1549                 zr->timing = zr->card.tvn[0];
1550
1551         /* We switch overlay off and on since a change in the
1552          * norm needs different VFE settings */
1553         on = zr->overlay_active && !zr->v4l_memgrab_active;
1554         if (on)
1555                 zr36057_overlay(zr, 0);
1556
1557         decoder_s_std(zr, norm);
1558         encoder_call(zr, video, s_std_output, norm);
1559
1560         if (on)
1561                 zr36057_overlay(zr, 1);
1562
1563         /* Make sure the changes come into effect */
1564         zr->norm = norm;
1565
1566         return 0;
1567 }
1568
1569 static int
1570 zoran_set_input (struct zoran *zr,
1571                  int           input)
1572 {
1573         struct v4l2_routing route = { 0, 0 };
1574
1575         if (input == zr->input) {
1576                 return 0;
1577         }
1578
1579         if (zr->v4l_buffers.active != ZORAN_FREE ||
1580             zr->jpg_buffers.active != ZORAN_FREE) {
1581                 dprintk(1,
1582                         KERN_WARNING
1583                         "%s: set_input() called while in playback/capture mode\n",
1584                         ZR_DEVNAME(zr));
1585                 return -EBUSY;
1586         }
1587
1588         if (input < 0 || input >= zr->card.inputs) {
1589                 dprintk(1,
1590                         KERN_ERR
1591                         "%s: set_input() - unnsupported input %d\n",
1592                         ZR_DEVNAME(zr), input);
1593                 return -EINVAL;
1594         }
1595
1596         route.input = zr->card.input[input].muxsel;
1597         zr->input = input;
1598
1599         decoder_s_routing(zr, &route);
1600
1601         return 0;
1602 }
1603
1604 /*
1605  *   ioctl routine
1606  */
1607
1608 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1609 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1610 {
1611         struct zoran_fh *fh = __fh;
1612         struct zoran *zr = fh->zr;
1613         struct zoran_jpg_settings settings;
1614
1615         switch (cmd) {
1616         case BUZIOC_G_PARAMS:
1617         {
1618                 struct zoran_params *bparams = arg;
1619
1620                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1621
1622                 memset(bparams, 0, sizeof(struct zoran_params));
1623                 bparams->major_version = MAJOR_VERSION;
1624                 bparams->minor_version = MINOR_VERSION;
1625
1626                 mutex_lock(&zr->resource_lock);
1627
1628                 if (zr->norm & V4L2_STD_NTSC)
1629                         bparams->norm = VIDEO_MODE_NTSC;
1630                 else if (zr->norm & V4L2_STD_PAL)
1631                         bparams->norm = VIDEO_MODE_PAL;
1632                 else
1633                         bparams->norm = VIDEO_MODE_SECAM;
1634
1635                 bparams->input = zr->input;
1636
1637                 bparams->decimation = fh->jpg_settings.decimation;
1638                 bparams->HorDcm = fh->jpg_settings.HorDcm;
1639                 bparams->VerDcm = fh->jpg_settings.VerDcm;
1640                 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1641                 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1642                 bparams->img_x = fh->jpg_settings.img_x;
1643                 bparams->img_y = fh->jpg_settings.img_y;
1644                 bparams->img_width = fh->jpg_settings.img_width;
1645                 bparams->img_height = fh->jpg_settings.img_height;
1646                 bparams->odd_even = fh->jpg_settings.odd_even;
1647
1648                 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1649                 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1650                 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1651                 memcpy(bparams->APP_data,
1652                        fh->jpg_settings.jpg_comp.APP_data,
1653                        sizeof(bparams->APP_data));
1654                 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1655                 memcpy(bparams->COM_data,
1656                        fh->jpg_settings.jpg_comp.COM_data,
1657                        sizeof(bparams->COM_data));
1658                 bparams->jpeg_markers =
1659                     fh->jpg_settings.jpg_comp.jpeg_markers;
1660
1661                 mutex_unlock(&zr->resource_lock);
1662
1663                 bparams->VFIFO_FB = 0;
1664
1665                 return 0;
1666         }
1667
1668         case BUZIOC_S_PARAMS:
1669         {
1670                 struct zoran_params *bparams = arg;
1671                 int res = 0;
1672
1673                 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1674
1675                 settings.decimation = bparams->decimation;
1676                 settings.HorDcm = bparams->HorDcm;
1677                 settings.VerDcm = bparams->VerDcm;
1678                 settings.TmpDcm = bparams->TmpDcm;
1679                 settings.field_per_buff = bparams->field_per_buff;
1680                 settings.img_x = bparams->img_x;
1681                 settings.img_y = bparams->img_y;
1682                 settings.img_width = bparams->img_width;
1683                 settings.img_height = bparams->img_height;
1684                 settings.odd_even = bparams->odd_even;
1685
1686                 settings.jpg_comp.quality = bparams->quality;
1687                 settings.jpg_comp.APPn = bparams->APPn;
1688                 settings.jpg_comp.APP_len = bparams->APP_len;
1689                 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1690                        sizeof(bparams->APP_data));
1691                 settings.jpg_comp.COM_len = bparams->COM_len;
1692                 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1693                        sizeof(bparams->COM_data));
1694                 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1695
1696                 mutex_lock(&zr->resource_lock);
1697
1698                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1699                         dprintk(1,
1700                                 KERN_ERR
1701                                 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1702                                 ZR_DEVNAME(zr));
1703                         res = -EINVAL;
1704                         goto sparams_unlock_and_return;
1705                 }
1706
1707                 /* Check the params first before overwriting our
1708                  * nternal values */
1709                 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1710                         res = -EINVAL;
1711                         goto sparams_unlock_and_return;
1712                 }
1713
1714                 fh->jpg_settings = settings;
1715 sparams_unlock_and_return:
1716                 mutex_unlock(&zr->resource_lock);
1717
1718                 return res;
1719         }
1720
1721         case BUZIOC_REQBUFS:
1722         {
1723                 struct zoran_requestbuffers *breq = arg;
1724                 int res = 0;
1725
1726                 dprintk(3,
1727                         KERN_DEBUG
1728                         "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1729                         ZR_DEVNAME(zr), breq->count, breq->size);
1730
1731                 /* Enforce reasonable lower and upper limits */
1732                 if (breq->count < 4)
1733                         breq->count = 4;        /* Could be choosen smaller */
1734                 if (breq->count > jpg_nbufs)
1735                         breq->count = jpg_nbufs;
1736                 breq->size = PAGE_ALIGN(breq->size);
1737                 if (breq->size < 8192)
1738                         breq->size = 8192;      /* Arbitrary */
1739                 /* breq->size is limited by 1 page for the stat_com
1740                  * tables to a Maximum of 2 MB */
1741                 if (breq->size > jpg_bufsize)
1742                         breq->size = jpg_bufsize;
1743
1744                 mutex_lock(&zr->resource_lock);
1745
1746                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1747                         dprintk(1,
1748                                 KERN_ERR
1749                                 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1750                                 ZR_DEVNAME(zr));
1751                         res = -EBUSY;
1752                         goto jpgreqbuf_unlock_and_return;
1753                 }
1754
1755                 fh->jpg_buffers.num_buffers = breq->count;
1756                 fh->jpg_buffers.buffer_size = breq->size;
1757
1758                 if (jpg_fbuffer_alloc(file)) {
1759                         res = -ENOMEM;
1760                         goto jpgreqbuf_unlock_and_return;
1761                 }
1762
1763                 /* The next mmap will map the MJPEG buffers - could
1764                  * also be *_PLAY, but it doesn't matter here */
1765                 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1766 jpgreqbuf_unlock_and_return:
1767                 mutex_unlock(&zr->resource_lock);
1768
1769                 return res;
1770         }
1771
1772         case BUZIOC_QBUF_CAPT:
1773         {
1774                 int *frame = arg, res;
1775
1776                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1777                         ZR_DEVNAME(zr), *frame);
1778
1779                 mutex_lock(&zr->resource_lock);
1780                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1781                 mutex_unlock(&zr->resource_lock);
1782
1783                 return res;
1784         }
1785
1786         case BUZIOC_QBUF_PLAY:
1787         {
1788                 int *frame = arg, res;
1789
1790                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1791                         ZR_DEVNAME(zr), *frame);
1792
1793                 mutex_lock(&zr->resource_lock);
1794                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1795                 mutex_unlock(&zr->resource_lock);
1796
1797                 return res;
1798         }
1799
1800         case BUZIOC_SYNC:
1801         {
1802                 struct zoran_sync *bsync = arg;
1803                 int res;
1804
1805                 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1806
1807                 mutex_lock(&zr->resource_lock);
1808                 res = jpg_sync(file, bsync);
1809                 mutex_unlock(&zr->resource_lock);
1810
1811                 return res;
1812         }
1813
1814         case BUZIOC_G_STATUS:
1815         {
1816                 struct zoran_status *bstat = arg;
1817                 struct v4l2_routing route = { 0, 0 };
1818                 int status = 0, res = 0;
1819                 v4l2_std_id norm;
1820
1821                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1822
1823                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1824                         dprintk(1,
1825                                 KERN_ERR
1826                                 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1827                                 ZR_DEVNAME(zr));
1828                         return -EINVAL;
1829                 }
1830
1831                 route.input = zr->card.input[bstat->input].muxsel;
1832
1833                 mutex_lock(&zr->resource_lock);
1834
1835                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1836                         dprintk(1,
1837                                 KERN_ERR
1838                                 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1839                                 ZR_DEVNAME(zr));
1840                         res = -EINVAL;
1841                         goto gstat_unlock_and_return;
1842                 }
1843
1844                 decoder_s_routing(zr, &route);
1845
1846                 /* sleep 1 second */
1847                 ssleep(1);
1848
1849                 /* Get status of video decoder */
1850                 decoder_call(zr, video, querystd, &norm);
1851                 decoder_call(zr, video, g_input_status, &status);
1852
1853                 /* restore previous input and norm */
1854                 route.input = zr->card.input[zr->input].muxsel;
1855                 decoder_s_routing(zr, &route);
1856 gstat_unlock_and_return:
1857                 mutex_unlock(&zr->resource_lock);
1858
1859                 if (!res) {
1860                         bstat->signal =
1861                             (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1862                         if (norm & V4L2_STD_NTSC)
1863                                 bstat->norm = VIDEO_MODE_NTSC;
1864                         else if (norm & V4L2_STD_SECAM)
1865                                 bstat->norm = VIDEO_MODE_SECAM;
1866                         else
1867                                 bstat->norm = VIDEO_MODE_PAL;
1868
1869                         bstat->color =
1870                             (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1871                 }
1872
1873                 return res;
1874         }
1875
1876         default:
1877                 return -EINVAL;
1878         }
1879 }
1880
1881 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1882 {
1883         struct zoran_fh *fh = __fh;
1884         struct zoran *zr = fh->zr;
1885         int i, res = 0;
1886
1887         vmbuf->size =
1888             fh->v4l_buffers.num_buffers *
1889             fh->v4l_buffers.buffer_size;
1890         vmbuf->frames = fh->v4l_buffers.num_buffers;
1891         for (i = 0; i < vmbuf->frames; i++) {
1892                 vmbuf->offsets[i] =
1893                     i * fh->v4l_buffers.buffer_size;
1894         }
1895
1896         mutex_lock(&zr->resource_lock);
1897
1898         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1899                 dprintk(1,
1900                         KERN_ERR
1901                         "%s: VIDIOCGMBUF - buffers already allocated\n",
1902                         ZR_DEVNAME(zr));
1903                 res = -EINVAL;
1904                 goto v4l1reqbuf_unlock_and_return;
1905         }
1906
1907         if (v4l_fbuffer_alloc(file)) {
1908                 res = -ENOMEM;
1909                 goto v4l1reqbuf_unlock_and_return;
1910         }
1911
1912         /* The next mmap will map the V4L buffers */
1913         fh->map_mode = ZORAN_MAP_MODE_RAW;
1914 v4l1reqbuf_unlock_and_return:
1915         mutex_unlock(&zr->resource_lock);
1916
1917         return res;
1918 }
1919 #endif
1920
1921 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1922 {
1923         struct zoran_fh *fh = __fh;
1924         struct zoran *zr = fh->zr;
1925
1926         memset(cap, 0, sizeof(*cap));
1927         strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1928         strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1929         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1930                  pci_name(zr->pci_dev));
1931         cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1932                            RELEASE_VERSION);
1933         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1934                             V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1935         return 0;
1936 }
1937
1938 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1939 {
1940         int num = -1, i;
1941
1942         for (i = 0; i < NUM_FORMATS; i++) {
1943                 if (zoran_formats[i].flags & flag)
1944                         num++;
1945                 if (num == fmt->index)
1946                         break;
1947         }
1948         if (fmt->index < 0 /* late, but not too late */  || i == NUM_FORMATS)
1949                 return -EINVAL;
1950
1951         strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1952         fmt->pixelformat = zoran_formats[i].fourcc;
1953         if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1954                 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1955         return 0;
1956 }
1957
1958 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1959                                             struct v4l2_fmtdesc *f)
1960 {
1961         struct zoran_fh *fh = __fh;
1962         struct zoran *zr = fh->zr;
1963
1964         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1965 }
1966
1967 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1968                                             struct v4l2_fmtdesc *f)
1969 {
1970         struct zoran_fh *fh = __fh;
1971         struct zoran *zr = fh->zr;
1972
1973         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1974 }
1975
1976 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1977                                             struct v4l2_fmtdesc *f)
1978 {
1979         struct zoran_fh *fh = __fh;
1980         struct zoran *zr = fh->zr;
1981
1982         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1983 }
1984
1985 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1986                                         struct v4l2_format *fmt)
1987 {
1988         struct zoran_fh *fh = __fh;
1989         struct zoran *zr = fh->zr;
1990
1991         mutex_lock(&zr->resource_lock);
1992
1993         fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1994         fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1995                 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1996         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1997         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1998         if (fh->jpg_settings.TmpDcm == 1)
1999                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2000                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2001         else
2002                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2003                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2004         fmt->fmt.pix.bytesperline = 0;
2005         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2006
2007         mutex_unlock(&zr->resource_lock);
2008         return 0;
2009 }
2010
2011 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2012                                         struct v4l2_format *fmt)
2013 {
2014         struct zoran_fh *fh = __fh;
2015         struct zoran *zr = fh->zr;
2016
2017         if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2018                 return zoran_g_fmt_vid_out(file, fh, fmt);
2019
2020         mutex_lock(&zr->resource_lock);
2021         fmt->fmt.pix.width = fh->v4l_settings.width;
2022         fmt->fmt.pix.height = fh->v4l_settings.height;
2023         fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2024                                         fh->v4l_settings.height;
2025         fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2026         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2027         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2028         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2029                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2030         else
2031                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2032         mutex_unlock(&zr->resource_lock);
2033         return 0;
2034 }
2035
2036 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2037                                         struct v4l2_format *fmt)
2038 {
2039         struct zoran_fh *fh = __fh;
2040         struct zoran *zr = fh->zr;
2041
2042         mutex_lock(&zr->resource_lock);
2043
2044         fmt->fmt.win.w.left = fh->overlay_settings.x;
2045         fmt->fmt.win.w.top = fh->overlay_settings.y;
2046         fmt->fmt.win.w.width = fh->overlay_settings.width;
2047         fmt->fmt.win.w.height = fh->overlay_settings.height;
2048         if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2049                 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2050         else
2051                 fmt->fmt.win.field = V4L2_FIELD_TOP;
2052
2053         mutex_unlock(&zr->resource_lock);
2054         return 0;
2055 }
2056
2057 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2058                                         struct v4l2_format *fmt)
2059 {
2060         struct zoran_fh *fh = __fh;
2061         struct zoran *zr = fh->zr;
2062
2063         mutex_lock(&zr->resource_lock);
2064
2065         if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2066                 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2067         if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2068                 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2069         if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2070                 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2071         if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2072                 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2073
2074         mutex_unlock(&zr->resource_lock);
2075         return 0;
2076 }
2077
2078 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2079                                         struct v4l2_format *fmt)
2080 {
2081         struct zoran_fh *fh = __fh;
2082         struct zoran *zr = fh->zr;
2083         struct zoran_jpg_settings settings;
2084         int res = 0;
2085
2086         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2087                 return -EINVAL;
2088
2089         mutex_lock(&zr->resource_lock);
2090         settings = fh->jpg_settings;
2091
2092         /* we actually need to set 'real' parameters now */
2093         if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2094                 settings.TmpDcm = 1;
2095         else
2096                 settings.TmpDcm = 2;
2097         settings.decimation = 0;
2098         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2099                 settings.VerDcm = 2;
2100         else
2101                 settings.VerDcm = 1;
2102         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2103                 settings.HorDcm = 4;
2104         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2105                 settings.HorDcm = 2;
2106         else
2107                 settings.HorDcm = 1;
2108         if (settings.TmpDcm == 1)
2109                 settings.field_per_buff = 2;
2110         else
2111                 settings.field_per_buff = 1;
2112
2113         if (settings.HorDcm > 1) {
2114                 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2115                 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2116         } else {
2117                 settings.img_x = 0;
2118                 settings.img_width = BUZ_MAX_WIDTH;
2119         }
2120
2121         /* check */
2122         res = zoran_check_jpg_settings(zr, &settings, 1);
2123         if (res)
2124                 goto tryfmt_unlock_and_return;
2125
2126         /* tell the user what we actually did */
2127         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2128         fmt->fmt.pix.height = settings.img_height * 2 /
2129                 (settings.TmpDcm * settings.VerDcm);
2130         if (settings.TmpDcm == 1)
2131                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2132                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2133         else
2134                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2135                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2136
2137         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2138         fmt->fmt.pix.bytesperline = 0;
2139         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2140 tryfmt_unlock_and_return:
2141         mutex_unlock(&zr->resource_lock);
2142         return res;
2143 }
2144
2145 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2146                                         struct v4l2_format *fmt)
2147 {
2148         struct zoran_fh *fh = __fh;
2149         struct zoran *zr = fh->zr;
2150         int bpp;
2151         int i;
2152
2153         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2154                 return zoran_try_fmt_vid_out(file, fh, fmt);
2155
2156         mutex_lock(&zr->resource_lock);
2157
2158         for (i = 0; i < NUM_FORMATS; i++)
2159                 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2160                         break;
2161
2162         if (i == NUM_FORMATS) {
2163                 mutex_unlock(&zr->resource_lock);
2164                 return -EINVAL;
2165         }
2166
2167         bpp = (zoran_formats[i].depth + 7) / 8;
2168         fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2169         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2170                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2171         if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2172                 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2173         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2174                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2175         if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2176                 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2177         mutex_unlock(&zr->resource_lock);
2178
2179         return 0;
2180 }
2181
2182 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2183                                         struct v4l2_format *fmt)
2184 {
2185         struct zoran_fh *fh = __fh;
2186         struct zoran *zr = fh->zr;
2187         int res;
2188
2189         dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2190                         fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2191                         fmt->fmt.win.w.width,
2192                         fmt->fmt.win.w.height,
2193                         fmt->fmt.win.clipcount,
2194                         fmt->fmt.win.bitmap);
2195         mutex_lock(&zr->resource_lock);
2196         res = setup_window(file, fmt->fmt.win.w.left,
2197                         fmt->fmt.win.w.top,
2198                         fmt->fmt.win.w.width,
2199                         fmt->fmt.win.w.height,
2200                         (struct v4l2_clip __user *)
2201                         fmt->fmt.win.clips,
2202                         fmt->fmt.win.clipcount,
2203                         fmt->fmt.win.bitmap);
2204         mutex_unlock(&zr->resource_lock);
2205         return res;
2206 }
2207
2208 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2209                                         struct v4l2_format *fmt)
2210 {
2211         struct zoran_fh *fh = __fh;
2212         struct zoran *zr = fh->zr;
2213         __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2214         struct zoran_jpg_settings settings;
2215         int res = 0;
2216
2217         dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2218                         fmt->fmt.pix.width, fmt->fmt.pix.height,
2219                         fmt->fmt.pix.pixelformat,
2220                         (char *) &printformat);
2221         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2222                 return -EINVAL;
2223
2224         mutex_lock(&zr->resource_lock);
2225
2226         settings = fh->jpg_settings;
2227
2228         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2229                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2230                                 ZR_DEVNAME(zr));
2231                 res = -EBUSY;
2232                 goto sfmtjpg_unlock_and_return;
2233         }
2234
2235         /* we actually need to set 'real' parameters now */
2236         if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2237                 settings.TmpDcm = 1;
2238         else
2239                 settings.TmpDcm = 2;
2240         settings.decimation = 0;
2241         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2242                 settings.VerDcm = 2;
2243         else
2244                 settings.VerDcm = 1;
2245         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2246                 settings.HorDcm = 4;
2247         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2248                 settings.HorDcm = 2;
2249         else
2250                 settings.HorDcm = 1;
2251         if (settings.TmpDcm == 1)
2252                 settings.field_per_buff = 2;
2253         else
2254                 settings.field_per_buff = 1;
2255
2256         if (settings.HorDcm > 1) {
2257                 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2258                 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2259         } else {
2260                 settings.img_x = 0;
2261                 settings.img_width = BUZ_MAX_WIDTH;
2262         }
2263
2264         /* check */
2265         res = zoran_check_jpg_settings(zr, &settings, 0);
2266         if (res)
2267                 goto sfmtjpg_unlock_and_return;
2268
2269         /* it's ok, so set them */
2270         fh->jpg_settings = settings;
2271
2272         /* tell the user what we actually did */
2273         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2274         fmt->fmt.pix.height = settings.img_height * 2 /
2275                 (settings.TmpDcm * settings.VerDcm);
2276         if (settings.TmpDcm == 1)
2277                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2278                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2279         else
2280                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2281                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2282         fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2283         fmt->fmt.pix.bytesperline = 0;
2284         fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2285         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2286
2287         /* we hereby abuse this variable to show that
2288          * we're gonna do mjpeg capture */
2289         fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2290                 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2291 sfmtjpg_unlock_and_return:
2292         mutex_unlock(&zr->resource_lock);
2293         return res;
2294 }
2295
2296 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2297                                         struct v4l2_format *fmt)
2298 {
2299         struct zoran_fh *fh = __fh;
2300         struct zoran *zr = fh->zr;
2301         int i;
2302         int res = 0;
2303
2304         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2305                 return zoran_s_fmt_vid_out(file, fh, fmt);
2306
2307         for (i = 0; i < NUM_FORMATS; i++)
2308                 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2309                         break;
2310         if (i == NUM_FORMATS) {
2311                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2312                         ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2313                 return -EINVAL;
2314         }
2315         mutex_lock(&zr->resource_lock);
2316         if (fh->jpg_buffers.allocated ||
2317                 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2318                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2319                                 ZR_DEVNAME(zr));
2320                 res = -EBUSY;
2321                 goto sfmtv4l_unlock_and_return;
2322         }
2323         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2324                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2325         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2326                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2327
2328         res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2329                         fmt->fmt.pix.height, &zoran_formats[i]);
2330         if (res)
2331                 goto sfmtv4l_unlock_and_return;
2332
2333         /* tell the user the
2334          * results/missing stuff */
2335         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2336         fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2337         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2338         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2339                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2340         else
2341                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2342
2343         fh->map_mode = ZORAN_MAP_MODE_RAW;
2344 sfmtv4l_unlock_and_return:
2345         mutex_unlock(&zr->resource_lock);
2346         return res;
2347 }
2348
2349 static int zoran_g_fbuf(struct file *file, void *__fh,
2350                 struct v4l2_framebuffer *fb)
2351 {
2352         struct zoran_fh *fh = __fh;
2353         struct zoran *zr = fh->zr;
2354
2355         memset(fb, 0, sizeof(*fb));
2356         mutex_lock(&zr->resource_lock);
2357         fb->base = zr->vbuf_base;
2358         fb->fmt.width = zr->vbuf_width;
2359         fb->fmt.height = zr->vbuf_height;
2360         if (zr->overlay_settings.format)
2361                 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2362         fb->fmt.bytesperline = zr->vbuf_bytesperline;
2363         mutex_unlock(&zr->resource_lock);
2364         fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2365         fb->fmt.field = V4L2_FIELD_INTERLACED;
2366         fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2367         fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2368
2369         return 0;
2370 }
2371
2372 static int zoran_s_fbuf(struct file *file, void *__fh,
2373                 struct v4l2_framebuffer *fb)
2374 {
2375         struct zoran_fh *fh = __fh;
2376         struct zoran *zr = fh->zr;
2377         int i, res = 0;
2378         __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2379
2380         for (i = 0; i < NUM_FORMATS; i++)
2381                 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2382                         break;
2383         if (i == NUM_FORMATS) {
2384                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2385                         ZR_DEVNAME(zr), fb->fmt.pixelformat,
2386                         (char *)&printformat);
2387                 return -EINVAL;
2388         }
2389
2390         mutex_lock(&zr->resource_lock);
2391         res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2392                                 fb->fmt.width, fb->fmt.height,
2393                                 fb->fmt.bytesperline);
2394         mutex_unlock(&zr->resource_lock);
2395
2396         return res;
2397 }
2398
2399 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2400 {
2401         struct zoran_fh *fh = __fh;
2402         struct zoran *zr = fh->zr;
2403         int res;
2404
2405         mutex_lock(&zr->resource_lock);
2406         res = setup_overlay(file, on);
2407         mutex_unlock(&zr->resource_lock);
2408
2409         return res;
2410 }
2411
2412 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2413
2414 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2415 {
2416         struct zoran_fh *fh = __fh;
2417         struct zoran *zr = fh->zr;
2418         int res = 0;
2419
2420         if (req->memory != V4L2_MEMORY_MMAP) {
2421                 dprintk(2,
2422                                 KERN_ERR
2423                                 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2424                                 ZR_DEVNAME(zr), req->memory);
2425                 return -EINVAL;
2426         }
2427
2428         if (req->count == 0)
2429                 return zoran_streamoff(file, fh, req->type);
2430
2431         mutex_lock(&zr->resource_lock);
2432         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2433                 dprintk(2,
2434                                 KERN_ERR
2435                                 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2436                                 ZR_DEVNAME(zr));
2437                 res = -EBUSY;
2438                 goto v4l2reqbuf_unlock_and_return;
2439         }
2440
2441         if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2442                         req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2443
2444                 /* control user input */
2445                 if (req->count < 2)
2446                         req->count = 2;
2447                 if (req->count > v4l_nbufs)
2448                         req->count = v4l_nbufs;
2449                 fh->v4l_buffers.num_buffers = req->count;
2450
2451                 if (v4l_fbuffer_alloc(file)) {
2452                         res = -ENOMEM;
2453                         goto v4l2reqbuf_unlock_and_return;
2454                 }
2455
2456                 /* The next mmap will map the V4L buffers */
2457                 fh->map_mode = ZORAN_MAP_MODE_RAW;
2458
2459         } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2460                         fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2461
2462                 /* we need to calculate size ourselves now */
2463                 if (req->count < 4)
2464                         req->count = 4;
2465                 if (req->count > jpg_nbufs)
2466                         req->count = jpg_nbufs;
2467                 fh->jpg_buffers.num_buffers = req->count;
2468                 fh->jpg_buffers.buffer_size =
2469                         zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2470
2471                 if (jpg_fbuffer_alloc(file)) {
2472                         res = -ENOMEM;
2473                         goto v4l2reqbuf_unlock_and_return;
2474                 }
2475
2476                 /* The next mmap will map the MJPEG buffers */
2477                 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2478                         fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2479                 else
2480                         fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2481
2482         } else {
2483                 dprintk(1,
2484                                 KERN_ERR
2485                                 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2486                                 ZR_DEVNAME(zr), req->type);
2487                 res = -EINVAL;
2488                 goto v4l2reqbuf_unlock_and_return;
2489         }
2490 v4l2reqbuf_unlock_and_return:
2491         mutex_unlock(&zr->resource_lock);
2492
2493         return res;
2494 }
2495
2496 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2497 {
2498         struct zoran_fh *fh = __fh;
2499         struct zoran *zr = fh->zr;
2500         int res;
2501
2502         mutex_lock(&zr->resource_lock);
2503         res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2504         mutex_unlock(&zr->resource_lock);
2505
2506         return res;
2507 }
2508
2509 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2510 {
2511         struct zoran_fh *fh = __fh;
2512         struct zoran *zr = fh->zr;
2513         int res = 0, codec_mode, buf_type;
2514
2515         mutex_lock(&zr->resource_lock);
2516
2517         switch (fh->map_mode) {
2518         case ZORAN_MAP_MODE_RAW:
2519                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2520                         dprintk(1, KERN_ERR
2521                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2522                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2523                         res = -EINVAL;
2524                         goto qbuf_unlock_and_return;
2525                 }
2526
2527                 res = zoran_v4l_queue_frame(file, buf->index);
2528                 if (res)
2529                         goto qbuf_unlock_and_return;
2530                 if (!zr->v4l_memgrab_active &&
2531                                 fh->v4l_buffers.active == ZORAN_LOCKED)
2532                         zr36057_set_memgrab(zr, 1);
2533                 break;
2534
2535         case ZORAN_MAP_MODE_JPG_REC:
2536         case ZORAN_MAP_MODE_JPG_PLAY:
2537                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2538                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2539                         codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2540                 } else {
2541                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2542                         codec_mode = BUZ_MODE_MOTION_COMPRESS;
2543                 }
2544
2545                 if (buf->type != buf_type) {
2546                         dprintk(1, KERN_ERR
2547                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2548                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2549                         res = -EINVAL;
2550                         goto qbuf_unlock_and_return;
2551                 }
2552
2553                 res = zoran_jpg_queue_frame(file, buf->index,
2554                                         codec_mode);
2555                 if (res != 0)
2556                         goto qbuf_unlock_and_return;
2557                 if (zr->codec_mode == BUZ_MODE_IDLE &&
2558                                 fh->jpg_buffers.active == ZORAN_LOCKED) {
2559                         zr36057_enable_jpg(zr, codec_mode);
2560                 }
2561                 break;
2562
2563         default:
2564                 dprintk(1, KERN_ERR
2565                         "%s: VIDIOC_QBUF - unsupported type %d\n",
2566                         ZR_DEVNAME(zr), buf->type);
2567                 res = -EINVAL;
2568                 break;
2569         }
2570 qbuf_unlock_and_return:
2571         mutex_unlock(&zr->resource_lock);
2572
2573         return res;
2574 }
2575
2576 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2577 {
2578         struct zoran_fh *fh = __fh;
2579         struct zoran *zr = fh->zr;
2580         int res = 0, buf_type, num = -1;        /* compiler borks here (?) */
2581
2582         mutex_lock(&zr->resource_lock);
2583
2584         switch (fh->map_mode) {
2585         case ZORAN_MAP_MODE_RAW:
2586                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2587                         dprintk(1, KERN_ERR
2588                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2589                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2590                         res = -EINVAL;
2591                         goto dqbuf_unlock_and_return;
2592                 }
2593
2594                 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2595                 if (file->f_flags & O_NONBLOCK &&
2596                     zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2597                         res = -EAGAIN;
2598                         goto dqbuf_unlock_and_return;
2599                 }
2600                 res = v4l_sync(file, num);
2601                 if (res)
2602                         goto dqbuf_unlock_and_return;
2603                 zr->v4l_sync_tail++;
2604                 res = zoran_v4l2_buffer_status(fh, buf, num);
2605                 break;
2606
2607         case ZORAN_MAP_MODE_JPG_REC:
2608         case ZORAN_MAP_MODE_JPG_PLAY:
2609         {
2610                 struct zoran_sync bs;
2611
2612                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2613                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2614                 else
2615                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2616
2617                 if (buf->type != buf_type) {
2618                         dprintk(1, KERN_ERR
2619                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2620                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2621                         res = -EINVAL;
2622                         goto dqbuf_unlock_and_return;
2623                 }
2624
2625                 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2626
2627                 if (file->f_flags & O_NONBLOCK &&
2628                     zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2629                         res = -EAGAIN;
2630                         goto dqbuf_unlock_and_return;
2631                 }
2632                 res = jpg_sync(file, &bs);
2633                 if (res)
2634                         goto dqbuf_unlock_and_return;
2635                 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2636                 break;
2637         }
2638
2639         default:
2640                 dprintk(1, KERN_ERR
2641                         "%s: VIDIOC_DQBUF - unsupported type %d\n",
2642                         ZR_DEVNAME(zr), buf->type);
2643                 res = -EINVAL;
2644                 break;
2645         }
2646 dqbuf_unlock_and_return:
2647         mutex_unlock(&zr->resource_lock);
2648
2649         return res;
2650 }
2651
2652 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2653 {
2654         struct zoran_fh *fh = __fh;
2655         struct zoran *zr = fh->zr;
2656         int res = 0;
2657
2658         mutex_lock(&zr->resource_lock);
2659
2660         switch (fh->map_mode) {
2661         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2662                 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2663                     fh->v4l_buffers.active != ZORAN_ACTIVE) {
2664                         res = -EBUSY;
2665                         goto strmon_unlock_and_return;
2666                 }
2667
2668                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2669                 zr->v4l_settings = fh->v4l_settings;
2670
2671                 zr->v4l_sync_tail = zr->v4l_pend_tail;
2672                 if (!zr->v4l_memgrab_active &&
2673                     zr->v4l_pend_head != zr->v4l_pend_tail) {
2674                         zr36057_set_memgrab(zr, 1);
2675                 }
2676                 break;
2677
2678         case ZORAN_MAP_MODE_JPG_REC:
2679         case ZORAN_MAP_MODE_JPG_PLAY:
2680                 /* what is the codec mode right now? */
2681                 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2682                     fh->jpg_buffers.active != ZORAN_ACTIVE) {
2683                         res = -EBUSY;
2684                         goto strmon_unlock_and_return;
2685                 }
2686
2687                 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2688
2689                 if (zr->jpg_que_head != zr->jpg_que_tail) {
2690                         /* Start the jpeg codec when the first frame is queued  */
2691                         jpeg_start(zr);
2692                 }
2693                 break;
2694
2695         default:
2696                 dprintk(1,
2697                         KERN_ERR
2698                         "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2699                         ZR_DEVNAME(zr), fh->map_mode);
2700                 res = -EINVAL;
2701                 break;
2702         }
2703 strmon_unlock_and_return:
2704         mutex_unlock(&zr->resource_lock);
2705
2706         return res;
2707 }
2708
2709 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2710 {
2711         struct zoran_fh *fh = __fh;
2712         struct zoran *zr = fh->zr;
2713         int i, res = 0;
2714
2715         mutex_lock(&zr->resource_lock);
2716
2717         switch (fh->map_mode) {
2718         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2719                 if (fh->v4l_buffers.active == ZORAN_FREE &&
2720                     zr->v4l_buffers.active != ZORAN_FREE) {
2721                         res = -EPERM;   /* stay off other's settings! */
2722                         goto strmoff_unlock_and_return;
2723                 }
2724                 if (zr->v4l_buffers.active == ZORAN_FREE)
2725                         goto strmoff_unlock_and_return;
2726
2727                 /* unload capture */
2728                 if (zr->v4l_memgrab_active) {
2729                         unsigned long flags;
2730
2731                         spin_lock_irqsave(&zr->spinlock, flags);
2732                         zr36057_set_memgrab(zr, 0);
2733                         spin_unlock_irqrestore(&zr->spinlock, flags);
2734                 }
2735
2736                 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2737                         zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2738                 fh->v4l_buffers = zr->v4l_buffers;
2739
2740                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2741
2742                 zr->v4l_grab_seq = 0;
2743                 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2744                 zr->v4l_sync_tail = 0;
2745
2746                 break;
2747
2748         case ZORAN_MAP_MODE_JPG_REC:
2749         case ZORAN_MAP_MODE_JPG_PLAY:
2750                 if (fh->jpg_buffers.active == ZORAN_FREE &&
2751                     zr->jpg_buffers.active != ZORAN_FREE) {
2752                         res = -EPERM;   /* stay off other's settings! */
2753                         goto strmoff_unlock_and_return;
2754                 }
2755                 if (zr->jpg_buffers.active == ZORAN_FREE)
2756                         goto strmoff_unlock_and_return;
2757
2758                 res = jpg_qbuf(file, -1,
2759                              (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2760                              BUZ_MODE_MOTION_COMPRESS :
2761                              BUZ_MODE_MOTION_DECOMPRESS);
2762                 if (res)
2763                         goto strmoff_unlock_and_return;
2764                 break;
2765         default:
2766                 dprintk(1, KERN_ERR
2767                         "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2768                         ZR_DEVNAME(zr), fh->map_mode);
2769                 res = -EINVAL;
2770                 break;
2771         }
2772 strmoff_unlock_and_return:
2773         mutex_unlock(&zr->resource_lock);
2774
2775         return res;
2776 }
2777
2778 static int zoran_queryctrl(struct file *file, void *__fh,
2779                                         struct v4l2_queryctrl *ctrl)
2780 {
2781         struct zoran_fh *fh = __fh;
2782         struct zoran *zr = fh->zr;
2783
2784         /* we only support hue/saturation/contrast/brightness */
2785         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2786             ctrl->id > V4L2_CID_HUE)
2787                 return -EINVAL;
2788
2789         decoder_call(zr, core, queryctrl, ctrl);
2790
2791         return 0;
2792 }
2793
2794 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2795 {
2796         struct zoran_fh *fh = __fh;
2797         struct zoran *zr = fh->zr;
2798
2799         /* we only support hue/saturation/contrast/brightness */
2800         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2801             ctrl->id > V4L2_CID_HUE)
2802                 return -EINVAL;
2803
2804         mutex_lock(&zr->resource_lock);
2805         decoder_call(zr, core, g_ctrl, ctrl);
2806         mutex_unlock(&zr->resource_lock);
2807
2808         return 0;
2809 }
2810
2811 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2812 {
2813         struct zoran_fh *fh = __fh;
2814         struct zoran *zr = fh->zr;
2815
2816         /* we only support hue/saturation/contrast/brightness */
2817         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2818             ctrl->id > V4L2_CID_HUE)
2819                 return -EINVAL;
2820
2821         mutex_lock(&zr->resource_lock);
2822         decoder_call(zr, core, s_ctrl, ctrl);
2823         mutex_unlock(&zr->resource_lock);
2824
2825         return 0;
2826 }
2827
2828 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2829 {
2830         struct zoran_fh *fh = __fh;
2831         struct zoran *zr = fh->zr;
2832
2833         mutex_lock(&zr->resource_lock);
2834         *std = zr->norm;
2835         mutex_unlock(&zr->resource_lock);
2836         return 0;
2837 }
2838
2839 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2840 {
2841         struct zoran_fh *fh = __fh;
2842         struct zoran *zr = fh->zr;
2843         int res = 0;
2844
2845         mutex_lock(&zr->resource_lock);
2846         res = zoran_set_norm(zr, *std);
2847         if (res)
2848                 goto sstd_unlock_and_return;
2849
2850         res = wait_grab_pending(zr);
2851 sstd_unlock_and_return:
2852         mutex_unlock(&zr->resource_lock);
2853         return res;
2854 }
2855
2856 static int zoran_enum_input(struct file *file, void *__fh,
2857                                  struct v4l2_input *inp)
2858 {
2859         struct zoran_fh *fh = __fh;
2860         struct zoran *zr = fh->zr;
2861
2862         if (inp->index < 0 || inp->index >= zr->card.inputs)
2863                 return -EINVAL;
2864         else {
2865                 int id = inp->index;
2866                 memset(inp, 0, sizeof(*inp));
2867                 inp->index = id;
2868         }
2869
2870         strncpy(inp->name, zr->card.input[inp->index].name,
2871                 sizeof(inp->name) - 1);
2872         inp->type = V4L2_INPUT_TYPE_CAMERA;
2873         inp->std = V4L2_STD_ALL;
2874
2875         /* Get status of video decoder */
2876         mutex_lock(&zr->resource_lock);
2877         decoder_call(zr, video, g_input_status, &inp->status);
2878         mutex_unlock(&zr->resource_lock);
2879         return 0;
2880 }
2881
2882 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2883 {
2884         struct zoran_fh *fh = __fh;
2885         struct zoran *zr = fh->zr;
2886
2887         mutex_lock(&zr->resource_lock);
2888         *input = zr->input;
2889         mutex_unlock(&zr->resource_lock);
2890
2891         return 0;
2892 }
2893
2894 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2895 {
2896         struct zoran_fh *fh = __fh;
2897         struct zoran *zr = fh->zr;
2898         int res;
2899
2900         mutex_lock(&zr->resource_lock);
2901         res = zoran_set_input(zr, input);
2902         if (res)
2903                 goto sinput_unlock_and_return;
2904
2905         /* Make sure the changes come into effect */
2906         res = wait_grab_pending(zr);
2907 sinput_unlock_and_return:
2908         mutex_unlock(&zr->resource_lock);
2909         return res;
2910 }
2911
2912 static int zoran_enum_output(struct file *file, void *__fh,
2913                                   struct v4l2_output *outp)
2914 {
2915         if (outp->index != 0)
2916                 return -EINVAL;
2917
2918         memset(outp, 0, sizeof(*outp));
2919         outp->index = 0;
2920         outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2921         strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2922
2923         return 0;
2924 }
2925
2926 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2927 {
2928         *output = 0;
2929
2930         return 0;
2931 }
2932
2933 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2934 {
2935         if (output != 0)
2936                 return -EINVAL;
2937
2938         return 0;
2939 }
2940
2941 /* cropping (sub-frame capture) */
2942 static int zoran_cropcap(struct file *file, void *__fh,
2943                                         struct v4l2_cropcap *cropcap)
2944 {
2945         struct zoran_fh *fh = __fh;
2946         struct zoran *zr = fh->zr;
2947         int type = cropcap->type, res = 0;
2948
2949         memset(cropcap, 0, sizeof(*cropcap));
2950         cropcap->type = type;
2951
2952         mutex_lock(&zr->resource_lock);
2953
2954         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2955             (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2956              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2957                 dprintk(1, KERN_ERR
2958                         "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2959                         ZR_DEVNAME(zr));
2960                 res = -EINVAL;
2961                 goto cropcap_unlock_and_return;
2962         }
2963
2964         cropcap->bounds.top = cropcap->bounds.left = 0;
2965         cropcap->bounds.width = BUZ_MAX_WIDTH;
2966         cropcap->bounds.height = BUZ_MAX_HEIGHT;
2967         cropcap->defrect.top = cropcap->defrect.left = 0;
2968         cropcap->defrect.width = BUZ_MIN_WIDTH;
2969         cropcap->defrect.height = BUZ_MIN_HEIGHT;
2970 cropcap_unlock_and_return:
2971         mutex_unlock(&zr->resource_lock);
2972         return res;
2973 }
2974
2975 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2976 {
2977         struct zoran_fh *fh = __fh;
2978         struct zoran *zr = fh->zr;
2979         int type = crop->type, res = 0;
2980
2981         memset(crop, 0, sizeof(*crop));
2982         crop->type = type;
2983
2984         mutex_lock(&zr->resource_lock);
2985
2986         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2987             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2988              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2989                 dprintk(1,
2990                         KERN_ERR
2991                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2992                         ZR_DEVNAME(zr));
2993                 res = -EINVAL;
2994                 goto gcrop_unlock_and_return;
2995         }
2996
2997         crop->c.top = fh->jpg_settings.img_y;
2998         crop->c.left = fh->jpg_settings.img_x;
2999         crop->c.width = fh->jpg_settings.img_width;
3000         crop->c.height = fh->jpg_settings.img_height;
3001
3002 gcrop_unlock_and_return:
3003         mutex_unlock(&zr->resource_lock);
3004
3005         return res;
3006 }
3007
3008 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3009 {
3010         struct zoran_fh *fh = __fh;
3011         struct zoran *zr = fh->zr;
3012         int res = 0;
3013         struct zoran_jpg_settings settings;
3014
3015         settings = fh->jpg_settings;
3016
3017         mutex_lock(&zr->resource_lock);
3018
3019         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3020                 dprintk(1, KERN_ERR
3021                         "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3022                         ZR_DEVNAME(zr));
3023                 res = -EBUSY;
3024                 goto scrop_unlock_and_return;
3025         }
3026
3027         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3028             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3029              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3030                 dprintk(1, KERN_ERR
3031                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3032                         ZR_DEVNAME(zr));
3033                 res = -EINVAL;
3034                 goto scrop_unlock_and_return;
3035         }
3036
3037         /* move into a form that we understand */
3038         settings.img_x = crop->c.left;
3039         settings.img_y = crop->c.top;
3040         settings.img_width = crop->c.width;
3041         settings.img_height = crop->c.height;
3042
3043         /* check validity */
3044         res = zoran_check_jpg_settings(zr, &settings, 0);
3045         if (res)
3046                 goto scrop_unlock_and_return;
3047
3048         /* accept */
3049         fh->jpg_settings = settings;
3050
3051 scrop_unlock_and_return:
3052         mutex_unlock(&zr->resource_lock);
3053         return res;
3054 }
3055
3056 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3057                                         struct v4l2_jpegcompression *params)
3058 {
3059         struct zoran_fh *fh = __fh;
3060         struct zoran *zr = fh->zr;
3061         memset(params, 0, sizeof(*params));
3062
3063         mutex_lock(&zr->resource_lock);
3064
3065         params->quality = fh->jpg_settings.jpg_comp.quality;
3066         params->APPn = fh->jpg_settings.jpg_comp.APPn;
3067         memcpy(params->APP_data,
3068                fh->jpg_settings.jpg_comp.APP_data,
3069                fh->jpg_settings.jpg_comp.APP_len);
3070         params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3071         memcpy(params->COM_data,
3072                fh->jpg_settings.jpg_comp.COM_data,
3073                fh->jpg_settings.jpg_comp.COM_len);
3074         params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3075         params->jpeg_markers =
3076             fh->jpg_settings.jpg_comp.jpeg_markers;
3077
3078         mutex_unlock(&zr->resource_lock);
3079
3080         return 0;
3081 }
3082
3083 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3084                                         struct v4l2_jpegcompression *params)
3085 {
3086         struct zoran_fh *fh = __fh;
3087         struct zoran *zr = fh->zr;
3088         int res = 0;
3089         struct zoran_jpg_settings settings;
3090
3091         settings = fh->jpg_settings;
3092
3093         settings.jpg_comp = *params;
3094
3095         mutex_lock(&zr->resource_lock);
3096
3097         if (fh->v4l_buffers.active != ZORAN_FREE ||
3098             fh->jpg_buffers.active != ZORAN_FREE) {
3099                 dprintk(1, KERN_WARNING
3100                         "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3101                         ZR_DEVNAME(zr));
3102                 res = -EBUSY;
3103                 goto sjpegc_unlock_and_return;
3104         }
3105
3106         res = zoran_check_jpg_settings(zr, &settings, 0);
3107         if (res)
3108                 goto sjpegc_unlock_and_return;
3109         if (!fh->jpg_buffers.allocated)
3110                 fh->jpg_buffers.buffer_size =
3111                     zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3112         fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3113 sjpegc_unlock_and_return:
3114         mutex_unlock(&zr->resource_lock);
3115
3116         return res;
3117 }
3118
3119 static unsigned int
3120 zoran_poll (struct file *file,
3121             poll_table  *wait)
3122 {
3123         struct zoran_fh *fh = file->private_data;
3124         struct zoran *zr = fh->zr;
3125         int res = 0, frame;
3126         unsigned long flags;
3127
3128         /* we should check whether buffers are ready to be synced on
3129          * (w/o waits - O_NONBLOCK) here
3130          * if ready for read (sync), return POLLIN|POLLRDNORM,
3131          * if ready for write (sync), return POLLOUT|POLLWRNORM,
3132          * if error, return POLLERR,
3133          * if no buffers queued or so, return POLLNVAL
3134          */
3135
3136         mutex_lock(&zr->resource_lock);
3137
3138         switch (fh->map_mode) {
3139         case ZORAN_MAP_MODE_RAW:
3140                 poll_wait(file, &zr->v4l_capq, wait);
3141                 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3142
3143                 spin_lock_irqsave(&zr->spinlock, flags);
3144                 dprintk(3,
3145                         KERN_DEBUG
3146                         "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3147                         ZR_DEVNAME(zr), __func__,
3148                         "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3149                         "UPMD"[zr->v4l_buffers.buffer[frame].state],
3150                         zr->v4l_pend_tail, zr->v4l_pend_head);
3151                 /* Process is the one capturing? */
3152                 if (fh->v4l_buffers.active != ZORAN_FREE &&
3153                     /* Buffer ready to DQBUF? */
3154                     zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3155                         res = POLLIN | POLLRDNORM;
3156                 spin_unlock_irqrestore(&zr->spinlock, flags);
3157
3158                 break;
3159
3160         case ZORAN_MAP_MODE_JPG_REC:
3161         case ZORAN_MAP_MODE_JPG_PLAY:
3162                 poll_wait(file, &zr->jpg_capq, wait);
3163                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3164
3165                 spin_lock_irqsave(&zr->spinlock, flags);
3166                 dprintk(3,
3167                         KERN_DEBUG
3168                         "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3169                         ZR_DEVNAME(zr), __func__,
3170                         "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3171                         "UPMD"[zr->jpg_buffers.buffer[frame].state],
3172                         zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3173                 if (fh->jpg_buffers.active != ZORAN_FREE &&
3174                     zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3175                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3176                                 res = POLLIN | POLLRDNORM;
3177                         else
3178                                 res = POLLOUT | POLLWRNORM;
3179                 }
3180                 spin_unlock_irqrestore(&zr->spinlock, flags);
3181
3182                 break;
3183
3184         default:
3185                 dprintk(1,
3186                         KERN_ERR
3187                         "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3188                         ZR_DEVNAME(zr), fh->map_mode);
3189                 res = POLLNVAL;
3190         }
3191
3192         mutex_unlock(&zr->resource_lock);
3193
3194         return res;
3195 }
3196
3197
3198 /*
3199  * This maps the buffers to user space.
3200  *
3201  * Depending on the state of fh->map_mode
3202  * the V4L or the MJPEG buffers are mapped
3203  * per buffer or all together
3204  *
3205  * Note that we need to connect to some
3206  * unmap signal event to unmap the de-allocate
3207  * the buffer accordingly (zoran_vm_close())
3208  */
3209
3210 static void
3211 zoran_vm_open (struct vm_area_struct *vma)
3212 {
3213         struct zoran_mapping *map = vma->vm_private_data;
3214
3215         map->count++;
3216 }
3217
3218 static void
3219 zoran_vm_close (struct vm_area_struct *vma)
3220 {
3221         struct zoran_mapping *map = vma->vm_private_data;
3222         struct file *file = map->file;
3223         struct zoran_fh *fh = file->private_data;
3224         struct zoran *zr = fh->zr;
3225         int i;
3226
3227         map->count--;
3228         if (map->count == 0) {
3229                 switch (fh->map_mode) {
3230                 case ZORAN_MAP_MODE_JPG_REC:
3231                 case ZORAN_MAP_MODE_JPG_PLAY:
3232
3233                         dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3234                                 ZR_DEVNAME(zr));
3235
3236                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3237                                 if (fh->jpg_buffers.buffer[i].map == map) {
3238                                         fh->jpg_buffers.buffer[i].map =
3239                                             NULL;
3240                                 }
3241                         }
3242                         kfree(map);
3243
3244                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3245                                 if (fh->jpg_buffers.buffer[i].map)
3246                                         break;
3247                         if (i == fh->jpg_buffers.num_buffers) {
3248                                 mutex_lock(&zr->resource_lock);
3249
3250                                 if (fh->jpg_buffers.active != ZORAN_FREE) {
3251                                         jpg_qbuf(file, -1, zr->codec_mode);
3252                                         zr->jpg_buffers.allocated = 0;
3253                                         zr->jpg_buffers.active =
3254                                             fh->jpg_buffers.active =
3255                                             ZORAN_FREE;
3256                                 }
3257                                 jpg_fbuffer_free(file);
3258                                 mutex_unlock(&zr->resource_lock);
3259                         }
3260
3261                         break;
3262
3263                 case ZORAN_MAP_MODE_RAW:
3264
3265                         dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3266                                 ZR_DEVNAME(zr));
3267
3268                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3269                                 if (fh->v4l_buffers.buffer[i].map == map) {
3270                                         /* unqueue/unmap */
3271                                         fh->v4l_buffers.buffer[i].map =
3272                                             NULL;
3273                                 }
3274                         }
3275                         kfree(map);
3276
3277                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3278                                 if (fh->v4l_buffers.buffer[i].map)
3279                                         break;
3280                         if (i == fh->v4l_buffers.num_buffers) {
3281                                 mutex_lock(&zr->resource_lock);
3282
3283                                 if (fh->v4l_buffers.active != ZORAN_FREE) {
3284                                         unsigned long flags;
3285
3286                                         spin_lock_irqsave(&zr->spinlock, flags);
3287                                         zr36057_set_memgrab(zr, 0);
3288                                         zr->v4l_buffers.allocated = 0;
3289                                         zr->v4l_buffers.active =
3290                                             fh->v4l_buffers.active =
3291                                             ZORAN_FREE;
3292                                         spin_unlock_irqrestore(&zr->spinlock, flags);
3293                                 }
3294                                 v4l_fbuffer_free(file);
3295                                 mutex_unlock(&zr->resource_lock);
3296                         }
3297
3298                         break;
3299
3300                 default:
3301                         printk(KERN_ERR
3302                                "%s: munmap() - internal error - unknown map mode %d\n",
3303                                ZR_DEVNAME(zr), fh->map_mode);
3304                         break;
3305
3306                 }
3307         }
3308 }
3309
3310 static struct vm_operations_struct zoran_vm_ops = {
3311         .open = zoran_vm_open,
3312         .close = zoran_vm_close,
3313 };
3314
3315 static int
3316 zoran_mmap (struct file           *file,
3317             struct vm_area_struct *vma)
3318 {
3319         struct zoran_fh *fh = file->private_data;
3320         struct zoran *zr = fh->zr;
3321         unsigned long size = (vma->vm_end - vma->vm_start);
3322         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3323         int i, j;
3324         unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3325         int first, last;
3326         struct zoran_mapping *map;
3327         int res = 0;
3328
3329         dprintk(3,
3330                 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3331                 ZR_DEVNAME(zr),
3332                 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3333                 vma->vm_start, vma->vm_end, size);
3334
3335         if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3336             !(vma->vm_flags & VM_WRITE)) {
3337                 dprintk(1,
3338                         KERN_ERR
3339                         "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3340                         ZR_DEVNAME(zr));
3341                 return -EINVAL;
3342         }
3343
3344         switch (fh->map_mode) {
3345
3346         case ZORAN_MAP_MODE_JPG_REC:
3347         case ZORAN_MAP_MODE_JPG_PLAY:
3348
3349                 /* lock */
3350                 mutex_lock(&zr->resource_lock);
3351
3352                 /* Map the MJPEG buffers */
3353                 if (!fh->jpg_buffers.allocated) {
3354                         dprintk(1,
3355                                 KERN_ERR
3356                                 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3357                                 ZR_DEVNAME(zr));
3358                         res = -ENOMEM;
3359                         goto jpg_mmap_unlock_and_return;
3360                 }
3361
3362                 first = offset / fh->jpg_buffers.buffer_size;
3363                 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3364                 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3365                     size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3366                     last < 0 || first >= fh->jpg_buffers.num_buffers ||
3367                     last >= fh->jpg_buffers.num_buffers) {
3368                         dprintk(1,
3369                                 KERN_ERR
3370                                 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3371                                 ZR_DEVNAME(zr), offset, size,
3372                                 fh->jpg_buffers.buffer_size,
3373                                 fh->jpg_buffers.num_buffers);
3374                         res = -EINVAL;
3375                         goto jpg_mmap_unlock_and_return;
3376                 }
3377                 for (i = first; i <= last; i++) {
3378                         if (fh->jpg_buffers.buffer[i].map) {
3379                                 dprintk(1,
3380                                         KERN_ERR
3381                                         "%s: mmap(MJPEG) - buffer %d already mapped\n",
3382                                         ZR_DEVNAME(zr), i);
3383                                 res = -EBUSY;
3384                                 goto jpg_mmap_unlock_and_return;
3385                         }
3386                 }
3387
3388                 /* map these buffers (v4l_buffers[i]) */
3389                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3390                 if (!map) {
3391                         res = -ENOMEM;
3392                         goto jpg_mmap_unlock_and_return;
3393                 }
3394                 map->file = file;
3395                 map->count = 1;
3396
3397                 vma->vm_ops = &zoran_vm_ops;
3398                 vma->vm_flags |= VM_DONTEXPAND;
3399                 vma->vm_private_data = map;
3400
3401                 for (i = first; i <= last; i++) {
3402                         for (j = 0;
3403                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3404                              j++) {
3405                                 fraglen =
3406                                     (le32_to_cpu(fh->jpg_buffers.buffer[i].
3407                                      frag_tab[2 * j + 1]) & ~1) << 1;
3408                                 todo = size;
3409                                 if (todo > fraglen)
3410                                         todo = fraglen;
3411                                 pos =
3412                                     le32_to_cpu(fh->jpg_buffers.
3413                                     buffer[i].frag_tab[2 * j]);
3414                                 /* should just be pos on i386 */
3415                                 page = virt_to_phys(bus_to_virt(pos))
3416                                                                 >> PAGE_SHIFT;
3417                                 if (remap_pfn_range(vma, start, page,
3418                                                         todo, PAGE_SHARED)) {
3419                                         dprintk(1,
3420                                                 KERN_ERR
3421                                                 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3422                                                 ZR_DEVNAME(zr));
3423                                         res = -EAGAIN;
3424                                         goto jpg_mmap_unlock_and_return;
3425                                 }
3426                                 size -= todo;
3427                                 start += todo;
3428                                 if (size == 0)
3429                                         break;
3430                                 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3431                                     frag_tab[2 * j + 1]) & 1)
3432                                         break;  /* was last fragment */
3433                         }
3434                         fh->jpg_buffers.buffer[i].map = map;
3435                         if (size == 0)
3436                                 break;
3437
3438                 }
3439         jpg_mmap_unlock_and_return:
3440                 mutex_unlock(&zr->resource_lock);
3441
3442                 break;
3443
3444         case ZORAN_MAP_MODE_RAW:
3445
3446                 mutex_lock(&zr->resource_lock);
3447
3448                 /* Map the V4L buffers */
3449                 if (!fh->v4l_buffers.allocated) {
3450                         dprintk(1,
3451                                 KERN_ERR
3452                                 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3453                                 ZR_DEVNAME(zr));
3454                         res = -ENOMEM;
3455                         goto v4l_mmap_unlock_and_return;
3456                 }
3457
3458                 first = offset / fh->v4l_buffers.buffer_size;
3459                 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3460                 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3461                     size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3462                     last < 0 || first >= fh->v4l_buffers.num_buffers ||
3463                     last >= fh->v4l_buffers.buffer_size) {
3464                         dprintk(1,
3465                                 KERN_ERR
3466                                 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3467                                 ZR_DEVNAME(zr), offset, size,
3468                                 fh->v4l_buffers.buffer_size,
3469                                 fh->v4l_buffers.num_buffers);
3470                         res = -EINVAL;
3471                         goto v4l_mmap_unlock_and_return;
3472                 }
3473                 for (i = first; i <= last; i++) {
3474                         if (fh->v4l_buffers.buffer[i].map) {
3475                                 dprintk(1,
3476                                         KERN_ERR
3477                                         "%s: mmap(V4L) - buffer %d already mapped\n",
3478                                         ZR_DEVNAME(zr), i);
3479                                 res = -EBUSY;
3480                                 goto v4l_mmap_unlock_and_return;
3481                         }
3482                 }
3483
3484                 /* map these buffers (v4l_buffers[i]) */
3485                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3486                 if (!map) {
3487                         res = -ENOMEM;
3488                         goto v4l_mmap_unlock_and_return;
3489                 }
3490                 map->file = file;
3491                 map->count = 1;
3492
3493                 vma->vm_ops = &zoran_vm_ops;
3494                 vma->vm_flags |= VM_DONTEXPAND;
3495                 vma->vm_private_data = map;
3496
3497                 for (i = first; i <= last; i++) {
3498                         todo = size;
3499                         if (todo > fh->v4l_buffers.buffer_size)
3500                                 todo = fh->v4l_buffers.buffer_size;
3501                         page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3502                         if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3503                                                         todo, PAGE_SHARED)) {
3504                                 dprintk(1,
3505                                         KERN_ERR
3506                                         "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3507                                         ZR_DEVNAME(zr));
3508                                 res = -EAGAIN;
3509                                 goto v4l_mmap_unlock_and_return;
3510                         }
3511                         size -= todo;
3512                         start += todo;
3513                         fh->v4l_buffers.buffer[i].map = map;
3514                         if (size == 0)
3515                                 break;
3516                 }
3517         v4l_mmap_unlock_and_return:
3518                 mutex_unlock(&zr->resource_lock);
3519
3520                 break;
3521
3522         default:
3523                 dprintk(1,
3524                         KERN_ERR
3525                         "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3526                         ZR_DEVNAME(zr), fh->map_mode);
3527                 break;
3528         }
3529
3530         return 0;
3531 }
3532
3533 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3534         .vidioc_querycap                    = zoran_querycap,
3535         .vidioc_cropcap                     = zoran_cropcap,
3536         .vidioc_s_crop                      = zoran_s_crop,
3537         .vidioc_g_crop                      = zoran_g_crop,
3538         .vidioc_enum_input                  = zoran_enum_input,
3539         .vidioc_g_input                     = zoran_g_input,
3540         .vidioc_s_input                     = zoran_s_input,
3541         .vidioc_enum_output                 = zoran_enum_output,
3542         .vidioc_g_output                    = zoran_g_output,
3543         .vidioc_s_output                    = zoran_s_output,
3544         .vidioc_g_fbuf                      = zoran_g_fbuf,
3545         .vidioc_s_fbuf                      = zoran_s_fbuf,
3546         .vidioc_g_std                       = zoran_g_std,
3547         .vidioc_s_std                       = zoran_s_std,
3548         .vidioc_g_jpegcomp                  = zoran_g_jpegcomp,
3549         .vidioc_s_jpegcomp                  = zoran_s_jpegcomp,
3550         .vidioc_overlay                     = zoran_overlay,
3551         .vidioc_reqbufs                     = zoran_reqbufs,
3552         .vidioc_querybuf                    = zoran_querybuf,
3553         .vidioc_qbuf                        = zoran_qbuf,
3554         .vidioc_dqbuf                       = zoran_dqbuf,
3555         .vidioc_streamon                    = zoran_streamon,
3556         .vidioc_streamoff                   = zoran_streamoff,
3557         .vidioc_enum_fmt_vid_cap            = zoran_enum_fmt_vid_cap,
3558         .vidioc_enum_fmt_vid_out            = zoran_enum_fmt_vid_out,
3559         .vidioc_enum_fmt_vid_overlay        = zoran_enum_fmt_vid_overlay,
3560         .vidioc_g_fmt_vid_cap               = zoran_g_fmt_vid_cap,
3561         .vidioc_g_fmt_vid_out               = zoran_g_fmt_vid_out,
3562         .vidioc_g_fmt_vid_overlay           = zoran_g_fmt_vid_overlay,
3563         .vidioc_s_fmt_vid_cap               = zoran_s_fmt_vid_cap,
3564         .vidioc_s_fmt_vid_out               = zoran_s_fmt_vid_out,
3565         .vidioc_s_fmt_vid_overlay           = zoran_s_fmt_vid_overlay,
3566         .vidioc_try_fmt_vid_cap             = zoran_try_fmt_vid_cap,
3567         .vidioc_try_fmt_vid_out             = zoran_try_fmt_vid_out,
3568         .vidioc_try_fmt_vid_overlay         = zoran_try_fmt_vid_overlay,
3569         .vidioc_queryctrl                   = zoran_queryctrl,
3570         .vidioc_s_ctrl                      = zoran_s_ctrl,
3571         .vidioc_g_ctrl                      = zoran_g_ctrl,
3572 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3573         .vidioc_default                     = zoran_default,
3574         .vidiocgmbuf                        = zoran_vidiocgmbuf,
3575 #endif
3576 };
3577
3578 static const struct v4l2_file_operations zoran_fops = {
3579         .owner = THIS_MODULE,
3580         .open = zoran_open,
3581         .release = zoran_close,
3582         .ioctl = video_ioctl2,
3583         .read = zoran_read,
3584         .write = zoran_write,
3585         .mmap = zoran_mmap,
3586         .poll = zoran_poll,
3587 };
3588
3589 struct video_device zoran_template __devinitdata = {
3590         .name = ZORAN_NAME,
3591         .fops = &zoran_fops,
3592         .ioctl_ops = &zoran_ioctl_ops,
3593         .release = &zoran_vdev_release,
3594         .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3595         .minor = -1
3596 };
3597