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