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