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