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