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