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