Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / media / video / zoran_driver.c
blobcf0ed6cbb0e39eb8363120e477bef27549c754ff
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/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
55 #include <linux/byteorder/generic.h>
57 #include <linux/interrupt.h>
58 #include <linux/i2c.h>
59 #include <linux/i2c-algo-bit.h>
61 #include <linux/spinlock.h>
62 #define MAP_NR(x) virt_to_page(x)
63 #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
64 #define ZORAN_VID_TYPE ( \
65 VID_TYPE_CAPTURE | \
66 VID_TYPE_OVERLAY | \
67 VID_TYPE_CLIPPING | \
68 VID_TYPE_FRAMERAM | \
69 VID_TYPE_SCALES | \
70 VID_TYPE_MJPEG_DECODER | \
71 VID_TYPE_MJPEG_ENCODER \
74 #include <linux/videodev.h>
75 #include <media/v4l2-common.h>
76 #include "videocodec.h"
78 #include <asm/io.h>
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
85 #include "zoran.h"
86 #include "zoran_device.h"
87 #include "zoran_card.h"
89 #ifdef CONFIG_VIDEO_V4L2
90 /* we declare some card type definitions here, they mean
91 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
92 #define ZORAN_V4L2_VID_FLAGS ( \
93 V4L2_CAP_STREAMING |\
94 V4L2_CAP_VIDEO_CAPTURE |\
95 V4L2_CAP_VIDEO_OUTPUT |\
96 V4L2_CAP_VIDEO_OVERLAY \
98 #endif
100 #include <asm/byteorder.h>
102 const struct zoran_format zoran_formats[] = {
104 .name = "15-bit RGB",
105 .palette = VIDEO_PALETTE_RGB555,
106 #ifdef CONFIG_VIDEO_V4L2
107 #ifdef __LITTLE_ENDIAN
108 .fourcc = V4L2_PIX_FMT_RGB555,
109 #else
110 .fourcc = V4L2_PIX_FMT_RGB555X,
111 #endif
112 .colorspace = V4L2_COLORSPACE_SRGB,
113 #endif
114 .depth = 15,
115 .flags = ZORAN_FORMAT_CAPTURE |
116 ZORAN_FORMAT_OVERLAY,
117 }, {
118 .name = "16-bit RGB",
119 .palette = VIDEO_PALETTE_RGB565,
120 #ifdef CONFIG_VIDEO_V4L2
121 #ifdef __LITTLE_ENDIAN
122 .fourcc = V4L2_PIX_FMT_RGB565,
123 #else
124 .fourcc = V4L2_PIX_FMT_RGB565X,
125 #endif
126 .colorspace = V4L2_COLORSPACE_SRGB,
127 #endif
128 .depth = 16,
129 .flags = ZORAN_FORMAT_CAPTURE |
130 ZORAN_FORMAT_OVERLAY,
131 }, {
132 .name = "24-bit RGB",
133 .palette = VIDEO_PALETTE_RGB24,
134 #ifdef CONFIG_VIDEO_V4L2
135 #ifdef __LITTLE_ENDIAN
136 .fourcc = V4L2_PIX_FMT_BGR24,
137 #else
138 .fourcc = V4L2_PIX_FMT_RGB24,
139 #endif
140 .colorspace = V4L2_COLORSPACE_SRGB,
141 #endif
142 .depth = 24,
143 .flags = ZORAN_FORMAT_CAPTURE |
144 ZORAN_FORMAT_OVERLAY,
145 }, {
146 .name = "32-bit RGB",
147 .palette = VIDEO_PALETTE_RGB32,
148 #ifdef CONFIG_VIDEO_V4L2
149 #ifdef __LITTLE_ENDIAN
150 .fourcc = V4L2_PIX_FMT_BGR32,
151 #else
152 .fourcc = V4L2_PIX_FMT_RGB32,
153 #endif
154 .colorspace = V4L2_COLORSPACE_SRGB,
155 #endif
156 .depth = 32,
157 .flags = ZORAN_FORMAT_CAPTURE |
158 ZORAN_FORMAT_OVERLAY,
159 }, {
160 .name = "4:2:2, packed, YUYV",
161 .palette = VIDEO_PALETTE_YUV422,
162 #ifdef CONFIG_VIDEO_V4L2
163 .fourcc = V4L2_PIX_FMT_YUYV,
164 .colorspace = V4L2_COLORSPACE_SMPTE170M,
165 #endif
166 .depth = 16,
167 .flags = ZORAN_FORMAT_CAPTURE |
168 ZORAN_FORMAT_OVERLAY,
169 }, {
170 .name = "Hardware-encoded Motion-JPEG",
171 .palette = -1,
172 #ifdef CONFIG_VIDEO_V4L2
173 .fourcc = V4L2_PIX_FMT_MJPEG,
174 .colorspace = V4L2_COLORSPACE_SMPTE170M,
175 #endif
176 .depth = 0,
177 .flags = ZORAN_FORMAT_CAPTURE |
178 ZORAN_FORMAT_PLAYBACK |
179 ZORAN_FORMAT_COMPRESSED,
182 static const int zoran_num_formats =
183 (sizeof(zoran_formats) / sizeof(struct zoran_format));
185 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
186 #if !defined(CONFIG_BIGPHYS_AREA)
187 //#undef CONFIG_BIGPHYS_AREA
188 #define BUZ_USE_HIMEM
189 #endif
191 #if defined(CONFIG_BIGPHYS_AREA)
192 # include <linux/bigphysarea.h>
193 #endif
195 extern int *zr_debug;
197 #define dprintk(num, format, args...) \
198 do { \
199 if (*zr_debug >= num) \
200 printk(format, ##args); \
201 } while (0)
203 extern int v4l_nbufs;
204 extern int v4l_bufsize;
205 extern int jpg_nbufs;
206 extern int jpg_bufsize;
207 extern int pass_through;
209 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
210 module_param(lock_norm, int, 0);
211 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
213 #ifdef CONFIG_VIDEO_V4L2
214 /* small helper function for calculating buffersizes for v4l2
215 * we calculate the nearest higher power-of-two, which
216 * will be the recommended buffersize */
217 static __u32
218 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
220 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
221 __u32 num = (1024 * 512) / (div);
222 __u32 result = 2;
224 num--;
225 while (num) {
226 num >>= 1;
227 result <<= 1;
230 if (result > jpg_bufsize)
231 return jpg_bufsize;
232 if (result < 8192)
233 return 8192;
234 return result;
236 #endif
238 /* forward references */
239 static void v4l_fbuffer_free(struct file *file);
240 static void jpg_fbuffer_free(struct file *file);
243 * Allocate the V4L grab buffers
245 * These have to be pysically contiguous.
246 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
247 * else we try to allocate them with bigphysarea_alloc_pages
248 * if the bigphysarea patch is present in the kernel,
249 * else we try to use high memory (if the user has bootet
250 * Linux with the necessary memory left over).
253 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
254 static unsigned long
255 get_high_mem (unsigned long size)
258 * Check if there is usable memory at the end of Linux memory
259 * of at least size. Return the physical address of this memory,
260 * return 0 on failure.
262 * The idea is from Alexandro Rubini's book "Linux device drivers".
263 * The driver from him which is downloadable from O'Reilly's
264 * web site misses the "virt_to_phys(high_memory)" part
265 * (and therefore doesn't work at all - at least with 2.2.x kernels).
267 * It should be unnecessary to mention that THIS IS DANGEROUS,
268 * if more than one driver at a time has the idea to use this memory!!!!
271 volatile unsigned char __iomem *mem;
272 unsigned char c;
273 unsigned long hi_mem_ph;
274 unsigned long i;
276 /* Map the high memory to user space */
278 hi_mem_ph = virt_to_phys(high_memory);
280 mem = ioremap(hi_mem_ph, size);
281 if (!mem) {
282 dprintk(1,
283 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
284 ZORAN_NAME);
285 return 0;
288 for (i = 0; i < size; i++) {
289 /* Check if it is memory */
290 c = i & 0xff;
291 writeb(c, mem + i);
292 if (readb(mem + i) != c)
293 break;
294 c = 255 - c;
295 writeb(c, mem + i);
296 if (readb(mem + i) != c)
297 break;
298 writeb(0, mem + i); /* zero out memory */
300 /* give the kernel air to breath */
301 if ((i & 0x3ffff) == 0x3ffff)
302 schedule();
305 iounmap(mem);
307 if (i != size) {
308 dprintk(1,
309 KERN_ERR
310 "%s: get_high_mem() - requested %lu, avail %lu\n",
311 ZORAN_NAME, size, i);
312 return 0;
315 return hi_mem_ph;
317 #endif
319 static int
320 v4l_fbuffer_alloc (struct file *file)
322 struct zoran_fh *fh = file->private_data;
323 struct zoran *zr = fh->zr;
324 int i, off;
325 unsigned char *mem;
326 #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
327 unsigned long pmem = 0;
328 #endif
330 /* we might have old buffers lying around... */
331 if (fh->v4l_buffers.ready_to_be_freed) {
332 v4l_fbuffer_free(file);
335 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
336 if (fh->v4l_buffers.buffer[i].fbuffer)
337 dprintk(2,
338 KERN_WARNING
339 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
340 ZR_DEVNAME(zr), i);
342 //udelay(20);
343 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
344 /* Use kmalloc */
346 mem =
347 (unsigned char *) kmalloc(fh->v4l_buffers.
348 buffer_size,
349 GFP_KERNEL);
350 if (mem == 0) {
351 dprintk(1,
352 KERN_ERR
353 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
354 ZR_DEVNAME(zr), i);
355 v4l_fbuffer_free(file);
356 return -ENOBUFS;
358 fh->v4l_buffers.buffer[i].fbuffer = mem;
359 fh->v4l_buffers.buffer[i].fbuffer_phys =
360 virt_to_phys(mem);
361 fh->v4l_buffers.buffer[i].fbuffer_bus =
362 virt_to_bus(mem);
363 for (off = 0; off < fh->v4l_buffers.buffer_size;
364 off += PAGE_SIZE)
365 SetPageReserved(MAP_NR(mem + off));
366 dprintk(4,
367 KERN_INFO
368 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
369 ZR_DEVNAME(zr), i, (unsigned long) mem,
370 virt_to_bus(mem));
371 } else {
372 #if defined(CONFIG_BIGPHYS_AREA)
373 /* Use bigphysarea_alloc_pages */
375 int n =
376 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
377 1) / PAGE_SIZE;
379 mem =
380 (unsigned char *) bigphysarea_alloc_pages(n, 0,
381 GFP_KERNEL);
382 if (mem == 0) {
383 dprintk(1,
384 KERN_ERR
385 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
386 ZR_DEVNAME(zr), i);
387 v4l_fbuffer_free(file);
388 return -ENOBUFS;
390 fh->v4l_buffers.buffer[i].fbuffer = mem;
391 fh->v4l_buffers.buffer[i].fbuffer_phys =
392 virt_to_phys(mem);
393 fh->v4l_buffers.buffer[i].fbuffer_bus =
394 virt_to_bus(mem);
395 dprintk(4,
396 KERN_INFO
397 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
398 ZR_DEVNAME(zr), i, (unsigned) mem,
399 (unsigned) virt_to_bus(mem));
401 /* Zero out the allocated memory */
402 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
403 fh->v4l_buffers.buffer_size);
404 #elif defined(BUZ_USE_HIMEM)
406 /* Use high memory which has been left at boot time */
408 /* Ok., Ok. this is an evil hack - we make
409 * the assumption that physical addresses are
410 * the same as bus addresses (true at least
411 * for Intel processors). The whole method of
412 * obtaining and using this memory is not very
413 * nice - but I hope it saves some poor users
414 * from kernel hacking, which might have even
415 * more evil results */
417 if (i == 0) {
418 int size =
419 fh->v4l_buffers.num_buffers *
420 fh->v4l_buffers.buffer_size;
422 pmem = get_high_mem(size);
423 if (pmem == 0) {
424 dprintk(1,
425 KERN_ERR
426 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
427 ZR_DEVNAME(zr), size >> 10);
428 return -ENOBUFS;
430 fh->v4l_buffers.buffer[0].fbuffer = NULL;
431 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
432 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
433 dprintk(4,
434 KERN_INFO
435 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
436 ZR_DEVNAME(zr), size >> 10);
437 } else {
438 fh->v4l_buffers.buffer[i].fbuffer = NULL;
439 fh->v4l_buffers.buffer[i].fbuffer_phys =
440 pmem + i * fh->v4l_buffers.buffer_size;
441 fh->v4l_buffers.buffer[i].fbuffer_bus =
442 pmem + i * fh->v4l_buffers.buffer_size;
444 #else
445 /* No bigphysarea present, usage of high memory disabled,
446 * but user wants buffers of more than MAX_KMALLOC_MEM */
447 dprintk(1,
448 KERN_ERR
449 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
450 ZR_DEVNAME(zr));
451 dprintk(1,
452 KERN_ERR
453 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
454 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
455 fh->v4l_buffers.buffer_size >> 10);
456 return -ENOBUFS;
457 #endif
461 fh->v4l_buffers.allocated = 1;
463 return 0;
466 /* free the V4L grab buffers */
467 static void
468 v4l_fbuffer_free (struct file *file)
470 struct zoran_fh *fh = file->private_data;
471 struct zoran *zr = fh->zr;
472 int i, off;
473 unsigned char *mem;
475 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
477 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
478 if (!fh->v4l_buffers.buffer[i].fbuffer)
479 continue;
481 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
482 mem = fh->v4l_buffers.buffer[i].fbuffer;
483 for (off = 0; off < fh->v4l_buffers.buffer_size;
484 off += PAGE_SIZE)
485 ClearPageReserved(MAP_NR(mem + off));
486 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
488 #if defined(CONFIG_BIGPHYS_AREA)
489 else
490 bigphysarea_free_pages((void *) fh->v4l_buffers.
491 buffer[i].fbuffer);
492 #endif
493 fh->v4l_buffers.buffer[i].fbuffer = NULL;
496 fh->v4l_buffers.allocated = 0;
497 fh->v4l_buffers.ready_to_be_freed = 0;
501 * Allocate the MJPEG grab buffers.
503 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
504 * kmalloc is used to request a physically contiguous area,
505 * else we allocate the memory in framgents with get_zeroed_page.
507 * If a Natoma chipset is present and this is a revision 1 zr36057,
508 * each MJPEG buffer needs to be physically contiguous.
509 * (RJ: This statement is from Dave Perks' original driver,
510 * I could never check it because I have a zr36067)
511 * The driver cares about this because it reduces the buffer
512 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
514 * RJ: The contents grab buffers needs never be accessed in the driver.
515 * Therefore there is no need to allocate them with vmalloc in order
516 * to get a contiguous virtual memory space.
517 * I don't understand why many other drivers first allocate them with
518 * vmalloc (which uses internally also get_zeroed_page, but delivers you
519 * virtual addresses) and then again have to make a lot of efforts
520 * to get the physical address.
522 * Ben Capper:
523 * On big-endian architectures (such as ppc) some extra steps
524 * are needed. When reading and writing to the stat_com array
525 * and fragment buffers, the device expects to see little-
526 * endian values. The use of cpu_to_le32() and le32_to_cpu()
527 * in this function (and one or two others in zoran_device.c)
528 * ensure that these values are always stored in little-endian
529 * form, regardless of architecture. The zr36057 does Very Bad
530 * Things on big endian architectures if the stat_com array
531 * and fragment buffers are not little-endian.
534 static int
535 jpg_fbuffer_alloc (struct file *file)
537 struct zoran_fh *fh = file->private_data;
538 struct zoran *zr = fh->zr;
539 int i, j, off;
540 unsigned long mem;
542 /* we might have old buffers lying around */
543 if (fh->jpg_buffers.ready_to_be_freed) {
544 jpg_fbuffer_free(file);
547 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
548 if (fh->jpg_buffers.buffer[i].frag_tab)
549 dprintk(2,
550 KERN_WARNING
551 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
552 ZR_DEVNAME(zr), i);
554 /* Allocate fragment table for this buffer */
556 mem = get_zeroed_page(GFP_KERNEL);
557 if (mem == 0) {
558 dprintk(1,
559 KERN_ERR
560 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
561 ZR_DEVNAME(zr), i);
562 jpg_fbuffer_free(file);
563 return -ENOBUFS;
565 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
566 fh->jpg_buffers.buffer[i].frag_tab_bus =
567 virt_to_bus((void *) mem);
569 //if (alloc_contig) {
570 if (fh->jpg_buffers.need_contiguous) {
571 mem =
572 (unsigned long) kmalloc(fh->jpg_buffers.
573 buffer_size,
574 GFP_KERNEL);
575 if (mem == 0) {
576 dprintk(1,
577 KERN_ERR
578 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
579 ZR_DEVNAME(zr), i);
580 jpg_fbuffer_free(file);
581 return -ENOBUFS;
583 fh->jpg_buffers.buffer[i].frag_tab[0] =
584 cpu_to_le32(virt_to_bus((void *) mem));
585 fh->jpg_buffers.buffer[i].frag_tab[1] =
586 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
587 for (off = 0; off < fh->jpg_buffers.buffer_size;
588 off += PAGE_SIZE)
589 SetPageReserved(MAP_NR(mem + off));
590 } else {
591 /* jpg_bufsize is allreay page aligned */
592 for (j = 0;
593 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
594 j++) {
595 mem = get_zeroed_page(GFP_KERNEL);
596 if (mem == 0) {
597 dprintk(1,
598 KERN_ERR
599 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
600 ZR_DEVNAME(zr), i);
601 jpg_fbuffer_free(file);
602 return -ENOBUFS;
605 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
606 cpu_to_le32(virt_to_bus((void *) mem));
607 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
608 1] =
609 cpu_to_le32((PAGE_SIZE / 4) << 1);
610 SetPageReserved(MAP_NR(mem));
613 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
617 dprintk(4,
618 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
619 ZR_DEVNAME(zr),
620 (fh->jpg_buffers.num_buffers *
621 fh->jpg_buffers.buffer_size) >> 10);
623 fh->jpg_buffers.allocated = 1;
625 return 0;
628 /* free the MJPEG grab buffers */
629 static void
630 jpg_fbuffer_free (struct file *file)
632 struct zoran_fh *fh = file->private_data;
633 struct zoran *zr = fh->zr;
634 int i, j, off;
635 unsigned char *mem;
637 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
639 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
640 if (!fh->jpg_buffers.buffer[i].frag_tab)
641 continue;
643 //if (alloc_contig) {
644 if (fh->jpg_buffers.need_contiguous) {
645 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
646 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
647 fh->jpg_buffers.buffer[i].frag_tab[0]));
648 for (off = 0;
649 off < fh->jpg_buffers.buffer_size;
650 off += PAGE_SIZE)
651 ClearPageReserved(MAP_NR
652 (mem + off));
653 kfree(mem);
654 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
655 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
657 } else {
658 for (j = 0;
659 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
660 j++) {
661 if (!fh->jpg_buffers.buffer[i].
662 frag_tab[2 * j])
663 break;
664 ClearPageReserved(MAP_NR
665 (bus_to_virt
666 (le32_to_cpu
667 (fh->jpg_buffers.
668 buffer[i].frag_tab[2 *
669 j]))));
670 free_page((unsigned long)
671 bus_to_virt
672 (le32_to_cpu
673 (fh->jpg_buffers.
674 buffer[i].
675 frag_tab[2 * j])));
676 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
678 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
679 1] = 0;
683 free_page((unsigned long) fh->jpg_buffers.buffer[i].
684 frag_tab);
685 fh->jpg_buffers.buffer[i].frag_tab = NULL;
688 fh->jpg_buffers.allocated = 0;
689 fh->jpg_buffers.ready_to_be_freed = 0;
693 * V4L Buffer grabbing
696 static int
697 zoran_v4l_set_format (struct file *file,
698 int width,
699 int height,
700 const struct zoran_format *format)
702 struct zoran_fh *fh = file->private_data;
703 struct zoran *zr = fh->zr;
704 int bpp;
706 /* Check size and format of the grab wanted */
708 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
709 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
710 dprintk(1,
711 KERN_ERR
712 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
713 ZR_DEVNAME(zr), width, height);
714 return -EINVAL;
717 bpp = (format->depth + 7) / 8;
719 /* Check against available buffer size */
720 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
721 dprintk(1,
722 KERN_ERR
723 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
724 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
725 return -EINVAL;
728 /* The video front end needs 4-byte alinged line sizes */
730 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
731 dprintk(1,
732 KERN_ERR
733 "%s: v4l_set_format() - wrong frame alingment\n",
734 ZR_DEVNAME(zr));
735 return -EINVAL;
738 fh->v4l_settings.width = width;
739 fh->v4l_settings.height = height;
740 fh->v4l_settings.format = format;
741 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
743 return 0;
746 static int
747 zoran_v4l_queue_frame (struct file *file,
748 int num)
750 struct zoran_fh *fh = file->private_data;
751 struct zoran *zr = fh->zr;
752 unsigned long flags;
753 int res = 0;
755 if (!fh->v4l_buffers.allocated) {
756 dprintk(1,
757 KERN_ERR
758 "%s: v4l_queue_frame() - buffers not yet allocated\n",
759 ZR_DEVNAME(zr));
760 res = -ENOMEM;
763 /* No grabbing outside the buffer range! */
764 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
765 dprintk(1,
766 KERN_ERR
767 "%s: v4l_queue_frame() - buffer %d is out of range\n",
768 ZR_DEVNAME(zr), num);
769 res = -EINVAL;
772 spin_lock_irqsave(&zr->spinlock, flags);
774 if (fh->v4l_buffers.active == ZORAN_FREE) {
775 if (zr->v4l_buffers.active == ZORAN_FREE) {
776 zr->v4l_buffers = fh->v4l_buffers;
777 fh->v4l_buffers.active = ZORAN_ACTIVE;
778 } else {
779 dprintk(1,
780 KERN_ERR
781 "%s: v4l_queue_frame() - another session is already capturing\n",
782 ZR_DEVNAME(zr));
783 res = -EBUSY;
787 /* make sure a grab isn't going on currently with this buffer */
788 if (!res) {
789 switch (zr->v4l_buffers.buffer[num].state) {
790 default:
791 case BUZ_STATE_PEND:
792 if (zr->v4l_buffers.active == ZORAN_FREE) {
793 fh->v4l_buffers.active = ZORAN_FREE;
794 zr->v4l_buffers.allocated = 0;
796 res = -EBUSY; /* what are you doing? */
797 break;
798 case BUZ_STATE_DONE:
799 dprintk(2,
800 KERN_WARNING
801 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
802 ZR_DEVNAME(zr), num);
803 case BUZ_STATE_USER:
804 /* since there is at least one unused buffer there's room for at least
805 * one more pend[] entry */
806 zr->v4l_pend[zr->v4l_pend_head++ &
807 V4L_MASK_FRAME] = num;
808 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
809 zr->v4l_buffers.buffer[num].bs.length =
810 fh->v4l_settings.bytesperline *
811 zr->v4l_settings.height;
812 fh->v4l_buffers.buffer[num] =
813 zr->v4l_buffers.buffer[num];
814 break;
818 spin_unlock_irqrestore(&zr->spinlock, flags);
820 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
821 zr->v4l_buffers.active = fh->v4l_buffers.active;
823 return res;
826 static int
827 v4l_grab (struct file *file,
828 struct video_mmap *mp)
830 struct zoran_fh *fh = file->private_data;
831 struct zoran *zr = fh->zr;
832 int res = 0, i;
834 for (i = 0; i < zoran_num_formats; i++) {
835 if (zoran_formats[i].palette == mp->format &&
836 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
837 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
838 break;
840 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
841 dprintk(1,
842 KERN_ERR
843 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
844 ZR_DEVNAME(zr));
845 return -EINVAL;
849 * To minimize the time spent in the IRQ routine, we avoid setting up
850 * the video front end there.
851 * If this grab has different parameters from a running streaming capture
852 * we stop the streaming capture and start it over again.
854 if (zr->v4l_memgrab_active &&
855 (zr->v4l_settings.width != mp->width ||
856 zr->v4l_settings.height != mp->height ||
857 zr->v4l_settings.format->palette != mp->format)) {
858 res = wait_grab_pending(zr);
859 if (res)
860 return res;
862 if ((res = zoran_v4l_set_format(file,
863 mp->width,
864 mp->height,
865 &zoran_formats[i])))
866 return res;
867 zr->v4l_settings = fh->v4l_settings;
869 /* queue the frame in the pending queue */
870 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
871 fh->v4l_buffers.active = ZORAN_FREE;
872 return res;
875 /* put the 36057 into frame grabbing mode */
876 if (!res && !zr->v4l_memgrab_active)
877 zr36057_set_memgrab(zr, 1);
879 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
881 return res;
885 * Sync on a V4L buffer
888 static int
889 v4l_sync (struct file *file,
890 int frame)
892 struct zoran_fh *fh = file->private_data;
893 struct zoran *zr = fh->zr;
894 unsigned long flags;
896 if (fh->v4l_buffers.active == ZORAN_FREE) {
897 dprintk(1,
898 KERN_ERR
899 "%s: v4l_sync() - no grab active for this session\n",
900 ZR_DEVNAME(zr));
901 return -EINVAL;
904 /* check passed-in frame number */
905 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
906 dprintk(1,
907 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
908 ZR_DEVNAME(zr), frame);
909 return -EINVAL;
912 /* Check if is buffer was queued at all */
913 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
914 dprintk(1,
915 KERN_ERR
916 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
917 ZR_DEVNAME(zr));
918 return -EPROTO;
921 /* wait on this buffer to get ready */
922 if (!wait_event_interruptible_timeout(zr->v4l_capq,
923 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
924 10*HZ))
925 return -ETIME;
926 if (signal_pending(current))
927 return -ERESTARTSYS;
929 /* buffer should now be in BUZ_STATE_DONE */
930 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
931 dprintk(2,
932 KERN_ERR "%s: v4l_sync() - internal state error\n",
933 ZR_DEVNAME(zr));
935 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
936 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
938 spin_lock_irqsave(&zr->spinlock, flags);
940 /* Check if streaming capture has finished */
941 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
942 zr36057_set_memgrab(zr, 0);
943 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
944 fh->v4l_buffers.active = zr->v4l_buffers.active =
945 ZORAN_FREE;
946 zr->v4l_buffers.allocated = 0;
950 spin_unlock_irqrestore(&zr->spinlock, flags);
952 return 0;
956 * Queue a MJPEG buffer for capture/playback
959 static int
960 zoran_jpg_queue_frame (struct file *file,
961 int num,
962 enum zoran_codec_mode mode)
964 struct zoran_fh *fh = file->private_data;
965 struct zoran *zr = fh->zr;
966 unsigned long flags;
967 int res = 0;
969 /* Check if buffers are allocated */
970 if (!fh->jpg_buffers.allocated) {
971 dprintk(1,
972 KERN_ERR
973 "%s: jpg_queue_frame() - buffers not yet allocated\n",
974 ZR_DEVNAME(zr));
975 return -ENOMEM;
978 /* No grabbing outside the buffer range! */
979 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
980 dprintk(1,
981 KERN_ERR
982 "%s: jpg_queue_frame() - buffer %d out of range\n",
983 ZR_DEVNAME(zr), num);
984 return -EINVAL;
987 /* what is the codec mode right now? */
988 if (zr->codec_mode == BUZ_MODE_IDLE) {
989 zr->jpg_settings = fh->jpg_settings;
990 } else if (zr->codec_mode != mode) {
991 /* wrong codec mode active - invalid */
992 dprintk(1,
993 KERN_ERR
994 "%s: jpg_queue_frame() - codec in wrong mode\n",
995 ZR_DEVNAME(zr));
996 return -EINVAL;
999 if (fh->jpg_buffers.active == ZORAN_FREE) {
1000 if (zr->jpg_buffers.active == ZORAN_FREE) {
1001 zr->jpg_buffers = fh->jpg_buffers;
1002 fh->jpg_buffers.active = ZORAN_ACTIVE;
1003 } else {
1004 dprintk(1,
1005 KERN_ERR
1006 "%s: jpg_queue_frame() - another session is already capturing\n",
1007 ZR_DEVNAME(zr));
1008 res = -EBUSY;
1012 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1013 /* Ok load up the jpeg codec */
1014 zr36057_enable_jpg(zr, mode);
1017 spin_lock_irqsave(&zr->spinlock, flags);
1019 if (!res) {
1020 switch (zr->jpg_buffers.buffer[num].state) {
1021 case BUZ_STATE_DONE:
1022 dprintk(2,
1023 KERN_WARNING
1024 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1025 ZR_DEVNAME(zr));
1026 case BUZ_STATE_USER:
1027 /* since there is at least one unused buffer there's room for at
1028 *least one more pend[] entry */
1029 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1030 num;
1031 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1032 fh->jpg_buffers.buffer[num] =
1033 zr->jpg_buffers.buffer[num];
1034 zoran_feed_stat_com(zr);
1035 break;
1036 default:
1037 case BUZ_STATE_DMA:
1038 case BUZ_STATE_PEND:
1039 if (zr->jpg_buffers.active == ZORAN_FREE) {
1040 fh->jpg_buffers.active = ZORAN_FREE;
1041 zr->jpg_buffers.allocated = 0;
1043 res = -EBUSY; /* what are you doing? */
1044 break;
1048 spin_unlock_irqrestore(&zr->spinlock, flags);
1050 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1051 zr->jpg_buffers.active = fh->jpg_buffers.active;
1054 return res;
1057 static int
1058 jpg_qbuf (struct file *file,
1059 int frame,
1060 enum zoran_codec_mode mode)
1062 struct zoran_fh *fh = file->private_data;
1063 struct zoran *zr = fh->zr;
1064 int res = 0;
1066 /* Does the user want to stop streaming? */
1067 if (frame < 0) {
1068 if (zr->codec_mode == mode) {
1069 if (fh->jpg_buffers.active == ZORAN_FREE) {
1070 dprintk(1,
1071 KERN_ERR
1072 "%s: jpg_qbuf(-1) - session not active\n",
1073 ZR_DEVNAME(zr));
1074 return -EINVAL;
1076 fh->jpg_buffers.active = zr->jpg_buffers.active =
1077 ZORAN_FREE;
1078 zr->jpg_buffers.allocated = 0;
1079 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1080 return 0;
1081 } else {
1082 dprintk(1,
1083 KERN_ERR
1084 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1085 ZR_DEVNAME(zr));
1086 return -EINVAL;
1090 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1091 return res;
1093 /* Start the jpeg codec when the first frame is queued */
1094 if (!res && zr->jpg_que_head == 1)
1095 jpeg_start(zr);
1097 return res;
1101 * Sync on a MJPEG buffer
1104 static int
1105 jpg_sync (struct file *file,
1106 struct zoran_sync *bs)
1108 struct zoran_fh *fh = file->private_data;
1109 struct zoran *zr = fh->zr;
1110 unsigned long flags;
1111 int frame;
1113 if (fh->jpg_buffers.active == ZORAN_FREE) {
1114 dprintk(1,
1115 KERN_ERR
1116 "%s: jpg_sync() - capture is not currently active\n",
1117 ZR_DEVNAME(zr));
1118 return -EINVAL;
1120 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1121 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1122 dprintk(1,
1123 KERN_ERR
1124 "%s: jpg_sync() - codec not in streaming mode\n",
1125 ZR_DEVNAME(zr));
1126 return -EINVAL;
1128 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1129 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1130 zr->jpg_dma_tail == zr->jpg_dma_head),
1131 10*HZ)) {
1132 int isr;
1134 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1135 udelay(1);
1136 zr->codec->control(zr->codec, CODEC_G_STATUS,
1137 sizeof(isr), &isr);
1138 dprintk(1,
1139 KERN_ERR
1140 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1141 ZR_DEVNAME(zr), isr);
1143 return -ETIME;
1146 if (signal_pending(current))
1147 return -ERESTARTSYS;
1149 spin_lock_irqsave(&zr->spinlock, flags);
1151 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1152 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1153 else
1154 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1156 /* buffer should now be in BUZ_STATE_DONE */
1157 if (*zr_debug > 0)
1158 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1159 dprintk(2,
1160 KERN_ERR
1161 "%s: jpg_sync() - internal state error\n",
1162 ZR_DEVNAME(zr));
1164 *bs = zr->jpg_buffers.buffer[frame].bs;
1165 bs->frame = frame;
1166 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1167 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1169 spin_unlock_irqrestore(&zr->spinlock, flags);
1171 return 0;
1174 static void
1175 zoran_open_init_session (struct file *file)
1177 int i;
1178 struct zoran_fh *fh = file->private_data;
1179 struct zoran *zr = fh->zr;
1181 /* Per default, map the V4L Buffers */
1182 fh->map_mode = ZORAN_MAP_MODE_RAW;
1184 /* take over the card's current settings */
1185 fh->overlay_settings = zr->overlay_settings;
1186 fh->overlay_settings.is_set = 0;
1187 fh->overlay_settings.format = zr->overlay_settings.format;
1188 fh->overlay_active = ZORAN_FREE;
1190 /* v4l settings */
1191 fh->v4l_settings = zr->v4l_settings;
1193 /* v4l_buffers */
1194 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1195 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1196 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1197 fh->v4l_buffers.buffer[i].bs.frame = i;
1199 fh->v4l_buffers.allocated = 0;
1200 fh->v4l_buffers.ready_to_be_freed = 0;
1201 fh->v4l_buffers.active = ZORAN_FREE;
1202 fh->v4l_buffers.buffer_size = v4l_bufsize;
1203 fh->v4l_buffers.num_buffers = v4l_nbufs;
1205 /* jpg settings */
1206 fh->jpg_settings = zr->jpg_settings;
1208 /* jpg_buffers */
1209 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1210 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1211 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1212 fh->jpg_buffers.buffer[i].bs.frame = i;
1214 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1215 fh->jpg_buffers.allocated = 0;
1216 fh->jpg_buffers.ready_to_be_freed = 0;
1217 fh->jpg_buffers.active = ZORAN_FREE;
1218 fh->jpg_buffers.buffer_size = jpg_bufsize;
1219 fh->jpg_buffers.num_buffers = jpg_nbufs;
1222 static void
1223 zoran_close_end_session (struct file *file)
1225 struct zoran_fh *fh = file->private_data;
1226 struct zoran *zr = fh->zr;
1228 /* overlay */
1229 if (fh->overlay_active != ZORAN_FREE) {
1230 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1231 zr->v4l_overlay_active = 0;
1232 if (!zr->v4l_memgrab_active)
1233 zr36057_overlay(zr, 0);
1234 zr->overlay_mask = NULL;
1237 /* v4l capture */
1238 if (fh->v4l_buffers.active != ZORAN_FREE) {
1239 zr36057_set_memgrab(zr, 0);
1240 zr->v4l_buffers.allocated = 0;
1241 zr->v4l_buffers.active = fh->v4l_buffers.active =
1242 ZORAN_FREE;
1245 /* v4l buffers */
1246 if (fh->v4l_buffers.allocated ||
1247 fh->v4l_buffers.ready_to_be_freed) {
1248 v4l_fbuffer_free(file);
1251 /* jpg capture */
1252 if (fh->jpg_buffers.active != ZORAN_FREE) {
1253 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1254 zr->jpg_buffers.allocated = 0;
1255 zr->jpg_buffers.active = fh->jpg_buffers.active =
1256 ZORAN_FREE;
1259 /* jpg buffers */
1260 if (fh->jpg_buffers.allocated ||
1261 fh->jpg_buffers.ready_to_be_freed) {
1262 jpg_fbuffer_free(file);
1267 * Open a zoran card. Right now the flags stuff is just playing
1270 static int
1271 zoran_open (struct inode *inode,
1272 struct file *file)
1274 unsigned int minor = iminor(inode);
1275 struct zoran *zr = NULL;
1276 struct zoran_fh *fh;
1277 int i, res, first_open = 0, have_module_locks = 0;
1279 /* find the device */
1280 for (i = 0; i < zoran_num; i++) {
1281 if (zoran[i].video_dev->minor == minor) {
1282 zr = &zoran[i];
1283 break;
1287 if (!zr) {
1288 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1289 res = -ENODEV;
1290 goto open_unlock_and_return;
1293 /* see fs/device.c - the kernel already locks during open(),
1294 * so locking ourselves only causes deadlocks */
1295 /*mutex_lock(&zr->resource_lock);*/
1297 if (!zr->decoder) {
1298 dprintk(1,
1299 KERN_ERR "%s: no TV decoder loaded for device!\n",
1300 ZR_DEVNAME(zr));
1301 res = -EIO;
1302 goto open_unlock_and_return;
1305 /* try to grab a module lock */
1306 if (!try_module_get(THIS_MODULE)) {
1307 dprintk(1,
1308 KERN_ERR
1309 "%s: failed to acquire my own lock! PANIC!\n",
1310 ZR_DEVNAME(zr));
1311 res = -ENODEV;
1312 goto open_unlock_and_return;
1314 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1315 dprintk(1,
1316 KERN_ERR
1317 "%s: failed to grab ownership of i2c decoder\n",
1318 ZR_DEVNAME(zr));
1319 res = -EIO;
1320 module_put(THIS_MODULE);
1321 goto open_unlock_and_return;
1323 if (zr->encoder &&
1324 !try_module_get(zr->encoder->driver->driver.owner)) {
1325 dprintk(1,
1326 KERN_ERR
1327 "%s: failed to grab ownership of i2c encoder\n",
1328 ZR_DEVNAME(zr));
1329 res = -EIO;
1330 module_put(zr->decoder->driver->driver.owner);
1331 module_put(THIS_MODULE);
1332 goto open_unlock_and_return;
1335 have_module_locks = 1;
1337 if (zr->user >= 2048) {
1338 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1339 ZR_DEVNAME(zr), zr->user);
1340 res = -EBUSY;
1341 goto open_unlock_and_return;
1344 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1345 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1347 /* now, create the open()-specific file_ops struct */
1348 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1349 if (!fh) {
1350 dprintk(1,
1351 KERN_ERR
1352 "%s: zoran_open() - allocation of zoran_fh failed\n",
1353 ZR_DEVNAME(zr));
1354 res = -ENOMEM;
1355 goto open_unlock_and_return;
1357 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1358 * on norm-change! */
1359 fh->overlay_mask =
1360 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1361 if (!fh->overlay_mask) {
1362 dprintk(1,
1363 KERN_ERR
1364 "%s: zoran_open() - allocation of overlay_mask failed\n",
1365 ZR_DEVNAME(zr));
1366 kfree(fh);
1367 res = -ENOMEM;
1368 goto open_unlock_and_return;
1371 if (zr->user++ == 0)
1372 first_open = 1;
1374 /*mutex_unlock(&zr->resource_lock);*/
1376 /* default setup - TODO: look at flags */
1377 if (first_open) { /* First device open */
1378 zr36057_restart(zr);
1379 zoran_open_init_params(zr);
1380 zoran_init_hardware(zr);
1382 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1385 /* set file_ops stuff */
1386 file->private_data = fh;
1387 fh->zr = zr;
1388 zoran_open_init_session(file);
1390 return 0;
1392 open_unlock_and_return:
1393 /* if we grabbed locks, release them accordingly */
1394 if (have_module_locks) {
1395 module_put(zr->decoder->driver->driver.owner);
1396 if (zr->encoder) {
1397 module_put(zr->encoder->driver->driver.owner);
1399 module_put(THIS_MODULE);
1402 /* if there's no device found, we didn't obtain the lock either */
1403 if (zr) {
1404 /*mutex_unlock(&zr->resource_lock);*/
1407 return res;
1410 static int
1411 zoran_close (struct inode *inode,
1412 struct file *file)
1414 struct zoran_fh *fh = file->private_data;
1415 struct zoran *zr = fh->zr;
1417 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1418 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1420 /* kernel locks (fs/device.c), so don't do that ourselves
1421 * (prevents deadlocks) */
1422 /*mutex_lock(&zr->resource_lock);*/
1424 zoran_close_end_session(file);
1426 if (zr->user-- == 1) { /* Last process */
1427 /* Clean up JPEG process */
1428 wake_up_interruptible(&zr->jpg_capq);
1429 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1430 zr->jpg_buffers.allocated = 0;
1431 zr->jpg_buffers.active = ZORAN_FREE;
1433 /* disable interrupts */
1434 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1436 if (*zr_debug > 1)
1437 print_interrupts(zr);
1439 /* Overlay off */
1440 zr->v4l_overlay_active = 0;
1441 zr36057_overlay(zr, 0);
1442 zr->overlay_mask = NULL;
1444 /* capture off */
1445 wake_up_interruptible(&zr->v4l_capq);
1446 zr36057_set_memgrab(zr, 0);
1447 zr->v4l_buffers.allocated = 0;
1448 zr->v4l_buffers.active = ZORAN_FREE;
1449 zoran_set_pci_master(zr, 0);
1451 if (!pass_through) { /* Switch to color bar */
1452 int zero = 0, two = 2;
1453 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1454 encoder_command(zr, ENCODER_SET_INPUT, &two);
1458 file->private_data = NULL;
1459 kfree(fh->overlay_mask);
1460 kfree(fh);
1462 /* release locks on the i2c modules */
1463 module_put(zr->decoder->driver->driver.owner);
1464 if (zr->encoder) {
1465 module_put(zr->encoder->driver->driver.owner);
1467 module_put(THIS_MODULE);
1469 /*mutex_unlock(&zr->resource_lock);*/
1471 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1473 return 0;
1477 static ssize_t
1478 zoran_read (struct file *file,
1479 char __user *data,
1480 size_t count,
1481 loff_t *ppos)
1483 /* we simply don't support read() (yet)... */
1485 return -EINVAL;
1488 static ssize_t
1489 zoran_write (struct file *file,
1490 const char __user *data,
1491 size_t count,
1492 loff_t *ppos)
1494 /* ...and the same goes for write() */
1496 return -EINVAL;
1499 static int
1500 setup_fbuffer (struct file *file,
1501 void *base,
1502 const struct zoran_format *fmt,
1503 int width,
1504 int height,
1505 int bytesperline)
1507 struct zoran_fh *fh = file->private_data;
1508 struct zoran *zr = fh->zr;
1510 /* (Ronald) v4l/v4l2 guidelines */
1511 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1512 return -EPERM;
1514 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1515 ALi Magik (that needs very low latency while the card needs a
1516 higher value always) */
1518 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1519 return -ENXIO;
1521 /* we need a bytesperline value, even if not given */
1522 if (!bytesperline)
1523 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1525 #if 0
1526 if (zr->overlay_active) {
1527 /* dzjee... stupid users... don't even bother to turn off
1528 * overlay before changing the memory location...
1529 * normally, we would return errors here. However, one of
1530 * the tools that does this is... xawtv! and since xawtv
1531 * is used by +/- 99% of the users, we'd rather be user-
1532 * friendly and silently do as if nothing went wrong */
1533 dprintk(3,
1534 KERN_ERR
1535 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1536 ZR_DEVNAME(zr));
1537 zr36057_overlay(zr, 0);
1539 #endif
1541 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1542 dprintk(1,
1543 KERN_ERR
1544 "%s: setup_fbuffer() - no valid overlay format given\n",
1545 ZR_DEVNAME(zr));
1546 return -EINVAL;
1548 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1549 dprintk(1,
1550 KERN_ERR
1551 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1552 ZR_DEVNAME(zr), width, height, bytesperline);
1553 return -EINVAL;
1555 if (bytesperline & 3) {
1556 dprintk(1,
1557 KERN_ERR
1558 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1559 ZR_DEVNAME(zr), bytesperline);
1560 return -EINVAL;
1563 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1564 zr->buffer.height = height;
1565 zr->buffer.width = width;
1566 zr->buffer.depth = fmt->depth;
1567 zr->overlay_settings.format = fmt;
1568 zr->buffer.bytesperline = bytesperline;
1570 /* The user should set new window parameters */
1571 zr->overlay_settings.is_set = 0;
1573 return 0;
1577 static int
1578 setup_window (struct file *file,
1579 int x,
1580 int y,
1581 int width,
1582 int height,
1583 struct video_clip __user *clips,
1584 int clipcount,
1585 void __user *bitmap)
1587 struct zoran_fh *fh = file->private_data;
1588 struct zoran *zr = fh->zr;
1589 struct video_clip *vcp = NULL;
1590 int on, end;
1593 if (!zr->buffer.base) {
1594 dprintk(1,
1595 KERN_ERR
1596 "%s: setup_window() - frame buffer has to be set first\n",
1597 ZR_DEVNAME(zr));
1598 return -EINVAL;
1601 if (!fh->overlay_settings.format) {
1602 dprintk(1,
1603 KERN_ERR
1604 "%s: setup_window() - no overlay format set\n",
1605 ZR_DEVNAME(zr));
1606 return -EINVAL;
1610 * The video front end needs 4-byte alinged line sizes, we correct that
1611 * silently here if necessary
1613 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1614 end = (x + width) & ~1; /* round down */
1615 x = (x + 1) & ~1; /* round up */
1616 width = end - x;
1619 if (zr->buffer.depth == 24) {
1620 end = (x + width) & ~3; /* round down */
1621 x = (x + 3) & ~3; /* round up */
1622 width = end - x;
1625 if (width > BUZ_MAX_WIDTH)
1626 width = BUZ_MAX_WIDTH;
1627 if (height > BUZ_MAX_HEIGHT)
1628 height = BUZ_MAX_HEIGHT;
1630 /* Check for vaild parameters */
1631 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1632 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1633 dprintk(1,
1634 KERN_ERR
1635 "%s: setup_window() - width = %d or height = %d invalid\n",
1636 ZR_DEVNAME(zr), width, height);
1637 return -EINVAL;
1640 fh->overlay_settings.x = x;
1641 fh->overlay_settings.y = y;
1642 fh->overlay_settings.width = width;
1643 fh->overlay_settings.height = height;
1644 fh->overlay_settings.clipcount = clipcount;
1647 * If an overlay is running, we have to switch it off
1648 * and switch it on again in order to get the new settings in effect.
1650 * We also want to avoid that the overlay mask is written
1651 * when an overlay is running.
1654 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1655 zr->overlay_active != ZORAN_FREE &&
1656 fh->overlay_active != ZORAN_FREE;
1657 if (on)
1658 zr36057_overlay(zr, 0);
1661 * Write the overlay mask if clips are wanted.
1662 * We prefer a bitmap.
1664 if (bitmap) {
1665 /* fake value - it just means we want clips */
1666 fh->overlay_settings.clipcount = 1;
1668 if (copy_from_user(fh->overlay_mask, bitmap,
1669 (width * height + 7) / 8)) {
1670 return -EFAULT;
1672 } else if (clipcount > 0) {
1673 /* write our own bitmap from the clips */
1674 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1675 if (vcp == NULL) {
1676 dprintk(1,
1677 KERN_ERR
1678 "%s: setup_window() - Alloc of clip mask failed\n",
1679 ZR_DEVNAME(zr));
1680 return -ENOMEM;
1682 if (copy_from_user
1683 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1684 vfree(vcp);
1685 return -EFAULT;
1687 write_overlay_mask(file, vcp, clipcount);
1688 vfree(vcp);
1691 fh->overlay_settings.is_set = 1;
1692 if (fh->overlay_active != ZORAN_FREE &&
1693 zr->overlay_active != ZORAN_FREE)
1694 zr->overlay_settings = fh->overlay_settings;
1696 if (on)
1697 zr36057_overlay(zr, 1);
1699 /* Make sure the changes come into effect */
1700 return wait_grab_pending(zr);
1703 static int
1704 setup_overlay (struct file *file,
1705 int on)
1707 struct zoran_fh *fh = file->private_data;
1708 struct zoran *zr = fh->zr;
1710 /* If there is nothing to do, return immediatly */
1711 if ((on && fh->overlay_active != ZORAN_FREE) ||
1712 (!on && fh->overlay_active == ZORAN_FREE))
1713 return 0;
1715 /* check whether we're touching someone else's overlay */
1716 if (on && zr->overlay_active != ZORAN_FREE &&
1717 fh->overlay_active == ZORAN_FREE) {
1718 dprintk(1,
1719 KERN_ERR
1720 "%s: setup_overlay() - overlay is already active for another session\n",
1721 ZR_DEVNAME(zr));
1722 return -EBUSY;
1724 if (!on && zr->overlay_active != ZORAN_FREE &&
1725 fh->overlay_active == ZORAN_FREE) {
1726 dprintk(1,
1727 KERN_ERR
1728 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1729 ZR_DEVNAME(zr));
1730 return -EPERM;
1733 if (on == 0) {
1734 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1735 zr->v4l_overlay_active = 0;
1736 /* When a grab is running, the video simply
1737 * won't be switched on any more */
1738 if (!zr->v4l_memgrab_active)
1739 zr36057_overlay(zr, 0);
1740 zr->overlay_mask = NULL;
1741 } else {
1742 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1743 dprintk(1,
1744 KERN_ERR
1745 "%s: setup_overlay() - buffer or window not set\n",
1746 ZR_DEVNAME(zr));
1747 return -EINVAL;
1749 if (!fh->overlay_settings.format) {
1750 dprintk(1,
1751 KERN_ERR
1752 "%s: setup_overlay() - no overlay format set\n",
1753 ZR_DEVNAME(zr));
1754 return -EINVAL;
1756 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1757 zr->v4l_overlay_active = 1;
1758 zr->overlay_mask = fh->overlay_mask;
1759 zr->overlay_settings = fh->overlay_settings;
1760 if (!zr->v4l_memgrab_active)
1761 zr36057_overlay(zr, 1);
1762 /* When a grab is running, the video will be
1763 * switched on when grab is finished */
1766 /* Make sure the changes come into effect */
1767 return wait_grab_pending(zr);
1770 #ifdef CONFIG_VIDEO_V4L2
1771 /* get the status of a buffer in the clients buffer queue */
1772 static int
1773 zoran_v4l2_buffer_status (struct file *file,
1774 struct v4l2_buffer *buf,
1775 int num)
1777 struct zoran_fh *fh = file->private_data;
1778 struct zoran *zr = fh->zr;
1780 buf->flags = V4L2_BUF_FLAG_MAPPED;
1782 switch (fh->map_mode) {
1783 case ZORAN_MAP_MODE_RAW:
1785 /* check range */
1786 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1787 !fh->v4l_buffers.allocated) {
1788 dprintk(1,
1789 KERN_ERR
1790 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1791 ZR_DEVNAME(zr));
1792 return -EINVAL;
1795 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1796 buf->length = fh->v4l_buffers.buffer_size;
1798 /* get buffer */
1799 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1800 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1801 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1802 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1803 buf->flags |= V4L2_BUF_FLAG_DONE;
1804 buf->timestamp =
1805 fh->v4l_buffers.buffer[num].bs.timestamp;
1806 } else {
1807 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1810 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1811 buf->field = V4L2_FIELD_TOP;
1812 else
1813 buf->field = V4L2_FIELD_INTERLACED;
1815 break;
1817 case ZORAN_MAP_MODE_JPG_REC:
1818 case ZORAN_MAP_MODE_JPG_PLAY:
1820 /* check range */
1821 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1822 !fh->jpg_buffers.allocated) {
1823 dprintk(1,
1824 KERN_ERR
1825 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1826 ZR_DEVNAME(zr));
1827 return -EINVAL;
1830 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1831 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1832 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1833 buf->length = fh->jpg_buffers.buffer_size;
1835 /* these variables are only written after frame has been captured */
1836 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1837 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1838 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1839 buf->timestamp =
1840 fh->jpg_buffers.buffer[num].bs.timestamp;
1841 buf->bytesused =
1842 fh->jpg_buffers.buffer[num].bs.length;
1843 buf->flags |= V4L2_BUF_FLAG_DONE;
1844 } else {
1845 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1848 /* which fields are these? */
1849 if (fh->jpg_settings.TmpDcm != 1)
1850 buf->field =
1851 fh->jpg_settings.
1852 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1853 else
1854 buf->field =
1855 fh->jpg_settings.
1856 odd_even ? V4L2_FIELD_SEQ_TB :
1857 V4L2_FIELD_SEQ_BT;
1859 break;
1861 default:
1863 dprintk(5,
1864 KERN_ERR
1865 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1866 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1867 return -EINVAL;
1870 buf->memory = V4L2_MEMORY_MMAP;
1871 buf->index = num;
1872 buf->m.offset = buf->length * num;
1874 return 0;
1876 #endif
1878 static int
1879 zoran_set_norm (struct zoran *zr,
1880 int norm) /* VIDEO_MODE_* */
1882 int norm_encoder, on;
1884 if (zr->v4l_buffers.active != ZORAN_FREE ||
1885 zr->jpg_buffers.active != ZORAN_FREE) {
1886 dprintk(1,
1887 KERN_WARNING
1888 "%s: set_norm() called while in playback/capture mode\n",
1889 ZR_DEVNAME(zr));
1890 return -EBUSY;
1893 if (lock_norm && norm != zr->norm) {
1894 if (lock_norm > 1) {
1895 dprintk(1,
1896 KERN_WARNING
1897 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1898 ZR_DEVNAME(zr));
1899 return -EPERM;
1900 } else {
1901 dprintk(1,
1902 KERN_WARNING
1903 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1904 ZR_DEVNAME(zr));
1905 norm = zr->norm;
1909 if (norm != VIDEO_MODE_AUTO &&
1910 (norm < 0 || norm >= zr->card.norms ||
1911 !zr->card.tvn[norm])) {
1912 dprintk(1,
1913 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1914 ZR_DEVNAME(zr), norm);
1915 return -EINVAL;
1918 if (norm == VIDEO_MODE_AUTO) {
1919 int status;
1921 /* if we have autodetect, ... */
1922 struct video_decoder_capability caps;
1923 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1924 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1925 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1926 ZR_DEVNAME(zr));
1927 return -EINVAL;
1930 decoder_command(zr, DECODER_SET_NORM, &norm);
1932 /* let changes come into effect */
1933 ssleep(2);
1935 decoder_command(zr, DECODER_GET_STATUS, &status);
1936 if (!(status & DECODER_STATUS_GOOD)) {
1937 dprintk(1,
1938 KERN_ERR
1939 "%s: set_norm() - no norm detected\n",
1940 ZR_DEVNAME(zr));
1941 /* reset norm */
1942 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1943 return -EIO;
1946 if (status & DECODER_STATUS_NTSC)
1947 norm = VIDEO_MODE_NTSC;
1948 else if (status & DECODER_STATUS_SECAM)
1949 norm = VIDEO_MODE_SECAM;
1950 else
1951 norm = VIDEO_MODE_PAL;
1953 zr->timing = zr->card.tvn[norm];
1954 norm_encoder = norm;
1956 /* We switch overlay off and on since a change in the
1957 * norm needs different VFE settings */
1958 on = zr->overlay_active && !zr->v4l_memgrab_active;
1959 if (on)
1960 zr36057_overlay(zr, 0);
1962 decoder_command(zr, DECODER_SET_NORM, &norm);
1963 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1965 if (on)
1966 zr36057_overlay(zr, 1);
1968 /* Make sure the changes come into effect */
1969 zr->norm = norm;
1971 return 0;
1974 static int
1975 zoran_set_input (struct zoran *zr,
1976 int input)
1978 int realinput;
1980 if (input == zr->input) {
1981 return 0;
1984 if (zr->v4l_buffers.active != ZORAN_FREE ||
1985 zr->jpg_buffers.active != ZORAN_FREE) {
1986 dprintk(1,
1987 KERN_WARNING
1988 "%s: set_input() called while in playback/capture mode\n",
1989 ZR_DEVNAME(zr));
1990 return -EBUSY;
1993 if (input < 0 || input >= zr->card.inputs) {
1994 dprintk(1,
1995 KERN_ERR
1996 "%s: set_input() - unnsupported input %d\n",
1997 ZR_DEVNAME(zr), input);
1998 return -EINVAL;
2001 realinput = zr->card.input[input].muxsel;
2002 zr->input = input;
2004 decoder_command(zr, DECODER_SET_INPUT, &realinput);
2006 return 0;
2010 * ioctl routine
2013 static int
2014 zoran_do_ioctl (struct inode *inode,
2015 struct file *file,
2016 unsigned int cmd,
2017 void *arg)
2019 struct zoran_fh *fh = file->private_data;
2020 struct zoran *zr = fh->zr;
2021 /* CAREFUL: used in multiple places here */
2022 struct zoran_jpg_settings settings;
2024 /* we might have older buffers lying around... We don't want
2025 * to wait, but we do want to try cleaning them up ASAP. So
2026 * we try to obtain the lock and free them. If that fails, we
2027 * don't do anything and wait for the next turn. In the end,
2028 * zoran_close() or a new allocation will still free them...
2029 * This is just a 'the sooner the better' extra 'feature'
2031 * We don't free the buffers right on munmap() because that
2032 * causes oopses (kfree() inside munmap() oopses for no
2033 * apparent reason - it's also not reproduceable in any way,
2034 * but moving the free code outside the munmap() handler fixes
2035 * all this... If someone knows why, please explain me (Ronald)
2037 if (mutex_trylock(&zr->resource_lock)) {
2038 /* we obtained it! Let's try to free some things */
2039 if (fh->jpg_buffers.ready_to_be_freed)
2040 jpg_fbuffer_free(file);
2041 if (fh->v4l_buffers.ready_to_be_freed)
2042 v4l_fbuffer_free(file);
2044 mutex_unlock(&zr->resource_lock);
2047 switch (cmd) {
2049 case VIDIOCGCAP:
2051 struct video_capability *vcap = arg;
2053 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2055 memset(vcap, 0, sizeof(struct video_capability));
2056 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
2057 vcap->type = ZORAN_VID_TYPE;
2059 vcap->channels = zr->card.inputs;
2060 vcap->audios = 0;
2061 mutex_lock(&zr->resource_lock);
2062 vcap->maxwidth = BUZ_MAX_WIDTH;
2063 vcap->maxheight = BUZ_MAX_HEIGHT;
2064 vcap->minwidth = BUZ_MIN_WIDTH;
2065 vcap->minheight = BUZ_MIN_HEIGHT;
2066 mutex_unlock(&zr->resource_lock);
2068 return 0;
2070 break;
2072 case VIDIOCGCHAN:
2074 struct video_channel *vchan = arg;
2075 int channel = vchan->channel;
2077 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2078 ZR_DEVNAME(zr), vchan->channel);
2080 memset(vchan, 0, sizeof(struct video_channel));
2081 if (channel > zr->card.inputs || channel < 0) {
2082 dprintk(1,
2083 KERN_ERR
2084 "%s: VIDIOCGCHAN on not existing channel %d\n",
2085 ZR_DEVNAME(zr), channel);
2086 return -EINVAL;
2089 strcpy(vchan->name, zr->card.input[channel].name);
2091 vchan->tuners = 0;
2092 vchan->flags = 0;
2093 vchan->type = VIDEO_TYPE_CAMERA;
2094 mutex_lock(&zr->resource_lock);
2095 vchan->norm = zr->norm;
2096 mutex_unlock(&zr->resource_lock);
2097 vchan->channel = channel;
2099 return 0;
2101 break;
2103 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2105 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2106 * * ^^^^^^^
2107 * * The famos BTTV driver has it implemented with a struct video_channel argument
2108 * * and we follow it for compatibility reasons
2110 * * BTW: this is the only way the user can set the norm!
2113 case VIDIOCSCHAN:
2115 struct video_channel *vchan = arg;
2116 int res;
2118 dprintk(3,
2119 KERN_DEBUG
2120 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2121 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2123 mutex_lock(&zr->resource_lock);
2124 if ((res = zoran_set_input(zr, vchan->channel)))
2125 goto schan_unlock_and_return;
2126 if ((res = zoran_set_norm(zr, vchan->norm)))
2127 goto schan_unlock_and_return;
2129 /* Make sure the changes come into effect */
2130 res = wait_grab_pending(zr);
2131 schan_unlock_and_return:
2132 mutex_unlock(&zr->resource_lock);
2133 return res;
2135 break;
2137 case VIDIOCGPICT:
2139 struct video_picture *vpict = arg;
2141 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2143 memset(vpict, 0, sizeof(struct video_picture));
2144 mutex_lock(&zr->resource_lock);
2145 vpict->hue = zr->hue;
2146 vpict->brightness = zr->brightness;
2147 vpict->contrast = zr->contrast;
2148 vpict->colour = zr->saturation;
2149 if (fh->overlay_settings.format) {
2150 vpict->depth = fh->overlay_settings.format->depth;
2151 vpict->palette = fh->overlay_settings.format->palette;
2152 } else {
2153 vpict->depth = 0;
2155 mutex_unlock(&zr->resource_lock);
2157 return 0;
2159 break;
2161 case VIDIOCSPICT:
2163 struct video_picture *vpict = arg;
2164 int i;
2166 dprintk(3,
2167 KERN_DEBUG
2168 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2169 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2170 vpict->colour, vpict->contrast, vpict->depth,
2171 vpict->palette);
2173 for (i = 0; i < zoran_num_formats; i++) {
2174 const struct zoran_format *fmt = &zoran_formats[i];
2176 if (fmt->palette != -1 &&
2177 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2178 fmt->palette == vpict->palette &&
2179 fmt->depth == vpict->depth)
2180 break;
2182 if (i == zoran_num_formats) {
2183 dprintk(1,
2184 KERN_ERR
2185 "%s: VIDIOCSPICT - Invalid palette %d\n",
2186 ZR_DEVNAME(zr), vpict->palette);
2187 return -EINVAL;
2190 mutex_lock(&zr->resource_lock);
2192 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2194 zr->hue = vpict->hue;
2195 zr->contrast = vpict->contrast;
2196 zr->saturation = vpict->colour;
2197 zr->brightness = vpict->brightness;
2199 fh->overlay_settings.format = &zoran_formats[i];
2201 mutex_unlock(&zr->resource_lock);
2203 return 0;
2205 break;
2207 case VIDIOCCAPTURE:
2209 int *on = arg, res;
2211 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2212 ZR_DEVNAME(zr), *on);
2214 mutex_lock(&zr->resource_lock);
2215 res = setup_overlay(file, *on);
2216 mutex_unlock(&zr->resource_lock);
2218 return res;
2220 break;
2222 case VIDIOCGWIN:
2224 struct video_window *vwin = arg;
2226 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2228 memset(vwin, 0, sizeof(struct video_window));
2229 mutex_lock(&zr->resource_lock);
2230 vwin->x = fh->overlay_settings.x;
2231 vwin->y = fh->overlay_settings.y;
2232 vwin->width = fh->overlay_settings.width;
2233 vwin->height = fh->overlay_settings.height;
2234 mutex_unlock(&zr->resource_lock);
2235 vwin->clipcount = 0;
2236 return 0;
2238 break;
2240 case VIDIOCSWIN:
2242 struct video_window *vwin = arg;
2243 int res;
2245 dprintk(3,
2246 KERN_DEBUG
2247 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2248 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2249 vwin->height, vwin->clipcount);
2251 mutex_lock(&zr->resource_lock);
2252 res =
2253 setup_window(file, vwin->x, vwin->y, vwin->width,
2254 vwin->height, vwin->clips,
2255 vwin->clipcount, NULL);
2256 mutex_unlock(&zr->resource_lock);
2258 return res;
2260 break;
2262 case VIDIOCGFBUF:
2264 struct video_buffer *vbuf = arg;
2266 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2268 mutex_lock(&zr->resource_lock);
2269 *vbuf = zr->buffer;
2270 mutex_unlock(&zr->resource_lock);
2271 return 0;
2273 break;
2275 case VIDIOCSFBUF:
2277 struct video_buffer *vbuf = arg;
2278 int i, res = 0;
2280 dprintk(3,
2281 KERN_DEBUG
2282 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2283 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2284 vbuf->height, vbuf->depth, vbuf->bytesperline);
2286 for (i = 0; i < zoran_num_formats; i++)
2287 if (zoran_formats[i].depth == vbuf->depth)
2288 break;
2289 if (i == zoran_num_formats) {
2290 dprintk(1,
2291 KERN_ERR
2292 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2293 ZR_DEVNAME(zr), vbuf->depth);
2294 return -EINVAL;
2297 mutex_lock(&zr->resource_lock);
2298 res =
2299 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2300 vbuf->width, vbuf->height,
2301 vbuf->bytesperline);
2302 mutex_unlock(&zr->resource_lock);
2304 return res;
2306 break;
2308 case VIDIOCSYNC:
2310 int *frame = arg, res;
2312 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2313 ZR_DEVNAME(zr), *frame);
2315 mutex_lock(&zr->resource_lock);
2316 res = v4l_sync(file, *frame);
2317 mutex_unlock(&zr->resource_lock);
2318 if (!res)
2319 zr->v4l_sync_tail++;
2320 return res;
2322 break;
2324 case VIDIOCMCAPTURE:
2326 struct video_mmap *vmap = arg;
2327 int res;
2329 dprintk(3,
2330 KERN_DEBUG
2331 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2332 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2333 vmap->format);
2335 mutex_lock(&zr->resource_lock);
2336 res = v4l_grab(file, vmap);
2337 mutex_unlock(&zr->resource_lock);
2338 return res;
2340 break;
2342 case VIDIOCGMBUF:
2344 struct video_mbuf *vmbuf = arg;
2345 int i, res = 0;
2347 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2349 vmbuf->size =
2350 fh->v4l_buffers.num_buffers *
2351 fh->v4l_buffers.buffer_size;
2352 vmbuf->frames = fh->v4l_buffers.num_buffers;
2353 for (i = 0; i < vmbuf->frames; i++) {
2354 vmbuf->offsets[i] =
2355 i * fh->v4l_buffers.buffer_size;
2358 mutex_lock(&zr->resource_lock);
2360 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2361 dprintk(1,
2362 KERN_ERR
2363 "%s: VIDIOCGMBUF - buffers already allocated\n",
2364 ZR_DEVNAME(zr));
2365 res = -EINVAL;
2366 goto v4l1reqbuf_unlock_and_return;
2369 if (v4l_fbuffer_alloc(file)) {
2370 res = -ENOMEM;
2371 goto v4l1reqbuf_unlock_and_return;
2374 /* The next mmap will map the V4L buffers */
2375 fh->map_mode = ZORAN_MAP_MODE_RAW;
2376 v4l1reqbuf_unlock_and_return:
2377 mutex_unlock(&zr->resource_lock);
2379 return res;
2381 break;
2383 case VIDIOCGUNIT:
2385 struct video_unit *vunit = arg;
2387 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2389 vunit->video = zr->video_dev->minor;
2390 vunit->vbi = VIDEO_NO_UNIT;
2391 vunit->radio = VIDEO_NO_UNIT;
2392 vunit->audio = VIDEO_NO_UNIT;
2393 vunit->teletext = VIDEO_NO_UNIT;
2395 return 0;
2397 break;
2400 * RJ: In principal we could support subcaptures for V4L grabbing.
2401 * Not even the famous BTTV driver has them, however.
2402 * If there should be a strong demand, one could consider
2403 * to implement them.
2405 case VIDIOCGCAPTURE:
2407 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2408 ZR_DEVNAME(zr));
2409 return -EINVAL;
2411 break;
2413 case VIDIOCSCAPTURE:
2415 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2416 ZR_DEVNAME(zr));
2417 return -EINVAL;
2419 break;
2421 case BUZIOC_G_PARAMS:
2423 struct zoran_params *bparams = arg;
2425 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2427 memset(bparams, 0, sizeof(struct zoran_params));
2428 bparams->major_version = MAJOR_VERSION;
2429 bparams->minor_version = MINOR_VERSION;
2431 mutex_lock(&zr->resource_lock);
2433 bparams->norm = zr->norm;
2434 bparams->input = zr->input;
2436 bparams->decimation = fh->jpg_settings.decimation;
2437 bparams->HorDcm = fh->jpg_settings.HorDcm;
2438 bparams->VerDcm = fh->jpg_settings.VerDcm;
2439 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2440 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2441 bparams->img_x = fh->jpg_settings.img_x;
2442 bparams->img_y = fh->jpg_settings.img_y;
2443 bparams->img_width = fh->jpg_settings.img_width;
2444 bparams->img_height = fh->jpg_settings.img_height;
2445 bparams->odd_even = fh->jpg_settings.odd_even;
2447 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2448 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2449 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2450 memcpy(bparams->APP_data,
2451 fh->jpg_settings.jpg_comp.APP_data,
2452 sizeof(bparams->APP_data));
2453 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2454 memcpy(bparams->COM_data,
2455 fh->jpg_settings.jpg_comp.COM_data,
2456 sizeof(bparams->COM_data));
2457 bparams->jpeg_markers =
2458 fh->jpg_settings.jpg_comp.jpeg_markers;
2460 mutex_unlock(&zr->resource_lock);
2462 bparams->VFIFO_FB = 0;
2464 return 0;
2466 break;
2468 case BUZIOC_S_PARAMS:
2470 struct zoran_params *bparams = arg;
2471 int res = 0;
2473 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2475 settings.decimation = bparams->decimation;
2476 settings.HorDcm = bparams->HorDcm;
2477 settings.VerDcm = bparams->VerDcm;
2478 settings.TmpDcm = bparams->TmpDcm;
2479 settings.field_per_buff = bparams->field_per_buff;
2480 settings.img_x = bparams->img_x;
2481 settings.img_y = bparams->img_y;
2482 settings.img_width = bparams->img_width;
2483 settings.img_height = bparams->img_height;
2484 settings.odd_even = bparams->odd_even;
2486 settings.jpg_comp.quality = bparams->quality;
2487 settings.jpg_comp.APPn = bparams->APPn;
2488 settings.jpg_comp.APP_len = bparams->APP_len;
2489 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2490 sizeof(bparams->APP_data));
2491 settings.jpg_comp.COM_len = bparams->COM_len;
2492 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2493 sizeof(bparams->COM_data));
2494 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2496 mutex_lock(&zr->resource_lock);
2498 if (zr->codec_mode != BUZ_MODE_IDLE) {
2499 dprintk(1,
2500 KERN_ERR
2501 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2502 ZR_DEVNAME(zr));
2503 res = -EINVAL;
2504 goto sparams_unlock_and_return;
2507 /* Check the params first before overwriting our
2508 * nternal values */
2509 if (zoran_check_jpg_settings(zr, &settings)) {
2510 res = -EINVAL;
2511 goto sparams_unlock_and_return;
2514 fh->jpg_settings = settings;
2515 sparams_unlock_and_return:
2516 mutex_unlock(&zr->resource_lock);
2518 return res;
2520 break;
2522 case BUZIOC_REQBUFS:
2524 struct zoran_requestbuffers *breq = arg;
2525 int res = 0;
2527 dprintk(3,
2528 KERN_DEBUG
2529 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2530 ZR_DEVNAME(zr), breq->count, breq->size);
2532 /* Enforce reasonable lower and upper limits */
2533 if (breq->count < 4)
2534 breq->count = 4; /* Could be choosen smaller */
2535 if (breq->count > jpg_nbufs)
2536 breq->count = jpg_nbufs;
2537 breq->size = PAGE_ALIGN(breq->size);
2538 if (breq->size < 8192)
2539 breq->size = 8192; /* Arbitrary */
2540 /* breq->size is limited by 1 page for the stat_com
2541 * tables to a Maximum of 2 MB */
2542 if (breq->size > jpg_bufsize)
2543 breq->size = jpg_bufsize;
2544 if (fh->jpg_buffers.need_contiguous &&
2545 breq->size > MAX_KMALLOC_MEM)
2546 breq->size = MAX_KMALLOC_MEM;
2548 mutex_lock(&zr->resource_lock);
2550 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2551 dprintk(1,
2552 KERN_ERR
2553 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2554 ZR_DEVNAME(zr));
2555 res = -EBUSY;
2556 goto jpgreqbuf_unlock_and_return;
2559 fh->jpg_buffers.num_buffers = breq->count;
2560 fh->jpg_buffers.buffer_size = breq->size;
2562 if (jpg_fbuffer_alloc(file)) {
2563 res = -ENOMEM;
2564 goto jpgreqbuf_unlock_and_return;
2567 /* The next mmap will map the MJPEG buffers - could
2568 * also be *_PLAY, but it doesn't matter here */
2569 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2570 jpgreqbuf_unlock_and_return:
2571 mutex_unlock(&zr->resource_lock);
2573 return res;
2575 break;
2577 case BUZIOC_QBUF_CAPT:
2579 int *frame = arg, res;
2581 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2582 ZR_DEVNAME(zr), *frame);
2584 mutex_lock(&zr->resource_lock);
2585 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2586 mutex_unlock(&zr->resource_lock);
2588 return res;
2590 break;
2592 case BUZIOC_QBUF_PLAY:
2594 int *frame = arg, res;
2596 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2597 ZR_DEVNAME(zr), *frame);
2599 mutex_lock(&zr->resource_lock);
2600 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2601 mutex_unlock(&zr->resource_lock);
2603 return res;
2605 break;
2607 case BUZIOC_SYNC:
2609 struct zoran_sync *bsync = arg;
2610 int res;
2612 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2614 mutex_lock(&zr->resource_lock);
2615 res = jpg_sync(file, bsync);
2616 mutex_unlock(&zr->resource_lock);
2618 return res;
2620 break;
2622 case BUZIOC_G_STATUS:
2624 struct zoran_status *bstat = arg;
2625 int norm, input, status, res = 0;
2627 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2629 if (zr->codec_mode != BUZ_MODE_IDLE) {
2630 dprintk(1,
2631 KERN_ERR
2632 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2633 ZR_DEVNAME(zr));
2634 return -EINVAL;
2637 input = zr->card.input[bstat->input].muxsel;
2638 norm = VIDEO_MODE_AUTO;
2640 mutex_lock(&zr->resource_lock);
2642 if (zr->codec_mode != BUZ_MODE_IDLE) {
2643 dprintk(1,
2644 KERN_ERR
2645 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2646 ZR_DEVNAME(zr));
2647 res = -EINVAL;
2648 goto gstat_unlock_and_return;
2651 decoder_command(zr, DECODER_SET_INPUT, &input);
2652 decoder_command(zr, DECODER_SET_NORM, &norm);
2654 /* sleep 1 second */
2655 ssleep(1);
2657 /* Get status of video decoder */
2658 decoder_command(zr, DECODER_GET_STATUS, &status);
2660 /* restore previous input and norm */
2661 input = zr->card.input[zr->input].muxsel;
2662 decoder_command(zr, DECODER_SET_INPUT, &input);
2663 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2664 gstat_unlock_and_return:
2665 mutex_unlock(&zr->resource_lock);
2667 if (!res) {
2668 bstat->signal =
2669 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2670 if (status & DECODER_STATUS_NTSC)
2671 bstat->norm = VIDEO_MODE_NTSC;
2672 else if (status & DECODER_STATUS_SECAM)
2673 bstat->norm = VIDEO_MODE_SECAM;
2674 else
2675 bstat->norm = VIDEO_MODE_PAL;
2677 bstat->color =
2678 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2681 return res;
2683 break;
2685 #ifdef CONFIG_VIDEO_V4L2
2687 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2688 * it allows for integrating the JPEG capturing calls inside standard v4l2
2691 case VIDIOC_QUERYCAP:
2693 struct v4l2_capability *cap = arg;
2695 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2697 memset(cap, 0, sizeof(*cap));
2698 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2699 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2700 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2701 pci_name(zr->pci_dev));
2702 cap->version =
2703 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2704 RELEASE_VERSION);
2705 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2707 return 0;
2709 break;
2711 case VIDIOC_ENUM_FMT:
2713 struct v4l2_fmtdesc *fmt = arg;
2714 int index = fmt->index, num = -1, i, flag = 0, type =
2715 fmt->type;
2717 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2718 ZR_DEVNAME(zr), fmt->index);
2720 switch (fmt->type) {
2721 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2722 flag = ZORAN_FORMAT_CAPTURE;
2723 break;
2724 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2725 flag = ZORAN_FORMAT_PLAYBACK;
2726 break;
2727 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2728 flag = ZORAN_FORMAT_OVERLAY;
2729 break;
2730 default:
2731 dprintk(1,
2732 KERN_ERR
2733 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2734 ZR_DEVNAME(zr), fmt->type);
2735 return -EINVAL;
2738 for (i = 0; i < zoran_num_formats; i++) {
2739 if (zoran_formats[i].flags & flag)
2740 num++;
2741 if (num == fmt->index)
2742 break;
2744 if (fmt->index < 0 /* late, but not too late */ ||
2745 i == zoran_num_formats)
2746 return -EINVAL;
2748 memset(fmt, 0, sizeof(*fmt));
2749 fmt->index = index;
2750 fmt->type = type;
2751 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2752 fmt->pixelformat = zoran_formats[i].fourcc;
2753 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2754 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2756 return 0;
2758 break;
2760 case VIDIOC_G_FMT:
2762 struct v4l2_format *fmt = arg;
2763 int type = fmt->type;
2765 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2767 memset(fmt, 0, sizeof(*fmt));
2768 fmt->type = type;
2770 switch (fmt->type) {
2771 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2773 mutex_lock(&zr->resource_lock);
2775 fmt->fmt.win.w.left = fh->overlay_settings.x;
2776 fmt->fmt.win.w.top = fh->overlay_settings.y;
2777 fmt->fmt.win.w.width = fh->overlay_settings.width;
2778 fmt->fmt.win.w.height =
2779 fh->overlay_settings.height;
2780 if (fh->overlay_settings.width * 2 >
2781 BUZ_MAX_HEIGHT)
2782 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2783 else
2784 fmt->fmt.win.field = V4L2_FIELD_TOP;
2786 mutex_unlock(&zr->resource_lock);
2788 break;
2790 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2791 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2793 mutex_lock(&zr->resource_lock);
2795 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2796 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2798 fmt->fmt.pix.width =
2799 fh->v4l_settings.width;
2800 fmt->fmt.pix.height =
2801 fh->v4l_settings.height;
2802 fmt->fmt.pix.sizeimage =
2803 fh->v4l_buffers.buffer_size;
2804 fmt->fmt.pix.pixelformat =
2805 fh->v4l_settings.format->fourcc;
2806 fmt->fmt.pix.colorspace =
2807 fh->v4l_settings.format->colorspace;
2808 fmt->fmt.pix.bytesperline = 0;
2809 if (BUZ_MAX_HEIGHT <
2810 (fh->v4l_settings.height * 2))
2811 fmt->fmt.pix.field =
2812 V4L2_FIELD_INTERLACED;
2813 else
2814 fmt->fmt.pix.field =
2815 V4L2_FIELD_TOP;
2817 } else {
2819 fmt->fmt.pix.width =
2820 fh->jpg_settings.img_width /
2821 fh->jpg_settings.HorDcm;
2822 fmt->fmt.pix.height =
2823 fh->jpg_settings.img_height /
2824 (fh->jpg_settings.VerDcm *
2825 fh->jpg_settings.TmpDcm);
2826 fmt->fmt.pix.sizeimage =
2827 zoran_v4l2_calc_bufsize(&fh->
2828 jpg_settings);
2829 fmt->fmt.pix.pixelformat =
2830 V4L2_PIX_FMT_MJPEG;
2831 if (fh->jpg_settings.TmpDcm == 1)
2832 fmt->fmt.pix.field =
2833 (fh->jpg_settings.
2834 odd_even ? V4L2_FIELD_SEQ_BT :
2835 V4L2_FIELD_SEQ_BT);
2836 else
2837 fmt->fmt.pix.field =
2838 (fh->jpg_settings.
2839 odd_even ? V4L2_FIELD_TOP :
2840 V4L2_FIELD_BOTTOM);
2842 fmt->fmt.pix.bytesperline = 0;
2843 fmt->fmt.pix.colorspace =
2844 V4L2_COLORSPACE_SMPTE170M;
2847 mutex_unlock(&zr->resource_lock);
2849 break;
2851 default:
2852 dprintk(1,
2853 KERN_ERR
2854 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2855 ZR_DEVNAME(zr), fmt->type);
2856 return -EINVAL;
2858 return 0;
2860 break;
2862 case VIDIOC_S_FMT:
2864 struct v4l2_format *fmt = arg;
2865 int i, res = 0;
2866 __u32 printformat;
2868 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2869 ZR_DEVNAME(zr), fmt->type);
2871 switch (fmt->type) {
2872 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2874 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2875 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2876 fmt->fmt.win.w.width,
2877 fmt->fmt.win.w.height,
2878 fmt->fmt.win.clipcount,
2879 fmt->fmt.win.bitmap);
2880 mutex_lock(&zr->resource_lock);
2881 res =
2882 setup_window(file, fmt->fmt.win.w.left,
2883 fmt->fmt.win.w.top,
2884 fmt->fmt.win.w.width,
2885 fmt->fmt.win.w.height,
2886 (struct video_clip __user *)
2887 fmt->fmt.win.clips,
2888 fmt->fmt.win.clipcount,
2889 fmt->fmt.win.bitmap);
2890 mutex_unlock(&zr->resource_lock);
2891 return res;
2892 break;
2894 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2895 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2897 printformat =
2898 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2899 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2900 fmt->fmt.pix.width, fmt->fmt.pix.height,
2901 fmt->fmt.pix.pixelformat,
2902 (char *) &printformat);
2904 if (fmt->fmt.pix.bytesperline > 0) {
2905 dprintk(5,
2906 KERN_ERR "%s: bpl not supported\n",
2907 ZR_DEVNAME(zr));
2908 return -EINVAL;
2911 /* we can be requested to do JPEG/raw playback/capture */
2912 if (!
2913 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2914 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2915 fmt->fmt.pix.pixelformat ==
2916 V4L2_PIX_FMT_MJPEG))) {
2917 dprintk(1,
2918 KERN_ERR
2919 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2920 ZR_DEVNAME(zr), fmt->type,
2921 fmt->fmt.pix.pixelformat,
2922 (char *) &printformat);
2923 return -EINVAL;
2926 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2927 mutex_lock(&zr->resource_lock);
2929 settings = fh->jpg_settings;
2931 if (fh->v4l_buffers.allocated ||
2932 fh->jpg_buffers.allocated) {
2933 dprintk(1,
2934 KERN_ERR
2935 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2936 ZR_DEVNAME(zr));
2937 res = -EBUSY;
2938 goto sfmtjpg_unlock_and_return;
2941 /* we actually need to set 'real' parameters now */
2942 if ((fmt->fmt.pix.height * 2) >
2943 BUZ_MAX_HEIGHT)
2944 settings.TmpDcm = 1;
2945 else
2946 settings.TmpDcm = 2;
2947 settings.decimation = 0;
2948 if (fmt->fmt.pix.height <=
2949 fh->jpg_settings.img_height / 2)
2950 settings.VerDcm = 2;
2951 else
2952 settings.VerDcm = 1;
2953 if (fmt->fmt.pix.width <=
2954 fh->jpg_settings.img_width / 4)
2955 settings.HorDcm = 4;
2956 else if (fmt->fmt.pix.width <=
2957 fh->jpg_settings.img_width / 2)
2958 settings.HorDcm = 2;
2959 else
2960 settings.HorDcm = 1;
2961 if (settings.TmpDcm == 1)
2962 settings.field_per_buff = 2;
2963 else
2964 settings.field_per_buff = 1;
2966 /* check */
2967 if ((res =
2968 zoran_check_jpg_settings(zr,
2969 &settings)))
2970 goto sfmtjpg_unlock_and_return;
2972 /* it's ok, so set them */
2973 fh->jpg_settings = settings;
2975 /* tell the user what we actually did */
2976 fmt->fmt.pix.width =
2977 settings.img_width / settings.HorDcm;
2978 fmt->fmt.pix.height =
2979 settings.img_height * 2 /
2980 (settings.TmpDcm * settings.VerDcm);
2981 if (settings.TmpDcm == 1)
2982 fmt->fmt.pix.field =
2983 (fh->jpg_settings.
2984 odd_even ? V4L2_FIELD_SEQ_TB :
2985 V4L2_FIELD_SEQ_BT);
2986 else
2987 fmt->fmt.pix.field =
2988 (fh->jpg_settings.
2989 odd_even ? V4L2_FIELD_TOP :
2990 V4L2_FIELD_BOTTOM);
2991 fh->jpg_buffers.buffer_size =
2992 zoran_v4l2_calc_bufsize(&fh->
2993 jpg_settings);
2994 fmt->fmt.pix.sizeimage =
2995 fh->jpg_buffers.buffer_size;
2997 /* we hereby abuse this variable to show that
2998 * we're gonna do mjpeg capture */
2999 fh->map_mode =
3000 (fmt->type ==
3001 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
3002 ZORAN_MAP_MODE_JPG_REC :
3003 ZORAN_MAP_MODE_JPG_PLAY;
3004 sfmtjpg_unlock_and_return:
3005 mutex_unlock(&zr->resource_lock);
3006 } else {
3007 for (i = 0; i < zoran_num_formats; i++)
3008 if (fmt->fmt.pix.pixelformat ==
3009 zoran_formats[i].fourcc)
3010 break;
3011 if (i == zoran_num_formats) {
3012 dprintk(1,
3013 KERN_ERR
3014 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3015 ZR_DEVNAME(zr),
3016 fmt->fmt.pix.pixelformat,
3017 (char *) &printformat);
3018 return -EINVAL;
3020 mutex_lock(&zr->resource_lock);
3021 if (fh->jpg_buffers.allocated ||
3022 (fh->v4l_buffers.allocated &&
3023 fh->v4l_buffers.active !=
3024 ZORAN_FREE)) {
3025 dprintk(1,
3026 KERN_ERR
3027 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3028 ZR_DEVNAME(zr));
3029 res = -EBUSY;
3030 goto sfmtv4l_unlock_and_return;
3032 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3033 fmt->fmt.pix.height =
3034 BUZ_MAX_HEIGHT;
3035 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3036 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3038 if ((res =
3039 zoran_v4l_set_format(file,
3040 fmt->fmt.pix.
3041 width,
3042 fmt->fmt.pix.
3043 height,
3044 &zoran_formats
3045 [i])))
3046 goto sfmtv4l_unlock_and_return;
3048 /* tell the user the
3049 * results/missing stuff */
3050 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3052 if (BUZ_MAX_HEIGHT <
3053 (fh->v4l_settings.height * 2))
3054 fmt->fmt.pix.field =
3055 V4L2_FIELD_INTERLACED;
3056 else
3057 fmt->fmt.pix.field =
3058 V4L2_FIELD_TOP;
3060 fh->map_mode = ZORAN_MAP_MODE_RAW;
3061 sfmtv4l_unlock_and_return:
3062 mutex_unlock(&zr->resource_lock);
3065 break;
3067 default:
3068 dprintk(3, "unsupported\n");
3069 dprintk(1,
3070 KERN_ERR
3071 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3072 ZR_DEVNAME(zr), fmt->type);
3073 return -EINVAL;
3076 return res;
3078 break;
3080 case VIDIOC_G_FBUF:
3082 struct v4l2_framebuffer *fb = arg;
3084 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3086 memset(fb, 0, sizeof(*fb));
3087 mutex_lock(&zr->resource_lock);
3088 fb->base = zr->buffer.base;
3089 fb->fmt.width = zr->buffer.width;
3090 fb->fmt.height = zr->buffer.height;
3091 if (zr->overlay_settings.format) {
3092 fb->fmt.pixelformat =
3093 fh->overlay_settings.format->fourcc;
3095 fb->fmt.bytesperline = zr->buffer.bytesperline;
3096 mutex_unlock(&zr->resource_lock);
3097 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3098 fb->fmt.field = V4L2_FIELD_INTERLACED;
3099 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3100 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3102 return 0;
3104 break;
3106 case VIDIOC_S_FBUF:
3108 int i, res = 0;
3109 struct v4l2_framebuffer *fb = arg;
3110 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3112 dprintk(3,
3113 KERN_DEBUG
3114 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3115 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3116 fb->fmt.bytesperline, fb->fmt.pixelformat,
3117 (char *) &printformat);
3119 for (i = 0; i < zoran_num_formats; i++)
3120 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3121 break;
3122 if (i == zoran_num_formats) {
3123 dprintk(1,
3124 KERN_ERR
3125 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3126 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3127 (char *) &printformat);
3128 return -EINVAL;
3131 mutex_lock(&zr->resource_lock);
3132 res =
3133 setup_fbuffer(file, fb->base, &zoran_formats[i],
3134 fb->fmt.width, fb->fmt.height,
3135 fb->fmt.bytesperline);
3136 mutex_unlock(&zr->resource_lock);
3138 return res;
3140 break;
3142 case VIDIOC_OVERLAY:
3144 int *on = arg, res;
3146 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3147 ZR_DEVNAME(zr), *on);
3149 mutex_lock(&zr->resource_lock);
3150 res = setup_overlay(file, *on);
3151 mutex_unlock(&zr->resource_lock);
3153 return res;
3155 break;
3157 case VIDIOC_REQBUFS:
3159 struct v4l2_requestbuffers *req = arg;
3160 int res = 0;
3162 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3163 ZR_DEVNAME(zr), req->type);
3165 if (req->memory != V4L2_MEMORY_MMAP) {
3166 dprintk(1,
3167 KERN_ERR
3168 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3169 ZR_DEVNAME(zr), req->memory);
3170 return -EINVAL;
3173 mutex_lock(&zr->resource_lock);
3175 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3176 dprintk(1,
3177 KERN_ERR
3178 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3179 ZR_DEVNAME(zr));
3180 res = -EBUSY;
3181 goto v4l2reqbuf_unlock_and_return;
3184 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3185 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3187 /* control user input */
3188 if (req->count < 2)
3189 req->count = 2;
3190 if (req->count > v4l_nbufs)
3191 req->count = v4l_nbufs;
3192 fh->v4l_buffers.num_buffers = req->count;
3194 if (v4l_fbuffer_alloc(file)) {
3195 res = -ENOMEM;
3196 goto v4l2reqbuf_unlock_and_return;
3199 /* The next mmap will map the V4L buffers */
3200 fh->map_mode = ZORAN_MAP_MODE_RAW;
3202 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3203 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3205 /* we need to calculate size ourselves now */
3206 if (req->count < 4)
3207 req->count = 4;
3208 if (req->count > jpg_nbufs)
3209 req->count = jpg_nbufs;
3210 fh->jpg_buffers.num_buffers = req->count;
3211 fh->jpg_buffers.buffer_size =
3212 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3214 if (jpg_fbuffer_alloc(file)) {
3215 res = -ENOMEM;
3216 goto v4l2reqbuf_unlock_and_return;
3219 /* The next mmap will map the MJPEG buffers */
3220 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3221 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3222 else
3223 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3225 } else {
3226 dprintk(1,
3227 KERN_ERR
3228 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3229 ZR_DEVNAME(zr), req->type);
3230 res = -EINVAL;
3231 goto v4l2reqbuf_unlock_and_return;
3233 v4l2reqbuf_unlock_and_return:
3234 mutex_unlock(&zr->resource_lock);
3236 return 0;
3238 break;
3240 case VIDIOC_QUERYBUF:
3242 struct v4l2_buffer *buf = arg;
3243 __u32 type = buf->type;
3244 int index = buf->index, res;
3246 dprintk(3,
3247 KERN_DEBUG
3248 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3249 ZR_DEVNAME(zr), buf->index, buf->type);
3251 memset(buf, 0, sizeof(buf));
3252 buf->type = type;
3253 buf->index = index;
3255 mutex_lock(&zr->resource_lock);
3256 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3257 mutex_unlock(&zr->resource_lock);
3259 return res;
3261 break;
3263 case VIDIOC_QBUF:
3265 struct v4l2_buffer *buf = arg;
3266 int res = 0, codec_mode, buf_type;
3268 dprintk(3,
3269 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3270 ZR_DEVNAME(zr), buf->type, buf->index);
3272 mutex_lock(&zr->resource_lock);
3274 switch (fh->map_mode) {
3275 case ZORAN_MAP_MODE_RAW:
3276 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3277 dprintk(1,
3278 KERN_ERR
3279 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3280 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3281 res = -EINVAL;
3282 goto qbuf_unlock_and_return;
3285 res = zoran_v4l_queue_frame(file, buf->index);
3286 if (res)
3287 goto qbuf_unlock_and_return;
3288 if (!zr->v4l_memgrab_active &&
3289 fh->v4l_buffers.active == ZORAN_LOCKED)
3290 zr36057_set_memgrab(zr, 1);
3291 break;
3293 case ZORAN_MAP_MODE_JPG_REC:
3294 case ZORAN_MAP_MODE_JPG_PLAY:
3295 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3296 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3297 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3298 } else {
3299 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3300 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3303 if (buf->type != buf_type) {
3304 dprintk(1,
3305 KERN_ERR
3306 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3307 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3308 res = -EINVAL;
3309 goto qbuf_unlock_and_return;
3312 res =
3313 zoran_jpg_queue_frame(file, buf->index,
3314 codec_mode);
3315 if (res != 0)
3316 goto qbuf_unlock_and_return;
3317 if (zr->codec_mode == BUZ_MODE_IDLE &&
3318 fh->jpg_buffers.active == ZORAN_LOCKED) {
3319 zr36057_enable_jpg(zr, codec_mode);
3321 break;
3323 default:
3324 dprintk(1,
3325 KERN_ERR
3326 "%s: VIDIOC_QBUF - unsupported type %d\n",
3327 ZR_DEVNAME(zr), buf->type);
3328 res = -EINVAL;
3329 goto qbuf_unlock_and_return;
3331 qbuf_unlock_and_return:
3332 mutex_unlock(&zr->resource_lock);
3334 return res;
3336 break;
3338 case VIDIOC_DQBUF:
3340 struct v4l2_buffer *buf = arg;
3341 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3343 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3344 ZR_DEVNAME(zr), buf->type);
3346 mutex_lock(&zr->resource_lock);
3348 switch (fh->map_mode) {
3349 case ZORAN_MAP_MODE_RAW:
3350 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3351 dprintk(1,
3352 KERN_ERR
3353 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3354 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3355 res = -EINVAL;
3356 goto dqbuf_unlock_and_return;
3359 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3360 if (file->f_flags & O_NONBLOCK &&
3361 zr->v4l_buffers.buffer[num].state !=
3362 BUZ_STATE_DONE) {
3363 res = -EAGAIN;
3364 goto dqbuf_unlock_and_return;
3366 res = v4l_sync(file, num);
3367 if (res)
3368 goto dqbuf_unlock_and_return;
3369 else
3370 zr->v4l_sync_tail++;
3371 res = zoran_v4l2_buffer_status(file, buf, num);
3372 break;
3374 case ZORAN_MAP_MODE_JPG_REC:
3375 case ZORAN_MAP_MODE_JPG_PLAY:
3377 struct zoran_sync bs;
3379 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3380 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3381 else
3382 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3384 if (buf->type != buf_type) {
3385 dprintk(1,
3386 KERN_ERR
3387 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3388 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3389 res = -EINVAL;
3390 goto dqbuf_unlock_and_return;
3393 num =
3394 zr->jpg_pend[zr->
3395 jpg_que_tail & BUZ_MASK_FRAME];
3397 if (file->f_flags & O_NONBLOCK &&
3398 zr->jpg_buffers.buffer[num].state !=
3399 BUZ_STATE_DONE) {
3400 res = -EAGAIN;
3401 goto dqbuf_unlock_and_return;
3403 res = jpg_sync(file, &bs);
3404 if (res)
3405 goto dqbuf_unlock_and_return;
3406 res =
3407 zoran_v4l2_buffer_status(file, buf, bs.frame);
3408 break;
3411 default:
3412 dprintk(1,
3413 KERN_ERR
3414 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3415 ZR_DEVNAME(zr), buf->type);
3416 res = -EINVAL;
3417 goto dqbuf_unlock_and_return;
3419 dqbuf_unlock_and_return:
3420 mutex_unlock(&zr->resource_lock);
3422 return res;
3424 break;
3426 case VIDIOC_STREAMON:
3428 int res = 0;
3430 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3432 mutex_lock(&zr->resource_lock);
3434 switch (fh->map_mode) {
3435 case ZORAN_MAP_MODE_RAW: /* raw capture */
3436 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3437 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3438 res = -EBUSY;
3439 goto strmon_unlock_and_return;
3442 zr->v4l_buffers.active = fh->v4l_buffers.active =
3443 ZORAN_LOCKED;
3444 zr->v4l_settings = fh->v4l_settings;
3446 zr->v4l_sync_tail = zr->v4l_pend_tail;
3447 if (!zr->v4l_memgrab_active &&
3448 zr->v4l_pend_head != zr->v4l_pend_tail) {
3449 zr36057_set_memgrab(zr, 1);
3451 break;
3453 case ZORAN_MAP_MODE_JPG_REC:
3454 case ZORAN_MAP_MODE_JPG_PLAY:
3455 /* what is the codec mode right now? */
3456 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3457 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3458 res = -EBUSY;
3459 goto strmon_unlock_and_return;
3462 zr->jpg_buffers.active = fh->jpg_buffers.active =
3463 ZORAN_LOCKED;
3465 if (zr->jpg_que_head != zr->jpg_que_tail) {
3466 /* Start the jpeg codec when the first frame is queued */
3467 jpeg_start(zr);
3470 break;
3471 default:
3472 dprintk(1,
3473 KERN_ERR
3474 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3475 ZR_DEVNAME(zr), fh->map_mode);
3476 res = -EINVAL;
3477 goto strmon_unlock_and_return;
3479 strmon_unlock_and_return:
3480 mutex_unlock(&zr->resource_lock);
3482 return res;
3484 break;
3486 case VIDIOC_STREAMOFF:
3488 int i, res = 0;
3490 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3492 mutex_lock(&zr->resource_lock);
3494 switch (fh->map_mode) {
3495 case ZORAN_MAP_MODE_RAW: /* raw capture */
3496 if (fh->v4l_buffers.active == ZORAN_FREE &&
3497 zr->v4l_buffers.active != ZORAN_FREE) {
3498 res = -EPERM; /* stay off other's settings! */
3499 goto strmoff_unlock_and_return;
3501 if (zr->v4l_buffers.active == ZORAN_FREE)
3502 goto strmoff_unlock_and_return;
3504 /* unload capture */
3505 if (zr->v4l_memgrab_active)
3506 zr36057_set_memgrab(zr, 0);
3508 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3509 zr->v4l_buffers.buffer[i].state =
3510 BUZ_STATE_USER;
3511 fh->v4l_buffers = zr->v4l_buffers;
3513 zr->v4l_buffers.active = fh->v4l_buffers.active =
3514 ZORAN_FREE;
3516 zr->v4l_grab_seq = 0;
3517 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3518 zr->v4l_sync_tail = 0;
3520 break;
3522 case ZORAN_MAP_MODE_JPG_REC:
3523 case ZORAN_MAP_MODE_JPG_PLAY:
3524 if (fh->jpg_buffers.active == ZORAN_FREE &&
3525 zr->jpg_buffers.active != ZORAN_FREE) {
3526 res = -EPERM; /* stay off other's settings! */
3527 goto strmoff_unlock_and_return;
3529 if (zr->jpg_buffers.active == ZORAN_FREE)
3530 goto strmoff_unlock_and_return;
3532 res =
3533 jpg_qbuf(file, -1,
3534 (fh->map_mode ==
3535 ZORAN_MAP_MODE_JPG_REC) ?
3536 BUZ_MODE_MOTION_COMPRESS :
3537 BUZ_MODE_MOTION_DECOMPRESS);
3538 if (res)
3539 goto strmoff_unlock_and_return;
3540 break;
3541 default:
3542 dprintk(1,
3543 KERN_ERR
3544 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3545 ZR_DEVNAME(zr), fh->map_mode);
3546 res = -EINVAL;
3547 goto strmoff_unlock_and_return;
3549 strmoff_unlock_and_return:
3550 mutex_unlock(&zr->resource_lock);
3552 return res;
3554 break;
3556 case VIDIOC_QUERYCTRL:
3558 struct v4l2_queryctrl *ctrl = arg;
3560 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3561 ZR_DEVNAME(zr), ctrl->id);
3563 /* we only support hue/saturation/contrast/brightness */
3564 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3565 ctrl->id > V4L2_CID_HUE)
3566 return -EINVAL;
3567 else {
3568 int id = ctrl->id;
3569 memset(ctrl, 0, sizeof(*ctrl));
3570 ctrl->id = id;
3573 switch (ctrl->id) {
3574 case V4L2_CID_BRIGHTNESS:
3575 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3576 break;
3577 case V4L2_CID_CONTRAST:
3578 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3579 break;
3580 case V4L2_CID_SATURATION:
3581 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3582 break;
3583 case V4L2_CID_HUE:
3584 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3585 break;
3588 ctrl->minimum = 0;
3589 ctrl->maximum = 65535;
3590 ctrl->step = 1;
3591 ctrl->default_value = 32768;
3592 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3594 return 0;
3596 break;
3598 case VIDIOC_G_CTRL:
3600 struct v4l2_control *ctrl = arg;
3602 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3603 ZR_DEVNAME(zr), ctrl->id);
3605 /* we only support hue/saturation/contrast/brightness */
3606 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3607 ctrl->id > V4L2_CID_HUE)
3608 return -EINVAL;
3610 mutex_lock(&zr->resource_lock);
3611 switch (ctrl->id) {
3612 case V4L2_CID_BRIGHTNESS:
3613 ctrl->value = zr->brightness;
3614 break;
3615 case V4L2_CID_CONTRAST:
3616 ctrl->value = zr->contrast;
3617 break;
3618 case V4L2_CID_SATURATION:
3619 ctrl->value = zr->saturation;
3620 break;
3621 case V4L2_CID_HUE:
3622 ctrl->value = zr->hue;
3623 break;
3625 mutex_unlock(&zr->resource_lock);
3627 return 0;
3629 break;
3631 case VIDIOC_S_CTRL:
3633 struct v4l2_control *ctrl = arg;
3634 struct video_picture pict;
3636 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3637 ZR_DEVNAME(zr), ctrl->id);
3639 /* we only support hue/saturation/contrast/brightness */
3640 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3641 ctrl->id > V4L2_CID_HUE)
3642 return -EINVAL;
3644 if (ctrl->value < 0 || ctrl->value > 65535) {
3645 dprintk(1,
3646 KERN_ERR
3647 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3648 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3649 return -EINVAL;
3652 mutex_lock(&zr->resource_lock);
3653 switch (ctrl->id) {
3654 case V4L2_CID_BRIGHTNESS:
3655 zr->brightness = ctrl->value;
3656 break;
3657 case V4L2_CID_CONTRAST:
3658 zr->contrast = ctrl->value;
3659 break;
3660 case V4L2_CID_SATURATION:
3661 zr->saturation = ctrl->value;
3662 break;
3663 case V4L2_CID_HUE:
3664 zr->hue = ctrl->value;
3665 break;
3667 pict.brightness = zr->brightness;
3668 pict.contrast = zr->contrast;
3669 pict.colour = zr->saturation;
3670 pict.hue = zr->hue;
3672 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3674 mutex_unlock(&zr->resource_lock);
3676 return 0;
3678 break;
3680 case VIDIOC_ENUMSTD:
3682 struct v4l2_standard *std = arg;
3684 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3685 ZR_DEVNAME(zr), std->index);
3687 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3688 return -EINVAL;
3689 else {
3690 int id = std->index;
3691 memset(std, 0, sizeof(*std));
3692 std->index = id;
3695 if (std->index == zr->card.norms) {
3696 /* if we have autodetect, ... */
3697 struct video_decoder_capability caps;
3698 decoder_command(zr, DECODER_GET_CAPABILITIES,
3699 &caps);
3700 if (caps.flags & VIDEO_DECODER_AUTO) {
3701 std->id = V4L2_STD_ALL;
3702 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3703 return 0;
3704 } else
3705 return -EINVAL;
3707 switch (std->index) {
3708 case 0:
3709 std->id = V4L2_STD_PAL;
3710 strncpy(std->name, "PAL", sizeof(std->name)-1);
3711 std->frameperiod.numerator = 1;
3712 std->frameperiod.denominator = 25;
3713 std->framelines = zr->card.tvn[0]->Ht;
3714 break;
3715 case 1:
3716 std->id = V4L2_STD_NTSC;
3717 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3718 std->frameperiod.numerator = 1001;
3719 std->frameperiod.denominator = 30000;
3720 std->framelines = zr->card.tvn[1]->Ht;
3721 break;
3722 case 2:
3723 std->id = V4L2_STD_SECAM;
3724 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3725 std->frameperiod.numerator = 1;
3726 std->frameperiod.denominator = 25;
3727 std->framelines = zr->card.tvn[2]->Ht;
3728 break;
3731 return 0;
3733 break;
3735 case VIDIOC_G_STD:
3737 v4l2_std_id *std = arg;
3738 int norm;
3740 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3742 mutex_lock(&zr->resource_lock);
3743 norm = zr->norm;
3744 mutex_unlock(&zr->resource_lock);
3746 switch (norm) {
3747 case VIDEO_MODE_PAL:
3748 *std = V4L2_STD_PAL;
3749 break;
3750 case VIDEO_MODE_NTSC:
3751 *std = V4L2_STD_NTSC;
3752 break;
3753 case VIDEO_MODE_SECAM:
3754 *std = V4L2_STD_SECAM;
3755 break;
3758 return 0;
3760 break;
3762 case VIDIOC_S_STD:
3764 int norm = -1, res = 0;
3765 v4l2_std_id *std = arg;
3767 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3768 ZR_DEVNAME(zr), (unsigned long long)*std);
3770 if (*std == V4L2_STD_PAL)
3771 norm = VIDEO_MODE_PAL;
3772 else if (*std == V4L2_STD_NTSC)
3773 norm = VIDEO_MODE_NTSC;
3774 else if (*std == V4L2_STD_SECAM)
3775 norm = VIDEO_MODE_SECAM;
3776 else if (*std == V4L2_STD_ALL)
3777 norm = VIDEO_MODE_AUTO;
3778 else {
3779 dprintk(1,
3780 KERN_ERR
3781 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3782 ZR_DEVNAME(zr), (unsigned long long)*std);
3783 return -EINVAL;
3786 mutex_lock(&zr->resource_lock);
3787 if ((res = zoran_set_norm(zr, norm)))
3788 goto sstd_unlock_and_return;
3790 res = wait_grab_pending(zr);
3791 sstd_unlock_and_return:
3792 mutex_unlock(&zr->resource_lock);
3793 return res;
3795 break;
3797 case VIDIOC_ENUMINPUT:
3799 struct v4l2_input *inp = arg;
3800 int status;
3802 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3803 ZR_DEVNAME(zr), inp->index);
3805 if (inp->index < 0 || inp->index >= zr->card.inputs)
3806 return -EINVAL;
3807 else {
3808 int id = inp->index;
3809 memset(inp, 0, sizeof(*inp));
3810 inp->index = id;
3813 strncpy(inp->name, zr->card.input[inp->index].name,
3814 sizeof(inp->name) - 1);
3815 inp->type = V4L2_INPUT_TYPE_CAMERA;
3816 inp->std = V4L2_STD_ALL;
3818 /* Get status of video decoder */
3819 mutex_lock(&zr->resource_lock);
3820 decoder_command(zr, DECODER_GET_STATUS, &status);
3821 mutex_unlock(&zr->resource_lock);
3823 if (!(status & DECODER_STATUS_GOOD)) {
3824 inp->status |= V4L2_IN_ST_NO_POWER;
3825 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3827 if (!(status & DECODER_STATUS_COLOR))
3828 inp->status |= V4L2_IN_ST_NO_COLOR;
3830 return 0;
3832 break;
3834 case VIDIOC_G_INPUT:
3836 int *input = arg;
3838 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3840 mutex_lock(&zr->resource_lock);
3841 *input = zr->input;
3842 mutex_unlock(&zr->resource_lock);
3844 return 0;
3846 break;
3848 case VIDIOC_S_INPUT:
3850 int *input = arg, res = 0;
3852 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3853 ZR_DEVNAME(zr), *input);
3855 mutex_lock(&zr->resource_lock);
3856 if ((res = zoran_set_input(zr, *input)))
3857 goto sinput_unlock_and_return;
3859 /* Make sure the changes come into effect */
3860 res = wait_grab_pending(zr);
3861 sinput_unlock_and_return:
3862 mutex_unlock(&zr->resource_lock);
3863 return res;
3865 break;
3867 case VIDIOC_ENUMOUTPUT:
3869 struct v4l2_output *outp = arg;
3871 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3872 ZR_DEVNAME(zr), outp->index);
3874 if (outp->index != 0)
3875 return -EINVAL;
3877 memset(outp, 0, sizeof(*outp));
3878 outp->index = 0;
3879 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3880 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3882 return 0;
3884 break;
3886 case VIDIOC_G_OUTPUT:
3888 int *output = arg;
3890 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3892 *output = 0;
3894 return 0;
3896 break;
3898 case VIDIOC_S_OUTPUT:
3900 int *output = arg;
3902 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3903 ZR_DEVNAME(zr), *output);
3905 if (*output != 0)
3906 return -EINVAL;
3908 return 0;
3910 break;
3912 /* cropping (sub-frame capture) */
3913 case VIDIOC_CROPCAP:
3915 struct v4l2_cropcap *cropcap = arg;
3916 int type = cropcap->type, res = 0;
3918 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3919 ZR_DEVNAME(zr), cropcap->type);
3921 memset(cropcap, 0, sizeof(*cropcap));
3922 cropcap->type = type;
3924 mutex_lock(&zr->resource_lock);
3926 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3927 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3928 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3929 dprintk(1,
3930 KERN_ERR
3931 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3932 ZR_DEVNAME(zr));
3933 res = -EINVAL;
3934 goto cropcap_unlock_and_return;
3937 cropcap->bounds.top = cropcap->bounds.left = 0;
3938 cropcap->bounds.width = BUZ_MAX_WIDTH;
3939 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3940 cropcap->defrect.top = cropcap->defrect.left = 0;
3941 cropcap->defrect.width = BUZ_MIN_WIDTH;
3942 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3943 cropcap_unlock_and_return:
3944 mutex_unlock(&zr->resource_lock);
3945 return res;
3947 break;
3949 case VIDIOC_G_CROP:
3951 struct v4l2_crop *crop = arg;
3952 int type = crop->type, res = 0;
3954 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3955 ZR_DEVNAME(zr), crop->type);
3957 memset(crop, 0, sizeof(*crop));
3958 crop->type = type;
3960 mutex_lock(&zr->resource_lock);
3962 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3963 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3964 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3965 dprintk(1,
3966 KERN_ERR
3967 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3968 ZR_DEVNAME(zr));
3969 res = -EINVAL;
3970 goto gcrop_unlock_and_return;
3973 crop->c.top = fh->jpg_settings.img_y;
3974 crop->c.left = fh->jpg_settings.img_x;
3975 crop->c.width = fh->jpg_settings.img_width;
3976 crop->c.height = fh->jpg_settings.img_height;
3978 gcrop_unlock_and_return:
3979 mutex_unlock(&zr->resource_lock);
3981 return res;
3983 break;
3985 case VIDIOC_S_CROP:
3987 struct v4l2_crop *crop = arg;
3988 int res = 0;
3990 settings = fh->jpg_settings;
3992 dprintk(3,
3993 KERN_ERR
3994 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3995 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3996 crop->c.width, crop->c.height);
3998 mutex_lock(&zr->resource_lock);
4000 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
4001 dprintk(1,
4002 KERN_ERR
4003 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
4004 ZR_DEVNAME(zr));
4005 res = -EBUSY;
4006 goto scrop_unlock_and_return;
4009 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
4010 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4011 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4012 dprintk(1,
4013 KERN_ERR
4014 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4015 ZR_DEVNAME(zr));
4016 res = -EINVAL;
4017 goto scrop_unlock_and_return;
4020 /* move into a form that we understand */
4021 settings.img_x = crop->c.left;
4022 settings.img_y = crop->c.top;
4023 settings.img_width = crop->c.width;
4024 settings.img_height = crop->c.height;
4026 /* check validity */
4027 if ((res = zoran_check_jpg_settings(zr, &settings)))
4028 goto scrop_unlock_and_return;
4030 /* accept */
4031 fh->jpg_settings = settings;
4033 scrop_unlock_and_return:
4034 mutex_unlock(&zr->resource_lock);
4035 return res;
4037 break;
4039 case VIDIOC_G_JPEGCOMP:
4041 struct v4l2_jpegcompression *params = arg;
4043 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4044 ZR_DEVNAME(zr));
4046 memset(params, 0, sizeof(*params));
4048 mutex_lock(&zr->resource_lock);
4050 params->quality = fh->jpg_settings.jpg_comp.quality;
4051 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4052 memcpy(params->APP_data,
4053 fh->jpg_settings.jpg_comp.APP_data,
4054 fh->jpg_settings.jpg_comp.APP_len);
4055 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4056 memcpy(params->COM_data,
4057 fh->jpg_settings.jpg_comp.COM_data,
4058 fh->jpg_settings.jpg_comp.COM_len);
4059 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4060 params->jpeg_markers =
4061 fh->jpg_settings.jpg_comp.jpeg_markers;
4063 mutex_unlock(&zr->resource_lock);
4065 return 0;
4067 break;
4069 case VIDIOC_S_JPEGCOMP:
4071 struct v4l2_jpegcompression *params = arg;
4072 int res = 0;
4074 settings = fh->jpg_settings;
4076 dprintk(3,
4077 KERN_DEBUG
4078 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4079 ZR_DEVNAME(zr), params->quality, params->APPn,
4080 params->APP_len, params->COM_len);
4082 settings.jpg_comp = *params;
4084 mutex_lock(&zr->resource_lock);
4086 if (fh->v4l_buffers.active != ZORAN_FREE ||
4087 fh->jpg_buffers.active != ZORAN_FREE) {
4088 dprintk(1,
4089 KERN_WARNING
4090 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4091 ZR_DEVNAME(zr));
4092 res = -EBUSY;
4093 goto sjpegc_unlock_and_return;
4096 if ((res = zoran_check_jpg_settings(zr, &settings)))
4097 goto sjpegc_unlock_and_return;
4098 if (!fh->jpg_buffers.allocated)
4099 fh->jpg_buffers.buffer_size =
4100 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4101 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4102 sjpegc_unlock_and_return:
4103 mutex_unlock(&zr->resource_lock);
4105 return 0;
4107 break;
4109 case VIDIOC_QUERYSTD: /* why is this useful? */
4111 v4l2_std_id *std = arg;
4113 dprintk(3,
4114 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4115 ZR_DEVNAME(zr), (unsigned long long)*std);
4117 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4118 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4119 zr->card.norms == 3)) {
4120 return 0;
4123 return -EINVAL;
4125 break;
4127 case VIDIOC_TRY_FMT:
4129 struct v4l2_format *fmt = arg;
4130 int res = 0;
4132 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4133 ZR_DEVNAME(zr), fmt->type);
4135 switch (fmt->type) {
4136 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4137 mutex_lock(&zr->resource_lock);
4139 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4140 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4141 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4142 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4143 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4144 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4145 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4146 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4148 mutex_unlock(&zr->resource_lock);
4149 break;
4151 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4152 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4153 if (fmt->fmt.pix.bytesperline > 0)
4154 return -EINVAL;
4156 mutex_lock(&zr->resource_lock);
4158 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4159 settings = fh->jpg_settings;
4161 /* we actually need to set 'real' parameters now */
4162 if ((fmt->fmt.pix.height * 2) >
4163 BUZ_MAX_HEIGHT)
4164 settings.TmpDcm = 1;
4165 else
4166 settings.TmpDcm = 2;
4167 settings.decimation = 0;
4168 if (fmt->fmt.pix.height <=
4169 fh->jpg_settings.img_height / 2)
4170 settings.VerDcm = 2;
4171 else
4172 settings.VerDcm = 1;
4173 if (fmt->fmt.pix.width <=
4174 fh->jpg_settings.img_width / 4)
4175 settings.HorDcm = 4;
4176 else if (fmt->fmt.pix.width <=
4177 fh->jpg_settings.img_width / 2)
4178 settings.HorDcm = 2;
4179 else
4180 settings.HorDcm = 1;
4181 if (settings.TmpDcm == 1)
4182 settings.field_per_buff = 2;
4183 else
4184 settings.field_per_buff = 1;
4186 /* check */
4187 if ((res =
4188 zoran_check_jpg_settings(zr,
4189 &settings)))
4190 goto tryfmt_unlock_and_return;
4192 /* tell the user what we actually did */
4193 fmt->fmt.pix.width =
4194 settings.img_width / settings.HorDcm;
4195 fmt->fmt.pix.height =
4196 settings.img_height * 2 /
4197 (settings.TmpDcm * settings.VerDcm);
4198 if (settings.TmpDcm == 1)
4199 fmt->fmt.pix.field =
4200 (fh->jpg_settings.
4201 odd_even ? V4L2_FIELD_SEQ_TB :
4202 V4L2_FIELD_SEQ_BT);
4203 else
4204 fmt->fmt.pix.field =
4205 (fh->jpg_settings.
4206 odd_even ? V4L2_FIELD_TOP :
4207 V4L2_FIELD_BOTTOM);
4209 fmt->fmt.pix.sizeimage =
4210 zoran_v4l2_calc_bufsize(&settings);
4211 } else if (fmt->type ==
4212 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4213 int i;
4215 for (i = 0; i < zoran_num_formats; i++)
4216 if (zoran_formats[i].fourcc ==
4217 fmt->fmt.pix.pixelformat)
4218 break;
4219 if (i == zoran_num_formats) {
4220 res = -EINVAL;
4221 goto tryfmt_unlock_and_return;
4224 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4225 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4226 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4227 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4228 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4229 fmt->fmt.pix.height =
4230 BUZ_MAX_HEIGHT;
4231 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4232 fmt->fmt.pix.height =
4233 BUZ_MIN_HEIGHT;
4234 } else {
4235 res = -EINVAL;
4236 goto tryfmt_unlock_and_return;
4238 tryfmt_unlock_and_return:
4239 mutex_unlock(&zr->resource_lock);
4241 return res;
4242 break;
4244 default:
4245 return -EINVAL;
4248 return 0;
4250 break;
4251 #endif
4253 default:
4254 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4255 ZR_DEVNAME(zr), cmd);
4256 return -ENOIOCTLCMD;
4257 break;
4260 return 0;
4264 static int
4265 zoran_ioctl (struct inode *inode,
4266 struct file *file,
4267 unsigned int cmd,
4268 unsigned long arg)
4270 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4273 static unsigned int
4274 zoran_poll (struct file *file,
4275 poll_table *wait)
4277 struct zoran_fh *fh = file->private_data;
4278 struct zoran *zr = fh->zr;
4279 wait_queue_head_t *queue = NULL;
4280 int res = 0, frame;
4282 /* we should check whether buffers are ready to be synced on
4283 * (w/o waits - O_NONBLOCK) here
4284 * if ready for read (sync), return POLLIN|POLLRDNORM,
4285 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4286 * if error, return POLLERR,
4287 * if no buffers queued or so, return POLLNVAL
4290 mutex_lock(&zr->resource_lock);
4292 switch (fh->map_mode) {
4293 case ZORAN_MAP_MODE_RAW:
4294 if (fh->v4l_buffers.active == ZORAN_FREE ||
4295 zr->v4l_pend_head == zr->v4l_pend_tail) {
4296 dprintk(1,
4297 "%s: zoran_poll() - no buffers queued\n",
4298 ZR_DEVNAME(zr));
4299 res = POLLNVAL;
4300 goto poll_unlock_and_return;
4302 queue = &zr->v4l_capq;
4303 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4304 poll_wait(file, queue, wait);
4305 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4306 res = POLLIN | POLLRDNORM;
4307 break;
4309 case ZORAN_MAP_MODE_JPG_REC:
4310 case ZORAN_MAP_MODE_JPG_PLAY:
4311 if (fh->jpg_buffers.active == ZORAN_FREE ||
4312 zr->jpg_que_head == zr->jpg_que_tail) {
4313 dprintk(1,
4314 "%s: zoran_poll() - no buffers queued\n",
4315 ZR_DEVNAME(zr));
4316 res = POLLNVAL;
4317 goto poll_unlock_and_return;
4319 queue = &zr->jpg_capq;
4320 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4321 poll_wait(file, queue, wait);
4322 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4323 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4324 res = POLLIN | POLLRDNORM;
4325 else
4326 res = POLLOUT | POLLWRNORM;
4328 break;
4330 default:
4331 dprintk(1,
4332 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4333 ZR_DEVNAME(zr), fh->map_mode);
4334 res = POLLNVAL;
4335 goto poll_unlock_and_return;
4338 poll_unlock_and_return:
4339 mutex_unlock(&zr->resource_lock);
4341 return res;
4346 * This maps the buffers to user space.
4348 * Depending on the state of fh->map_mode
4349 * the V4L or the MJPEG buffers are mapped
4350 * per buffer or all together
4352 * Note that we need to connect to some
4353 * unmap signal event to unmap the de-allocate
4354 * the buffer accordingly (zoran_vm_close())
4357 static void
4358 zoran_vm_open (struct vm_area_struct *vma)
4360 struct zoran_mapping *map = vma->vm_private_data;
4362 map->count++;
4365 static void
4366 zoran_vm_close (struct vm_area_struct *vma)
4368 struct zoran_mapping *map = vma->vm_private_data;
4369 struct file *file = map->file;
4370 struct zoran_fh *fh = file->private_data;
4371 struct zoran *zr = fh->zr;
4372 int i;
4374 map->count--;
4375 if (map->count == 0) {
4376 switch (fh->map_mode) {
4377 case ZORAN_MAP_MODE_JPG_REC:
4378 case ZORAN_MAP_MODE_JPG_PLAY:
4380 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4381 ZR_DEVNAME(zr));
4383 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4384 if (fh->jpg_buffers.buffer[i].map == map) {
4385 fh->jpg_buffers.buffer[i].map =
4386 NULL;
4389 kfree(map);
4391 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4392 if (fh->jpg_buffers.buffer[i].map)
4393 break;
4394 if (i == fh->jpg_buffers.num_buffers) {
4395 mutex_lock(&zr->resource_lock);
4397 if (fh->jpg_buffers.active != ZORAN_FREE) {
4398 jpg_qbuf(file, -1, zr->codec_mode);
4399 zr->jpg_buffers.allocated = 0;
4400 zr->jpg_buffers.active =
4401 fh->jpg_buffers.active =
4402 ZORAN_FREE;
4404 //jpg_fbuffer_free(file);
4405 fh->jpg_buffers.allocated = 0;
4406 fh->jpg_buffers.ready_to_be_freed = 1;
4408 mutex_unlock(&zr->resource_lock);
4411 break;
4413 case ZORAN_MAP_MODE_RAW:
4415 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4416 ZR_DEVNAME(zr));
4418 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4419 if (fh->v4l_buffers.buffer[i].map == map) {
4420 /* unqueue/unmap */
4421 fh->v4l_buffers.buffer[i].map =
4422 NULL;
4425 kfree(map);
4427 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4428 if (fh->v4l_buffers.buffer[i].map)
4429 break;
4430 if (i == fh->v4l_buffers.num_buffers) {
4431 mutex_lock(&zr->resource_lock);
4433 if (fh->v4l_buffers.active != ZORAN_FREE) {
4434 zr36057_set_memgrab(zr, 0);
4435 zr->v4l_buffers.allocated = 0;
4436 zr->v4l_buffers.active =
4437 fh->v4l_buffers.active =
4438 ZORAN_FREE;
4440 //v4l_fbuffer_free(file);
4441 fh->v4l_buffers.allocated = 0;
4442 fh->v4l_buffers.ready_to_be_freed = 1;
4444 mutex_unlock(&zr->resource_lock);
4447 break;
4449 default:
4450 printk(KERN_ERR
4451 "%s: munmap() - internal error - unknown map mode %d\n",
4452 ZR_DEVNAME(zr), fh->map_mode);
4453 break;
4459 static struct vm_operations_struct zoran_vm_ops = {
4460 .open = zoran_vm_open,
4461 .close = zoran_vm_close,
4464 static int
4465 zoran_mmap (struct file *file,
4466 struct vm_area_struct *vma)
4468 struct zoran_fh *fh = file->private_data;
4469 struct zoran *zr = fh->zr;
4470 unsigned long size = (vma->vm_end - vma->vm_start);
4471 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4472 int i, j;
4473 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4474 int first, last;
4475 struct zoran_mapping *map;
4476 int res = 0;
4478 dprintk(3,
4479 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4480 ZR_DEVNAME(zr),
4481 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4482 vma->vm_start, vma->vm_end, size);
4484 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4485 !(vma->vm_flags & VM_WRITE)) {
4486 dprintk(1,
4487 KERN_ERR
4488 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4489 ZR_DEVNAME(zr));
4490 return -EINVAL;
4493 switch (fh->map_mode) {
4495 case ZORAN_MAP_MODE_JPG_REC:
4496 case ZORAN_MAP_MODE_JPG_PLAY:
4498 /* lock */
4499 mutex_lock(&zr->resource_lock);
4501 /* Map the MJPEG buffers */
4502 if (!fh->jpg_buffers.allocated) {
4503 dprintk(1,
4504 KERN_ERR
4505 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4506 ZR_DEVNAME(zr));
4507 res = -ENOMEM;
4508 goto jpg_mmap_unlock_and_return;
4511 first = offset / fh->jpg_buffers.buffer_size;
4512 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4513 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4514 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4515 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4516 last >= fh->jpg_buffers.num_buffers) {
4517 dprintk(1,
4518 KERN_ERR
4519 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4520 ZR_DEVNAME(zr), offset, size,
4521 fh->jpg_buffers.buffer_size,
4522 fh->jpg_buffers.num_buffers);
4523 res = -EINVAL;
4524 goto jpg_mmap_unlock_and_return;
4526 for (i = first; i <= last; i++) {
4527 if (fh->jpg_buffers.buffer[i].map) {
4528 dprintk(1,
4529 KERN_ERR
4530 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4531 ZR_DEVNAME(zr), i);
4532 res = -EBUSY;
4533 goto jpg_mmap_unlock_and_return;
4537 /* map these buffers (v4l_buffers[i]) */
4538 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4539 if (!map) {
4540 res = -ENOMEM;
4541 goto jpg_mmap_unlock_and_return;
4543 map->file = file;
4544 map->count = 1;
4546 vma->vm_ops = &zoran_vm_ops;
4547 vma->vm_flags |= VM_DONTEXPAND;
4548 vma->vm_private_data = map;
4550 for (i = first; i <= last; i++) {
4551 for (j = 0;
4552 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4553 j++) {
4554 fraglen =
4555 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4556 frag_tab[2 * j + 1]) & ~1) << 1;
4557 todo = size;
4558 if (todo > fraglen)
4559 todo = fraglen;
4560 pos =
4561 le32_to_cpu((unsigned long) fh->jpg_buffers.
4562 buffer[i].frag_tab[2 * j]);
4563 /* should just be pos on i386 */
4564 page = virt_to_phys(bus_to_virt(pos))
4565 >> PAGE_SHIFT;
4566 if (remap_pfn_range(vma, start, page,
4567 todo, PAGE_SHARED)) {
4568 dprintk(1,
4569 KERN_ERR
4570 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4571 ZR_DEVNAME(zr));
4572 res = -EAGAIN;
4573 goto jpg_mmap_unlock_and_return;
4575 size -= todo;
4576 start += todo;
4577 if (size == 0)
4578 break;
4579 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4580 frag_tab[2 * j + 1]) & 1)
4581 break; /* was last fragment */
4583 fh->jpg_buffers.buffer[i].map = map;
4584 if (size == 0)
4585 break;
4588 jpg_mmap_unlock_and_return:
4589 mutex_unlock(&zr->resource_lock);
4591 break;
4593 case ZORAN_MAP_MODE_RAW:
4595 mutex_lock(&zr->resource_lock);
4597 /* Map the V4L buffers */
4598 if (!fh->v4l_buffers.allocated) {
4599 dprintk(1,
4600 KERN_ERR
4601 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4602 ZR_DEVNAME(zr));
4603 res = -ENOMEM;
4604 goto v4l_mmap_unlock_and_return;
4607 first = offset / fh->v4l_buffers.buffer_size;
4608 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4609 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4610 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4611 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4612 last >= fh->v4l_buffers.buffer_size) {
4613 dprintk(1,
4614 KERN_ERR
4615 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4616 ZR_DEVNAME(zr), offset, size,
4617 fh->v4l_buffers.buffer_size,
4618 fh->v4l_buffers.num_buffers);
4619 res = -EINVAL;
4620 goto v4l_mmap_unlock_and_return;
4622 for (i = first; i <= last; i++) {
4623 if (fh->v4l_buffers.buffer[i].map) {
4624 dprintk(1,
4625 KERN_ERR
4626 "%s: mmap(V4L) - buffer %d already mapped\n",
4627 ZR_DEVNAME(zr), i);
4628 res = -EBUSY;
4629 goto v4l_mmap_unlock_and_return;
4633 /* map these buffers (v4l_buffers[i]) */
4634 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4635 if (!map) {
4636 res = -ENOMEM;
4637 goto v4l_mmap_unlock_and_return;
4639 map->file = file;
4640 map->count = 1;
4642 vma->vm_ops = &zoran_vm_ops;
4643 vma->vm_flags |= VM_DONTEXPAND;
4644 vma->vm_private_data = map;
4646 for (i = first; i <= last; i++) {
4647 todo = size;
4648 if (todo > fh->v4l_buffers.buffer_size)
4649 todo = fh->v4l_buffers.buffer_size;
4650 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4651 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4652 todo, PAGE_SHARED)) {
4653 dprintk(1,
4654 KERN_ERR
4655 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4656 ZR_DEVNAME(zr));
4657 res = -EAGAIN;
4658 goto v4l_mmap_unlock_and_return;
4660 size -= todo;
4661 start += todo;
4662 fh->v4l_buffers.buffer[i].map = map;
4663 if (size == 0)
4664 break;
4666 v4l_mmap_unlock_and_return:
4667 mutex_unlock(&zr->resource_lock);
4669 break;
4671 default:
4672 dprintk(1,
4673 KERN_ERR
4674 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4675 ZR_DEVNAME(zr), fh->map_mode);
4676 break;
4679 return 0;
4682 static const struct file_operations zoran_fops = {
4683 .owner = THIS_MODULE,
4684 .open = zoran_open,
4685 .release = zoran_close,
4686 .ioctl = zoran_ioctl,
4687 .compat_ioctl = v4l_compat_ioctl32,
4688 .llseek = no_llseek,
4689 .read = zoran_read,
4690 .write = zoran_write,
4691 .mmap = zoran_mmap,
4692 .poll = zoran_poll,
4695 struct video_device zoran_template __devinitdata = {
4696 .name = ZORAN_NAME,
4697 .type = ZORAN_VID_TYPE,
4698 #ifdef CONFIG_VIDEO_V4L2
4699 .type2 = ZORAN_V4L2_VID_FLAGS,
4700 #endif
4701 .hardware = ZORAN_HARDWARE,
4702 .fops = &zoran_fops,
4703 .release = &zoran_vdev_release,
4704 .minor = -1