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