V4L/DVB (4070): Zoran strncpy() fix
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / media / video / zoran_driver.c
blob9711f6248ef75a0ba5001dabec60a58cf8207e86
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.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
16 * Based on
18 * Miro DC10 driver
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
24 * buz.0.0.3
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
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)
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.
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.
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.
47 #include <linux/config.h>
48 #include <linux/version.h>
49 #include <linux/init.h>
50 #include <linux/module.h>
51 #include <linux/delay.h>
52 #include <linux/slab.h>
53 #include <linux/pci.h>
54 #include <linux/vmalloc.h>
55 #include <linux/wait.h>
56 #include <linux/byteorder/generic.h>
58 #include <linux/interrupt.h>
59 #include <linux/i2c.h>
60 #include <linux/i2c-algo-bit.h>
62 #include <linux/spinlock.h>
63 #define MAP_NR(x) virt_to_page(x)
64 #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
65 #define ZORAN_VID_TYPE ( \
66 VID_TYPE_CAPTURE | \
67 VID_TYPE_OVERLAY | \
68 VID_TYPE_CLIPPING | \
69 VID_TYPE_FRAMERAM | \
70 VID_TYPE_SCALES | \
71 VID_TYPE_MJPEG_DECODER | \
72 VID_TYPE_MJPEG_ENCODER \
75 #include <linux/videodev.h>
76 #include <media/v4l2-common.h>
77 #include "videocodec.h"
79 #include <asm/io.h>
80 #include <asm/uaccess.h>
81 #include <linux/proc_fs.h>
83 #include <linux/video_decoder.h>
84 #include <linux/video_encoder.h>
85 #include <linux/mutex.h>
86 #include "zoran.h"
87 #include "zoran_device.h"
88 #include "zoran_card.h"
90 #ifdef HAVE_V4L2
91 /* we declare some card type definitions here, they mean
92 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
93 #define ZORAN_V4L2_VID_FLAGS ( \
94 V4L2_CAP_STREAMING |\
95 V4L2_CAP_VIDEO_CAPTURE |\
96 V4L2_CAP_VIDEO_OUTPUT |\
97 V4L2_CAP_VIDEO_OVERLAY \
99 #endif
101 #include <asm/byteorder.h>
103 const struct zoran_format zoran_formats[] = {
105 .name = "15-bit RGB",
106 .palette = VIDEO_PALETTE_RGB555,
107 #ifdef HAVE_V4L2
108 #ifdef __LITTLE_ENDIAN
109 .fourcc = V4L2_PIX_FMT_RGB555,
110 #else
111 .fourcc = V4L2_PIX_FMT_RGB555X,
112 #endif
113 .colorspace = V4L2_COLORSPACE_SRGB,
114 #endif
115 .depth = 15,
116 .flags = ZORAN_FORMAT_CAPTURE |
117 ZORAN_FORMAT_OVERLAY,
118 }, {
119 .name = "16-bit RGB",
120 .palette = VIDEO_PALETTE_RGB565,
121 #ifdef HAVE_V4L2
122 #ifdef __LITTLE_ENDIAN
123 .fourcc = V4L2_PIX_FMT_RGB565,
124 #else
125 .fourcc = V4L2_PIX_FMT_RGB565X,
126 #endif
127 .colorspace = V4L2_COLORSPACE_SRGB,
128 #endif
129 .depth = 16,
130 .flags = ZORAN_FORMAT_CAPTURE |
131 ZORAN_FORMAT_OVERLAY,
132 }, {
133 .name = "24-bit RGB",
134 .palette = VIDEO_PALETTE_RGB24,
135 #ifdef HAVE_V4L2
136 #ifdef __LITTLE_ENDIAN
137 .fourcc = V4L2_PIX_FMT_BGR24,
138 #else
139 .fourcc = V4L2_PIX_FMT_RGB24,
140 #endif
141 .colorspace = V4L2_COLORSPACE_SRGB,
142 #endif
143 .depth = 24,
144 .flags = ZORAN_FORMAT_CAPTURE |
145 ZORAN_FORMAT_OVERLAY,
146 }, {
147 .name = "32-bit RGB",
148 .palette = VIDEO_PALETTE_RGB32,
149 #ifdef HAVE_V4L2
150 #ifdef __LITTLE_ENDIAN
151 .fourcc = V4L2_PIX_FMT_BGR32,
152 #else
153 .fourcc = V4L2_PIX_FMT_RGB32,
154 #endif
155 .colorspace = V4L2_COLORSPACE_SRGB,
156 #endif
157 .depth = 32,
158 .flags = ZORAN_FORMAT_CAPTURE |
159 ZORAN_FORMAT_OVERLAY,
160 }, {
161 .name = "4:2:2, packed, YUYV",
162 .palette = VIDEO_PALETTE_YUV422,
163 #ifdef HAVE_V4L2
164 .fourcc = V4L2_PIX_FMT_YUYV,
165 .colorspace = V4L2_COLORSPACE_SMPTE170M,
166 #endif
167 .depth = 16,
168 .flags = ZORAN_FORMAT_CAPTURE |
169 ZORAN_FORMAT_OVERLAY,
170 }, {
171 .name = "Hardware-encoded Motion-JPEG",
172 .palette = -1,
173 #ifdef HAVE_V4L2
174 .fourcc = V4L2_PIX_FMT_MJPEG,
175 .colorspace = V4L2_COLORSPACE_SMPTE170M,
176 #endif
177 .depth = 0,
178 .flags = ZORAN_FORMAT_CAPTURE |
179 ZORAN_FORMAT_PLAYBACK |
180 ZORAN_FORMAT_COMPRESSED,
183 static const int zoran_num_formats =
184 (sizeof(zoran_formats) / sizeof(struct zoran_format));
186 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
187 #if !defined(CONFIG_BIGPHYS_AREA)
188 //#undef CONFIG_BIGPHYS_AREA
189 #define BUZ_USE_HIMEM
190 #endif
192 #if defined(CONFIG_BIGPHYS_AREA)
193 # include <linux/bigphysarea.h>
194 #endif
196 extern int *zr_debug;
198 #define dprintk(num, format, args...) \
199 do { \
200 if (*zr_debug >= num) \
201 printk(format, ##args); \
202 } while (0)
204 extern int v4l_nbufs;
205 extern int v4l_bufsize;
206 extern int jpg_nbufs;
207 extern int jpg_bufsize;
208 extern int pass_through;
210 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
211 module_param(lock_norm, int, 0);
212 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
214 #ifdef HAVE_V4L2
215 /* small helper function for calculating buffersizes for v4l2
216 * we calculate the nearest higher power-of-two, which
217 * will be the recommended buffersize */
218 static __u32
219 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
221 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
222 __u32 num = (1024 * 512) / (div);
223 __u32 result = 2;
225 num--;
226 while (num) {
227 num >>= 1;
228 result <<= 1;
231 if (result > jpg_bufsize)
232 return jpg_bufsize;
233 if (result < 8192)
234 return 8192;
235 return result;
237 #endif
239 /* forward references */
240 static void v4l_fbuffer_free(struct file *file);
241 static void jpg_fbuffer_free(struct file *file);
244 * Allocate the V4L grab buffers
246 * These have to be pysically contiguous.
247 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
248 * else we try to allocate them with bigphysarea_alloc_pages
249 * if the bigphysarea patch is present in the kernel,
250 * else we try to use high memory (if the user has bootet
251 * Linux with the necessary memory left over).
254 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
255 static unsigned long
256 get_high_mem (unsigned long size)
259 * Check if there is usable memory at the end of Linux memory
260 * of at least size. Return the physical address of this memory,
261 * return 0 on failure.
263 * The idea is from Alexandro Rubini's book "Linux device drivers".
264 * The driver from him which is downloadable from O'Reilly's
265 * web site misses the "virt_to_phys(high_memory)" part
266 * (and therefore doesn't work at all - at least with 2.2.x kernels).
268 * It should be unnecessary to mention that THIS IS DANGEROUS,
269 * if more than one driver at a time has the idea to use this memory!!!!
272 volatile unsigned char __iomem *mem;
273 unsigned char c;
274 unsigned long hi_mem_ph;
275 unsigned long i;
277 /* Map the high memory to user space */
279 hi_mem_ph = virt_to_phys(high_memory);
281 mem = ioremap(hi_mem_ph, size);
282 if (!mem) {
283 dprintk(1,
284 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
285 ZORAN_NAME);
286 return 0;
289 for (i = 0; i < size; i++) {
290 /* Check if it is memory */
291 c = i & 0xff;
292 writeb(c, mem + i);
293 if (readb(mem + i) != c)
294 break;
295 c = 255 - c;
296 writeb(c, mem + i);
297 if (readb(mem + i) != c)
298 break;
299 writeb(0, mem + i); /* zero out memory */
301 /* give the kernel air to breath */
302 if ((i & 0x3ffff) == 0x3ffff)
303 schedule();
306 iounmap(mem);
308 if (i != size) {
309 dprintk(1,
310 KERN_ERR
311 "%s: get_high_mem() - requested %lu, avail %lu\n",
312 ZORAN_NAME, size, i);
313 return 0;
316 return hi_mem_ph;
318 #endif
320 static int
321 v4l_fbuffer_alloc (struct file *file)
323 struct zoran_fh *fh = file->private_data;
324 struct zoran *zr = fh->zr;
325 int i, off;
326 unsigned char *mem;
327 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
328 unsigned long pmem = 0;
329 #endif
331 /* we might have old buffers lying around... */
332 if (fh->v4l_buffers.ready_to_be_freed) {
333 v4l_fbuffer_free(file);
336 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
337 if (fh->v4l_buffers.buffer[i].fbuffer)
338 dprintk(2,
339 KERN_WARNING
340 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
341 ZR_DEVNAME(zr), i);
343 //udelay(20);
344 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
345 /* Use kmalloc */
347 mem =
348 (unsigned char *) kmalloc(fh->v4l_buffers.
349 buffer_size,
350 GFP_KERNEL);
351 if (mem == 0) {
352 dprintk(1,
353 KERN_ERR
354 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
355 ZR_DEVNAME(zr), i);
356 v4l_fbuffer_free(file);
357 return -ENOBUFS;
359 fh->v4l_buffers.buffer[i].fbuffer = mem;
360 fh->v4l_buffers.buffer[i].fbuffer_phys =
361 virt_to_phys(mem);
362 fh->v4l_buffers.buffer[i].fbuffer_bus =
363 virt_to_bus(mem);
364 for (off = 0; off < fh->v4l_buffers.buffer_size;
365 off += PAGE_SIZE)
366 SetPageReserved(MAP_NR(mem + off));
367 dprintk(4,
368 KERN_INFO
369 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
370 ZR_DEVNAME(zr), i, (unsigned long) mem,
371 virt_to_bus(mem));
372 } else {
373 #if defined(CONFIG_BIGPHYS_AREA)
374 /* Use bigphysarea_alloc_pages */
376 int n =
377 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
378 1) / PAGE_SIZE;
380 mem =
381 (unsigned char *) bigphysarea_alloc_pages(n, 0,
382 GFP_KERNEL);
383 if (mem == 0) {
384 dprintk(1,
385 KERN_ERR
386 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
387 ZR_DEVNAME(zr), i);
388 v4l_fbuffer_free(file);
389 return -ENOBUFS;
391 fh->v4l_buffers.buffer[i].fbuffer = mem;
392 fh->v4l_buffers.buffer[i].fbuffer_phys =
393 virt_to_phys(mem);
394 fh->v4l_buffers.buffer[i].fbuffer_bus =
395 virt_to_bus(mem);
396 dprintk(4,
397 KERN_INFO
398 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
399 ZR_DEVNAME(zr), i, (unsigned) mem,
400 (unsigned) virt_to_bus(mem));
402 /* Zero out the allocated memory */
403 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
404 fh->v4l_buffers.buffer_size);
405 #elif defined(BUZ_USE_HIMEM)
407 /* Use high memory which has been left at boot time */
409 /* Ok., Ok. this is an evil hack - we make
410 * the assumption that physical addresses are
411 * the same as bus addresses (true at least
412 * for Intel processors). The whole method of
413 * obtaining and using this memory is not very
414 * nice - but I hope it saves some poor users
415 * from kernel hacking, which might have even
416 * more evil results */
418 if (i == 0) {
419 int size =
420 fh->v4l_buffers.num_buffers *
421 fh->v4l_buffers.buffer_size;
423 pmem = get_high_mem(size);
424 if (pmem == 0) {
425 dprintk(1,
426 KERN_ERR
427 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
428 ZR_DEVNAME(zr), size >> 10);
429 return -ENOBUFS;
431 fh->v4l_buffers.buffer[0].fbuffer = NULL;
432 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
433 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
434 dprintk(4,
435 KERN_INFO
436 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
437 ZR_DEVNAME(zr), size >> 10);
438 } else {
439 fh->v4l_buffers.buffer[i].fbuffer = NULL;
440 fh->v4l_buffers.buffer[i].fbuffer_phys =
441 pmem + i * fh->v4l_buffers.buffer_size;
442 fh->v4l_buffers.buffer[i].fbuffer_bus =
443 pmem + i * fh->v4l_buffers.buffer_size;
445 #else
446 /* No bigphysarea present, usage of high memory disabled,
447 * but user wants buffers of more than MAX_KMALLOC_MEM */
448 dprintk(1,
449 KERN_ERR
450 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
451 ZR_DEVNAME(zr));
452 dprintk(1,
453 KERN_ERR
454 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
455 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
456 fh->v4l_buffers.buffer_size >> 10);
457 return -ENOBUFS;
458 #endif
462 fh->v4l_buffers.allocated = 1;
464 return 0;
467 /* free the V4L grab buffers */
468 static void
469 v4l_fbuffer_free (struct file *file)
471 struct zoran_fh *fh = file->private_data;
472 struct zoran *zr = fh->zr;
473 int i, off;
474 unsigned char *mem;
476 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
478 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
479 if (!fh->v4l_buffers.buffer[i].fbuffer)
480 continue;
482 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
483 mem = fh->v4l_buffers.buffer[i].fbuffer;
484 for (off = 0; off < fh->v4l_buffers.buffer_size;
485 off += PAGE_SIZE)
486 ClearPageReserved(MAP_NR(mem + off));
487 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
489 #if defined(CONFIG_BIGPHYS_AREA)
490 else
491 bigphysarea_free_pages((void *) fh->v4l_buffers.
492 buffer[i].fbuffer);
493 #endif
494 fh->v4l_buffers.buffer[i].fbuffer = NULL;
497 fh->v4l_buffers.allocated = 0;
498 fh->v4l_buffers.ready_to_be_freed = 0;
502 * Allocate the MJPEG grab buffers.
504 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
505 * kmalloc is used to request a physically contiguous area,
506 * else we allocate the memory in framgents with get_zeroed_page.
508 * If a Natoma chipset is present and this is a revision 1 zr36057,
509 * each MJPEG buffer needs to be physically contiguous.
510 * (RJ: This statement is from Dave Perks' original driver,
511 * I could never check it because I have a zr36067)
512 * The driver cares about this because it reduces the buffer
513 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
515 * RJ: The contents grab buffers needs never be accessed in the driver.
516 * Therefore there is no need to allocate them with vmalloc in order
517 * to get a contiguous virtual memory space.
518 * I don't understand why many other drivers first allocate them with
519 * vmalloc (which uses internally also get_zeroed_page, but delivers you
520 * virtual addresses) and then again have to make a lot of efforts
521 * to get the physical address.
523 * Ben Capper:
524 * On big-endian architectures (such as ppc) some extra steps
525 * are needed. When reading and writing to the stat_com array
526 * and fragment buffers, the device expects to see little-
527 * endian values. The use of cpu_to_le32() and le32_to_cpu()
528 * in this function (and one or two others in zoran_device.c)
529 * ensure that these values are always stored in little-endian
530 * form, regardless of architecture. The zr36057 does Very Bad
531 * Things on big endian architectures if the stat_com array
532 * and fragment buffers are not little-endian.
535 static int
536 jpg_fbuffer_alloc (struct file *file)
538 struct zoran_fh *fh = file->private_data;
539 struct zoran *zr = fh->zr;
540 int i, j, off;
541 unsigned long mem;
543 /* we might have old buffers lying around */
544 if (fh->jpg_buffers.ready_to_be_freed) {
545 jpg_fbuffer_free(file);
548 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
549 if (fh->jpg_buffers.buffer[i].frag_tab)
550 dprintk(2,
551 KERN_WARNING
552 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
553 ZR_DEVNAME(zr), i);
555 /* Allocate fragment table for this buffer */
557 mem = get_zeroed_page(GFP_KERNEL);
558 if (mem == 0) {
559 dprintk(1,
560 KERN_ERR
561 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
562 ZR_DEVNAME(zr), i);
563 jpg_fbuffer_free(file);
564 return -ENOBUFS;
566 memset((void *) mem, 0, PAGE_SIZE);
567 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
568 fh->jpg_buffers.buffer[i].frag_tab_bus =
569 virt_to_bus((void *) mem);
571 //if (alloc_contig) {
572 if (fh->jpg_buffers.need_contiguous) {
573 mem =
574 (unsigned long) kmalloc(fh->jpg_buffers.
575 buffer_size,
576 GFP_KERNEL);
577 if (mem == 0) {
578 dprintk(1,
579 KERN_ERR
580 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
581 ZR_DEVNAME(zr), i);
582 jpg_fbuffer_free(file);
583 return -ENOBUFS;
585 fh->jpg_buffers.buffer[i].frag_tab[0] =
586 cpu_to_le32(virt_to_bus((void *) mem));
587 fh->jpg_buffers.buffer[i].frag_tab[1] =
588 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
589 for (off = 0; off < fh->jpg_buffers.buffer_size;
590 off += PAGE_SIZE)
591 SetPageReserved(MAP_NR(mem + off));
592 } else {
593 /* jpg_bufsize is allreay page aligned */
594 for (j = 0;
595 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
596 j++) {
597 mem = get_zeroed_page(GFP_KERNEL);
598 if (mem == 0) {
599 dprintk(1,
600 KERN_ERR
601 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
602 ZR_DEVNAME(zr), i);
603 jpg_fbuffer_free(file);
604 return -ENOBUFS;
607 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
608 cpu_to_le32(virt_to_bus((void *) mem));
609 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
610 1] =
611 cpu_to_le32((PAGE_SIZE / 4) << 1);
612 SetPageReserved(MAP_NR(mem));
615 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
619 dprintk(4,
620 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
621 ZR_DEVNAME(zr),
622 (fh->jpg_buffers.num_buffers *
623 fh->jpg_buffers.buffer_size) >> 10);
625 fh->jpg_buffers.allocated = 1;
627 return 0;
630 /* free the MJPEG grab buffers */
631 static void
632 jpg_fbuffer_free (struct file *file)
634 struct zoran_fh *fh = file->private_data;
635 struct zoran *zr = fh->zr;
636 int i, j, off;
637 unsigned char *mem;
639 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
641 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
642 if (!fh->jpg_buffers.buffer[i].frag_tab)
643 continue;
645 //if (alloc_contig) {
646 if (fh->jpg_buffers.need_contiguous) {
647 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
648 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
649 fh->jpg_buffers.buffer[i].frag_tab[0]));
650 for (off = 0;
651 off < fh->jpg_buffers.buffer_size;
652 off += PAGE_SIZE)
653 ClearPageReserved(MAP_NR
654 (mem + off));
655 kfree(mem);
656 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
657 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
659 } else {
660 for (j = 0;
661 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
662 j++) {
663 if (!fh->jpg_buffers.buffer[i].
664 frag_tab[2 * j])
665 break;
666 ClearPageReserved(MAP_NR
667 (bus_to_virt
668 (le32_to_cpu
669 (fh->jpg_buffers.
670 buffer[i].frag_tab[2 *
671 j]))));
672 free_page((unsigned long)
673 bus_to_virt
674 (le32_to_cpu
675 (fh->jpg_buffers.
676 buffer[i].
677 frag_tab[2 * j])));
678 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
680 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
681 1] = 0;
685 free_page((unsigned long) fh->jpg_buffers.buffer[i].
686 frag_tab);
687 fh->jpg_buffers.buffer[i].frag_tab = NULL;
690 fh->jpg_buffers.allocated = 0;
691 fh->jpg_buffers.ready_to_be_freed = 0;
695 * V4L Buffer grabbing
698 static int
699 zoran_v4l_set_format (struct file *file,
700 int width,
701 int height,
702 const struct zoran_format *format)
704 struct zoran_fh *fh = file->private_data;
705 struct zoran *zr = fh->zr;
706 int bpp;
708 /* Check size and format of the grab wanted */
710 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
711 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
712 dprintk(1,
713 KERN_ERR
714 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
715 ZR_DEVNAME(zr), width, height);
716 return -EINVAL;
719 bpp = (format->depth + 7) / 8;
721 /* Check against available buffer size */
722 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
723 dprintk(1,
724 KERN_ERR
725 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
726 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
727 return -EINVAL;
730 /* The video front end needs 4-byte alinged line sizes */
732 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
733 dprintk(1,
734 KERN_ERR
735 "%s: v4l_set_format() - wrong frame alingment\n",
736 ZR_DEVNAME(zr));
737 return -EINVAL;
740 fh->v4l_settings.width = width;
741 fh->v4l_settings.height = height;
742 fh->v4l_settings.format = format;
743 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
745 return 0;
748 static int
749 zoran_v4l_queue_frame (struct file *file,
750 int num)
752 struct zoran_fh *fh = file->private_data;
753 struct zoran *zr = fh->zr;
754 unsigned long flags;
755 int res = 0;
757 if (!fh->v4l_buffers.allocated) {
758 dprintk(1,
759 KERN_ERR
760 "%s: v4l_queue_frame() - buffers not yet allocated\n",
761 ZR_DEVNAME(zr));
762 res = -ENOMEM;
765 /* No grabbing outside the buffer range! */
766 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
767 dprintk(1,
768 KERN_ERR
769 "%s: v4l_queue_frame() - buffer %d is out of range\n",
770 ZR_DEVNAME(zr), num);
771 res = -EINVAL;
774 spin_lock_irqsave(&zr->spinlock, flags);
776 if (fh->v4l_buffers.active == ZORAN_FREE) {
777 if (zr->v4l_buffers.active == ZORAN_FREE) {
778 zr->v4l_buffers = fh->v4l_buffers;
779 fh->v4l_buffers.active = ZORAN_ACTIVE;
780 } else {
781 dprintk(1,
782 KERN_ERR
783 "%s: v4l_queue_frame() - another session is already capturing\n",
784 ZR_DEVNAME(zr));
785 res = -EBUSY;
789 /* make sure a grab isn't going on currently with this buffer */
790 if (!res) {
791 switch (zr->v4l_buffers.buffer[num].state) {
792 default:
793 case BUZ_STATE_PEND:
794 if (zr->v4l_buffers.active == ZORAN_FREE) {
795 fh->v4l_buffers.active = ZORAN_FREE;
796 zr->v4l_buffers.allocated = 0;
798 res = -EBUSY; /* what are you doing? */
799 break;
800 case BUZ_STATE_DONE:
801 dprintk(2,
802 KERN_WARNING
803 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
804 ZR_DEVNAME(zr), num);
805 case BUZ_STATE_USER:
806 /* since there is at least one unused buffer there's room for at least
807 * one more pend[] entry */
808 zr->v4l_pend[zr->v4l_pend_head++ &
809 V4L_MASK_FRAME] = num;
810 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
811 zr->v4l_buffers.buffer[num].bs.length =
812 fh->v4l_settings.bytesperline *
813 zr->v4l_settings.height;
814 fh->v4l_buffers.buffer[num] =
815 zr->v4l_buffers.buffer[num];
816 break;
820 spin_unlock_irqrestore(&zr->spinlock, flags);
822 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
823 zr->v4l_buffers.active = fh->v4l_buffers.active;
825 return res;
828 static int
829 v4l_grab (struct file *file,
830 struct video_mmap *mp)
832 struct zoran_fh *fh = file->private_data;
833 struct zoran *zr = fh->zr;
834 int res = 0, i;
836 for (i = 0; i < zoran_num_formats; i++) {
837 if (zoran_formats[i].palette == mp->format &&
838 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
839 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
840 break;
842 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
843 dprintk(1,
844 KERN_ERR
845 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
846 ZR_DEVNAME(zr));
847 return -EINVAL;
851 * To minimize the time spent in the IRQ routine, we avoid setting up
852 * the video front end there.
853 * If this grab has different parameters from a running streaming capture
854 * we stop the streaming capture and start it over again.
856 if (zr->v4l_memgrab_active &&
857 (zr->v4l_settings.width != mp->width ||
858 zr->v4l_settings.height != mp->height ||
859 zr->v4l_settings.format->palette != mp->format)) {
860 res = wait_grab_pending(zr);
861 if (res)
862 return res;
864 if ((res = zoran_v4l_set_format(file,
865 mp->width,
866 mp->height,
867 &zoran_formats[i])))
868 return res;
869 zr->v4l_settings = fh->v4l_settings;
871 /* queue the frame in the pending queue */
872 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
873 fh->v4l_buffers.active = ZORAN_FREE;
874 return res;
877 /* put the 36057 into frame grabbing mode */
878 if (!res && !zr->v4l_memgrab_active)
879 zr36057_set_memgrab(zr, 1);
881 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
883 return res;
887 * Sync on a V4L buffer
890 static int
891 v4l_sync (struct file *file,
892 int frame)
894 struct zoran_fh *fh = file->private_data;
895 struct zoran *zr = fh->zr;
896 unsigned long flags;
898 if (fh->v4l_buffers.active == ZORAN_FREE) {
899 dprintk(1,
900 KERN_ERR
901 "%s: v4l_sync() - no grab active for this session\n",
902 ZR_DEVNAME(zr));
903 return -EINVAL;
906 /* check passed-in frame number */
907 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
908 dprintk(1,
909 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
910 ZR_DEVNAME(zr), frame);
911 return -EINVAL;
914 /* Check if is buffer was queued at all */
915 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
916 dprintk(1,
917 KERN_ERR
918 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
919 ZR_DEVNAME(zr));
920 return -EPROTO;
923 /* wait on this buffer to get ready */
924 if (!wait_event_interruptible_timeout(zr->v4l_capq,
925 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
926 10*HZ))
927 return -ETIME;
928 if (signal_pending(current))
929 return -ERESTARTSYS;
931 /* buffer should now be in BUZ_STATE_DONE */
932 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
933 dprintk(2,
934 KERN_ERR "%s: v4l_sync() - internal state error\n",
935 ZR_DEVNAME(zr));
937 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
938 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
940 spin_lock_irqsave(&zr->spinlock, flags);
942 /* Check if streaming capture has finished */
943 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
944 zr36057_set_memgrab(zr, 0);
945 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
946 fh->v4l_buffers.active = zr->v4l_buffers.active =
947 ZORAN_FREE;
948 zr->v4l_buffers.allocated = 0;
952 spin_unlock_irqrestore(&zr->spinlock, flags);
954 return 0;
958 * Queue a MJPEG buffer for capture/playback
961 static int
962 zoran_jpg_queue_frame (struct file *file,
963 int num,
964 enum zoran_codec_mode mode)
966 struct zoran_fh *fh = file->private_data;
967 struct zoran *zr = fh->zr;
968 unsigned long flags;
969 int res = 0;
971 /* Check if buffers are allocated */
972 if (!fh->jpg_buffers.allocated) {
973 dprintk(1,
974 KERN_ERR
975 "%s: jpg_queue_frame() - buffers not yet allocated\n",
976 ZR_DEVNAME(zr));
977 return -ENOMEM;
980 /* No grabbing outside the buffer range! */
981 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
982 dprintk(1,
983 KERN_ERR
984 "%s: jpg_queue_frame() - buffer %d out of range\n",
985 ZR_DEVNAME(zr), num);
986 return -EINVAL;
989 /* what is the codec mode right now? */
990 if (zr->codec_mode == BUZ_MODE_IDLE) {
991 zr->jpg_settings = fh->jpg_settings;
992 } else if (zr->codec_mode != mode) {
993 /* wrong codec mode active - invalid */
994 dprintk(1,
995 KERN_ERR
996 "%s: jpg_queue_frame() - codec in wrong mode\n",
997 ZR_DEVNAME(zr));
998 return -EINVAL;
1001 if (fh->jpg_buffers.active == ZORAN_FREE) {
1002 if (zr->jpg_buffers.active == ZORAN_FREE) {
1003 zr->jpg_buffers = fh->jpg_buffers;
1004 fh->jpg_buffers.active = ZORAN_ACTIVE;
1005 } else {
1006 dprintk(1,
1007 KERN_ERR
1008 "%s: jpg_queue_frame() - another session is already capturing\n",
1009 ZR_DEVNAME(zr));
1010 res = -EBUSY;
1014 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1015 /* Ok load up the jpeg codec */
1016 zr36057_enable_jpg(zr, mode);
1019 spin_lock_irqsave(&zr->spinlock, flags);
1021 if (!res) {
1022 switch (zr->jpg_buffers.buffer[num].state) {
1023 case BUZ_STATE_DONE:
1024 dprintk(2,
1025 KERN_WARNING
1026 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1027 ZR_DEVNAME(zr));
1028 case BUZ_STATE_USER:
1029 /* since there is at least one unused buffer there's room for at
1030 *least one more pend[] entry */
1031 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1032 num;
1033 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1034 fh->jpg_buffers.buffer[num] =
1035 zr->jpg_buffers.buffer[num];
1036 zoran_feed_stat_com(zr);
1037 break;
1038 default:
1039 case BUZ_STATE_DMA:
1040 case BUZ_STATE_PEND:
1041 if (zr->jpg_buffers.active == ZORAN_FREE) {
1042 fh->jpg_buffers.active = ZORAN_FREE;
1043 zr->jpg_buffers.allocated = 0;
1045 res = -EBUSY; /* what are you doing? */
1046 break;
1050 spin_unlock_irqrestore(&zr->spinlock, flags);
1052 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1053 zr->jpg_buffers.active = fh->jpg_buffers.active;
1056 return res;
1059 static int
1060 jpg_qbuf (struct file *file,
1061 int frame,
1062 enum zoran_codec_mode mode)
1064 struct zoran_fh *fh = file->private_data;
1065 struct zoran *zr = fh->zr;
1066 int res = 0;
1068 /* Does the user want to stop streaming? */
1069 if (frame < 0) {
1070 if (zr->codec_mode == mode) {
1071 if (fh->jpg_buffers.active == ZORAN_FREE) {
1072 dprintk(1,
1073 KERN_ERR
1074 "%s: jpg_qbuf(-1) - session not active\n",
1075 ZR_DEVNAME(zr));
1076 return -EINVAL;
1078 fh->jpg_buffers.active = zr->jpg_buffers.active =
1079 ZORAN_FREE;
1080 zr->jpg_buffers.allocated = 0;
1081 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1082 return 0;
1083 } else {
1084 dprintk(1,
1085 KERN_ERR
1086 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1087 ZR_DEVNAME(zr));
1088 return -EINVAL;
1092 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1093 return res;
1095 /* Start the jpeg codec when the first frame is queued */
1096 if (!res && zr->jpg_que_head == 1)
1097 jpeg_start(zr);
1099 return res;
1103 * Sync on a MJPEG buffer
1106 static int
1107 jpg_sync (struct file *file,
1108 struct zoran_sync *bs)
1110 struct zoran_fh *fh = file->private_data;
1111 struct zoran *zr = fh->zr;
1112 unsigned long flags;
1113 int frame;
1115 if (fh->jpg_buffers.active == ZORAN_FREE) {
1116 dprintk(1,
1117 KERN_ERR
1118 "%s: jpg_sync() - capture is not currently active\n",
1119 ZR_DEVNAME(zr));
1120 return -EINVAL;
1122 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1123 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1124 dprintk(1,
1125 KERN_ERR
1126 "%s: jpg_sync() - codec not in streaming mode\n",
1127 ZR_DEVNAME(zr));
1128 return -EINVAL;
1130 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1131 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1132 zr->jpg_dma_tail == zr->jpg_dma_head),
1133 10*HZ)) {
1134 int isr;
1136 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1137 udelay(1);
1138 zr->codec->control(zr->codec, CODEC_G_STATUS,
1139 sizeof(isr), &isr);
1140 dprintk(1,
1141 KERN_ERR
1142 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1143 ZR_DEVNAME(zr), isr);
1145 return -ETIME;
1148 if (signal_pending(current))
1149 return -ERESTARTSYS;
1151 spin_lock_irqsave(&zr->spinlock, flags);
1153 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1154 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1155 else
1156 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1158 /* buffer should now be in BUZ_STATE_DONE */
1159 if (*zr_debug > 0)
1160 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1161 dprintk(2,
1162 KERN_ERR
1163 "%s: jpg_sync() - internal state error\n",
1164 ZR_DEVNAME(zr));
1166 *bs = zr->jpg_buffers.buffer[frame].bs;
1167 bs->frame = frame;
1168 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1169 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1171 spin_unlock_irqrestore(&zr->spinlock, flags);
1173 return 0;
1176 static void
1177 zoran_open_init_session (struct file *file)
1179 int i;
1180 struct zoran_fh *fh = file->private_data;
1181 struct zoran *zr = fh->zr;
1183 /* Per default, map the V4L Buffers */
1184 fh->map_mode = ZORAN_MAP_MODE_RAW;
1186 /* take over the card's current settings */
1187 fh->overlay_settings = zr->overlay_settings;
1188 fh->overlay_settings.is_set = 0;
1189 fh->overlay_settings.format = zr->overlay_settings.format;
1190 fh->overlay_active = ZORAN_FREE;
1192 /* v4l settings */
1193 fh->v4l_settings = zr->v4l_settings;
1195 /* v4l_buffers */
1196 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1197 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1198 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1199 fh->v4l_buffers.buffer[i].bs.frame = i;
1201 fh->v4l_buffers.allocated = 0;
1202 fh->v4l_buffers.ready_to_be_freed = 0;
1203 fh->v4l_buffers.active = ZORAN_FREE;
1204 fh->v4l_buffers.buffer_size = v4l_bufsize;
1205 fh->v4l_buffers.num_buffers = v4l_nbufs;
1207 /* jpg settings */
1208 fh->jpg_settings = zr->jpg_settings;
1210 /* jpg_buffers */
1211 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1212 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1213 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1214 fh->jpg_buffers.buffer[i].bs.frame = i;
1216 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1217 fh->jpg_buffers.allocated = 0;
1218 fh->jpg_buffers.ready_to_be_freed = 0;
1219 fh->jpg_buffers.active = ZORAN_FREE;
1220 fh->jpg_buffers.buffer_size = jpg_bufsize;
1221 fh->jpg_buffers.num_buffers = jpg_nbufs;
1224 static void
1225 zoran_close_end_session (struct file *file)
1227 struct zoran_fh *fh = file->private_data;
1228 struct zoran *zr = fh->zr;
1230 /* overlay */
1231 if (fh->overlay_active != ZORAN_FREE) {
1232 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1233 zr->v4l_overlay_active = 0;
1234 if (!zr->v4l_memgrab_active)
1235 zr36057_overlay(zr, 0);
1236 zr->overlay_mask = NULL;
1239 /* v4l capture */
1240 if (fh->v4l_buffers.active != ZORAN_FREE) {
1241 zr36057_set_memgrab(zr, 0);
1242 zr->v4l_buffers.allocated = 0;
1243 zr->v4l_buffers.active = fh->v4l_buffers.active =
1244 ZORAN_FREE;
1247 /* v4l buffers */
1248 if (fh->v4l_buffers.allocated ||
1249 fh->v4l_buffers.ready_to_be_freed) {
1250 v4l_fbuffer_free(file);
1253 /* jpg capture */
1254 if (fh->jpg_buffers.active != ZORAN_FREE) {
1255 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1256 zr->jpg_buffers.allocated = 0;
1257 zr->jpg_buffers.active = fh->jpg_buffers.active =
1258 ZORAN_FREE;
1261 /* jpg buffers */
1262 if (fh->jpg_buffers.allocated ||
1263 fh->jpg_buffers.ready_to_be_freed) {
1264 jpg_fbuffer_free(file);
1269 * Open a zoran card. Right now the flags stuff is just playing
1272 static int
1273 zoran_open (struct inode *inode,
1274 struct file *file)
1276 unsigned int minor = iminor(inode);
1277 struct zoran *zr = NULL;
1278 struct zoran_fh *fh;
1279 int i, res, first_open = 0, have_module_locks = 0;
1281 /* find the device */
1282 for (i = 0; i < zoran_num; i++) {
1283 if (zoran[i].video_dev->minor == minor) {
1284 zr = &zoran[i];
1285 break;
1289 if (!zr) {
1290 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1291 res = -ENODEV;
1292 goto open_unlock_and_return;
1295 /* see fs/device.c - the kernel already locks during open(),
1296 * so locking ourselves only causes deadlocks */
1297 /*mutex_lock(&zr->resource_lock);*/
1299 if (!zr->decoder) {
1300 dprintk(1,
1301 KERN_ERR "%s: no TV decoder loaded for device!\n",
1302 ZR_DEVNAME(zr));
1303 res = -EIO;
1304 goto open_unlock_and_return;
1307 /* try to grab a module lock */
1308 if (!try_module_get(THIS_MODULE)) {
1309 dprintk(1,
1310 KERN_ERR
1311 "%s: failed to acquire my own lock! PANIC!\n",
1312 ZR_DEVNAME(zr));
1313 res = -ENODEV;
1314 goto open_unlock_and_return;
1316 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1317 dprintk(1,
1318 KERN_ERR
1319 "%s: failed to grab ownership of i2c decoder\n",
1320 ZR_DEVNAME(zr));
1321 res = -EIO;
1322 module_put(THIS_MODULE);
1323 goto open_unlock_and_return;
1325 if (zr->encoder &&
1326 !try_module_get(zr->encoder->driver->driver.owner)) {
1327 dprintk(1,
1328 KERN_ERR
1329 "%s: failed to grab ownership of i2c encoder\n",
1330 ZR_DEVNAME(zr));
1331 res = -EIO;
1332 module_put(zr->decoder->driver->driver.owner);
1333 module_put(THIS_MODULE);
1334 goto open_unlock_and_return;
1337 have_module_locks = 1;
1339 if (zr->user >= 2048) {
1340 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1341 ZR_DEVNAME(zr), zr->user);
1342 res = -EBUSY;
1343 goto open_unlock_and_return;
1346 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1347 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1349 /* now, create the open()-specific file_ops struct */
1350 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1351 if (!fh) {
1352 dprintk(1,
1353 KERN_ERR
1354 "%s: zoran_open() - allocation of zoran_fh failed\n",
1355 ZR_DEVNAME(zr));
1356 res = -ENOMEM;
1357 goto open_unlock_and_return;
1359 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1360 * on norm-change! */
1361 fh->overlay_mask =
1362 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1363 if (!fh->overlay_mask) {
1364 dprintk(1,
1365 KERN_ERR
1366 "%s: zoran_open() - allocation of overlay_mask failed\n",
1367 ZR_DEVNAME(zr));
1368 kfree(fh);
1369 res = -ENOMEM;
1370 goto open_unlock_and_return;
1373 if (zr->user++ == 0)
1374 first_open = 1;
1376 /*mutex_unlock(&zr->resource_lock);*/
1378 /* default setup - TODO: look at flags */
1379 if (first_open) { /* First device open */
1380 zr36057_restart(zr);
1381 zoran_open_init_params(zr);
1382 zoran_init_hardware(zr);
1384 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1387 /* set file_ops stuff */
1388 file->private_data = fh;
1389 fh->zr = zr;
1390 zoran_open_init_session(file);
1392 return 0;
1394 open_unlock_and_return:
1395 /* if we grabbed locks, release them accordingly */
1396 if (have_module_locks) {
1397 module_put(zr->decoder->driver->driver.owner);
1398 if (zr->encoder) {
1399 module_put(zr->encoder->driver->driver.owner);
1401 module_put(THIS_MODULE);
1404 /* if there's no device found, we didn't obtain the lock either */
1405 if (zr) {
1406 /*mutex_unlock(&zr->resource_lock);*/
1409 return res;
1412 static int
1413 zoran_close (struct inode *inode,
1414 struct file *file)
1416 struct zoran_fh *fh = file->private_data;
1417 struct zoran *zr = fh->zr;
1419 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1420 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1422 /* kernel locks (fs/device.c), so don't do that ourselves
1423 * (prevents deadlocks) */
1424 /*mutex_lock(&zr->resource_lock);*/
1426 zoran_close_end_session(file);
1428 if (zr->user-- == 1) { /* Last process */
1429 /* Clean up JPEG process */
1430 wake_up_interruptible(&zr->jpg_capq);
1431 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1432 zr->jpg_buffers.allocated = 0;
1433 zr->jpg_buffers.active = ZORAN_FREE;
1435 /* disable interrupts */
1436 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1438 if (*zr_debug > 1)
1439 print_interrupts(zr);
1441 /* Overlay off */
1442 zr->v4l_overlay_active = 0;
1443 zr36057_overlay(zr, 0);
1444 zr->overlay_mask = NULL;
1446 /* capture off */
1447 wake_up_interruptible(&zr->v4l_capq);
1448 zr36057_set_memgrab(zr, 0);
1449 zr->v4l_buffers.allocated = 0;
1450 zr->v4l_buffers.active = ZORAN_FREE;
1451 zoran_set_pci_master(zr, 0);
1453 if (!pass_through) { /* Switch to color bar */
1454 int zero = 0, two = 2;
1455 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1456 encoder_command(zr, ENCODER_SET_INPUT, &two);
1460 file->private_data = NULL;
1461 kfree(fh->overlay_mask);
1462 kfree(fh);
1464 /* release locks on the i2c modules */
1465 module_put(zr->decoder->driver->driver.owner);
1466 if (zr->encoder) {
1467 module_put(zr->encoder->driver->driver.owner);
1469 module_put(THIS_MODULE);
1471 /*mutex_unlock(&zr->resource_lock);*/
1473 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1475 return 0;
1479 static ssize_t
1480 zoran_read (struct file *file,
1481 char __user *data,
1482 size_t count,
1483 loff_t *ppos)
1485 /* we simply don't support read() (yet)... */
1487 return -EINVAL;
1490 static ssize_t
1491 zoran_write (struct file *file,
1492 const char __user *data,
1493 size_t count,
1494 loff_t *ppos)
1496 /* ...and the same goes for write() */
1498 return -EINVAL;
1501 static int
1502 setup_fbuffer (struct file *file,
1503 void *base,
1504 const struct zoran_format *fmt,
1505 int width,
1506 int height,
1507 int bytesperline)
1509 struct zoran_fh *fh = file->private_data;
1510 struct zoran *zr = fh->zr;
1512 /* (Ronald) v4l/v4l2 guidelines */
1513 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1514 return -EPERM;
1516 /* we need a bytesperline value, even if not given */
1517 if (!bytesperline)
1518 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1520 #if 0
1521 if (zr->overlay_active) {
1522 /* dzjee... stupid users... don't even bother to turn off
1523 * overlay before changing the memory location...
1524 * normally, we would return errors here. However, one of
1525 * the tools that does this is... xawtv! and since xawtv
1526 * is used by +/- 99% of the users, we'd rather be user-
1527 * friendly and silently do as if nothing went wrong */
1528 dprintk(3,
1529 KERN_ERR
1530 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1531 ZR_DEVNAME(zr));
1532 zr36057_overlay(zr, 0);
1534 #endif
1536 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1537 dprintk(1,
1538 KERN_ERR
1539 "%s: setup_fbuffer() - no valid overlay format given\n",
1540 ZR_DEVNAME(zr));
1541 return -EINVAL;
1543 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1544 dprintk(1,
1545 KERN_ERR
1546 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1547 ZR_DEVNAME(zr), width, height, bytesperline);
1548 return -EINVAL;
1550 if (bytesperline & 3) {
1551 dprintk(1,
1552 KERN_ERR
1553 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1554 ZR_DEVNAME(zr), bytesperline);
1555 return -EINVAL;
1558 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1559 zr->buffer.height = height;
1560 zr->buffer.width = width;
1561 zr->buffer.depth = fmt->depth;
1562 zr->overlay_settings.format = fmt;
1563 zr->buffer.bytesperline = bytesperline;
1565 /* The user should set new window parameters */
1566 zr->overlay_settings.is_set = 0;
1568 return 0;
1572 static int
1573 setup_window (struct file *file,
1574 int x,
1575 int y,
1576 int width,
1577 int height,
1578 struct video_clip __user *clips,
1579 int clipcount,
1580 void __user *bitmap)
1582 struct zoran_fh *fh = file->private_data;
1583 struct zoran *zr = fh->zr;
1584 struct video_clip *vcp = NULL;
1585 int on, end;
1588 if (!zr->buffer.base) {
1589 dprintk(1,
1590 KERN_ERR
1591 "%s: setup_window() - frame buffer has to be set first\n",
1592 ZR_DEVNAME(zr));
1593 return -EINVAL;
1596 if (!fh->overlay_settings.format) {
1597 dprintk(1,
1598 KERN_ERR
1599 "%s: setup_window() - no overlay format set\n",
1600 ZR_DEVNAME(zr));
1601 return -EINVAL;
1605 * The video front end needs 4-byte alinged line sizes, we correct that
1606 * silently here if necessary
1608 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1609 end = (x + width) & ~1; /* round down */
1610 x = (x + 1) & ~1; /* round up */
1611 width = end - x;
1614 if (zr->buffer.depth == 24) {
1615 end = (x + width) & ~3; /* round down */
1616 x = (x + 3) & ~3; /* round up */
1617 width = end - x;
1620 if (width > BUZ_MAX_WIDTH)
1621 width = BUZ_MAX_WIDTH;
1622 if (height > BUZ_MAX_HEIGHT)
1623 height = BUZ_MAX_HEIGHT;
1625 /* Check for vaild parameters */
1626 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1627 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1628 dprintk(1,
1629 KERN_ERR
1630 "%s: setup_window() - width = %d or height = %d invalid\n",
1631 ZR_DEVNAME(zr), width, height);
1632 return -EINVAL;
1635 fh->overlay_settings.x = x;
1636 fh->overlay_settings.y = y;
1637 fh->overlay_settings.width = width;
1638 fh->overlay_settings.height = height;
1639 fh->overlay_settings.clipcount = clipcount;
1642 * If an overlay is running, we have to switch it off
1643 * and switch it on again in order to get the new settings in effect.
1645 * We also want to avoid that the overlay mask is written
1646 * when an overlay is running.
1649 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1650 zr->overlay_active != ZORAN_FREE &&
1651 fh->overlay_active != ZORAN_FREE;
1652 if (on)
1653 zr36057_overlay(zr, 0);
1656 * Write the overlay mask if clips are wanted.
1657 * We prefer a bitmap.
1659 if (bitmap) {
1660 /* fake value - it just means we want clips */
1661 fh->overlay_settings.clipcount = 1;
1663 if (copy_from_user(fh->overlay_mask, bitmap,
1664 (width * height + 7) / 8)) {
1665 return -EFAULT;
1667 } else if (clipcount > 0) {
1668 /* write our own bitmap from the clips */
1669 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1670 if (vcp == NULL) {
1671 dprintk(1,
1672 KERN_ERR
1673 "%s: setup_window() - Alloc of clip mask failed\n",
1674 ZR_DEVNAME(zr));
1675 return -ENOMEM;
1677 if (copy_from_user
1678 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1679 vfree(vcp);
1680 return -EFAULT;
1682 write_overlay_mask(file, vcp, clipcount);
1683 vfree(vcp);
1686 fh->overlay_settings.is_set = 1;
1687 if (fh->overlay_active != ZORAN_FREE &&
1688 zr->overlay_active != ZORAN_FREE)
1689 zr->overlay_settings = fh->overlay_settings;
1691 if (on)
1692 zr36057_overlay(zr, 1);
1694 /* Make sure the changes come into effect */
1695 return wait_grab_pending(zr);
1698 static int
1699 setup_overlay (struct file *file,
1700 int on)
1702 struct zoran_fh *fh = file->private_data;
1703 struct zoran *zr = fh->zr;
1705 /* If there is nothing to do, return immediatly */
1706 if ((on && fh->overlay_active != ZORAN_FREE) ||
1707 (!on && fh->overlay_active == ZORAN_FREE))
1708 return 0;
1710 /* check whether we're touching someone else's overlay */
1711 if (on && zr->overlay_active != ZORAN_FREE &&
1712 fh->overlay_active == ZORAN_FREE) {
1713 dprintk(1,
1714 KERN_ERR
1715 "%s: setup_overlay() - overlay is already active for another session\n",
1716 ZR_DEVNAME(zr));
1717 return -EBUSY;
1719 if (!on && zr->overlay_active != ZORAN_FREE &&
1720 fh->overlay_active == ZORAN_FREE) {
1721 dprintk(1,
1722 KERN_ERR
1723 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1724 ZR_DEVNAME(zr));
1725 return -EPERM;
1728 if (on == 0) {
1729 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1730 zr->v4l_overlay_active = 0;
1731 /* When a grab is running, the video simply
1732 * won't be switched on any more */
1733 if (!zr->v4l_memgrab_active)
1734 zr36057_overlay(zr, 0);
1735 zr->overlay_mask = NULL;
1736 } else {
1737 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1738 dprintk(1,
1739 KERN_ERR
1740 "%s: setup_overlay() - buffer or window not set\n",
1741 ZR_DEVNAME(zr));
1742 return -EINVAL;
1744 if (!fh->overlay_settings.format) {
1745 dprintk(1,
1746 KERN_ERR
1747 "%s: setup_overlay() - no overlay format set\n",
1748 ZR_DEVNAME(zr));
1749 return -EINVAL;
1751 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1752 zr->v4l_overlay_active = 1;
1753 zr->overlay_mask = fh->overlay_mask;
1754 zr->overlay_settings = fh->overlay_settings;
1755 if (!zr->v4l_memgrab_active)
1756 zr36057_overlay(zr, 1);
1757 /* When a grab is running, the video will be
1758 * switched on when grab is finished */
1761 /* Make sure the changes come into effect */
1762 return wait_grab_pending(zr);
1765 #ifdef HAVE_V4L2
1766 /* get the status of a buffer in the clients buffer queue */
1767 static int
1768 zoran_v4l2_buffer_status (struct file *file,
1769 struct v4l2_buffer *buf,
1770 int num)
1772 struct zoran_fh *fh = file->private_data;
1773 struct zoran *zr = fh->zr;
1775 buf->flags = V4L2_BUF_FLAG_MAPPED;
1777 switch (fh->map_mode) {
1778 case ZORAN_MAP_MODE_RAW:
1780 /* check range */
1781 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1782 !fh->v4l_buffers.allocated) {
1783 dprintk(1,
1784 KERN_ERR
1785 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1786 ZR_DEVNAME(zr));
1787 return -EINVAL;
1790 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1791 buf->length = fh->v4l_buffers.buffer_size;
1793 /* get buffer */
1794 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1795 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1796 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1797 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1798 buf->flags |= V4L2_BUF_FLAG_DONE;
1799 buf->timestamp =
1800 fh->v4l_buffers.buffer[num].bs.timestamp;
1801 } else {
1802 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1805 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1806 buf->field = V4L2_FIELD_TOP;
1807 else
1808 buf->field = V4L2_FIELD_INTERLACED;
1810 break;
1812 case ZORAN_MAP_MODE_JPG_REC:
1813 case ZORAN_MAP_MODE_JPG_PLAY:
1815 /* check range */
1816 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1817 !fh->jpg_buffers.allocated) {
1818 dprintk(1,
1819 KERN_ERR
1820 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1821 ZR_DEVNAME(zr));
1822 return -EINVAL;
1825 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1826 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1827 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1828 buf->length = fh->jpg_buffers.buffer_size;
1830 /* these variables are only written after frame has been captured */
1831 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1832 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1833 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1834 buf->timestamp =
1835 fh->jpg_buffers.buffer[num].bs.timestamp;
1836 buf->bytesused =
1837 fh->jpg_buffers.buffer[num].bs.length;
1838 buf->flags |= V4L2_BUF_FLAG_DONE;
1839 } else {
1840 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1843 /* which fields are these? */
1844 if (fh->jpg_settings.TmpDcm != 1)
1845 buf->field =
1846 fh->jpg_settings.
1847 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1848 else
1849 buf->field =
1850 fh->jpg_settings.
1851 odd_even ? V4L2_FIELD_SEQ_TB :
1852 V4L2_FIELD_SEQ_BT;
1854 break;
1856 default:
1858 dprintk(5,
1859 KERN_ERR
1860 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1861 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1862 return -EINVAL;
1865 buf->memory = V4L2_MEMORY_MMAP;
1866 buf->index = num;
1867 buf->m.offset = buf->length * num;
1869 return 0;
1871 #endif
1873 static int
1874 zoran_set_norm (struct zoran *zr,
1875 int norm) /* VIDEO_MODE_* */
1877 int norm_encoder, on;
1879 if (zr->v4l_buffers.active != ZORAN_FREE ||
1880 zr->jpg_buffers.active != ZORAN_FREE) {
1881 dprintk(1,
1882 KERN_WARNING
1883 "%s: set_norm() called while in playback/capture mode\n",
1884 ZR_DEVNAME(zr));
1885 return -EBUSY;
1888 if (lock_norm && norm != zr->norm) {
1889 if (lock_norm > 1) {
1890 dprintk(1,
1891 KERN_WARNING
1892 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1893 ZR_DEVNAME(zr));
1894 return -EPERM;
1895 } else {
1896 dprintk(1,
1897 KERN_WARNING
1898 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1899 ZR_DEVNAME(zr));
1900 norm = zr->norm;
1904 if (norm != VIDEO_MODE_AUTO &&
1905 (norm < 0 || norm >= zr->card.norms ||
1906 !zr->card.tvn[norm])) {
1907 dprintk(1,
1908 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1909 ZR_DEVNAME(zr), norm);
1910 return -EINVAL;
1913 if (norm == VIDEO_MODE_AUTO) {
1914 int status;
1916 /* if we have autodetect, ... */
1917 struct video_decoder_capability caps;
1918 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1919 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1920 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1921 ZR_DEVNAME(zr));
1922 return -EINVAL;
1925 decoder_command(zr, DECODER_SET_NORM, &norm);
1927 /* let changes come into effect */
1928 ssleep(2);
1930 decoder_command(zr, DECODER_GET_STATUS, &status);
1931 if (!(status & DECODER_STATUS_GOOD)) {
1932 dprintk(1,
1933 KERN_ERR
1934 "%s: set_norm() - no norm detected\n",
1935 ZR_DEVNAME(zr));
1936 /* reset norm */
1937 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1938 return -EIO;
1941 if (status & DECODER_STATUS_NTSC)
1942 norm = VIDEO_MODE_NTSC;
1943 else if (status & DECODER_STATUS_SECAM)
1944 norm = VIDEO_MODE_SECAM;
1945 else
1946 norm = VIDEO_MODE_PAL;
1948 zr->timing = zr->card.tvn[norm];
1949 norm_encoder = norm;
1951 /* We switch overlay off and on since a change in the
1952 * norm needs different VFE settings */
1953 on = zr->overlay_active && !zr->v4l_memgrab_active;
1954 if (on)
1955 zr36057_overlay(zr, 0);
1957 decoder_command(zr, DECODER_SET_NORM, &norm);
1958 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1960 if (on)
1961 zr36057_overlay(zr, 1);
1963 /* Make sure the changes come into effect */
1964 zr->norm = norm;
1966 return 0;
1969 static int
1970 zoran_set_input (struct zoran *zr,
1971 int input)
1973 int realinput;
1975 if (input == zr->input) {
1976 return 0;
1979 if (zr->v4l_buffers.active != ZORAN_FREE ||
1980 zr->jpg_buffers.active != ZORAN_FREE) {
1981 dprintk(1,
1982 KERN_WARNING
1983 "%s: set_input() called while in playback/capture mode\n",
1984 ZR_DEVNAME(zr));
1985 return -EBUSY;
1988 if (input < 0 || input >= zr->card.inputs) {
1989 dprintk(1,
1990 KERN_ERR
1991 "%s: set_input() - unnsupported input %d\n",
1992 ZR_DEVNAME(zr), input);
1993 return -EINVAL;
1996 realinput = zr->card.input[input].muxsel;
1997 zr->input = input;
1999 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2001 return 0;
2005 * ioctl routine
2008 static int
2009 zoran_do_ioctl (struct inode *inode,
2010 struct file *file,
2011 unsigned int cmd,
2012 void *arg)
2014 struct zoran_fh *fh = file->private_data;
2015 struct zoran *zr = fh->zr;
2016 /* CAREFUL: used in multiple places here */
2017 struct zoran_jpg_settings settings;
2019 /* we might have older buffers lying around... We don't want
2020 * to wait, but we do want to try cleaning them up ASAP. So
2021 * we try to obtain the lock and free them. If that fails, we
2022 * don't do anything and wait for the next turn. In the end,
2023 * zoran_close() or a new allocation will still free them...
2024 * This is just a 'the sooner the better' extra 'feature'
2026 * We don't free the buffers right on munmap() because that
2027 * causes oopses (kfree() inside munmap() oopses for no
2028 * apparent reason - it's also not reproduceable in any way,
2029 * but moving the free code outside the munmap() handler fixes
2030 * all this... If someone knows why, please explain me (Ronald)
2032 if (!!mutex_trylock(&zr->resource_lock)) {
2033 /* we obtained it! Let's try to free some things */
2034 if (fh->jpg_buffers.ready_to_be_freed)
2035 jpg_fbuffer_free(file);
2036 if (fh->v4l_buffers.ready_to_be_freed)
2037 v4l_fbuffer_free(file);
2039 mutex_unlock(&zr->resource_lock);
2042 switch (cmd) {
2044 case VIDIOCGCAP:
2046 struct video_capability *vcap = arg;
2048 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2050 memset(vcap, 0, sizeof(struct video_capability));
2051 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
2052 vcap->type = ZORAN_VID_TYPE;
2054 vcap->channels = zr->card.inputs;
2055 vcap->audios = 0;
2056 mutex_lock(&zr->resource_lock);
2057 vcap->maxwidth = BUZ_MAX_WIDTH;
2058 vcap->maxheight = BUZ_MAX_HEIGHT;
2059 vcap->minwidth = BUZ_MIN_WIDTH;
2060 vcap->minheight = BUZ_MIN_HEIGHT;
2061 mutex_unlock(&zr->resource_lock);
2063 return 0;
2065 break;
2067 case VIDIOCGCHAN:
2069 struct video_channel *vchan = arg;
2070 int channel = vchan->channel;
2072 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2073 ZR_DEVNAME(zr), vchan->channel);
2075 memset(vchan, 0, sizeof(struct video_channel));
2076 if (channel > zr->card.inputs || channel < 0) {
2077 dprintk(1,
2078 KERN_ERR
2079 "%s: VIDIOCGCHAN on not existing channel %d\n",
2080 ZR_DEVNAME(zr), channel);
2081 return -EINVAL;
2084 strcpy(vchan->name, zr->card.input[channel].name);
2086 vchan->tuners = 0;
2087 vchan->flags = 0;
2088 vchan->type = VIDEO_TYPE_CAMERA;
2089 mutex_lock(&zr->resource_lock);
2090 vchan->norm = zr->norm;
2091 mutex_unlock(&zr->resource_lock);
2092 vchan->channel = channel;
2094 return 0;
2096 break;
2098 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2100 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2101 * * ^^^^^^^
2102 * * The famos BTTV driver has it implemented with a struct video_channel argument
2103 * * and we follow it for compatibility reasons
2105 * * BTW: this is the only way the user can set the norm!
2108 case VIDIOCSCHAN:
2110 struct video_channel *vchan = arg;
2111 int res;
2113 dprintk(3,
2114 KERN_DEBUG
2115 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2116 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2118 mutex_lock(&zr->resource_lock);
2119 if ((res = zoran_set_input(zr, vchan->channel)))
2120 goto schan_unlock_and_return;
2121 if ((res = zoran_set_norm(zr, vchan->norm)))
2122 goto schan_unlock_and_return;
2124 /* Make sure the changes come into effect */
2125 res = wait_grab_pending(zr);
2126 schan_unlock_and_return:
2127 mutex_unlock(&zr->resource_lock);
2128 return res;
2130 break;
2132 case VIDIOCGPICT:
2134 struct video_picture *vpict = arg;
2136 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2138 memset(vpict, 0, sizeof(struct video_picture));
2139 mutex_lock(&zr->resource_lock);
2140 vpict->hue = zr->hue;
2141 vpict->brightness = zr->brightness;
2142 vpict->contrast = zr->contrast;
2143 vpict->colour = zr->saturation;
2144 if (fh->overlay_settings.format) {
2145 vpict->depth = fh->overlay_settings.format->depth;
2146 vpict->palette = fh->overlay_settings.format->palette;
2147 } else {
2148 vpict->depth = 0;
2150 mutex_unlock(&zr->resource_lock);
2152 return 0;
2154 break;
2156 case VIDIOCSPICT:
2158 struct video_picture *vpict = arg;
2159 int i;
2161 dprintk(3,
2162 KERN_DEBUG
2163 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2164 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2165 vpict->colour, vpict->contrast, vpict->depth,
2166 vpict->palette);
2168 for (i = 0; i < zoran_num_formats; i++) {
2169 const struct zoran_format *fmt = &zoran_formats[i];
2171 if (fmt->palette != -1 &&
2172 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2173 fmt->palette == vpict->palette &&
2174 fmt->depth == vpict->depth)
2175 break;
2177 if (i == zoran_num_formats) {
2178 dprintk(1,
2179 KERN_ERR
2180 "%s: VIDIOCSPICT - Invalid palette %d\n",
2181 ZR_DEVNAME(zr), vpict->palette);
2182 return -EINVAL;
2185 mutex_lock(&zr->resource_lock);
2187 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2189 zr->hue = vpict->hue;
2190 zr->contrast = vpict->contrast;
2191 zr->saturation = vpict->colour;
2192 zr->brightness = vpict->brightness;
2194 fh->overlay_settings.format = &zoran_formats[i];
2196 mutex_unlock(&zr->resource_lock);
2198 return 0;
2200 break;
2202 case VIDIOCCAPTURE:
2204 int *on = arg, res;
2206 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2207 ZR_DEVNAME(zr), *on);
2209 mutex_lock(&zr->resource_lock);
2210 res = setup_overlay(file, *on);
2211 mutex_unlock(&zr->resource_lock);
2213 return res;
2215 break;
2217 case VIDIOCGWIN:
2219 struct video_window *vwin = arg;
2221 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2223 memset(vwin, 0, sizeof(struct video_window));
2224 mutex_lock(&zr->resource_lock);
2225 vwin->x = fh->overlay_settings.x;
2226 vwin->y = fh->overlay_settings.y;
2227 vwin->width = fh->overlay_settings.width;
2228 vwin->height = fh->overlay_settings.height;
2229 mutex_unlock(&zr->resource_lock);
2230 vwin->clipcount = 0;
2231 return 0;
2233 break;
2235 case VIDIOCSWIN:
2237 struct video_window *vwin = arg;
2238 int res;
2240 dprintk(3,
2241 KERN_DEBUG
2242 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2243 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2244 vwin->height, vwin->clipcount);
2246 mutex_lock(&zr->resource_lock);
2247 res =
2248 setup_window(file, vwin->x, vwin->y, vwin->width,
2249 vwin->height, vwin->clips,
2250 vwin->clipcount, NULL);
2251 mutex_unlock(&zr->resource_lock);
2253 return res;
2255 break;
2257 case VIDIOCGFBUF:
2259 struct video_buffer *vbuf = arg;
2261 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2263 mutex_lock(&zr->resource_lock);
2264 *vbuf = zr->buffer;
2265 mutex_unlock(&zr->resource_lock);
2266 return 0;
2268 break;
2270 case VIDIOCSFBUF:
2272 struct video_buffer *vbuf = arg;
2273 int i, res = 0;
2275 dprintk(3,
2276 KERN_DEBUG
2277 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2278 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2279 vbuf->height, vbuf->depth, vbuf->bytesperline);
2281 for (i = 0; i < zoran_num_formats; i++)
2282 if (zoran_formats[i].depth == vbuf->depth)
2283 break;
2284 if (i == zoran_num_formats) {
2285 dprintk(1,
2286 KERN_ERR
2287 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2288 ZR_DEVNAME(zr), vbuf->depth);
2289 return -EINVAL;
2292 mutex_lock(&zr->resource_lock);
2293 res =
2294 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2295 vbuf->width, vbuf->height,
2296 vbuf->bytesperline);
2297 mutex_unlock(&zr->resource_lock);
2299 return res;
2301 break;
2303 case VIDIOCSYNC:
2305 int *frame = arg, res;
2307 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2308 ZR_DEVNAME(zr), *frame);
2310 mutex_lock(&zr->resource_lock);
2311 res = v4l_sync(file, *frame);
2312 mutex_unlock(&zr->resource_lock);
2313 if (!res)
2314 zr->v4l_sync_tail++;
2315 return res;
2317 break;
2319 case VIDIOCMCAPTURE:
2321 struct video_mmap *vmap = arg;
2322 int res;
2324 dprintk(3,
2325 KERN_DEBUG
2326 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2327 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2328 vmap->format);
2330 mutex_lock(&zr->resource_lock);
2331 res = v4l_grab(file, vmap);
2332 mutex_unlock(&zr->resource_lock);
2333 return res;
2335 break;
2337 case VIDIOCGMBUF:
2339 struct video_mbuf *vmbuf = arg;
2340 int i, res = 0;
2342 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2344 vmbuf->size =
2345 fh->v4l_buffers.num_buffers *
2346 fh->v4l_buffers.buffer_size;
2347 vmbuf->frames = fh->v4l_buffers.num_buffers;
2348 for (i = 0; i < vmbuf->frames; i++) {
2349 vmbuf->offsets[i] =
2350 i * fh->v4l_buffers.buffer_size;
2353 mutex_lock(&zr->resource_lock);
2355 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2356 dprintk(1,
2357 KERN_ERR
2358 "%s: VIDIOCGMBUF - buffers already allocated\n",
2359 ZR_DEVNAME(zr));
2360 res = -EINVAL;
2361 goto v4l1reqbuf_unlock_and_return;
2364 if (v4l_fbuffer_alloc(file)) {
2365 res = -ENOMEM;
2366 goto v4l1reqbuf_unlock_and_return;
2369 /* The next mmap will map the V4L buffers */
2370 fh->map_mode = ZORAN_MAP_MODE_RAW;
2371 v4l1reqbuf_unlock_and_return:
2372 mutex_unlock(&zr->resource_lock);
2374 return res;
2376 break;
2378 case VIDIOCGUNIT:
2380 struct video_unit *vunit = arg;
2382 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2384 vunit->video = zr->video_dev->minor;
2385 vunit->vbi = VIDEO_NO_UNIT;
2386 vunit->radio = VIDEO_NO_UNIT;
2387 vunit->audio = VIDEO_NO_UNIT;
2388 vunit->teletext = VIDEO_NO_UNIT;
2390 return 0;
2392 break;
2395 * RJ: In principal we could support subcaptures for V4L grabbing.
2396 * Not even the famous BTTV driver has them, however.
2397 * If there should be a strong demand, one could consider
2398 * to implement them.
2400 case VIDIOCGCAPTURE:
2402 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2403 ZR_DEVNAME(zr));
2404 return -EINVAL;
2406 break;
2408 case VIDIOCSCAPTURE:
2410 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2411 ZR_DEVNAME(zr));
2412 return -EINVAL;
2414 break;
2416 case BUZIOC_G_PARAMS:
2418 struct zoran_params *bparams = arg;
2420 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2422 memset(bparams, 0, sizeof(struct zoran_params));
2423 bparams->major_version = MAJOR_VERSION;
2424 bparams->minor_version = MINOR_VERSION;
2426 mutex_lock(&zr->resource_lock);
2428 bparams->norm = zr->norm;
2429 bparams->input = zr->input;
2431 bparams->decimation = fh->jpg_settings.decimation;
2432 bparams->HorDcm = fh->jpg_settings.HorDcm;
2433 bparams->VerDcm = fh->jpg_settings.VerDcm;
2434 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2435 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2436 bparams->img_x = fh->jpg_settings.img_x;
2437 bparams->img_y = fh->jpg_settings.img_y;
2438 bparams->img_width = fh->jpg_settings.img_width;
2439 bparams->img_height = fh->jpg_settings.img_height;
2440 bparams->odd_even = fh->jpg_settings.odd_even;
2442 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2443 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2444 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2445 memcpy(bparams->APP_data,
2446 fh->jpg_settings.jpg_comp.APP_data,
2447 sizeof(bparams->APP_data));
2448 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2449 memcpy(bparams->COM_data,
2450 fh->jpg_settings.jpg_comp.COM_data,
2451 sizeof(bparams->COM_data));
2452 bparams->jpeg_markers =
2453 fh->jpg_settings.jpg_comp.jpeg_markers;
2455 mutex_unlock(&zr->resource_lock);
2457 bparams->VFIFO_FB = 0;
2459 return 0;
2461 break;
2463 case BUZIOC_S_PARAMS:
2465 struct zoran_params *bparams = arg;
2466 int res = 0;
2468 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2470 settings.decimation = bparams->decimation;
2471 settings.HorDcm = bparams->HorDcm;
2472 settings.VerDcm = bparams->VerDcm;
2473 settings.TmpDcm = bparams->TmpDcm;
2474 settings.field_per_buff = bparams->field_per_buff;
2475 settings.img_x = bparams->img_x;
2476 settings.img_y = bparams->img_y;
2477 settings.img_width = bparams->img_width;
2478 settings.img_height = bparams->img_height;
2479 settings.odd_even = bparams->odd_even;
2481 settings.jpg_comp.quality = bparams->quality;
2482 settings.jpg_comp.APPn = bparams->APPn;
2483 settings.jpg_comp.APP_len = bparams->APP_len;
2484 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2485 sizeof(bparams->APP_data));
2486 settings.jpg_comp.COM_len = bparams->COM_len;
2487 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2488 sizeof(bparams->COM_data));
2489 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2491 mutex_lock(&zr->resource_lock);
2493 if (zr->codec_mode != BUZ_MODE_IDLE) {
2494 dprintk(1,
2495 KERN_ERR
2496 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2497 ZR_DEVNAME(zr));
2498 res = -EINVAL;
2499 goto sparams_unlock_and_return;
2502 /* Check the params first before overwriting our
2503 * nternal values */
2504 if (zoran_check_jpg_settings(zr, &settings)) {
2505 res = -EINVAL;
2506 goto sparams_unlock_and_return;
2509 fh->jpg_settings = settings;
2510 sparams_unlock_and_return:
2511 mutex_unlock(&zr->resource_lock);
2513 return res;
2515 break;
2517 case BUZIOC_REQBUFS:
2519 struct zoran_requestbuffers *breq = arg;
2520 int res = 0;
2522 dprintk(3,
2523 KERN_DEBUG
2524 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2525 ZR_DEVNAME(zr), breq->count, breq->size);
2527 /* Enforce reasonable lower and upper limits */
2528 if (breq->count < 4)
2529 breq->count = 4; /* Could be choosen smaller */
2530 if (breq->count > jpg_nbufs)
2531 breq->count = jpg_nbufs;
2532 breq->size = PAGE_ALIGN(breq->size);
2533 if (breq->size < 8192)
2534 breq->size = 8192; /* Arbitrary */
2535 /* breq->size is limited by 1 page for the stat_com
2536 * tables to a Maximum of 2 MB */
2537 if (breq->size > jpg_bufsize)
2538 breq->size = jpg_bufsize;
2539 if (fh->jpg_buffers.need_contiguous &&
2540 breq->size > MAX_KMALLOC_MEM)
2541 breq->size = MAX_KMALLOC_MEM;
2543 mutex_lock(&zr->resource_lock);
2545 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2546 dprintk(1,
2547 KERN_ERR
2548 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2549 ZR_DEVNAME(zr));
2550 res = -EBUSY;
2551 goto jpgreqbuf_unlock_and_return;
2554 fh->jpg_buffers.num_buffers = breq->count;
2555 fh->jpg_buffers.buffer_size = breq->size;
2557 if (jpg_fbuffer_alloc(file)) {
2558 res = -ENOMEM;
2559 goto jpgreqbuf_unlock_and_return;
2562 /* The next mmap will map the MJPEG buffers - could
2563 * also be *_PLAY, but it doesn't matter here */
2564 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2565 jpgreqbuf_unlock_and_return:
2566 mutex_unlock(&zr->resource_lock);
2568 return res;
2570 break;
2572 case BUZIOC_QBUF_CAPT:
2574 int *frame = arg, res;
2576 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2577 ZR_DEVNAME(zr), *frame);
2579 mutex_lock(&zr->resource_lock);
2580 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2581 mutex_unlock(&zr->resource_lock);
2583 return res;
2585 break;
2587 case BUZIOC_QBUF_PLAY:
2589 int *frame = arg, res;
2591 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2592 ZR_DEVNAME(zr), *frame);
2594 mutex_lock(&zr->resource_lock);
2595 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2596 mutex_unlock(&zr->resource_lock);
2598 return res;
2600 break;
2602 case BUZIOC_SYNC:
2604 struct zoran_sync *bsync = arg;
2605 int res;
2607 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2609 mutex_lock(&zr->resource_lock);
2610 res = jpg_sync(file, bsync);
2611 mutex_unlock(&zr->resource_lock);
2613 return res;
2615 break;
2617 case BUZIOC_G_STATUS:
2619 struct zoran_status *bstat = arg;
2620 int norm, input, status, res = 0;
2622 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2624 if (zr->codec_mode != BUZ_MODE_IDLE) {
2625 dprintk(1,
2626 KERN_ERR
2627 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2628 ZR_DEVNAME(zr));
2629 return -EINVAL;
2632 input = zr->card.input[bstat->input].muxsel;
2633 norm = VIDEO_MODE_AUTO;
2635 mutex_lock(&zr->resource_lock);
2637 if (zr->codec_mode != BUZ_MODE_IDLE) {
2638 dprintk(1,
2639 KERN_ERR
2640 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2641 ZR_DEVNAME(zr));
2642 res = -EINVAL;
2643 goto gstat_unlock_and_return;
2646 decoder_command(zr, DECODER_SET_INPUT, &input);
2647 decoder_command(zr, DECODER_SET_NORM, &norm);
2649 /* sleep 1 second */
2650 ssleep(1);
2652 /* Get status of video decoder */
2653 decoder_command(zr, DECODER_GET_STATUS, &status);
2655 /* restore previous input and norm */
2656 input = zr->card.input[zr->input].muxsel;
2657 decoder_command(zr, DECODER_SET_INPUT, &input);
2658 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2659 gstat_unlock_and_return:
2660 mutex_unlock(&zr->resource_lock);
2662 if (!res) {
2663 bstat->signal =
2664 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2665 if (status & DECODER_STATUS_NTSC)
2666 bstat->norm = VIDEO_MODE_NTSC;
2667 else if (status & DECODER_STATUS_SECAM)
2668 bstat->norm = VIDEO_MODE_SECAM;
2669 else
2670 bstat->norm = VIDEO_MODE_PAL;
2672 bstat->color =
2673 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2676 return res;
2678 break;
2680 #ifdef HAVE_V4L2
2682 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2683 * it allows for integrating the JPEG capturing calls inside standard v4l2
2686 case VIDIOC_QUERYCAP:
2688 struct v4l2_capability *cap = arg;
2690 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2692 memset(cap, 0, sizeof(*cap));
2693 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2694 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2695 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2696 pci_name(zr->pci_dev));
2697 cap->version =
2698 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2699 RELEASE_VERSION);
2700 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2702 return 0;
2704 break;
2706 case VIDIOC_ENUM_FMT:
2708 struct v4l2_fmtdesc *fmt = arg;
2709 int index = fmt->index, num = -1, i, flag = 0, type =
2710 fmt->type;
2712 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2713 ZR_DEVNAME(zr), fmt->index);
2715 switch (fmt->type) {
2716 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2717 flag = ZORAN_FORMAT_CAPTURE;
2718 break;
2719 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2720 flag = ZORAN_FORMAT_PLAYBACK;
2721 break;
2722 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2723 flag = ZORAN_FORMAT_OVERLAY;
2724 break;
2725 default:
2726 dprintk(1,
2727 KERN_ERR
2728 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2729 ZR_DEVNAME(zr), fmt->type);
2730 return -EINVAL;
2733 for (i = 0; i < zoran_num_formats; i++) {
2734 if (zoran_formats[i].flags & flag)
2735 num++;
2736 if (num == fmt->index)
2737 break;
2739 if (fmt->index < 0 /* late, but not too late */ ||
2740 i == zoran_num_formats)
2741 return -EINVAL;
2743 memset(fmt, 0, sizeof(*fmt));
2744 fmt->index = index;
2745 fmt->type = type;
2746 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2747 fmt->pixelformat = zoran_formats[i].fourcc;
2748 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2749 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2751 return 0;
2753 break;
2755 case VIDIOC_G_FMT:
2757 struct v4l2_format *fmt = arg;
2758 int type = fmt->type;
2760 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2762 memset(fmt, 0, sizeof(*fmt));
2763 fmt->type = type;
2765 switch (fmt->type) {
2766 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2768 mutex_lock(&zr->resource_lock);
2770 fmt->fmt.win.w.left = fh->overlay_settings.x;
2771 fmt->fmt.win.w.top = fh->overlay_settings.y;
2772 fmt->fmt.win.w.width = fh->overlay_settings.width;
2773 fmt->fmt.win.w.height =
2774 fh->overlay_settings.height;
2775 if (fh->overlay_settings.width * 2 >
2776 BUZ_MAX_HEIGHT)
2777 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2778 else
2779 fmt->fmt.win.field = V4L2_FIELD_TOP;
2781 mutex_unlock(&zr->resource_lock);
2783 break;
2785 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2786 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2788 mutex_lock(&zr->resource_lock);
2790 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2791 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2793 fmt->fmt.pix.width =
2794 fh->v4l_settings.width;
2795 fmt->fmt.pix.height =
2796 fh->v4l_settings.height;
2797 fmt->fmt.pix.sizeimage =
2798 fh->v4l_buffers.buffer_size;
2799 fmt->fmt.pix.pixelformat =
2800 fh->v4l_settings.format->fourcc;
2801 fmt->fmt.pix.colorspace =
2802 fh->v4l_settings.format->colorspace;
2803 fmt->fmt.pix.bytesperline = 0;
2804 if (BUZ_MAX_HEIGHT <
2805 (fh->v4l_settings.height * 2))
2806 fmt->fmt.pix.field =
2807 V4L2_FIELD_INTERLACED;
2808 else
2809 fmt->fmt.pix.field =
2810 V4L2_FIELD_TOP;
2812 } else {
2814 fmt->fmt.pix.width =
2815 fh->jpg_settings.img_width /
2816 fh->jpg_settings.HorDcm;
2817 fmt->fmt.pix.height =
2818 fh->jpg_settings.img_height /
2819 (fh->jpg_settings.VerDcm *
2820 fh->jpg_settings.TmpDcm);
2821 fmt->fmt.pix.sizeimage =
2822 zoran_v4l2_calc_bufsize(&fh->
2823 jpg_settings);
2824 fmt->fmt.pix.pixelformat =
2825 V4L2_PIX_FMT_MJPEG;
2826 if (fh->jpg_settings.TmpDcm == 1)
2827 fmt->fmt.pix.field =
2828 (fh->jpg_settings.
2829 odd_even ? V4L2_FIELD_SEQ_BT :
2830 V4L2_FIELD_SEQ_BT);
2831 else
2832 fmt->fmt.pix.field =
2833 (fh->jpg_settings.
2834 odd_even ? V4L2_FIELD_TOP :
2835 V4L2_FIELD_BOTTOM);
2837 fmt->fmt.pix.bytesperline = 0;
2838 fmt->fmt.pix.colorspace =
2839 V4L2_COLORSPACE_SMPTE170M;
2842 mutex_unlock(&zr->resource_lock);
2844 break;
2846 default:
2847 dprintk(1,
2848 KERN_ERR
2849 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2850 ZR_DEVNAME(zr), fmt->type);
2851 return -EINVAL;
2853 return 0;
2855 break;
2857 case VIDIOC_S_FMT:
2859 struct v4l2_format *fmt = arg;
2860 int i, res = 0;
2861 __u32 printformat;
2863 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2864 ZR_DEVNAME(zr), fmt->type);
2866 switch (fmt->type) {
2867 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2869 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2870 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2871 fmt->fmt.win.w.width,
2872 fmt->fmt.win.w.height,
2873 fmt->fmt.win.clipcount,
2874 fmt->fmt.win.bitmap);
2875 mutex_lock(&zr->resource_lock);
2876 res =
2877 setup_window(file, fmt->fmt.win.w.left,
2878 fmt->fmt.win.w.top,
2879 fmt->fmt.win.w.width,
2880 fmt->fmt.win.w.height,
2881 (struct video_clip __user *)
2882 fmt->fmt.win.clips,
2883 fmt->fmt.win.clipcount,
2884 fmt->fmt.win.bitmap);
2885 mutex_unlock(&zr->resource_lock);
2886 return res;
2887 break;
2889 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2890 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2892 printformat =
2893 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2894 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2895 fmt->fmt.pix.width, fmt->fmt.pix.height,
2896 fmt->fmt.pix.pixelformat,
2897 (char *) &printformat);
2899 if (fmt->fmt.pix.bytesperline > 0) {
2900 dprintk(5,
2901 KERN_ERR "%s: bpl not supported\n",
2902 ZR_DEVNAME(zr));
2903 return -EINVAL;
2906 /* we can be requested to do JPEG/raw playback/capture */
2907 if (!
2908 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2909 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2910 fmt->fmt.pix.pixelformat ==
2911 V4L2_PIX_FMT_MJPEG))) {
2912 dprintk(1,
2913 KERN_ERR
2914 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2915 ZR_DEVNAME(zr), fmt->type,
2916 fmt->fmt.pix.pixelformat,
2917 (char *) &printformat);
2918 return -EINVAL;
2921 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2922 mutex_lock(&zr->resource_lock);
2924 settings = fh->jpg_settings;
2926 if (fh->v4l_buffers.allocated ||
2927 fh->jpg_buffers.allocated) {
2928 dprintk(1,
2929 KERN_ERR
2930 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2931 ZR_DEVNAME(zr));
2932 res = -EBUSY;
2933 goto sfmtjpg_unlock_and_return;
2936 /* we actually need to set 'real' parameters now */
2937 if ((fmt->fmt.pix.height * 2) >
2938 BUZ_MAX_HEIGHT)
2939 settings.TmpDcm = 1;
2940 else
2941 settings.TmpDcm = 2;
2942 settings.decimation = 0;
2943 if (fmt->fmt.pix.height <=
2944 fh->jpg_settings.img_height / 2)
2945 settings.VerDcm = 2;
2946 else
2947 settings.VerDcm = 1;
2948 if (fmt->fmt.pix.width <=
2949 fh->jpg_settings.img_width / 4)
2950 settings.HorDcm = 4;
2951 else if (fmt->fmt.pix.width <=
2952 fh->jpg_settings.img_width / 2)
2953 settings.HorDcm = 2;
2954 else
2955 settings.HorDcm = 1;
2956 if (settings.TmpDcm == 1)
2957 settings.field_per_buff = 2;
2958 else
2959 settings.field_per_buff = 1;
2961 /* check */
2962 if ((res =
2963 zoran_check_jpg_settings(zr,
2964 &settings)))
2965 goto sfmtjpg_unlock_and_return;
2967 /* it's ok, so set them */
2968 fh->jpg_settings = settings;
2970 /* tell the user what we actually did */
2971 fmt->fmt.pix.width =
2972 settings.img_width / settings.HorDcm;
2973 fmt->fmt.pix.height =
2974 settings.img_height * 2 /
2975 (settings.TmpDcm * settings.VerDcm);
2976 if (settings.TmpDcm == 1)
2977 fmt->fmt.pix.field =
2978 (fh->jpg_settings.
2979 odd_even ? V4L2_FIELD_SEQ_TB :
2980 V4L2_FIELD_SEQ_BT);
2981 else
2982 fmt->fmt.pix.field =
2983 (fh->jpg_settings.
2984 odd_even ? V4L2_FIELD_TOP :
2985 V4L2_FIELD_BOTTOM);
2986 fh->jpg_buffers.buffer_size =
2987 zoran_v4l2_calc_bufsize(&fh->
2988 jpg_settings);
2989 fmt->fmt.pix.sizeimage =
2990 fh->jpg_buffers.buffer_size;
2992 /* we hereby abuse this variable to show that
2993 * we're gonna do mjpeg capture */
2994 fh->map_mode =
2995 (fmt->type ==
2996 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2997 ZORAN_MAP_MODE_JPG_REC :
2998 ZORAN_MAP_MODE_JPG_PLAY;
2999 sfmtjpg_unlock_and_return:
3000 mutex_unlock(&zr->resource_lock);
3001 } else {
3002 for (i = 0; i < zoran_num_formats; i++)
3003 if (fmt->fmt.pix.pixelformat ==
3004 zoran_formats[i].fourcc)
3005 break;
3006 if (i == zoran_num_formats) {
3007 dprintk(1,
3008 KERN_ERR
3009 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3010 ZR_DEVNAME(zr),
3011 fmt->fmt.pix.pixelformat,
3012 (char *) &printformat);
3013 return -EINVAL;
3015 mutex_lock(&zr->resource_lock);
3016 if (fh->jpg_buffers.allocated ||
3017 (fh->v4l_buffers.allocated &&
3018 fh->v4l_buffers.active !=
3019 ZORAN_FREE)) {
3020 dprintk(1,
3021 KERN_ERR
3022 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3023 ZR_DEVNAME(zr));
3024 res = -EBUSY;
3025 goto sfmtv4l_unlock_and_return;
3027 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3028 fmt->fmt.pix.height =
3029 BUZ_MAX_HEIGHT;
3030 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3031 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3033 if ((res =
3034 zoran_v4l_set_format(file,
3035 fmt->fmt.pix.
3036 width,
3037 fmt->fmt.pix.
3038 height,
3039 &zoran_formats
3040 [i])))
3041 goto sfmtv4l_unlock_and_return;
3043 /* tell the user the
3044 * results/missing stuff */
3045 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3047 if (BUZ_MAX_HEIGHT <
3048 (fh->v4l_settings.height * 2))
3049 fmt->fmt.pix.field =
3050 V4L2_FIELD_INTERLACED;
3051 else
3052 fmt->fmt.pix.field =
3053 V4L2_FIELD_TOP;
3055 fh->map_mode = ZORAN_MAP_MODE_RAW;
3056 sfmtv4l_unlock_and_return:
3057 mutex_unlock(&zr->resource_lock);
3060 break;
3062 default:
3063 dprintk(3, "unsupported\n");
3064 dprintk(1,
3065 KERN_ERR
3066 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3067 ZR_DEVNAME(zr), fmt->type);
3068 return -EINVAL;
3071 return res;
3073 break;
3075 case VIDIOC_G_FBUF:
3077 struct v4l2_framebuffer *fb = arg;
3079 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3081 memset(fb, 0, sizeof(*fb));
3082 mutex_lock(&zr->resource_lock);
3083 fb->base = zr->buffer.base;
3084 fb->fmt.width = zr->buffer.width;
3085 fb->fmt.height = zr->buffer.height;
3086 if (zr->overlay_settings.format) {
3087 fb->fmt.pixelformat =
3088 fh->overlay_settings.format->fourcc;
3090 fb->fmt.bytesperline = zr->buffer.bytesperline;
3091 mutex_unlock(&zr->resource_lock);
3092 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3093 fb->fmt.field = V4L2_FIELD_INTERLACED;
3094 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3095 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3097 return 0;
3099 break;
3101 case VIDIOC_S_FBUF:
3103 int i, res = 0;
3104 struct v4l2_framebuffer *fb = arg;
3105 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3107 dprintk(3,
3108 KERN_DEBUG
3109 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3110 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3111 fb->fmt.bytesperline, fb->fmt.pixelformat,
3112 (char *) &printformat);
3114 for (i = 0; i < zoran_num_formats; i++)
3115 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3116 break;
3117 if (i == zoran_num_formats) {
3118 dprintk(1,
3119 KERN_ERR
3120 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3121 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3122 (char *) &printformat);
3123 return -EINVAL;
3126 mutex_lock(&zr->resource_lock);
3127 res =
3128 setup_fbuffer(file, fb->base, &zoran_formats[i],
3129 fb->fmt.width, fb->fmt.height,
3130 fb->fmt.bytesperline);
3131 mutex_unlock(&zr->resource_lock);
3133 return res;
3135 break;
3137 case VIDIOC_OVERLAY:
3139 int *on = arg, res;
3141 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3142 ZR_DEVNAME(zr), *on);
3144 mutex_lock(&zr->resource_lock);
3145 res = setup_overlay(file, *on);
3146 mutex_unlock(&zr->resource_lock);
3148 return res;
3150 break;
3152 case VIDIOC_REQBUFS:
3154 struct v4l2_requestbuffers *req = arg;
3155 int res = 0;
3157 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3158 ZR_DEVNAME(zr), req->type);
3160 if (req->memory != V4L2_MEMORY_MMAP) {
3161 dprintk(1,
3162 KERN_ERR
3163 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3164 ZR_DEVNAME(zr), req->memory);
3165 return -EINVAL;
3168 mutex_lock(&zr->resource_lock);
3170 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3171 dprintk(1,
3172 KERN_ERR
3173 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3174 ZR_DEVNAME(zr));
3175 res = -EBUSY;
3176 goto v4l2reqbuf_unlock_and_return;
3179 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3180 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3182 /* control user input */
3183 if (req->count < 2)
3184 req->count = 2;
3185 if (req->count > v4l_nbufs)
3186 req->count = v4l_nbufs;
3187 fh->v4l_buffers.num_buffers = req->count;
3189 if (v4l_fbuffer_alloc(file)) {
3190 res = -ENOMEM;
3191 goto v4l2reqbuf_unlock_and_return;
3194 /* The next mmap will map the V4L buffers */
3195 fh->map_mode = ZORAN_MAP_MODE_RAW;
3197 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3198 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3200 /* we need to calculate size ourselves now */
3201 if (req->count < 4)
3202 req->count = 4;
3203 if (req->count > jpg_nbufs)
3204 req->count = jpg_nbufs;
3205 fh->jpg_buffers.num_buffers = req->count;
3206 fh->jpg_buffers.buffer_size =
3207 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3209 if (jpg_fbuffer_alloc(file)) {
3210 res = -ENOMEM;
3211 goto v4l2reqbuf_unlock_and_return;
3214 /* The next mmap will map the MJPEG buffers */
3215 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3216 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3217 else
3218 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3220 } else {
3221 dprintk(1,
3222 KERN_ERR
3223 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3224 ZR_DEVNAME(zr), req->type);
3225 res = -EINVAL;
3226 goto v4l2reqbuf_unlock_and_return;
3228 v4l2reqbuf_unlock_and_return:
3229 mutex_unlock(&zr->resource_lock);
3231 return 0;
3233 break;
3235 case VIDIOC_QUERYBUF:
3237 struct v4l2_buffer *buf = arg;
3238 __u32 type = buf->type;
3239 int index = buf->index, res;
3241 dprintk(3,
3242 KERN_DEBUG
3243 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3244 ZR_DEVNAME(zr), buf->index, buf->type);
3246 memset(buf, 0, sizeof(buf));
3247 buf->type = type;
3248 buf->index = index;
3250 mutex_lock(&zr->resource_lock);
3251 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3252 mutex_unlock(&zr->resource_lock);
3254 return res;
3256 break;
3258 case VIDIOC_QBUF:
3260 struct v4l2_buffer *buf = arg;
3261 int res = 0, codec_mode, buf_type;
3263 dprintk(3,
3264 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3265 ZR_DEVNAME(zr), buf->type, buf->index);
3267 mutex_lock(&zr->resource_lock);
3269 switch (fh->map_mode) {
3270 case ZORAN_MAP_MODE_RAW:
3271 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3272 dprintk(1,
3273 KERN_ERR
3274 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3275 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3276 res = -EINVAL;
3277 goto qbuf_unlock_and_return;
3280 res = zoran_v4l_queue_frame(file, buf->index);
3281 if (res)
3282 goto qbuf_unlock_and_return;
3283 if (!zr->v4l_memgrab_active &&
3284 fh->v4l_buffers.active == ZORAN_LOCKED)
3285 zr36057_set_memgrab(zr, 1);
3286 break;
3288 case ZORAN_MAP_MODE_JPG_REC:
3289 case ZORAN_MAP_MODE_JPG_PLAY:
3290 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3291 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3292 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3293 } else {
3294 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3295 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3298 if (buf->type != buf_type) {
3299 dprintk(1,
3300 KERN_ERR
3301 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3302 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3303 res = -EINVAL;
3304 goto qbuf_unlock_and_return;
3307 res =
3308 zoran_jpg_queue_frame(file, buf->index,
3309 codec_mode);
3310 if (res != 0)
3311 goto qbuf_unlock_and_return;
3312 if (zr->codec_mode == BUZ_MODE_IDLE &&
3313 fh->jpg_buffers.active == ZORAN_LOCKED) {
3314 zr36057_enable_jpg(zr, codec_mode);
3316 break;
3318 default:
3319 dprintk(1,
3320 KERN_ERR
3321 "%s: VIDIOC_QBUF - unsupported type %d\n",
3322 ZR_DEVNAME(zr), buf->type);
3323 res = -EINVAL;
3324 goto qbuf_unlock_and_return;
3326 qbuf_unlock_and_return:
3327 mutex_unlock(&zr->resource_lock);
3329 return res;
3331 break;
3333 case VIDIOC_DQBUF:
3335 struct v4l2_buffer *buf = arg;
3336 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3338 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3339 ZR_DEVNAME(zr), buf->type);
3341 mutex_lock(&zr->resource_lock);
3343 switch (fh->map_mode) {
3344 case ZORAN_MAP_MODE_RAW:
3345 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3346 dprintk(1,
3347 KERN_ERR
3348 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3349 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3350 res = -EINVAL;
3351 goto dqbuf_unlock_and_return;
3354 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3355 if (file->f_flags & O_NONBLOCK &&
3356 zr->v4l_buffers.buffer[num].state !=
3357 BUZ_STATE_DONE) {
3358 res = -EAGAIN;
3359 goto dqbuf_unlock_and_return;
3361 res = v4l_sync(file, num);
3362 if (res)
3363 goto dqbuf_unlock_and_return;
3364 else
3365 zr->v4l_sync_tail++;
3366 res = zoran_v4l2_buffer_status(file, buf, num);
3367 break;
3369 case ZORAN_MAP_MODE_JPG_REC:
3370 case ZORAN_MAP_MODE_JPG_PLAY:
3372 struct zoran_sync bs;
3374 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3375 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3376 else
3377 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3379 if (buf->type != buf_type) {
3380 dprintk(1,
3381 KERN_ERR
3382 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3383 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3384 res = -EINVAL;
3385 goto dqbuf_unlock_and_return;
3388 num =
3389 zr->jpg_pend[zr->
3390 jpg_que_tail & BUZ_MASK_FRAME];
3392 if (file->f_flags & O_NONBLOCK &&
3393 zr->jpg_buffers.buffer[num].state !=
3394 BUZ_STATE_DONE) {
3395 res = -EAGAIN;
3396 goto dqbuf_unlock_and_return;
3398 res = jpg_sync(file, &bs);
3399 if (res)
3400 goto dqbuf_unlock_and_return;
3401 res =
3402 zoran_v4l2_buffer_status(file, buf, bs.frame);
3403 break;
3406 default:
3407 dprintk(1,
3408 KERN_ERR
3409 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3410 ZR_DEVNAME(zr), buf->type);
3411 res = -EINVAL;
3412 goto dqbuf_unlock_and_return;
3414 dqbuf_unlock_and_return:
3415 mutex_unlock(&zr->resource_lock);
3417 return res;
3419 break;
3421 case VIDIOC_STREAMON:
3423 int res = 0;
3425 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3427 mutex_lock(&zr->resource_lock);
3429 switch (fh->map_mode) {
3430 case ZORAN_MAP_MODE_RAW: /* raw capture */
3431 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3432 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3433 res = -EBUSY;
3434 goto strmon_unlock_and_return;
3437 zr->v4l_buffers.active = fh->v4l_buffers.active =
3438 ZORAN_LOCKED;
3439 zr->v4l_settings = fh->v4l_settings;
3441 zr->v4l_sync_tail = zr->v4l_pend_tail;
3442 if (!zr->v4l_memgrab_active &&
3443 zr->v4l_pend_head != zr->v4l_pend_tail) {
3444 zr36057_set_memgrab(zr, 1);
3446 break;
3448 case ZORAN_MAP_MODE_JPG_REC:
3449 case ZORAN_MAP_MODE_JPG_PLAY:
3450 /* what is the codec mode right now? */
3451 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3452 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3453 res = -EBUSY;
3454 goto strmon_unlock_and_return;
3457 zr->jpg_buffers.active = fh->jpg_buffers.active =
3458 ZORAN_LOCKED;
3460 if (zr->jpg_que_head != zr->jpg_que_tail) {
3461 /* Start the jpeg codec when the first frame is queued */
3462 jpeg_start(zr);
3465 break;
3466 default:
3467 dprintk(1,
3468 KERN_ERR
3469 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3470 ZR_DEVNAME(zr), fh->map_mode);
3471 res = -EINVAL;
3472 goto strmon_unlock_and_return;
3474 strmon_unlock_and_return:
3475 mutex_unlock(&zr->resource_lock);
3477 return res;
3479 break;
3481 case VIDIOC_STREAMOFF:
3483 int i, res = 0;
3485 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3487 mutex_lock(&zr->resource_lock);
3489 switch (fh->map_mode) {
3490 case ZORAN_MAP_MODE_RAW: /* raw capture */
3491 if (fh->v4l_buffers.active == ZORAN_FREE &&
3492 zr->v4l_buffers.active != ZORAN_FREE) {
3493 res = -EPERM; /* stay off other's settings! */
3494 goto strmoff_unlock_and_return;
3496 if (zr->v4l_buffers.active == ZORAN_FREE)
3497 goto strmoff_unlock_and_return;
3499 /* unload capture */
3500 if (zr->v4l_memgrab_active)
3501 zr36057_set_memgrab(zr, 0);
3503 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3504 zr->v4l_buffers.buffer[i].state =
3505 BUZ_STATE_USER;
3506 fh->v4l_buffers = zr->v4l_buffers;
3508 zr->v4l_buffers.active = fh->v4l_buffers.active =
3509 ZORAN_FREE;
3511 zr->v4l_grab_seq = 0;
3512 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3513 zr->v4l_sync_tail = 0;
3515 break;
3517 case ZORAN_MAP_MODE_JPG_REC:
3518 case ZORAN_MAP_MODE_JPG_PLAY:
3519 if (fh->jpg_buffers.active == ZORAN_FREE &&
3520 zr->jpg_buffers.active != ZORAN_FREE) {
3521 res = -EPERM; /* stay off other's settings! */
3522 goto strmoff_unlock_and_return;
3524 if (zr->jpg_buffers.active == ZORAN_FREE)
3525 goto strmoff_unlock_and_return;
3527 res =
3528 jpg_qbuf(file, -1,
3529 (fh->map_mode ==
3530 ZORAN_MAP_MODE_JPG_REC) ?
3531 BUZ_MODE_MOTION_COMPRESS :
3532 BUZ_MODE_MOTION_DECOMPRESS);
3533 if (res)
3534 goto strmoff_unlock_and_return;
3535 break;
3536 default:
3537 dprintk(1,
3538 KERN_ERR
3539 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3540 ZR_DEVNAME(zr), fh->map_mode);
3541 res = -EINVAL;
3542 goto strmoff_unlock_and_return;
3544 strmoff_unlock_and_return:
3545 mutex_unlock(&zr->resource_lock);
3547 return res;
3549 break;
3551 case VIDIOC_QUERYCTRL:
3553 struct v4l2_queryctrl *ctrl = arg;
3555 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3556 ZR_DEVNAME(zr), ctrl->id);
3558 /* we only support hue/saturation/contrast/brightness */
3559 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3560 ctrl->id > V4L2_CID_HUE)
3561 return -EINVAL;
3562 else {
3563 int id = ctrl->id;
3564 memset(ctrl, 0, sizeof(*ctrl));
3565 ctrl->id = id;
3568 switch (ctrl->id) {
3569 case V4L2_CID_BRIGHTNESS:
3570 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3571 break;
3572 case V4L2_CID_CONTRAST:
3573 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3574 break;
3575 case V4L2_CID_SATURATION:
3576 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3577 break;
3578 case V4L2_CID_HUE:
3579 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3580 break;
3583 ctrl->minimum = 0;
3584 ctrl->maximum = 65535;
3585 ctrl->step = 1;
3586 ctrl->default_value = 32768;
3587 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3589 return 0;
3591 break;
3593 case VIDIOC_G_CTRL:
3595 struct v4l2_control *ctrl = arg;
3597 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3598 ZR_DEVNAME(zr), ctrl->id);
3600 /* we only support hue/saturation/contrast/brightness */
3601 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3602 ctrl->id > V4L2_CID_HUE)
3603 return -EINVAL;
3605 mutex_lock(&zr->resource_lock);
3606 switch (ctrl->id) {
3607 case V4L2_CID_BRIGHTNESS:
3608 ctrl->value = zr->brightness;
3609 break;
3610 case V4L2_CID_CONTRAST:
3611 ctrl->value = zr->contrast;
3612 break;
3613 case V4L2_CID_SATURATION:
3614 ctrl->value = zr->saturation;
3615 break;
3616 case V4L2_CID_HUE:
3617 ctrl->value = zr->hue;
3618 break;
3620 mutex_unlock(&zr->resource_lock);
3622 return 0;
3624 break;
3626 case VIDIOC_S_CTRL:
3628 struct v4l2_control *ctrl = arg;
3629 struct video_picture pict;
3631 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3632 ZR_DEVNAME(zr), ctrl->id);
3634 /* we only support hue/saturation/contrast/brightness */
3635 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3636 ctrl->id > V4L2_CID_HUE)
3637 return -EINVAL;
3639 if (ctrl->value < 0 || ctrl->value > 65535) {
3640 dprintk(1,
3641 KERN_ERR
3642 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3643 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3644 return -EINVAL;
3647 mutex_lock(&zr->resource_lock);
3648 switch (ctrl->id) {
3649 case V4L2_CID_BRIGHTNESS:
3650 zr->brightness = ctrl->value;
3651 break;
3652 case V4L2_CID_CONTRAST:
3653 zr->contrast = ctrl->value;
3654 break;
3655 case V4L2_CID_SATURATION:
3656 zr->saturation = ctrl->value;
3657 break;
3658 case V4L2_CID_HUE:
3659 zr->hue = ctrl->value;
3660 break;
3662 pict.brightness = zr->brightness;
3663 pict.contrast = zr->contrast;
3664 pict.colour = zr->saturation;
3665 pict.hue = zr->hue;
3667 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3669 mutex_unlock(&zr->resource_lock);
3671 return 0;
3673 break;
3675 case VIDIOC_ENUMSTD:
3677 struct v4l2_standard *std = arg;
3679 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3680 ZR_DEVNAME(zr), std->index);
3682 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3683 return -EINVAL;
3684 else {
3685 int id = std->index;
3686 memset(std, 0, sizeof(*std));
3687 std->index = id;
3690 if (std->index == zr->card.norms) {
3691 /* if we have autodetect, ... */
3692 struct video_decoder_capability caps;
3693 decoder_command(zr, DECODER_GET_CAPABILITIES,
3694 &caps);
3695 if (caps.flags & VIDEO_DECODER_AUTO) {
3696 std->id = V4L2_STD_ALL;
3697 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3698 return 0;
3699 } else
3700 return -EINVAL;
3702 switch (std->index) {
3703 case 0:
3704 std->id = V4L2_STD_PAL;
3705 strncpy(std->name, "PAL", sizeof(std->name)-1);
3706 std->frameperiod.numerator = 1;
3707 std->frameperiod.denominator = 25;
3708 std->framelines = zr->card.tvn[0]->Ht;
3709 break;
3710 case 1:
3711 std->id = V4L2_STD_NTSC;
3712 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3713 std->frameperiod.numerator = 1001;
3714 std->frameperiod.denominator = 30000;
3715 std->framelines = zr->card.tvn[1]->Ht;
3716 break;
3717 case 2:
3718 std->id = V4L2_STD_SECAM;
3719 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3720 std->frameperiod.numerator = 1;
3721 std->frameperiod.denominator = 25;
3722 std->framelines = zr->card.tvn[2]->Ht;
3723 break;
3726 return 0;
3728 break;
3730 case VIDIOC_G_STD:
3732 v4l2_std_id *std = arg;
3733 int norm;
3735 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3737 mutex_lock(&zr->resource_lock);
3738 norm = zr->norm;
3739 mutex_unlock(&zr->resource_lock);
3741 switch (norm) {
3742 case VIDEO_MODE_PAL:
3743 *std = V4L2_STD_PAL;
3744 break;
3745 case VIDEO_MODE_NTSC:
3746 *std = V4L2_STD_NTSC;
3747 break;
3748 case VIDEO_MODE_SECAM:
3749 *std = V4L2_STD_SECAM;
3750 break;
3753 return 0;
3755 break;
3757 case VIDIOC_S_STD:
3759 int norm = -1, res = 0;
3760 v4l2_std_id *std = arg;
3762 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3763 ZR_DEVNAME(zr), (unsigned long long)*std);
3765 if (*std == V4L2_STD_PAL)
3766 norm = VIDEO_MODE_PAL;
3767 else if (*std == V4L2_STD_NTSC)
3768 norm = VIDEO_MODE_NTSC;
3769 else if (*std == V4L2_STD_SECAM)
3770 norm = VIDEO_MODE_SECAM;
3771 else if (*std == V4L2_STD_ALL)
3772 norm = VIDEO_MODE_AUTO;
3773 else {
3774 dprintk(1,
3775 KERN_ERR
3776 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3777 ZR_DEVNAME(zr), (unsigned long long)*std);
3778 return -EINVAL;
3781 mutex_lock(&zr->resource_lock);
3782 if ((res = zoran_set_norm(zr, norm)))
3783 goto sstd_unlock_and_return;
3785 res = wait_grab_pending(zr);
3786 sstd_unlock_and_return:
3787 mutex_unlock(&zr->resource_lock);
3788 return res;
3790 break;
3792 case VIDIOC_ENUMINPUT:
3794 struct v4l2_input *inp = arg;
3795 int status;
3797 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3798 ZR_DEVNAME(zr), inp->index);
3800 if (inp->index < 0 || inp->index >= zr->card.inputs)
3801 return -EINVAL;
3802 else {
3803 int id = inp->index;
3804 memset(inp, 0, sizeof(*inp));
3805 inp->index = id;
3808 strncpy(inp->name, zr->card.input[inp->index].name,
3809 sizeof(inp->name) - 1);
3810 inp->type = V4L2_INPUT_TYPE_CAMERA;
3811 inp->std = V4L2_STD_ALL;
3813 /* Get status of video decoder */
3814 mutex_lock(&zr->resource_lock);
3815 decoder_command(zr, DECODER_GET_STATUS, &status);
3816 mutex_unlock(&zr->resource_lock);
3818 if (!(status & DECODER_STATUS_GOOD)) {
3819 inp->status |= V4L2_IN_ST_NO_POWER;
3820 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3822 if (!(status & DECODER_STATUS_COLOR))
3823 inp->status |= V4L2_IN_ST_NO_COLOR;
3825 return 0;
3827 break;
3829 case VIDIOC_G_INPUT:
3831 int *input = arg;
3833 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3835 mutex_lock(&zr->resource_lock);
3836 *input = zr->input;
3837 mutex_unlock(&zr->resource_lock);
3839 return 0;
3841 break;
3843 case VIDIOC_S_INPUT:
3845 int *input = arg, res = 0;
3847 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3848 ZR_DEVNAME(zr), *input);
3850 mutex_lock(&zr->resource_lock);
3851 if ((res = zoran_set_input(zr, *input)))
3852 goto sinput_unlock_and_return;
3854 /* Make sure the changes come into effect */
3855 res = wait_grab_pending(zr);
3856 sinput_unlock_and_return:
3857 mutex_unlock(&zr->resource_lock);
3858 return res;
3860 break;
3862 case VIDIOC_ENUMOUTPUT:
3864 struct v4l2_output *outp = arg;
3866 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3867 ZR_DEVNAME(zr), outp->index);
3869 if (outp->index != 0)
3870 return -EINVAL;
3872 memset(outp, 0, sizeof(*outp));
3873 outp->index = 0;
3874 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3875 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3877 return 0;
3879 break;
3881 case VIDIOC_G_OUTPUT:
3883 int *output = arg;
3885 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3887 *output = 0;
3889 return 0;
3891 break;
3893 case VIDIOC_S_OUTPUT:
3895 int *output = arg;
3897 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3898 ZR_DEVNAME(zr), *output);
3900 if (*output != 0)
3901 return -EINVAL;
3903 return 0;
3905 break;
3907 /* cropping (sub-frame capture) */
3908 case VIDIOC_CROPCAP:
3910 struct v4l2_cropcap *cropcap = arg;
3911 int type = cropcap->type, res = 0;
3913 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3914 ZR_DEVNAME(zr), cropcap->type);
3916 memset(cropcap, 0, sizeof(*cropcap));
3917 cropcap->type = type;
3919 mutex_lock(&zr->resource_lock);
3921 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3922 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3923 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3924 dprintk(1,
3925 KERN_ERR
3926 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3927 ZR_DEVNAME(zr));
3928 res = -EINVAL;
3929 goto cropcap_unlock_and_return;
3932 cropcap->bounds.top = cropcap->bounds.left = 0;
3933 cropcap->bounds.width = BUZ_MAX_WIDTH;
3934 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3935 cropcap->defrect.top = cropcap->defrect.left = 0;
3936 cropcap->defrect.width = BUZ_MIN_WIDTH;
3937 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3938 cropcap_unlock_and_return:
3939 mutex_unlock(&zr->resource_lock);
3940 return res;
3942 break;
3944 case VIDIOC_G_CROP:
3946 struct v4l2_crop *crop = arg;
3947 int type = crop->type, res = 0;
3949 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3950 ZR_DEVNAME(zr), crop->type);
3952 memset(crop, 0, sizeof(*crop));
3953 crop->type = type;
3955 mutex_lock(&zr->resource_lock);
3957 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3958 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3959 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3960 dprintk(1,
3961 KERN_ERR
3962 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3963 ZR_DEVNAME(zr));
3964 res = -EINVAL;
3965 goto gcrop_unlock_and_return;
3968 crop->c.top = fh->jpg_settings.img_y;
3969 crop->c.left = fh->jpg_settings.img_x;
3970 crop->c.width = fh->jpg_settings.img_width;
3971 crop->c.height = fh->jpg_settings.img_height;
3973 gcrop_unlock_and_return:
3974 mutex_unlock(&zr->resource_lock);
3976 return res;
3978 break;
3980 case VIDIOC_S_CROP:
3982 struct v4l2_crop *crop = arg;
3983 int res = 0;
3985 settings = fh->jpg_settings;
3987 dprintk(3,
3988 KERN_ERR
3989 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3990 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3991 crop->c.width, crop->c.height);
3993 mutex_lock(&zr->resource_lock);
3995 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3996 dprintk(1,
3997 KERN_ERR
3998 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3999 ZR_DEVNAME(zr));
4000 res = -EBUSY;
4001 goto scrop_unlock_and_return;
4004 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
4005 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4006 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4007 dprintk(1,
4008 KERN_ERR
4009 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4010 ZR_DEVNAME(zr));
4011 res = -EINVAL;
4012 goto scrop_unlock_and_return;
4015 /* move into a form that we understand */
4016 settings.img_x = crop->c.left;
4017 settings.img_y = crop->c.top;
4018 settings.img_width = crop->c.width;
4019 settings.img_height = crop->c.height;
4021 /* check validity */
4022 if ((res = zoran_check_jpg_settings(zr, &settings)))
4023 goto scrop_unlock_and_return;
4025 /* accept */
4026 fh->jpg_settings = settings;
4028 scrop_unlock_and_return:
4029 mutex_unlock(&zr->resource_lock);
4030 return res;
4032 break;
4034 case VIDIOC_G_JPEGCOMP:
4036 struct v4l2_jpegcompression *params = arg;
4038 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4039 ZR_DEVNAME(zr));
4041 memset(params, 0, sizeof(*params));
4043 mutex_lock(&zr->resource_lock);
4045 params->quality = fh->jpg_settings.jpg_comp.quality;
4046 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4047 memcpy(params->APP_data,
4048 fh->jpg_settings.jpg_comp.APP_data,
4049 fh->jpg_settings.jpg_comp.APP_len);
4050 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4051 memcpy(params->COM_data,
4052 fh->jpg_settings.jpg_comp.COM_data,
4053 fh->jpg_settings.jpg_comp.COM_len);
4054 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4055 params->jpeg_markers =
4056 fh->jpg_settings.jpg_comp.jpeg_markers;
4058 mutex_unlock(&zr->resource_lock);
4060 return 0;
4062 break;
4064 case VIDIOC_S_JPEGCOMP:
4066 struct v4l2_jpegcompression *params = arg;
4067 int res = 0;
4069 settings = fh->jpg_settings;
4071 dprintk(3,
4072 KERN_DEBUG
4073 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4074 ZR_DEVNAME(zr), params->quality, params->APPn,
4075 params->APP_len, params->COM_len);
4077 settings.jpg_comp = *params;
4079 mutex_lock(&zr->resource_lock);
4081 if (fh->v4l_buffers.active != ZORAN_FREE ||
4082 fh->jpg_buffers.active != ZORAN_FREE) {
4083 dprintk(1,
4084 KERN_WARNING
4085 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4086 ZR_DEVNAME(zr));
4087 res = -EBUSY;
4088 goto sjpegc_unlock_and_return;
4091 if ((res = zoran_check_jpg_settings(zr, &settings)))
4092 goto sjpegc_unlock_and_return;
4093 if (!fh->jpg_buffers.allocated)
4094 fh->jpg_buffers.buffer_size =
4095 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4096 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4097 sjpegc_unlock_and_return:
4098 mutex_unlock(&zr->resource_lock);
4100 return 0;
4102 break;
4104 case VIDIOC_QUERYSTD: /* why is this useful? */
4106 v4l2_std_id *std = arg;
4108 dprintk(3,
4109 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4110 ZR_DEVNAME(zr), (unsigned long long)*std);
4112 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4113 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4114 zr->card.norms == 3)) {
4115 return 0;
4118 return -EINVAL;
4120 break;
4122 case VIDIOC_TRY_FMT:
4124 struct v4l2_format *fmt = arg;
4125 int res = 0;
4127 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4128 ZR_DEVNAME(zr), fmt->type);
4130 switch (fmt->type) {
4131 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4132 mutex_lock(&zr->resource_lock);
4134 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4135 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4136 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4137 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4138 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4139 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4140 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4141 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4143 mutex_unlock(&zr->resource_lock);
4144 break;
4146 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4147 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4148 if (fmt->fmt.pix.bytesperline > 0)
4149 return -EINVAL;
4151 mutex_lock(&zr->resource_lock);
4153 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4154 settings = fh->jpg_settings;
4156 /* we actually need to set 'real' parameters now */
4157 if ((fmt->fmt.pix.height * 2) >
4158 BUZ_MAX_HEIGHT)
4159 settings.TmpDcm = 1;
4160 else
4161 settings.TmpDcm = 2;
4162 settings.decimation = 0;
4163 if (fmt->fmt.pix.height <=
4164 fh->jpg_settings.img_height / 2)
4165 settings.VerDcm = 2;
4166 else
4167 settings.VerDcm = 1;
4168 if (fmt->fmt.pix.width <=
4169 fh->jpg_settings.img_width / 4)
4170 settings.HorDcm = 4;
4171 else if (fmt->fmt.pix.width <=
4172 fh->jpg_settings.img_width / 2)
4173 settings.HorDcm = 2;
4174 else
4175 settings.HorDcm = 1;
4176 if (settings.TmpDcm == 1)
4177 settings.field_per_buff = 2;
4178 else
4179 settings.field_per_buff = 1;
4181 /* check */
4182 if ((res =
4183 zoran_check_jpg_settings(zr,
4184 &settings)))
4185 goto tryfmt_unlock_and_return;
4187 /* tell the user what we actually did */
4188 fmt->fmt.pix.width =
4189 settings.img_width / settings.HorDcm;
4190 fmt->fmt.pix.height =
4191 settings.img_height * 2 /
4192 (settings.TmpDcm * settings.VerDcm);
4193 if (settings.TmpDcm == 1)
4194 fmt->fmt.pix.field =
4195 (fh->jpg_settings.
4196 odd_even ? V4L2_FIELD_SEQ_TB :
4197 V4L2_FIELD_SEQ_BT);
4198 else
4199 fmt->fmt.pix.field =
4200 (fh->jpg_settings.
4201 odd_even ? V4L2_FIELD_TOP :
4202 V4L2_FIELD_BOTTOM);
4204 fmt->fmt.pix.sizeimage =
4205 zoran_v4l2_calc_bufsize(&settings);
4206 } else if (fmt->type ==
4207 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4208 int i;
4210 for (i = 0; i < zoran_num_formats; i++)
4211 if (zoran_formats[i].fourcc ==
4212 fmt->fmt.pix.pixelformat)
4213 break;
4214 if (i == zoran_num_formats) {
4215 res = -EINVAL;
4216 goto tryfmt_unlock_and_return;
4219 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4220 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4221 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4222 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4223 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4224 fmt->fmt.pix.height =
4225 BUZ_MAX_HEIGHT;
4226 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4227 fmt->fmt.pix.height =
4228 BUZ_MIN_HEIGHT;
4229 } else {
4230 res = -EINVAL;
4231 goto tryfmt_unlock_and_return;
4233 tryfmt_unlock_and_return:
4234 mutex_unlock(&zr->resource_lock);
4236 return res;
4237 break;
4239 default:
4240 return -EINVAL;
4243 return 0;
4245 break;
4246 #endif
4248 default:
4249 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4250 ZR_DEVNAME(zr), cmd);
4251 return -ENOIOCTLCMD;
4252 break;
4255 return 0;
4259 static int
4260 zoran_ioctl (struct inode *inode,
4261 struct file *file,
4262 unsigned int cmd,
4263 unsigned long arg)
4265 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4268 static unsigned int
4269 zoran_poll (struct file *file,
4270 poll_table *wait)
4272 struct zoran_fh *fh = file->private_data;
4273 struct zoran *zr = fh->zr;
4274 wait_queue_head_t *queue = NULL;
4275 int res = 0, frame;
4277 /* we should check whether buffers are ready to be synced on
4278 * (w/o waits - O_NONBLOCK) here
4279 * if ready for read (sync), return POLLIN|POLLRDNORM,
4280 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4281 * if error, return POLLERR,
4282 * if no buffers queued or so, return POLLNVAL
4285 mutex_lock(&zr->resource_lock);
4287 switch (fh->map_mode) {
4288 case ZORAN_MAP_MODE_RAW:
4289 if (fh->v4l_buffers.active == ZORAN_FREE ||
4290 zr->v4l_pend_head == zr->v4l_pend_tail) {
4291 dprintk(1,
4292 "%s: zoran_poll() - no buffers queued\n",
4293 ZR_DEVNAME(zr));
4294 res = POLLNVAL;
4295 goto poll_unlock_and_return;
4297 queue = &zr->v4l_capq;
4298 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4299 poll_wait(file, queue, wait);
4300 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4301 res = POLLIN | POLLRDNORM;
4302 break;
4304 case ZORAN_MAP_MODE_JPG_REC:
4305 case ZORAN_MAP_MODE_JPG_PLAY:
4306 if (fh->jpg_buffers.active == ZORAN_FREE ||
4307 zr->jpg_que_head == zr->jpg_que_tail) {
4308 dprintk(1,
4309 "%s: zoran_poll() - no buffers queued\n",
4310 ZR_DEVNAME(zr));
4311 res = POLLNVAL;
4312 goto poll_unlock_and_return;
4314 queue = &zr->jpg_capq;
4315 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4316 poll_wait(file, queue, wait);
4317 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4318 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4319 res = POLLIN | POLLRDNORM;
4320 else
4321 res = POLLOUT | POLLWRNORM;
4323 break;
4325 default:
4326 dprintk(1,
4327 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4328 ZR_DEVNAME(zr), fh->map_mode);
4329 res = POLLNVAL;
4330 goto poll_unlock_and_return;
4333 poll_unlock_and_return:
4334 mutex_unlock(&zr->resource_lock);
4336 return res;
4341 * This maps the buffers to user space.
4343 * Depending on the state of fh->map_mode
4344 * the V4L or the MJPEG buffers are mapped
4345 * per buffer or all together
4347 * Note that we need to connect to some
4348 * unmap signal event to unmap the de-allocate
4349 * the buffer accordingly (zoran_vm_close())
4352 static void
4353 zoran_vm_open (struct vm_area_struct *vma)
4355 struct zoran_mapping *map = vma->vm_private_data;
4357 map->count++;
4360 static void
4361 zoran_vm_close (struct vm_area_struct *vma)
4363 struct zoran_mapping *map = vma->vm_private_data;
4364 struct file *file = map->file;
4365 struct zoran_fh *fh = file->private_data;
4366 struct zoran *zr = fh->zr;
4367 int i;
4369 map->count--;
4370 if (map->count == 0) {
4371 switch (fh->map_mode) {
4372 case ZORAN_MAP_MODE_JPG_REC:
4373 case ZORAN_MAP_MODE_JPG_PLAY:
4375 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4376 ZR_DEVNAME(zr));
4378 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4379 if (fh->jpg_buffers.buffer[i].map == map) {
4380 fh->jpg_buffers.buffer[i].map =
4381 NULL;
4384 kfree(map);
4386 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4387 if (fh->jpg_buffers.buffer[i].map)
4388 break;
4389 if (i == fh->jpg_buffers.num_buffers) {
4390 mutex_lock(&zr->resource_lock);
4392 if (fh->jpg_buffers.active != ZORAN_FREE) {
4393 jpg_qbuf(file, -1, zr->codec_mode);
4394 zr->jpg_buffers.allocated = 0;
4395 zr->jpg_buffers.active =
4396 fh->jpg_buffers.active =
4397 ZORAN_FREE;
4399 //jpg_fbuffer_free(file);
4400 fh->jpg_buffers.allocated = 0;
4401 fh->jpg_buffers.ready_to_be_freed = 1;
4403 mutex_unlock(&zr->resource_lock);
4406 break;
4408 case ZORAN_MAP_MODE_RAW:
4410 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4411 ZR_DEVNAME(zr));
4413 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4414 if (fh->v4l_buffers.buffer[i].map == map) {
4415 /* unqueue/unmap */
4416 fh->v4l_buffers.buffer[i].map =
4417 NULL;
4420 kfree(map);
4422 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4423 if (fh->v4l_buffers.buffer[i].map)
4424 break;
4425 if (i == fh->v4l_buffers.num_buffers) {
4426 mutex_lock(&zr->resource_lock);
4428 if (fh->v4l_buffers.active != ZORAN_FREE) {
4429 zr36057_set_memgrab(zr, 0);
4430 zr->v4l_buffers.allocated = 0;
4431 zr->v4l_buffers.active =
4432 fh->v4l_buffers.active =
4433 ZORAN_FREE;
4435 //v4l_fbuffer_free(file);
4436 fh->v4l_buffers.allocated = 0;
4437 fh->v4l_buffers.ready_to_be_freed = 1;
4439 mutex_unlock(&zr->resource_lock);
4442 break;
4444 default:
4445 printk(KERN_ERR
4446 "%s: munmap() - internal error - unknown map mode %d\n",
4447 ZR_DEVNAME(zr), fh->map_mode);
4448 break;
4454 static struct vm_operations_struct zoran_vm_ops = {
4455 .open = zoran_vm_open,
4456 .close = zoran_vm_close,
4459 static int
4460 zoran_mmap (struct file *file,
4461 struct vm_area_struct *vma)
4463 struct zoran_fh *fh = file->private_data;
4464 struct zoran *zr = fh->zr;
4465 unsigned long size = (vma->vm_end - vma->vm_start);
4466 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4467 int i, j;
4468 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4469 int first, last;
4470 struct zoran_mapping *map;
4471 int res = 0;
4473 dprintk(3,
4474 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4475 ZR_DEVNAME(zr),
4476 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4477 vma->vm_start, vma->vm_end, size);
4479 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4480 !(vma->vm_flags & VM_WRITE)) {
4481 dprintk(1,
4482 KERN_ERR
4483 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4484 ZR_DEVNAME(zr));
4485 return -EINVAL;
4488 switch (fh->map_mode) {
4490 case ZORAN_MAP_MODE_JPG_REC:
4491 case ZORAN_MAP_MODE_JPG_PLAY:
4493 /* lock */
4494 mutex_lock(&zr->resource_lock);
4496 /* Map the MJPEG buffers */
4497 if (!fh->jpg_buffers.allocated) {
4498 dprintk(1,
4499 KERN_ERR
4500 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4501 ZR_DEVNAME(zr));
4502 res = -ENOMEM;
4503 goto jpg_mmap_unlock_and_return;
4506 first = offset / fh->jpg_buffers.buffer_size;
4507 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4508 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4509 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4510 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4511 last >= fh->jpg_buffers.num_buffers) {
4512 dprintk(1,
4513 KERN_ERR
4514 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4515 ZR_DEVNAME(zr), offset, size,
4516 fh->jpg_buffers.buffer_size,
4517 fh->jpg_buffers.num_buffers);
4518 res = -EINVAL;
4519 goto jpg_mmap_unlock_and_return;
4521 for (i = first; i <= last; i++) {
4522 if (fh->jpg_buffers.buffer[i].map) {
4523 dprintk(1,
4524 KERN_ERR
4525 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4526 ZR_DEVNAME(zr), i);
4527 res = -EBUSY;
4528 goto jpg_mmap_unlock_and_return;
4532 /* map these buffers (v4l_buffers[i]) */
4533 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4534 if (!map) {
4535 res = -ENOMEM;
4536 goto jpg_mmap_unlock_and_return;
4538 map->file = file;
4539 map->count = 1;
4541 vma->vm_ops = &zoran_vm_ops;
4542 vma->vm_flags |= VM_DONTEXPAND;
4543 vma->vm_private_data = map;
4545 for (i = first; i <= last; i++) {
4546 for (j = 0;
4547 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4548 j++) {
4549 fraglen =
4550 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4551 frag_tab[2 * j + 1]) & ~1) << 1;
4552 todo = size;
4553 if (todo > fraglen)
4554 todo = fraglen;
4555 pos =
4556 le32_to_cpu((unsigned long) fh->jpg_buffers.
4557 buffer[i].frag_tab[2 * j]);
4558 /* should just be pos on i386 */
4559 page = virt_to_phys(bus_to_virt(pos))
4560 >> PAGE_SHIFT;
4561 if (remap_pfn_range(vma, start, page,
4562 todo, PAGE_SHARED)) {
4563 dprintk(1,
4564 KERN_ERR
4565 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4566 ZR_DEVNAME(zr));
4567 res = -EAGAIN;
4568 goto jpg_mmap_unlock_and_return;
4570 size -= todo;
4571 start += todo;
4572 if (size == 0)
4573 break;
4574 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4575 frag_tab[2 * j + 1]) & 1)
4576 break; /* was last fragment */
4578 fh->jpg_buffers.buffer[i].map = map;
4579 if (size == 0)
4580 break;
4583 jpg_mmap_unlock_and_return:
4584 mutex_unlock(&zr->resource_lock);
4586 break;
4588 case ZORAN_MAP_MODE_RAW:
4590 mutex_lock(&zr->resource_lock);
4592 /* Map the V4L buffers */
4593 if (!fh->v4l_buffers.allocated) {
4594 dprintk(1,
4595 KERN_ERR
4596 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4597 ZR_DEVNAME(zr));
4598 res = -ENOMEM;
4599 goto v4l_mmap_unlock_and_return;
4602 first = offset / fh->v4l_buffers.buffer_size;
4603 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4604 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4605 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4606 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4607 last >= fh->v4l_buffers.buffer_size) {
4608 dprintk(1,
4609 KERN_ERR
4610 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4611 ZR_DEVNAME(zr), offset, size,
4612 fh->v4l_buffers.buffer_size,
4613 fh->v4l_buffers.num_buffers);
4614 res = -EINVAL;
4615 goto v4l_mmap_unlock_and_return;
4617 for (i = first; i <= last; i++) {
4618 if (fh->v4l_buffers.buffer[i].map) {
4619 dprintk(1,
4620 KERN_ERR
4621 "%s: mmap(V4L) - buffer %d already mapped\n",
4622 ZR_DEVNAME(zr), i);
4623 res = -EBUSY;
4624 goto v4l_mmap_unlock_and_return;
4628 /* map these buffers (v4l_buffers[i]) */
4629 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4630 if (!map) {
4631 res = -ENOMEM;
4632 goto v4l_mmap_unlock_and_return;
4634 map->file = file;
4635 map->count = 1;
4637 vma->vm_ops = &zoran_vm_ops;
4638 vma->vm_flags |= VM_DONTEXPAND;
4639 vma->vm_private_data = map;
4641 for (i = first; i <= last; i++) {
4642 todo = size;
4643 if (todo > fh->v4l_buffers.buffer_size)
4644 todo = fh->v4l_buffers.buffer_size;
4645 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4646 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4647 todo, PAGE_SHARED)) {
4648 dprintk(1,
4649 KERN_ERR
4650 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4651 ZR_DEVNAME(zr));
4652 res = -EAGAIN;
4653 goto v4l_mmap_unlock_and_return;
4655 size -= todo;
4656 start += todo;
4657 fh->v4l_buffers.buffer[i].map = map;
4658 if (size == 0)
4659 break;
4661 v4l_mmap_unlock_and_return:
4662 mutex_unlock(&zr->resource_lock);
4664 break;
4666 default:
4667 dprintk(1,
4668 KERN_ERR
4669 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4670 ZR_DEVNAME(zr), fh->map_mode);
4671 break;
4674 return 0;
4677 static struct file_operations zoran_fops = {
4678 .owner = THIS_MODULE,
4679 .open = zoran_open,
4680 .release = zoran_close,
4681 .ioctl = zoran_ioctl,
4682 .compat_ioctl = v4l_compat_ioctl32,
4683 .llseek = no_llseek,
4684 .read = zoran_read,
4685 .write = zoran_write,
4686 .mmap = zoran_mmap,
4687 .poll = zoran_poll,
4690 struct video_device zoran_template __devinitdata = {
4691 .name = ZORAN_NAME,
4692 .type = ZORAN_VID_TYPE,
4693 #ifdef HAVE_V4L2
4694 .type2 = ZORAN_V4L2_VID_FLAGS,
4695 #endif
4696 .hardware = ZORAN_HARDWARE,
4697 .fops = &zoran_fops,
4698 .release = &zoran_vdev_release,
4699 .minor = -1