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