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