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