V4L/DVB (10708): zoran: remove old V4L1 ioctls, use v4l1-compat instead.
[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 /*
617  * Sync on a V4L buffer
618  */
619
620 static int
621 v4l_sync (struct file *file,
622           int          frame)
623 {
624         struct zoran_fh *fh = file->private_data;
625         struct zoran *zr = fh->zr;
626         unsigned long flags;
627
628         if (fh->v4l_buffers.active == ZORAN_FREE) {
629                 dprintk(1,
630                         KERN_ERR
631                         "%s: v4l_sync() - no grab active for this session\n",
632                         ZR_DEVNAME(zr));
633                 return -EINVAL;
634         }
635
636         /* check passed-in frame number */
637         if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
638                 dprintk(1,
639                         KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
640                         ZR_DEVNAME(zr), frame);
641                 return -EINVAL;
642         }
643
644         /* Check if is buffer was queued at all */
645         if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
646                 dprintk(1,
647                         KERN_ERR
648                         "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
649                         ZR_DEVNAME(zr));
650                 return -EPROTO;
651         }
652
653         /* wait on this buffer to get ready */
654         if (!wait_event_interruptible_timeout(zr->v4l_capq,
655                                 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
656                                 10*HZ))
657                 return -ETIME;
658         if (signal_pending(current))
659                 return -ERESTARTSYS;
660
661         /* buffer should now be in BUZ_STATE_DONE */
662         if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
663                 dprintk(2,
664                         KERN_ERR "%s: v4l_sync() - internal state error\n",
665                         ZR_DEVNAME(zr));
666
667         zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
668         fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
669
670         spin_lock_irqsave(&zr->spinlock, flags);
671
672         /* Check if streaming capture has finished */
673         if (zr->v4l_pend_tail == zr->v4l_pend_head) {
674                 zr36057_set_memgrab(zr, 0);
675                 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
676                         fh->v4l_buffers.active = zr->v4l_buffers.active =
677                             ZORAN_FREE;
678                         zr->v4l_buffers.allocated = 0;
679                 }
680         }
681
682         spin_unlock_irqrestore(&zr->spinlock, flags);
683
684         return 0;
685 }
686
687 /*
688  *   Queue a MJPEG buffer for capture/playback
689  */
690
691 static int
692 zoran_jpg_queue_frame (struct file          *file,
693                        int                   num,
694                        enum zoran_codec_mode mode)
695 {
696         struct zoran_fh *fh = file->private_data;
697         struct zoran *zr = fh->zr;
698         unsigned long flags;
699         int res = 0;
700
701         /* Check if buffers are allocated */
702         if (!fh->jpg_buffers.allocated) {
703                 dprintk(1,
704                         KERN_ERR
705                         "%s: jpg_queue_frame() - buffers not yet allocated\n",
706                         ZR_DEVNAME(zr));
707                 return -ENOMEM;
708         }
709
710         /* No grabbing outside the buffer range! */
711         if (num >= fh->jpg_buffers.num_buffers || num < 0) {
712                 dprintk(1,
713                         KERN_ERR
714                         "%s: jpg_queue_frame() - buffer %d out of range\n",
715                         ZR_DEVNAME(zr), num);
716                 return -EINVAL;
717         }
718
719         /* what is the codec mode right now? */
720         if (zr->codec_mode == BUZ_MODE_IDLE) {
721                 zr->jpg_settings = fh->jpg_settings;
722         } else if (zr->codec_mode != mode) {
723                 /* wrong codec mode active - invalid */
724                 dprintk(1,
725                         KERN_ERR
726                         "%s: jpg_queue_frame() - codec in wrong mode\n",
727                         ZR_DEVNAME(zr));
728                 return -EINVAL;
729         }
730
731         if (fh->jpg_buffers.active == ZORAN_FREE) {
732                 if (zr->jpg_buffers.active == ZORAN_FREE) {
733                         zr->jpg_buffers = fh->jpg_buffers;
734                         fh->jpg_buffers.active = ZORAN_ACTIVE;
735                 } else {
736                         dprintk(1,
737                                 KERN_ERR
738                                 "%s: jpg_queue_frame() - another session is already capturing\n",
739                                 ZR_DEVNAME(zr));
740                         res = -EBUSY;
741                 }
742         }
743
744         if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
745                 /* Ok load up the jpeg codec */
746                 zr36057_enable_jpg(zr, mode);
747         }
748
749         spin_lock_irqsave(&zr->spinlock, flags);
750
751         if (!res) {
752                 switch (zr->jpg_buffers.buffer[num].state) {
753                 case BUZ_STATE_DONE:
754                         dprintk(2,
755                                 KERN_WARNING
756                                 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
757                                 ZR_DEVNAME(zr));
758                 case BUZ_STATE_USER:
759                         /* since there is at least one unused buffer there's room for at
760                          *least one more pend[] entry */
761                         zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
762                             num;
763                         zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
764                         fh->jpg_buffers.buffer[num] =
765                             zr->jpg_buffers.buffer[num];
766                         zoran_feed_stat_com(zr);
767                         break;
768                 default:
769                 case BUZ_STATE_DMA:
770                 case BUZ_STATE_PEND:
771                         if (zr->jpg_buffers.active == ZORAN_FREE) {
772                                 fh->jpg_buffers.active = ZORAN_FREE;
773                                 zr->jpg_buffers.allocated = 0;
774                         }
775                         res = -EBUSY;   /* what are you doing? */
776                         break;
777                 }
778         }
779
780         spin_unlock_irqrestore(&zr->spinlock, flags);
781
782         if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
783                 zr->jpg_buffers.active = fh->jpg_buffers.active;
784         }
785
786         return res;
787 }
788
789 static int
790 jpg_qbuf (struct file          *file,
791           int                   frame,
792           enum zoran_codec_mode mode)
793 {
794         struct zoran_fh *fh = file->private_data;
795         struct zoran *zr = fh->zr;
796         int res = 0;
797
798         /* Does the user want to stop streaming? */
799         if (frame < 0) {
800                 if (zr->codec_mode == mode) {
801                         if (fh->jpg_buffers.active == ZORAN_FREE) {
802                                 dprintk(1,
803                                         KERN_ERR
804                                         "%s: jpg_qbuf(-1) - session not active\n",
805                                         ZR_DEVNAME(zr));
806                                 return -EINVAL;
807                         }
808                         fh->jpg_buffers.active = zr->jpg_buffers.active =
809                             ZORAN_FREE;
810                         zr->jpg_buffers.allocated = 0;
811                         zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
812                         return 0;
813                 } else {
814                         dprintk(1,
815                                 KERN_ERR
816                                 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
817                                 ZR_DEVNAME(zr));
818                         return -EINVAL;
819                 }
820         }
821
822         if ((res = zoran_jpg_queue_frame(file, frame, mode)))
823                 return res;
824
825         /* Start the jpeg codec when the first frame is queued  */
826         if (!res && zr->jpg_que_head == 1)
827                 jpeg_start(zr);
828
829         return res;
830 }
831
832 /*
833  *   Sync on a MJPEG buffer
834  */
835
836 static int
837 jpg_sync (struct file       *file,
838           struct zoran_sync *bs)
839 {
840         struct zoran_fh *fh = file->private_data;
841         struct zoran *zr = fh->zr;
842         unsigned long flags;
843         int frame;
844
845         if (fh->jpg_buffers.active == ZORAN_FREE) {
846                 dprintk(1,
847                         KERN_ERR
848                         "%s: jpg_sync() - capture is not currently active\n",
849                         ZR_DEVNAME(zr));
850                 return -EINVAL;
851         }
852         if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
853             zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
854                 dprintk(1,
855                         KERN_ERR
856                         "%s: jpg_sync() - codec not in streaming mode\n",
857                         ZR_DEVNAME(zr));
858                 return -EINVAL;
859         }
860         if (!wait_event_interruptible_timeout(zr->jpg_capq,
861                         (zr->jpg_que_tail != zr->jpg_dma_tail ||
862                          zr->jpg_dma_tail == zr->jpg_dma_head),
863                         10*HZ)) {
864                 int isr;
865
866                 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
867                 udelay(1);
868                 zr->codec->control(zr->codec, CODEC_G_STATUS,
869                                            sizeof(isr), &isr);
870                 dprintk(1,
871                         KERN_ERR
872                         "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
873                         ZR_DEVNAME(zr), isr);
874
875                 return -ETIME;
876
877         }
878         if (signal_pending(current))
879                 return -ERESTARTSYS;
880
881         spin_lock_irqsave(&zr->spinlock, flags);
882
883         if (zr->jpg_dma_tail != zr->jpg_dma_head)
884                 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
885         else
886                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
887
888         /* buffer should now be in BUZ_STATE_DONE */
889         if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
890                 dprintk(2,
891                         KERN_ERR "%s: jpg_sync() - internal state error\n",
892                         ZR_DEVNAME(zr));
893
894         *bs = zr->jpg_buffers.buffer[frame].bs;
895         bs->frame = frame;
896         zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
897         fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
898
899         spin_unlock_irqrestore(&zr->spinlock, flags);
900
901         return 0;
902 }
903
904 static void
905 zoran_open_init_session (struct file *file)
906 {
907         int i;
908         struct zoran_fh *fh = file->private_data;
909         struct zoran *zr = fh->zr;
910
911         /* Per default, map the V4L Buffers */
912         fh->map_mode = ZORAN_MAP_MODE_RAW;
913
914         /* take over the card's current settings */
915         fh->overlay_settings = zr->overlay_settings;
916         fh->overlay_settings.is_set = 0;
917         fh->overlay_settings.format = zr->overlay_settings.format;
918         fh->overlay_active = ZORAN_FREE;
919
920         /* v4l settings */
921         fh->v4l_settings = zr->v4l_settings;
922
923         /* v4l_buffers */
924         memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
925         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
926                 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
927                 fh->v4l_buffers.buffer[i].bs.frame = i;
928         }
929         fh->v4l_buffers.allocated = 0;
930         fh->v4l_buffers.active = ZORAN_FREE;
931         fh->v4l_buffers.buffer_size = v4l_bufsize;
932         fh->v4l_buffers.num_buffers = v4l_nbufs;
933
934         /* jpg settings */
935         fh->jpg_settings = zr->jpg_settings;
936
937         /* jpg_buffers */
938         memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
939         for (i = 0; i < BUZ_MAX_FRAME; i++) {
940                 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
941                 fh->jpg_buffers.buffer[i].bs.frame = i;
942         }
943         fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
944         fh->jpg_buffers.allocated = 0;
945         fh->jpg_buffers.active = ZORAN_FREE;
946         fh->jpg_buffers.buffer_size = jpg_bufsize;
947         fh->jpg_buffers.num_buffers = jpg_nbufs;
948 }
949
950 static void
951 zoran_close_end_session (struct file *file)
952 {
953         struct zoran_fh *fh = file->private_data;
954         struct zoran *zr = fh->zr;
955
956         /* overlay */
957         if (fh->overlay_active != ZORAN_FREE) {
958                 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
959                 zr->v4l_overlay_active = 0;
960                 if (!zr->v4l_memgrab_active)
961                         zr36057_overlay(zr, 0);
962                 zr->overlay_mask = NULL;
963         }
964
965         /* v4l capture */
966         if (fh->v4l_buffers.active != ZORAN_FREE) {
967                 unsigned long flags;
968
969                 spin_lock_irqsave(&zr->spinlock, flags);
970                 zr36057_set_memgrab(zr, 0);
971                 zr->v4l_buffers.allocated = 0;
972                 zr->v4l_buffers.active = fh->v4l_buffers.active =
973                     ZORAN_FREE;
974                 spin_unlock_irqrestore(&zr->spinlock, flags);
975         }
976
977         /* v4l buffers */
978         if (fh->v4l_buffers.allocated)
979                 v4l_fbuffer_free(file);
980
981         /* jpg capture */
982         if (fh->jpg_buffers.active != ZORAN_FREE) {
983                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
984                 zr->jpg_buffers.allocated = 0;
985                 zr->jpg_buffers.active = fh->jpg_buffers.active =
986                     ZORAN_FREE;
987         }
988
989         /* jpg buffers */
990         if (fh->jpg_buffers.allocated)
991                 jpg_fbuffer_free(file);
992 }
993
994 /*
995  *   Open a zoran card. Right now the flags stuff is just playing
996  */
997
998 static int zoran_open(struct file *file)
999 {
1000         struct zoran *zr = video_drvdata(file);
1001         struct zoran_fh *fh;
1002         int res, first_open = 0;
1003
1004         dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1005                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
1006
1007         lock_kernel();
1008
1009         /* see fs/device.c - the kernel already locks during open(),
1010          * so locking ourselves only causes deadlocks */
1011         /*mutex_lock(&zr->resource_lock);*/
1012
1013         if (zr->user >= 2048) {
1014                 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1015                         ZR_DEVNAME(zr), zr->user);
1016                 res = -EBUSY;
1017                 goto fail_unlock;
1018         }
1019
1020         if (!zr->decoder) {
1021                 dprintk(1,
1022                         KERN_ERR "%s: no TV decoder loaded for device!\n",
1023                         ZR_DEVNAME(zr));
1024                 res = -EIO;
1025                 goto fail_unlock;
1026         }
1027
1028         if (!try_module_get(zr->decoder->driver->driver.owner)) {
1029                 dprintk(1,
1030                         KERN_ERR
1031                         "%s: failed to grab ownership of video decoder\n",
1032                         ZR_DEVNAME(zr));
1033                 res = -EIO;
1034                 goto fail_unlock;
1035         }
1036         if (zr->encoder &&
1037             !try_module_get(zr->encoder->driver->driver.owner)) {
1038                 dprintk(1,
1039                         KERN_ERR
1040                         "%s: failed to grab ownership of video encoder\n",
1041                         ZR_DEVNAME(zr));
1042                 res = -EIO;
1043                 goto fail_decoder;
1044         }
1045
1046         /* now, create the open()-specific file_ops struct */
1047         fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1048         if (!fh) {
1049                 dprintk(1,
1050                         KERN_ERR
1051                         "%s: zoran_open() - allocation of zoran_fh failed\n",
1052                         ZR_DEVNAME(zr));
1053                 res = -ENOMEM;
1054                 goto fail_encoder;
1055         }
1056         /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1057          * on norm-change! */
1058         fh->overlay_mask =
1059             kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1060         if (!fh->overlay_mask) {
1061                 dprintk(1,
1062                         KERN_ERR
1063                         "%s: zoran_open() - allocation of overlay_mask failed\n",
1064                         ZR_DEVNAME(zr));
1065                 res = -ENOMEM;
1066                 goto fail_fh;
1067         }
1068
1069         if (zr->user++ == 0)
1070                 first_open = 1;
1071
1072         /*mutex_unlock(&zr->resource_lock);*/
1073
1074         /* default setup - TODO: look at flags */
1075         if (first_open) {       /* First device open */
1076                 zr36057_restart(zr);
1077                 zoran_open_init_params(zr);
1078                 zoran_init_hardware(zr);
1079
1080                 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1081         }
1082
1083         /* set file_ops stuff */
1084         file->private_data = fh;
1085         fh->zr = zr;
1086         zoran_open_init_session(file);
1087         unlock_kernel();
1088
1089         return 0;
1090
1091 fail_fh:
1092         kfree(fh);
1093 fail_encoder:
1094         if (zr->encoder)
1095                 module_put(zr->encoder->driver->driver.owner);
1096 fail_decoder:
1097         module_put(zr->decoder->driver->driver.owner);
1098 fail_unlock:
1099         unlock_kernel();
1100
1101         dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
1102                 ZR_DEVNAME(zr), res, zr->user);
1103
1104         return res;
1105 }
1106
1107 static int
1108 zoran_close(struct file  *file)
1109 {
1110         struct zoran_fh *fh = file->private_data;
1111         struct zoran *zr = fh->zr;
1112
1113         dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1114                 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
1115
1116         /* kernel locks (fs/device.c), so don't do that ourselves
1117          * (prevents deadlocks) */
1118         /*mutex_lock(&zr->resource_lock);*/
1119
1120         zoran_close_end_session(file);
1121
1122         if (zr->user-- == 1) {  /* Last process */
1123                 /* Clean up JPEG process */
1124                 wake_up_interruptible(&zr->jpg_capq);
1125                 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1126                 zr->jpg_buffers.allocated = 0;
1127                 zr->jpg_buffers.active = ZORAN_FREE;
1128
1129                 /* disable interrupts */
1130                 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1131
1132                 if (zr36067_debug > 1)
1133                         print_interrupts(zr);
1134
1135                 /* Overlay off */
1136                 zr->v4l_overlay_active = 0;
1137                 zr36057_overlay(zr, 0);
1138                 zr->overlay_mask = NULL;
1139
1140                 /* capture off */
1141                 wake_up_interruptible(&zr->v4l_capq);
1142                 zr36057_set_memgrab(zr, 0);
1143                 zr->v4l_buffers.allocated = 0;
1144                 zr->v4l_buffers.active = ZORAN_FREE;
1145                 zoran_set_pci_master(zr, 0);
1146
1147                 if (!pass_through) {    /* Switch to color bar */
1148                         int zero = 0, two = 2;
1149                         decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1150                         encoder_command(zr, ENCODER_SET_INPUT, &two);
1151                 }
1152         }
1153
1154         file->private_data = NULL;
1155         kfree(fh->overlay_mask);
1156         kfree(fh);
1157
1158         /* release locks on the i2c modules */
1159         module_put(zr->decoder->driver->driver.owner);
1160         if (zr->encoder)
1161                 module_put(zr->encoder->driver->driver.owner);
1162
1163         /*mutex_unlock(&zr->resource_lock);*/
1164
1165         dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1166
1167         return 0;
1168 }
1169
1170
1171 static ssize_t
1172 zoran_read (struct file *file,
1173             char        __user *data,
1174             size_t       count,
1175             loff_t      *ppos)
1176 {
1177         /* we simply don't support read() (yet)... */
1178
1179         return -EINVAL;
1180 }
1181
1182 static ssize_t
1183 zoran_write (struct file *file,
1184              const char  __user *data,
1185              size_t       count,
1186              loff_t      *ppos)
1187 {
1188         /* ...and the same goes for write() */
1189
1190         return -EINVAL;
1191 }
1192
1193 static int
1194 setup_fbuffer (struct file               *file,
1195                void                      *base,
1196                const struct zoran_format *fmt,
1197                int                        width,
1198                int                        height,
1199                int                        bytesperline)
1200 {
1201         struct zoran_fh *fh = file->private_data;
1202         struct zoran *zr = fh->zr;
1203
1204         /* (Ronald) v4l/v4l2 guidelines */
1205         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1206                 return -EPERM;
1207
1208         /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1209            ALi Magik (that needs very low latency while the card needs a
1210            higher value always) */
1211
1212         if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1213                 return -ENXIO;
1214
1215         /* we need a bytesperline value, even if not given */
1216         if (!bytesperline)
1217                 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1218
1219 #if 0
1220         if (zr->overlay_active) {
1221                 /* dzjee... stupid users... don't even bother to turn off
1222                  * overlay before changing the memory location...
1223                  * normally, we would return errors here. However, one of
1224                  * the tools that does this is... xawtv! and since xawtv
1225                  * is used by +/- 99% of the users, we'd rather be user-
1226                  * friendly and silently do as if nothing went wrong */
1227                 dprintk(3,
1228                         KERN_ERR
1229                         "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1230                         ZR_DEVNAME(zr));
1231                 zr36057_overlay(zr, 0);
1232         }
1233 #endif
1234
1235         if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1236                 dprintk(1,
1237                         KERN_ERR
1238                         "%s: setup_fbuffer() - no valid overlay format given\n",
1239                         ZR_DEVNAME(zr));
1240                 return -EINVAL;
1241         }
1242         if (height <= 0 || width <= 0 || bytesperline <= 0) {
1243                 dprintk(1,
1244                         KERN_ERR
1245                         "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1246                         ZR_DEVNAME(zr), width, height, bytesperline);
1247                 return -EINVAL;
1248         }
1249         if (bytesperline & 3) {
1250                 dprintk(1,
1251                         KERN_ERR
1252                         "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1253                         ZR_DEVNAME(zr), bytesperline);
1254                 return -EINVAL;
1255         }
1256
1257         zr->buffer.base = (void *) ((unsigned long) base & ~3);
1258         zr->buffer.height = height;
1259         zr->buffer.width = width;
1260         zr->buffer.depth = fmt->depth;
1261         zr->overlay_settings.format = fmt;
1262         zr->buffer.bytesperline = bytesperline;
1263
1264         /* The user should set new window parameters */
1265         zr->overlay_settings.is_set = 0;
1266
1267         return 0;
1268 }
1269
1270
1271 static int
1272 setup_window (struct file       *file,
1273               int                x,
1274               int                y,
1275               int                width,
1276               int                height,
1277               struct video_clip __user *clips,
1278               int                clipcount,
1279               void              __user *bitmap)
1280 {
1281         struct zoran_fh *fh = file->private_data;
1282         struct zoran *zr = fh->zr;
1283         struct video_clip *vcp = NULL;
1284         int on, end;
1285
1286
1287         if (!zr->buffer.base) {
1288                 dprintk(1,
1289                         KERN_ERR
1290                         "%s: setup_window() - frame buffer has to be set first\n",
1291                         ZR_DEVNAME(zr));
1292                 return -EINVAL;
1293         }
1294
1295         if (!fh->overlay_settings.format) {
1296                 dprintk(1,
1297                         KERN_ERR
1298                         "%s: setup_window() - no overlay format set\n",
1299                         ZR_DEVNAME(zr));
1300                 return -EINVAL;
1301         }
1302
1303         /*
1304          * The video front end needs 4-byte alinged line sizes, we correct that
1305          * silently here if necessary
1306          */
1307         if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1308                 end = (x + width) & ~1; /* round down */
1309                 x = (x + 1) & ~1;       /* round up */
1310                 width = end - x;
1311         }
1312
1313         if (zr->buffer.depth == 24) {
1314                 end = (x + width) & ~3; /* round down */
1315                 x = (x + 3) & ~3;       /* round up */
1316                 width = end - x;
1317         }
1318
1319         if (width > BUZ_MAX_WIDTH)
1320                 width = BUZ_MAX_WIDTH;
1321         if (height > BUZ_MAX_HEIGHT)
1322                 height = BUZ_MAX_HEIGHT;
1323
1324         /* Check for vaild parameters */
1325         if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1326             width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1327                 dprintk(1,
1328                         KERN_ERR
1329                         "%s: setup_window() - width = %d or height = %d invalid\n",
1330                         ZR_DEVNAME(zr), width, height);
1331                 return -EINVAL;
1332         }
1333
1334         fh->overlay_settings.x = x;
1335         fh->overlay_settings.y = y;
1336         fh->overlay_settings.width = width;
1337         fh->overlay_settings.height = height;
1338         fh->overlay_settings.clipcount = clipcount;
1339
1340         /*
1341          * If an overlay is running, we have to switch it off
1342          * and switch it on again in order to get the new settings in effect.
1343          *
1344          * We also want to avoid that the overlay mask is written
1345          * when an overlay is running.
1346          */
1347
1348         on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1349             zr->overlay_active != ZORAN_FREE &&
1350             fh->overlay_active != ZORAN_FREE;
1351         if (on)
1352                 zr36057_overlay(zr, 0);
1353
1354         /*
1355          *   Write the overlay mask if clips are wanted.
1356          *   We prefer a bitmap.
1357          */
1358         if (bitmap) {
1359                 /* fake value - it just means we want clips */
1360                 fh->overlay_settings.clipcount = 1;
1361
1362                 if (copy_from_user(fh->overlay_mask, bitmap,
1363                                    (width * height + 7) / 8)) {
1364                         return -EFAULT;
1365                 }
1366         } else if (clipcount > 0) {
1367                 /* write our own bitmap from the clips */
1368                 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1369                 if (vcp == NULL) {
1370                         dprintk(1,
1371                                 KERN_ERR
1372                                 "%s: setup_window() - Alloc of clip mask failed\n",
1373                                 ZR_DEVNAME(zr));
1374                         return -ENOMEM;
1375                 }
1376                 if (copy_from_user
1377                     (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1378                         vfree(vcp);
1379                         return -EFAULT;
1380                 }
1381                 write_overlay_mask(file, vcp, clipcount);
1382                 vfree(vcp);
1383         }
1384
1385         fh->overlay_settings.is_set = 1;
1386         if (fh->overlay_active != ZORAN_FREE &&
1387             zr->overlay_active != ZORAN_FREE)
1388                 zr->overlay_settings = fh->overlay_settings;
1389
1390         if (on)
1391                 zr36057_overlay(zr, 1);
1392
1393         /* Make sure the changes come into effect */
1394         return wait_grab_pending(zr);
1395 }
1396
1397 static int
1398 setup_overlay (struct file *file,
1399                int          on)
1400 {
1401         struct zoran_fh *fh = file->private_data;
1402         struct zoran *zr = fh->zr;
1403
1404         /* If there is nothing to do, return immediatly */
1405         if ((on && fh->overlay_active != ZORAN_FREE) ||
1406             (!on && fh->overlay_active == ZORAN_FREE))
1407                 return 0;
1408
1409         /* check whether we're touching someone else's overlay */
1410         if (on && zr->overlay_active != ZORAN_FREE &&
1411             fh->overlay_active == ZORAN_FREE) {
1412                 dprintk(1,
1413                         KERN_ERR
1414                         "%s: setup_overlay() - overlay is already active for another session\n",
1415                         ZR_DEVNAME(zr));
1416                 return -EBUSY;
1417         }
1418         if (!on && zr->overlay_active != ZORAN_FREE &&
1419             fh->overlay_active == ZORAN_FREE) {
1420                 dprintk(1,
1421                         KERN_ERR
1422                         "%s: setup_overlay() - you cannot cancel someone else's session\n",
1423                         ZR_DEVNAME(zr));
1424                 return -EPERM;
1425         }
1426
1427         if (on == 0) {
1428                 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1429                 zr->v4l_overlay_active = 0;
1430                 /* When a grab is running, the video simply
1431                  * won't be switched on any more */
1432                 if (!zr->v4l_memgrab_active)
1433                         zr36057_overlay(zr, 0);
1434                 zr->overlay_mask = NULL;
1435         } else {
1436                 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1437                         dprintk(1,
1438                                 KERN_ERR
1439                                 "%s: setup_overlay() - buffer or window not set\n",
1440                                 ZR_DEVNAME(zr));
1441                         return -EINVAL;
1442                 }
1443                 if (!fh->overlay_settings.format) {
1444                         dprintk(1,
1445                                 KERN_ERR
1446                                 "%s: setup_overlay() - no overlay format set\n",
1447                                 ZR_DEVNAME(zr));
1448                         return -EINVAL;
1449                 }
1450                 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1451                 zr->v4l_overlay_active = 1;
1452                 zr->overlay_mask = fh->overlay_mask;
1453                 zr->overlay_settings = fh->overlay_settings;
1454                 if (!zr->v4l_memgrab_active)
1455                         zr36057_overlay(zr, 1);
1456                 /* When a grab is running, the video will be
1457                  * switched on when grab is finished */
1458         }
1459
1460         /* Make sure the changes come into effect */
1461         return wait_grab_pending(zr);
1462 }
1463
1464         /* get the status of a buffer in the clients buffer queue */
1465 static int
1466 zoran_v4l2_buffer_status (struct file        *file,
1467                           struct v4l2_buffer *buf,
1468                           int                 num)
1469 {
1470         struct zoran_fh *fh = file->private_data;
1471         struct zoran *zr = fh->zr;
1472
1473         buf->flags = V4L2_BUF_FLAG_MAPPED;
1474
1475         switch (fh->map_mode) {
1476         case ZORAN_MAP_MODE_RAW:
1477
1478                 /* check range */
1479                 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1480                     !fh->v4l_buffers.allocated) {
1481                         dprintk(1,
1482                                 KERN_ERR
1483                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1484                                 ZR_DEVNAME(zr));
1485                         return -EINVAL;
1486                 }
1487
1488                 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1489                 buf->length = fh->v4l_buffers.buffer_size;
1490
1491                 /* get buffer */
1492                 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1493                 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1494                     fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1495                         buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1496                         buf->flags |= V4L2_BUF_FLAG_DONE;
1497                         buf->timestamp =
1498                             fh->v4l_buffers.buffer[num].bs.timestamp;
1499                 } else {
1500                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1501                 }
1502
1503                 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1504                         buf->field = V4L2_FIELD_TOP;
1505                 else
1506                         buf->field = V4L2_FIELD_INTERLACED;
1507
1508                 break;
1509
1510         case ZORAN_MAP_MODE_JPG_REC:
1511         case ZORAN_MAP_MODE_JPG_PLAY:
1512
1513                 /* check range */
1514                 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1515                     !fh->jpg_buffers.allocated) {
1516                         dprintk(1,
1517                                 KERN_ERR
1518                                 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1519                                 ZR_DEVNAME(zr));
1520                         return -EINVAL;
1521                 }
1522
1523                 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1524                               V4L2_BUF_TYPE_VIDEO_CAPTURE :
1525                               V4L2_BUF_TYPE_VIDEO_OUTPUT;
1526                 buf->length = fh->jpg_buffers.buffer_size;
1527
1528                 /* these variables are only written after frame has been captured */
1529                 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1530                     fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1531                         buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1532                         buf->timestamp =
1533                             fh->jpg_buffers.buffer[num].bs.timestamp;
1534                         buf->bytesused =
1535                             fh->jpg_buffers.buffer[num].bs.length;
1536                         buf->flags |= V4L2_BUF_FLAG_DONE;
1537                 } else {
1538                         buf->flags |= V4L2_BUF_FLAG_QUEUED;
1539                 }
1540
1541                 /* which fields are these? */
1542                 if (fh->jpg_settings.TmpDcm != 1)
1543                         buf->field =
1544                             fh->jpg_settings.
1545                             odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1546                 else
1547                         buf->field =
1548                             fh->jpg_settings.
1549                             odd_even ? V4L2_FIELD_SEQ_TB :
1550                             V4L2_FIELD_SEQ_BT;
1551
1552                 break;
1553
1554         default:
1555
1556                 dprintk(5,
1557                         KERN_ERR
1558                         "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1559                         ZR_DEVNAME(zr), buf->type, fh->map_mode);
1560                 return -EINVAL;
1561         }
1562
1563         buf->memory = V4L2_MEMORY_MMAP;
1564         buf->index = num;
1565         buf->m.offset = buf->length * num;
1566
1567         return 0;
1568 }
1569
1570 static int
1571 zoran_set_norm (struct zoran *zr,
1572                 int           norm) /* VIDEO_MODE_* */
1573 {
1574         int norm_encoder, on;
1575
1576         if (zr->v4l_buffers.active != ZORAN_FREE ||
1577             zr->jpg_buffers.active != ZORAN_FREE) {
1578                 dprintk(1,
1579                         KERN_WARNING
1580                         "%s: set_norm() called while in playback/capture mode\n",
1581                         ZR_DEVNAME(zr));
1582                 return -EBUSY;
1583         }
1584
1585         if (lock_norm && norm != zr->norm) {
1586                 if (lock_norm > 1) {
1587                         dprintk(1,
1588                                 KERN_WARNING
1589                                 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1590                                 ZR_DEVNAME(zr));
1591                         return -EPERM;
1592                 } else {
1593                         dprintk(1,
1594                                 KERN_WARNING
1595                                 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1596                                 ZR_DEVNAME(zr));
1597                         norm = zr->norm;
1598                 }
1599         }
1600
1601         if (norm != VIDEO_MODE_AUTO &&
1602             (norm < 0 || norm >= zr->card.norms ||
1603              !zr->card.tvn[norm])) {
1604                 dprintk(1,
1605                         KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1606                         ZR_DEVNAME(zr), norm);
1607                 return -EINVAL;
1608         }
1609
1610         if (norm == VIDEO_MODE_AUTO) {
1611                 int status;
1612
1613                 /* if we have autodetect, ... */
1614                 struct video_decoder_capability caps;
1615                 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1616                 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1617                         dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1618                                 ZR_DEVNAME(zr));
1619                         return -EINVAL;
1620                 }
1621
1622                 decoder_command(zr, DECODER_SET_NORM, &norm);
1623
1624                 /* let changes come into effect */
1625                 ssleep(2);
1626
1627                 decoder_command(zr, DECODER_GET_STATUS, &status);
1628                 if (!(status & DECODER_STATUS_GOOD)) {
1629                         dprintk(1,
1630                                 KERN_ERR
1631                                 "%s: set_norm() - no norm detected\n",
1632                                 ZR_DEVNAME(zr));
1633                         /* reset norm */
1634                         decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1635                         return -EIO;
1636                 }
1637
1638                 if (status & DECODER_STATUS_NTSC)
1639                         norm = VIDEO_MODE_NTSC;
1640                 else if (status & DECODER_STATUS_SECAM)
1641                         norm = VIDEO_MODE_SECAM;
1642                 else
1643                         norm = VIDEO_MODE_PAL;
1644         }
1645         zr->timing = zr->card.tvn[norm];
1646         norm_encoder = norm;
1647
1648         /* We switch overlay off and on since a change in the
1649          * norm needs different VFE settings */
1650         on = zr->overlay_active && !zr->v4l_memgrab_active;
1651         if (on)
1652                 zr36057_overlay(zr, 0);
1653
1654         decoder_command(zr, DECODER_SET_NORM, &norm);
1655         encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1656
1657         if (on)
1658                 zr36057_overlay(zr, 1);
1659
1660         /* Make sure the changes come into effect */
1661         zr->norm = norm;
1662
1663         return 0;
1664 }
1665
1666 static int
1667 zoran_set_input (struct zoran *zr,
1668                  int           input)
1669 {
1670         int realinput;
1671
1672         if (input == zr->input) {
1673                 return 0;
1674         }
1675
1676         if (zr->v4l_buffers.active != ZORAN_FREE ||
1677             zr->jpg_buffers.active != ZORAN_FREE) {
1678                 dprintk(1,
1679                         KERN_WARNING
1680                         "%s: set_input() called while in playback/capture mode\n",
1681                         ZR_DEVNAME(zr));
1682                 return -EBUSY;
1683         }
1684
1685         if (input < 0 || input >= zr->card.inputs) {
1686                 dprintk(1,
1687                         KERN_ERR
1688                         "%s: set_input() - unnsupported input %d\n",
1689                         ZR_DEVNAME(zr), input);
1690                 return -EINVAL;
1691         }
1692
1693         realinput = zr->card.input[input].muxsel;
1694         zr->input = input;
1695
1696         decoder_command(zr, DECODER_SET_INPUT, &realinput);
1697
1698         return 0;
1699 }
1700
1701 /*
1702  *   ioctl routine
1703  */
1704
1705 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1706 static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1707 {
1708         struct zoran_fh *fh = __fh;
1709         struct zoran *zr = fh->zr;
1710         struct zoran_jpg_settings settings;
1711
1712         switch (cmd) {
1713         case BUZIOC_G_PARAMS:
1714         {
1715                 struct zoran_params *bparams = arg;
1716
1717                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1718
1719                 memset(bparams, 0, sizeof(struct zoran_params));
1720                 bparams->major_version = MAJOR_VERSION;
1721                 bparams->minor_version = MINOR_VERSION;
1722
1723                 mutex_lock(&zr->resource_lock);
1724
1725                 bparams->norm = zr->norm;
1726                 bparams->input = zr->input;
1727
1728                 bparams->decimation = fh->jpg_settings.decimation;
1729                 bparams->HorDcm = fh->jpg_settings.HorDcm;
1730                 bparams->VerDcm = fh->jpg_settings.VerDcm;
1731                 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1732                 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1733                 bparams->img_x = fh->jpg_settings.img_x;
1734                 bparams->img_y = fh->jpg_settings.img_y;
1735                 bparams->img_width = fh->jpg_settings.img_width;
1736                 bparams->img_height = fh->jpg_settings.img_height;
1737                 bparams->odd_even = fh->jpg_settings.odd_even;
1738
1739                 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1740                 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1741                 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1742                 memcpy(bparams->APP_data,
1743                        fh->jpg_settings.jpg_comp.APP_data,
1744                        sizeof(bparams->APP_data));
1745                 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1746                 memcpy(bparams->COM_data,
1747                        fh->jpg_settings.jpg_comp.COM_data,
1748                        sizeof(bparams->COM_data));
1749                 bparams->jpeg_markers =
1750                     fh->jpg_settings.jpg_comp.jpeg_markers;
1751
1752                 mutex_unlock(&zr->resource_lock);
1753
1754                 bparams->VFIFO_FB = 0;
1755
1756                 return 0;
1757         }
1758
1759         case BUZIOC_S_PARAMS:
1760         {
1761                 struct zoran_params *bparams = arg;
1762                 int res = 0;
1763
1764                 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1765
1766                 settings.decimation = bparams->decimation;
1767                 settings.HorDcm = bparams->HorDcm;
1768                 settings.VerDcm = bparams->VerDcm;
1769                 settings.TmpDcm = bparams->TmpDcm;
1770                 settings.field_per_buff = bparams->field_per_buff;
1771                 settings.img_x = bparams->img_x;
1772                 settings.img_y = bparams->img_y;
1773                 settings.img_width = bparams->img_width;
1774                 settings.img_height = bparams->img_height;
1775                 settings.odd_even = bparams->odd_even;
1776
1777                 settings.jpg_comp.quality = bparams->quality;
1778                 settings.jpg_comp.APPn = bparams->APPn;
1779                 settings.jpg_comp.APP_len = bparams->APP_len;
1780                 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1781                        sizeof(bparams->APP_data));
1782                 settings.jpg_comp.COM_len = bparams->COM_len;
1783                 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1784                        sizeof(bparams->COM_data));
1785                 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1786
1787                 mutex_lock(&zr->resource_lock);
1788
1789                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1790                         dprintk(1,
1791                                 KERN_ERR
1792                                 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1793                                 ZR_DEVNAME(zr));
1794                         res = -EINVAL;
1795                         goto sparams_unlock_and_return;
1796                 }
1797
1798                 /* Check the params first before overwriting our
1799                  * nternal values */
1800                 if (zoran_check_jpg_settings(zr, &settings)) {
1801                         res = -EINVAL;
1802                         goto sparams_unlock_and_return;
1803                 }
1804
1805                 fh->jpg_settings = settings;
1806 sparams_unlock_and_return:
1807                 mutex_unlock(&zr->resource_lock);
1808
1809                 return res;
1810         }
1811
1812         case BUZIOC_REQBUFS:
1813         {
1814                 struct zoran_requestbuffers *breq = arg;
1815                 int res = 0;
1816
1817                 dprintk(3,
1818                         KERN_DEBUG
1819                         "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1820                         ZR_DEVNAME(zr), breq->count, breq->size);
1821
1822                 /* Enforce reasonable lower and upper limits */
1823                 if (breq->count < 4)
1824                         breq->count = 4;        /* Could be choosen smaller */
1825                 if (breq->count > jpg_nbufs)
1826                         breq->count = jpg_nbufs;
1827                 breq->size = PAGE_ALIGN(breq->size);
1828                 if (breq->size < 8192)
1829                         breq->size = 8192;      /* Arbitrary */
1830                 /* breq->size is limited by 1 page for the stat_com
1831                  * tables to a Maximum of 2 MB */
1832                 if (breq->size > jpg_bufsize)
1833                         breq->size = jpg_bufsize;
1834
1835                 mutex_lock(&zr->resource_lock);
1836
1837                 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1838                         dprintk(1,
1839                                 KERN_ERR
1840                                 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1841                                 ZR_DEVNAME(zr));
1842                         res = -EBUSY;
1843                         goto jpgreqbuf_unlock_and_return;
1844                 }
1845
1846                 fh->jpg_buffers.num_buffers = breq->count;
1847                 fh->jpg_buffers.buffer_size = breq->size;
1848
1849                 if (jpg_fbuffer_alloc(file)) {
1850                         res = -ENOMEM;
1851                         goto jpgreqbuf_unlock_and_return;
1852                 }
1853
1854                 /* The next mmap will map the MJPEG buffers - could
1855                  * also be *_PLAY, but it doesn't matter here */
1856                 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
1857 jpgreqbuf_unlock_and_return:
1858                 mutex_unlock(&zr->resource_lock);
1859
1860                 return res;
1861         }
1862
1863         case BUZIOC_QBUF_CAPT:
1864         {
1865                 int *frame = arg, res;
1866
1867                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1868                         ZR_DEVNAME(zr), *frame);
1869
1870                 mutex_lock(&zr->resource_lock);
1871                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
1872                 mutex_unlock(&zr->resource_lock);
1873
1874                 return res;
1875         }
1876
1877         case BUZIOC_QBUF_PLAY:
1878         {
1879                 int *frame = arg, res;
1880
1881                 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1882                         ZR_DEVNAME(zr), *frame);
1883
1884                 mutex_lock(&zr->resource_lock);
1885                 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
1886                 mutex_unlock(&zr->resource_lock);
1887
1888                 return res;
1889         }
1890
1891         case BUZIOC_SYNC:
1892         {
1893                 struct zoran_sync *bsync = arg;
1894                 int res;
1895
1896                 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1897
1898                 mutex_lock(&zr->resource_lock);
1899                 res = jpg_sync(file, bsync);
1900                 mutex_unlock(&zr->resource_lock);
1901
1902                 return res;
1903         }
1904
1905         case BUZIOC_G_STATUS:
1906         {
1907                 struct zoran_status *bstat = arg;
1908                 int norm, input, status, res = 0;
1909
1910                 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1911
1912                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1913                         dprintk(1,
1914                                 KERN_ERR
1915                                 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1916                                 ZR_DEVNAME(zr));
1917                         return -EINVAL;
1918                 }
1919
1920                 input = zr->card.input[bstat->input].muxsel;
1921                 norm = VIDEO_MODE_AUTO;
1922
1923                 mutex_lock(&zr->resource_lock);
1924
1925                 if (zr->codec_mode != BUZ_MODE_IDLE) {
1926                         dprintk(1,
1927                                 KERN_ERR
1928                                 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1929                                 ZR_DEVNAME(zr));
1930                         res = -EINVAL;
1931                         goto gstat_unlock_and_return;
1932                 }
1933
1934                 decoder_command(zr, DECODER_SET_INPUT, &input);
1935                 decoder_command(zr, DECODER_SET_NORM, &norm);
1936
1937                 /* sleep 1 second */
1938                 ssleep(1);
1939
1940                 /* Get status of video decoder */
1941                 decoder_command(zr, DECODER_GET_STATUS, &status);
1942
1943                 /* restore previous input and norm */
1944                 input = zr->card.input[zr->input].muxsel;
1945                 decoder_command(zr, DECODER_SET_INPUT, &input);
1946                 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1947 gstat_unlock_and_return:
1948                 mutex_unlock(&zr->resource_lock);
1949
1950                 if (!res) {
1951                         bstat->signal =
1952                             (status & DECODER_STATUS_GOOD) ? 1 : 0;
1953                         if (status & DECODER_STATUS_NTSC)
1954                                 bstat->norm = VIDEO_MODE_NTSC;
1955                         else if (status & DECODER_STATUS_SECAM)
1956                                 bstat->norm = VIDEO_MODE_SECAM;
1957                         else
1958                                 bstat->norm = VIDEO_MODE_PAL;
1959
1960                         bstat->color =
1961                             (status & DECODER_STATUS_COLOR) ? 1 : 0;
1962                 }
1963
1964                 return res;
1965         }
1966
1967         default:
1968                 return -EINVAL;
1969         }
1970 }
1971
1972 static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1973 {
1974         struct zoran_fh *fh = __fh;
1975         struct zoran *zr = fh->zr;
1976         int i, res = 0;
1977
1978         vmbuf->size =
1979             fh->v4l_buffers.num_buffers *
1980             fh->v4l_buffers.buffer_size;
1981         vmbuf->frames = fh->v4l_buffers.num_buffers;
1982         for (i = 0; i < vmbuf->frames; i++) {
1983                 vmbuf->offsets[i] =
1984                     i * fh->v4l_buffers.buffer_size;
1985         }
1986
1987         mutex_lock(&zr->resource_lock);
1988
1989         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
1990                 dprintk(1,
1991                         KERN_ERR
1992                         "%s: VIDIOCGMBUF - buffers already allocated\n",
1993                         ZR_DEVNAME(zr));
1994                 res = -EINVAL;
1995                 goto v4l1reqbuf_unlock_and_return;
1996         }
1997
1998         if (v4l_fbuffer_alloc(file)) {
1999                 res = -ENOMEM;
2000                 goto v4l1reqbuf_unlock_and_return;
2001         }
2002
2003         /* The next mmap will map the V4L buffers */
2004         fh->map_mode = ZORAN_MAP_MODE_RAW;
2005 v4l1reqbuf_unlock_and_return:
2006         mutex_unlock(&zr->resource_lock);
2007
2008         return res;
2009 }
2010 #endif
2011
2012 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
2013 {
2014         struct zoran_fh *fh = __fh;
2015         struct zoran *zr = fh->zr;
2016
2017         memset(cap, 0, sizeof(*cap));
2018         strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2019         strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2020         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2021                  pci_name(zr->pci_dev));
2022         cap->version =
2023             KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2024                            RELEASE_VERSION);
2025         cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2026
2027         return 0;
2028 }
2029
2030 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
2031 {
2032         int num = -1, i;
2033
2034         for (i = 0; i < NUM_FORMATS; i++) {
2035                 if (zoran_formats[i].flags & flag)
2036                         num++;
2037                 if (num == fmt->index)
2038                         break;
2039         }
2040         if (fmt->index < 0 /* late, but not too late */  || i == NUM_FORMATS)
2041                 return -EINVAL;
2042
2043         strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2044         fmt->pixelformat = zoran_formats[i].fourcc;
2045         if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2046                 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2047         return 0;
2048 }
2049
2050 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
2051                                             struct v4l2_fmtdesc *f)
2052 {
2053         struct zoran_fh *fh = __fh;
2054         struct zoran *zr = fh->zr;
2055
2056         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
2057 }
2058
2059 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
2060                                             struct v4l2_fmtdesc *f)
2061 {
2062         struct zoran_fh *fh = __fh;
2063         struct zoran *zr = fh->zr;
2064
2065         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
2066 }
2067
2068 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
2069                                             struct v4l2_fmtdesc *f)
2070 {
2071         struct zoran_fh *fh = __fh;
2072         struct zoran *zr = fh->zr;
2073
2074         return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
2075 }
2076
2077 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
2078                                         struct v4l2_format *fmt)
2079 {
2080         struct zoran_fh *fh = __fh;
2081         struct zoran *zr = fh->zr;
2082
2083         mutex_lock(&zr->resource_lock);
2084
2085         fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
2086         fmt->fmt.pix.height = fh->jpg_settings.img_height /
2087                 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
2088         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2089         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
2090         if (fh->jpg_settings.TmpDcm == 1)
2091                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2092                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2093         else
2094                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2095                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2096         fmt->fmt.pix.bytesperline = 0;
2097         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2098
2099         mutex_unlock(&zr->resource_lock);
2100         return 0;
2101 }
2102
2103 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
2104                                         struct v4l2_format *fmt)
2105 {
2106         struct zoran_fh *fh = __fh;
2107         struct zoran *zr = fh->zr;
2108
2109         if (fh->map_mode != ZORAN_MAP_MODE_RAW)
2110                 return zoran_g_fmt_vid_out(file, fh, fmt);
2111
2112         mutex_lock(&zr->resource_lock);
2113         fmt->fmt.pix.width = fh->v4l_settings.width;
2114         fmt->fmt.pix.height = fh->v4l_settings.height;
2115         fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
2116                                         fh->v4l_settings.height;
2117         fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
2118         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2119         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2120         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2121                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2122         else
2123                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2124         mutex_unlock(&zr->resource_lock);
2125         return 0;
2126 }
2127
2128 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
2129                                         struct v4l2_format *fmt)
2130 {
2131         struct zoran_fh *fh = __fh;
2132         struct zoran *zr = fh->zr;
2133
2134         mutex_lock(&zr->resource_lock);
2135
2136         fmt->fmt.win.w.left = fh->overlay_settings.x;
2137         fmt->fmt.win.w.top = fh->overlay_settings.y;
2138         fmt->fmt.win.w.width = fh->overlay_settings.width;
2139         fmt->fmt.win.w.height = fh->overlay_settings.height;
2140         if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
2141                 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2142         else
2143                 fmt->fmt.win.field = V4L2_FIELD_TOP;
2144
2145         mutex_unlock(&zr->resource_lock);
2146         return 0;
2147 }
2148
2149 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
2150                                         struct v4l2_format *fmt)
2151 {
2152         struct zoran_fh *fh = __fh;
2153         struct zoran *zr = fh->zr;
2154
2155         mutex_lock(&zr->resource_lock);
2156
2157         if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
2158                 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
2159         if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
2160                 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
2161         if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
2162                 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
2163         if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
2164                 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
2165
2166         mutex_unlock(&zr->resource_lock);
2167         return 0;
2168 }
2169
2170 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2171                                         struct v4l2_format *fmt)
2172 {
2173         struct zoran_fh *fh = __fh;
2174         struct zoran *zr = fh->zr;
2175         struct zoran_jpg_settings settings;
2176         int res = 0;
2177
2178         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2179                 return -EINVAL;
2180
2181         fmt->fmt.pix.bytesperline = 0;
2182
2183         mutex_lock(&zr->resource_lock);
2184         settings = fh->jpg_settings;
2185
2186         /* we actually need to set 'real' parameters now */
2187         if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2188                 settings.TmpDcm = 1;
2189         else
2190                 settings.TmpDcm = 2;
2191         settings.decimation = 0;
2192         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2193                 settings.VerDcm = 2;
2194         else
2195                 settings.VerDcm = 1;
2196         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2197                 settings.HorDcm = 4;
2198         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2199                 settings.HorDcm = 2;
2200         else
2201                 settings.HorDcm = 1;
2202         if (settings.TmpDcm == 1)
2203                 settings.field_per_buff = 2;
2204         else
2205                 settings.field_per_buff = 1;
2206
2207         /* check */
2208         res = zoran_check_jpg_settings(zr, &settings);
2209         if (res)
2210                 goto tryfmt_unlock_and_return;
2211
2212         /* tell the user what we actually did */
2213         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2214         fmt->fmt.pix.height = settings.img_height * 2 /
2215                 (settings.TmpDcm * settings.VerDcm);
2216         if (settings.TmpDcm == 1)
2217                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2218                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2219         else
2220                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2221                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2222
2223         fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2224 tryfmt_unlock_and_return:
2225         mutex_unlock(&zr->resource_lock);
2226         return res;
2227 }
2228
2229 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2230                                         struct v4l2_format *fmt)
2231 {
2232         struct zoran_fh *fh = __fh;
2233         struct zoran *zr = fh->zr;
2234         int i;
2235
2236         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2237                 return zoran_try_fmt_vid_out(file, fh, fmt);
2238
2239         mutex_lock(&zr->resource_lock);
2240
2241         for (i = 0; i < NUM_FORMATS; i++)
2242                 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2243                         break;
2244
2245         if (i == NUM_FORMATS) {
2246                 mutex_unlock(&zr->resource_lock);
2247                 return -EINVAL;
2248         }
2249
2250         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2251                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2252         if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2253                 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2254         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2255                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2256         if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2257                 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2258         mutex_unlock(&zr->resource_lock);
2259
2260         return 0;
2261 }
2262
2263 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2264                                         struct v4l2_format *fmt)
2265 {
2266         struct zoran_fh *fh = __fh;
2267         struct zoran *zr = fh->zr;
2268         int res;
2269
2270         dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2271                         fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2272                         fmt->fmt.win.w.width,
2273                         fmt->fmt.win.w.height,
2274                         fmt->fmt.win.clipcount,
2275                         fmt->fmt.win.bitmap);
2276         mutex_lock(&zr->resource_lock);
2277         res = setup_window(file, fmt->fmt.win.w.left,
2278                         fmt->fmt.win.w.top,
2279                         fmt->fmt.win.w.width,
2280                         fmt->fmt.win.w.height,
2281                         (struct video_clip __user *)
2282                         fmt->fmt.win.clips,
2283                         fmt->fmt.win.clipcount,
2284                         fmt->fmt.win.bitmap);
2285         mutex_unlock(&zr->resource_lock);
2286         return res;
2287 }
2288
2289 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2290                                         struct v4l2_format *fmt)
2291 {
2292         struct zoran_fh *fh = __fh;
2293         struct zoran *zr = fh->zr;
2294         __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2295         struct zoran_jpg_settings settings;
2296         int res = 0;
2297
2298         dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2299                         fmt->fmt.pix.width, fmt->fmt.pix.height,
2300                         fmt->fmt.pix.pixelformat,
2301                         (char *) &printformat);
2302         if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2303                 return -EINVAL;
2304
2305         mutex_lock(&zr->resource_lock);
2306
2307         settings = fh->jpg_settings;
2308
2309         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2310                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2311                                 ZR_DEVNAME(zr));
2312                 res = -EBUSY;
2313                 goto sfmtjpg_unlock_and_return;
2314         }
2315
2316         /* we actually need to set 'real' parameters now */
2317         if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2318                 settings.TmpDcm = 1;
2319         else
2320                 settings.TmpDcm = 2;
2321         settings.decimation = 0;
2322         if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2323                 settings.VerDcm = 2;
2324         else
2325                 settings.VerDcm = 1;
2326         if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2327                 settings.HorDcm = 4;
2328         else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2329                 settings.HorDcm = 2;
2330         else
2331                 settings.HorDcm = 1;
2332         if (settings.TmpDcm == 1)
2333                 settings.field_per_buff = 2;
2334         else
2335                 settings.field_per_buff = 1;
2336
2337         /* check */
2338         res = zoran_check_jpg_settings(zr, &settings);
2339         if (res)
2340                 goto sfmtjpg_unlock_and_return;
2341
2342         /* it's ok, so set them */
2343         fh->jpg_settings = settings;
2344
2345         /* tell the user what we actually did */
2346         fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2347         fmt->fmt.pix.height = settings.img_height * 2 /
2348                 (settings.TmpDcm * settings.VerDcm);
2349         if (settings.TmpDcm == 1)
2350                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2351                                 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2352         else
2353                 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2354                                 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2355         fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2356         fmt->fmt.pix.bytesperline = 0;
2357         fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
2358         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2359
2360         /* we hereby abuse this variable to show that
2361          * we're gonna do mjpeg capture */
2362         fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2363                 ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
2364 sfmtjpg_unlock_and_return:
2365         mutex_unlock(&zr->resource_lock);
2366         return res;
2367 }
2368
2369 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2370                                         struct v4l2_format *fmt)
2371 {
2372         struct zoran_fh *fh = __fh;
2373         struct zoran *zr = fh->zr;
2374         int i;
2375         int res = 0;
2376
2377         if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2378                 return zoran_s_fmt_vid_out(file, fh, fmt);
2379
2380         for (i = 0; i < NUM_FORMATS; i++)
2381                 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2382                         break;
2383         if (i == NUM_FORMATS) {
2384                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2385                         ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2386                 return -EINVAL;
2387         }
2388         mutex_lock(&zr->resource_lock);
2389         if (fh->jpg_buffers.allocated ||
2390                 (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
2391                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2392                                 ZR_DEVNAME(zr));
2393                 res = -EBUSY;
2394                 goto sfmtv4l_unlock_and_return;
2395         }
2396         if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2397                 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2398         if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2399                 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2400
2401         res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
2402                         fmt->fmt.pix.height, &zoran_formats[i]);
2403         if (res)
2404                 goto sfmtv4l_unlock_and_return;
2405
2406         /* tell the user the
2407          * results/missing stuff */
2408         fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2409         fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2410         fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2411         if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2412                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2413         else
2414                 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2415
2416         fh->map_mode = ZORAN_MAP_MODE_RAW;
2417 sfmtv4l_unlock_and_return:
2418         mutex_unlock(&zr->resource_lock);
2419         return res;
2420 }
2421
2422 static int zoran_g_fbuf(struct file *file, void *__fh,
2423                 struct v4l2_framebuffer *fb)
2424 {
2425         struct zoran_fh *fh = __fh;
2426         struct zoran *zr = fh->zr;
2427
2428         memset(fb, 0, sizeof(*fb));
2429         mutex_lock(&zr->resource_lock);
2430         fb->base = zr->buffer.base;
2431         fb->fmt.width = zr->buffer.width;
2432         fb->fmt.height = zr->buffer.height;
2433         if (zr->overlay_settings.format)
2434                 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2435         fb->fmt.bytesperline = zr->buffer.bytesperline;
2436         mutex_unlock(&zr->resource_lock);
2437         fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2438         fb->fmt.field = V4L2_FIELD_INTERLACED;
2439         fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2440         fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2441
2442         return 0;
2443 }
2444
2445 static int zoran_s_fbuf(struct file *file, void *__fh,
2446                 struct v4l2_framebuffer *fb)
2447 {
2448         struct zoran_fh *fh = __fh;
2449         struct zoran *zr = fh->zr;
2450         int i, res = 0;
2451         __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2452
2453         for (i = 0; i < NUM_FORMATS; i++)
2454                 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2455                         break;
2456         if (i == NUM_FORMATS) {
2457                 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2458                         ZR_DEVNAME(zr), fb->fmt.pixelformat,
2459                         (char *)&printformat);
2460                 return -EINVAL;
2461         }
2462
2463         mutex_lock(&zr->resource_lock);
2464         res = setup_fbuffer(file, fb->base, &zoran_formats[i],
2465                                 fb->fmt.width, fb->fmt.height,
2466                                 fb->fmt.bytesperline);
2467         mutex_unlock(&zr->resource_lock);
2468
2469         return res;
2470 }
2471
2472 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2473 {
2474         struct zoran_fh *fh = __fh;
2475         struct zoran *zr = fh->zr;
2476         int res;
2477
2478         mutex_lock(&zr->resource_lock);
2479         res = setup_overlay(file, on);
2480         mutex_unlock(&zr->resource_lock);
2481
2482         return res;
2483 }
2484
2485 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2486 {
2487         struct zoran_fh *fh = __fh;
2488         struct zoran *zr = fh->zr;
2489         int res = 0;
2490
2491         if (req->memory != V4L2_MEMORY_MMAP) {
2492                 dprintk(1,
2493                                 KERN_ERR
2494                                 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2495                                 ZR_DEVNAME(zr), req->memory);
2496                 return -EINVAL;
2497         }
2498
2499         mutex_lock(&zr->resource_lock);
2500
2501         if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
2502                 dprintk(1,
2503                                 KERN_ERR
2504                                 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
2505                                 ZR_DEVNAME(zr));
2506                 res = -EBUSY;
2507                 goto v4l2reqbuf_unlock_and_return;
2508         }
2509
2510         if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2511                         req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2512
2513                 /* control user input */
2514                 if (req->count < 2)
2515                         req->count = 2;
2516                 if (req->count > v4l_nbufs)
2517                         req->count = v4l_nbufs;
2518                 fh->v4l_buffers.num_buffers = req->count;
2519
2520                 if (v4l_fbuffer_alloc(file)) {
2521                         res = -ENOMEM;
2522                         goto v4l2reqbuf_unlock_and_return;
2523                 }
2524
2525                 /* The next mmap will map the V4L buffers */
2526                 fh->map_mode = ZORAN_MAP_MODE_RAW;
2527
2528         } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2529                         fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2530
2531                 /* we need to calculate size ourselves now */
2532                 if (req->count < 4)
2533                         req->count = 4;
2534                 if (req->count > jpg_nbufs)
2535                         req->count = jpg_nbufs;
2536                 fh->jpg_buffers.num_buffers = req->count;
2537                 fh->jpg_buffers.buffer_size =
2538                         zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2539
2540                 if (jpg_fbuffer_alloc(file)) {
2541                         res = -ENOMEM;
2542                         goto v4l2reqbuf_unlock_and_return;
2543                 }
2544
2545                 /* The next mmap will map the MJPEG buffers */
2546                 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2547                         fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2548                 else
2549                         fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
2550
2551         } else {
2552                 dprintk(1,
2553                                 KERN_ERR
2554                                 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2555                                 ZR_DEVNAME(zr), req->type);
2556                 res = -EINVAL;
2557                 goto v4l2reqbuf_unlock_and_return;
2558         }
2559 v4l2reqbuf_unlock_and_return:
2560         mutex_unlock(&zr->resource_lock);
2561
2562         return res;
2563 }
2564
2565 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2566 {
2567         struct zoran_fh *fh = __fh;
2568         struct zoran *zr = fh->zr;
2569         __u32 type = buf->type;
2570         int index = buf->index, res;
2571
2572         memset(buf, 0, sizeof(*buf));
2573         buf->type = type;
2574         buf->index = index;
2575
2576         mutex_lock(&zr->resource_lock);
2577         res = zoran_v4l2_buffer_status(file, buf, buf->index);
2578         mutex_unlock(&zr->resource_lock);
2579
2580         return res;
2581 }
2582
2583 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2584 {
2585         struct zoran_fh *fh = __fh;
2586         struct zoran *zr = fh->zr;
2587         int res = 0, codec_mode, buf_type;
2588
2589         mutex_lock(&zr->resource_lock);
2590
2591         switch (fh->map_mode) {
2592         case ZORAN_MAP_MODE_RAW:
2593                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2594                         dprintk(1, KERN_ERR
2595                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2596                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2597                         res = -EINVAL;
2598                         goto qbuf_unlock_and_return;
2599                 }
2600
2601                 res = zoran_v4l_queue_frame(file, buf->index);
2602                 if (res)
2603                         goto qbuf_unlock_and_return;
2604                 if (!zr->v4l_memgrab_active &&
2605                                 fh->v4l_buffers.active == ZORAN_LOCKED)
2606                         zr36057_set_memgrab(zr, 1);
2607                 break;
2608
2609         case ZORAN_MAP_MODE_JPG_REC:
2610         case ZORAN_MAP_MODE_JPG_PLAY:
2611                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2612                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2613                         codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2614                 } else {
2615                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2616                         codec_mode = BUZ_MODE_MOTION_COMPRESS;
2617                 }
2618
2619                 if (buf->type != buf_type) {
2620                         dprintk(1, KERN_ERR
2621                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2622                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2623                         res = -EINVAL;
2624                         goto qbuf_unlock_and_return;
2625                 }
2626
2627                 res = zoran_jpg_queue_frame(file, buf->index,
2628                                         codec_mode);
2629                 if (res != 0)
2630                         goto qbuf_unlock_and_return;
2631                 if (zr->codec_mode == BUZ_MODE_IDLE &&
2632                                 fh->jpg_buffers.active == ZORAN_LOCKED) {
2633                         zr36057_enable_jpg(zr, codec_mode);
2634                 }
2635                 break;
2636
2637         default:
2638                 dprintk(1, KERN_ERR
2639                         "%s: VIDIOC_QBUF - unsupported type %d\n",
2640                         ZR_DEVNAME(zr), buf->type);
2641                 res = -EINVAL;
2642                 break;
2643         }
2644 qbuf_unlock_and_return:
2645         mutex_unlock(&zr->resource_lock);
2646
2647         return res;
2648 }
2649
2650 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2651 {
2652         struct zoran_fh *fh = __fh;
2653         struct zoran *zr = fh->zr;
2654         int res = 0, buf_type, num = -1;        /* compiler borks here (?) */
2655
2656         mutex_lock(&zr->resource_lock);
2657
2658         switch (fh->map_mode) {
2659         case ZORAN_MAP_MODE_RAW:
2660                 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2661                         dprintk(1, KERN_ERR
2662                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2663                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2664                         res = -EINVAL;
2665                         goto dqbuf_unlock_and_return;
2666                 }
2667
2668                 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2669                 if (file->f_flags & O_NONBLOCK &&
2670                     zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2671                         res = -EAGAIN;
2672                         goto dqbuf_unlock_and_return;
2673                 }
2674                 res = v4l_sync(file, num);
2675                 if (res)
2676                         goto dqbuf_unlock_and_return;
2677                 zr->v4l_sync_tail++;
2678                 res = zoran_v4l2_buffer_status(file, buf, num);
2679                 break;
2680
2681         case ZORAN_MAP_MODE_JPG_REC:
2682         case ZORAN_MAP_MODE_JPG_PLAY:
2683         {
2684                 struct zoran_sync bs;
2685
2686                 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2687                         buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2688                 else
2689                         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2690
2691                 if (buf->type != buf_type) {
2692                         dprintk(1, KERN_ERR
2693                                 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2694                                 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2695                         res = -EINVAL;
2696                         goto dqbuf_unlock_and_return;
2697                 }
2698
2699                 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2700
2701                 if (file->f_flags & O_NONBLOCK &&
2702                     zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2703                         res = -EAGAIN;
2704                         goto dqbuf_unlock_and_return;
2705                 }
2706                 res = jpg_sync(file, &bs);
2707                 if (res)
2708                         goto dqbuf_unlock_and_return;
2709                 res = zoran_v4l2_buffer_status(file, buf, bs.frame);
2710                 break;
2711         }
2712
2713         default:
2714                 dprintk(1, KERN_ERR
2715                         "%s: VIDIOC_DQBUF - unsupported type %d\n",
2716                         ZR_DEVNAME(zr), buf->type);
2717                 res = -EINVAL;
2718                 break;
2719         }
2720 dqbuf_unlock_and_return:
2721         mutex_unlock(&zr->resource_lock);
2722
2723         return res;
2724 }
2725
2726 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2727 {
2728         struct zoran_fh *fh = __fh;
2729         struct zoran *zr = fh->zr;
2730         int res = 0;
2731
2732         mutex_lock(&zr->resource_lock);
2733
2734         switch (fh->map_mode) {
2735         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2736                 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2737                     fh->v4l_buffers.active != ZORAN_ACTIVE) {
2738                         res = -EBUSY;
2739                         goto strmon_unlock_and_return;
2740                 }
2741
2742                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
2743                 zr->v4l_settings = fh->v4l_settings;
2744
2745                 zr->v4l_sync_tail = zr->v4l_pend_tail;
2746                 if (!zr->v4l_memgrab_active &&
2747                     zr->v4l_pend_head != zr->v4l_pend_tail) {
2748                         zr36057_set_memgrab(zr, 1);
2749                 }
2750                 break;
2751
2752         case ZORAN_MAP_MODE_JPG_REC:
2753         case ZORAN_MAP_MODE_JPG_PLAY:
2754                 /* what is the codec mode right now? */
2755                 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2756                     fh->jpg_buffers.active != ZORAN_ACTIVE) {
2757                         res = -EBUSY;
2758                         goto strmon_unlock_and_return;
2759                 }
2760
2761                 zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
2762
2763                 if (zr->jpg_que_head != zr->jpg_que_tail) {
2764                         /* Start the jpeg codec when the first frame is queued  */
2765                         jpeg_start(zr);
2766                 }
2767                 break;
2768
2769         default:
2770                 dprintk(1,
2771                         KERN_ERR
2772                         "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2773                         ZR_DEVNAME(zr), fh->map_mode);
2774                 res = -EINVAL;
2775                 break;
2776         }
2777 strmon_unlock_and_return:
2778         mutex_unlock(&zr->resource_lock);
2779
2780         return res;
2781 }
2782
2783 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2784 {
2785         struct zoran_fh *fh = __fh;
2786         struct zoran *zr = fh->zr;
2787         int i, res = 0;
2788
2789         mutex_lock(&zr->resource_lock);
2790
2791         switch (fh->map_mode) {
2792         case ZORAN_MAP_MODE_RAW:        /* raw capture */
2793                 if (fh->v4l_buffers.active == ZORAN_FREE &&
2794                     zr->v4l_buffers.active != ZORAN_FREE) {
2795                         res = -EPERM;   /* stay off other's settings! */
2796                         goto strmoff_unlock_and_return;
2797                 }
2798                 if (zr->v4l_buffers.active == ZORAN_FREE)
2799                         goto strmoff_unlock_and_return;
2800
2801                 /* unload capture */
2802                 if (zr->v4l_memgrab_active) {
2803                         unsigned long flags;
2804
2805                         spin_lock_irqsave(&zr->spinlock, flags);
2806                         zr36057_set_memgrab(zr, 0);
2807                         spin_unlock_irqrestore(&zr->spinlock, flags);
2808                 }
2809
2810                 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
2811                         zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2812                 fh->v4l_buffers = zr->v4l_buffers;
2813
2814                 zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
2815
2816                 zr->v4l_grab_seq = 0;
2817                 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2818                 zr->v4l_sync_tail = 0;
2819
2820                 break;
2821
2822         case ZORAN_MAP_MODE_JPG_REC:
2823         case ZORAN_MAP_MODE_JPG_PLAY:
2824                 if (fh->jpg_buffers.active == ZORAN_FREE &&
2825                     zr->jpg_buffers.active != ZORAN_FREE) {
2826                         res = -EPERM;   /* stay off other's settings! */
2827                         goto strmoff_unlock_and_return;
2828                 }
2829                 if (zr->jpg_buffers.active == ZORAN_FREE)
2830                         goto strmoff_unlock_and_return;
2831
2832                 res = jpg_qbuf(file, -1,
2833                              (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2834                              BUZ_MODE_MOTION_COMPRESS :
2835                              BUZ_MODE_MOTION_DECOMPRESS);
2836                 if (res)
2837                         goto strmoff_unlock_and_return;
2838                 break;
2839         default:
2840                 dprintk(1, KERN_ERR
2841                         "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2842                         ZR_DEVNAME(zr), fh->map_mode);
2843                 res = -EINVAL;
2844                 break;
2845         }
2846 strmoff_unlock_and_return:
2847         mutex_unlock(&zr->resource_lock);
2848
2849         return res;
2850 }
2851
2852 static int zoran_queryctrl(struct file *file, void *__fh,
2853                                         struct v4l2_queryctrl *ctrl)
2854 {
2855         /* we only support hue/saturation/contrast/brightness */
2856         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2857             ctrl->id > V4L2_CID_HUE)
2858                 return -EINVAL;
2859         else {
2860                 int id = ctrl->id;
2861                 memset(ctrl, 0, sizeof(*ctrl));
2862                 ctrl->id = id;
2863         }
2864
2865         switch (ctrl->id) {
2866         case V4L2_CID_BRIGHTNESS:
2867                 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
2868                 break;
2869         case V4L2_CID_CONTRAST:
2870                 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
2871                 break;
2872         case V4L2_CID_SATURATION:
2873                 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
2874                 break;
2875         case V4L2_CID_HUE:
2876                 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
2877                 break;
2878         }
2879
2880         ctrl->minimum = 0;
2881         ctrl->maximum = 65535;
2882         ctrl->step = 1;
2883         ctrl->default_value = 32768;
2884         ctrl->type = V4L2_CTRL_TYPE_INTEGER;
2885         ctrl->flags = V4L2_CTRL_FLAG_SLIDER;
2886
2887         return 0;
2888 }
2889
2890 static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2891 {
2892         struct zoran_fh *fh = __fh;
2893         struct zoran *zr = fh->zr;
2894
2895         /* we only support hue/saturation/contrast/brightness */
2896         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2897             ctrl->id > V4L2_CID_HUE)
2898                 return -EINVAL;
2899
2900         mutex_lock(&zr->resource_lock);
2901         switch (ctrl->id) {
2902         case V4L2_CID_BRIGHTNESS:
2903                 ctrl->value = zr->brightness;
2904                 break;
2905         case V4L2_CID_CONTRAST:
2906                 ctrl->value = zr->contrast;
2907                 break;
2908         case V4L2_CID_SATURATION:
2909                 ctrl->value = zr->saturation;
2910                 break;
2911         case V4L2_CID_HUE:
2912                 ctrl->value = zr->hue;
2913                 break;
2914         }
2915         mutex_unlock(&zr->resource_lock);
2916
2917         return 0;
2918 }
2919
2920 static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2921 {
2922         struct zoran_fh *fh = __fh;
2923         struct zoran *zr = fh->zr;
2924         struct video_picture pict;
2925
2926         /* we only support hue/saturation/contrast/brightness */
2927         if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2928             ctrl->id > V4L2_CID_HUE)
2929                 return -EINVAL;
2930
2931         if (ctrl->value < 0 || ctrl->value > 65535) {
2932                 dprintk(1, KERN_ERR
2933                         "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
2934                         ZR_DEVNAME(zr), ctrl->value, ctrl->id);
2935                 return -EINVAL;
2936         }
2937
2938         mutex_lock(&zr->resource_lock);
2939         switch (ctrl->id) {
2940         case V4L2_CID_BRIGHTNESS:
2941                 zr->brightness = ctrl->value;
2942                 break;
2943         case V4L2_CID_CONTRAST:
2944                 zr->contrast = ctrl->value;
2945                 break;
2946         case V4L2_CID_SATURATION:
2947                 zr->saturation = ctrl->value;
2948                 break;
2949         case V4L2_CID_HUE:
2950                 zr->hue = ctrl->value;
2951                 break;
2952         }
2953         pict.brightness = zr->brightness;
2954         pict.contrast = zr->contrast;
2955         pict.colour = zr->saturation;
2956         pict.hue = zr->hue;
2957
2958         decoder_command(zr, DECODER_SET_PICTURE, &pict);
2959
2960         mutex_unlock(&zr->resource_lock);
2961
2962         return 0;
2963 }
2964
2965 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2966 {
2967         struct zoran_fh *fh = __fh;
2968         struct zoran *zr = fh->zr;
2969         int norm;
2970
2971         mutex_lock(&zr->resource_lock);
2972         norm = zr->norm;
2973         mutex_unlock(&zr->resource_lock);
2974
2975         switch (norm) {
2976         case VIDEO_MODE_PAL:
2977                 *std = V4L2_STD_PAL;
2978                 break;
2979         case VIDEO_MODE_NTSC:
2980                 *std = V4L2_STD_NTSC;
2981                 break;
2982         case VIDEO_MODE_SECAM:
2983                 *std = V4L2_STD_SECAM;
2984                 break;
2985         }
2986
2987         return 0;
2988 }
2989
2990 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2991 {
2992         struct zoran_fh *fh = __fh;
2993         struct zoran *zr = fh->zr;
2994         int norm = -1, res = 0;
2995
2996         if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
2997                 norm = VIDEO_MODE_PAL;
2998         else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
2999                 norm = VIDEO_MODE_NTSC;
3000         else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3001                 norm = VIDEO_MODE_SECAM;
3002         else if (*std == V4L2_STD_ALL)
3003                 norm = VIDEO_MODE_AUTO;
3004         else {
3005                 dprintk(1, KERN_ERR
3006                         "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3007                         ZR_DEVNAME(zr), (unsigned long long)*std);
3008                 return -EINVAL;
3009         }
3010
3011         mutex_lock(&zr->resource_lock);
3012         res = zoran_set_norm(zr, norm);
3013         if (res)
3014                 goto sstd_unlock_and_return;
3015
3016         res = wait_grab_pending(zr);
3017 sstd_unlock_and_return:
3018         mutex_unlock(&zr->resource_lock);
3019         return res;
3020 }
3021
3022 static int zoran_enum_input(struct file *file, void *__fh,
3023                                  struct v4l2_input *inp)
3024 {
3025         struct zoran_fh *fh = __fh;
3026         struct zoran *zr = fh->zr;
3027         int status;
3028
3029         if (inp->index < 0 || inp->index >= zr->card.inputs)
3030                 return -EINVAL;
3031         else {
3032                 int id = inp->index;
3033                 memset(inp, 0, sizeof(*inp));
3034                 inp->index = id;
3035         }
3036
3037         strncpy(inp->name, zr->card.input[inp->index].name,
3038                 sizeof(inp->name) - 1);
3039         inp->type = V4L2_INPUT_TYPE_CAMERA;
3040         inp->std = V4L2_STD_ALL;
3041
3042         /* Get status of video decoder */
3043         mutex_lock(&zr->resource_lock);
3044         decoder_command(zr, DECODER_GET_STATUS, &status);
3045         mutex_unlock(&zr->resource_lock);
3046
3047         if (!(status & DECODER_STATUS_GOOD)) {
3048                 inp->status |= V4L2_IN_ST_NO_POWER;
3049                 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3050         }
3051         if (!(status & DECODER_STATUS_COLOR))
3052                 inp->status |= V4L2_IN_ST_NO_COLOR;
3053
3054         return 0;
3055 }
3056
3057 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
3058 {
3059         struct zoran_fh *fh = __fh;
3060         struct zoran *zr = fh->zr;
3061
3062         mutex_lock(&zr->resource_lock);
3063         *input = zr->input;
3064         mutex_unlock(&zr->resource_lock);
3065
3066         return 0;
3067 }
3068
3069 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
3070 {
3071         struct zoran_fh *fh = __fh;
3072         struct zoran *zr = fh->zr;
3073         int res;
3074
3075         mutex_lock(&zr->resource_lock);
3076         res = zoran_set_input(zr, input);
3077         if (res)
3078                 goto sinput_unlock_and_return;
3079
3080         /* Make sure the changes come into effect */
3081         res = wait_grab_pending(zr);
3082 sinput_unlock_and_return:
3083         mutex_unlock(&zr->resource_lock);
3084         return res;
3085 }
3086
3087 static int zoran_enum_output(struct file *file, void *__fh,
3088                                   struct v4l2_output *outp)
3089 {
3090         if (outp->index != 0)
3091                 return -EINVAL;
3092
3093         memset(outp, 0, sizeof(*outp));
3094         outp->index = 0;
3095         outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3096         strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3097
3098         return 0;
3099 }
3100
3101 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
3102 {
3103         *output = 0;
3104
3105         return 0;
3106 }
3107
3108 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
3109 {
3110         if (output != 0)
3111                 return -EINVAL;
3112
3113         return 0;
3114 }
3115
3116 /* cropping (sub-frame capture) */
3117 static int zoran_cropcap(struct file *file, void *__fh,
3118                                         struct v4l2_cropcap *cropcap)
3119 {
3120         struct zoran_fh *fh = __fh;
3121         struct zoran *zr = fh->zr;
3122         int type = cropcap->type, res = 0;
3123
3124         memset(cropcap, 0, sizeof(*cropcap));
3125         cropcap->type = type;
3126
3127         mutex_lock(&zr->resource_lock);
3128
3129         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3130             (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3131              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3132                 dprintk(1, KERN_ERR
3133                         "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3134                         ZR_DEVNAME(zr));
3135                 res = -EINVAL;
3136                 goto cropcap_unlock_and_return;
3137         }
3138
3139         cropcap->bounds.top = cropcap->bounds.left = 0;
3140         cropcap->bounds.width = BUZ_MAX_WIDTH;
3141         cropcap->bounds.height = BUZ_MAX_HEIGHT;
3142         cropcap->defrect.top = cropcap->defrect.left = 0;
3143         cropcap->defrect.width = BUZ_MIN_WIDTH;
3144         cropcap->defrect.height = BUZ_MIN_HEIGHT;
3145 cropcap_unlock_and_return:
3146         mutex_unlock(&zr->resource_lock);
3147         return res;
3148 }
3149
3150 static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3151 {
3152         struct zoran_fh *fh = __fh;
3153         struct zoran *zr = fh->zr;
3154         int type = crop->type, res = 0;
3155
3156         memset(crop, 0, sizeof(*crop));
3157         crop->type = type;
3158
3159         mutex_lock(&zr->resource_lock);
3160
3161         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3162             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3163              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3164                 dprintk(1,
3165                         KERN_ERR
3166                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3167                         ZR_DEVNAME(zr));
3168                 res = -EINVAL;
3169                 goto gcrop_unlock_and_return;
3170         }
3171
3172         crop->c.top = fh->jpg_settings.img_y;
3173         crop->c.left = fh->jpg_settings.img_x;
3174         crop->c.width = fh->jpg_settings.img_width;
3175         crop->c.height = fh->jpg_settings.img_height;
3176
3177 gcrop_unlock_and_return:
3178         mutex_unlock(&zr->resource_lock);
3179
3180         return res;
3181 }
3182
3183 static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
3184 {
3185         struct zoran_fh *fh = __fh;
3186         struct zoran *zr = fh->zr;
3187         int res = 0;
3188         struct zoran_jpg_settings settings;
3189
3190         settings = fh->jpg_settings;
3191
3192         mutex_lock(&zr->resource_lock);
3193
3194         if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3195                 dprintk(1, KERN_ERR
3196                         "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3197                         ZR_DEVNAME(zr));
3198                 res = -EBUSY;
3199                 goto scrop_unlock_and_return;
3200         }
3201
3202         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3203             (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3204              fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3205                 dprintk(1, KERN_ERR
3206                         "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3207                         ZR_DEVNAME(zr));
3208                 res = -EINVAL;
3209                 goto scrop_unlock_and_return;
3210         }
3211
3212         /* move into a form that we understand */
3213         settings.img_x = crop->c.left;
3214         settings.img_y = crop->c.top;
3215         settings.img_width = crop->c.width;
3216         settings.img_height = crop->c.height;
3217
3218         /* check validity */
3219         res = zoran_check_jpg_settings(zr, &settings);
3220         if (res)
3221                 goto scrop_unlock_and_return;
3222
3223         /* accept */
3224         fh->jpg_settings = settings;
3225
3226 scrop_unlock_and_return:
3227         mutex_unlock(&zr->resource_lock);
3228         return res;
3229 }
3230
3231 static int zoran_g_jpegcomp(struct file *file, void *__fh,
3232                                         struct v4l2_jpegcompression *params)
3233 {
3234         struct zoran_fh *fh = __fh;
3235         struct zoran *zr = fh->zr;
3236         memset(params, 0, sizeof(*params));
3237
3238         mutex_lock(&zr->resource_lock);
3239
3240         params->quality = fh->jpg_settings.jpg_comp.quality;
3241         params->APPn = fh->jpg_settings.jpg_comp.APPn;
3242         memcpy(params->APP_data,
3243                fh->jpg_settings.jpg_comp.APP_data,
3244                fh->jpg_settings.jpg_comp.APP_len);
3245         params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
3246         memcpy(params->COM_data,
3247                fh->jpg_settings.jpg_comp.COM_data,
3248                fh->jpg_settings.jpg_comp.COM_len);
3249         params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
3250         params->jpeg_markers =
3251             fh->jpg_settings.jpg_comp.jpeg_markers;
3252
3253         mutex_unlock(&zr->resource_lock);
3254
3255         return 0;
3256 }
3257
3258 static int zoran_s_jpegcomp(struct file *file, void *__fh,
3259                                         struct v4l2_jpegcompression *params)
3260 {
3261         struct zoran_fh *fh = __fh;
3262         struct zoran *zr = fh->zr;
3263         int res = 0;
3264         struct zoran_jpg_settings settings;
3265
3266         settings = fh->jpg_settings;
3267
3268         settings.jpg_comp = *params;
3269
3270         mutex_lock(&zr->resource_lock);
3271
3272         if (fh->v4l_buffers.active != ZORAN_FREE ||
3273             fh->jpg_buffers.active != ZORAN_FREE) {
3274                 dprintk(1, KERN_WARNING
3275                         "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3276                         ZR_DEVNAME(zr));
3277                 res = -EBUSY;
3278                 goto sjpegc_unlock_and_return;
3279         }
3280
3281         res = zoran_check_jpg_settings(zr, &settings);
3282         if (res)
3283                 goto sjpegc_unlock_and_return;
3284         if (!fh->jpg_buffers.allocated)
3285                 fh->jpg_buffers.buffer_size =
3286                     zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3287         fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3288 sjpegc_unlock_and_return:
3289         mutex_unlock(&zr->resource_lock);
3290
3291         return 0;
3292 }
3293
3294 static unsigned int
3295 zoran_poll (struct file *file,
3296             poll_table  *wait)
3297 {
3298         struct zoran_fh *fh = file->private_data;
3299         struct zoran *zr = fh->zr;
3300         int res = 0, frame;
3301         unsigned long flags;
3302
3303         /* we should check whether buffers are ready to be synced on
3304          * (w/o waits - O_NONBLOCK) here
3305          * if ready for read (sync), return POLLIN|POLLRDNORM,
3306          * if ready for write (sync), return POLLOUT|POLLWRNORM,
3307          * if error, return POLLERR,
3308          * if no buffers queued or so, return POLLNVAL
3309          */
3310
3311         mutex_lock(&zr->resource_lock);
3312
3313         switch (fh->map_mode) {
3314         case ZORAN_MAP_MODE_RAW:
3315                 poll_wait(file, &zr->v4l_capq, wait);
3316                 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3317
3318                 spin_lock_irqsave(&zr->spinlock, flags);
3319                 dprintk(3,
3320                         KERN_DEBUG
3321                         "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3322                         ZR_DEVNAME(zr), __func__,
3323                         "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
3324                         "UPMD"[zr->v4l_buffers.buffer[frame].state],
3325                         zr->v4l_pend_tail, zr->v4l_pend_head);
3326                 /* Process is the one capturing? */
3327                 if (fh->v4l_buffers.active != ZORAN_FREE &&
3328                     /* Buffer ready to DQBUF? */
3329                     zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
3330                         res = POLLIN | POLLRDNORM;
3331                 spin_unlock_irqrestore(&zr->spinlock, flags);
3332
3333                 break;
3334
3335         case ZORAN_MAP_MODE_JPG_REC:
3336         case ZORAN_MAP_MODE_JPG_PLAY:
3337                 poll_wait(file, &zr->jpg_capq, wait);
3338                 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3339
3340                 spin_lock_irqsave(&zr->spinlock, flags);
3341                 dprintk(3,
3342                         KERN_DEBUG
3343                         "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3344                         ZR_DEVNAME(zr), __func__,
3345                         "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
3346                         "UPMD"[zr->jpg_buffers.buffer[frame].state],
3347                         zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3348                 if (fh->jpg_buffers.active != ZORAN_FREE &&
3349                     zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
3350                         if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3351                                 res = POLLIN | POLLRDNORM;
3352                         else
3353                                 res = POLLOUT | POLLWRNORM;
3354                 }
3355                 spin_unlock_irqrestore(&zr->spinlock, flags);
3356
3357                 break;
3358
3359         default:
3360                 dprintk(1,
3361                         KERN_ERR
3362                         "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3363                         ZR_DEVNAME(zr), fh->map_mode);
3364                 res = POLLNVAL;
3365         }
3366
3367         mutex_unlock(&zr->resource_lock);
3368
3369         return res;
3370 }
3371
3372
3373 /*
3374  * This maps the buffers to user space.
3375  *
3376  * Depending on the state of fh->map_mode
3377  * the V4L or the MJPEG buffers are mapped
3378  * per buffer or all together
3379  *
3380  * Note that we need to connect to some
3381  * unmap signal event to unmap the de-allocate
3382  * the buffer accordingly (zoran_vm_close())
3383  */
3384
3385 static void
3386 zoran_vm_open (struct vm_area_struct *vma)
3387 {
3388         struct zoran_mapping *map = vma->vm_private_data;
3389
3390         map->count++;
3391 }
3392
3393 static void
3394 zoran_vm_close (struct vm_area_struct *vma)
3395 {
3396         struct zoran_mapping *map = vma->vm_private_data;
3397         struct file *file = map->file;
3398         struct zoran_fh *fh = file->private_data;
3399         struct zoran *zr = fh->zr;
3400         int i;
3401
3402         map->count--;
3403         if (map->count == 0) {
3404                 switch (fh->map_mode) {
3405                 case ZORAN_MAP_MODE_JPG_REC:
3406                 case ZORAN_MAP_MODE_JPG_PLAY:
3407
3408                         dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
3409                                 ZR_DEVNAME(zr));
3410
3411                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
3412                                 if (fh->jpg_buffers.buffer[i].map == map) {
3413                                         fh->jpg_buffers.buffer[i].map =
3414                                             NULL;
3415                                 }
3416                         }
3417                         kfree(map);
3418
3419                         for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
3420                                 if (fh->jpg_buffers.buffer[i].map)
3421                                         break;
3422                         if (i == fh->jpg_buffers.num_buffers) {
3423                                 mutex_lock(&zr->resource_lock);
3424
3425                                 if (fh->jpg_buffers.active != ZORAN_FREE) {
3426                                         jpg_qbuf(file, -1, zr->codec_mode);
3427                                         zr->jpg_buffers.allocated = 0;
3428                                         zr->jpg_buffers.active =
3429                                             fh->jpg_buffers.active =
3430                                             ZORAN_FREE;
3431                                 }
3432                                 jpg_fbuffer_free(file);
3433                                 mutex_unlock(&zr->resource_lock);
3434                         }
3435
3436                         break;
3437
3438                 case ZORAN_MAP_MODE_RAW:
3439
3440                         dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
3441                                 ZR_DEVNAME(zr));
3442
3443                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
3444                                 if (fh->v4l_buffers.buffer[i].map == map) {
3445                                         /* unqueue/unmap */
3446                                         fh->v4l_buffers.buffer[i].map =
3447                                             NULL;
3448                                 }
3449                         }
3450                         kfree(map);
3451
3452                         for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3453                                 if (fh->v4l_buffers.buffer[i].map)
3454                                         break;
3455                         if (i == fh->v4l_buffers.num_buffers) {
3456                                 mutex_lock(&zr->resource_lock);
3457
3458                                 if (fh->v4l_buffers.active != ZORAN_FREE) {
3459                                         unsigned long flags;
3460
3461                                         spin_lock_irqsave(&zr->spinlock, flags);
3462                                         zr36057_set_memgrab(zr, 0);
3463                                         zr->v4l_buffers.allocated = 0;
3464                                         zr->v4l_buffers.active =
3465                                             fh->v4l_buffers.active =
3466                                             ZORAN_FREE;
3467                                         spin_unlock_irqrestore(&zr->spinlock, flags);
3468                                 }
3469                                 v4l_fbuffer_free(file);
3470                                 mutex_unlock(&zr->resource_lock);
3471                         }
3472
3473                         break;
3474
3475                 default:
3476                         printk(KERN_ERR
3477                                "%s: munmap() - internal error - unknown map mode %d\n",
3478                                ZR_DEVNAME(zr), fh->map_mode);
3479                         break;
3480
3481                 }
3482         }
3483 }
3484
3485 static struct vm_operations_struct zoran_vm_ops = {
3486         .open = zoran_vm_open,
3487         .close = zoran_vm_close,
3488 };
3489
3490 static int
3491 zoran_mmap (struct file           *file,
3492             struct vm_area_struct *vma)
3493 {
3494         struct zoran_fh *fh = file->private_data;
3495         struct zoran *zr = fh->zr;
3496         unsigned long size = (vma->vm_end - vma->vm_start);
3497         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3498         int i, j;
3499         unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3500         int first, last;
3501         struct zoran_mapping *map;
3502         int res = 0;
3503
3504         dprintk(3,
3505                 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3506                 ZR_DEVNAME(zr),
3507                 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
3508                 vma->vm_start, vma->vm_end, size);
3509
3510         if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3511             !(vma->vm_flags & VM_WRITE)) {
3512                 dprintk(1,
3513                         KERN_ERR
3514                         "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3515                         ZR_DEVNAME(zr));
3516                 return -EINVAL;
3517         }
3518
3519         switch (fh->map_mode) {
3520
3521         case ZORAN_MAP_MODE_JPG_REC:
3522         case ZORAN_MAP_MODE_JPG_PLAY:
3523
3524                 /* lock */
3525                 mutex_lock(&zr->resource_lock);
3526
3527                 /* Map the MJPEG buffers */
3528                 if (!fh->jpg_buffers.allocated) {
3529                         dprintk(1,
3530                                 KERN_ERR
3531                                 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3532                                 ZR_DEVNAME(zr));
3533                         res = -ENOMEM;
3534                         goto jpg_mmap_unlock_and_return;
3535                 }
3536
3537                 first = offset / fh->jpg_buffers.buffer_size;
3538                 last = first - 1 + size / fh->jpg_buffers.buffer_size;
3539                 if (offset % fh->jpg_buffers.buffer_size != 0 ||
3540                     size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
3541                     last < 0 || first >= fh->jpg_buffers.num_buffers ||
3542                     last >= fh->jpg_buffers.num_buffers) {
3543                         dprintk(1,
3544                                 KERN_ERR
3545                                 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3546                                 ZR_DEVNAME(zr), offset, size,
3547                                 fh->jpg_buffers.buffer_size,
3548                                 fh->jpg_buffers.num_buffers);
3549                         res = -EINVAL;
3550                         goto jpg_mmap_unlock_and_return;
3551                 }
3552                 for (i = first; i <= last; i++) {
3553                         if (fh->jpg_buffers.buffer[i].map) {
3554                                 dprintk(1,
3555                                         KERN_ERR
3556                                         "%s: mmap(MJPEG) - buffer %d already mapped\n",
3557                                         ZR_DEVNAME(zr), i);
3558                                 res = -EBUSY;
3559                                 goto jpg_mmap_unlock_and_return;
3560                         }
3561                 }
3562
3563                 /* map these buffers (v4l_buffers[i]) */
3564                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3565                 if (!map) {
3566                         res = -ENOMEM;
3567                         goto jpg_mmap_unlock_and_return;
3568                 }
3569                 map->file = file;
3570                 map->count = 1;
3571
3572                 vma->vm_ops = &zoran_vm_ops;
3573                 vma->vm_flags |= VM_DONTEXPAND;
3574                 vma->vm_private_data = map;
3575
3576                 for (i = first; i <= last; i++) {
3577                         for (j = 0;
3578                              j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
3579                              j++) {
3580                                 fraglen =
3581                                     (le32_to_cpu(fh->jpg_buffers.buffer[i].
3582                                      frag_tab[2 * j + 1]) & ~1) << 1;
3583                                 todo = size;
3584                                 if (todo > fraglen)
3585                                         todo = fraglen;
3586                                 pos =
3587                                     le32_to_cpu(fh->jpg_buffers.
3588                                     buffer[i].frag_tab[2 * j]);
3589                                 /* should just be pos on i386 */
3590                                 page = virt_to_phys(bus_to_virt(pos))
3591                                                                 >> PAGE_SHIFT;
3592                                 if (remap_pfn_range(vma, start, page,
3593                                                         todo, PAGE_SHARED)) {
3594                                         dprintk(1,
3595                                                 KERN_ERR
3596                                                 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3597                                                 ZR_DEVNAME(zr));
3598                                         res = -EAGAIN;
3599                                         goto jpg_mmap_unlock_and_return;
3600                                 }
3601                                 size -= todo;
3602                                 start += todo;
3603                                 if (size == 0)
3604                                         break;
3605                                 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
3606                                     frag_tab[2 * j + 1]) & 1)
3607                                         break;  /* was last fragment */
3608                         }
3609                         fh->jpg_buffers.buffer[i].map = map;
3610                         if (size == 0)
3611                                 break;
3612
3613                 }
3614         jpg_mmap_unlock_and_return:
3615                 mutex_unlock(&zr->resource_lock);
3616
3617                 break;
3618
3619         case ZORAN_MAP_MODE_RAW:
3620
3621                 mutex_lock(&zr->resource_lock);
3622
3623                 /* Map the V4L buffers */
3624                 if (!fh->v4l_buffers.allocated) {
3625                         dprintk(1,
3626                                 KERN_ERR
3627                                 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
3628                                 ZR_DEVNAME(zr));
3629                         res = -ENOMEM;
3630                         goto v4l_mmap_unlock_and_return;
3631                 }
3632
3633                 first = offset / fh->v4l_buffers.buffer_size;
3634                 last = first - 1 + size / fh->v4l_buffers.buffer_size;
3635                 if (offset % fh->v4l_buffers.buffer_size != 0 ||
3636                     size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
3637                     last < 0 || first >= fh->v4l_buffers.num_buffers ||
3638                     last >= fh->v4l_buffers.buffer_size) {
3639                         dprintk(1,
3640                                 KERN_ERR
3641                                 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3642                                 ZR_DEVNAME(zr), offset, size,
3643                                 fh->v4l_buffers.buffer_size,
3644                                 fh->v4l_buffers.num_buffers);
3645                         res = -EINVAL;
3646                         goto v4l_mmap_unlock_and_return;
3647                 }
3648                 for (i = first; i <= last; i++) {
3649                         if (fh->v4l_buffers.buffer[i].map) {
3650                                 dprintk(1,
3651                                         KERN_ERR
3652                                         "%s: mmap(V4L) - buffer %d already mapped\n",
3653                                         ZR_DEVNAME(zr), i);
3654                                 res = -EBUSY;
3655                                 goto v4l_mmap_unlock_and_return;
3656                         }
3657                 }
3658
3659                 /* map these buffers (v4l_buffers[i]) */
3660                 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3661                 if (!map) {
3662                         res = -ENOMEM;
3663                         goto v4l_mmap_unlock_and_return;
3664                 }
3665                 map->file = file;
3666                 map->count = 1;
3667
3668                 vma->vm_ops = &zoran_vm_ops;
3669                 vma->vm_flags |= VM_DONTEXPAND;
3670                 vma->vm_private_data = map;
3671
3672                 for (i = first; i <= last; i++) {
3673                         todo = size;
3674                         if (todo > fh->v4l_buffers.buffer_size)
3675                                 todo = fh->v4l_buffers.buffer_size;
3676                         page = fh->v4l_buffers.buffer[i].fbuffer_phys;
3677                         if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3678                                                         todo, PAGE_SHARED)) {
3679                                 dprintk(1,
3680                                         KERN_ERR
3681                                         "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
3682                                         ZR_DEVNAME(zr));
3683                                 res = -EAGAIN;
3684                                 goto v4l_mmap_unlock_and_return;
3685                         }
3686                         size -= todo;
3687                         start += todo;
3688                         fh->v4l_buffers.buffer[i].map = map;
3689                         if (size == 0)
3690                                 break;
3691                 }
3692         v4l_mmap_unlock_and_return:
3693                 mutex_unlock(&zr->resource_lock);
3694
3695                 break;
3696
3697         default:
3698                 dprintk(1,
3699                         KERN_ERR
3700                         "%s: zoran_mmap() - internal error - unknown map mode %d\n",
3701                         ZR_DEVNAME(zr), fh->map_mode);
3702                 break;
3703         }
3704
3705         return 0;
3706 }
3707
3708 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3709         .vidioc_querycap                    = zoran_querycap,
3710         .vidioc_cropcap                     = zoran_cropcap,
3711         .vidioc_s_crop                      = zoran_s_crop,
3712         .vidioc_g_crop                      = zoran_g_crop,
3713         .vidioc_enum_input                  = zoran_enum_input,
3714         .vidioc_g_input                     = zoran_g_input,
3715         .vidioc_s_input                     = zoran_s_input,
3716         .vidioc_enum_output                 = zoran_enum_output,
3717         .vidioc_g_output                    = zoran_g_output,
3718         .vidioc_s_output                    = zoran_s_output,
3719         .vidioc_g_fbuf                      = zoran_g_fbuf,
3720         .vidioc_s_fbuf                      = zoran_s_fbuf,
3721         .vidioc_g_std                       = zoran_g_std,
3722         .vidioc_s_std                       = zoran_s_std,
3723         .vidioc_g_jpegcomp                  = zoran_g_jpegcomp,
3724         .vidioc_s_jpegcomp                  = zoran_s_jpegcomp,
3725         .vidioc_overlay                     = zoran_overlay,
3726         .vidioc_reqbufs                     = zoran_reqbufs,
3727         .vidioc_querybuf                    = zoran_querybuf,
3728         .vidioc_qbuf                        = zoran_qbuf,
3729         .vidioc_dqbuf                       = zoran_dqbuf,
3730         .vidioc_streamon                    = zoran_streamon,
3731         .vidioc_streamoff                   = zoran_streamoff,
3732         .vidioc_enum_fmt_vid_cap            = zoran_enum_fmt_vid_cap,
3733         .vidioc_enum_fmt_vid_out            = zoran_enum_fmt_vid_out,
3734         .vidioc_enum_fmt_vid_overlay        = zoran_enum_fmt_vid_overlay,
3735         .vidioc_g_fmt_vid_cap               = zoran_g_fmt_vid_cap,
3736         .vidioc_g_fmt_vid_out               = zoran_g_fmt_vid_out,
3737         .vidioc_g_fmt_vid_overlay           = zoran_g_fmt_vid_overlay,
3738         .vidioc_s_fmt_vid_cap               = zoran_s_fmt_vid_cap,
3739         .vidioc_s_fmt_vid_out               = zoran_s_fmt_vid_out,
3740         .vidioc_s_fmt_vid_overlay           = zoran_s_fmt_vid_overlay,
3741         .vidioc_try_fmt_vid_cap             = zoran_try_fmt_vid_cap,
3742         .vidioc_try_fmt_vid_out             = zoran_try_fmt_vid_out,
3743         .vidioc_try_fmt_vid_overlay         = zoran_try_fmt_vid_overlay,
3744         .vidioc_queryctrl                   = zoran_queryctrl,
3745         .vidioc_s_ctrl                      = zoran_s_ctrl,
3746         .vidioc_g_ctrl                      = zoran_g_ctrl,
3747 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3748         .vidioc_default                     = zoran_default,
3749         .vidiocgmbuf                        = zoran_vidiocgmbuf,
3750 #endif
3751 };
3752
3753 static const struct v4l2_file_operations zoran_fops = {
3754         .owner = THIS_MODULE,
3755         .open = zoran_open,
3756         .release = zoran_close,
3757         .ioctl = video_ioctl2,
3758         .read = zoran_read,
3759         .write = zoran_write,
3760         .mmap = zoran_mmap,
3761         .poll = zoran_poll,
3762 };
3763
3764 struct video_device zoran_template __devinitdata = {
3765         .name = ZORAN_NAME,
3766         .fops = &zoran_fops,
3767         .ioctl_ops = &zoran_ioctl_ops,
3768         .release = &zoran_vdev_release,
3769         .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3770         .minor = -1
3771 };
3772