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