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