]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/zoran/zoran_driver.c
V4L/DVB (10794): v4l2: Move code to zero querybuf output struct to v4l2_ioctl
[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/videodev2.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 file        *file,
1381                           struct v4l2_buffer *buf,
1382                           int                 num)
1383 {
1384         struct zoran_fh *fh = file->private_data;
1385         struct zoran *zr = fh->zr;
1386
1387         buf->flags = V4L2_BUF_FLAG_MAPPED;
1388
1389         switch (fh->map_mode) {
1390         case ZORAN_MAP_MODE_RAW:
1391
1392                 /* check range */
1393                 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1394                     !fh->v4l_buffers.allocated) {
1395                         dprintk(1,
1396                                 KERN_ERR
1397                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1398                                 ZR_DEVNAME(zr));
1399                         return -EINVAL;
1400                 }
1401
1402                 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1403                 buf->length = fh->v4l_buffers.buffer_size;
1404
1405                 /* get buffer */
1406                 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1407                 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1408                     fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1409                         buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1410                         buf->flags |= V4L2_BUF_FLAG_DONE;
1411                         buf->timestamp =
1412                             fh->v4l_buffers.buffer[num].bs.timestamp;
1413                 } else {
1414                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1415                 }
1416
1417                 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1418                         buf->field = V4L2_FIELD_TOP;
1419                 else
1420                         buf->field = V4L2_FIELD_INTERLACED;
1421
1422                 break;
1423
1424         case ZORAN_MAP_MODE_JPG_REC:
1425         case ZORAN_MAP_MODE_JPG_PLAY:
1426
1427                 /* check range */
1428                 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1429                     !fh->jpg_buffers.allocated) {
1430                         dprintk(1,
1431                                 KERN_ERR
1432                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1433                                 ZR_DEVNAME(zr));
1434                         return -EINVAL;
1435                 }
1436
1437                 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1438                               V4L2_BUF_TYPE_VIDEO_CAPTURE :
1439                               V4L2_BUF_TYPE_VIDEO_OUTPUT;
1440                 buf->length = fh->jpg_buffers.buffer_size;
1441
1442                 /* these variables are only written after frame has been captured */
1443                 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1444                     fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1445                         buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1446                         buf->timestamp =
1447                             fh->jpg_buffers.buffer[num].bs.timestamp;
1448                         buf->bytesused =
1449                             fh->jpg_buffers.buffer[num].bs.length;
1450                         buf->flags |= V4L2_BUF_FLAG_DONE;
1451                 } else {
1452                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1453                 }
1454
1455                 /* which fields are these? */
1456                 if (fh->jpg_settings.TmpDcm != 1)
1457                         buf->field =
1458                             fh->jpg_settings.
1459                             odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1460                 else
1461                         buf->field =
1462                             fh->jpg_settings.
1463                             odd_even ? V4L2_FIELD_SEQ_TB :
1464                             V4L2_FIELD_SEQ_BT;
1465
1466                 break;
1467
1468         default:
1469
1470                 dprintk(5,
1471                         KERN_ERR
1472                         "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1473                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
1474                 return -EINVAL;
1475         }
1476
1477         buf->memory = V4L2_MEMORY_MMAP;
1478         buf->index = num;
1479         buf->m.offset = buf->length * num;
1480
1481         return 0;
1482 }
1483
1484 static int
1485 zoran_set_norm (struct zoran *zr,
1486                 v4l2_std_id norm)
1487 {
1488         int on;
1489
1490         if (zr->v4l_buffers.active != ZORAN_FREE ||
1491             zr->jpg_buffers.active != ZORAN_FREE) {
1492                 dprintk(1,
1493                         KERN_WARNING
1494                         "%s: set_norm() called while in playback/capture mode\n",
1495                         ZR_DEVNAME(zr));
1496                 return -EBUSY;
1497         }
1498
1499         if (lock_norm && norm != zr->norm) {
1500                 if (lock_norm > 1) {
1501                         dprintk(1,
1502                                 KERN_WARNING
1503                                 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1504                                 ZR_DEVNAME(zr));
1505                         return -EPERM;
1506                 } else {
1507                         dprintk(1,
1508                                 KERN_WARNING
1509                                 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1510                                 ZR_DEVNAME(zr));
1511                         norm = zr->norm;
1512                 }
1513         }
1514
1515         if (!(norm & zr->card.norms)) {
1516                 dprintk(1,
1517                         KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1518                         ZR_DEVNAME(zr), norm);
1519                 return -EINVAL;
1520         }
1521
1522         if (norm == V4L2_STD_ALL) {
1523                 int status = 0;
1524                 v4l2_std_id std = 0;
1525
1526                 decoder_call(zr, video, querystd, &std);
1527                 decoder_s_std(zr, std);
1528
1529                 /* let changes come into effect */
1530                 ssleep(2);
1531
1532                 decoder_call(zr, video, g_input_status, &status);
1533                 if (status & V4L2_IN_ST_NO_SIGNAL) {
1534                         dprintk(1,
1535                                 KERN_ERR
1536                                 "%s: set_norm() - no norm detected\n",
1537                                 ZR_DEVNAME(zr));
1538                         /* reset norm */
1539                         decoder_s_std(zr, zr->norm);
1540                         return -EIO;
1541                 }
1542
1543                 norm = std;
1544         }
1545         if (norm & V4L2_STD_SECAM)
1546                 zr->timing = zr->card.tvn[2];
1547         else if (norm & V4L2_STD_NTSC)
1548                 zr->timing = zr->card.tvn[1];
1549         else
1550                 zr->timing = zr->card.tvn[0];
1551
1552         /* We switch overlay off and on since a change in the
1553          * norm needs different VFE settings */
1554         on = zr->overlay_active && !zr->v4l_memgrab_active;
1555         if (on)
1556                 zr36057_overlay(zr, 0);
1557
1558         decoder_s_std(zr, norm);
1559         encoder_call(zr, video, s_std_output, norm);
1560
1561         if (on)
1562                 zr36057_overlay(zr, 1);
1563
1564         /* Make sure the changes come into effect */
1565         zr->norm = norm;
1566
1567         return 0;
1568 }
1569
1570 static int
1571 zoran_set_input (struct zoran *zr,
1572                  int           input)
1573 {
1574         struct v4l2_routing route = { 0, 0 };
1575
1576         if (input == zr->input) {
1577                 return 0;
1578         }
1579
1580         if (zr->v4l_buffers.active != ZORAN_FREE ||
1581             zr->jpg_buffers.active != ZORAN_FREE) {
1582                 dprintk(1,
1583                         KERN_WARNING
1584                         "%s: set_input() called while in playback/capture mode\n",
1585                         ZR_DEVNAME(zr));
1586                 return -EBUSY;
1587         }
1588
1589         if (input < 0 || input >= zr->card.inputs) {
1590                 dprintk(1,
1591                         KERN_ERR
1592                         "%s: set_input() - unnsupported input %d\n",
1593                         ZR_DEVNAME(zr), input);
1594                 return -EINVAL;
1595         }
1596
1597         route.input = zr->card.input[input].muxsel;
1598         zr->input = input;
1599
1600         decoder_s_routing(zr, &route);
1601
1602         return 0;
1603 }
1604
1605 /*
1606  *   ioctl routine
1607  */
1608
1609 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1610 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1611 {
1612         struct zoran_fh *fh = __fh;
1613         struct zoran *zr = fh->zr;
1614         struct zoran_jpg_settings settings;
1615
1616         switch (cmd) {
1617         case BUZIOC_G_PARAMS:
1618         {
1619                 struct zoran_params *bparams = arg;
1620
1621                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1622
1623                 memset(bparams, 0, sizeof(struct zoran_params));
1624                 bparams->major_version = MAJOR_VERSION;
1625                 bparams->minor_version = MINOR_VERSION;
1626
1627                 mutex_lock(&zr->resource_lock);
1628
1629                 if (zr->norm & V4L2_STD_NTSC)
1630                         bparams->norm = VIDEO_MODE_NTSC;
1631                 else if (zr->norm & V4L2_STD_PAL)
1632                         bparams->norm = VIDEO_MODE_PAL;
1633                 else
1634                         bparams->norm = VIDEO_MODE_SECAM;
1635
1636                 bparams->input = zr->input;
1637
1638                 bparams->decimation = fh->jpg_settings.decimation;
1639                 bparams->HorDcm = fh->jpg_settings.HorDcm;
1640                 bparams->VerDcm = fh->jpg_settings.VerDcm;
1641                 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1642                 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1643                 bparams->img_x = fh->jpg_settings.img_x;
1644                 bparams->img_y = fh->jpg_settings.img_y;
1645                 bparams->img_width = fh->jpg_settings.img_width;
1646                 bparams->img_height = fh->jpg_settings.img_height;
1647                 bparams->odd_even = fh->jpg_settings.odd_even;
1648
1649                 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1650                 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1651                 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1652                 memcpy(bparams->APP_data,
1653                        fh->jpg_settings.jpg_comp.APP_data,
1654                        sizeof(bparams->APP_data));
1655                 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1656                 memcpy(bparams->COM_data,
1657                        fh->jpg_settings.jpg_comp.COM_data,
1658                        sizeof(bparams->COM_data));
1659                 bparams->jpeg_markers =
1660                     fh->jpg_settings.jpg_comp.jpeg_markers;
1661
1662                 mutex_unlock(&zr->resource_lock);
1663
1664                 bparams->VFIFO_FB = 0;
1665
1666                 return 0;
1667         }
1668
1669         case BUZIOC_S_PARAMS:
1670         {
1671                 struct zoran_params *bparams = arg;
1672                 int res = 0;
1673
1674                 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1675
1676                 settings.decimation = bparams->decimation;
1677                 settings.HorDcm = bparams->HorDcm;
1678                 settings.VerDcm = bparams->VerDcm;
1679                 settings.TmpDcm = bparams->TmpDcm;
1680                 settings.field_per_buff = bparams->field_per_buff;
1681                 settings.img_x = bparams->img_x;
1682                 settings.img_y = bparams->img_y;
1683                 settings.img_width = bparams->img_width;
1684                 settings.img_height = bparams->img_height;
1685                 settings.odd_even = bparams->odd_even;
1686
1687                 settings.jpg_comp.quality = bparams->quality;
1688                 settings.jpg_comp.APPn = bparams->APPn;
1689                 settings.jpg_comp.APP_len = bparams->APP_len;
1690                 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1691                        sizeof(bparams->APP_data));
1692                 settings.jpg_comp.COM_len = bparams->COM_len;
1693                 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1694                        sizeof(bparams->COM_data));
1695                 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1696
1697                 mutex_lock(&zr->resource_lock);
1698
1699                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1700                         dprintk(1,
1701                                 KERN_ERR
1702                                 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1703                                 ZR_DEVNAME(zr));
1704                         res = -EINVAL;
1705                         goto sparams_unlock_and_return;
1706                 }
1707
1708                 /* Check the params first before overwriting our
1709                  * nternal values */
1710                 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1711                         res = -EINVAL;
1712                         goto sparams_unlock_and_return;
1713                 }
1714
1715                 fh->jpg_settings = settings;
1716 sparams_unlock_and_return:
1717                 mutex_unlock(&zr->resource_lock);
1718
1719                 return res;
1720         }
1721
1722         case BUZIOC_REQBUFS:
1723         {
1724                 struct zoran_requestbuffers *breq = arg;
1725                 int res = 0;
1726
1727                 dprintk(3,
1728                         KERN_DEBUG
1729                         "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1730                         ZR_DEVNAME(zr), breq->count, breq->size);
1731
1732                 /* Enforce reasonable lower and upper limits */
1733                 if (breq->count < 4)
1734                         breq->count = 4;        /* Could be choosen smaller */
1735                 if (breq->count > jpg_nbufs)
1736                         breq->count = jpg_nbufs;
1737                 breq->size = PAGE_ALIGN(breq->size);
1738                 if (breq->size < 8192)
1739                         breq->size = 8192;      /* Arbitrary */
1740                 /* breq->size is limited by 1 page for the stat_com
1741                  * tables to a Maximum of 2 MB */
1742                 if (breq->size > jpg_bufsize)
1743                         breq->size = jpg_bufsize;
1744
1745                 mutex_lock(&zr->resource_lock);
1746
1747                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1748                         dprintk(1,
1749                                 KERN_ERR
1750                                 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1751                                 ZR_DEVNAME(zr));
1752                         res = -EBUSY;
1753                         goto jpgreqbuf_unlock_and_return;
1754                 }
1755
1756                 fh->jpg_buffers.num_buffers = breq->count;
1757                 fh->jpg_buffers.buffer_size = breq->size;
1758
1759                 if (jpg_fbuffer_alloc(file)) {
1760                         res = -ENOMEM;
1761                         goto jpgreqbuf_unlock_and_return;
1762                 }
1763
1764                 /* The next mmap will map the MJPEG buffers - could
1765                  * also be *_PLAY, but it doesn't matter here */
1766                 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1767 jpgreqbuf_unlock_and_return:
1768                 mutex_unlock(&zr->resource_lock);
1769
1770                 return res;
1771         }
1772
1773         case BUZIOC_QBUF_CAPT:
1774         {
1775                 int *frame = arg, res;
1776
1777                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1778                         ZR_DEVNAME(zr), *frame);
1779
1780                 mutex_lock(&zr->resource_lock);
1781                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1782                 mutex_unlock(&zr->resource_lock);
1783
1784                 return res;
1785         }
1786
1787         case BUZIOC_QBUF_PLAY:
1788         {
1789                 int *frame = arg, res;
1790
1791                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1792                         ZR_DEVNAME(zr), *frame);
1793
1794                 mutex_lock(&zr->resource_lock);
1795                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1796                 mutex_unlock(&zr->resource_lock);
1797
1798                 return res;
1799         }
1800
1801         case BUZIOC_SYNC:
1802         {
1803                 struct zoran_sync *bsync = arg;
1804                 int res;
1805
1806                 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1807
1808                 mutex_lock(&zr->resource_lock);
1809                 res = jpg_sync(file, bsync);
1810                 mutex_unlock(&zr->resource_lock);
1811
1812                 return res;
1813         }
1814
1815         case BUZIOC_G_STATUS:
1816         {
1817                 struct zoran_status *bstat = arg;
1818                 struct v4l2_routing route = { 0, 0 };
1819                 int status = 0, res = 0;
1820                 v4l2_std_id norm;
1821
1822                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1823
1824                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1825                         dprintk(1,
1826                                 KERN_ERR
1827                                 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1828                                 ZR_DEVNAME(zr));
1829                         return -EINVAL;
1830                 }
1831
1832                 route.input = zr->card.input[bstat->input].muxsel;
1833
1834                 mutex_lock(&zr->resource_lock);
1835
1836                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1837                         dprintk(1,
1838                                 KERN_ERR
1839                                 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1840                                 ZR_DEVNAME(zr));
1841                         res = -EINVAL;
1842                         goto gstat_unlock_and_return;
1843                 }
1844
1845                 decoder_s_routing(zr, &route);
1846
1847                 /* sleep 1 second */
1848                 ssleep(1);
1849
1850                 /* Get status of video decoder */
1851                 decoder_call(zr, video, querystd, &norm);
1852                 decoder_call(zr, video, g_input_status, &status);
1853
1854                 /* restore previous input and norm */
1855                 route.input = zr->card.input[zr->input].muxsel;
1856                 decoder_s_routing(zr, &route);
1857 gstat_unlock_and_return:
1858                 mutex_unlock(&zr->resource_lock);
1859
1860                 if (!res) {
1861                         bstat->signal =
1862                             (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1863                         if (norm & V4L2_STD_NTSC)
1864                                 bstat->norm = VIDEO_MODE_NTSC;
1865                         else if (norm & V4L2_STD_SECAM)
1866                                 bstat->norm = VIDEO_MODE_SECAM;
1867                         else
1868                                 bstat->norm = VIDEO_MODE_PAL;
1869
1870                         bstat->color =
1871                             (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1872                 }
1873
1874                 return res;
1875         }
1876
1877         default:
1878                 return -EINVAL;
1879         }
1880 }
1881
1882 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1883 {
1884         struct zoran_fh *fh = __fh;
1885         struct zoran *zr = fh->zr;
1886         int i, res = 0;
1887
1888         vmbuf->size =
1889             fh->v4l_buffers.num_buffers *
1890             fh->v4l_buffers.buffer_size;
1891         vmbuf->frames = fh->v4l_buffers.num_buffers;
1892         for (i = 0; i < vmbuf->frames; i++) {
1893                 vmbuf->offsets[i] =
1894                     i * fh->v4l_buffers.buffer_size;
1895         }
1896
1897         mutex_lock(&zr->resource_lock);
1898
1899         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1900                 dprintk(1,
1901                         KERN_ERR
1902                         "%s: VIDIOCGMBUF - buffers already allocated\n",
1903                         ZR_DEVNAME(zr));
1904                 res = -EINVAL;
1905                 goto v4l1reqbuf_unlock_and_return;
1906         }
1907
1908         if (v4l_fbuffer_alloc(file)) {
1909                 res = -ENOMEM;
1910                 goto v4l1reqbuf_unlock_and_return;
1911         }
1912
1913         /* The next mmap will map the V4L buffers */
1914         fh->map_mode = ZORAN_MAP_MODE_RAW;
1915 v4l1reqbuf_unlock_and_return:
1916         mutex_unlock(&zr->resource_lock);
1917
1918         return res;
1919 }
1920 #endif
1921
1922 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1923 {
1924         struct zoran_fh *fh = __fh;
1925         struct zoran *zr = fh->zr;
1926
1927         memset(cap, 0, sizeof(*cap));
1928         strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1929         strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1930         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1931                  pci_name(zr->pci_dev));
1932         cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1933                            RELEASE_VERSION);
1934         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1935                             V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1936         return 0;
1937 }
1938
1939 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1940 {
1941         int num = -1, i;
1942
1943         for (i = 0; i < NUM_FORMATS; i++) {
1944                 if (zoran_formats[i].flags & flag)
1945                         num++;
1946                 if (num == fmt->index)
1947                         break;
1948         }
1949         if (fmt->index < 0 /* late, but not too late */  || i == NUM_FORMATS)
1950                 return -EINVAL;
1951
1952         strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1953         fmt->pixelformat = zoran_formats[i].fourcc;
1954         if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1955                 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1956         return 0;
1957 }
1958
1959 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1960                                             struct v4l2_fmtdesc *f)
1961 {
1962         struct zoran_fh *fh = __fh;
1963         struct zoran *zr = fh->zr;
1964
1965         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1966 }
1967
1968 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1969                                             struct v4l2_fmtdesc *f)
1970 {
1971         struct zoran_fh *fh = __fh;
1972         struct zoran *zr = fh->zr;
1973
1974         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1975 }
1976
1977 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1978                                             struct v4l2_fmtdesc *f)
1979 {
1980         struct zoran_fh *fh = __fh;
1981         struct zoran *zr = fh->zr;
1982
1983         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1984 }
1985
1986 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1987                                         struct v4l2_format *fmt)
1988 {
1989         struct zoran_fh *fh = __fh;
1990         struct zoran *zr = fh->zr;
1991
1992         mutex_lock(&zr->resource_lock);
1993
1994         fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1995         fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1996                 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1997         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1998         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1999         if (fh->jpg_settings.TmpDcm == 1)
2000                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2001                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2002         else
2003                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2004                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2005         fmt->fmt.pix.bytesperline = 0;
2006         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2007
2008         mutex_unlock(&zr->resource_lock);
2009         return 0;
2010 }
2011
2012 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2013                                         struct v4l2_format *fmt)
2014 {
2015         struct zoran_fh *fh = __fh;
2016         struct zoran *zr = fh->zr;
2017
2018         if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2019                 return zoran_g_fmt_vid_out(file, fh, fmt);
2020
2021         mutex_lock(&zr->resource_lock);
2022         fmt->fmt.pix.width = fh->v4l_settings.width;
2023         fmt->fmt.pix.height = fh->v4l_settings.height;
2024         fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2025                                         fh->v4l_settings.height;
2026         fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2027         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2028         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2029         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2030                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2031         else
2032                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2033         mutex_unlock(&zr->resource_lock);
2034         return 0;
2035 }
2036
2037 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2038                                         struct v4l2_format *fmt)
2039 {
2040         struct zoran_fh *fh = __fh;
2041         struct zoran *zr = fh->zr;
2042
2043         mutex_lock(&zr->resource_lock);
2044
2045         fmt->fmt.win.w.left = fh->overlay_settings.x;
2046         fmt->fmt.win.w.top = fh->overlay_settings.y;
2047         fmt->fmt.win.w.width = fh->overlay_settings.width;
2048         fmt->fmt.win.w.height = fh->overlay_settings.height;
2049         if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2050                 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2051         else
2052                 fmt->fmt.win.field = V4L2_FIELD_TOP;
2053
2054         mutex_unlock(&zr->resource_lock);
2055         return 0;
2056 }
2057
2058 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2059                                         struct v4l2_format *fmt)
2060 {
2061         struct zoran_fh *fh = __fh;
2062         struct zoran *zr = fh->zr;
2063
2064         mutex_lock(&zr->resource_lock);
2065
2066         if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2067                 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2068         if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2069                 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2070         if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2071                 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2072         if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2073                 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2074
2075         mutex_unlock(&zr->resource_lock);
2076         return 0;
2077 }
2078
2079 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2080                                         struct v4l2_format *fmt)
2081 {
2082         struct zoran_fh *fh = __fh;
2083         struct zoran *zr = fh->zr;
2084         struct zoran_jpg_settings settings;
2085         int res = 0;
2086
2087         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2088                 return -EINVAL;
2089
2090         mutex_lock(&zr->resource_lock);
2091         settings = fh->jpg_settings;
2092
2093         /* we actually need to set 'real' parameters now */
2094         if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2095                 settings.TmpDcm = 1;
2096         else
2097                 settings.TmpDcm = 2;
2098         settings.decimation = 0;
2099         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2100                 settings.VerDcm = 2;
2101         else
2102                 settings.VerDcm = 1;
2103         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2104                 settings.HorDcm = 4;
2105         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2106                 settings.HorDcm = 2;
2107         else
2108                 settings.HorDcm = 1;
2109         if (settings.TmpDcm == 1)
2110                 settings.field_per_buff = 2;
2111         else
2112                 settings.field_per_buff = 1;
2113
2114         if (settings.HorDcm > 1) {
2115                 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2116                 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2117         } else {
2118                 settings.img_x = 0;
2119                 settings.img_width = BUZ_MAX_WIDTH;
2120         }
2121
2122         /* check */
2123         res = zoran_check_jpg_settings(zr, &settings, 1);
2124         if (res)
2125                 goto tryfmt_unlock_and_return;
2126
2127         /* tell the user what we actually did */
2128         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2129         fmt->fmt.pix.height = settings.img_height * 2 /
2130                 (settings.TmpDcm * settings.VerDcm);
2131         if (settings.TmpDcm == 1)
2132                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2133                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2134         else
2135                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2136                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2137
2138         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2139         fmt->fmt.pix.bytesperline = 0;
2140         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2141 tryfmt_unlock_and_return:
2142         mutex_unlock(&zr->resource_lock);
2143         return res;
2144 }
2145
2146 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2147                                         struct v4l2_format *fmt)
2148 {
2149         struct zoran_fh *fh = __fh;
2150         struct zoran *zr = fh->zr;
2151         int bpp;
2152         int i;
2153
2154         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2155                 return zoran_try_fmt_vid_out(file, fh, fmt);
2156
2157         mutex_lock(&zr->resource_lock);
2158
2159         for (i = 0; i < NUM_FORMATS; i++)
2160                 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2161                         break;
2162
2163         if (i == NUM_FORMATS) {
2164                 mutex_unlock(&zr->resource_lock);
2165                 return -EINVAL;
2166         }
2167
2168         bpp = (zoran_formats[i].depth + 7) / 8;
2169         fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2170         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2171                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2172         if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2173                 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2174         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2175                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2176         if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2177                 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2178         mutex_unlock(&zr->resource_lock);
2179
2180         return 0;
2181 }
2182
2183 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2184                                         struct v4l2_format *fmt)
2185 {
2186         struct zoran_fh *fh = __fh;
2187         struct zoran *zr = fh->zr;
2188         int res;
2189
2190         dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2191                         fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2192                         fmt->fmt.win.w.width,
2193                         fmt->fmt.win.w.height,
2194                         fmt->fmt.win.clipcount,
2195                         fmt->fmt.win.bitmap);
2196         mutex_lock(&zr->resource_lock);
2197         res = setup_window(file, fmt->fmt.win.w.left,
2198                         fmt->fmt.win.w.top,
2199                         fmt->fmt.win.w.width,
2200                         fmt->fmt.win.w.height,
2201                         (struct v4l2_clip __user *)
2202                         fmt->fmt.win.clips,
2203                         fmt->fmt.win.clipcount,
2204                         fmt->fmt.win.bitmap);
2205         mutex_unlock(&zr->resource_lock);
2206         return res;
2207 }
2208
2209 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2210                                         struct v4l2_format *fmt)
2211 {
2212         struct zoran_fh *fh = __fh;
2213         struct zoran *zr = fh->zr;
2214         __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2215         struct zoran_jpg_settings settings;
2216         int res = 0;
2217
2218         dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2219                         fmt->fmt.pix.width, fmt->fmt.pix.height,
2220                         fmt->fmt.pix.pixelformat,
2221                         (char *) &printformat);
2222         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2223                 return -EINVAL;
2224
2225         mutex_lock(&zr->resource_lock);
2226
2227         settings = fh->jpg_settings;
2228
2229         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2230                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2231                                 ZR_DEVNAME(zr));
2232                 res = -EBUSY;
2233                 goto sfmtjpg_unlock_and_return;
2234         }
2235
2236         /* we actually need to set 'real' parameters now */
2237         if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2238                 settings.TmpDcm = 1;
2239         else
2240                 settings.TmpDcm = 2;
2241         settings.decimation = 0;
2242         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2243                 settings.VerDcm = 2;
2244         else
2245                 settings.VerDcm = 1;
2246         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2247                 settings.HorDcm = 4;
2248         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2249                 settings.HorDcm = 2;
2250         else
2251                 settings.HorDcm = 1;
2252         if (settings.TmpDcm == 1)
2253                 settings.field_per_buff = 2;
2254         else
2255                 settings.field_per_buff = 1;
2256
2257         if (settings.HorDcm > 1) {
2258                 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2259                 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2260         } else {
2261                 settings.img_x = 0;
2262                 settings.img_width = BUZ_MAX_WIDTH;
2263         }
2264
2265         /* check */
2266         res = zoran_check_jpg_settings(zr, &settings, 0);
2267         if (res)
2268                 goto sfmtjpg_unlock_and_return;
2269
2270         /* it's ok, so set them */
2271         fh->jpg_settings = settings;
2272
2273         /* tell the user what we actually did */
2274         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2275         fmt->fmt.pix.height = settings.img_height * 2 /
2276                 (settings.TmpDcm * settings.VerDcm);
2277         if (settings.TmpDcm == 1)
2278                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2279                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2280         else
2281                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2282                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2283         fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2284         fmt->fmt.pix.bytesperline = 0;
2285         fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2286         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2287
2288         /* we hereby abuse this variable to show that
2289          * we're gonna do mjpeg capture */
2290         fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2291                 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2292 sfmtjpg_unlock_and_return:
2293         mutex_unlock(&zr->resource_lock);
2294         return res;
2295 }
2296
2297 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2298                                         struct v4l2_format *fmt)
2299 {
2300         struct zoran_fh *fh = __fh;
2301         struct zoran *zr = fh->zr;
2302         int i;
2303         int res = 0;
2304
2305         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2306                 return zoran_s_fmt_vid_out(file, fh, fmt);
2307
2308         for (i = 0; i < NUM_FORMATS; i++)
2309                 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2310                         break;
2311         if (i == NUM_FORMATS) {
2312                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2313                         ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2314                 return -EINVAL;
2315         }
2316         mutex_lock(&zr->resource_lock);
2317         if (fh->jpg_buffers.allocated ||
2318                 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2319                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2320                                 ZR_DEVNAME(zr));
2321                 res = -EBUSY;
2322                 goto sfmtv4l_unlock_and_return;
2323         }
2324         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2325                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2326         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2327                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2328
2329         res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2330                         fmt->fmt.pix.height, &zoran_formats[i]);
2331         if (res)
2332                 goto sfmtv4l_unlock_and_return;
2333
2334         /* tell the user the
2335          * results/missing stuff */
2336         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2337         fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2338         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2339         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2340                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2341         else
2342                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2343
2344         fh->map_mode = ZORAN_MAP_MODE_RAW;
2345 sfmtv4l_unlock_and_return:
2346         mutex_unlock(&zr->resource_lock);
2347         return res;
2348 }
2349
2350 static int zoran_g_fbuf(struct file *file, void *__fh,
2351                 struct v4l2_framebuffer *fb)
2352 {
2353         struct zoran_fh *fh = __fh;
2354         struct zoran *zr = fh->zr;
2355
2356         memset(fb, 0, sizeof(*fb));
2357         mutex_lock(&zr->resource_lock);
2358         fb->base = zr->vbuf_base;
2359         fb->fmt.width = zr->vbuf_width;
2360         fb->fmt.height = zr->vbuf_height;
2361         if (zr->overlay_settings.format)
2362                 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2363         fb->fmt.bytesperline = zr->vbuf_bytesperline;
2364         mutex_unlock(&zr->resource_lock);
2365         fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2366         fb->fmt.field = V4L2_FIELD_INTERLACED;
2367         fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2368         fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2369
2370         return 0;
2371 }
2372
2373 static int zoran_s_fbuf(struct file *file, void *__fh,
2374                 struct v4l2_framebuffer *fb)
2375 {
2376         struct zoran_fh *fh = __fh;
2377         struct zoran *zr = fh->zr;
2378         int i, res = 0;
2379         __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2380
2381         for (i = 0; i < NUM_FORMATS; i++)
2382                 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2383                         break;
2384         if (i == NUM_FORMATS) {
2385                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2386                         ZR_DEVNAME(zr), fb->fmt.pixelformat,
2387                         (char *)&printformat);
2388                 return -EINVAL;
2389         }
2390
2391         mutex_lock(&zr->resource_lock);
2392         res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2393                                 fb->fmt.width, fb->fmt.height,
2394                                 fb->fmt.bytesperline);
2395         mutex_unlock(&zr->resource_lock);
2396
2397         return res;
2398 }
2399
2400 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2401 {
2402         struct zoran_fh *fh = __fh;
2403         struct zoran *zr = fh->zr;
2404         int res;
2405
2406         mutex_lock(&zr->resource_lock);
2407         res = setup_overlay(file, on);
2408         mutex_unlock(&zr->resource_lock);
2409
2410         return res;
2411 }
2412
2413 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2414
2415 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2416 {
2417         struct zoran_fh *fh = __fh;
2418         struct zoran *zr = fh->zr;
2419         int res = 0;
2420
2421         if (req->memory != V4L2_MEMORY_MMAP) {
2422                 dprintk(2,
2423                                 KERN_ERR
2424                                 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2425                                 ZR_DEVNAME(zr), req->memory);
2426                 return -EINVAL;
2427         }
2428
2429         if (req->count == 0)
2430                 return zoran_streamoff(file, fh, req->type);
2431
2432         mutex_lock(&zr->resource_lock);
2433         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2434                 dprintk(2,
2435                                 KERN_ERR
2436                                 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2437                                 ZR_DEVNAME(zr));
2438                 res = -EBUSY;
2439                 goto v4l2reqbuf_unlock_and_return;
2440         }
2441
2442         if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2443                         req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2444
2445                 /* control user input */
2446                 if (req->count < 2)
2447                         req->count = 2;
2448                 if (req->count > v4l_nbufs)
2449                         req->count = v4l_nbufs;
2450                 fh->v4l_buffers.num_buffers = req->count;
2451
2452                 if (v4l_fbuffer_alloc(file)) {
2453                         res = -ENOMEM;
2454                         goto v4l2reqbuf_unlock_and_return;
2455                 }
2456
2457                 /* The next mmap will map the V4L buffers */
2458                 fh->map_mode = ZORAN_MAP_MODE_RAW;
2459
2460         } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2461                         fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2462
2463                 /* we need to calculate size ourselves now */
2464                 if (req->count < 4)
2465                         req->count = 4;
2466                 if (req->count > jpg_nbufs)
2467                         req->count = jpg_nbufs;
2468                 fh->jpg_buffers.num_buffers = req->count;
2469                 fh->jpg_buffers.buffer_size =
2470                         zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2471
2472                 if (jpg_fbuffer_alloc(file)) {
2473                         res = -ENOMEM;
2474                         goto v4l2reqbuf_unlock_and_return;
2475                 }
2476
2477                 /* The next mmap will map the MJPEG buffers */
2478                 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2479                         fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2480                 else
2481                         fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2482
2483         } else {
2484                 dprintk(1,
2485                                 KERN_ERR
2486                                 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2487                                 ZR_DEVNAME(zr), req->type);
2488                 res = -EINVAL;
2489                 goto v4l2reqbuf_unlock_and_return;
2490         }
2491 v4l2reqbuf_unlock_and_return:
2492         mutex_unlock(&zr->resource_lock);
2493
2494         return res;
2495 }
2496
2497 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2498 {
2499         struct zoran_fh *fh = __fh;
2500         struct zoran *zr = fh->zr;
2501         int res;
2502
2503         mutex_lock(&zr->resource_lock);
2504         res = zoran_v4l2_buffer_status(file, buf, buf->index);
2505         mutex_unlock(&zr->resource_lock);
2506
2507         return res;
2508 }
2509
2510 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2511 {
2512         struct zoran_fh *fh = __fh;
2513         struct zoran *zr = fh->zr;
2514         int res = 0, codec_mode, buf_type;
2515
2516         mutex_lock(&zr->resource_lock);
2517
2518         switch (fh->map_mode) {
2519         case ZORAN_MAP_MODE_RAW:
2520                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2521                         dprintk(1, KERN_ERR
2522                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2523                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2524                         res = -EINVAL;
2525                         goto qbuf_unlock_and_return;
2526                 }
2527
2528                 res = zoran_v4l_queue_frame(file, buf->index);
2529                 if (res)
2530                         goto qbuf_unlock_and_return;
2531                 if (!zr->v4l_memgrab_active &&
2532                                 fh->v4l_buffers.active == ZORAN_LOCKED)
2533                         zr36057_set_memgrab(zr, 1);
2534                 break;
2535
2536         case ZORAN_MAP_MODE_JPG_REC:
2537         case ZORAN_MAP_MODE_JPG_PLAY:
2538                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2539                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2540                         codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2541                 } else {
2542                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2543                         codec_mode = BUZ_MODE_MOTION_COMPRESS;
2544                 }
2545
2546                 if (buf->type != buf_type) {
2547                         dprintk(1, KERN_ERR
2548                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2549                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2550                         res = -EINVAL;
2551                         goto qbuf_unlock_and_return;
2552                 }
2553
2554                 res = zoran_jpg_queue_frame(file, buf->index,
2555                                         codec_mode);
2556                 if (res != 0)
2557                         goto qbuf_unlock_and_return;
2558                 if (zr->codec_mode == BUZ_MODE_IDLE &&
2559                                 fh->jpg_buffers.active == ZORAN_LOCKED) {
2560                         zr36057_enable_jpg(zr, codec_mode);
2561                 }
2562                 break;
2563
2564         default:
2565                 dprintk(1, KERN_ERR
2566                         "%s: VIDIOC_QBUF - unsupported type %d\n",
2567                         ZR_DEVNAME(zr), buf->type);
2568                 res = -EINVAL;
2569                 break;
2570         }
2571 qbuf_unlock_and_return:
2572         mutex_unlock(&zr->resource_lock);
2573
2574         return res;
2575 }
2576
2577 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2578 {
2579         struct zoran_fh *fh = __fh;
2580         struct zoran *zr = fh->zr;
2581         int res = 0, buf_type, num = -1;        /* compiler borks here (?) */
2582
2583         mutex_lock(&zr->resource_lock);
2584
2585         switch (fh->map_mode) {
2586         case ZORAN_MAP_MODE_RAW:
2587                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2588                         dprintk(1, KERN_ERR
2589                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2590                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2591                         res = -EINVAL;
2592                         goto dqbuf_unlock_and_return;
2593                 }
2594
2595                 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2596                 if (file->f_flags & O_NONBLOCK &&
2597                     zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2598                         res = -EAGAIN;
2599                         goto dqbuf_unlock_and_return;
2600                 }
2601                 res = v4l_sync(file, num);
2602                 if (res)
2603                         goto dqbuf_unlock_and_return;
2604                 zr->v4l_sync_tail++;
2605                 res = zoran_v4l2_buffer_status(file, buf, num);
2606                 break;
2607
2608         case ZORAN_MAP_MODE_JPG_REC:
2609         case ZORAN_MAP_MODE_JPG_PLAY:
2610         {
2611                 struct zoran_sync bs;
2612
2613                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2614                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2615                 else
2616                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2617
2618                 if (buf->type != buf_type) {
2619                         dprintk(1, KERN_ERR
2620                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2621                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2622                         res = -EINVAL;
2623                         goto dqbuf_unlock_and_return;
2624                 }
2625
2626                 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2627
2628                 if (file->f_flags & O_NONBLOCK &&
2629                     zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2630                         res = -EAGAIN;
2631                         goto dqbuf_unlock_and_return;
2632                 }
2633                 res = jpg_sync(file, &bs);
2634                 if (res)
2635                         goto dqbuf_unlock_and_return;
2636                 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2637                 break;
2638         }
2639
2640         default:
2641                 dprintk(1, KERN_ERR
2642                         "%s: VIDIOC_DQBUF - unsupported type %d\n",
2643                         ZR_DEVNAME(zr), buf->type);
2644                 res = -EINVAL;
2645                 break;
2646         }
2647 dqbuf_unlock_and_return:
2648         mutex_unlock(&zr->resource_lock);
2649
2650         return res;
2651 }
2652
2653 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2654 {
2655         struct zoran_fh *fh = __fh;
2656         struct zoran *zr = fh->zr;
2657         int res = 0;
2658
2659         mutex_lock(&zr->resource_lock);
2660
2661         switch (fh->map_mode) {
2662         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2663                 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2664                     fh->v4l_buffers.active != ZORAN_ACTIVE) {
2665                         res = -EBUSY;
2666                         goto strmon_unlock_and_return;
2667                 }
2668
2669                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2670                 zr->v4l_settings = fh->v4l_settings;
2671
2672                 zr->v4l_sync_tail = zr->v4l_pend_tail;
2673                 if (!zr->v4l_memgrab_active &&
2674                     zr->v4l_pend_head != zr->v4l_pend_tail) {
2675                         zr36057_set_memgrab(zr, 1);
2676                 }
2677                 break;
2678
2679         case ZORAN_MAP_MODE_JPG_REC:
2680         case ZORAN_MAP_MODE_JPG_PLAY:
2681                 /* what is the codec mode right now? */
2682                 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2683                     fh->jpg_buffers.active != ZORAN_ACTIVE) {
2684                         res = -EBUSY;
2685                         goto strmon_unlock_and_return;
2686                 }
2687
2688                 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2689
2690                 if (zr->jpg_que_head != zr->jpg_que_tail) {
2691                         /* Start the jpeg codec when the first frame is queued  */
2692                         jpeg_start(zr);
2693                 }
2694                 break;
2695
2696         default:
2697                 dprintk(1,
2698                         KERN_ERR
2699                         "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2700                         ZR_DEVNAME(zr), fh->map_mode);
2701                 res = -EINVAL;
2702                 break;
2703         }
2704 strmon_unlock_and_return:
2705         mutex_unlock(&zr->resource_lock);
2706
2707         return res;
2708 }
2709
2710 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2711 {
2712         struct zoran_fh *fh = __fh;
2713         struct zoran *zr = fh->zr;
2714         int i, res = 0;
2715
2716         mutex_lock(&zr->resource_lock);
2717
2718         switch (fh->map_mode) {
2719         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2720                 if (fh->v4l_buffers.active == ZORAN_FREE &&
2721                     zr->v4l_buffers.active != ZORAN_FREE) {
2722                         res = -EPERM;   /* stay off other's settings! */
2723                         goto strmoff_unlock_and_return;
2724                 }
2725                 if (zr->v4l_buffers.active == ZORAN_FREE)
2726                         goto strmoff_unlock_and_return;
2727
2728                 /* unload capture */
2729                 if (zr->v4l_memgrab_active) {
2730                         unsigned long flags;
2731
2732                         spin_lock_irqsave(&zr->spinlock, flags);
2733                         zr36057_set_memgrab(zr, 0);
2734                         spin_unlock_irqrestore(&zr->spinlock, flags);
2735                 }
2736
2737                 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2738                         zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2739                 fh->v4l_buffers = zr->v4l_buffers;
2740
2741                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2742
2743                 zr->v4l_grab_seq = 0;
2744                 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2745                 zr->v4l_sync_tail = 0;
2746
2747                 break;
2748
2749         case ZORAN_MAP_MODE_JPG_REC:
2750         case ZORAN_MAP_MODE_JPG_PLAY:
2751                 if (fh->jpg_buffers.active == ZORAN_FREE &&
2752                     zr->jpg_buffers.active != ZORAN_FREE) {
2753                         res = -EPERM;   /* stay off other's settings! */
2754                         goto strmoff_unlock_and_return;
2755                 }
2756                 if (zr->jpg_buffers.active == ZORAN_FREE)
2757                         goto strmoff_unlock_and_return;
2758
2759                 res = jpg_qbuf(file, -1,
2760                              (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2761                              BUZ_MODE_MOTION_COMPRESS :
2762                              BUZ_MODE_MOTION_DECOMPRESS);
2763                 if (res)
2764                         goto strmoff_unlock_and_return;
2765                 break;
2766         default:
2767                 dprintk(1, KERN_ERR
2768                         "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2769                         ZR_DEVNAME(zr), fh->map_mode);
2770                 res = -EINVAL;
2771                 break;
2772         }
2773 strmoff_unlock_and_return:
2774         mutex_unlock(&zr->resource_lock);
2775
2776         return res;
2777 }
2778
2779 static int zoran_queryctrl(struct file *file, void *__fh,
2780                                         struct v4l2_queryctrl *ctrl)
2781 {
2782         struct zoran_fh *fh = __fh;
2783         struct zoran *zr = fh->zr;
2784
2785         /* we only support hue/saturation/contrast/brightness */
2786         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2787             ctrl->id > V4L2_CID_HUE)
2788                 return -EINVAL;
2789
2790         decoder_call(zr, core, queryctrl, ctrl);
2791
2792         return 0;
2793 }
2794
2795 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2796 {
2797         struct zoran_fh *fh = __fh;
2798         struct zoran *zr = fh->zr;
2799
2800         /* we only support hue/saturation/contrast/brightness */
2801         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2802             ctrl->id > V4L2_CID_HUE)
2803                 return -EINVAL;
2804
2805         mutex_lock(&zr->resource_lock);
2806         decoder_call(zr, core, g_ctrl, ctrl);
2807         mutex_unlock(&zr->resource_lock);
2808
2809         return 0;
2810 }
2811
2812 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2813 {
2814         struct zoran_fh *fh = __fh;
2815         struct zoran *zr = fh->zr;
2816
2817         /* we only support hue/saturation/contrast/brightness */
2818         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2819             ctrl->id > V4L2_CID_HUE)
2820                 return -EINVAL;
2821
2822         mutex_lock(&zr->resource_lock);
2823         decoder_call(zr, core, s_ctrl, ctrl);
2824         mutex_unlock(&zr->resource_lock);
2825
2826         return 0;
2827 }
2828
2829 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2830 {
2831         struct zoran_fh *fh = __fh;
2832         struct zoran *zr = fh->zr;
2833
2834         mutex_lock(&zr->resource_lock);
2835         *std = zr->norm;
2836         mutex_unlock(&zr->resource_lock);
2837         return 0;
2838 }
2839
2840 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2841 {
2842         struct zoran_fh *fh = __fh;
2843         struct zoran *zr = fh->zr;
2844         int res = 0;
2845
2846         mutex_lock(&zr->resource_lock);
2847         res = zoran_set_norm(zr, *std);
2848         if (res)
2849                 goto sstd_unlock_and_return;
2850
2851         res = wait_grab_pending(zr);
2852 sstd_unlock_and_return:
2853         mutex_unlock(&zr->resource_lock);
2854         return res;
2855 }
2856
2857 static int zoran_enum_input(struct file *file, void *__fh,
2858                                  struct v4l2_input *inp)
2859 {
2860         struct zoran_fh *fh = __fh;
2861         struct zoran *zr = fh->zr;
2862
2863         if (inp->index < 0 || inp->index >= zr->card.inputs)
2864                 return -EINVAL;
2865         else {
2866                 int id = inp->index;
2867                 memset(inp, 0, sizeof(*inp));
2868                 inp->index = id;
2869         }
2870
2871         strncpy(inp->name, zr->card.input[inp->index].name,
2872                 sizeof(inp->name) - 1);
2873         inp->type = V4L2_INPUT_TYPE_CAMERA;
2874         inp->std = V4L2_STD_ALL;
2875
2876         /* Get status of video decoder */
2877         mutex_lock(&zr->resource_lock);
2878         decoder_call(zr, video, g_input_status, &inp->status);
2879         mutex_unlock(&zr->resource_lock);
2880         return 0;
2881 }
2882
2883 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2884 {
2885         struct zoran_fh *fh = __fh;
2886         struct zoran *zr = fh->zr;
2887
2888         mutex_lock(&zr->resource_lock);
2889         *input = zr->input;
2890         mutex_unlock(&zr->resource_lock);
2891
2892         return 0;
2893 }
2894
2895 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2896 {
2897         struct zoran_fh *fh = __fh;
2898         struct zoran *zr = fh->zr;
2899         int res;
2900
2901         mutex_lock(&zr->resource_lock);
2902         res = zoran_set_input(zr, input);
2903         if (res)
2904                 goto sinput_unlock_and_return;
2905
2906         /* Make sure the changes come into effect */
2907         res = wait_grab_pending(zr);
2908 sinput_unlock_and_return:
2909         mutex_unlock(&zr->resource_lock);
2910         return res;
2911 }
2912
2913 static int zoran_enum_output(struct file *file, void *__fh,
2914                                   struct v4l2_output *outp)
2915 {
2916         if (outp->index != 0)
2917                 return -EINVAL;
2918
2919         memset(outp, 0, sizeof(*outp));
2920         outp->index = 0;
2921         outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2922         strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2923
2924         return 0;
2925 }
2926
2927 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2928 {
2929         *output = 0;
2930
2931         return 0;
2932 }
2933
2934 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2935 {
2936         if (output != 0)
2937                 return -EINVAL;
2938
2939         return 0;
2940 }
2941
2942 /* cropping (sub-frame capture) */
2943 static int zoran_cropcap(struct file *file, void *__fh,
2944                                         struct v4l2_cropcap *cropcap)
2945 {
2946         struct zoran_fh *fh = __fh;
2947         struct zoran *zr = fh->zr;
2948         int type = cropcap->type, res = 0;
2949
2950         memset(cropcap, 0, sizeof(*cropcap));
2951         cropcap->type = type;
2952
2953         mutex_lock(&zr->resource_lock);
2954
2955         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2956             (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2957              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2958                 dprintk(1, KERN_ERR
2959                         "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2960                         ZR_DEVNAME(zr));
2961                 res = -EINVAL;
2962                 goto cropcap_unlock_and_return;
2963         }
2964
2965         cropcap->bounds.top = cropcap->bounds.left = 0;
2966         cropcap->bounds.width = BUZ_MAX_WIDTH;
2967         cropcap->bounds.height = BUZ_MAX_HEIGHT;
2968         cropcap->defrect.top = cropcap->defrect.left = 0;
2969         cropcap->defrect.width = BUZ_MIN_WIDTH;
2970         cropcap->defrect.height = BUZ_MIN_HEIGHT;
2971 cropcap_unlock_and_return:
2972         mutex_unlock(&zr->resource_lock);
2973         return res;
2974 }
2975
2976 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2977 {
2978         struct zoran_fh *fh = __fh;
2979         struct zoran *zr = fh->zr;
2980         int type = crop->type, res = 0;
2981
2982         memset(crop, 0, sizeof(*crop));
2983         crop->type = type;
2984
2985         mutex_lock(&zr->resource_lock);
2986
2987         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2988             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2989              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2990                 dprintk(1,
2991                         KERN_ERR
2992                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2993                         ZR_DEVNAME(zr));
2994                 res = -EINVAL;
2995                 goto gcrop_unlock_and_return;
2996         }
2997
2998         crop->c.top = fh->jpg_settings.img_y;
2999         crop->c.left = fh->jpg_settings.img_x;
3000         crop->c.width = fh->jpg_settings.img_width;
3001         crop->c.height = fh->jpg_settings.img_height;
3002
3003 gcrop_unlock_and_return:
3004         mutex_unlock(&zr->resource_lock);
3005
3006         return res;
3007 }
3008
3009 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3010 {
3011         struct zoran_fh *fh = __fh;
3012         struct zoran *zr = fh->zr;
3013         int res = 0;
3014         struct zoran_jpg_settings settings;
3015
3016         settings = fh->jpg_settings;
3017
3018         mutex_lock(&zr->resource_lock);
3019
3020         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3021                 dprintk(1, KERN_ERR
3022                         "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3023                         ZR_DEVNAME(zr));
3024                 res = -EBUSY;
3025                 goto scrop_unlock_and_return;
3026         }
3027
3028         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3029             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3030              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3031                 dprintk(1, KERN_ERR
3032                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3033                         ZR_DEVNAME(zr));
3034                 res = -EINVAL;
3035                 goto scrop_unlock_and_return;
3036         }
3037
3038         /* move into a form that we understand */
3039         settings.img_x = crop->c.left;
3040         settings.img_y = crop->c.top;
3041         settings.img_width = crop->c.width;
3042         settings.img_height = crop->c.height;
3043
3044         /* check validity */
3045         res = zoran_check_jpg_settings(zr, &settings, 0);
3046         if (res)
3047                 goto scrop_unlock_and_return;
3048
3049         /* accept */
3050         fh->jpg_settings = settings;
3051
3052 scrop_unlock_and_return:
3053         mutex_unlock(&zr->resource_lock);
3054         return res;
3055 }
3056
3057 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3058                                         struct v4l2_jpegcompression *params)
3059 {
3060         struct zoran_fh *fh = __fh;
3061         struct zoran *zr = fh->zr;
3062         memset(params, 0, sizeof(*params));
3063
3064         mutex_lock(&zr->resource_lock);
3065
3066         params->quality = fh->jpg_settings.jpg_comp.quality;
3067         params->APPn = fh->jpg_settings.jpg_comp.APPn;
3068         memcpy(params->APP_data,
3069                fh->jpg_settings.jpg_comp.APP_data,
3070                fh->jpg_settings.jpg_comp.APP_len);
3071         params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3072         memcpy(params->COM_data,
3073                fh->jpg_settings.jpg_comp.COM_data,
3074                fh->jpg_settings.jpg_comp.COM_len);
3075         params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3076         params->jpeg_markers =
3077             fh->jpg_settings.jpg_comp.jpeg_markers;
3078
3079         mutex_unlock(&zr->resource_lock);
3080
3081         return 0;
3082 }
3083
3084 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3085                                         struct v4l2_jpegcompression *params)
3086 {
3087         struct zoran_fh *fh = __fh;
3088         struct zoran *zr = fh->zr;
3089         int res = 0;
3090         struct zoran_jpg_settings settings;
3091
3092         settings = fh->jpg_settings;
3093
3094         settings.jpg_comp = *params;
3095
3096         mutex_lock(&zr->resource_lock);
3097
3098         if (fh->v4l_buffers.active != ZORAN_FREE ||
3099             fh->jpg_buffers.active != ZORAN_FREE) {
3100                 dprintk(1, KERN_WARNING
3101                         "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3102                         ZR_DEVNAME(zr));
3103                 res = -EBUSY;
3104                 goto sjpegc_unlock_and_return;
3105         }
3106
3107         res = zoran_check_jpg_settings(zr, &settings, 0);
3108         if (res)
3109                 goto sjpegc_unlock_and_return;
3110         if (!fh->jpg_buffers.allocated)
3111                 fh->jpg_buffers.buffer_size =
3112                     zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3113         fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3114 sjpegc_unlock_and_return:
3115         mutex_unlock(&zr->resource_lock);
3116
3117         return res;
3118 }
3119
3120 static unsigned int
3121 zoran_poll (struct file *file,
3122             poll_table  *wait)
3123 {
3124         struct zoran_fh *fh = file->private_data;
3125         struct zoran *zr = fh->zr;
3126         int res = 0, frame;
3127         unsigned long flags;
3128
3129         /* we should check whether buffers are ready to be synced on
3130          * (w/o waits - O_NONBLOCK) here
3131          * if ready for read (sync), return POLLIN|POLLRDNORM,
3132          * if ready for write (sync), return POLLOUT|POLLWRNORM,
3133          * if error, return POLLERR,
3134          * if no buffers queued or so, return POLLNVAL
3135          */
3136
3137         mutex_lock(&zr->resource_lock);
3138
3139         switch (fh->map_mode) {
3140         case ZORAN_MAP_MODE_RAW:
3141                 poll_wait(file, &zr->v4l_capq, wait);
3142                 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3143
3144                 spin_lock_irqsave(&zr->spinlock, flags);
3145                 dprintk(3,
3146                         KERN_DEBUG
3147                         "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3148                         ZR_DEVNAME(zr), __func__,
3149                         "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3150                         "UPMD"[zr->v4l_buffers.buffer[frame].state],
3151                         zr->v4l_pend_tail, zr->v4l_pend_head);
3152                 /* Process is the one capturing? */
3153                 if (fh->v4l_buffers.active != ZORAN_FREE &&
3154                     /* Buffer ready to DQBUF? */
3155                     zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3156                         res = POLLIN | POLLRDNORM;
3157                 spin_unlock_irqrestore(&zr->spinlock, flags);
3158
3159                 break;
3160
3161         case ZORAN_MAP_MODE_JPG_REC:
3162         case ZORAN_MAP_MODE_JPG_PLAY:
3163                 poll_wait(file, &zr->jpg_capq, wait);
3164                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3165
3166                 spin_lock_irqsave(&zr->spinlock, flags);
3167                 dprintk(3,
3168                         KERN_DEBUG
3169                         "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3170                         ZR_DEVNAME(zr), __func__,
3171                         "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3172                         "UPMD"[zr->jpg_buffers.buffer[frame].state],
3173                         zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3174                 if (fh->jpg_buffers.active != ZORAN_FREE &&
3175                     zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3176                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3177                                 res = POLLIN | POLLRDNORM;
3178                         else
3179                                 res = POLLOUT | POLLWRNORM;
3180                 }
3181                 spin_unlock_irqrestore(&zr->spinlock, flags);
3182
3183                 break;
3184
3185         default:
3186                 dprintk(1,
3187                         KERN_ERR
3188                         "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3189                         ZR_DEVNAME(zr), fh->map_mode);
3190                 res = POLLNVAL;
3191         }
3192
3193         mutex_unlock(&zr->resource_lock);
3194
3195         return res;
3196 }
3197
3198
3199 /*
3200  * This maps the buffers to user space.
3201  *
3202  * Depending on the state of fh->map_mode
3203  * the V4L or the MJPEG buffers are mapped
3204  * per buffer or all together
3205  *
3206  * Note that we need to connect to some
3207  * unmap signal event to unmap the de-allocate
3208  * the buffer accordingly (zoran_vm_close())
3209  */
3210
3211 static void
3212 zoran_vm_open (struct vm_area_struct *vma)
3213 {
3214         struct zoran_mapping *map = vma->vm_private_data;
3215
3216         map->count++;
3217 }
3218
3219 static void
3220 zoran_vm_close (struct vm_area_struct *vma)
3221 {
3222         struct zoran_mapping *map = vma->vm_private_data;
3223         struct file *file = map->file;
3224         struct zoran_fh *fh = file->private_data;
3225         struct zoran *zr = fh->zr;
3226         int i;
3227
3228         map->count--;
3229         if (map->count == 0) {
3230                 switch (fh->map_mode) {
3231                 case ZORAN_MAP_MODE_JPG_REC:
3232                 case ZORAN_MAP_MODE_JPG_PLAY:
3233
3234                         dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3235                                 ZR_DEVNAME(zr));
3236
3237                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3238                                 if (fh->jpg_buffers.buffer[i].map == map) {
3239                                         fh->jpg_buffers.buffer[i].map =
3240                                             NULL;
3241                                 }
3242                         }
3243                         kfree(map);
3244
3245                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3246                                 if (fh->jpg_buffers.buffer[i].map)
3247                                         break;
3248                         if (i == fh->jpg_buffers.num_buffers) {
3249                                 mutex_lock(&zr->resource_lock);
3250
3251                                 if (fh->jpg_buffers.active != ZORAN_FREE) {
3252                                         jpg_qbuf(file, -1, zr->codec_mode);
3253                                         zr->jpg_buffers.allocated = 0;
3254                                         zr->jpg_buffers.active =
3255                                             fh->jpg_buffers.active =
3256                                             ZORAN_FREE;
3257                                 }
3258                                 jpg_fbuffer_free(file);
3259                                 mutex_unlock(&zr->resource_lock);
3260                         }
3261
3262                         break;
3263
3264                 case ZORAN_MAP_MODE_RAW:
3265
3266                         dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3267                                 ZR_DEVNAME(zr));
3268
3269                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3270                                 if (fh->v4l_buffers.buffer[i].map == map) {
3271                                         /* unqueue/unmap */
3272                                         fh->v4l_buffers.buffer[i].map =
3273                                             NULL;
3274                                 }
3275                         }
3276                         kfree(map);
3277
3278                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3279                                 if (fh->v4l_buffers.buffer[i].map)
3280                                         break;
3281                         if (i == fh->v4l_buffers.num_buffers) {
3282                                 mutex_lock(&zr->resource_lock);
3283
3284                                 if (fh->v4l_buffers.active != ZORAN_FREE) {
3285                                         unsigned long flags;
3286
3287                                         spin_lock_irqsave(&zr->spinlock, flags);
3288                                         zr36057_set_memgrab(zr, 0);
3289                                         zr->v4l_buffers.allocated = 0;
3290                                         zr->v4l_buffers.active =
3291                                             fh->v4l_buffers.active =
3292                                             ZORAN_FREE;
3293                                         spin_unlock_irqrestore(&zr->spinlock, flags);
3294                                 }
3295                                 v4l_fbuffer_free(file);
3296                                 mutex_unlock(&zr->resource_lock);
3297                         }
3298
3299                         break;
3300
3301                 default:
3302                         printk(KERN_ERR
3303                                "%s: munmap() - internal error - unknown map mode %d\n",
3304                                ZR_DEVNAME(zr), fh->map_mode);
3305                         break;
3306
3307                 }
3308         }
3309 }
3310
3311 static struct vm_operations_struct zoran_vm_ops = {
3312         .open = zoran_vm_open,
3313         .close = zoran_vm_close,
3314 };
3315
3316 static int
3317 zoran_mmap (struct file           *file,
3318             struct vm_area_struct *vma)
3319 {
3320         struct zoran_fh *fh = file->private_data;
3321         struct zoran *zr = fh->zr;
3322         unsigned long size = (vma->vm_end - vma->vm_start);
3323         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3324         int i, j;
3325         unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3326         int first, last;
3327         struct zoran_mapping *map;
3328         int res = 0;
3329
3330         dprintk(3,
3331                 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3332                 ZR_DEVNAME(zr),
3333                 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3334                 vma->vm_start, vma->vm_end, size);
3335
3336         if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3337             !(vma->vm_flags & VM_WRITE)) {
3338                 dprintk(1,
3339                         KERN_ERR
3340                         "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3341                         ZR_DEVNAME(zr));
3342                 return -EINVAL;
3343         }
3344
3345         switch (fh->map_mode) {
3346
3347         case ZORAN_MAP_MODE_JPG_REC:
3348         case ZORAN_MAP_MODE_JPG_PLAY:
3349
3350                 /* lock */
3351                 mutex_lock(&zr->resource_lock);
3352
3353                 /* Map the MJPEG buffers */
3354                 if (!fh->jpg_buffers.allocated) {
3355                         dprintk(1,
3356                                 KERN_ERR
3357                                 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3358                                 ZR_DEVNAME(zr));
3359                         res = -ENOMEM;
3360                         goto jpg_mmap_unlock_and_return;
3361                 }
3362
3363                 first = offset / fh->jpg_buffers.buffer_size;
3364                 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3365                 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3366                     size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3367                     last < 0 || first >= fh->jpg_buffers.num_buffers ||
3368                     last >= fh->jpg_buffers.num_buffers) {
3369                         dprintk(1,
3370                                 KERN_ERR
3371                                 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3372                                 ZR_DEVNAME(zr), offset, size,
3373                                 fh->jpg_buffers.buffer_size,
3374                                 fh->jpg_buffers.num_buffers);
3375                         res = -EINVAL;
3376                         goto jpg_mmap_unlock_and_return;
3377                 }
3378                 for (i = first; i <= last; i++) {
3379                         if (fh->jpg_buffers.buffer[i].map) {
3380                                 dprintk(1,
3381                                         KERN_ERR
3382                                         "%s: mmap(MJPEG) - buffer %d already mapped\n",
3383                                         ZR_DEVNAME(zr), i);
3384                                 res = -EBUSY;
3385                                 goto jpg_mmap_unlock_and_return;
3386                         }
3387                 }
3388
3389                 /* map these buffers (v4l_buffers[i]) */
3390                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3391                 if (!map) {
3392                         res = -ENOMEM;
3393                         goto jpg_mmap_unlock_and_return;
3394                 }
3395                 map->file = file;
3396                 map->count = 1;
3397
3398                 vma->vm_ops = &zoran_vm_ops;
3399                 vma->vm_flags |= VM_DONTEXPAND;
3400                 vma->vm_private_data = map;
3401
3402                 for (i = first; i <= last; i++) {
3403                         for (j = 0;
3404                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3405                              j++) {
3406                                 fraglen =
3407                                     (le32_to_cpu(fh->jpg_buffers.buffer[i].
3408                                      frag_tab[2 * j + 1]) & ~1) << 1;
3409                                 todo = size;
3410                                 if (todo > fraglen)
3411                                         todo = fraglen;
3412                                 pos =
3413                                     le32_to_cpu(fh->jpg_buffers.
3414                                     buffer[i].frag_tab[2 * j]);
3415                                 /* should just be pos on i386 */
3416                                 page = virt_to_phys(bus_to_virt(pos))
3417                                                                 >> PAGE_SHIFT;
3418                                 if (remap_pfn_range(vma, start, page,
3419                                                         todo, PAGE_SHARED)) {
3420                                         dprintk(1,
3421                                                 KERN_ERR
3422                                                 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3423                                                 ZR_DEVNAME(zr));
3424                                         res = -EAGAIN;
3425                                         goto jpg_mmap_unlock_and_return;
3426                                 }
3427                                 size -= todo;
3428                                 start += todo;
3429                                 if (size == 0)
3430                                         break;
3431                                 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3432                                     frag_tab[2 * j + 1]) & 1)
3433                                         break;  /* was last fragment */
3434                         }
3435                         fh->jpg_buffers.buffer[i].map = map;
3436                         if (size == 0)
3437                                 break;
3438
3439                 }
3440         jpg_mmap_unlock_and_return:
3441                 mutex_unlock(&zr->resource_lock);
3442
3443                 break;
3444
3445         case ZORAN_MAP_MODE_RAW:
3446
3447                 mutex_lock(&zr->resource_lock);
3448
3449                 /* Map the V4L buffers */
3450                 if (!fh->v4l_buffers.allocated) {
3451                         dprintk(1,
3452                                 KERN_ERR
3453                                 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3454                                 ZR_DEVNAME(zr));
3455                         res = -ENOMEM;
3456                         goto v4l_mmap_unlock_and_return;
3457                 }
3458
3459                 first = offset / fh->v4l_buffers.buffer_size;
3460                 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3461                 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3462                     size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3463                     last < 0 || first >= fh->v4l_buffers.num_buffers ||
3464                     last >= fh->v4l_buffers.buffer_size) {
3465                         dprintk(1,
3466                                 KERN_ERR
3467                                 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3468                                 ZR_DEVNAME(zr), offset, size,
3469                                 fh->v4l_buffers.buffer_size,
3470                                 fh->v4l_buffers.num_buffers);
3471                         res = -EINVAL;
3472                         goto v4l_mmap_unlock_and_return;
3473                 }
3474                 for (i = first; i <= last; i++) {
3475                         if (fh->v4l_buffers.buffer[i].map) {
3476                                 dprintk(1,
3477                                         KERN_ERR
3478                                         "%s: mmap(V4L) - buffer %d already mapped\n",
3479                                         ZR_DEVNAME(zr), i);
3480                                 res = -EBUSY;
3481                                 goto v4l_mmap_unlock_and_return;
3482                         }
3483                 }
3484
3485                 /* map these buffers (v4l_buffers[i]) */
3486                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3487                 if (!map) {
3488                         res = -ENOMEM;
3489                         goto v4l_mmap_unlock_and_return;
3490                 }
3491                 map->file = file;
3492                 map->count = 1;
3493
3494                 vma->vm_ops = &zoran_vm_ops;
3495                 vma->vm_flags |= VM_DONTEXPAND;
3496                 vma->vm_private_data = map;
3497
3498                 for (i = first; i <= last; i++) {
3499                         todo = size;
3500                         if (todo > fh->v4l_buffers.buffer_size)
3501                                 todo = fh->v4l_buffers.buffer_size;
3502                         page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3503                         if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3504                                                         todo, PAGE_SHARED)) {
3505                                 dprintk(1,
3506                                         KERN_ERR
3507                                         "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3508                                         ZR_DEVNAME(zr));
3509                                 res = -EAGAIN;
3510                                 goto v4l_mmap_unlock_and_return;
3511                         }
3512                         size -= todo;
3513                         start += todo;
3514                         fh->v4l_buffers.buffer[i].map = map;
3515                         if (size == 0)
3516                                 break;
3517                 }
3518         v4l_mmap_unlock_and_return:
3519                 mutex_unlock(&zr->resource_lock);
3520
3521                 break;
3522
3523         default:
3524                 dprintk(1,
3525                         KERN_ERR
3526                         "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3527                         ZR_DEVNAME(zr), fh->map_mode);
3528                 break;
3529         }
3530
3531         return 0;
3532 }
3533
3534 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3535         .vidioc_querycap                    = zoran_querycap,
3536         .vidioc_cropcap                     = zoran_cropcap,
3537         .vidioc_s_crop                      = zoran_s_crop,
3538         .vidioc_g_crop                      = zoran_g_crop,
3539         .vidioc_enum_input                  = zoran_enum_input,
3540         .vidioc_g_input                     = zoran_g_input,
3541         .vidioc_s_input                     = zoran_s_input,
3542         .vidioc_enum_output                 = zoran_enum_output,
3543         .vidioc_g_output                    = zoran_g_output,
3544         .vidioc_s_output                    = zoran_s_output,
3545         .vidioc_g_fbuf                      = zoran_g_fbuf,
3546         .vidioc_s_fbuf                      = zoran_s_fbuf,
3547         .vidioc_g_std                       = zoran_g_std,
3548         .vidioc_s_std                       = zoran_s_std,
3549         .vidioc_g_jpegcomp                  = zoran_g_jpegcomp,
3550         .vidioc_s_jpegcomp                  = zoran_s_jpegcomp,
3551         .vidioc_overlay                     = zoran_overlay,
3552         .vidioc_reqbufs                     = zoran_reqbufs,
3553         .vidioc_querybuf                    = zoran_querybuf,
3554         .vidioc_qbuf                        = zoran_qbuf,
3555         .vidioc_dqbuf                       = zoran_dqbuf,
3556         .vidioc_streamon                    = zoran_streamon,
3557         .vidioc_streamoff                   = zoran_streamoff,
3558         .vidioc_enum_fmt_vid_cap            = zoran_enum_fmt_vid_cap,
3559         .vidioc_enum_fmt_vid_out            = zoran_enum_fmt_vid_out,
3560         .vidioc_enum_fmt_vid_overlay        = zoran_enum_fmt_vid_overlay,
3561         .vidioc_g_fmt_vid_cap               = zoran_g_fmt_vid_cap,
3562         .vidioc_g_fmt_vid_out               = zoran_g_fmt_vid_out,
3563         .vidioc_g_fmt_vid_overlay           = zoran_g_fmt_vid_overlay,
3564         .vidioc_s_fmt_vid_cap               = zoran_s_fmt_vid_cap,
3565         .vidioc_s_fmt_vid_out               = zoran_s_fmt_vid_out,
3566         .vidioc_s_fmt_vid_overlay           = zoran_s_fmt_vid_overlay,
3567         .vidioc_try_fmt_vid_cap             = zoran_try_fmt_vid_cap,
3568         .vidioc_try_fmt_vid_out             = zoran_try_fmt_vid_out,
3569         .vidioc_try_fmt_vid_overlay         = zoran_try_fmt_vid_overlay,
3570         .vidioc_queryctrl                   = zoran_queryctrl,
3571         .vidioc_s_ctrl                      = zoran_s_ctrl,
3572         .vidioc_g_ctrl                      = zoran_g_ctrl,
3573 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3574         .vidioc_default                     = zoran_default,
3575         .vidiocgmbuf                        = zoran_vidiocgmbuf,
3576 #endif
3577 };
3578
3579 static const struct v4l2_file_operations zoran_fops = {
3580         .owner = THIS_MODULE,
3581         .open = zoran_open,
3582         .release = zoran_close,
3583         .ioctl = video_ioctl2,
3584         .read = zoran_read,
3585         .write = zoran_write,
3586         .mmap = zoran_mmap,
3587         .poll = zoran_poll,
3588 };
3589
3590 struct video_device zoran_template __devinitdata = {
3591         .name = ZORAN_NAME,
3592         .fops = &zoran_fops,
3593         .ioctl_ops = &zoran_ioctl_ops,
3594         .release = &zoran_vdev_release,
3595         .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3596         .minor = -1
3597 };
3598