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