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