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