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