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