]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/video/zoran/zoran_driver.c
V4L/DVB (10715): zoran: clean up some old V4L1 left-overs and remove the MAP_NR macro.
[karo-tx-linux.git] / drivers / media / video / zoran / zoran_driver.c
1 /*
2  * Zoran zr36057/zr36067 PCI controller driver, for the
3  * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4  * Media Labs LML33/LML33R10.
5  *
6  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7  *
8  * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9  *
10  * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11  *
12  * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13  *
14  * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15  *
16  * Based on
17  *
18  * Miro DC10 driver
19  * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20  *
21  * Iomega Buz driver version 1.0
22  * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23  *
24  * buz.0.0.3
25  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26  *
27  * bttv - Bt848 frame grabber driver
28  * Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
29  *                        & Marcus Metzler (mocm@thp.uni-koeln.de)
30  *
31  *
32  * This program is free software; you can redistribute it and/or modify
33  * it under the terms of the GNU General Public License as published by
34  * the Free Software Foundation; either version 2 of the License, or
35  * (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this program; if not, write to the Free Software
44  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45  */
46
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
55
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
59
60 #include <linux/spinlock.h>
61
62 #include <linux/videodev.h>
63 #include <media/v4l2-common.h>
64 #include <media/v4l2-ioctl.h>
65 #include "videocodec.h"
66
67 #include <asm/byteorder.h>
68 #include <asm/io.h>
69 #include <asm/uaccess.h>
70 #include <linux/proc_fs.h>
71
72 #include <linux/video_decoder.h>
73 #include <linux/video_encoder.h>
74 #include <linux/mutex.h>
75 #include "zoran.h"
76 #include "zoran_device.h"
77 #include "zoran_card.h"
78
79
80 const struct zoran_format zoran_formats[] = {
81         {
82                 .name = "15-bit RGB LE",
83                 .fourcc = V4L2_PIX_FMT_RGB555,
84                 .colorspace = V4L2_COLORSPACE_SRGB,
85                 .depth = 15,
86                 .flags = ZORAN_FORMAT_CAPTURE |
87                          ZORAN_FORMAT_OVERLAY,
88                 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
89                            ZR36057_VFESPFR_LittleEndian,
90         }, {
91                 .name = "15-bit RGB BE",
92                 .fourcc = V4L2_PIX_FMT_RGB555X,
93                 .colorspace = V4L2_COLORSPACE_SRGB,
94                 .depth = 15,
95                 .flags = ZORAN_FORMAT_CAPTURE |
96                          ZORAN_FORMAT_OVERLAY,
97                 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
98         }, {
99                 .name = "16-bit RGB LE",
100                 .fourcc = V4L2_PIX_FMT_RGB565,
101                 .colorspace = V4L2_COLORSPACE_SRGB,
102                 .depth = 16,
103                 .flags = ZORAN_FORMAT_CAPTURE |
104                          ZORAN_FORMAT_OVERLAY,
105                 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
106                            ZR36057_VFESPFR_LittleEndian,
107         }, {
108                 .name = "16-bit RGB BE",
109                 .fourcc = V4L2_PIX_FMT_RGB565X,
110                 .colorspace = V4L2_COLORSPACE_SRGB,
111                 .depth = 16,
112                 .flags = ZORAN_FORMAT_CAPTURE |
113                          ZORAN_FORMAT_OVERLAY,
114                 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
115         }, {
116                 .name = "24-bit RGB",
117                 .fourcc = V4L2_PIX_FMT_BGR24,
118                 .colorspace = V4L2_COLORSPACE_SRGB,
119                 .depth = 24,
120                 .flags = ZORAN_FORMAT_CAPTURE |
121                          ZORAN_FORMAT_OVERLAY,
122                 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
123         }, {
124                 .name = "32-bit RGB LE",
125                 .fourcc = V4L2_PIX_FMT_BGR32,
126                 .colorspace = V4L2_COLORSPACE_SRGB,
127                 .depth = 32,
128                 .flags = ZORAN_FORMAT_CAPTURE |
129                          ZORAN_FORMAT_OVERLAY,
130                 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
131         }, {
132                 .name = "32-bit RGB BE",
133                 .fourcc = V4L2_PIX_FMT_RGB32,
134                 .colorspace = V4L2_COLORSPACE_SRGB,
135                 .depth = 32,
136                 .flags = ZORAN_FORMAT_CAPTURE |
137                          ZORAN_FORMAT_OVERLAY,
138                 .vfespfr = ZR36057_VFESPFR_RGB888,
139         }, {
140                 .name = "4:2:2, packed, YUYV",
141                 .fourcc = V4L2_PIX_FMT_YUYV,
142                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
143                 .depth = 16,
144                 .flags = ZORAN_FORMAT_CAPTURE |
145                          ZORAN_FORMAT_OVERLAY,
146                 .vfespfr = ZR36057_VFESPFR_YUV422,
147         }, {
148                 .name = "4:2:2, packed, UYVY",
149                 .fourcc = V4L2_PIX_FMT_UYVY,
150                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
151                 .depth = 16,
152                 .flags = ZORAN_FORMAT_CAPTURE |
153                          ZORAN_FORMAT_OVERLAY,
154                 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
155         }, {
156                 .name = "Hardware-encoded Motion-JPEG",
157                 .fourcc = V4L2_PIX_FMT_MJPEG,
158                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
159                 .depth = 0,
160                 .flags = ZORAN_FORMAT_CAPTURE |
161                          ZORAN_FORMAT_PLAYBACK |
162                          ZORAN_FORMAT_COMPRESSED,
163         }
164 };
165 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
166
167 static int lock_norm;   /* 0 = default 1 = Don't change TV standard (norm) */
168 module_param(lock_norm, int, 0644);
169 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
170
171         /* small helper function for calculating buffersizes for v4l2
172          * we calculate the nearest higher power-of-two, which
173          * will be the recommended buffersize */
174 static __u32
175 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
176 {
177         __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
178         __u32 num = (1024 * 512) / (div);
179         __u32 result = 2;
180
181         num--;
182         while (num) {
183                 num >>= 1;
184                 result <<= 1;
185         }
186
187         if (result > jpg_bufsize)
188                 return jpg_bufsize;
189         if (result < 8192)
190                 return 8192;
191         return result;
192 }
193
194 /* forward references */
195 static void v4l_fbuffer_free(struct file *file);
196 static void jpg_fbuffer_free(struct file *file);
197
198 /*
199  *   Allocate the V4L grab buffers
200  *
201  *   These have to be pysically contiguous.
202  */
203
204 static int
205 v4l_fbuffer_alloc (struct file *file)
206 {
207         struct zoran_fh *fh = file->private_data;
208         struct zoran *zr = fh->zr;
209         int i, off;
210         unsigned char *mem;
211
212         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
213                 if (fh->v4l_buffers.buffer[i].fbuffer)
214                         dprintk(2,
215                                 KERN_WARNING
216                                 "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
217                                 ZR_DEVNAME(zr), i);
218
219                 //udelay(20);
220                 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
221                 if (!mem) {
222                         dprintk(1,
223                                 KERN_ERR
224                                 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
225                                 ZR_DEVNAME(zr), i);
226                         v4l_fbuffer_free(file);
227                         return -ENOBUFS;
228                 }
229                 fh->v4l_buffers.buffer[i].fbuffer = mem;
230                 fh->v4l_buffers.buffer[i].fbuffer_phys =
231                     virt_to_phys(mem);
232                 fh->v4l_buffers.buffer[i].fbuffer_bus =
233                     virt_to_bus(mem);
234                 for (off = 0; off < fh->v4l_buffers.buffer_size;
235                      off += PAGE_SIZE)
236                         SetPageReserved(virt_to_page(mem + off));
237                 dprintk(4,
238                         KERN_INFO
239                         "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
240                         ZR_DEVNAME(zr), i, (unsigned long) mem,
241                         virt_to_bus(mem));
242         }
243
244         fh->v4l_buffers.allocated = 1;
245
246         return 0;
247 }
248
249 /* free the V4L grab buffers */
250 static void
251 v4l_fbuffer_free (struct file *file)
252 {
253         struct zoran_fh *fh = file->private_data;
254         struct zoran *zr = fh->zr;
255         int i, off;
256         unsigned char *mem;
257
258         dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
259
260         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
261                 if (!fh->v4l_buffers.buffer[i].fbuffer)
262                         continue;
263
264                 mem = fh->v4l_buffers.buffer[i].fbuffer;
265                 for (off = 0; off < fh->v4l_buffers.buffer_size;
266                      off += PAGE_SIZE)
267                         ClearPageReserved(virt_to_page(mem + off));
268                 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
269                 fh->v4l_buffers.buffer[i].fbuffer = NULL;
270         }
271
272         fh->v4l_buffers.allocated = 0;
273 }
274
275 /*
276  *   Allocate the MJPEG grab buffers.
277  *
278  *   If a Natoma chipset is present and this is a revision 1 zr36057,
279  *   each MJPEG buffer needs to be physically contiguous.
280  *   (RJ: This statement is from Dave Perks' original driver,
281  *   I could never check it because I have a zr36067)
282  *
283  *   RJ: The contents grab buffers needs never be accessed in the driver.
284  *       Therefore there is no need to allocate them with vmalloc in order
285  *       to get a contiguous virtual memory space.
286  *       I don't understand why many other drivers first allocate them with
287  *       vmalloc (which uses internally also get_zeroed_page, but delivers you
288  *       virtual addresses) and then again have to make a lot of efforts
289  *       to get the physical address.
290  *
291  *   Ben Capper:
292  *       On big-endian architectures (such as ppc) some extra steps
293  *       are needed. When reading and writing to the stat_com array
294  *       and fragment buffers, the device expects to see little-
295  *       endian values. The use of cpu_to_le32() and le32_to_cpu()
296  *       in this function (and one or two others in zoran_device.c)
297  *       ensure that these values are always stored in little-endian
298  *       form, regardless of architecture. The zr36057 does Very Bad
299  *       Things on big endian architectures if the stat_com array
300  *       and fragment buffers are not little-endian.
301  */
302
303 static int
304 jpg_fbuffer_alloc (struct file *file)
305 {
306         struct zoran_fh *fh = file->private_data;
307         struct zoran *zr = fh->zr;
308         int i, j, off;
309         unsigned long mem;
310
311         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
312                 if (fh->jpg_buffers.buffer[i].frag_tab)
313                         dprintk(2,
314                                 KERN_WARNING
315                                 "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
316                                 ZR_DEVNAME(zr), i);
317
318                 /* Allocate fragment table for this buffer */
319
320                 mem = get_zeroed_page(GFP_KERNEL);
321                 if (mem == 0) {
322                         dprintk(1,
323                                 KERN_ERR
324                                 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
325                                 ZR_DEVNAME(zr), i);
326                         jpg_fbuffer_free(file);
327                         return -ENOBUFS;
328                 }
329                 fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
330                 fh->jpg_buffers.buffer[i].frag_tab_bus =
331                     virt_to_bus((void *) mem);
332
333                 //if (alloc_contig) {
334                 if (fh->jpg_buffers.need_contiguous) {
335                         mem =
336                             (unsigned long) kmalloc(fh->jpg_buffers.
337                                                     buffer_size,
338                                                     GFP_KERNEL);
339                         if (mem == 0) {
340                                 dprintk(1,
341                                         KERN_ERR
342                                         "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
343                                         ZR_DEVNAME(zr), i);
344                                 jpg_fbuffer_free(file);
345                                 return -ENOBUFS;
346                         }
347                         fh->jpg_buffers.buffer[i].frag_tab[0] =
348                             cpu_to_le32(virt_to_bus((void *) mem));
349                         fh->jpg_buffers.buffer[i].frag_tab[1] =
350                             cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
351                         for (off = 0; off < fh->jpg_buffers.buffer_size;
352                              off += PAGE_SIZE)
353                                 SetPageReserved(virt_to_page(mem + off));
354                 } else {
355                         /* jpg_bufsize is already page aligned */
356                         for (j = 0;
357                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
358                              j++) {
359                                 mem = get_zeroed_page(GFP_KERNEL);
360                                 if (mem == 0) {
361                                         dprintk(1,
362                                                 KERN_ERR
363                                                 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
364                                                 ZR_DEVNAME(zr), i);
365                                         jpg_fbuffer_free(file);
366                                         return -ENOBUFS;
367                                 }
368
369                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
370                                     cpu_to_le32(virt_to_bus((void *) mem));
371                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
372                                                                    1] =
373                                     cpu_to_le32((PAGE_SIZE / 4) << 1);
374                                 SetPageReserved(virt_to_page(mem));
375                         }
376
377                         fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
378                 }
379         }
380
381         dprintk(4,
382                 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
383                 ZR_DEVNAME(zr),
384                 (fh->jpg_buffers.num_buffers *
385                  fh->jpg_buffers.buffer_size) >> 10);
386
387         fh->jpg_buffers.allocated = 1;
388
389         return 0;
390 }
391
392 /* free the MJPEG grab buffers */
393 static void
394 jpg_fbuffer_free (struct file *file)
395 {
396         struct zoran_fh *fh = file->private_data;
397         struct zoran *zr = fh->zr;
398         int i, j, off;
399         unsigned char *mem;
400         __le32 frag_tab;
401
402         dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
403
404         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
405                 if (!fh->jpg_buffers.buffer[i].frag_tab)
406                         continue;
407
408                 if (fh->jpg_buffers.need_contiguous) {
409                         frag_tab = fh->jpg_buffers.buffer[i].frag_tab[0];
410
411                         if (frag_tab) {
412                                 mem = (unsigned char *)bus_to_virt(le32_to_cpu(frag_tab));
413                                 for (off = 0; off < fh->jpg_buffers.buffer_size; off += PAGE_SIZE)
414                                         ClearPageReserved(virt_to_page(mem + off));
415                                 kfree(mem);
416                                 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
417                                 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
418                         }
419                 } else {
420                         for (j = 0; j < fh->jpg_buffers.buffer_size / PAGE_SIZE; j++) {
421                                 frag_tab = fh->jpg_buffers.buffer[i].frag_tab[2 * j];
422
423                                 if (!frag_tab)
424                                         break;
425                                 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
426                                 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
427                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j] = 0;
428                                 fh->jpg_buffers.buffer[i].frag_tab[2 * j + 1] = 0;
429                         }
430                 }
431
432                 free_page((unsigned long)fh->jpg_buffers.buffer[i].frag_tab);
433                 fh->jpg_buffers.buffer[i].frag_tab = NULL;
434         }
435
436         fh->jpg_buffers.allocated = 0;
437 }
438
439 /*
440  *   V4L Buffer grabbing
441  */
442
443 static int
444 zoran_v4l_set_format (struct file               *file,
445                       int                        width,
446                       int                        height,
447                       const struct zoran_format *format)
448 {
449         struct zoran_fh *fh = file->private_data;
450         struct zoran *zr = fh->zr;
451         int bpp;
452
453         /* Check size and format of the grab wanted */
454
455         if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
456             height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
457                 dprintk(1,
458                         KERN_ERR
459                         "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
460                         ZR_DEVNAME(zr), width, height);
461                 return -EINVAL;
462         }
463
464         bpp = (format->depth + 7) / 8;
465
466         /* Check against available buffer size */
467         if (height * width * bpp > fh->v4l_buffers.buffer_size) {
468                 dprintk(1,
469                         KERN_ERR
470                         "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
471                         ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
472                 return -EINVAL;
473         }
474
475         /* The video front end needs 4-byte alinged line sizes */
476
477         if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
478                 dprintk(1,
479                         KERN_ERR
480                         "%s: v4l_set_format() - wrong frame alignment\n",
481                         ZR_DEVNAME(zr));
482                 return -EINVAL;
483         }
484
485         fh->v4l_settings.width = width;
486         fh->v4l_settings.height = height;
487         fh->v4l_settings.format = format;
488         fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
489
490         return 0;
491 }
492
493 static int
494 zoran_v4l_queue_frame (struct file *file,
495                        int          num)
496 {
497         struct zoran_fh *fh = file->private_data;
498         struct zoran *zr = fh->zr;
499         unsigned long flags;
500         int res = 0;
501
502         if (!fh->v4l_buffers.allocated) {
503                 dprintk(1,
504                         KERN_ERR
505                         "%s: v4l_queue_frame() - buffers not yet allocated\n",
506                         ZR_DEVNAME(zr));
507                 res = -ENOMEM;
508         }
509
510         /* No grabbing outside the buffer range! */
511         if (num >= fh->v4l_buffers.num_buffers || num < 0) {
512                 dprintk(1,
513                         KERN_ERR
514                         "%s: v4l_queue_frame() - buffer %d is out of range\n",
515                         ZR_DEVNAME(zr), num);
516                 res = -EINVAL;
517         }
518
519         spin_lock_irqsave(&zr->spinlock, flags);
520
521         if (fh->v4l_buffers.active == ZORAN_FREE) {
522                 if (zr->v4l_buffers.active == ZORAN_FREE) {
523                         zr->v4l_buffers = fh->v4l_buffers;
524                         fh->v4l_buffers.active = ZORAN_ACTIVE;
525                 } else {
526                         dprintk(1,
527                                 KERN_ERR
528                                 "%s: v4l_queue_frame() - another session is already capturing\n",
529                                 ZR_DEVNAME(zr));
530                         res = -EBUSY;
531                 }
532         }
533
534         /* make sure a grab isn't going on currently with this buffer */
535         if (!res) {
536                 switch (zr->v4l_buffers.buffer[num].state) {
537                 default:
538                 case BUZ_STATE_PEND:
539                         if (zr->v4l_buffers.active == ZORAN_FREE) {
540                                 fh->v4l_buffers.active = ZORAN_FREE;
541                                 zr->v4l_buffers.allocated = 0;
542                         }
543                         res = -EBUSY;   /* what are you doing? */
544                         break;
545                 case BUZ_STATE_DONE:
546                         dprintk(2,
547                                 KERN_WARNING
548                                 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
549                                 ZR_DEVNAME(zr), num);
550                 case BUZ_STATE_USER:
551                         /* since there is at least one unused buffer there's room for at least
552                          * one more pend[] entry */
553                         zr->v4l_pend[zr->v4l_pend_head++ &
554                                         V4L_MASK_FRAME] = num;
555                         zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
556                         zr->v4l_buffers.buffer[num].bs.length =
557                             fh->v4l_settings.bytesperline *
558                             zr->v4l_settings.height;
559                         fh->v4l_buffers.buffer[num] =
560                             zr->v4l_buffers.buffer[num];
561                         break;
562                 }
563         }
564
565         spin_unlock_irqrestore(&zr->spinlock, flags);
566
567         if (!res && zr->v4l_buffers.active == ZORAN_FREE)
568                 zr->v4l_buffers.active = fh->v4l_buffers.active;
569
570         return res;
571 }
572
573 /*
574  * Sync on a V4L buffer
575  */
576
577 static int
578 v4l_sync (struct file *file,
579           int          frame)
580 {
581         struct zoran_fh *fh = file->private_data;
582         struct zoran *zr = fh->zr;
583         unsigned long flags;
584
585         if (fh->v4l_buffers.active == ZORAN_FREE) {
586                 dprintk(1,
587                         KERN_ERR
588                         "%s: v4l_sync() - no grab active for this session\n",
589                         ZR_DEVNAME(zr));
590                 return -EINVAL;
591         }
592
593         /* check passed-in frame number */
594         if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
595                 dprintk(1,
596                         KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
597                         ZR_DEVNAME(zr), frame);
598                 return -EINVAL;
599         }
600
601         /* Check if is buffer was queued at all */
602         if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
603                 dprintk(1,
604                         KERN_ERR
605                         "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
606                         ZR_DEVNAME(zr));
607                 return -EPROTO;
608         }
609
610         /* wait on this buffer to get ready */
611         if (!wait_event_interruptible_timeout(zr->v4l_capq,
612                                 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
613                                 10*HZ))
614                 return -ETIME;
615         if (signal_pending(current))
616                 return -ERESTARTSYS;
617
618         /* buffer should now be in BUZ_STATE_DONE */
619         if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
620                 dprintk(2,
621                         KERN_ERR "%s: v4l_sync() - internal state error\n",
622                         ZR_DEVNAME(zr));
623
624         zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
625         fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
626
627         spin_lock_irqsave(&zr->spinlock, flags);
628
629         /* Check if streaming capture has finished */
630         if (zr->v4l_pend_tail == zr->v4l_pend_head) {
631                 zr36057_set_memgrab(zr, 0);
632                 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
633                         fh->v4l_buffers.active = zr->v4l_buffers.active =
634                             ZORAN_FREE;
635                         zr->v4l_buffers.allocated = 0;
636                 }
637         }
638
639         spin_unlock_irqrestore(&zr->spinlock, flags);
640
641         return 0;
642 }
643
644 /*
645  *   Queue a MJPEG buffer for capture/playback
646  */
647
648 static int
649 zoran_jpg_queue_frame (struct file          *file,
650                        int                   num,
651                        enum zoran_codec_mode mode)
652 {
653         struct zoran_fh *fh = file->private_data;
654         struct zoran *zr = fh->zr;
655         unsigned long flags;
656         int res = 0;
657
658         /* Check if buffers are allocated */
659         if (!fh->jpg_buffers.allocated) {
660                 dprintk(1,
661                         KERN_ERR
662                         "%s: jpg_queue_frame() - buffers not yet allocated\n",
663                         ZR_DEVNAME(zr));
664                 return -ENOMEM;
665         }
666
667         /* No grabbing outside the buffer range! */
668         if (num >= fh->jpg_buffers.num_buffers || num < 0) {
669                 dprintk(1,
670                         KERN_ERR
671                         "%s: jpg_queue_frame() - buffer %d out of range\n",
672                         ZR_DEVNAME(zr), num);
673                 return -EINVAL;
674         }
675
676         /* what is the codec mode right now? */
677         if (zr->codec_mode == BUZ_MODE_IDLE) {
678                 zr->jpg_settings = fh->jpg_settings;
679         } else if (zr->codec_mode != mode) {
680                 /* wrong codec mode active - invalid */
681                 dprintk(1,
682                         KERN_ERR
683                         "%s: jpg_queue_frame() - codec in wrong mode\n",
684                         ZR_DEVNAME(zr));
685                 return -EINVAL;
686         }
687
688         if (fh->jpg_buffers.active == ZORAN_FREE) {
689                 if (zr->jpg_buffers.active == ZORAN_FREE) {
690                         zr->jpg_buffers = fh->jpg_buffers;
691                         fh->jpg_buffers.active = ZORAN_ACTIVE;
692                 } else {
693                         dprintk(1,
694                                 KERN_ERR
695                                 "%s: jpg_queue_frame() - another session is already capturing\n",
696                                 ZR_DEVNAME(zr));
697                         res = -EBUSY;
698                 }
699         }
700
701         if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
702                 /* Ok load up the jpeg codec */
703                 zr36057_enable_jpg(zr, mode);
704         }
705
706         spin_lock_irqsave(&zr->spinlock, flags);
707
708         if (!res) {
709                 switch (zr->jpg_buffers.buffer[num].state) {
710                 case BUZ_STATE_DONE:
711                         dprintk(2,
712                                 KERN_WARNING
713                                 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
714                                 ZR_DEVNAME(zr));
715                 case BUZ_STATE_USER:
716                         /* since there is at least one unused buffer there's room for at
717                          *least one more pend[] entry */
718                         zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
719                             num;
720                         zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
721                         fh->jpg_buffers.buffer[num] =
722                             zr->jpg_buffers.buffer[num];
723                         zoran_feed_stat_com(zr);
724                         break;
725                 default:
726                 case BUZ_STATE_DMA:
727                 case BUZ_STATE_PEND:
728                         if (zr->jpg_buffers.active == ZORAN_FREE) {
729                                 fh->jpg_buffers.active = ZORAN_FREE;
730                                 zr->jpg_buffers.allocated = 0;
731                         }
732                         res = -EBUSY;   /* what are you doing? */
733                         break;
734                 }
735         }
736
737         spin_unlock_irqrestore(&zr->spinlock, flags);
738
739         if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
740                 zr->jpg_buffers.active = fh->jpg_buffers.active;
741         }
742
743         return res;
744 }
745
746 static int
747 jpg_qbuf (struct file          *file,
748           int                   frame,
749           enum zoran_codec_mode mode)
750 {
751         struct zoran_fh *fh = file->private_data;
752         struct zoran *zr = fh->zr;
753         int res = 0;
754
755         /* Does the user want to stop streaming? */
756         if (frame < 0) {
757                 if (zr->codec_mode == mode) {
758                         if (fh->jpg_buffers.active == ZORAN_FREE) {
759                                 dprintk(1,
760                                         KERN_ERR
761                                         "%s: jpg_qbuf(-1) - session not active\n",
762                                         ZR_DEVNAME(zr));
763                                 return -EINVAL;
764                         }
765                         fh->jpg_buffers.active = zr->jpg_buffers.active =
766                             ZORAN_FREE;
767                         zr->jpg_buffers.allocated = 0;
768                         zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
769                         return 0;
770                 } else {
771                         dprintk(1,
772                                 KERN_ERR
773                                 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
774                                 ZR_DEVNAME(zr));
775                         return -EINVAL;
776                 }
777         }
778
779         if ((res = zoran_jpg_queue_frame(file, frame, mode)))
780                 return res;
781
782         /* Start the jpeg codec when the first frame is queued  */
783         if (!res && zr->jpg_que_head == 1)
784                 jpeg_start(zr);
785
786         return res;
787 }
788
789 /*
790  *   Sync on a MJPEG buffer
791  */
792
793 static int
794 jpg_sync (struct file       *file,
795           struct zoran_sync *bs)
796 {
797         struct zoran_fh *fh = file->private_data;
798         struct zoran *zr = fh->zr;
799         unsigned long flags;
800         int frame;
801
802         if (fh->jpg_buffers.active == ZORAN_FREE) {
803                 dprintk(1,
804                         KERN_ERR
805                         "%s: jpg_sync() - capture is not currently active\n",
806                         ZR_DEVNAME(zr));
807                 return -EINVAL;
808         }
809         if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
810             zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
811                 dprintk(1,
812                         KERN_ERR
813                         "%s: jpg_sync() - codec not in streaming mode\n",
814                         ZR_DEVNAME(zr));
815                 return -EINVAL;
816         }
817         if (!wait_event_interruptible_timeout(zr->jpg_capq,
818                         (zr->jpg_que_tail != zr->jpg_dma_tail ||
819                          zr->jpg_dma_tail == zr->jpg_dma_head),
820                         10*HZ)) {
821                 int isr;
822
823                 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
824                 udelay(1);
825                 zr->codec->control(zr->codec, CODEC_G_STATUS,
826                                            sizeof(isr), &isr);
827                 dprintk(1,
828                         KERN_ERR
829                         "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
830                         ZR_DEVNAME(zr), isr);
831
832                 return -ETIME;
833
834         }
835         if (signal_pending(current))
836                 return -ERESTARTSYS;
837
838         spin_lock_irqsave(&zr->spinlock, flags);
839
840         if (zr->jpg_dma_tail != zr->jpg_dma_head)
841                 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
842         else
843                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
844
845         /* buffer should now be in BUZ_STATE_DONE */
846         if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
847                 dprintk(2,
848                         KERN_ERR "%s: jpg_sync() - internal state error\n",
849                         ZR_DEVNAME(zr));
850
851         *bs = zr->jpg_buffers.buffer[frame].bs;
852         bs->frame = frame;
853         zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
854         fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
855
856         spin_unlock_irqrestore(&zr->spinlock, flags);
857
858         return 0;
859 }
860
861 static void
862 zoran_open_init_session (struct file *file)
863 {
864         int i;
865         struct zoran_fh *fh = file->private_data;
866         struct zoran *zr = fh->zr;
867
868         /* Per default, map the V4L Buffers */
869         fh->map_mode = ZORAN_MAP_MODE_RAW;
870
871         /* take over the card's current settings */
872         fh->overlay_settings = zr->overlay_settings;
873         fh->overlay_settings.is_set = 0;
874         fh->overlay_settings.format = zr->overlay_settings.format;
875         fh->overlay_active = ZORAN_FREE;
876
877         /* v4l settings */
878         fh->v4l_settings = zr->v4l_settings;
879
880         /* v4l_buffers */
881         memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
882         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
883                 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
884                 fh->v4l_buffers.buffer[i].bs.frame = i;
885         }
886         fh->v4l_buffers.allocated = 0;
887         fh->v4l_buffers.active = ZORAN_FREE;
888         fh->v4l_buffers.buffer_size = v4l_bufsize;
889         fh->v4l_buffers.num_buffers = v4l_nbufs;
890
891         /* jpg settings */
892         fh->jpg_settings = zr->jpg_settings;
893
894         /* jpg_buffers */
895         memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
896         for (i = 0; i < BUZ_MAX_FRAME; i++) {
897                 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
898                 fh->jpg_buffers.buffer[i].bs.frame = i;
899         }
900         fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
901         fh->jpg_buffers.allocated = 0;
902         fh->jpg_buffers.active = ZORAN_FREE;
903         fh->jpg_buffers.buffer_size = jpg_bufsize;
904         fh->jpg_buffers.num_buffers = jpg_nbufs;
905 }
906
907 static void
908 zoran_close_end_session (struct file *file)
909 {
910         struct zoran_fh *fh = file->private_data;
911         struct zoran *zr = fh->zr;
912
913         /* overlay */
914         if (fh->overlay_active != ZORAN_FREE) {
915                 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
916                 zr->v4l_overlay_active = 0;
917                 if (!zr->v4l_memgrab_active)
918                         zr36057_overlay(zr, 0);
919                 zr->overlay_mask = NULL;
920         }
921
922         /* v4l capture */
923         if (fh->v4l_buffers.active != ZORAN_FREE) {
924                 unsigned long flags;
925
926                 spin_lock_irqsave(&zr->spinlock, flags);
927                 zr36057_set_memgrab(zr, 0);
928                 zr->v4l_buffers.allocated = 0;
929                 zr->v4l_buffers.active = fh->v4l_buffers.active =
930                     ZORAN_FREE;
931                 spin_unlock_irqrestore(&zr->spinlock, flags);
932         }
933
934         /* v4l buffers */
935         if (fh->v4l_buffers.allocated)
936                 v4l_fbuffer_free(file);
937
938         /* jpg capture */
939         if (fh->jpg_buffers.active != ZORAN_FREE) {
940                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
941                 zr->jpg_buffers.allocated = 0;
942                 zr->jpg_buffers.active = fh->jpg_buffers.active =
943                     ZORAN_FREE;
944         }
945
946         /* jpg buffers */
947         if (fh->jpg_buffers.allocated)
948                 jpg_fbuffer_free(file);
949 }
950
951 /*
952  *   Open a zoran card. Right now the flags stuff is just playing
953  */
954
955 static int zoran_open(struct file *file)
956 {
957         struct zoran *zr = video_drvdata(file);
958         struct zoran_fh *fh;
959         int res, first_open = 0;
960
961         dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
962                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
963
964         lock_kernel();
965
966         /* see fs/device.c - the kernel already locks during open(),
967          * so locking ourselves only causes deadlocks */
968         /*mutex_lock(&zr->resource_lock);*/
969
970         if (zr->user >= 2048) {
971                 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
972                         ZR_DEVNAME(zr), zr->user);
973                 res = -EBUSY;
974                 goto fail_unlock;
975         }
976
977         if (!zr->decoder) {
978                 dprintk(1,
979                         KERN_ERR "%s: no TV decoder loaded for device!\n",
980                         ZR_DEVNAME(zr));
981                 res = -EIO;
982                 goto fail_unlock;
983         }
984
985         if (!try_module_get(zr->decoder->driver->driver.owner)) {
986                 dprintk(1,
987                         KERN_ERR
988                         "%s: failed to grab ownership of video decoder\n",
989                         ZR_DEVNAME(zr));
990                 res = -EIO;
991                 goto fail_unlock;
992         }
993         if (zr->encoder &&
994             !try_module_get(zr->encoder->driver->driver.owner)) {
995                 dprintk(1,
996                         KERN_ERR
997                         "%s: failed to grab ownership of video encoder\n",
998                         ZR_DEVNAME(zr));
999                 res = -EIO;
1000                 goto fail_decoder;
1001         }
1002
1003         /* now, create the open()-specific file_ops struct */
1004         fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1005         if (!fh) {
1006                 dprintk(1,
1007                         KERN_ERR
1008                         "%s: zoran_open() - allocation of zoran_fh failed\n",
1009                         ZR_DEVNAME(zr));
1010                 res = -ENOMEM;
1011                 goto fail_encoder;
1012         }
1013         /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1014          * on norm-change! */
1015         fh->overlay_mask =
1016             kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1017         if (!fh->overlay_mask) {
1018                 dprintk(1,
1019                         KERN_ERR
1020                         "%s: zoran_open() - allocation of overlay_mask failed\n",
1021                         ZR_DEVNAME(zr));
1022                 res = -ENOMEM;
1023                 goto fail_fh;
1024         }
1025
1026         if (zr->user++ == 0)
1027                 first_open = 1;
1028
1029         /*mutex_unlock(&zr->resource_lock);*/
1030
1031         /* default setup - TODO: look at flags */
1032         if (first_open) {       /* First device open */
1033                 zr36057_restart(zr);
1034                 zoran_open_init_params(zr);
1035                 zoran_init_hardware(zr);
1036
1037                 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1038         }
1039
1040         /* set file_ops stuff */
1041         file->private_data = fh;
1042         fh->zr = zr;
1043         zoran_open_init_session(file);
1044         unlock_kernel();
1045
1046         return 0;
1047
1048 fail_fh:
1049         kfree(fh);
1050 fail_encoder:
1051         if (zr->encoder)
1052                 module_put(zr->encoder->driver->driver.owner);
1053 fail_decoder:
1054         module_put(zr->decoder->driver->driver.owner);
1055 fail_unlock:
1056         unlock_kernel();
1057
1058         dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1059                 ZR_DEVNAME(zr), res, zr->user);
1060
1061         return res;
1062 }
1063
1064 static int
1065 zoran_close(struct file  *file)
1066 {
1067         struct zoran_fh *fh = file->private_data;
1068         struct zoran *zr = fh->zr;
1069
1070         dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1071                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1072
1073         /* kernel locks (fs/device.c), so don't do that ourselves
1074          * (prevents deadlocks) */
1075         /*mutex_lock(&zr->resource_lock);*/
1076
1077         zoran_close_end_session(file);
1078
1079         if (zr->user-- == 1) {  /* Last process */
1080                 /* Clean up JPEG process */
1081                 wake_up_interruptible(&zr->jpg_capq);
1082                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1083                 zr->jpg_buffers.allocated = 0;
1084                 zr->jpg_buffers.active = ZORAN_FREE;
1085
1086                 /* disable interrupts */
1087                 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1088
1089                 if (zr36067_debug > 1)
1090                         print_interrupts(zr);
1091
1092                 /* Overlay off */
1093                 zr->v4l_overlay_active = 0;
1094                 zr36057_overlay(zr, 0);
1095                 zr->overlay_mask = NULL;
1096
1097                 /* capture off */
1098                 wake_up_interruptible(&zr->v4l_capq);
1099                 zr36057_set_memgrab(zr, 0);
1100                 zr->v4l_buffers.allocated = 0;
1101                 zr->v4l_buffers.active = ZORAN_FREE;
1102                 zoran_set_pci_master(zr, 0);
1103
1104                 if (!pass_through) {    /* Switch to color bar */
1105                         struct v4l2_routing route = { 2, 0 };
1106
1107                         decoder_command(zr, VIDIOC_STREAMOFF, 0);
1108                         encoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1109                 }
1110         }
1111
1112         file->private_data = NULL;
1113         kfree(fh->overlay_mask);
1114         kfree(fh);
1115
1116         /* release locks on the i2c modules */
1117         module_put(zr->decoder->driver->driver.owner);
1118         if (zr->encoder)
1119                 module_put(zr->encoder->driver->driver.owner);
1120
1121         /*mutex_unlock(&zr->resource_lock);*/
1122
1123         dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1124
1125         return 0;
1126 }
1127
1128
1129 static ssize_t
1130 zoran_read (struct file *file,
1131             char        __user *data,
1132             size_t       count,
1133             loff_t      *ppos)
1134 {
1135         /* we simply don't support read() (yet)... */
1136
1137         return -EINVAL;
1138 }
1139
1140 static ssize_t
1141 zoran_write (struct file *file,
1142              const char  __user *data,
1143              size_t       count,
1144              loff_t      *ppos)
1145 {
1146         /* ...and the same goes for write() */
1147
1148         return -EINVAL;
1149 }
1150
1151 static int
1152 setup_fbuffer (struct file               *file,
1153                void                      *base,
1154                const struct zoran_format *fmt,
1155                int                        width,
1156                int                        height,
1157                int                        bytesperline)
1158 {
1159         struct zoran_fh *fh = file->private_data;
1160         struct zoran *zr = fh->zr;
1161
1162         /* (Ronald) v4l/v4l2 guidelines */
1163         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1164                 return -EPERM;
1165
1166         /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1167            ALi Magik (that needs very low latency while the card needs a
1168            higher value always) */
1169
1170         if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1171                 return -ENXIO;
1172
1173         /* we need a bytesperline value, even if not given */
1174         if (!bytesperline)
1175                 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1176
1177 #if 0
1178         if (zr->overlay_active) {
1179                 /* dzjee... stupid users... don't even bother to turn off
1180                  * overlay before changing the memory location...
1181                  * normally, we would return errors here. However, one of
1182                  * the tools that does this is... xawtv! and since xawtv
1183                  * is used by +/- 99% of the users, we'd rather be user-
1184                  * friendly and silently do as if nothing went wrong */
1185                 dprintk(3,
1186                         KERN_ERR
1187                         "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1188                         ZR_DEVNAME(zr));
1189                 zr36057_overlay(zr, 0);
1190         }
1191 #endif
1192
1193         if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1194                 dprintk(1,
1195                         KERN_ERR
1196                         "%s: setup_fbuffer() - no valid overlay format given\n",
1197                         ZR_DEVNAME(zr));
1198                 return -EINVAL;
1199         }
1200         if (height <= 0 || width <= 0 || bytesperline <= 0) {
1201                 dprintk(1,
1202                         KERN_ERR
1203                         "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1204                         ZR_DEVNAME(zr), width, height, bytesperline);
1205                 return -EINVAL;
1206         }
1207         if (bytesperline & 3) {
1208                 dprintk(1,
1209                         KERN_ERR
1210                         "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1211                         ZR_DEVNAME(zr), bytesperline);
1212                 return -EINVAL;
1213         }
1214
1215         zr->buffer.base = (void *) ((unsigned long) base & ~3);
1216         zr->buffer.height = height;
1217         zr->buffer.width = width;
1218         zr->buffer.depth = fmt->depth;
1219         zr->overlay_settings.format = fmt;
1220         zr->buffer.bytesperline = bytesperline;
1221
1222         /* The user should set new window parameters */
1223         zr->overlay_settings.is_set = 0;
1224
1225         return 0;
1226 }
1227
1228
1229 static int
1230 setup_window (struct file       *file,
1231               int                x,
1232               int                y,
1233               int                width,
1234               int                height,
1235               struct video_clip __user *clips,
1236               int                clipcount,
1237               void              __user *bitmap)
1238 {
1239         struct zoran_fh *fh = file->private_data;
1240         struct zoran *zr = fh->zr;
1241         struct video_clip *vcp = NULL;
1242         int on, end;
1243
1244
1245         if (!zr->buffer.base) {
1246                 dprintk(1,
1247                         KERN_ERR
1248                         "%s: setup_window() - frame buffer has to be set first\n",
1249                         ZR_DEVNAME(zr));
1250                 return -EINVAL;
1251         }
1252
1253         if (!fh->overlay_settings.format) {
1254                 dprintk(1,
1255                         KERN_ERR
1256                         "%s: setup_window() - no overlay format set\n",
1257                         ZR_DEVNAME(zr));
1258                 return -EINVAL;
1259         }
1260
1261         /*
1262          * The video front end needs 4-byte alinged line sizes, we correct that
1263          * silently here if necessary
1264          */
1265         if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1266                 end = (x + width) & ~1; /* round down */
1267                 x = (x + 1) & ~1;       /* round up */
1268                 width = end - x;
1269         }
1270
1271         if (zr->buffer.depth == 24) {
1272                 end = (x + width) & ~3; /* round down */
1273                 x = (x + 3) & ~3;       /* round up */
1274                 width = end - x;
1275         }
1276
1277         if (width > BUZ_MAX_WIDTH)
1278                 width = BUZ_MAX_WIDTH;
1279         if (height > BUZ_MAX_HEIGHT)
1280                 height = BUZ_MAX_HEIGHT;
1281
1282         /* Check for vaild parameters */
1283         if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1284             width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1285                 dprintk(1,
1286                         KERN_ERR
1287                         "%s: setup_window() - width = %d or height = %d invalid\n",
1288                         ZR_DEVNAME(zr), width, height);
1289                 return -EINVAL;
1290         }
1291
1292         fh->overlay_settings.x = x;
1293         fh->overlay_settings.y = y;
1294         fh->overlay_settings.width = width;
1295         fh->overlay_settings.height = height;
1296         fh->overlay_settings.clipcount = clipcount;
1297
1298         /*
1299          * If an overlay is running, we have to switch it off
1300          * and switch it on again in order to get the new settings in effect.
1301          *
1302          * We also want to avoid that the overlay mask is written
1303          * when an overlay is running.
1304          */
1305
1306         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1307             zr->overlay_active != ZORAN_FREE &&
1308             fh->overlay_active != ZORAN_FREE;
1309         if (on)
1310                 zr36057_overlay(zr, 0);
1311
1312         /*
1313          *   Write the overlay mask if clips are wanted.
1314          *   We prefer a bitmap.
1315          */
1316         if (bitmap) {
1317                 /* fake value - it just means we want clips */
1318                 fh->overlay_settings.clipcount = 1;
1319
1320                 if (copy_from_user(fh->overlay_mask, bitmap,
1321                                    (width * height + 7) / 8)) {
1322                         return -EFAULT;
1323                 }
1324         } else if (clipcount > 0) {
1325                 /* write our own bitmap from the clips */
1326                 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1327                 if (vcp == NULL) {
1328                         dprintk(1,
1329                                 KERN_ERR
1330                                 "%s: setup_window() - Alloc of clip mask failed\n",
1331                                 ZR_DEVNAME(zr));
1332                         return -ENOMEM;
1333                 }
1334                 if (copy_from_user
1335                     (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1336                         vfree(vcp);
1337                         return -EFAULT;
1338                 }
1339                 write_overlay_mask(file, vcp, clipcount);
1340                 vfree(vcp);
1341         }
1342
1343         fh->overlay_settings.is_set = 1;
1344         if (fh->overlay_active != ZORAN_FREE &&
1345             zr->overlay_active != ZORAN_FREE)
1346                 zr->overlay_settings = fh->overlay_settings;
1347
1348         if (on)
1349                 zr36057_overlay(zr, 1);
1350
1351         /* Make sure the changes come into effect */
1352         return wait_grab_pending(zr);
1353 }
1354
1355 static int
1356 setup_overlay (struct file *file,
1357                int          on)
1358 {
1359         struct zoran_fh *fh = file->private_data;
1360         struct zoran *zr = fh->zr;
1361
1362         /* If there is nothing to do, return immediatly */
1363         if ((on && fh->overlay_active != ZORAN_FREE) ||
1364             (!on && fh->overlay_active == ZORAN_FREE))
1365                 return 0;
1366
1367         /* check whether we're touching someone else's overlay */
1368         if (on && zr->overlay_active != ZORAN_FREE &&
1369             fh->overlay_active == ZORAN_FREE) {
1370                 dprintk(1,
1371                         KERN_ERR
1372                         "%s: setup_overlay() - overlay is already active for another session\n",
1373                         ZR_DEVNAME(zr));
1374                 return -EBUSY;
1375         }
1376         if (!on && zr->overlay_active != ZORAN_FREE &&
1377             fh->overlay_active == ZORAN_FREE) {
1378                 dprintk(1,
1379                         KERN_ERR
1380                         "%s: setup_overlay() - you cannot cancel someone else's session\n",
1381                         ZR_DEVNAME(zr));
1382                 return -EPERM;
1383         }
1384
1385         if (on == 0) {
1386                 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1387                 zr->v4l_overlay_active = 0;
1388                 /* When a grab is running, the video simply
1389                  * won't be switched on any more */
1390                 if (!zr->v4l_memgrab_active)
1391                         zr36057_overlay(zr, 0);
1392                 zr->overlay_mask = NULL;
1393         } else {
1394                 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1395                         dprintk(1,
1396                                 KERN_ERR
1397                                 "%s: setup_overlay() - buffer or window not set\n",
1398                                 ZR_DEVNAME(zr));
1399                         return -EINVAL;
1400                 }
1401                 if (!fh->overlay_settings.format) {
1402                         dprintk(1,
1403                                 KERN_ERR
1404                                 "%s: setup_overlay() - no overlay format set\n",
1405                                 ZR_DEVNAME(zr));
1406                         return -EINVAL;
1407                 }
1408                 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1409                 zr->v4l_overlay_active = 1;
1410                 zr->overlay_mask = fh->overlay_mask;
1411                 zr->overlay_settings = fh->overlay_settings;
1412                 if (!zr->v4l_memgrab_active)
1413                         zr36057_overlay(zr, 1);
1414                 /* When a grab is running, the video will be
1415                  * switched on when grab is finished */
1416         }
1417
1418         /* Make sure the changes come into effect */
1419         return wait_grab_pending(zr);
1420 }
1421
1422         /* get the status of a buffer in the clients buffer queue */
1423 static int
1424 zoran_v4l2_buffer_status (struct file        *file,
1425                           struct v4l2_buffer *buf,
1426                           int                 num)
1427 {
1428         struct zoran_fh *fh = file->private_data;
1429         struct zoran *zr = fh->zr;
1430
1431         buf->flags = V4L2_BUF_FLAG_MAPPED;
1432
1433         switch (fh->map_mode) {
1434         case ZORAN_MAP_MODE_RAW:
1435
1436                 /* check range */
1437                 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1438                     !fh->v4l_buffers.allocated) {
1439                         dprintk(1,
1440                                 KERN_ERR
1441                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1442                                 ZR_DEVNAME(zr));
1443                         return -EINVAL;
1444                 }
1445
1446                 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1447                 buf->length = fh->v4l_buffers.buffer_size;
1448
1449                 /* get buffer */
1450                 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1451                 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1452                     fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1453                         buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1454                         buf->flags |= V4L2_BUF_FLAG_DONE;
1455                         buf->timestamp =
1456                             fh->v4l_buffers.buffer[num].bs.timestamp;
1457                 } else {
1458                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1459                 }
1460
1461                 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1462                         buf->field = V4L2_FIELD_TOP;
1463                 else
1464                         buf->field = V4L2_FIELD_INTERLACED;
1465
1466                 break;
1467
1468         case ZORAN_MAP_MODE_JPG_REC:
1469         case ZORAN_MAP_MODE_JPG_PLAY:
1470
1471                 /* check range */
1472                 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1473                     !fh->jpg_buffers.allocated) {
1474                         dprintk(1,
1475                                 KERN_ERR
1476                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1477                                 ZR_DEVNAME(zr));
1478                         return -EINVAL;
1479                 }
1480
1481                 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1482                               V4L2_BUF_TYPE_VIDEO_CAPTURE :
1483                               V4L2_BUF_TYPE_VIDEO_OUTPUT;
1484                 buf->length = fh->jpg_buffers.buffer_size;
1485
1486                 /* these variables are only written after frame has been captured */
1487                 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1488                     fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1489                         buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1490                         buf->timestamp =
1491                             fh->jpg_buffers.buffer[num].bs.timestamp;
1492                         buf->bytesused =
1493                             fh->jpg_buffers.buffer[num].bs.length;
1494                         buf->flags |= V4L2_BUF_FLAG_DONE;
1495                 } else {
1496                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1497                 }
1498
1499                 /* which fields are these? */
1500                 if (fh->jpg_settings.TmpDcm != 1)
1501                         buf->field =
1502                             fh->jpg_settings.
1503                             odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1504                 else
1505                         buf->field =
1506                             fh->jpg_settings.
1507                             odd_even ? V4L2_FIELD_SEQ_TB :
1508                             V4L2_FIELD_SEQ_BT;
1509
1510                 break;
1511
1512         default:
1513
1514                 dprintk(5,
1515                         KERN_ERR
1516                         "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1517                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
1518                 return -EINVAL;
1519         }
1520
1521         buf->memory = V4L2_MEMORY_MMAP;
1522         buf->index = num;
1523         buf->m.offset = buf->length * num;
1524
1525         return 0;
1526 }
1527
1528 static int
1529 zoran_set_norm (struct zoran *zr,
1530                 v4l2_std_id norm)
1531 {
1532         int on;
1533
1534         if (zr->v4l_buffers.active != ZORAN_FREE ||
1535             zr->jpg_buffers.active != ZORAN_FREE) {
1536                 dprintk(1,
1537                         KERN_WARNING
1538                         "%s: set_norm() called while in playback/capture mode\n",
1539                         ZR_DEVNAME(zr));
1540                 return -EBUSY;
1541         }
1542
1543         if (lock_norm && norm != zr->norm) {
1544                 if (lock_norm > 1) {
1545                         dprintk(1,
1546                                 KERN_WARNING
1547                                 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1548                                 ZR_DEVNAME(zr));
1549                         return -EPERM;
1550                 } else {
1551                         dprintk(1,
1552                                 KERN_WARNING
1553                                 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1554                                 ZR_DEVNAME(zr));
1555                         norm = zr->norm;
1556                 }
1557         }
1558
1559         if (!(norm & zr->card.norms)) {
1560                 dprintk(1,
1561                         KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
1562                         ZR_DEVNAME(zr), norm);
1563                 return -EINVAL;
1564         }
1565
1566         if (norm == V4L2_STD_ALL) {
1567                 int status = 0;
1568                 v4l2_std_id std = 0;
1569
1570                 decoder_command(zr, VIDIOC_QUERYSTD, &std);
1571                 decoder_command(zr, VIDIOC_S_STD, &std);
1572
1573                 /* let changes come into effect */
1574                 ssleep(2);
1575
1576                 decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
1577                 if (status & V4L2_IN_ST_NO_SIGNAL) {
1578                         dprintk(1,
1579                                 KERN_ERR
1580                                 "%s: set_norm() - no norm detected\n",
1581                                 ZR_DEVNAME(zr));
1582                         /* reset norm */
1583                         decoder_command(zr, VIDIOC_S_STD, &zr->norm);
1584                         return -EIO;
1585                 }
1586
1587                 norm = std;
1588         }
1589         if (norm & V4L2_STD_SECAM)
1590                 zr->timing = zr->card.tvn[2];
1591         else if (norm & V4L2_STD_NTSC)
1592                 zr->timing = zr->card.tvn[1];
1593         else
1594                 zr->timing = zr->card.tvn[0];
1595
1596         /* We switch overlay off and on since a change in the
1597          * norm needs different VFE settings */
1598         on = zr->overlay_active && !zr->v4l_memgrab_active;
1599         if (on)
1600                 zr36057_overlay(zr, 0);
1601
1602         decoder_command(zr, VIDIOC_S_STD, &norm);
1603         encoder_command(zr, VIDIOC_INT_S_STD_OUTPUT, &norm);
1604
1605         if (on)
1606                 zr36057_overlay(zr, 1);
1607
1608         /* Make sure the changes come into effect */
1609         zr->norm = norm;
1610
1611         return 0;
1612 }
1613
1614 static int
1615 zoran_set_input (struct zoran *zr,
1616                  int           input)
1617 {
1618         struct v4l2_routing route = { 0, 0 };
1619
1620         if (input == zr->input) {
1621                 return 0;
1622         }
1623
1624         if (zr->v4l_buffers.active != ZORAN_FREE ||
1625             zr->jpg_buffers.active != ZORAN_FREE) {
1626                 dprintk(1,
1627                         KERN_WARNING
1628                         "%s: set_input() called while in playback/capture mode\n",
1629                         ZR_DEVNAME(zr));
1630                 return -EBUSY;
1631         }
1632
1633         if (input < 0 || input >= zr->card.inputs) {
1634                 dprintk(1,
1635                         KERN_ERR
1636                         "%s: set_input() - unnsupported input %d\n",
1637                         ZR_DEVNAME(zr), input);
1638                 return -EINVAL;
1639         }
1640
1641         route.input = zr->card.input[input].muxsel;
1642         zr->input = input;
1643
1644         decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1645
1646         return 0;
1647 }
1648
1649 /*
1650  *   ioctl routine
1651  */
1652
1653 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1654 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1655 {
1656         struct zoran_fh *fh = __fh;
1657         struct zoran *zr = fh->zr;
1658         struct zoran_jpg_settings settings;
1659
1660         switch (cmd) {
1661         case BUZIOC_G_PARAMS:
1662         {
1663                 struct zoran_params *bparams = arg;
1664
1665                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1666
1667                 memset(bparams, 0, sizeof(struct zoran_params));
1668                 bparams->major_version = MAJOR_VERSION;
1669                 bparams->minor_version = MINOR_VERSION;
1670
1671                 mutex_lock(&zr->resource_lock);
1672
1673                 if (zr->norm & V4L2_STD_NTSC)
1674                         bparams->norm = VIDEO_MODE_NTSC;
1675                 else if (zr->norm & V4L2_STD_PAL)
1676                         bparams->norm = VIDEO_MODE_PAL;
1677                 else
1678                         bparams->norm = VIDEO_MODE_SECAM;
1679
1680                 bparams->input = zr->input;
1681
1682                 bparams->decimation = fh->jpg_settings.decimation;
1683                 bparams->HorDcm = fh->jpg_settings.HorDcm;
1684                 bparams->VerDcm = fh->jpg_settings.VerDcm;
1685                 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1686                 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1687                 bparams->img_x = fh->jpg_settings.img_x;
1688                 bparams->img_y = fh->jpg_settings.img_y;
1689                 bparams->img_width = fh->jpg_settings.img_width;
1690                 bparams->img_height = fh->jpg_settings.img_height;
1691                 bparams->odd_even = fh->jpg_settings.odd_even;
1692
1693                 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1694                 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1695                 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1696                 memcpy(bparams->APP_data,
1697                        fh->jpg_settings.jpg_comp.APP_data,
1698                        sizeof(bparams->APP_data));
1699                 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1700                 memcpy(bparams->COM_data,
1701                        fh->jpg_settings.jpg_comp.COM_data,
1702                        sizeof(bparams->COM_data));
1703                 bparams->jpeg_markers =
1704                     fh->jpg_settings.jpg_comp.jpeg_markers;
1705
1706                 mutex_unlock(&zr->resource_lock);
1707
1708                 bparams->VFIFO_FB = 0;
1709
1710                 return 0;
1711         }
1712
1713         case BUZIOC_S_PARAMS:
1714         {
1715                 struct zoran_params *bparams = arg;
1716                 int res = 0;
1717
1718                 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1719
1720                 settings.decimation = bparams->decimation;
1721                 settings.HorDcm = bparams->HorDcm;
1722                 settings.VerDcm = bparams->VerDcm;
1723                 settings.TmpDcm = bparams->TmpDcm;
1724                 settings.field_per_buff = bparams->field_per_buff;
1725                 settings.img_x = bparams->img_x;
1726                 settings.img_y = bparams->img_y;
1727                 settings.img_width = bparams->img_width;
1728                 settings.img_height = bparams->img_height;
1729                 settings.odd_even = bparams->odd_even;
1730
1731                 settings.jpg_comp.quality = bparams->quality;
1732                 settings.jpg_comp.APPn = bparams->APPn;
1733                 settings.jpg_comp.APP_len = bparams->APP_len;
1734                 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1735                        sizeof(bparams->APP_data));
1736                 settings.jpg_comp.COM_len = bparams->COM_len;
1737                 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1738                        sizeof(bparams->COM_data));
1739                 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1740
1741                 mutex_lock(&zr->resource_lock);
1742
1743                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1744                         dprintk(1,
1745                                 KERN_ERR
1746                                 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1747                                 ZR_DEVNAME(zr));
1748                         res = -EINVAL;
1749                         goto sparams_unlock_and_return;
1750                 }
1751
1752                 /* Check the params first before overwriting our
1753                  * nternal values */
1754                 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1755                         res = -EINVAL;
1756                         goto sparams_unlock_and_return;
1757                 }
1758
1759                 fh->jpg_settings = settings;
1760 sparams_unlock_and_return:
1761                 mutex_unlock(&zr->resource_lock);
1762
1763                 return res;
1764         }
1765
1766         case BUZIOC_REQBUFS:
1767         {
1768                 struct zoran_requestbuffers *breq = arg;
1769                 int res = 0;
1770
1771                 dprintk(3,
1772                         KERN_DEBUG
1773                         "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1774                         ZR_DEVNAME(zr), breq->count, breq->size);
1775
1776                 /* Enforce reasonable lower and upper limits */
1777                 if (breq->count < 4)
1778                         breq->count = 4;        /* Could be choosen smaller */
1779                 if (breq->count > jpg_nbufs)
1780                         breq->count = jpg_nbufs;
1781                 breq->size = PAGE_ALIGN(breq->size);
1782                 if (breq->size < 8192)
1783                         breq->size = 8192;      /* Arbitrary */
1784                 /* breq->size is limited by 1 page for the stat_com
1785                  * tables to a Maximum of 2 MB */
1786                 if (breq->size > jpg_bufsize)
1787                         breq->size = jpg_bufsize;
1788
1789                 mutex_lock(&zr->resource_lock);
1790
1791                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1792                         dprintk(1,
1793                                 KERN_ERR
1794                                 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1795                                 ZR_DEVNAME(zr));
1796                         res = -EBUSY;
1797                         goto jpgreqbuf_unlock_and_return;
1798                 }
1799
1800                 fh->jpg_buffers.num_buffers = breq->count;
1801                 fh->jpg_buffers.buffer_size = breq->size;
1802
1803                 if (jpg_fbuffer_alloc(file)) {
1804                         res = -ENOMEM;
1805                         goto jpgreqbuf_unlock_and_return;
1806                 }
1807
1808                 /* The next mmap will map the MJPEG buffers - could
1809                  * also be *_PLAY, but it doesn't matter here */
1810                 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1811 jpgreqbuf_unlock_and_return:
1812                 mutex_unlock(&zr->resource_lock);
1813
1814                 return res;
1815         }
1816
1817         case BUZIOC_QBUF_CAPT:
1818         {
1819                 int *frame = arg, res;
1820
1821                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1822                         ZR_DEVNAME(zr), *frame);
1823
1824                 mutex_lock(&zr->resource_lock);
1825                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1826                 mutex_unlock(&zr->resource_lock);
1827
1828                 return res;
1829         }
1830
1831         case BUZIOC_QBUF_PLAY:
1832         {
1833                 int *frame = arg, res;
1834
1835                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1836                         ZR_DEVNAME(zr), *frame);
1837
1838                 mutex_lock(&zr->resource_lock);
1839                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1840                 mutex_unlock(&zr->resource_lock);
1841
1842                 return res;
1843         }
1844
1845         case BUZIOC_SYNC:
1846         {
1847                 struct zoran_sync *bsync = arg;
1848                 int res;
1849
1850                 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1851
1852                 mutex_lock(&zr->resource_lock);
1853                 res = jpg_sync(file, bsync);
1854                 mutex_unlock(&zr->resource_lock);
1855
1856                 return res;
1857         }
1858
1859         case BUZIOC_G_STATUS:
1860         {
1861                 struct zoran_status *bstat = arg;
1862                 struct v4l2_routing route = { 0, 0 };
1863                 int status = 0, res = 0;
1864                 v4l2_std_id norm;
1865
1866                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1867
1868                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1869                         dprintk(1,
1870                                 KERN_ERR
1871                                 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1872                                 ZR_DEVNAME(zr));
1873                         return -EINVAL;
1874                 }
1875
1876                 route.input = zr->card.input[bstat->input].muxsel;
1877
1878                 mutex_lock(&zr->resource_lock);
1879
1880                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1881                         dprintk(1,
1882                                 KERN_ERR
1883                                 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1884                                 ZR_DEVNAME(zr));
1885                         res = -EINVAL;
1886                         goto gstat_unlock_and_return;
1887                 }
1888
1889                 decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1890
1891                 /* sleep 1 second */
1892                 ssleep(1);
1893
1894                 /* Get status of video decoder */
1895                 decoder_command(zr, VIDIOC_QUERYSTD, &norm);
1896                 decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
1897
1898                 /* restore previous input and norm */
1899                 route.input = zr->card.input[zr->input].muxsel;
1900                 decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1901 gstat_unlock_and_return:
1902                 mutex_unlock(&zr->resource_lock);
1903
1904                 if (!res) {
1905                         bstat->signal =
1906                             (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1907                         if (norm & V4L2_STD_NTSC)
1908                                 bstat->norm = VIDEO_MODE_NTSC;
1909                         else if (norm & V4L2_STD_SECAM)
1910                                 bstat->norm = VIDEO_MODE_SECAM;
1911                         else
1912                                 bstat->norm = VIDEO_MODE_PAL;
1913
1914                         bstat->color =
1915                             (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1916                 }
1917
1918                 return res;
1919         }
1920
1921         default:
1922                 return -EINVAL;
1923         }
1924 }
1925
1926 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1927 {
1928         struct zoran_fh *fh = __fh;
1929         struct zoran *zr = fh->zr;
1930         int i, res = 0;
1931
1932         vmbuf->size =
1933             fh->v4l_buffers.num_buffers *
1934             fh->v4l_buffers.buffer_size;
1935         vmbuf->frames = fh->v4l_buffers.num_buffers;
1936         for (i = 0; i < vmbuf->frames; i++) {
1937                 vmbuf->offsets[i] =
1938                     i * fh->v4l_buffers.buffer_size;
1939         }
1940
1941         mutex_lock(&zr->resource_lock);
1942
1943         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1944                 dprintk(1,
1945                         KERN_ERR
1946                         "%s: VIDIOCGMBUF - buffers already allocated\n",
1947                         ZR_DEVNAME(zr));
1948                 res = -EINVAL;
1949                 goto v4l1reqbuf_unlock_and_return;
1950         }
1951
1952         if (v4l_fbuffer_alloc(file)) {
1953                 res = -ENOMEM;
1954                 goto v4l1reqbuf_unlock_and_return;
1955         }
1956
1957         /* The next mmap will map the V4L buffers */
1958         fh->map_mode = ZORAN_MAP_MODE_RAW;
1959 v4l1reqbuf_unlock_and_return:
1960         mutex_unlock(&zr->resource_lock);
1961
1962         return res;
1963 }
1964 #endif
1965
1966 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1967 {
1968         struct zoran_fh *fh = __fh;
1969         struct zoran *zr = fh->zr;
1970
1971         memset(cap, 0, sizeof(*cap));
1972         strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1973         strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1974         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1975                  pci_name(zr->pci_dev));
1976         cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1977                            RELEASE_VERSION);
1978         cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1979                             V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1980         return 0;
1981 }
1982
1983 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1984 {
1985         int num = -1, i;
1986
1987         for (i = 0; i < NUM_FORMATS; i++) {
1988                 if (zoran_formats[i].flags & flag)
1989                         num++;
1990                 if (num == fmt->index)
1991                         break;
1992         }
1993         if (fmt->index < 0 /* late, but not too late */  || i == NUM_FORMATS)
1994                 return -EINVAL;
1995
1996         strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
1997         fmt->pixelformat = zoran_formats[i].fourcc;
1998         if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1999                 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2000         return 0;
2001 }
2002
2003 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
2004                                             struct v4l2_fmtdesc *f)
2005 {
2006         struct zoran_fh *fh = __fh;
2007         struct zoran *zr = fh->zr;
2008
2009         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
2010 }
2011
2012 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
2013                                             struct v4l2_fmtdesc *f)
2014 {
2015         struct zoran_fh *fh = __fh;
2016         struct zoran *zr = fh->zr;
2017
2018         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
2019 }
2020
2021 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
2022                                             struct v4l2_fmtdesc *f)
2023 {
2024         struct zoran_fh *fh = __fh;
2025         struct zoran *zr = fh->zr;
2026
2027         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
2028 }
2029
2030 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
2031                                         struct v4l2_format *fmt)
2032 {
2033         struct zoran_fh *fh = __fh;
2034         struct zoran *zr = fh->zr;
2035
2036         mutex_lock(&zr->resource_lock);
2037
2038         fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
2039         fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
2040                 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
2041         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2042         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
2043         if (fh->jpg_settings.TmpDcm == 1)
2044                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2045                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2046         else
2047                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2048                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2049         fmt->fmt.pix.bytesperline = 0;
2050         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2051
2052         mutex_unlock(&zr->resource_lock);
2053         return 0;
2054 }
2055
2056 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2057                                         struct v4l2_format *fmt)
2058 {
2059         struct zoran_fh *fh = __fh;
2060         struct zoran *zr = fh->zr;
2061
2062         if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2063                 return zoran_g_fmt_vid_out(file, fh, fmt);
2064
2065         mutex_lock(&zr->resource_lock);
2066         fmt->fmt.pix.width = fh->v4l_settings.width;
2067         fmt->fmt.pix.height = fh->v4l_settings.height;
2068         fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2069                                         fh->v4l_settings.height;
2070         fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2071         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2072         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2073         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2074                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2075         else
2076                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2077         mutex_unlock(&zr->resource_lock);
2078         return 0;
2079 }
2080
2081 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2082                                         struct v4l2_format *fmt)
2083 {
2084         struct zoran_fh *fh = __fh;
2085         struct zoran *zr = fh->zr;
2086
2087         mutex_lock(&zr->resource_lock);
2088
2089         fmt->fmt.win.w.left = fh->overlay_settings.x;
2090         fmt->fmt.win.w.top = fh->overlay_settings.y;
2091         fmt->fmt.win.w.width = fh->overlay_settings.width;
2092         fmt->fmt.win.w.height = fh->overlay_settings.height;
2093         if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2094                 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2095         else
2096                 fmt->fmt.win.field = V4L2_FIELD_TOP;
2097
2098         mutex_unlock(&zr->resource_lock);
2099         return 0;
2100 }
2101
2102 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2103                                         struct v4l2_format *fmt)
2104 {
2105         struct zoran_fh *fh = __fh;
2106         struct zoran *zr = fh->zr;
2107
2108         mutex_lock(&zr->resource_lock);
2109
2110         if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2111                 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2112         if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2113                 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2114         if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2115                 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2116         if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2117                 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2118
2119         mutex_unlock(&zr->resource_lock);
2120         return 0;
2121 }
2122
2123 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2124                                         struct v4l2_format *fmt)
2125 {
2126         struct zoran_fh *fh = __fh;
2127         struct zoran *zr = fh->zr;
2128         struct zoran_jpg_settings settings;
2129         int res = 0;
2130
2131         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2132                 return -EINVAL;
2133
2134         fmt->fmt.pix.bytesperline = 0;
2135
2136         mutex_lock(&zr->resource_lock);
2137         settings = fh->jpg_settings;
2138
2139         /* we actually need to set 'real' parameters now */
2140         if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2141                 settings.TmpDcm = 1;
2142         else
2143                 settings.TmpDcm = 2;
2144         settings.decimation = 0;
2145         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2146                 settings.VerDcm = 2;
2147         else
2148                 settings.VerDcm = 1;
2149         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2150                 settings.HorDcm = 4;
2151         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2152                 settings.HorDcm = 2;
2153         else
2154                 settings.HorDcm = 1;
2155         if (settings.TmpDcm == 1)
2156                 settings.field_per_buff = 2;
2157         else
2158                 settings.field_per_buff = 1;
2159
2160         /* check */
2161         res = zoran_check_jpg_settings(zr, &settings, 1);
2162         if (res)
2163                 goto tryfmt_unlock_and_return;
2164
2165         /* tell the user what we actually did */
2166         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2167         fmt->fmt.pix.height = settings.img_height * 2 /
2168                 (settings.TmpDcm * settings.VerDcm);
2169         if (settings.TmpDcm == 1)
2170                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2171                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2172         else
2173                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2174                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2175
2176         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2177 tryfmt_unlock_and_return:
2178         mutex_unlock(&zr->resource_lock);
2179         return res;
2180 }
2181
2182 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2183                                         struct v4l2_format *fmt)
2184 {
2185         struct zoran_fh *fh = __fh;
2186         struct zoran *zr = fh->zr;
2187         int bpp;
2188         int i;
2189
2190         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2191                 return zoran_try_fmt_vid_out(file, fh, fmt);
2192
2193         mutex_lock(&zr->resource_lock);
2194
2195         for (i = 0; i < NUM_FORMATS; i++)
2196                 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2197                         break;
2198
2199         if (i == NUM_FORMATS) {
2200                 mutex_unlock(&zr->resource_lock);
2201                 return -EINVAL;
2202         }
2203
2204         bpp = (zoran_formats[i].depth + 7) / 8;
2205         fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2206         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2207                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2208         if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2209                 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2210         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2211                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2212         if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2213                 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2214         mutex_unlock(&zr->resource_lock);
2215
2216         return 0;
2217 }
2218
2219 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2220                                         struct v4l2_format *fmt)
2221 {
2222         struct zoran_fh *fh = __fh;
2223         struct zoran *zr = fh->zr;
2224         int res;
2225
2226         dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2227                         fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2228                         fmt->fmt.win.w.width,
2229                         fmt->fmt.win.w.height,
2230                         fmt->fmt.win.clipcount,
2231                         fmt->fmt.win.bitmap);
2232         mutex_lock(&zr->resource_lock);
2233         res = setup_window(file, fmt->fmt.win.w.left,
2234                         fmt->fmt.win.w.top,
2235                         fmt->fmt.win.w.width,
2236                         fmt->fmt.win.w.height,
2237                         (struct video_clip __user *)
2238                         fmt->fmt.win.clips,
2239                         fmt->fmt.win.clipcount,
2240                         fmt->fmt.win.bitmap);
2241         mutex_unlock(&zr->resource_lock);
2242         return res;
2243 }
2244
2245 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2246                                         struct v4l2_format *fmt)
2247 {
2248         struct zoran_fh *fh = __fh;
2249         struct zoran *zr = fh->zr;
2250         __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2251         struct zoran_jpg_settings settings;
2252         int res = 0;
2253
2254         dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2255                         fmt->fmt.pix.width, fmt->fmt.pix.height,
2256                         fmt->fmt.pix.pixelformat,
2257                         (char *) &printformat);
2258         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2259                 return -EINVAL;
2260
2261         mutex_lock(&zr->resource_lock);
2262
2263         settings = fh->jpg_settings;
2264
2265         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2266                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2267                                 ZR_DEVNAME(zr));
2268                 res = -EBUSY;
2269                 goto sfmtjpg_unlock_and_return;
2270         }
2271
2272         /* we actually need to set 'real' parameters now */
2273         if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2274                 settings.TmpDcm = 1;
2275         else
2276                 settings.TmpDcm = 2;
2277         settings.decimation = 0;
2278         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2279                 settings.VerDcm = 2;
2280         else
2281                 settings.VerDcm = 1;
2282         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2283                 settings.HorDcm = 4;
2284         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2285                 settings.HorDcm = 2;
2286         else
2287                 settings.HorDcm = 1;
2288         if (settings.TmpDcm == 1)
2289                 settings.field_per_buff = 2;
2290         else
2291                 settings.field_per_buff = 1;
2292
2293         if (settings.HorDcm > 1) {
2294                 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2295                 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2296         } else {
2297                 settings.img_x = 0;
2298                 settings.img_width = BUZ_MAX_WIDTH;
2299         }
2300
2301         /* check */
2302         res = zoran_check_jpg_settings(zr, &settings, 0);
2303         if (res)
2304                 goto sfmtjpg_unlock_and_return;
2305
2306         /* it's ok, so set them */
2307         fh->jpg_settings = settings;
2308
2309         /* tell the user what we actually did */
2310         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2311         fmt->fmt.pix.height = settings.img_height * 2 /
2312                 (settings.TmpDcm * settings.VerDcm);
2313         if (settings.TmpDcm == 1)
2314                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2315                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2316         else
2317                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2318                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2319         fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2320         fmt->fmt.pix.bytesperline = 0;
2321         fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2322         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2323
2324         /* we hereby abuse this variable to show that
2325          * we're gonna do mjpeg capture */
2326         fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2327                 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2328 sfmtjpg_unlock_and_return:
2329         mutex_unlock(&zr->resource_lock);
2330         return res;
2331 }
2332
2333 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2334                                         struct v4l2_format *fmt)
2335 {
2336         struct zoran_fh *fh = __fh;
2337         struct zoran *zr = fh->zr;
2338         int i;
2339         int res = 0;
2340
2341         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2342                 return zoran_s_fmt_vid_out(file, fh, fmt);
2343
2344         for (i = 0; i < NUM_FORMATS; i++)
2345                 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2346                         break;
2347         if (i == NUM_FORMATS) {
2348                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2349                         ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2350                 return -EINVAL;
2351         }
2352         mutex_lock(&zr->resource_lock);
2353         if (fh->jpg_buffers.allocated ||
2354                 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2355                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2356                                 ZR_DEVNAME(zr));
2357                 res = -EBUSY;
2358                 goto sfmtv4l_unlock_and_return;
2359         }
2360         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2361                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2362         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2363                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2364
2365         res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2366                         fmt->fmt.pix.height, &zoran_formats[i]);
2367         if (res)
2368                 goto sfmtv4l_unlock_and_return;
2369
2370         /* tell the user the
2371          * results/missing stuff */
2372         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2373         fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2374         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2375         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2376                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2377         else
2378                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2379
2380         fh->map_mode = ZORAN_MAP_MODE_RAW;
2381 sfmtv4l_unlock_and_return:
2382         mutex_unlock(&zr->resource_lock);
2383         return res;
2384 }
2385
2386 static int zoran_g_fbuf(struct file *file, void *__fh,
2387                 struct v4l2_framebuffer *fb)
2388 {
2389         struct zoran_fh *fh = __fh;
2390         struct zoran *zr = fh->zr;
2391
2392         memset(fb, 0, sizeof(*fb));
2393         mutex_lock(&zr->resource_lock);
2394         fb->base = zr->buffer.base;
2395         fb->fmt.width = zr->buffer.width;
2396         fb->fmt.height = zr->buffer.height;
2397         if (zr->overlay_settings.format)
2398                 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2399         fb->fmt.bytesperline = zr->buffer.bytesperline;
2400         mutex_unlock(&zr->resource_lock);
2401         fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2402         fb->fmt.field = V4L2_FIELD_INTERLACED;
2403         fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2404         fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2405
2406         return 0;
2407 }
2408
2409 static int zoran_s_fbuf(struct file *file, void *__fh,
2410                 struct v4l2_framebuffer *fb)
2411 {
2412         struct zoran_fh *fh = __fh;
2413         struct zoran *zr = fh->zr;
2414         int i, res = 0;
2415         __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2416
2417         for (i = 0; i < NUM_FORMATS; i++)
2418                 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2419                         break;
2420         if (i == NUM_FORMATS) {
2421                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2422                         ZR_DEVNAME(zr), fb->fmt.pixelformat,
2423                         (char *)&printformat);
2424                 return -EINVAL;
2425         }
2426
2427         mutex_lock(&zr->resource_lock);
2428         res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2429                                 fb->fmt.width, fb->fmt.height,
2430                                 fb->fmt.bytesperline);
2431         mutex_unlock(&zr->resource_lock);
2432
2433         return res;
2434 }
2435
2436 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2437 {
2438         struct zoran_fh *fh = __fh;
2439         struct zoran *zr = fh->zr;
2440         int res;
2441
2442         mutex_lock(&zr->resource_lock);
2443         res = setup_overlay(file, on);
2444         mutex_unlock(&zr->resource_lock);
2445
2446         return res;
2447 }
2448
2449 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2450
2451 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2452 {
2453         struct zoran_fh *fh = __fh;
2454         struct zoran *zr = fh->zr;
2455         int res = 0;
2456
2457         if (req->memory != V4L2_MEMORY_MMAP) {
2458                 dprintk(2,
2459                                 KERN_ERR
2460                                 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2461                                 ZR_DEVNAME(zr), req->memory);
2462                 return -EINVAL;
2463         }
2464
2465         if (req->count == 0)
2466                 return zoran_streamoff(file, fh, req->type);
2467
2468         mutex_lock(&zr->resource_lock);
2469         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2470                 dprintk(2,
2471                                 KERN_ERR
2472                                 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2473                                 ZR_DEVNAME(zr));
2474                 res = -EBUSY;
2475                 goto v4l2reqbuf_unlock_and_return;
2476         }
2477
2478         if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2479                         req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2480
2481                 /* control user input */
2482                 if (req->count < 2)
2483                         req->count = 2;
2484                 if (req->count > v4l_nbufs)
2485                         req->count = v4l_nbufs;
2486                 fh->v4l_buffers.num_buffers = req->count;
2487
2488                 if (v4l_fbuffer_alloc(file)) {
2489                         res = -ENOMEM;
2490                         goto v4l2reqbuf_unlock_and_return;
2491                 }
2492
2493                 /* The next mmap will map the V4L buffers */
2494                 fh->map_mode = ZORAN_MAP_MODE_RAW;
2495
2496         } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2497                         fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2498
2499                 /* we need to calculate size ourselves now */
2500                 if (req->count < 4)
2501                         req->count = 4;
2502                 if (req->count > jpg_nbufs)
2503                         req->count = jpg_nbufs;
2504                 fh->jpg_buffers.num_buffers = req->count;
2505                 fh->jpg_buffers.buffer_size =
2506                         zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2507
2508                 if (jpg_fbuffer_alloc(file)) {
2509                         res = -ENOMEM;
2510                         goto v4l2reqbuf_unlock_and_return;
2511                 }
2512
2513                 /* The next mmap will map the MJPEG buffers */
2514                 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2515                         fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2516                 else
2517                         fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2518
2519         } else {
2520                 dprintk(1,
2521                                 KERN_ERR
2522                                 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2523                                 ZR_DEVNAME(zr), req->type);
2524                 res = -EINVAL;
2525                 goto v4l2reqbuf_unlock_and_return;
2526         }
2527 v4l2reqbuf_unlock_and_return:
2528         mutex_unlock(&zr->resource_lock);
2529
2530         return res;
2531 }
2532
2533 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2534 {
2535         struct zoran_fh *fh = __fh;
2536         struct zoran *zr = fh->zr;
2537         __u32 type = buf->type;
2538         int index = buf->index, res;
2539
2540         memset(buf, 0, sizeof(*buf));
2541         buf->type = type;
2542         buf->index = index;
2543
2544         mutex_lock(&zr->resource_lock);
2545         res = zoran_v4l2_buffer_status(file, buf, buf->index);
2546         mutex_unlock(&zr->resource_lock);
2547
2548         return res;
2549 }
2550
2551 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2552 {
2553         struct zoran_fh *fh = __fh;
2554         struct zoran *zr = fh->zr;
2555         int res = 0, codec_mode, buf_type;
2556
2557         mutex_lock(&zr->resource_lock);
2558
2559         switch (fh->map_mode) {
2560         case ZORAN_MAP_MODE_RAW:
2561                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2562                         dprintk(1, KERN_ERR
2563                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2564                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2565                         res = -EINVAL;
2566                         goto qbuf_unlock_and_return;
2567                 }
2568
2569                 res = zoran_v4l_queue_frame(file, buf->index);
2570                 if (res)
2571                         goto qbuf_unlock_and_return;
2572                 if (!zr->v4l_memgrab_active &&
2573                                 fh->v4l_buffers.active == ZORAN_LOCKED)
2574                         zr36057_set_memgrab(zr, 1);
2575                 break;
2576
2577         case ZORAN_MAP_MODE_JPG_REC:
2578         case ZORAN_MAP_MODE_JPG_PLAY:
2579                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2580                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2581                         codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2582                 } else {
2583                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2584                         codec_mode = BUZ_MODE_MOTION_COMPRESS;
2585                 }
2586
2587                 if (buf->type != buf_type) {
2588                         dprintk(1, KERN_ERR
2589                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2590                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2591                         res = -EINVAL;
2592                         goto qbuf_unlock_and_return;
2593                 }
2594
2595                 res = zoran_jpg_queue_frame(file, buf->index,
2596                                         codec_mode);
2597                 if (res != 0)
2598                         goto qbuf_unlock_and_return;
2599                 if (zr->codec_mode == BUZ_MODE_IDLE &&
2600                                 fh->jpg_buffers.active == ZORAN_LOCKED) {
2601                         zr36057_enable_jpg(zr, codec_mode);
2602                 }
2603                 break;
2604
2605         default:
2606                 dprintk(1, KERN_ERR
2607                         "%s: VIDIOC_QBUF - unsupported type %d\n",
2608                         ZR_DEVNAME(zr), buf->type);
2609                 res = -EINVAL;
2610                 break;
2611         }
2612 qbuf_unlock_and_return:
2613         mutex_unlock(&zr->resource_lock);
2614
2615         return res;
2616 }
2617
2618 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2619 {
2620         struct zoran_fh *fh = __fh;
2621         struct zoran *zr = fh->zr;
2622         int res = 0, buf_type, num = -1;        /* compiler borks here (?) */
2623
2624         mutex_lock(&zr->resource_lock);
2625
2626         switch (fh->map_mode) {
2627         case ZORAN_MAP_MODE_RAW:
2628                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2629                         dprintk(1, KERN_ERR
2630                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2631                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2632                         res = -EINVAL;
2633                         goto dqbuf_unlock_and_return;
2634                 }
2635
2636                 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2637                 if (file->f_flags & O_NONBLOCK &&
2638                     zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2639                         res = -EAGAIN;
2640                         goto dqbuf_unlock_and_return;
2641                 }
2642                 res = v4l_sync(file, num);
2643                 if (res)
2644                         goto dqbuf_unlock_and_return;
2645                 zr->v4l_sync_tail++;
2646                 res = zoran_v4l2_buffer_status(file, buf, num);
2647                 break;
2648
2649         case ZORAN_MAP_MODE_JPG_REC:
2650         case ZORAN_MAP_MODE_JPG_PLAY:
2651         {
2652                 struct zoran_sync bs;
2653
2654                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2655                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2656                 else
2657                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2658
2659                 if (buf->type != buf_type) {
2660                         dprintk(1, KERN_ERR
2661                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2662                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2663                         res = -EINVAL;
2664                         goto dqbuf_unlock_and_return;
2665                 }
2666
2667                 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2668
2669                 if (file->f_flags & O_NONBLOCK &&
2670                     zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2671                         res = -EAGAIN;
2672                         goto dqbuf_unlock_and_return;
2673                 }
2674                 res = jpg_sync(file, &bs);
2675                 if (res)
2676                         goto dqbuf_unlock_and_return;
2677                 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2678                 break;
2679         }
2680
2681         default:
2682                 dprintk(1, KERN_ERR
2683                         "%s: VIDIOC_DQBUF - unsupported type %d\n",
2684                         ZR_DEVNAME(zr), buf->type);
2685                 res = -EINVAL;
2686                 break;
2687         }
2688 dqbuf_unlock_and_return:
2689         mutex_unlock(&zr->resource_lock);
2690
2691         return res;
2692 }
2693
2694 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2695 {
2696         struct zoran_fh *fh = __fh;
2697         struct zoran *zr = fh->zr;
2698         int res = 0;
2699
2700         mutex_lock(&zr->resource_lock);
2701
2702         switch (fh->map_mode) {
2703         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2704                 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2705                     fh->v4l_buffers.active != ZORAN_ACTIVE) {
2706                         res = -EBUSY;
2707                         goto strmon_unlock_and_return;
2708                 }
2709
2710                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2711                 zr->v4l_settings = fh->v4l_settings;
2712
2713                 zr->v4l_sync_tail = zr->v4l_pend_tail;
2714                 if (!zr->v4l_memgrab_active &&
2715                     zr->v4l_pend_head != zr->v4l_pend_tail) {
2716                         zr36057_set_memgrab(zr, 1);
2717                 }
2718                 break;
2719
2720         case ZORAN_MAP_MODE_JPG_REC:
2721         case ZORAN_MAP_MODE_JPG_PLAY:
2722                 /* what is the codec mode right now? */
2723                 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2724                     fh->jpg_buffers.active != ZORAN_ACTIVE) {
2725                         res = -EBUSY;
2726                         goto strmon_unlock_and_return;
2727                 }
2728
2729                 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2730
2731                 if (zr->jpg_que_head != zr->jpg_que_tail) {
2732                         /* Start the jpeg codec when the first frame is queued  */
2733                         jpeg_start(zr);
2734                 }
2735                 break;
2736
2737         default:
2738                 dprintk(1,
2739                         KERN_ERR
2740                         "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2741                         ZR_DEVNAME(zr), fh->map_mode);
2742                 res = -EINVAL;
2743                 break;
2744         }
2745 strmon_unlock_and_return:
2746         mutex_unlock(&zr->resource_lock);
2747
2748         return res;
2749 }
2750
2751 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2752 {
2753         struct zoran_fh *fh = __fh;
2754         struct zoran *zr = fh->zr;
2755         int i, res = 0;
2756
2757         mutex_lock(&zr->resource_lock);
2758
2759         switch (fh->map_mode) {
2760         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2761                 if (fh->v4l_buffers.active == ZORAN_FREE &&
2762                     zr->v4l_buffers.active != ZORAN_FREE) {
2763                         res = -EPERM;   /* stay off other's settings! */
2764                         goto strmoff_unlock_and_return;
2765                 }
2766                 if (zr->v4l_buffers.active == ZORAN_FREE)
2767                         goto strmoff_unlock_and_return;
2768
2769                 /* unload capture */
2770                 if (zr->v4l_memgrab_active) {
2771                         unsigned long flags;
2772
2773                         spin_lock_irqsave(&zr->spinlock, flags);
2774                         zr36057_set_memgrab(zr, 0);
2775                         spin_unlock_irqrestore(&zr->spinlock, flags);
2776                 }
2777
2778                 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2779                         zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2780                 fh->v4l_buffers = zr->v4l_buffers;
2781
2782                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2783
2784                 zr->v4l_grab_seq = 0;
2785                 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2786                 zr->v4l_sync_tail = 0;
2787
2788                 break;
2789
2790         case ZORAN_MAP_MODE_JPG_REC:
2791         case ZORAN_MAP_MODE_JPG_PLAY:
2792                 if (fh->jpg_buffers.active == ZORAN_FREE &&
2793                     zr->jpg_buffers.active != ZORAN_FREE) {
2794                         res = -EPERM;   /* stay off other's settings! */
2795                         goto strmoff_unlock_and_return;
2796                 }
2797                 if (zr->jpg_buffers.active == ZORAN_FREE)
2798                         goto strmoff_unlock_and_return;
2799
2800                 res = jpg_qbuf(file, -1,
2801                              (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2802                              BUZ_MODE_MOTION_COMPRESS :
2803                              BUZ_MODE_MOTION_DECOMPRESS);
2804                 if (res)
2805                         goto strmoff_unlock_and_return;
2806                 break;
2807         default:
2808                 dprintk(1, KERN_ERR
2809                         "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2810                         ZR_DEVNAME(zr), fh->map_mode);
2811                 res = -EINVAL;
2812                 break;
2813         }
2814 strmoff_unlock_and_return:
2815         mutex_unlock(&zr->resource_lock);
2816
2817         return res;
2818 }
2819
2820 static int zoran_queryctrl(struct file *file, void *__fh,
2821                                         struct v4l2_queryctrl *ctrl)
2822 {
2823         struct zoran_fh *fh = __fh;
2824         struct zoran *zr = fh->zr;
2825
2826         /* we only support hue/saturation/contrast/brightness */
2827         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2828             ctrl->id > V4L2_CID_HUE)
2829                 return -EINVAL;
2830
2831         decoder_command(zr, VIDIOC_QUERYCTRL, ctrl);
2832
2833         return 0;
2834 }
2835
2836 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2837 {
2838         struct zoran_fh *fh = __fh;
2839         struct zoran *zr = fh->zr;
2840
2841         /* we only support hue/saturation/contrast/brightness */
2842         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2843             ctrl->id > V4L2_CID_HUE)
2844                 return -EINVAL;
2845
2846         mutex_lock(&zr->resource_lock);
2847         decoder_command(zr, VIDIOC_G_CTRL, ctrl);
2848         mutex_unlock(&zr->resource_lock);
2849
2850         return 0;
2851 }
2852
2853 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2854 {
2855         struct zoran_fh *fh = __fh;
2856         struct zoran *zr = fh->zr;
2857
2858         /* we only support hue/saturation/contrast/brightness */
2859         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2860             ctrl->id > V4L2_CID_HUE)
2861                 return -EINVAL;
2862
2863         mutex_lock(&zr->resource_lock);
2864         decoder_command(zr, VIDIOC_S_CTRL, ctrl);
2865         mutex_unlock(&zr->resource_lock);
2866
2867         return 0;
2868 }
2869
2870 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2871 {
2872         struct zoran_fh *fh = __fh;
2873         struct zoran *zr = fh->zr;
2874
2875         mutex_lock(&zr->resource_lock);
2876         *std = zr->norm;
2877         mutex_unlock(&zr->resource_lock);
2878         return 0;
2879 }
2880
2881 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2882 {
2883         struct zoran_fh *fh = __fh;
2884         struct zoran *zr = fh->zr;
2885         int res = 0;
2886
2887         mutex_lock(&zr->resource_lock);
2888         res = zoran_set_norm(zr, *std);
2889         if (res)
2890                 goto sstd_unlock_and_return;
2891
2892         res = wait_grab_pending(zr);
2893 sstd_unlock_and_return:
2894         mutex_unlock(&zr->resource_lock);
2895         return res;
2896 }
2897
2898 static int zoran_enum_input(struct file *file, void *__fh,
2899                                  struct v4l2_input *inp)
2900 {
2901         struct zoran_fh *fh = __fh;
2902         struct zoran *zr = fh->zr;
2903
2904         if (inp->index < 0 || inp->index >= zr->card.inputs)
2905                 return -EINVAL;
2906         else {
2907                 int id = inp->index;
2908                 memset(inp, 0, sizeof(*inp));
2909                 inp->index = id;
2910         }
2911
2912         strncpy(inp->name, zr->card.input[inp->index].name,
2913                 sizeof(inp->name) - 1);
2914         inp->type = V4L2_INPUT_TYPE_CAMERA;
2915         inp->std = V4L2_STD_ALL;
2916
2917         /* Get status of video decoder */
2918         mutex_lock(&zr->resource_lock);
2919         decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &inp->status);
2920         mutex_unlock(&zr->resource_lock);
2921         return 0;
2922 }
2923
2924 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2925 {
2926         struct zoran_fh *fh = __fh;
2927         struct zoran *zr = fh->zr;
2928
2929         mutex_lock(&zr->resource_lock);
2930         *input = zr->input;
2931         mutex_unlock(&zr->resource_lock);
2932
2933         return 0;
2934 }
2935
2936 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2937 {
2938         struct zoran_fh *fh = __fh;
2939         struct zoran *zr = fh->zr;
2940         int res;
2941
2942         mutex_lock(&zr->resource_lock);
2943         res = zoran_set_input(zr, input);
2944         if (res)
2945                 goto sinput_unlock_and_return;
2946
2947         /* Make sure the changes come into effect */
2948         res = wait_grab_pending(zr);
2949 sinput_unlock_and_return:
2950         mutex_unlock(&zr->resource_lock);
2951         return res;
2952 }
2953
2954 static int zoran_enum_output(struct file *file, void *__fh,
2955                                   struct v4l2_output *outp)
2956 {
2957         if (outp->index != 0)
2958                 return -EINVAL;
2959
2960         memset(outp, 0, sizeof(*outp));
2961         outp->index = 0;
2962         outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2963         strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2964
2965         return 0;
2966 }
2967
2968 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2969 {
2970         *output = 0;
2971
2972         return 0;
2973 }
2974
2975 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2976 {
2977         if (output != 0)
2978                 return -EINVAL;
2979
2980         return 0;
2981 }
2982
2983 /* cropping (sub-frame capture) */
2984 static int zoran_cropcap(struct file *file, void *__fh,
2985                                         struct v4l2_cropcap *cropcap)
2986 {
2987         struct zoran_fh *fh = __fh;
2988         struct zoran *zr = fh->zr;
2989         int type = cropcap->type, res = 0;
2990
2991         memset(cropcap, 0, sizeof(*cropcap));
2992         cropcap->type = type;
2993
2994         mutex_lock(&zr->resource_lock);
2995
2996         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2997             (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2998              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2999                 dprintk(1, KERN_ERR
3000                         "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3001                         ZR_DEVNAME(zr));
3002                 res = -EINVAL;
3003                 goto cropcap_unlock_and_return;
3004         }
3005
3006         cropcap->bounds.top = cropcap->bounds.left = 0;
3007         cropcap->bounds.width = BUZ_MAX_WIDTH;
3008         cropcap->bounds.height = BUZ_MAX_HEIGHT;
3009         cropcap->defrect.top = cropcap->defrect.left = 0;
3010         cropcap->defrect.width = BUZ_MIN_WIDTH;
3011         cropcap->defrect.height = BUZ_MIN_HEIGHT;
3012 cropcap_unlock_and_return:
3013         mutex_unlock(&zr->resource_lock);
3014         return res;
3015 }
3016
3017 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3018 {
3019         struct zoran_fh *fh = __fh;
3020         struct zoran *zr = fh->zr;
3021         int type = crop->type, res = 0;
3022
3023         memset(crop, 0, sizeof(*crop));
3024         crop->type = type;
3025
3026         mutex_lock(&zr->resource_lock);
3027
3028         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3029             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3030              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3031                 dprintk(1,
3032                         KERN_ERR
3033                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3034                         ZR_DEVNAME(zr));
3035                 res = -EINVAL;
3036                 goto gcrop_unlock_and_return;
3037         }
3038
3039         crop->c.top = fh->jpg_settings.img_y;
3040         crop->c.left = fh->jpg_settings.img_x;
3041         crop->c.width = fh->jpg_settings.img_width;
3042         crop->c.height = fh->jpg_settings.img_height;
3043
3044 gcrop_unlock_and_return:
3045         mutex_unlock(&zr->resource_lock);
3046
3047         return res;
3048 }
3049
3050 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3051 {
3052         struct zoran_fh *fh = __fh;
3053         struct zoran *zr = fh->zr;
3054         int res = 0;
3055         struct zoran_jpg_settings settings;
3056
3057         settings = fh->jpg_settings;
3058
3059         mutex_lock(&zr->resource_lock);
3060
3061         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3062                 dprintk(1, KERN_ERR
3063                         "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3064                         ZR_DEVNAME(zr));
3065                 res = -EBUSY;
3066                 goto scrop_unlock_and_return;
3067         }
3068
3069         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3070             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3071              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3072                 dprintk(1, KERN_ERR
3073                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3074                         ZR_DEVNAME(zr));
3075                 res = -EINVAL;
3076                 goto scrop_unlock_and_return;
3077         }
3078
3079         /* move into a form that we understand */
3080         settings.img_x = crop->c.left;
3081         settings.img_y = crop->c.top;
3082         settings.img_width = crop->c.width;
3083         settings.img_height = crop->c.height;
3084
3085         /* check validity */
3086         res = zoran_check_jpg_settings(zr, &settings, 0);
3087         if (res)
3088                 goto scrop_unlock_and_return;
3089
3090         /* accept */
3091         fh->jpg_settings = settings;
3092
3093 scrop_unlock_and_return:
3094         mutex_unlock(&zr->resource_lock);
3095         return res;
3096 }
3097
3098 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3099                                         struct v4l2_jpegcompression *params)
3100 {
3101         struct zoran_fh *fh = __fh;
3102         struct zoran *zr = fh->zr;
3103         memset(params, 0, sizeof(*params));
3104
3105         mutex_lock(&zr->resource_lock);
3106
3107         params->quality = fh->jpg_settings.jpg_comp.quality;
3108         params->APPn = fh->jpg_settings.jpg_comp.APPn;
3109         memcpy(params->APP_data,
3110                fh->jpg_settings.jpg_comp.APP_data,
3111                fh->jpg_settings.jpg_comp.APP_len);
3112         params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3113         memcpy(params->COM_data,
3114                fh->jpg_settings.jpg_comp.COM_data,
3115                fh->jpg_settings.jpg_comp.COM_len);
3116         params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3117         params->jpeg_markers =
3118             fh->jpg_settings.jpg_comp.jpeg_markers;
3119
3120         mutex_unlock(&zr->resource_lock);
3121
3122         return 0;
3123 }
3124
3125 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3126                                         struct v4l2_jpegcompression *params)
3127 {
3128         struct zoran_fh *fh = __fh;
3129         struct zoran *zr = fh->zr;
3130         int res = 0;
3131         struct zoran_jpg_settings settings;
3132
3133         settings = fh->jpg_settings;
3134
3135         settings.jpg_comp = *params;
3136
3137         mutex_lock(&zr->resource_lock);
3138
3139         if (fh->v4l_buffers.active != ZORAN_FREE ||
3140             fh->jpg_buffers.active != ZORAN_FREE) {
3141                 dprintk(1, KERN_WARNING
3142                         "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3143                         ZR_DEVNAME(zr));
3144                 res = -EBUSY;
3145                 goto sjpegc_unlock_and_return;
3146         }
3147
3148         res = zoran_check_jpg_settings(zr, &settings, 0);
3149         if (res)
3150                 goto sjpegc_unlock_and_return;
3151         if (!fh->jpg_buffers.allocated)
3152                 fh->jpg_buffers.buffer_size =
3153                     zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3154         fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3155 sjpegc_unlock_and_return:
3156         mutex_unlock(&zr->resource_lock);
3157
3158         return 0;
3159 }
3160
3161 static unsigned int
3162 zoran_poll (struct file *file,
3163             poll_table  *wait)
3164 {
3165         struct zoran_fh *fh = file->private_data;
3166         struct zoran *zr = fh->zr;
3167         int res = 0, frame;
3168         unsigned long flags;
3169
3170         /* we should check whether buffers are ready to be synced on
3171          * (w/o waits - O_NONBLOCK) here
3172          * if ready for read (sync), return POLLIN|POLLRDNORM,
3173          * if ready for write (sync), return POLLOUT|POLLWRNORM,
3174          * if error, return POLLERR,
3175          * if no buffers queued or so, return POLLNVAL
3176          */
3177
3178         mutex_lock(&zr->resource_lock);
3179
3180         switch (fh->map_mode) {
3181         case ZORAN_MAP_MODE_RAW:
3182                 poll_wait(file, &zr->v4l_capq, wait);
3183                 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3184
3185                 spin_lock_irqsave(&zr->spinlock, flags);
3186                 dprintk(3,
3187                         KERN_DEBUG
3188                         "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3189                         ZR_DEVNAME(zr), __func__,
3190                         "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3191                         "UPMD"[zr->v4l_buffers.buffer[frame].state],
3192                         zr->v4l_pend_tail, zr->v4l_pend_head);
3193                 /* Process is the one capturing? */
3194                 if (fh->v4l_buffers.active != ZORAN_FREE &&
3195                     /* Buffer ready to DQBUF? */
3196                     zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3197                         res = POLLIN | POLLRDNORM;
3198                 spin_unlock_irqrestore(&zr->spinlock, flags);
3199
3200                 break;
3201
3202         case ZORAN_MAP_MODE_JPG_REC:
3203         case ZORAN_MAP_MODE_JPG_PLAY:
3204                 poll_wait(file, &zr->jpg_capq, wait);
3205                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3206
3207                 spin_lock_irqsave(&zr->spinlock, flags);
3208                 dprintk(3,
3209                         KERN_DEBUG
3210                         "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3211                         ZR_DEVNAME(zr), __func__,
3212                         "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3213                         "UPMD"[zr->jpg_buffers.buffer[frame].state],
3214                         zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3215                 if (fh->jpg_buffers.active != ZORAN_FREE &&
3216                     zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3217                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3218                                 res = POLLIN | POLLRDNORM;
3219                         else
3220                                 res = POLLOUT | POLLWRNORM;
3221                 }
3222                 spin_unlock_irqrestore(&zr->spinlock, flags);
3223
3224                 break;
3225
3226         default:
3227                 dprintk(1,
3228                         KERN_ERR
3229                         "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3230                         ZR_DEVNAME(zr), fh->map_mode);
3231                 res = POLLNVAL;
3232         }
3233
3234         mutex_unlock(&zr->resource_lock);
3235
3236         return res;
3237 }
3238
3239
3240 /*
3241  * This maps the buffers to user space.
3242  *
3243  * Depending on the state of fh->map_mode
3244  * the V4L or the MJPEG buffers are mapped
3245  * per buffer or all together
3246  *
3247  * Note that we need to connect to some
3248  * unmap signal event to unmap the de-allocate
3249  * the buffer accordingly (zoran_vm_close())
3250  */
3251
3252 static void
3253 zoran_vm_open (struct vm_area_struct *vma)
3254 {
3255         struct zoran_mapping *map = vma->vm_private_data;
3256
3257         map->count++;
3258 }
3259
3260 static void
3261 zoran_vm_close (struct vm_area_struct *vma)
3262 {
3263         struct zoran_mapping *map = vma->vm_private_data;
3264         struct file *file = map->file;
3265         struct zoran_fh *fh = file->private_data;
3266         struct zoran *zr = fh->zr;
3267         int i;
3268
3269         map->count--;
3270         if (map->count == 0) {
3271                 switch (fh->map_mode) {
3272                 case ZORAN_MAP_MODE_JPG_REC:
3273                 case ZORAN_MAP_MODE_JPG_PLAY:
3274
3275                         dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3276                                 ZR_DEVNAME(zr));
3277
3278                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3279                                 if (fh->jpg_buffers.buffer[i].map == map) {
3280                                         fh->jpg_buffers.buffer[i].map =
3281                                             NULL;
3282                                 }
3283                         }
3284                         kfree(map);
3285
3286                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3287                                 if (fh->jpg_buffers.buffer[i].map)
3288                                         break;
3289                         if (i == fh->jpg_buffers.num_buffers) {
3290                                 mutex_lock(&zr->resource_lock);
3291
3292                                 if (fh->jpg_buffers.active != ZORAN_FREE) {
3293                                         jpg_qbuf(file, -1, zr->codec_mode);
3294                                         zr->jpg_buffers.allocated = 0;
3295                                         zr->jpg_buffers.active =
3296                                             fh->jpg_buffers.active =
3297                                             ZORAN_FREE;
3298                                 }
3299                                 jpg_fbuffer_free(file);
3300                                 mutex_unlock(&zr->resource_lock);
3301                         }
3302
3303                         break;
3304
3305                 case ZORAN_MAP_MODE_RAW:
3306
3307                         dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3308                                 ZR_DEVNAME(zr));
3309
3310                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3311                                 if (fh->v4l_buffers.buffer[i].map == map) {
3312                                         /* unqueue/unmap */
3313                                         fh->v4l_buffers.buffer[i].map =
3314                                             NULL;
3315                                 }
3316                         }
3317                         kfree(map);
3318
3319                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3320                                 if (fh->v4l_buffers.buffer[i].map)
3321                                         break;
3322                         if (i == fh->v4l_buffers.num_buffers) {
3323                                 mutex_lock(&zr->resource_lock);
3324
3325                                 if (fh->v4l_buffers.active != ZORAN_FREE) {
3326                                         unsigned long flags;
3327
3328                                         spin_lock_irqsave(&zr->spinlock, flags);
3329                                         zr36057_set_memgrab(zr, 0);
3330                                         zr->v4l_buffers.allocated = 0;
3331                                         zr->v4l_buffers.active =
3332                                             fh->v4l_buffers.active =
3333                                             ZORAN_FREE;
3334                                         spin_unlock_irqrestore(&zr->spinlock, flags);
3335                                 }
3336                                 v4l_fbuffer_free(file);
3337                                 mutex_unlock(&zr->resource_lock);
3338                         }
3339
3340                         break;
3341
3342                 default:
3343                         printk(KERN_ERR
3344                                "%s: munmap() - internal error - unknown map mode %d\n",
3345                                ZR_DEVNAME(zr), fh->map_mode);
3346                         break;
3347
3348                 }
3349         }
3350 }
3351
3352 static struct vm_operations_struct zoran_vm_ops = {
3353         .open = zoran_vm_open,
3354         .close = zoran_vm_close,
3355 };
3356
3357 static int
3358 zoran_mmap (struct file           *file,
3359             struct vm_area_struct *vma)
3360 {
3361         struct zoran_fh *fh = file->private_data;
3362         struct zoran *zr = fh->zr;
3363         unsigned long size = (vma->vm_end - vma->vm_start);
3364         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3365         int i, j;
3366         unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3367         int first, last;
3368         struct zoran_mapping *map;
3369         int res = 0;
3370
3371         dprintk(3,
3372                 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3373                 ZR_DEVNAME(zr),
3374                 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3375                 vma->vm_start, vma->vm_end, size);
3376
3377         if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3378             !(vma->vm_flags & VM_WRITE)) {
3379                 dprintk(1,
3380                         KERN_ERR
3381                         "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3382                         ZR_DEVNAME(zr));
3383                 return -EINVAL;
3384         }
3385
3386         switch (fh->map_mode) {
3387
3388         case ZORAN_MAP_MODE_JPG_REC:
3389         case ZORAN_MAP_MODE_JPG_PLAY:
3390
3391                 /* lock */
3392                 mutex_lock(&zr->resource_lock);
3393
3394                 /* Map the MJPEG buffers */
3395                 if (!fh->jpg_buffers.allocated) {
3396                         dprintk(1,
3397                                 KERN_ERR
3398                                 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3399                                 ZR_DEVNAME(zr));
3400                         res = -ENOMEM;
3401                         goto jpg_mmap_unlock_and_return;
3402                 }
3403
3404                 first = offset / fh->jpg_buffers.buffer_size;
3405                 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3406                 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3407                     size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3408                     last < 0 || first >= fh->jpg_buffers.num_buffers ||
3409                     last >= fh->jpg_buffers.num_buffers) {
3410                         dprintk(1,
3411                                 KERN_ERR
3412                                 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3413                                 ZR_DEVNAME(zr), offset, size,
3414                                 fh->jpg_buffers.buffer_size,
3415                                 fh->jpg_buffers.num_buffers);
3416                         res = -EINVAL;
3417                         goto jpg_mmap_unlock_and_return;
3418                 }
3419                 for (i = first; i <= last; i++) {
3420                         if (fh->jpg_buffers.buffer[i].map) {
3421                                 dprintk(1,
3422                                         KERN_ERR
3423                                         "%s: mmap(MJPEG) - buffer %d already mapped\n",
3424                                         ZR_DEVNAME(zr), i);
3425                                 res = -EBUSY;
3426                                 goto jpg_mmap_unlock_and_return;
3427                         }
3428                 }
3429
3430                 /* map these buffers (v4l_buffers[i]) */
3431                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3432                 if (!map) {
3433                         res = -ENOMEM;
3434                         goto jpg_mmap_unlock_and_return;
3435                 }
3436                 map->file = file;
3437                 map->count = 1;
3438
3439                 vma->vm_ops = &zoran_vm_ops;
3440                 vma->vm_flags |= VM_DONTEXPAND;
3441                 vma->vm_private_data = map;
3442
3443                 for (i = first; i <= last; i++) {
3444                         for (j = 0;
3445                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3446                              j++) {
3447                                 fraglen =
3448                                     (le32_to_cpu(fh->jpg_buffers.buffer[i].
3449                                      frag_tab[2 * j + 1]) & ~1) << 1;
3450                                 todo = size;
3451                                 if (todo > fraglen)
3452                                         todo = fraglen;
3453                                 pos =
3454                                     le32_to_cpu(fh->jpg_buffers.
3455                                     buffer[i].frag_tab[2 * j]);
3456                                 /* should just be pos on i386 */
3457                                 page = virt_to_phys(bus_to_virt(pos))
3458                                                                 >> PAGE_SHIFT;
3459                                 if (remap_pfn_range(vma, start, page,
3460                                                         todo, PAGE_SHARED)) {
3461                                         dprintk(1,
3462                                                 KERN_ERR
3463                                                 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3464                                                 ZR_DEVNAME(zr));
3465                                         res = -EAGAIN;
3466                                         goto jpg_mmap_unlock_and_return;
3467                                 }
3468                                 size -= todo;
3469                                 start += todo;
3470                                 if (size == 0)
3471                                         break;
3472                                 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3473                                     frag_tab[2 * j + 1]) & 1)
3474                                         break;  /* was last fragment */
3475                         }
3476                         fh->jpg_buffers.buffer[i].map = map;
3477                         if (size == 0)
3478                                 break;
3479
3480                 }
3481         jpg_mmap_unlock_and_return:
3482                 mutex_unlock(&zr->resource_lock);
3483
3484                 break;
3485
3486         case ZORAN_MAP_MODE_RAW:
3487
3488                 mutex_lock(&zr->resource_lock);
3489
3490                 /* Map the V4L buffers */
3491                 if (!fh->v4l_buffers.allocated) {
3492                         dprintk(1,
3493                                 KERN_ERR
3494                                 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3495                                 ZR_DEVNAME(zr));
3496                         res = -ENOMEM;
3497                         goto v4l_mmap_unlock_and_return;
3498                 }
3499
3500                 first = offset / fh->v4l_buffers.buffer_size;
3501                 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3502                 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3503                     size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3504                     last < 0 || first >= fh->v4l_buffers.num_buffers ||
3505                     last >= fh->v4l_buffers.buffer_size) {
3506                         dprintk(1,
3507                                 KERN_ERR
3508                                 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3509                                 ZR_DEVNAME(zr), offset, size,
3510                                 fh->v4l_buffers.buffer_size,
3511                                 fh->v4l_buffers.num_buffers);
3512                         res = -EINVAL;
3513                         goto v4l_mmap_unlock_and_return;
3514                 }
3515                 for (i = first; i <= last; i++) {
3516                         if (fh->v4l_buffers.buffer[i].map) {
3517                                 dprintk(1,
3518                                         KERN_ERR
3519                                         "%s: mmap(V4L) - buffer %d already mapped\n",
3520                                         ZR_DEVNAME(zr), i);
3521                                 res = -EBUSY;
3522                                 goto v4l_mmap_unlock_and_return;
3523                         }
3524                 }
3525
3526                 /* map these buffers (v4l_buffers[i]) */
3527                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3528                 if (!map) {
3529                         res = -ENOMEM;
3530                         goto v4l_mmap_unlock_and_return;
3531                 }
3532                 map->file = file;
3533                 map->count = 1;
3534
3535                 vma->vm_ops = &zoran_vm_ops;
3536                 vma->vm_flags |= VM_DONTEXPAND;
3537                 vma->vm_private_data = map;
3538
3539                 for (i = first; i <= last; i++) {
3540                         todo = size;
3541                         if (todo > fh->v4l_buffers.buffer_size)
3542                                 todo = fh->v4l_buffers.buffer_size;
3543                         page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3544                         if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3545                                                         todo, PAGE_SHARED)) {
3546                                 dprintk(1,
3547                                         KERN_ERR
3548                                         "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3549                                         ZR_DEVNAME(zr));
3550                                 res = -EAGAIN;
3551                                 goto v4l_mmap_unlock_and_return;
3552                         }
3553                         size -= todo;
3554                         start += todo;
3555                         fh->v4l_buffers.buffer[i].map = map;
3556                         if (size == 0)
3557                                 break;
3558                 }
3559         v4l_mmap_unlock_and_return:
3560                 mutex_unlock(&zr->resource_lock);
3561
3562                 break;
3563
3564         default:
3565                 dprintk(1,
3566                         KERN_ERR
3567                         "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3568                         ZR_DEVNAME(zr), fh->map_mode);
3569                 break;
3570         }
3571
3572         return 0;
3573 }
3574
3575 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3576         .vidioc_querycap                    = zoran_querycap,
3577         .vidioc_cropcap                     = zoran_cropcap,
3578         .vidioc_s_crop                      = zoran_s_crop,
3579         .vidioc_g_crop                      = zoran_g_crop,
3580         .vidioc_enum_input                  = zoran_enum_input,
3581         .vidioc_g_input                     = zoran_g_input,
3582         .vidioc_s_input                     = zoran_s_input,
3583         .vidioc_enum_output                 = zoran_enum_output,
3584         .vidioc_g_output                    = zoran_g_output,
3585         .vidioc_s_output                    = zoran_s_output,
3586         .vidioc_g_fbuf                      = zoran_g_fbuf,
3587         .vidioc_s_fbuf                      = zoran_s_fbuf,
3588         .vidioc_g_std                       = zoran_g_std,
3589         .vidioc_s_std                       = zoran_s_std,
3590         .vidioc_g_jpegcomp                  = zoran_g_jpegcomp,
3591         .vidioc_s_jpegcomp                  = zoran_s_jpegcomp,
3592         .vidioc_overlay                     = zoran_overlay,
3593         .vidioc_reqbufs                     = zoran_reqbufs,
3594         .vidioc_querybuf                    = zoran_querybuf,
3595         .vidioc_qbuf                        = zoran_qbuf,
3596         .vidioc_dqbuf                       = zoran_dqbuf,
3597         .vidioc_streamon                    = zoran_streamon,
3598         .vidioc_streamoff                   = zoran_streamoff,
3599         .vidioc_enum_fmt_vid_cap            = zoran_enum_fmt_vid_cap,
3600         .vidioc_enum_fmt_vid_out            = zoran_enum_fmt_vid_out,
3601         .vidioc_enum_fmt_vid_overlay        = zoran_enum_fmt_vid_overlay,
3602         .vidioc_g_fmt_vid_cap               = zoran_g_fmt_vid_cap,
3603         .vidioc_g_fmt_vid_out               = zoran_g_fmt_vid_out,
3604         .vidioc_g_fmt_vid_overlay           = zoran_g_fmt_vid_overlay,
3605         .vidioc_s_fmt_vid_cap               = zoran_s_fmt_vid_cap,
3606         .vidioc_s_fmt_vid_out               = zoran_s_fmt_vid_out,
3607         .vidioc_s_fmt_vid_overlay           = zoran_s_fmt_vid_overlay,
3608         .vidioc_try_fmt_vid_cap             = zoran_try_fmt_vid_cap,
3609         .vidioc_try_fmt_vid_out             = zoran_try_fmt_vid_out,
3610         .vidioc_try_fmt_vid_overlay         = zoran_try_fmt_vid_overlay,
3611         .vidioc_queryctrl                   = zoran_queryctrl,
3612         .vidioc_s_ctrl                      = zoran_s_ctrl,
3613         .vidioc_g_ctrl                      = zoran_g_ctrl,
3614 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3615         .vidioc_default                     = zoran_default,
3616         .vidiocgmbuf                        = zoran_vidiocgmbuf,
3617 #endif
3618 };
3619
3620 static const struct v4l2_file_operations zoran_fops = {
3621         .owner = THIS_MODULE,
3622         .open = zoran_open,
3623         .release = zoran_close,
3624         .ioctl = video_ioctl2,
3625         .read = zoran_read,
3626         .write = zoran_write,
3627         .mmap = zoran_mmap,
3628         .poll = zoran_poll,
3629 };
3630
3631 struct video_device zoran_template __devinitdata = {
3632         .name = ZORAN_NAME,
3633         .fops = &zoran_fops,
3634         .ioctl_ops = &zoran_ioctl_ops,
3635         .release = &zoran_vdev_release,
3636         .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3637         .minor = -1
3638 };
3639