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