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