Initial commit
[wrt350n-kernel.git] / drivers / media / video / zoran_driver.c
blobdd3d7d2c8b0e38323ed29e2a9b21a6a429a46111
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_VID_TYPE ( \
64 VID_TYPE_CAPTURE | \
65 VID_TYPE_OVERLAY | \
66 VID_TYPE_CLIPPING | \
67 VID_TYPE_FRAMERAM | \
68 VID_TYPE_SCALES | \
69 VID_TYPE_MJPEG_DECODER | \
70 VID_TYPE_MJPEG_ENCODER \
73 #include <linux/videodev.h>
74 #include <media/v4l2-common.h>
75 #include "videocodec.h"
77 #include <asm/io.h>
78 #include <asm/uaccess.h>
79 #include <linux/proc_fs.h>
81 #include <linux/video_decoder.h>
82 #include <linux/video_encoder.h>
83 #include <linux/mutex.h>
84 #include "zoran.h"
85 #include "zoran_device.h"
86 #include "zoran_card.h"
88 #ifdef CONFIG_VIDEO_V4L2
89 /* we declare some card type definitions here, they mean
90 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
91 #define ZORAN_V4L2_VID_FLAGS ( \
92 V4L2_CAP_STREAMING |\
93 V4L2_CAP_VIDEO_CAPTURE |\
94 V4L2_CAP_VIDEO_OUTPUT |\
95 V4L2_CAP_VIDEO_OVERLAY \
97 #endif
99 #include <asm/byteorder.h>
101 #if defined(CONFIG_VIDEO_V4L2) && defined(CONFIG_VIDEO_V4L1_COMPAT)
102 #define ZFMT(pal, fcc, cs) \
103 .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
104 #elif defined(CONFIG_VIDEO_V4L2)
105 #define ZFMT(pal, fcc, cs) \
106 .fourcc = (fcc), .colorspace = (cs)
107 #else
108 #define ZFMT(pal, fcc, cs) \
109 .palette = (pal)
110 #endif
112 const struct zoran_format zoran_formats[] = {
114 .name = "15-bit RGB LE",
115 ZFMT(VIDEO_PALETTE_RGB555,
116 V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
117 .depth = 15,
118 .flags = ZORAN_FORMAT_CAPTURE |
119 ZORAN_FORMAT_OVERLAY,
120 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
121 ZR36057_VFESPFR_LittleEndian,
122 }, {
123 .name = "15-bit RGB BE",
124 ZFMT(-1,
125 V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
126 .depth = 15,
127 .flags = ZORAN_FORMAT_CAPTURE |
128 ZORAN_FORMAT_OVERLAY,
129 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
130 }, {
131 .name = "16-bit RGB LE",
132 ZFMT(VIDEO_PALETTE_RGB565,
133 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
134 .depth = 16,
135 .flags = ZORAN_FORMAT_CAPTURE |
136 ZORAN_FORMAT_OVERLAY,
137 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
138 ZR36057_VFESPFR_LittleEndian,
139 }, {
140 .name = "16-bit RGB BE",
141 ZFMT(-1,
142 V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
143 .depth = 16,
144 .flags = ZORAN_FORMAT_CAPTURE |
145 ZORAN_FORMAT_OVERLAY,
146 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
147 }, {
148 .name = "24-bit RGB",
149 ZFMT(VIDEO_PALETTE_RGB24,
150 V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
151 .depth = 24,
152 .flags = ZORAN_FORMAT_CAPTURE |
153 ZORAN_FORMAT_OVERLAY,
154 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
155 }, {
156 .name = "32-bit RGB LE",
157 ZFMT(VIDEO_PALETTE_RGB32,
158 V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
159 .depth = 32,
160 .flags = ZORAN_FORMAT_CAPTURE |
161 ZORAN_FORMAT_OVERLAY,
162 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
163 }, {
164 .name = "32-bit RGB BE",
165 ZFMT(-1,
166 V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
167 .depth = 32,
168 .flags = ZORAN_FORMAT_CAPTURE |
169 ZORAN_FORMAT_OVERLAY,
170 .vfespfr = ZR36057_VFESPFR_RGB888,
171 }, {
172 .name = "4:2:2, packed, YUYV",
173 ZFMT(VIDEO_PALETTE_YUV422,
174 V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
175 .depth = 16,
176 .flags = ZORAN_FORMAT_CAPTURE |
177 ZORAN_FORMAT_OVERLAY,
178 .vfespfr = ZR36057_VFESPFR_YUV422,
179 }, {
180 .name = "4:2:2, packed, UYVY",
181 ZFMT(VIDEO_PALETTE_UYVY,
182 V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
183 .depth = 16,
184 .flags = ZORAN_FORMAT_CAPTURE |
185 ZORAN_FORMAT_OVERLAY,
186 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
187 }, {
188 .name = "Hardware-encoded Motion-JPEG",
189 ZFMT(-1,
190 V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
191 .depth = 0,
192 .flags = ZORAN_FORMAT_CAPTURE |
193 ZORAN_FORMAT_PLAYBACK |
194 ZORAN_FORMAT_COMPRESSED,
197 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
199 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
202 extern int v4l_nbufs;
203 extern int v4l_bufsize;
204 extern int jpg_nbufs;
205 extern int jpg_bufsize;
206 extern int pass_through;
208 static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
209 module_param(lock_norm, int, 0644);
210 MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
212 #ifdef CONFIG_VIDEO_V4L2
213 /* small helper function for calculating buffersizes for v4l2
214 * we calculate the nearest higher power-of-two, which
215 * will be the recommended buffersize */
216 static __u32
217 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
219 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
220 __u32 num = (1024 * 512) / (div);
221 __u32 result = 2;
223 num--;
224 while (num) {
225 num >>= 1;
226 result <<= 1;
229 if (result > jpg_bufsize)
230 return jpg_bufsize;
231 if (result < 8192)
232 return 8192;
233 return result;
235 #endif
237 /* forward references */
238 static void v4l_fbuffer_free(struct file *file);
239 static void jpg_fbuffer_free(struct file *file);
242 * Allocate the V4L grab buffers
244 * These have to be pysically contiguous.
245 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
246 * else we try to allocate them with bigphysarea_alloc_pages
247 * if the bigphysarea patch is present in the kernel,
248 * else we try to use high memory (if the user has bootet
249 * Linux with the necessary memory left over).
252 static unsigned long
253 get_high_mem (unsigned long size)
256 * Check if there is usable memory at the end of Linux memory
257 * of at least size. Return the physical address of this memory,
258 * return 0 on failure.
260 * The idea is from Alexandro Rubini's book "Linux device drivers".
261 * The driver from him which is downloadable from O'Reilly's
262 * web site misses the "virt_to_phys(high_memory)" part
263 * (and therefore doesn't work at all - at least with 2.2.x kernels).
265 * It should be unnecessary to mention that THIS IS DANGEROUS,
266 * if more than one driver at a time has the idea to use this memory!!!!
269 volatile unsigned char __iomem *mem;
270 unsigned char c;
271 unsigned long hi_mem_ph;
272 unsigned long i;
274 /* Map the high memory to user space */
276 hi_mem_ph = virt_to_phys(high_memory);
278 mem = ioremap(hi_mem_ph, size);
279 if (!mem) {
280 dprintk(1,
281 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
282 ZORAN_NAME);
283 return 0;
286 for (i = 0; i < size; i++) {
287 /* Check if it is memory */
288 c = i & 0xff;
289 writeb(c, mem + i);
290 if (readb(mem + i) != c)
291 break;
292 c = 255 - c;
293 writeb(c, mem + i);
294 if (readb(mem + i) != c)
295 break;
296 writeb(0, mem + i); /* zero out memory */
298 /* give the kernel air to breath */
299 if ((i & 0x3ffff) == 0x3ffff)
300 schedule();
303 iounmap(mem);
305 if (i != size) {
306 dprintk(1,
307 KERN_ERR
308 "%s: get_high_mem() - requested %lu, avail %lu\n",
309 ZORAN_NAME, size, i);
310 return 0;
313 return hi_mem_ph;
316 static int
317 v4l_fbuffer_alloc (struct file *file)
319 struct zoran_fh *fh = file->private_data;
320 struct zoran *zr = fh->zr;
321 int i, off;
322 unsigned char *mem;
323 unsigned long pmem = 0;
325 /* we might have old buffers lying around... */
326 if (fh->v4l_buffers.ready_to_be_freed) {
327 v4l_fbuffer_free(file);
330 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
331 if (fh->v4l_buffers.buffer[i].fbuffer)
332 dprintk(2,
333 KERN_WARNING
334 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
335 ZR_DEVNAME(zr), i);
337 //udelay(20);
338 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
339 /* Use kmalloc */
341 mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
342 if (mem == 0) {
343 dprintk(1,
344 KERN_ERR
345 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
346 ZR_DEVNAME(zr), i);
347 v4l_fbuffer_free(file);
348 return -ENOBUFS;
350 fh->v4l_buffers.buffer[i].fbuffer = mem;
351 fh->v4l_buffers.buffer[i].fbuffer_phys =
352 virt_to_phys(mem);
353 fh->v4l_buffers.buffer[i].fbuffer_bus =
354 virt_to_bus(mem);
355 for (off = 0; off < fh->v4l_buffers.buffer_size;
356 off += PAGE_SIZE)
357 SetPageReserved(MAP_NR(mem + off));
358 dprintk(4,
359 KERN_INFO
360 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
361 ZR_DEVNAME(zr), i, (unsigned long) mem,
362 virt_to_bus(mem));
363 } else {
365 /* Use high memory which has been left at boot time */
367 /* Ok., Ok. this is an evil hack - we make
368 * the assumption that physical addresses are
369 * the same as bus addresses (true at least
370 * for Intel processors). The whole method of
371 * obtaining and using this memory is not very
372 * nice - but I hope it saves some poor users
373 * from kernel hacking, which might have even
374 * more evil results */
376 if (i == 0) {
377 int size =
378 fh->v4l_buffers.num_buffers *
379 fh->v4l_buffers.buffer_size;
381 pmem = get_high_mem(size);
382 if (pmem == 0) {
383 dprintk(1,
384 KERN_ERR
385 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
386 ZR_DEVNAME(zr), size >> 10);
387 return -ENOBUFS;
389 fh->v4l_buffers.buffer[0].fbuffer = NULL;
390 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
391 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
392 dprintk(4,
393 KERN_INFO
394 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
395 ZR_DEVNAME(zr), size >> 10);
396 } else {
397 fh->v4l_buffers.buffer[i].fbuffer = NULL;
398 fh->v4l_buffers.buffer[i].fbuffer_phys =
399 pmem + i * fh->v4l_buffers.buffer_size;
400 fh->v4l_buffers.buffer[i].fbuffer_bus =
401 pmem + i * fh->v4l_buffers.buffer_size;
406 fh->v4l_buffers.allocated = 1;
408 return 0;
411 /* free the V4L grab buffers */
412 static void
413 v4l_fbuffer_free (struct file *file)
415 struct zoran_fh *fh = file->private_data;
416 struct zoran *zr = fh->zr;
417 int i, off;
418 unsigned char *mem;
420 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
422 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
423 if (!fh->v4l_buffers.buffer[i].fbuffer)
424 continue;
426 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
427 mem = fh->v4l_buffers.buffer[i].fbuffer;
428 for (off = 0; off < fh->v4l_buffers.buffer_size;
429 off += PAGE_SIZE)
430 ClearPageReserved(MAP_NR(mem + off));
431 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
433 fh->v4l_buffers.buffer[i].fbuffer = NULL;
436 fh->v4l_buffers.allocated = 0;
437 fh->v4l_buffers.ready_to_be_freed = 0;
441 * Allocate the MJPEG grab buffers.
443 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
444 * kmalloc is used to request a physically contiguous area,
445 * else we allocate the memory in framgents with get_zeroed_page.
447 * If a Natoma chipset is present and this is a revision 1 zr36057,
448 * each MJPEG buffer needs to be physically contiguous.
449 * (RJ: This statement is from Dave Perks' original driver,
450 * I could never check it because I have a zr36067)
451 * The driver cares about this because it reduces the buffer
452 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
454 * RJ: The contents grab buffers needs never be accessed in the driver.
455 * Therefore there is no need to allocate them with vmalloc in order
456 * to get a contiguous virtual memory space.
457 * I don't understand why many other drivers first allocate them with
458 * vmalloc (which uses internally also get_zeroed_page, but delivers you
459 * virtual addresses) and then again have to make a lot of efforts
460 * to get the physical address.
462 * Ben Capper:
463 * On big-endian architectures (such as ppc) some extra steps
464 * are needed. When reading and writing to the stat_com array
465 * and fragment buffers, the device expects to see little-
466 * endian values. The use of cpu_to_le32() and le32_to_cpu()
467 * in this function (and one or two others in zoran_device.c)
468 * ensure that these values are always stored in little-endian
469 * form, regardless of architecture. The zr36057 does Very Bad
470 * Things on big endian architectures if the stat_com array
471 * and fragment buffers are not little-endian.
474 static int
475 jpg_fbuffer_alloc (struct file *file)
477 struct zoran_fh *fh = file->private_data;
478 struct zoran *zr = fh->zr;
479 int i, j, off;
480 unsigned long mem;
482 /* we might have old buffers lying around */
483 if (fh->jpg_buffers.ready_to_be_freed) {
484 jpg_fbuffer_free(file);
487 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
488 if (fh->jpg_buffers.buffer[i].frag_tab)
489 dprintk(2,
490 KERN_WARNING
491 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
492 ZR_DEVNAME(zr), i);
494 /* Allocate fragment table for this buffer */
496 mem = get_zeroed_page(GFP_KERNEL);
497 if (mem == 0) {
498 dprintk(1,
499 KERN_ERR
500 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
501 ZR_DEVNAME(zr), i);
502 jpg_fbuffer_free(file);
503 return -ENOBUFS;
505 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
506 fh->jpg_buffers.buffer[i].frag_tab_bus =
507 virt_to_bus((void *) mem);
509 //if (alloc_contig) {
510 if (fh->jpg_buffers.need_contiguous) {
511 mem =
512 (unsigned long) kmalloc(fh->jpg_buffers.
513 buffer_size,
514 GFP_KERNEL);
515 if (mem == 0) {
516 dprintk(1,
517 KERN_ERR
518 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
519 ZR_DEVNAME(zr), i);
520 jpg_fbuffer_free(file);
521 return -ENOBUFS;
523 fh->jpg_buffers.buffer[i].frag_tab[0] =
524 cpu_to_le32(virt_to_bus((void *) mem));
525 fh->jpg_buffers.buffer[i].frag_tab[1] =
526 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
527 for (off = 0; off < fh->jpg_buffers.buffer_size;
528 off += PAGE_SIZE)
529 SetPageReserved(MAP_NR(mem + off));
530 } else {
531 /* jpg_bufsize is allreay page aligned */
532 for (j = 0;
533 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
534 j++) {
535 mem = get_zeroed_page(GFP_KERNEL);
536 if (mem == 0) {
537 dprintk(1,
538 KERN_ERR
539 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
540 ZR_DEVNAME(zr), i);
541 jpg_fbuffer_free(file);
542 return -ENOBUFS;
545 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
546 cpu_to_le32(virt_to_bus((void *) mem));
547 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
548 1] =
549 cpu_to_le32((PAGE_SIZE / 4) << 1);
550 SetPageReserved(MAP_NR(mem));
553 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
557 dprintk(4,
558 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
559 ZR_DEVNAME(zr),
560 (fh->jpg_buffers.num_buffers *
561 fh->jpg_buffers.buffer_size) >> 10);
563 fh->jpg_buffers.allocated = 1;
565 return 0;
568 /* free the MJPEG grab buffers */
569 static void
570 jpg_fbuffer_free (struct file *file)
572 struct zoran_fh *fh = file->private_data;
573 struct zoran *zr = fh->zr;
574 int i, j, off;
575 unsigned char *mem;
577 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
579 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
580 if (!fh->jpg_buffers.buffer[i].frag_tab)
581 continue;
583 //if (alloc_contig) {
584 if (fh->jpg_buffers.need_contiguous) {
585 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
586 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
587 fh->jpg_buffers.buffer[i].frag_tab[0]));
588 for (off = 0;
589 off < fh->jpg_buffers.buffer_size;
590 off += PAGE_SIZE)
591 ClearPageReserved(MAP_NR
592 (mem + off));
593 kfree(mem);
594 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
595 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
597 } else {
598 for (j = 0;
599 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
600 j++) {
601 if (!fh->jpg_buffers.buffer[i].
602 frag_tab[2 * j])
603 break;
604 ClearPageReserved(MAP_NR
605 (bus_to_virt
606 (le32_to_cpu
607 (fh->jpg_buffers.
608 buffer[i].frag_tab[2 *
609 j]))));
610 free_page((unsigned long)
611 bus_to_virt
612 (le32_to_cpu
613 (fh->jpg_buffers.
614 buffer[i].
615 frag_tab[2 * j])));
616 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
618 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
619 1] = 0;
623 free_page((unsigned long) fh->jpg_buffers.buffer[i].
624 frag_tab);
625 fh->jpg_buffers.buffer[i].frag_tab = NULL;
628 fh->jpg_buffers.allocated = 0;
629 fh->jpg_buffers.ready_to_be_freed = 0;
633 * V4L Buffer grabbing
636 static int
637 zoran_v4l_set_format (struct file *file,
638 int width,
639 int height,
640 const struct zoran_format *format)
642 struct zoran_fh *fh = file->private_data;
643 struct zoran *zr = fh->zr;
644 int bpp;
646 /* Check size and format of the grab wanted */
648 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
649 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
650 dprintk(1,
651 KERN_ERR
652 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
653 ZR_DEVNAME(zr), width, height);
654 return -EINVAL;
657 bpp = (format->depth + 7) / 8;
659 /* Check against available buffer size */
660 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
661 dprintk(1,
662 KERN_ERR
663 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
664 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
665 return -EINVAL;
668 /* The video front end needs 4-byte alinged line sizes */
670 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
671 dprintk(1,
672 KERN_ERR
673 "%s: v4l_set_format() - wrong frame alingment\n",
674 ZR_DEVNAME(zr));
675 return -EINVAL;
678 fh->v4l_settings.width = width;
679 fh->v4l_settings.height = height;
680 fh->v4l_settings.format = format;
681 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
683 return 0;
686 static int
687 zoran_v4l_queue_frame (struct file *file,
688 int num)
690 struct zoran_fh *fh = file->private_data;
691 struct zoran *zr = fh->zr;
692 unsigned long flags;
693 int res = 0;
695 if (!fh->v4l_buffers.allocated) {
696 dprintk(1,
697 KERN_ERR
698 "%s: v4l_queue_frame() - buffers not yet allocated\n",
699 ZR_DEVNAME(zr));
700 res = -ENOMEM;
703 /* No grabbing outside the buffer range! */
704 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
705 dprintk(1,
706 KERN_ERR
707 "%s: v4l_queue_frame() - buffer %d is out of range\n",
708 ZR_DEVNAME(zr), num);
709 res = -EINVAL;
712 spin_lock_irqsave(&zr->spinlock, flags);
714 if (fh->v4l_buffers.active == ZORAN_FREE) {
715 if (zr->v4l_buffers.active == ZORAN_FREE) {
716 zr->v4l_buffers = fh->v4l_buffers;
717 fh->v4l_buffers.active = ZORAN_ACTIVE;
718 } else {
719 dprintk(1,
720 KERN_ERR
721 "%s: v4l_queue_frame() - another session is already capturing\n",
722 ZR_DEVNAME(zr));
723 res = -EBUSY;
727 /* make sure a grab isn't going on currently with this buffer */
728 if (!res) {
729 switch (zr->v4l_buffers.buffer[num].state) {
730 default:
731 case BUZ_STATE_PEND:
732 if (zr->v4l_buffers.active == ZORAN_FREE) {
733 fh->v4l_buffers.active = ZORAN_FREE;
734 zr->v4l_buffers.allocated = 0;
736 res = -EBUSY; /* what are you doing? */
737 break;
738 case BUZ_STATE_DONE:
739 dprintk(2,
740 KERN_WARNING
741 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
742 ZR_DEVNAME(zr), num);
743 case BUZ_STATE_USER:
744 /* since there is at least one unused buffer there's room for at least
745 * one more pend[] entry */
746 zr->v4l_pend[zr->v4l_pend_head++ &
747 V4L_MASK_FRAME] = num;
748 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
749 zr->v4l_buffers.buffer[num].bs.length =
750 fh->v4l_settings.bytesperline *
751 zr->v4l_settings.height;
752 fh->v4l_buffers.buffer[num] =
753 zr->v4l_buffers.buffer[num];
754 break;
758 spin_unlock_irqrestore(&zr->spinlock, flags);
760 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
761 zr->v4l_buffers.active = fh->v4l_buffers.active;
763 return res;
766 static int
767 v4l_grab (struct file *file,
768 struct video_mmap *mp)
770 struct zoran_fh *fh = file->private_data;
771 struct zoran *zr = fh->zr;
772 int res = 0, i;
774 for (i = 0; i < NUM_FORMATS; i++) {
775 if (zoran_formats[i].palette == mp->format &&
776 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
777 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
778 break;
780 if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
781 dprintk(1,
782 KERN_ERR
783 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
784 ZR_DEVNAME(zr));
785 return -EINVAL;
789 * To minimize the time spent in the IRQ routine, we avoid setting up
790 * the video front end there.
791 * If this grab has different parameters from a running streaming capture
792 * we stop the streaming capture and start it over again.
794 if (zr->v4l_memgrab_active &&
795 (zr->v4l_settings.width != mp->width ||
796 zr->v4l_settings.height != mp->height ||
797 zr->v4l_settings.format->palette != mp->format)) {
798 res = wait_grab_pending(zr);
799 if (res)
800 return res;
802 if ((res = zoran_v4l_set_format(file,
803 mp->width,
804 mp->height,
805 &zoran_formats[i])))
806 return res;
807 zr->v4l_settings = fh->v4l_settings;
809 /* queue the frame in the pending queue */
810 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
811 fh->v4l_buffers.active = ZORAN_FREE;
812 return res;
815 /* put the 36057 into frame grabbing mode */
816 if (!res && !zr->v4l_memgrab_active)
817 zr36057_set_memgrab(zr, 1);
819 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
821 return res;
825 * Sync on a V4L buffer
828 static int
829 v4l_sync (struct file *file,
830 int frame)
832 struct zoran_fh *fh = file->private_data;
833 struct zoran *zr = fh->zr;
834 unsigned long flags;
836 if (fh->v4l_buffers.active == ZORAN_FREE) {
837 dprintk(1,
838 KERN_ERR
839 "%s: v4l_sync() - no grab active for this session\n",
840 ZR_DEVNAME(zr));
841 return -EINVAL;
844 /* check passed-in frame number */
845 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
846 dprintk(1,
847 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
848 ZR_DEVNAME(zr), frame);
849 return -EINVAL;
852 /* Check if is buffer was queued at all */
853 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
854 dprintk(1,
855 KERN_ERR
856 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
857 ZR_DEVNAME(zr));
858 return -EPROTO;
861 /* wait on this buffer to get ready */
862 if (!wait_event_interruptible_timeout(zr->v4l_capq,
863 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
864 10*HZ))
865 return -ETIME;
866 if (signal_pending(current))
867 return -ERESTARTSYS;
869 /* buffer should now be in BUZ_STATE_DONE */
870 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
871 dprintk(2,
872 KERN_ERR "%s: v4l_sync() - internal state error\n",
873 ZR_DEVNAME(zr));
875 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
876 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
878 spin_lock_irqsave(&zr->spinlock, flags);
880 /* Check if streaming capture has finished */
881 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
882 zr36057_set_memgrab(zr, 0);
883 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
884 fh->v4l_buffers.active = zr->v4l_buffers.active =
885 ZORAN_FREE;
886 zr->v4l_buffers.allocated = 0;
890 spin_unlock_irqrestore(&zr->spinlock, flags);
892 return 0;
896 * Queue a MJPEG buffer for capture/playback
899 static int
900 zoran_jpg_queue_frame (struct file *file,
901 int num,
902 enum zoran_codec_mode mode)
904 struct zoran_fh *fh = file->private_data;
905 struct zoran *zr = fh->zr;
906 unsigned long flags;
907 int res = 0;
909 /* Check if buffers are allocated */
910 if (!fh->jpg_buffers.allocated) {
911 dprintk(1,
912 KERN_ERR
913 "%s: jpg_queue_frame() - buffers not yet allocated\n",
914 ZR_DEVNAME(zr));
915 return -ENOMEM;
918 /* No grabbing outside the buffer range! */
919 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
920 dprintk(1,
921 KERN_ERR
922 "%s: jpg_queue_frame() - buffer %d out of range\n",
923 ZR_DEVNAME(zr), num);
924 return -EINVAL;
927 /* what is the codec mode right now? */
928 if (zr->codec_mode == BUZ_MODE_IDLE) {
929 zr->jpg_settings = fh->jpg_settings;
930 } else if (zr->codec_mode != mode) {
931 /* wrong codec mode active - invalid */
932 dprintk(1,
933 KERN_ERR
934 "%s: jpg_queue_frame() - codec in wrong mode\n",
935 ZR_DEVNAME(zr));
936 return -EINVAL;
939 if (fh->jpg_buffers.active == ZORAN_FREE) {
940 if (zr->jpg_buffers.active == ZORAN_FREE) {
941 zr->jpg_buffers = fh->jpg_buffers;
942 fh->jpg_buffers.active = ZORAN_ACTIVE;
943 } else {
944 dprintk(1,
945 KERN_ERR
946 "%s: jpg_queue_frame() - another session is already capturing\n",
947 ZR_DEVNAME(zr));
948 res = -EBUSY;
952 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
953 /* Ok load up the jpeg codec */
954 zr36057_enable_jpg(zr, mode);
957 spin_lock_irqsave(&zr->spinlock, flags);
959 if (!res) {
960 switch (zr->jpg_buffers.buffer[num].state) {
961 case BUZ_STATE_DONE:
962 dprintk(2,
963 KERN_WARNING
964 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
965 ZR_DEVNAME(zr));
966 case BUZ_STATE_USER:
967 /* since there is at least one unused buffer there's room for at
968 *least one more pend[] entry */
969 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
970 num;
971 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
972 fh->jpg_buffers.buffer[num] =
973 zr->jpg_buffers.buffer[num];
974 zoran_feed_stat_com(zr);
975 break;
976 default:
977 case BUZ_STATE_DMA:
978 case BUZ_STATE_PEND:
979 if (zr->jpg_buffers.active == ZORAN_FREE) {
980 fh->jpg_buffers.active = ZORAN_FREE;
981 zr->jpg_buffers.allocated = 0;
983 res = -EBUSY; /* what are you doing? */
984 break;
988 spin_unlock_irqrestore(&zr->spinlock, flags);
990 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
991 zr->jpg_buffers.active = fh->jpg_buffers.active;
994 return res;
997 static int
998 jpg_qbuf (struct file *file,
999 int frame,
1000 enum zoran_codec_mode mode)
1002 struct zoran_fh *fh = file->private_data;
1003 struct zoran *zr = fh->zr;
1004 int res = 0;
1006 /* Does the user want to stop streaming? */
1007 if (frame < 0) {
1008 if (zr->codec_mode == mode) {
1009 if (fh->jpg_buffers.active == ZORAN_FREE) {
1010 dprintk(1,
1011 KERN_ERR
1012 "%s: jpg_qbuf(-1) - session not active\n",
1013 ZR_DEVNAME(zr));
1014 return -EINVAL;
1016 fh->jpg_buffers.active = zr->jpg_buffers.active =
1017 ZORAN_FREE;
1018 zr->jpg_buffers.allocated = 0;
1019 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1020 return 0;
1021 } else {
1022 dprintk(1,
1023 KERN_ERR
1024 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1025 ZR_DEVNAME(zr));
1026 return -EINVAL;
1030 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1031 return res;
1033 /* Start the jpeg codec when the first frame is queued */
1034 if (!res && zr->jpg_que_head == 1)
1035 jpeg_start(zr);
1037 return res;
1041 * Sync on a MJPEG buffer
1044 static int
1045 jpg_sync (struct file *file,
1046 struct zoran_sync *bs)
1048 struct zoran_fh *fh = file->private_data;
1049 struct zoran *zr = fh->zr;
1050 unsigned long flags;
1051 int frame;
1053 if (fh->jpg_buffers.active == ZORAN_FREE) {
1054 dprintk(1,
1055 KERN_ERR
1056 "%s: jpg_sync() - capture is not currently active\n",
1057 ZR_DEVNAME(zr));
1058 return -EINVAL;
1060 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1061 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1062 dprintk(1,
1063 KERN_ERR
1064 "%s: jpg_sync() - codec not in streaming mode\n",
1065 ZR_DEVNAME(zr));
1066 return -EINVAL;
1068 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1069 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1070 zr->jpg_dma_tail == zr->jpg_dma_head),
1071 10*HZ)) {
1072 int isr;
1074 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1075 udelay(1);
1076 zr->codec->control(zr->codec, CODEC_G_STATUS,
1077 sizeof(isr), &isr);
1078 dprintk(1,
1079 KERN_ERR
1080 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1081 ZR_DEVNAME(zr), isr);
1083 return -ETIME;
1086 if (signal_pending(current))
1087 return -ERESTARTSYS;
1089 spin_lock_irqsave(&zr->spinlock, flags);
1091 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1092 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1093 else
1094 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1096 /* buffer should now be in BUZ_STATE_DONE */
1097 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1098 dprintk(2,
1099 KERN_ERR "%s: jpg_sync() - internal state error\n",
1100 ZR_DEVNAME(zr));
1102 *bs = zr->jpg_buffers.buffer[frame].bs;
1103 bs->frame = frame;
1104 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1105 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1107 spin_unlock_irqrestore(&zr->spinlock, flags);
1109 return 0;
1112 static void
1113 zoran_open_init_session (struct file *file)
1115 int i;
1116 struct zoran_fh *fh = file->private_data;
1117 struct zoran *zr = fh->zr;
1119 /* Per default, map the V4L Buffers */
1120 fh->map_mode = ZORAN_MAP_MODE_RAW;
1122 /* take over the card's current settings */
1123 fh->overlay_settings = zr->overlay_settings;
1124 fh->overlay_settings.is_set = 0;
1125 fh->overlay_settings.format = zr->overlay_settings.format;
1126 fh->overlay_active = ZORAN_FREE;
1128 /* v4l settings */
1129 fh->v4l_settings = zr->v4l_settings;
1131 /* v4l_buffers */
1132 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1133 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1134 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1135 fh->v4l_buffers.buffer[i].bs.frame = i;
1137 fh->v4l_buffers.allocated = 0;
1138 fh->v4l_buffers.ready_to_be_freed = 0;
1139 fh->v4l_buffers.active = ZORAN_FREE;
1140 fh->v4l_buffers.buffer_size = v4l_bufsize;
1141 fh->v4l_buffers.num_buffers = v4l_nbufs;
1143 /* jpg settings */
1144 fh->jpg_settings = zr->jpg_settings;
1146 /* jpg_buffers */
1147 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1148 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1149 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1150 fh->jpg_buffers.buffer[i].bs.frame = i;
1152 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1153 fh->jpg_buffers.allocated = 0;
1154 fh->jpg_buffers.ready_to_be_freed = 0;
1155 fh->jpg_buffers.active = ZORAN_FREE;
1156 fh->jpg_buffers.buffer_size = jpg_bufsize;
1157 fh->jpg_buffers.num_buffers = jpg_nbufs;
1160 static void
1161 zoran_close_end_session (struct file *file)
1163 struct zoran_fh *fh = file->private_data;
1164 struct zoran *zr = fh->zr;
1166 /* overlay */
1167 if (fh->overlay_active != ZORAN_FREE) {
1168 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1169 zr->v4l_overlay_active = 0;
1170 if (!zr->v4l_memgrab_active)
1171 zr36057_overlay(zr, 0);
1172 zr->overlay_mask = NULL;
1175 /* v4l capture */
1176 if (fh->v4l_buffers.active != ZORAN_FREE) {
1177 long flags;
1179 spin_lock_irqsave(&zr->spinlock, flags);
1180 zr36057_set_memgrab(zr, 0);
1181 zr->v4l_buffers.allocated = 0;
1182 zr->v4l_buffers.active = fh->v4l_buffers.active =
1183 ZORAN_FREE;
1184 spin_unlock_irqrestore(&zr->spinlock, flags);
1187 /* v4l buffers */
1188 if (fh->v4l_buffers.allocated ||
1189 fh->v4l_buffers.ready_to_be_freed) {
1190 v4l_fbuffer_free(file);
1193 /* jpg capture */
1194 if (fh->jpg_buffers.active != ZORAN_FREE) {
1195 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1196 zr->jpg_buffers.allocated = 0;
1197 zr->jpg_buffers.active = fh->jpg_buffers.active =
1198 ZORAN_FREE;
1201 /* jpg buffers */
1202 if (fh->jpg_buffers.allocated ||
1203 fh->jpg_buffers.ready_to_be_freed) {
1204 jpg_fbuffer_free(file);
1209 * Open a zoran card. Right now the flags stuff is just playing
1212 static int
1213 zoran_open (struct inode *inode,
1214 struct file *file)
1216 unsigned int minor = iminor(inode);
1217 struct zoran *zr = NULL;
1218 struct zoran_fh *fh;
1219 int i, res, first_open = 0, have_module_locks = 0;
1221 /* find the device */
1222 for (i = 0; i < zoran_num; i++) {
1223 if (zoran[i].video_dev->minor == minor) {
1224 zr = &zoran[i];
1225 break;
1229 if (!zr) {
1230 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1231 res = -ENODEV;
1232 goto open_unlock_and_return;
1235 /* see fs/device.c - the kernel already locks during open(),
1236 * so locking ourselves only causes deadlocks */
1237 /*mutex_lock(&zr->resource_lock);*/
1239 if (!zr->decoder) {
1240 dprintk(1,
1241 KERN_ERR "%s: no TV decoder loaded for device!\n",
1242 ZR_DEVNAME(zr));
1243 res = -EIO;
1244 goto open_unlock_and_return;
1247 /* try to grab a module lock */
1248 if (!try_module_get(THIS_MODULE)) {
1249 dprintk(1,
1250 KERN_ERR
1251 "%s: failed to acquire my own lock! PANIC!\n",
1252 ZR_DEVNAME(zr));
1253 res = -ENODEV;
1254 goto open_unlock_and_return;
1256 if (!try_module_get(zr->decoder->driver->driver.owner)) {
1257 dprintk(1,
1258 KERN_ERR
1259 "%s: failed to grab ownership of i2c decoder\n",
1260 ZR_DEVNAME(zr));
1261 res = -EIO;
1262 module_put(THIS_MODULE);
1263 goto open_unlock_and_return;
1265 if (zr->encoder &&
1266 !try_module_get(zr->encoder->driver->driver.owner)) {
1267 dprintk(1,
1268 KERN_ERR
1269 "%s: failed to grab ownership of i2c encoder\n",
1270 ZR_DEVNAME(zr));
1271 res = -EIO;
1272 module_put(zr->decoder->driver->driver.owner);
1273 module_put(THIS_MODULE);
1274 goto open_unlock_and_return;
1277 have_module_locks = 1;
1279 if (zr->user >= 2048) {
1280 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1281 ZR_DEVNAME(zr), zr->user);
1282 res = -EBUSY;
1283 goto open_unlock_and_return;
1286 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1287 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1289 /* now, create the open()-specific file_ops struct */
1290 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1291 if (!fh) {
1292 dprintk(1,
1293 KERN_ERR
1294 "%s: zoran_open() - allocation of zoran_fh failed\n",
1295 ZR_DEVNAME(zr));
1296 res = -ENOMEM;
1297 goto open_unlock_and_return;
1299 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1300 * on norm-change! */
1301 fh->overlay_mask =
1302 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1303 if (!fh->overlay_mask) {
1304 dprintk(1,
1305 KERN_ERR
1306 "%s: zoran_open() - allocation of overlay_mask failed\n",
1307 ZR_DEVNAME(zr));
1308 kfree(fh);
1309 res = -ENOMEM;
1310 goto open_unlock_and_return;
1313 if (zr->user++ == 0)
1314 first_open = 1;
1316 /*mutex_unlock(&zr->resource_lock);*/
1318 /* default setup - TODO: look at flags */
1319 if (first_open) { /* First device open */
1320 zr36057_restart(zr);
1321 zoran_open_init_params(zr);
1322 zoran_init_hardware(zr);
1324 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1327 /* set file_ops stuff */
1328 file->private_data = fh;
1329 fh->zr = zr;
1330 zoran_open_init_session(file);
1332 return 0;
1334 open_unlock_and_return:
1335 /* if we grabbed locks, release them accordingly */
1336 if (have_module_locks) {
1337 module_put(zr->decoder->driver->driver.owner);
1338 if (zr->encoder) {
1339 module_put(zr->encoder->driver->driver.owner);
1341 module_put(THIS_MODULE);
1344 /* if there's no device found, we didn't obtain the lock either */
1345 if (zr) {
1346 /*mutex_unlock(&zr->resource_lock);*/
1349 return res;
1352 static int
1353 zoran_close (struct inode *inode,
1354 struct file *file)
1356 struct zoran_fh *fh = file->private_data;
1357 struct zoran *zr = fh->zr;
1359 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1360 ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user);
1362 /* kernel locks (fs/device.c), so don't do that ourselves
1363 * (prevents deadlocks) */
1364 /*mutex_lock(&zr->resource_lock);*/
1366 zoran_close_end_session(file);
1368 if (zr->user-- == 1) { /* Last process */
1369 /* Clean up JPEG process */
1370 wake_up_interruptible(&zr->jpg_capq);
1371 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1372 zr->jpg_buffers.allocated = 0;
1373 zr->jpg_buffers.active = ZORAN_FREE;
1375 /* disable interrupts */
1376 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1378 if (zr36067_debug > 1)
1379 print_interrupts(zr);
1381 /* Overlay off */
1382 zr->v4l_overlay_active = 0;
1383 zr36057_overlay(zr, 0);
1384 zr->overlay_mask = NULL;
1386 /* capture off */
1387 wake_up_interruptible(&zr->v4l_capq);
1388 zr36057_set_memgrab(zr, 0);
1389 zr->v4l_buffers.allocated = 0;
1390 zr->v4l_buffers.active = ZORAN_FREE;
1391 zoran_set_pci_master(zr, 0);
1393 if (!pass_through) { /* Switch to color bar */
1394 int zero = 0, two = 2;
1395 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1396 encoder_command(zr, ENCODER_SET_INPUT, &two);
1400 file->private_data = NULL;
1401 kfree(fh->overlay_mask);
1402 kfree(fh);
1404 /* release locks on the i2c modules */
1405 module_put(zr->decoder->driver->driver.owner);
1406 if (zr->encoder) {
1407 module_put(zr->encoder->driver->driver.owner);
1409 module_put(THIS_MODULE);
1411 /*mutex_unlock(&zr->resource_lock);*/
1413 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1415 return 0;
1419 static ssize_t
1420 zoran_read (struct file *file,
1421 char __user *data,
1422 size_t count,
1423 loff_t *ppos)
1425 /* we simply don't support read() (yet)... */
1427 return -EINVAL;
1430 static ssize_t
1431 zoran_write (struct file *file,
1432 const char __user *data,
1433 size_t count,
1434 loff_t *ppos)
1436 /* ...and the same goes for write() */
1438 return -EINVAL;
1441 static int
1442 setup_fbuffer (struct file *file,
1443 void *base,
1444 const struct zoran_format *fmt,
1445 int width,
1446 int height,
1447 int bytesperline)
1449 struct zoran_fh *fh = file->private_data;
1450 struct zoran *zr = fh->zr;
1452 /* (Ronald) v4l/v4l2 guidelines */
1453 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1454 return -EPERM;
1456 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1457 ALi Magik (that needs very low latency while the card needs a
1458 higher value always) */
1460 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1461 return -ENXIO;
1463 /* we need a bytesperline value, even if not given */
1464 if (!bytesperline)
1465 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1467 #if 0
1468 if (zr->overlay_active) {
1469 /* dzjee... stupid users... don't even bother to turn off
1470 * overlay before changing the memory location...
1471 * normally, we would return errors here. However, one of
1472 * the tools that does this is... xawtv! and since xawtv
1473 * is used by +/- 99% of the users, we'd rather be user-
1474 * friendly and silently do as if nothing went wrong */
1475 dprintk(3,
1476 KERN_ERR
1477 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1478 ZR_DEVNAME(zr));
1479 zr36057_overlay(zr, 0);
1481 #endif
1483 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1484 dprintk(1,
1485 KERN_ERR
1486 "%s: setup_fbuffer() - no valid overlay format given\n",
1487 ZR_DEVNAME(zr));
1488 return -EINVAL;
1490 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1491 dprintk(1,
1492 KERN_ERR
1493 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1494 ZR_DEVNAME(zr), width, height, bytesperline);
1495 return -EINVAL;
1497 if (bytesperline & 3) {
1498 dprintk(1,
1499 KERN_ERR
1500 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1501 ZR_DEVNAME(zr), bytesperline);
1502 return -EINVAL;
1505 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1506 zr->buffer.height = height;
1507 zr->buffer.width = width;
1508 zr->buffer.depth = fmt->depth;
1509 zr->overlay_settings.format = fmt;
1510 zr->buffer.bytesperline = bytesperline;
1512 /* The user should set new window parameters */
1513 zr->overlay_settings.is_set = 0;
1515 return 0;
1519 static int
1520 setup_window (struct file *file,
1521 int x,
1522 int y,
1523 int width,
1524 int height,
1525 struct video_clip __user *clips,
1526 int clipcount,
1527 void __user *bitmap)
1529 struct zoran_fh *fh = file->private_data;
1530 struct zoran *zr = fh->zr;
1531 struct video_clip *vcp = NULL;
1532 int on, end;
1535 if (!zr->buffer.base) {
1536 dprintk(1,
1537 KERN_ERR
1538 "%s: setup_window() - frame buffer has to be set first\n",
1539 ZR_DEVNAME(zr));
1540 return -EINVAL;
1543 if (!fh->overlay_settings.format) {
1544 dprintk(1,
1545 KERN_ERR
1546 "%s: setup_window() - no overlay format set\n",
1547 ZR_DEVNAME(zr));
1548 return -EINVAL;
1552 * The video front end needs 4-byte alinged line sizes, we correct that
1553 * silently here if necessary
1555 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1556 end = (x + width) & ~1; /* round down */
1557 x = (x + 1) & ~1; /* round up */
1558 width = end - x;
1561 if (zr->buffer.depth == 24) {
1562 end = (x + width) & ~3; /* round down */
1563 x = (x + 3) & ~3; /* round up */
1564 width = end - x;
1567 if (width > BUZ_MAX_WIDTH)
1568 width = BUZ_MAX_WIDTH;
1569 if (height > BUZ_MAX_HEIGHT)
1570 height = BUZ_MAX_HEIGHT;
1572 /* Check for vaild parameters */
1573 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1574 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1575 dprintk(1,
1576 KERN_ERR
1577 "%s: setup_window() - width = %d or height = %d invalid\n",
1578 ZR_DEVNAME(zr), width, height);
1579 return -EINVAL;
1582 fh->overlay_settings.x = x;
1583 fh->overlay_settings.y = y;
1584 fh->overlay_settings.width = width;
1585 fh->overlay_settings.height = height;
1586 fh->overlay_settings.clipcount = clipcount;
1589 * If an overlay is running, we have to switch it off
1590 * and switch it on again in order to get the new settings in effect.
1592 * We also want to avoid that the overlay mask is written
1593 * when an overlay is running.
1596 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1597 zr->overlay_active != ZORAN_FREE &&
1598 fh->overlay_active != ZORAN_FREE;
1599 if (on)
1600 zr36057_overlay(zr, 0);
1603 * Write the overlay mask if clips are wanted.
1604 * We prefer a bitmap.
1606 if (bitmap) {
1607 /* fake value - it just means we want clips */
1608 fh->overlay_settings.clipcount = 1;
1610 if (copy_from_user(fh->overlay_mask, bitmap,
1611 (width * height + 7) / 8)) {
1612 return -EFAULT;
1614 } else if (clipcount > 0) {
1615 /* write our own bitmap from the clips */
1616 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1617 if (vcp == NULL) {
1618 dprintk(1,
1619 KERN_ERR
1620 "%s: setup_window() - Alloc of clip mask failed\n",
1621 ZR_DEVNAME(zr));
1622 return -ENOMEM;
1624 if (copy_from_user
1625 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1626 vfree(vcp);
1627 return -EFAULT;
1629 write_overlay_mask(file, vcp, clipcount);
1630 vfree(vcp);
1633 fh->overlay_settings.is_set = 1;
1634 if (fh->overlay_active != ZORAN_FREE &&
1635 zr->overlay_active != ZORAN_FREE)
1636 zr->overlay_settings = fh->overlay_settings;
1638 if (on)
1639 zr36057_overlay(zr, 1);
1641 /* Make sure the changes come into effect */
1642 return wait_grab_pending(zr);
1645 static int
1646 setup_overlay (struct file *file,
1647 int on)
1649 struct zoran_fh *fh = file->private_data;
1650 struct zoran *zr = fh->zr;
1652 /* If there is nothing to do, return immediatly */
1653 if ((on && fh->overlay_active != ZORAN_FREE) ||
1654 (!on && fh->overlay_active == ZORAN_FREE))
1655 return 0;
1657 /* check whether we're touching someone else's overlay */
1658 if (on && zr->overlay_active != ZORAN_FREE &&
1659 fh->overlay_active == ZORAN_FREE) {
1660 dprintk(1,
1661 KERN_ERR
1662 "%s: setup_overlay() - overlay is already active for another session\n",
1663 ZR_DEVNAME(zr));
1664 return -EBUSY;
1666 if (!on && zr->overlay_active != ZORAN_FREE &&
1667 fh->overlay_active == ZORAN_FREE) {
1668 dprintk(1,
1669 KERN_ERR
1670 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1671 ZR_DEVNAME(zr));
1672 return -EPERM;
1675 if (on == 0) {
1676 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1677 zr->v4l_overlay_active = 0;
1678 /* When a grab is running, the video simply
1679 * won't be switched on any more */
1680 if (!zr->v4l_memgrab_active)
1681 zr36057_overlay(zr, 0);
1682 zr->overlay_mask = NULL;
1683 } else {
1684 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1685 dprintk(1,
1686 KERN_ERR
1687 "%s: setup_overlay() - buffer or window not set\n",
1688 ZR_DEVNAME(zr));
1689 return -EINVAL;
1691 if (!fh->overlay_settings.format) {
1692 dprintk(1,
1693 KERN_ERR
1694 "%s: setup_overlay() - no overlay format set\n",
1695 ZR_DEVNAME(zr));
1696 return -EINVAL;
1698 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1699 zr->v4l_overlay_active = 1;
1700 zr->overlay_mask = fh->overlay_mask;
1701 zr->overlay_settings = fh->overlay_settings;
1702 if (!zr->v4l_memgrab_active)
1703 zr36057_overlay(zr, 1);
1704 /* When a grab is running, the video will be
1705 * switched on when grab is finished */
1708 /* Make sure the changes come into effect */
1709 return wait_grab_pending(zr);
1712 #ifdef CONFIG_VIDEO_V4L2
1713 /* get the status of a buffer in the clients buffer queue */
1714 static int
1715 zoran_v4l2_buffer_status (struct file *file,
1716 struct v4l2_buffer *buf,
1717 int num)
1719 struct zoran_fh *fh = file->private_data;
1720 struct zoran *zr = fh->zr;
1722 buf->flags = V4L2_BUF_FLAG_MAPPED;
1724 switch (fh->map_mode) {
1725 case ZORAN_MAP_MODE_RAW:
1727 /* check range */
1728 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1729 !fh->v4l_buffers.allocated) {
1730 dprintk(1,
1731 KERN_ERR
1732 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1733 ZR_DEVNAME(zr));
1734 return -EINVAL;
1737 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1738 buf->length = fh->v4l_buffers.buffer_size;
1740 /* get buffer */
1741 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1742 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1743 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1744 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1745 buf->flags |= V4L2_BUF_FLAG_DONE;
1746 buf->timestamp =
1747 fh->v4l_buffers.buffer[num].bs.timestamp;
1748 } else {
1749 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1752 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1753 buf->field = V4L2_FIELD_TOP;
1754 else
1755 buf->field = V4L2_FIELD_INTERLACED;
1757 break;
1759 case ZORAN_MAP_MODE_JPG_REC:
1760 case ZORAN_MAP_MODE_JPG_PLAY:
1762 /* check range */
1763 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1764 !fh->jpg_buffers.allocated) {
1765 dprintk(1,
1766 KERN_ERR
1767 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1768 ZR_DEVNAME(zr));
1769 return -EINVAL;
1772 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1773 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1774 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1775 buf->length = fh->jpg_buffers.buffer_size;
1777 /* these variables are only written after frame has been captured */
1778 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1779 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1780 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1781 buf->timestamp =
1782 fh->jpg_buffers.buffer[num].bs.timestamp;
1783 buf->bytesused =
1784 fh->jpg_buffers.buffer[num].bs.length;
1785 buf->flags |= V4L2_BUF_FLAG_DONE;
1786 } else {
1787 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1790 /* which fields are these? */
1791 if (fh->jpg_settings.TmpDcm != 1)
1792 buf->field =
1793 fh->jpg_settings.
1794 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1795 else
1796 buf->field =
1797 fh->jpg_settings.
1798 odd_even ? V4L2_FIELD_SEQ_TB :
1799 V4L2_FIELD_SEQ_BT;
1801 break;
1803 default:
1805 dprintk(5,
1806 KERN_ERR
1807 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1808 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1809 return -EINVAL;
1812 buf->memory = V4L2_MEMORY_MMAP;
1813 buf->index = num;
1814 buf->m.offset = buf->length * num;
1816 return 0;
1818 #endif
1820 static int
1821 zoran_set_norm (struct zoran *zr,
1822 int norm) /* VIDEO_MODE_* */
1824 int norm_encoder, on;
1826 if (zr->v4l_buffers.active != ZORAN_FREE ||
1827 zr->jpg_buffers.active != ZORAN_FREE) {
1828 dprintk(1,
1829 KERN_WARNING
1830 "%s: set_norm() called while in playback/capture mode\n",
1831 ZR_DEVNAME(zr));
1832 return -EBUSY;
1835 if (lock_norm && norm != zr->norm) {
1836 if (lock_norm > 1) {
1837 dprintk(1,
1838 KERN_WARNING
1839 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1840 ZR_DEVNAME(zr));
1841 return -EPERM;
1842 } else {
1843 dprintk(1,
1844 KERN_WARNING
1845 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1846 ZR_DEVNAME(zr));
1847 norm = zr->norm;
1851 if (norm != VIDEO_MODE_AUTO &&
1852 (norm < 0 || norm >= zr->card.norms ||
1853 !zr->card.tvn[norm])) {
1854 dprintk(1,
1855 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1856 ZR_DEVNAME(zr), norm);
1857 return -EINVAL;
1860 if (norm == VIDEO_MODE_AUTO) {
1861 int status;
1863 /* if we have autodetect, ... */
1864 struct video_decoder_capability caps;
1865 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1866 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1867 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1868 ZR_DEVNAME(zr));
1869 return -EINVAL;
1872 decoder_command(zr, DECODER_SET_NORM, &norm);
1874 /* let changes come into effect */
1875 ssleep(2);
1877 decoder_command(zr, DECODER_GET_STATUS, &status);
1878 if (!(status & DECODER_STATUS_GOOD)) {
1879 dprintk(1,
1880 KERN_ERR
1881 "%s: set_norm() - no norm detected\n",
1882 ZR_DEVNAME(zr));
1883 /* reset norm */
1884 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1885 return -EIO;
1888 if (status & DECODER_STATUS_NTSC)
1889 norm = VIDEO_MODE_NTSC;
1890 else if (status & DECODER_STATUS_SECAM)
1891 norm = VIDEO_MODE_SECAM;
1892 else
1893 norm = VIDEO_MODE_PAL;
1895 zr->timing = zr->card.tvn[norm];
1896 norm_encoder = norm;
1898 /* We switch overlay off and on since a change in the
1899 * norm needs different VFE settings */
1900 on = zr->overlay_active && !zr->v4l_memgrab_active;
1901 if (on)
1902 zr36057_overlay(zr, 0);
1904 decoder_command(zr, DECODER_SET_NORM, &norm);
1905 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1907 if (on)
1908 zr36057_overlay(zr, 1);
1910 /* Make sure the changes come into effect */
1911 zr->norm = norm;
1913 return 0;
1916 static int
1917 zoran_set_input (struct zoran *zr,
1918 int input)
1920 int realinput;
1922 if (input == zr->input) {
1923 return 0;
1926 if (zr->v4l_buffers.active != ZORAN_FREE ||
1927 zr->jpg_buffers.active != ZORAN_FREE) {
1928 dprintk(1,
1929 KERN_WARNING
1930 "%s: set_input() called while in playback/capture mode\n",
1931 ZR_DEVNAME(zr));
1932 return -EBUSY;
1935 if (input < 0 || input >= zr->card.inputs) {
1936 dprintk(1,
1937 KERN_ERR
1938 "%s: set_input() - unnsupported input %d\n",
1939 ZR_DEVNAME(zr), input);
1940 return -EINVAL;
1943 realinput = zr->card.input[input].muxsel;
1944 zr->input = input;
1946 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1948 return 0;
1952 * ioctl routine
1955 static int
1956 zoran_do_ioctl (struct inode *inode,
1957 struct file *file,
1958 unsigned int cmd,
1959 void *arg)
1961 struct zoran_fh *fh = file->private_data;
1962 struct zoran *zr = fh->zr;
1963 /* CAREFUL: used in multiple places here */
1964 struct zoran_jpg_settings settings;
1966 /* we might have older buffers lying around... We don't want
1967 * to wait, but we do want to try cleaning them up ASAP. So
1968 * we try to obtain the lock and free them. If that fails, we
1969 * don't do anything and wait for the next turn. In the end,
1970 * zoran_close() or a new allocation will still free them...
1971 * This is just a 'the sooner the better' extra 'feature'
1973 * We don't free the buffers right on munmap() because that
1974 * causes oopses (kfree() inside munmap() oopses for no
1975 * apparent reason - it's also not reproduceable in any way,
1976 * but moving the free code outside the munmap() handler fixes
1977 * all this... If someone knows why, please explain me (Ronald)
1979 if (mutex_trylock(&zr->resource_lock)) {
1980 /* we obtained it! Let's try to free some things */
1981 if (fh->jpg_buffers.ready_to_be_freed)
1982 jpg_fbuffer_free(file);
1983 if (fh->v4l_buffers.ready_to_be_freed)
1984 v4l_fbuffer_free(file);
1986 mutex_unlock(&zr->resource_lock);
1989 switch (cmd) {
1991 case VIDIOCGCAP:
1993 struct video_capability *vcap = arg;
1995 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
1997 memset(vcap, 0, sizeof(struct video_capability));
1998 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
1999 vcap->type = ZORAN_VID_TYPE;
2001 vcap->channels = zr->card.inputs;
2002 vcap->audios = 0;
2003 mutex_lock(&zr->resource_lock);
2004 vcap->maxwidth = BUZ_MAX_WIDTH;
2005 vcap->maxheight = BUZ_MAX_HEIGHT;
2006 vcap->minwidth = BUZ_MIN_WIDTH;
2007 vcap->minheight = BUZ_MIN_HEIGHT;
2008 mutex_unlock(&zr->resource_lock);
2010 return 0;
2012 break;
2014 case VIDIOCGCHAN:
2016 struct video_channel *vchan = arg;
2017 int channel = vchan->channel;
2019 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2020 ZR_DEVNAME(zr), vchan->channel);
2022 memset(vchan, 0, sizeof(struct video_channel));
2023 if (channel > zr->card.inputs || channel < 0) {
2024 dprintk(1,
2025 KERN_ERR
2026 "%s: VIDIOCGCHAN on not existing channel %d\n",
2027 ZR_DEVNAME(zr), channel);
2028 return -EINVAL;
2031 strcpy(vchan->name, zr->card.input[channel].name);
2033 vchan->tuners = 0;
2034 vchan->flags = 0;
2035 vchan->type = VIDEO_TYPE_CAMERA;
2036 mutex_lock(&zr->resource_lock);
2037 vchan->norm = zr->norm;
2038 mutex_unlock(&zr->resource_lock);
2039 vchan->channel = channel;
2041 return 0;
2043 break;
2045 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2047 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2048 * * ^^^^^^^
2049 * * The famos BTTV driver has it implemented with a struct video_channel argument
2050 * * and we follow it for compatibility reasons
2052 * * BTW: this is the only way the user can set the norm!
2055 case VIDIOCSCHAN:
2057 struct video_channel *vchan = arg;
2058 int res;
2060 dprintk(3,
2061 KERN_DEBUG
2062 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2063 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2065 mutex_lock(&zr->resource_lock);
2066 if ((res = zoran_set_input(zr, vchan->channel)))
2067 goto schan_unlock_and_return;
2068 if ((res = zoran_set_norm(zr, vchan->norm)))
2069 goto schan_unlock_and_return;
2071 /* Make sure the changes come into effect */
2072 res = wait_grab_pending(zr);
2073 schan_unlock_and_return:
2074 mutex_unlock(&zr->resource_lock);
2075 return res;
2077 break;
2079 case VIDIOCGPICT:
2081 struct video_picture *vpict = arg;
2083 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2085 memset(vpict, 0, sizeof(struct video_picture));
2086 mutex_lock(&zr->resource_lock);
2087 vpict->hue = zr->hue;
2088 vpict->brightness = zr->brightness;
2089 vpict->contrast = zr->contrast;
2090 vpict->colour = zr->saturation;
2091 if (fh->overlay_settings.format) {
2092 vpict->depth = fh->overlay_settings.format->depth;
2093 vpict->palette = fh->overlay_settings.format->palette;
2094 } else {
2095 vpict->depth = 0;
2097 mutex_unlock(&zr->resource_lock);
2099 return 0;
2101 break;
2103 case VIDIOCSPICT:
2105 struct video_picture *vpict = arg;
2106 int i;
2108 dprintk(3,
2109 KERN_DEBUG
2110 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2111 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2112 vpict->colour, vpict->contrast, vpict->depth,
2113 vpict->palette);
2115 for (i = 0; i < NUM_FORMATS; i++) {
2116 const struct zoran_format *fmt = &zoran_formats[i];
2118 if (fmt->palette != -1 &&
2119 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2120 fmt->palette == vpict->palette &&
2121 fmt->depth == vpict->depth)
2122 break;
2124 if (i == NUM_FORMATS) {
2125 dprintk(1,
2126 KERN_ERR
2127 "%s: VIDIOCSPICT - Invalid palette %d\n",
2128 ZR_DEVNAME(zr), vpict->palette);
2129 return -EINVAL;
2132 mutex_lock(&zr->resource_lock);
2134 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2136 zr->hue = vpict->hue;
2137 zr->contrast = vpict->contrast;
2138 zr->saturation = vpict->colour;
2139 zr->brightness = vpict->brightness;
2141 fh->overlay_settings.format = &zoran_formats[i];
2143 mutex_unlock(&zr->resource_lock);
2145 return 0;
2147 break;
2149 case VIDIOCCAPTURE:
2151 int *on = arg, res;
2153 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2154 ZR_DEVNAME(zr), *on);
2156 mutex_lock(&zr->resource_lock);
2157 res = setup_overlay(file, *on);
2158 mutex_unlock(&zr->resource_lock);
2160 return res;
2162 break;
2164 case VIDIOCGWIN:
2166 struct video_window *vwin = arg;
2168 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2170 memset(vwin, 0, sizeof(struct video_window));
2171 mutex_lock(&zr->resource_lock);
2172 vwin->x = fh->overlay_settings.x;
2173 vwin->y = fh->overlay_settings.y;
2174 vwin->width = fh->overlay_settings.width;
2175 vwin->height = fh->overlay_settings.height;
2176 mutex_unlock(&zr->resource_lock);
2177 vwin->clipcount = 0;
2178 return 0;
2180 break;
2182 case VIDIOCSWIN:
2184 struct video_window *vwin = arg;
2185 int res;
2187 dprintk(3,
2188 KERN_DEBUG
2189 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2190 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2191 vwin->height, vwin->clipcount);
2193 mutex_lock(&zr->resource_lock);
2194 res =
2195 setup_window(file, vwin->x, vwin->y, vwin->width,
2196 vwin->height, vwin->clips,
2197 vwin->clipcount, NULL);
2198 mutex_unlock(&zr->resource_lock);
2200 return res;
2202 break;
2204 case VIDIOCGFBUF:
2206 struct video_buffer *vbuf = arg;
2208 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2210 mutex_lock(&zr->resource_lock);
2211 *vbuf = zr->buffer;
2212 mutex_unlock(&zr->resource_lock);
2213 return 0;
2215 break;
2217 case VIDIOCSFBUF:
2219 struct video_buffer *vbuf = arg;
2220 int i, res = 0;
2222 dprintk(3,
2223 KERN_DEBUG
2224 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2225 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2226 vbuf->height, vbuf->depth, vbuf->bytesperline);
2228 for (i = 0; i < NUM_FORMATS; i++)
2229 if (zoran_formats[i].depth == vbuf->depth)
2230 break;
2231 if (i == NUM_FORMATS) {
2232 dprintk(1,
2233 KERN_ERR
2234 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2235 ZR_DEVNAME(zr), vbuf->depth);
2236 return -EINVAL;
2239 mutex_lock(&zr->resource_lock);
2240 res =
2241 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2242 vbuf->width, vbuf->height,
2243 vbuf->bytesperline);
2244 mutex_unlock(&zr->resource_lock);
2246 return res;
2248 break;
2250 case VIDIOCSYNC:
2252 int *frame = arg, res;
2254 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2255 ZR_DEVNAME(zr), *frame);
2257 mutex_lock(&zr->resource_lock);
2258 res = v4l_sync(file, *frame);
2259 mutex_unlock(&zr->resource_lock);
2260 if (!res)
2261 zr->v4l_sync_tail++;
2262 return res;
2264 break;
2266 case VIDIOCMCAPTURE:
2268 struct video_mmap *vmap = arg;
2269 int res;
2271 dprintk(3,
2272 KERN_DEBUG
2273 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2274 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2275 vmap->format);
2277 mutex_lock(&zr->resource_lock);
2278 res = v4l_grab(file, vmap);
2279 mutex_unlock(&zr->resource_lock);
2280 return res;
2282 break;
2284 case VIDIOCGMBUF:
2286 struct video_mbuf *vmbuf = arg;
2287 int i, res = 0;
2289 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2291 vmbuf->size =
2292 fh->v4l_buffers.num_buffers *
2293 fh->v4l_buffers.buffer_size;
2294 vmbuf->frames = fh->v4l_buffers.num_buffers;
2295 for (i = 0; i < vmbuf->frames; i++) {
2296 vmbuf->offsets[i] =
2297 i * fh->v4l_buffers.buffer_size;
2300 mutex_lock(&zr->resource_lock);
2302 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2303 dprintk(1,
2304 KERN_ERR
2305 "%s: VIDIOCGMBUF - buffers already allocated\n",
2306 ZR_DEVNAME(zr));
2307 res = -EINVAL;
2308 goto v4l1reqbuf_unlock_and_return;
2311 if (v4l_fbuffer_alloc(file)) {
2312 res = -ENOMEM;
2313 goto v4l1reqbuf_unlock_and_return;
2316 /* The next mmap will map the V4L buffers */
2317 fh->map_mode = ZORAN_MAP_MODE_RAW;
2318 v4l1reqbuf_unlock_and_return:
2319 mutex_unlock(&zr->resource_lock);
2321 return res;
2323 break;
2325 case VIDIOCGUNIT:
2327 struct video_unit *vunit = arg;
2329 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2331 vunit->video = zr->video_dev->minor;
2332 vunit->vbi = VIDEO_NO_UNIT;
2333 vunit->radio = VIDEO_NO_UNIT;
2334 vunit->audio = VIDEO_NO_UNIT;
2335 vunit->teletext = VIDEO_NO_UNIT;
2337 return 0;
2339 break;
2342 * RJ: In principal we could support subcaptures for V4L grabbing.
2343 * Not even the famous BTTV driver has them, however.
2344 * If there should be a strong demand, one could consider
2345 * to implement them.
2347 case VIDIOCGCAPTURE:
2349 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2350 ZR_DEVNAME(zr));
2351 return -EINVAL;
2353 break;
2355 case VIDIOCSCAPTURE:
2357 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2358 ZR_DEVNAME(zr));
2359 return -EINVAL;
2361 break;
2363 case BUZIOC_G_PARAMS:
2365 struct zoran_params *bparams = arg;
2367 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2369 memset(bparams, 0, sizeof(struct zoran_params));
2370 bparams->major_version = MAJOR_VERSION;
2371 bparams->minor_version = MINOR_VERSION;
2373 mutex_lock(&zr->resource_lock);
2375 bparams->norm = zr->norm;
2376 bparams->input = zr->input;
2378 bparams->decimation = fh->jpg_settings.decimation;
2379 bparams->HorDcm = fh->jpg_settings.HorDcm;
2380 bparams->VerDcm = fh->jpg_settings.VerDcm;
2381 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2382 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2383 bparams->img_x = fh->jpg_settings.img_x;
2384 bparams->img_y = fh->jpg_settings.img_y;
2385 bparams->img_width = fh->jpg_settings.img_width;
2386 bparams->img_height = fh->jpg_settings.img_height;
2387 bparams->odd_even = fh->jpg_settings.odd_even;
2389 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2390 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2391 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2392 memcpy(bparams->APP_data,
2393 fh->jpg_settings.jpg_comp.APP_data,
2394 sizeof(bparams->APP_data));
2395 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2396 memcpy(bparams->COM_data,
2397 fh->jpg_settings.jpg_comp.COM_data,
2398 sizeof(bparams->COM_data));
2399 bparams->jpeg_markers =
2400 fh->jpg_settings.jpg_comp.jpeg_markers;
2402 mutex_unlock(&zr->resource_lock);
2404 bparams->VFIFO_FB = 0;
2406 return 0;
2408 break;
2410 case BUZIOC_S_PARAMS:
2412 struct zoran_params *bparams = arg;
2413 int res = 0;
2415 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2417 settings.decimation = bparams->decimation;
2418 settings.HorDcm = bparams->HorDcm;
2419 settings.VerDcm = bparams->VerDcm;
2420 settings.TmpDcm = bparams->TmpDcm;
2421 settings.field_per_buff = bparams->field_per_buff;
2422 settings.img_x = bparams->img_x;
2423 settings.img_y = bparams->img_y;
2424 settings.img_width = bparams->img_width;
2425 settings.img_height = bparams->img_height;
2426 settings.odd_even = bparams->odd_even;
2428 settings.jpg_comp.quality = bparams->quality;
2429 settings.jpg_comp.APPn = bparams->APPn;
2430 settings.jpg_comp.APP_len = bparams->APP_len;
2431 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2432 sizeof(bparams->APP_data));
2433 settings.jpg_comp.COM_len = bparams->COM_len;
2434 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2435 sizeof(bparams->COM_data));
2436 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2438 mutex_lock(&zr->resource_lock);
2440 if (zr->codec_mode != BUZ_MODE_IDLE) {
2441 dprintk(1,
2442 KERN_ERR
2443 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2444 ZR_DEVNAME(zr));
2445 res = -EINVAL;
2446 goto sparams_unlock_and_return;
2449 /* Check the params first before overwriting our
2450 * nternal values */
2451 if (zoran_check_jpg_settings(zr, &settings)) {
2452 res = -EINVAL;
2453 goto sparams_unlock_and_return;
2456 fh->jpg_settings = settings;
2457 sparams_unlock_and_return:
2458 mutex_unlock(&zr->resource_lock);
2460 return res;
2462 break;
2464 case BUZIOC_REQBUFS:
2466 struct zoran_requestbuffers *breq = arg;
2467 int res = 0;
2469 dprintk(3,
2470 KERN_DEBUG
2471 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2472 ZR_DEVNAME(zr), breq->count, breq->size);
2474 /* Enforce reasonable lower and upper limits */
2475 if (breq->count < 4)
2476 breq->count = 4; /* Could be choosen smaller */
2477 if (breq->count > jpg_nbufs)
2478 breq->count = jpg_nbufs;
2479 breq->size = PAGE_ALIGN(breq->size);
2480 if (breq->size < 8192)
2481 breq->size = 8192; /* Arbitrary */
2482 /* breq->size is limited by 1 page for the stat_com
2483 * tables to a Maximum of 2 MB */
2484 if (breq->size > jpg_bufsize)
2485 breq->size = jpg_bufsize;
2486 if (fh->jpg_buffers.need_contiguous &&
2487 breq->size > MAX_KMALLOC_MEM)
2488 breq->size = MAX_KMALLOC_MEM;
2490 mutex_lock(&zr->resource_lock);
2492 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2493 dprintk(1,
2494 KERN_ERR
2495 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2496 ZR_DEVNAME(zr));
2497 res = -EBUSY;
2498 goto jpgreqbuf_unlock_and_return;
2501 fh->jpg_buffers.num_buffers = breq->count;
2502 fh->jpg_buffers.buffer_size = breq->size;
2504 if (jpg_fbuffer_alloc(file)) {
2505 res = -ENOMEM;
2506 goto jpgreqbuf_unlock_and_return;
2509 /* The next mmap will map the MJPEG buffers - could
2510 * also be *_PLAY, but it doesn't matter here */
2511 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2512 jpgreqbuf_unlock_and_return:
2513 mutex_unlock(&zr->resource_lock);
2515 return res;
2517 break;
2519 case BUZIOC_QBUF_CAPT:
2521 int *frame = arg, res;
2523 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2524 ZR_DEVNAME(zr), *frame);
2526 mutex_lock(&zr->resource_lock);
2527 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2528 mutex_unlock(&zr->resource_lock);
2530 return res;
2532 break;
2534 case BUZIOC_QBUF_PLAY:
2536 int *frame = arg, res;
2538 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2539 ZR_DEVNAME(zr), *frame);
2541 mutex_lock(&zr->resource_lock);
2542 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2543 mutex_unlock(&zr->resource_lock);
2545 return res;
2547 break;
2549 case BUZIOC_SYNC:
2551 struct zoran_sync *bsync = arg;
2552 int res;
2554 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2556 mutex_lock(&zr->resource_lock);
2557 res = jpg_sync(file, bsync);
2558 mutex_unlock(&zr->resource_lock);
2560 return res;
2562 break;
2564 case BUZIOC_G_STATUS:
2566 struct zoran_status *bstat = arg;
2567 int norm, input, status, res = 0;
2569 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2571 if (zr->codec_mode != BUZ_MODE_IDLE) {
2572 dprintk(1,
2573 KERN_ERR
2574 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2575 ZR_DEVNAME(zr));
2576 return -EINVAL;
2579 input = zr->card.input[bstat->input].muxsel;
2580 norm = VIDEO_MODE_AUTO;
2582 mutex_lock(&zr->resource_lock);
2584 if (zr->codec_mode != BUZ_MODE_IDLE) {
2585 dprintk(1,
2586 KERN_ERR
2587 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2588 ZR_DEVNAME(zr));
2589 res = -EINVAL;
2590 goto gstat_unlock_and_return;
2593 decoder_command(zr, DECODER_SET_INPUT, &input);
2594 decoder_command(zr, DECODER_SET_NORM, &norm);
2596 /* sleep 1 second */
2597 ssleep(1);
2599 /* Get status of video decoder */
2600 decoder_command(zr, DECODER_GET_STATUS, &status);
2602 /* restore previous input and norm */
2603 input = zr->card.input[zr->input].muxsel;
2604 decoder_command(zr, DECODER_SET_INPUT, &input);
2605 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2606 gstat_unlock_and_return:
2607 mutex_unlock(&zr->resource_lock);
2609 if (!res) {
2610 bstat->signal =
2611 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2612 if (status & DECODER_STATUS_NTSC)
2613 bstat->norm = VIDEO_MODE_NTSC;
2614 else if (status & DECODER_STATUS_SECAM)
2615 bstat->norm = VIDEO_MODE_SECAM;
2616 else
2617 bstat->norm = VIDEO_MODE_PAL;
2619 bstat->color =
2620 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2623 return res;
2625 break;
2627 #ifdef CONFIG_VIDEO_V4L2
2629 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2630 * it allows for integrating the JPEG capturing calls inside standard v4l2
2633 case VIDIOC_QUERYCAP:
2635 struct v4l2_capability *cap = arg;
2637 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2639 memset(cap, 0, sizeof(*cap));
2640 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
2641 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
2642 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2643 pci_name(zr->pci_dev));
2644 cap->version =
2645 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2646 RELEASE_VERSION);
2647 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2649 return 0;
2651 break;
2653 case VIDIOC_ENUM_FMT:
2655 struct v4l2_fmtdesc *fmt = arg;
2656 int index = fmt->index, num = -1, i, flag = 0, type =
2657 fmt->type;
2659 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2660 ZR_DEVNAME(zr), fmt->index);
2662 switch (fmt->type) {
2663 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2664 flag = ZORAN_FORMAT_CAPTURE;
2665 break;
2666 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2667 flag = ZORAN_FORMAT_PLAYBACK;
2668 break;
2669 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2670 flag = ZORAN_FORMAT_OVERLAY;
2671 break;
2672 default:
2673 dprintk(1,
2674 KERN_ERR
2675 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2676 ZR_DEVNAME(zr), fmt->type);
2677 return -EINVAL;
2680 for (i = 0; i < NUM_FORMATS; i++) {
2681 if (zoran_formats[i].flags & flag)
2682 num++;
2683 if (num == fmt->index)
2684 break;
2686 if (fmt->index < 0 /* late, but not too late */ ||
2687 i == NUM_FORMATS)
2688 return -EINVAL;
2690 memset(fmt, 0, sizeof(*fmt));
2691 fmt->index = index;
2692 fmt->type = type;
2693 strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
2694 fmt->pixelformat = zoran_formats[i].fourcc;
2695 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2696 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2698 return 0;
2700 break;
2702 case VIDIOC_G_FMT:
2704 struct v4l2_format *fmt = arg;
2705 int type = fmt->type;
2707 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2709 memset(fmt, 0, sizeof(*fmt));
2710 fmt->type = type;
2712 switch (fmt->type) {
2713 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2715 mutex_lock(&zr->resource_lock);
2717 fmt->fmt.win.w.left = fh->overlay_settings.x;
2718 fmt->fmt.win.w.top = fh->overlay_settings.y;
2719 fmt->fmt.win.w.width = fh->overlay_settings.width;
2720 fmt->fmt.win.w.height =
2721 fh->overlay_settings.height;
2722 if (fh->overlay_settings.width * 2 >
2723 BUZ_MAX_HEIGHT)
2724 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2725 else
2726 fmt->fmt.win.field = V4L2_FIELD_TOP;
2728 mutex_unlock(&zr->resource_lock);
2730 break;
2732 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2733 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2735 mutex_lock(&zr->resource_lock);
2737 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2738 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2740 fmt->fmt.pix.width =
2741 fh->v4l_settings.width;
2742 fmt->fmt.pix.height =
2743 fh->v4l_settings.height;
2744 fmt->fmt.pix.sizeimage =
2745 fh->v4l_settings.bytesperline *
2746 fh->v4l_settings.height;
2747 fmt->fmt.pix.pixelformat =
2748 fh->v4l_settings.format->fourcc;
2749 fmt->fmt.pix.colorspace =
2750 fh->v4l_settings.format->colorspace;
2751 fmt->fmt.pix.bytesperline = 0;
2752 if (BUZ_MAX_HEIGHT <
2753 (fh->v4l_settings.height * 2))
2754 fmt->fmt.pix.field =
2755 V4L2_FIELD_INTERLACED;
2756 else
2757 fmt->fmt.pix.field =
2758 V4L2_FIELD_TOP;
2760 } else {
2762 fmt->fmt.pix.width =
2763 fh->jpg_settings.img_width /
2764 fh->jpg_settings.HorDcm;
2765 fmt->fmt.pix.height =
2766 fh->jpg_settings.img_height /
2767 (fh->jpg_settings.VerDcm *
2768 fh->jpg_settings.TmpDcm);
2769 fmt->fmt.pix.sizeimage =
2770 zoran_v4l2_calc_bufsize(&fh->
2771 jpg_settings);
2772 fmt->fmt.pix.pixelformat =
2773 V4L2_PIX_FMT_MJPEG;
2774 if (fh->jpg_settings.TmpDcm == 1)
2775 fmt->fmt.pix.field =
2776 (fh->jpg_settings.
2777 odd_even ? V4L2_FIELD_SEQ_BT :
2778 V4L2_FIELD_SEQ_BT);
2779 else
2780 fmt->fmt.pix.field =
2781 (fh->jpg_settings.
2782 odd_even ? V4L2_FIELD_TOP :
2783 V4L2_FIELD_BOTTOM);
2785 fmt->fmt.pix.bytesperline = 0;
2786 fmt->fmt.pix.colorspace =
2787 V4L2_COLORSPACE_SMPTE170M;
2790 mutex_unlock(&zr->resource_lock);
2792 break;
2794 default:
2795 dprintk(1,
2796 KERN_ERR
2797 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2798 ZR_DEVNAME(zr), fmt->type);
2799 return -EINVAL;
2801 return 0;
2803 break;
2805 case VIDIOC_S_FMT:
2807 struct v4l2_format *fmt = arg;
2808 int i, res = 0;
2809 __u32 printformat;
2811 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2812 ZR_DEVNAME(zr), fmt->type);
2814 switch (fmt->type) {
2815 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2817 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2818 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2819 fmt->fmt.win.w.width,
2820 fmt->fmt.win.w.height,
2821 fmt->fmt.win.clipcount,
2822 fmt->fmt.win.bitmap);
2823 mutex_lock(&zr->resource_lock);
2824 res =
2825 setup_window(file, fmt->fmt.win.w.left,
2826 fmt->fmt.win.w.top,
2827 fmt->fmt.win.w.width,
2828 fmt->fmt.win.w.height,
2829 (struct video_clip __user *)
2830 fmt->fmt.win.clips,
2831 fmt->fmt.win.clipcount,
2832 fmt->fmt.win.bitmap);
2833 mutex_unlock(&zr->resource_lock);
2834 return res;
2835 break;
2837 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2838 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2840 printformat =
2841 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2842 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2843 fmt->fmt.pix.width, fmt->fmt.pix.height,
2844 fmt->fmt.pix.pixelformat,
2845 (char *) &printformat);
2847 if (fmt->fmt.pix.bytesperline > 0) {
2848 dprintk(5,
2849 KERN_ERR "%s: bpl not supported\n",
2850 ZR_DEVNAME(zr));
2851 return -EINVAL;
2854 /* we can be requested to do JPEG/raw playback/capture */
2855 if (!
2856 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2857 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2858 fmt->fmt.pix.pixelformat ==
2859 V4L2_PIX_FMT_MJPEG))) {
2860 dprintk(1,
2861 KERN_ERR
2862 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2863 ZR_DEVNAME(zr), fmt->type,
2864 fmt->fmt.pix.pixelformat,
2865 (char *) &printformat);
2866 return -EINVAL;
2869 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2870 mutex_lock(&zr->resource_lock);
2872 settings = fh->jpg_settings;
2874 if (fh->v4l_buffers.allocated ||
2875 fh->jpg_buffers.allocated) {
2876 dprintk(1,
2877 KERN_ERR
2878 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2879 ZR_DEVNAME(zr));
2880 res = -EBUSY;
2881 goto sfmtjpg_unlock_and_return;
2884 /* we actually need to set 'real' parameters now */
2885 if ((fmt->fmt.pix.height * 2) >
2886 BUZ_MAX_HEIGHT)
2887 settings.TmpDcm = 1;
2888 else
2889 settings.TmpDcm = 2;
2890 settings.decimation = 0;
2891 if (fmt->fmt.pix.height <=
2892 fh->jpg_settings.img_height / 2)
2893 settings.VerDcm = 2;
2894 else
2895 settings.VerDcm = 1;
2896 if (fmt->fmt.pix.width <=
2897 fh->jpg_settings.img_width / 4)
2898 settings.HorDcm = 4;
2899 else if (fmt->fmt.pix.width <=
2900 fh->jpg_settings.img_width / 2)
2901 settings.HorDcm = 2;
2902 else
2903 settings.HorDcm = 1;
2904 if (settings.TmpDcm == 1)
2905 settings.field_per_buff = 2;
2906 else
2907 settings.field_per_buff = 1;
2909 /* check */
2910 if ((res =
2911 zoran_check_jpg_settings(zr,
2912 &settings)))
2913 goto sfmtjpg_unlock_and_return;
2915 /* it's ok, so set them */
2916 fh->jpg_settings = settings;
2918 /* tell the user what we actually did */
2919 fmt->fmt.pix.width =
2920 settings.img_width / settings.HorDcm;
2921 fmt->fmt.pix.height =
2922 settings.img_height * 2 /
2923 (settings.TmpDcm * settings.VerDcm);
2924 if (settings.TmpDcm == 1)
2925 fmt->fmt.pix.field =
2926 (fh->jpg_settings.
2927 odd_even ? V4L2_FIELD_SEQ_TB :
2928 V4L2_FIELD_SEQ_BT);
2929 else
2930 fmt->fmt.pix.field =
2931 (fh->jpg_settings.
2932 odd_even ? V4L2_FIELD_TOP :
2933 V4L2_FIELD_BOTTOM);
2934 fh->jpg_buffers.buffer_size =
2935 zoran_v4l2_calc_bufsize(&fh->
2936 jpg_settings);
2937 fmt->fmt.pix.sizeimage =
2938 fh->jpg_buffers.buffer_size;
2940 /* we hereby abuse this variable to show that
2941 * we're gonna do mjpeg capture */
2942 fh->map_mode =
2943 (fmt->type ==
2944 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2945 ZORAN_MAP_MODE_JPG_REC :
2946 ZORAN_MAP_MODE_JPG_PLAY;
2947 sfmtjpg_unlock_and_return:
2948 mutex_unlock(&zr->resource_lock);
2949 } else {
2950 for (i = 0; i < NUM_FORMATS; i++)
2951 if (fmt->fmt.pix.pixelformat ==
2952 zoran_formats[i].fourcc)
2953 break;
2954 if (i == NUM_FORMATS) {
2955 dprintk(1,
2956 KERN_ERR
2957 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
2958 ZR_DEVNAME(zr),
2959 fmt->fmt.pix.pixelformat,
2960 (char *) &printformat);
2961 return -EINVAL;
2963 mutex_lock(&zr->resource_lock);
2964 if (fh->jpg_buffers.allocated ||
2965 (fh->v4l_buffers.allocated &&
2966 fh->v4l_buffers.active !=
2967 ZORAN_FREE)) {
2968 dprintk(1,
2969 KERN_ERR
2970 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2971 ZR_DEVNAME(zr));
2972 res = -EBUSY;
2973 goto sfmtv4l_unlock_and_return;
2975 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2976 fmt->fmt.pix.height =
2977 BUZ_MAX_HEIGHT;
2978 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2979 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2981 if ((res =
2982 zoran_v4l_set_format(file,
2983 fmt->fmt.pix.
2984 width,
2985 fmt->fmt.pix.
2986 height,
2987 &zoran_formats
2988 [i])))
2989 goto sfmtv4l_unlock_and_return;
2991 /* tell the user the
2992 * results/missing stuff */
2993 fmt->fmt.pix.sizeimage =
2994 fh->v4l_settings.height *
2995 fh->v4l_settings.bytesperline;
2996 if (BUZ_MAX_HEIGHT <
2997 (fh->v4l_settings.height * 2))
2998 fmt->fmt.pix.field =
2999 V4L2_FIELD_INTERLACED;
3000 else
3001 fmt->fmt.pix.field =
3002 V4L2_FIELD_TOP;
3004 fh->map_mode = ZORAN_MAP_MODE_RAW;
3005 sfmtv4l_unlock_and_return:
3006 mutex_unlock(&zr->resource_lock);
3009 break;
3011 default:
3012 dprintk(3, "unsupported\n");
3013 dprintk(1,
3014 KERN_ERR
3015 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3016 ZR_DEVNAME(zr), fmt->type);
3017 return -EINVAL;
3020 return res;
3022 break;
3024 case VIDIOC_G_FBUF:
3026 struct v4l2_framebuffer *fb = arg;
3028 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3030 memset(fb, 0, sizeof(*fb));
3031 mutex_lock(&zr->resource_lock);
3032 fb->base = zr->buffer.base;
3033 fb->fmt.width = zr->buffer.width;
3034 fb->fmt.height = zr->buffer.height;
3035 if (zr->overlay_settings.format) {
3036 fb->fmt.pixelformat =
3037 fh->overlay_settings.format->fourcc;
3039 fb->fmt.bytesperline = zr->buffer.bytesperline;
3040 mutex_unlock(&zr->resource_lock);
3041 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3042 fb->fmt.field = V4L2_FIELD_INTERLACED;
3043 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3044 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3046 return 0;
3048 break;
3050 case VIDIOC_S_FBUF:
3052 int i, res = 0;
3053 struct v4l2_framebuffer *fb = arg;
3054 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3056 dprintk(3,
3057 KERN_DEBUG
3058 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3059 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3060 fb->fmt.bytesperline, fb->fmt.pixelformat,
3061 (char *) &printformat);
3063 for (i = 0; i < NUM_FORMATS; i++)
3064 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3065 break;
3066 if (i == NUM_FORMATS) {
3067 dprintk(1,
3068 KERN_ERR
3069 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3070 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3071 (char *) &printformat);
3072 return -EINVAL;
3075 mutex_lock(&zr->resource_lock);
3076 res =
3077 setup_fbuffer(file, fb->base, &zoran_formats[i],
3078 fb->fmt.width, fb->fmt.height,
3079 fb->fmt.bytesperline);
3080 mutex_unlock(&zr->resource_lock);
3082 return res;
3084 break;
3086 case VIDIOC_OVERLAY:
3088 int *on = arg, res;
3090 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3091 ZR_DEVNAME(zr), *on);
3093 mutex_lock(&zr->resource_lock);
3094 res = setup_overlay(file, *on);
3095 mutex_unlock(&zr->resource_lock);
3097 return res;
3099 break;
3101 case VIDIOC_REQBUFS:
3103 struct v4l2_requestbuffers *req = arg;
3104 int res = 0;
3106 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3107 ZR_DEVNAME(zr), req->type);
3109 if (req->memory != V4L2_MEMORY_MMAP) {
3110 dprintk(1,
3111 KERN_ERR
3112 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3113 ZR_DEVNAME(zr), req->memory);
3114 return -EINVAL;
3117 mutex_lock(&zr->resource_lock);
3119 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3120 dprintk(1,
3121 KERN_ERR
3122 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3123 ZR_DEVNAME(zr));
3124 res = -EBUSY;
3125 goto v4l2reqbuf_unlock_and_return;
3128 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3129 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3131 /* control user input */
3132 if (req->count < 2)
3133 req->count = 2;
3134 if (req->count > v4l_nbufs)
3135 req->count = v4l_nbufs;
3136 fh->v4l_buffers.num_buffers = req->count;
3138 if (v4l_fbuffer_alloc(file)) {
3139 res = -ENOMEM;
3140 goto v4l2reqbuf_unlock_and_return;
3143 /* The next mmap will map the V4L buffers */
3144 fh->map_mode = ZORAN_MAP_MODE_RAW;
3146 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3147 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3149 /* we need to calculate size ourselves now */
3150 if (req->count < 4)
3151 req->count = 4;
3152 if (req->count > jpg_nbufs)
3153 req->count = jpg_nbufs;
3154 fh->jpg_buffers.num_buffers = req->count;
3155 fh->jpg_buffers.buffer_size =
3156 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3158 if (jpg_fbuffer_alloc(file)) {
3159 res = -ENOMEM;
3160 goto v4l2reqbuf_unlock_and_return;
3163 /* The next mmap will map the MJPEG buffers */
3164 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3165 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3166 else
3167 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3169 } else {
3170 dprintk(1,
3171 KERN_ERR
3172 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3173 ZR_DEVNAME(zr), req->type);
3174 res = -EINVAL;
3175 goto v4l2reqbuf_unlock_and_return;
3177 v4l2reqbuf_unlock_and_return:
3178 mutex_unlock(&zr->resource_lock);
3180 return 0;
3182 break;
3184 case VIDIOC_QUERYBUF:
3186 struct v4l2_buffer *buf = arg;
3187 __u32 type = buf->type;
3188 int index = buf->index, res;
3190 dprintk(3,
3191 KERN_DEBUG
3192 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3193 ZR_DEVNAME(zr), buf->index, buf->type);
3195 memset(buf, 0, sizeof(*buf));
3196 buf->type = type;
3197 buf->index = index;
3199 mutex_lock(&zr->resource_lock);
3200 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3201 mutex_unlock(&zr->resource_lock);
3203 return res;
3205 break;
3207 case VIDIOC_QBUF:
3209 struct v4l2_buffer *buf = arg;
3210 int res = 0, codec_mode, buf_type;
3212 dprintk(3,
3213 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3214 ZR_DEVNAME(zr), buf->type, buf->index);
3216 mutex_lock(&zr->resource_lock);
3218 switch (fh->map_mode) {
3219 case ZORAN_MAP_MODE_RAW:
3220 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3221 dprintk(1,
3222 KERN_ERR
3223 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3224 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3225 res = -EINVAL;
3226 goto qbuf_unlock_and_return;
3229 res = zoran_v4l_queue_frame(file, buf->index);
3230 if (res)
3231 goto qbuf_unlock_and_return;
3232 if (!zr->v4l_memgrab_active &&
3233 fh->v4l_buffers.active == ZORAN_LOCKED)
3234 zr36057_set_memgrab(zr, 1);
3235 break;
3237 case ZORAN_MAP_MODE_JPG_REC:
3238 case ZORAN_MAP_MODE_JPG_PLAY:
3239 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3240 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3241 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3242 } else {
3243 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3244 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3247 if (buf->type != buf_type) {
3248 dprintk(1,
3249 KERN_ERR
3250 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3251 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3252 res = -EINVAL;
3253 goto qbuf_unlock_and_return;
3256 res =
3257 zoran_jpg_queue_frame(file, buf->index,
3258 codec_mode);
3259 if (res != 0)
3260 goto qbuf_unlock_and_return;
3261 if (zr->codec_mode == BUZ_MODE_IDLE &&
3262 fh->jpg_buffers.active == ZORAN_LOCKED) {
3263 zr36057_enable_jpg(zr, codec_mode);
3265 break;
3267 default:
3268 dprintk(1,
3269 KERN_ERR
3270 "%s: VIDIOC_QBUF - unsupported type %d\n",
3271 ZR_DEVNAME(zr), buf->type);
3272 res = -EINVAL;
3273 goto qbuf_unlock_and_return;
3275 qbuf_unlock_and_return:
3276 mutex_unlock(&zr->resource_lock);
3278 return res;
3280 break;
3282 case VIDIOC_DQBUF:
3284 struct v4l2_buffer *buf = arg;
3285 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3287 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3288 ZR_DEVNAME(zr), buf->type);
3290 mutex_lock(&zr->resource_lock);
3292 switch (fh->map_mode) {
3293 case ZORAN_MAP_MODE_RAW:
3294 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3295 dprintk(1,
3296 KERN_ERR
3297 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3298 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3299 res = -EINVAL;
3300 goto dqbuf_unlock_and_return;
3303 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3304 if (file->f_flags & O_NONBLOCK &&
3305 zr->v4l_buffers.buffer[num].state !=
3306 BUZ_STATE_DONE) {
3307 res = -EAGAIN;
3308 goto dqbuf_unlock_and_return;
3310 res = v4l_sync(file, num);
3311 if (res)
3312 goto dqbuf_unlock_and_return;
3313 else
3314 zr->v4l_sync_tail++;
3315 res = zoran_v4l2_buffer_status(file, buf, num);
3316 break;
3318 case ZORAN_MAP_MODE_JPG_REC:
3319 case ZORAN_MAP_MODE_JPG_PLAY:
3321 struct zoran_sync bs;
3323 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3324 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3325 else
3326 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3328 if (buf->type != buf_type) {
3329 dprintk(1,
3330 KERN_ERR
3331 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3332 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3333 res = -EINVAL;
3334 goto dqbuf_unlock_and_return;
3337 num =
3338 zr->jpg_pend[zr->
3339 jpg_que_tail & BUZ_MASK_FRAME];
3341 if (file->f_flags & O_NONBLOCK &&
3342 zr->jpg_buffers.buffer[num].state !=
3343 BUZ_STATE_DONE) {
3344 res = -EAGAIN;
3345 goto dqbuf_unlock_and_return;
3347 res = jpg_sync(file, &bs);
3348 if (res)
3349 goto dqbuf_unlock_and_return;
3350 res =
3351 zoran_v4l2_buffer_status(file, buf, bs.frame);
3352 break;
3355 default:
3356 dprintk(1,
3357 KERN_ERR
3358 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3359 ZR_DEVNAME(zr), buf->type);
3360 res = -EINVAL;
3361 goto dqbuf_unlock_and_return;
3363 dqbuf_unlock_and_return:
3364 mutex_unlock(&zr->resource_lock);
3366 return res;
3368 break;
3370 case VIDIOC_STREAMON:
3372 int res = 0;
3374 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3376 mutex_lock(&zr->resource_lock);
3378 switch (fh->map_mode) {
3379 case ZORAN_MAP_MODE_RAW: /* raw capture */
3380 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3381 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3382 res = -EBUSY;
3383 goto strmon_unlock_and_return;
3386 zr->v4l_buffers.active = fh->v4l_buffers.active =
3387 ZORAN_LOCKED;
3388 zr->v4l_settings = fh->v4l_settings;
3390 zr->v4l_sync_tail = zr->v4l_pend_tail;
3391 if (!zr->v4l_memgrab_active &&
3392 zr->v4l_pend_head != zr->v4l_pend_tail) {
3393 zr36057_set_memgrab(zr, 1);
3395 break;
3397 case ZORAN_MAP_MODE_JPG_REC:
3398 case ZORAN_MAP_MODE_JPG_PLAY:
3399 /* what is the codec mode right now? */
3400 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3401 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3402 res = -EBUSY;
3403 goto strmon_unlock_and_return;
3406 zr->jpg_buffers.active = fh->jpg_buffers.active =
3407 ZORAN_LOCKED;
3409 if (zr->jpg_que_head != zr->jpg_que_tail) {
3410 /* Start the jpeg codec when the first frame is queued */
3411 jpeg_start(zr);
3414 break;
3415 default:
3416 dprintk(1,
3417 KERN_ERR
3418 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3419 ZR_DEVNAME(zr), fh->map_mode);
3420 res = -EINVAL;
3421 goto strmon_unlock_and_return;
3423 strmon_unlock_and_return:
3424 mutex_unlock(&zr->resource_lock);
3426 return res;
3428 break;
3430 case VIDIOC_STREAMOFF:
3432 int i, res = 0;
3434 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3436 mutex_lock(&zr->resource_lock);
3438 switch (fh->map_mode) {
3439 case ZORAN_MAP_MODE_RAW: /* raw capture */
3440 if (fh->v4l_buffers.active == ZORAN_FREE &&
3441 zr->v4l_buffers.active != ZORAN_FREE) {
3442 res = -EPERM; /* stay off other's settings! */
3443 goto strmoff_unlock_and_return;
3445 if (zr->v4l_buffers.active == ZORAN_FREE)
3446 goto strmoff_unlock_and_return;
3448 /* unload capture */
3449 if (zr->v4l_memgrab_active) {
3450 long flags;
3452 spin_lock_irqsave(&zr->spinlock, flags);
3453 zr36057_set_memgrab(zr, 0);
3454 spin_unlock_irqrestore(&zr->spinlock, flags);
3457 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3458 zr->v4l_buffers.buffer[i].state =
3459 BUZ_STATE_USER;
3460 fh->v4l_buffers = zr->v4l_buffers;
3462 zr->v4l_buffers.active = fh->v4l_buffers.active =
3463 ZORAN_FREE;
3465 zr->v4l_grab_seq = 0;
3466 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3467 zr->v4l_sync_tail = 0;
3469 break;
3471 case ZORAN_MAP_MODE_JPG_REC:
3472 case ZORAN_MAP_MODE_JPG_PLAY:
3473 if (fh->jpg_buffers.active == ZORAN_FREE &&
3474 zr->jpg_buffers.active != ZORAN_FREE) {
3475 res = -EPERM; /* stay off other's settings! */
3476 goto strmoff_unlock_and_return;
3478 if (zr->jpg_buffers.active == ZORAN_FREE)
3479 goto strmoff_unlock_and_return;
3481 res =
3482 jpg_qbuf(file, -1,
3483 (fh->map_mode ==
3484 ZORAN_MAP_MODE_JPG_REC) ?
3485 BUZ_MODE_MOTION_COMPRESS :
3486 BUZ_MODE_MOTION_DECOMPRESS);
3487 if (res)
3488 goto strmoff_unlock_and_return;
3489 break;
3490 default:
3491 dprintk(1,
3492 KERN_ERR
3493 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3494 ZR_DEVNAME(zr), fh->map_mode);
3495 res = -EINVAL;
3496 goto strmoff_unlock_and_return;
3498 strmoff_unlock_and_return:
3499 mutex_unlock(&zr->resource_lock);
3501 return res;
3503 break;
3505 case VIDIOC_QUERYCTRL:
3507 struct v4l2_queryctrl *ctrl = arg;
3509 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3510 ZR_DEVNAME(zr), ctrl->id);
3512 /* we only support hue/saturation/contrast/brightness */
3513 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3514 ctrl->id > V4L2_CID_HUE)
3515 return -EINVAL;
3516 else {
3517 int id = ctrl->id;
3518 memset(ctrl, 0, sizeof(*ctrl));
3519 ctrl->id = id;
3522 switch (ctrl->id) {
3523 case V4L2_CID_BRIGHTNESS:
3524 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
3525 break;
3526 case V4L2_CID_CONTRAST:
3527 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
3528 break;
3529 case V4L2_CID_SATURATION:
3530 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
3531 break;
3532 case V4L2_CID_HUE:
3533 strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
3534 break;
3537 ctrl->minimum = 0;
3538 ctrl->maximum = 65535;
3539 ctrl->step = 1;
3540 ctrl->default_value = 32768;
3541 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3543 return 0;
3545 break;
3547 case VIDIOC_G_CTRL:
3549 struct v4l2_control *ctrl = arg;
3551 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3552 ZR_DEVNAME(zr), ctrl->id);
3554 /* we only support hue/saturation/contrast/brightness */
3555 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3556 ctrl->id > V4L2_CID_HUE)
3557 return -EINVAL;
3559 mutex_lock(&zr->resource_lock);
3560 switch (ctrl->id) {
3561 case V4L2_CID_BRIGHTNESS:
3562 ctrl->value = zr->brightness;
3563 break;
3564 case V4L2_CID_CONTRAST:
3565 ctrl->value = zr->contrast;
3566 break;
3567 case V4L2_CID_SATURATION:
3568 ctrl->value = zr->saturation;
3569 break;
3570 case V4L2_CID_HUE:
3571 ctrl->value = zr->hue;
3572 break;
3574 mutex_unlock(&zr->resource_lock);
3576 return 0;
3578 break;
3580 case VIDIOC_S_CTRL:
3582 struct v4l2_control *ctrl = arg;
3583 struct video_picture pict;
3585 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3586 ZR_DEVNAME(zr), ctrl->id);
3588 /* we only support hue/saturation/contrast/brightness */
3589 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3590 ctrl->id > V4L2_CID_HUE)
3591 return -EINVAL;
3593 if (ctrl->value < 0 || ctrl->value > 65535) {
3594 dprintk(1,
3595 KERN_ERR
3596 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3597 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3598 return -EINVAL;
3601 mutex_lock(&zr->resource_lock);
3602 switch (ctrl->id) {
3603 case V4L2_CID_BRIGHTNESS:
3604 zr->brightness = ctrl->value;
3605 break;
3606 case V4L2_CID_CONTRAST:
3607 zr->contrast = ctrl->value;
3608 break;
3609 case V4L2_CID_SATURATION:
3610 zr->saturation = ctrl->value;
3611 break;
3612 case V4L2_CID_HUE:
3613 zr->hue = ctrl->value;
3614 break;
3616 pict.brightness = zr->brightness;
3617 pict.contrast = zr->contrast;
3618 pict.colour = zr->saturation;
3619 pict.hue = zr->hue;
3621 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3623 mutex_unlock(&zr->resource_lock);
3625 return 0;
3627 break;
3629 case VIDIOC_ENUMSTD:
3631 struct v4l2_standard *std = arg;
3633 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3634 ZR_DEVNAME(zr), std->index);
3636 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3637 return -EINVAL;
3638 else {
3639 int id = std->index;
3640 memset(std, 0, sizeof(*std));
3641 std->index = id;
3644 if (std->index == zr->card.norms) {
3645 /* if we have autodetect, ... */
3646 struct video_decoder_capability caps;
3647 decoder_command(zr, DECODER_GET_CAPABILITIES,
3648 &caps);
3649 if (caps.flags & VIDEO_DECODER_AUTO) {
3650 std->id = V4L2_STD_ALL;
3651 strncpy(std->name, "Autodetect", sizeof(std->name)-1);
3652 return 0;
3653 } else
3654 return -EINVAL;
3656 switch (std->index) {
3657 case 0:
3658 std->id = V4L2_STD_PAL;
3659 strncpy(std->name, "PAL", sizeof(std->name)-1);
3660 std->frameperiod.numerator = 1;
3661 std->frameperiod.denominator = 25;
3662 std->framelines = zr->card.tvn[0]->Ht;
3663 break;
3664 case 1:
3665 std->id = V4L2_STD_NTSC;
3666 strncpy(std->name, "NTSC", sizeof(std->name)-1);
3667 std->frameperiod.numerator = 1001;
3668 std->frameperiod.denominator = 30000;
3669 std->framelines = zr->card.tvn[1]->Ht;
3670 break;
3671 case 2:
3672 std->id = V4L2_STD_SECAM;
3673 strncpy(std->name, "SECAM", sizeof(std->name)-1);
3674 std->frameperiod.numerator = 1;
3675 std->frameperiod.denominator = 25;
3676 std->framelines = zr->card.tvn[2]->Ht;
3677 break;
3680 return 0;
3682 break;
3684 case VIDIOC_G_STD:
3686 v4l2_std_id *std = arg;
3687 int norm;
3689 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3691 mutex_lock(&zr->resource_lock);
3692 norm = zr->norm;
3693 mutex_unlock(&zr->resource_lock);
3695 switch (norm) {
3696 case VIDEO_MODE_PAL:
3697 *std = V4L2_STD_PAL;
3698 break;
3699 case VIDEO_MODE_NTSC:
3700 *std = V4L2_STD_NTSC;
3701 break;
3702 case VIDEO_MODE_SECAM:
3703 *std = V4L2_STD_SECAM;
3704 break;
3707 return 0;
3709 break;
3711 case VIDIOC_S_STD:
3713 int norm = -1, res = 0;
3714 v4l2_std_id *std = arg;
3716 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3717 ZR_DEVNAME(zr), (unsigned long long)*std);
3719 if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
3720 norm = VIDEO_MODE_PAL;
3721 else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
3722 norm = VIDEO_MODE_NTSC;
3723 else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
3724 norm = VIDEO_MODE_SECAM;
3725 else if (*std == V4L2_STD_ALL)
3726 norm = VIDEO_MODE_AUTO;
3727 else {
3728 dprintk(1,
3729 KERN_ERR
3730 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3731 ZR_DEVNAME(zr), (unsigned long long)*std);
3732 return -EINVAL;
3735 mutex_lock(&zr->resource_lock);
3736 if ((res = zoran_set_norm(zr, norm)))
3737 goto sstd_unlock_and_return;
3739 res = wait_grab_pending(zr);
3740 sstd_unlock_and_return:
3741 mutex_unlock(&zr->resource_lock);
3742 return res;
3744 break;
3746 case VIDIOC_ENUMINPUT:
3748 struct v4l2_input *inp = arg;
3749 int status;
3751 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3752 ZR_DEVNAME(zr), inp->index);
3754 if (inp->index < 0 || inp->index >= zr->card.inputs)
3755 return -EINVAL;
3756 else {
3757 int id = inp->index;
3758 memset(inp, 0, sizeof(*inp));
3759 inp->index = id;
3762 strncpy(inp->name, zr->card.input[inp->index].name,
3763 sizeof(inp->name) - 1);
3764 inp->type = V4L2_INPUT_TYPE_CAMERA;
3765 inp->std = V4L2_STD_ALL;
3767 /* Get status of video decoder */
3768 mutex_lock(&zr->resource_lock);
3769 decoder_command(zr, DECODER_GET_STATUS, &status);
3770 mutex_unlock(&zr->resource_lock);
3772 if (!(status & DECODER_STATUS_GOOD)) {
3773 inp->status |= V4L2_IN_ST_NO_POWER;
3774 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3776 if (!(status & DECODER_STATUS_COLOR))
3777 inp->status |= V4L2_IN_ST_NO_COLOR;
3779 return 0;
3781 break;
3783 case VIDIOC_G_INPUT:
3785 int *input = arg;
3787 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3789 mutex_lock(&zr->resource_lock);
3790 *input = zr->input;
3791 mutex_unlock(&zr->resource_lock);
3793 return 0;
3795 break;
3797 case VIDIOC_S_INPUT:
3799 int *input = arg, res = 0;
3801 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3802 ZR_DEVNAME(zr), *input);
3804 mutex_lock(&zr->resource_lock);
3805 if ((res = zoran_set_input(zr, *input)))
3806 goto sinput_unlock_and_return;
3808 /* Make sure the changes come into effect */
3809 res = wait_grab_pending(zr);
3810 sinput_unlock_and_return:
3811 mutex_unlock(&zr->resource_lock);
3812 return res;
3814 break;
3816 case VIDIOC_ENUMOUTPUT:
3818 struct v4l2_output *outp = arg;
3820 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3821 ZR_DEVNAME(zr), outp->index);
3823 if (outp->index != 0)
3824 return -EINVAL;
3826 memset(outp, 0, sizeof(*outp));
3827 outp->index = 0;
3828 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3829 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
3831 return 0;
3833 break;
3835 case VIDIOC_G_OUTPUT:
3837 int *output = arg;
3839 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3841 *output = 0;
3843 return 0;
3845 break;
3847 case VIDIOC_S_OUTPUT:
3849 int *output = arg;
3851 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3852 ZR_DEVNAME(zr), *output);
3854 if (*output != 0)
3855 return -EINVAL;
3857 return 0;
3859 break;
3861 /* cropping (sub-frame capture) */
3862 case VIDIOC_CROPCAP:
3864 struct v4l2_cropcap *cropcap = arg;
3865 int type = cropcap->type, res = 0;
3867 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3868 ZR_DEVNAME(zr), cropcap->type);
3870 memset(cropcap, 0, sizeof(*cropcap));
3871 cropcap->type = type;
3873 mutex_lock(&zr->resource_lock);
3875 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3876 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3877 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3878 dprintk(1,
3879 KERN_ERR
3880 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3881 ZR_DEVNAME(zr));
3882 res = -EINVAL;
3883 goto cropcap_unlock_and_return;
3886 cropcap->bounds.top = cropcap->bounds.left = 0;
3887 cropcap->bounds.width = BUZ_MAX_WIDTH;
3888 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3889 cropcap->defrect.top = cropcap->defrect.left = 0;
3890 cropcap->defrect.width = BUZ_MIN_WIDTH;
3891 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3892 cropcap_unlock_and_return:
3893 mutex_unlock(&zr->resource_lock);
3894 return res;
3896 break;
3898 case VIDIOC_G_CROP:
3900 struct v4l2_crop *crop = arg;
3901 int type = crop->type, res = 0;
3903 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3904 ZR_DEVNAME(zr), crop->type);
3906 memset(crop, 0, sizeof(*crop));
3907 crop->type = type;
3909 mutex_lock(&zr->resource_lock);
3911 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3912 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3913 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3914 dprintk(1,
3915 KERN_ERR
3916 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3917 ZR_DEVNAME(zr));
3918 res = -EINVAL;
3919 goto gcrop_unlock_and_return;
3922 crop->c.top = fh->jpg_settings.img_y;
3923 crop->c.left = fh->jpg_settings.img_x;
3924 crop->c.width = fh->jpg_settings.img_width;
3925 crop->c.height = fh->jpg_settings.img_height;
3927 gcrop_unlock_and_return:
3928 mutex_unlock(&zr->resource_lock);
3930 return res;
3932 break;
3934 case VIDIOC_S_CROP:
3936 struct v4l2_crop *crop = arg;
3937 int res = 0;
3939 settings = fh->jpg_settings;
3941 dprintk(3,
3942 KERN_ERR
3943 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3944 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3945 crop->c.width, crop->c.height);
3947 mutex_lock(&zr->resource_lock);
3949 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3950 dprintk(1,
3951 KERN_ERR
3952 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3953 ZR_DEVNAME(zr));
3954 res = -EBUSY;
3955 goto scrop_unlock_and_return;
3958 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3959 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3960 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3961 dprintk(1,
3962 KERN_ERR
3963 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3964 ZR_DEVNAME(zr));
3965 res = -EINVAL;
3966 goto scrop_unlock_and_return;
3969 /* move into a form that we understand */
3970 settings.img_x = crop->c.left;
3971 settings.img_y = crop->c.top;
3972 settings.img_width = crop->c.width;
3973 settings.img_height = crop->c.height;
3975 /* check validity */
3976 if ((res = zoran_check_jpg_settings(zr, &settings)))
3977 goto scrop_unlock_and_return;
3979 /* accept */
3980 fh->jpg_settings = settings;
3982 scrop_unlock_and_return:
3983 mutex_unlock(&zr->resource_lock);
3984 return res;
3986 break;
3988 case VIDIOC_G_JPEGCOMP:
3990 struct v4l2_jpegcompression *params = arg;
3992 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
3993 ZR_DEVNAME(zr));
3995 memset(params, 0, sizeof(*params));
3997 mutex_lock(&zr->resource_lock);
3999 params->quality = fh->jpg_settings.jpg_comp.quality;
4000 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4001 memcpy(params->APP_data,
4002 fh->jpg_settings.jpg_comp.APP_data,
4003 fh->jpg_settings.jpg_comp.APP_len);
4004 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4005 memcpy(params->COM_data,
4006 fh->jpg_settings.jpg_comp.COM_data,
4007 fh->jpg_settings.jpg_comp.COM_len);
4008 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4009 params->jpeg_markers =
4010 fh->jpg_settings.jpg_comp.jpeg_markers;
4012 mutex_unlock(&zr->resource_lock);
4014 return 0;
4016 break;
4018 case VIDIOC_S_JPEGCOMP:
4020 struct v4l2_jpegcompression *params = arg;
4021 int res = 0;
4023 settings = fh->jpg_settings;
4025 dprintk(3,
4026 KERN_DEBUG
4027 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4028 ZR_DEVNAME(zr), params->quality, params->APPn,
4029 params->APP_len, params->COM_len);
4031 settings.jpg_comp = *params;
4033 mutex_lock(&zr->resource_lock);
4035 if (fh->v4l_buffers.active != ZORAN_FREE ||
4036 fh->jpg_buffers.active != ZORAN_FREE) {
4037 dprintk(1,
4038 KERN_WARNING
4039 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4040 ZR_DEVNAME(zr));
4041 res = -EBUSY;
4042 goto sjpegc_unlock_and_return;
4045 if ((res = zoran_check_jpg_settings(zr, &settings)))
4046 goto sjpegc_unlock_and_return;
4047 if (!fh->jpg_buffers.allocated)
4048 fh->jpg_buffers.buffer_size =
4049 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4050 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4051 sjpegc_unlock_and_return:
4052 mutex_unlock(&zr->resource_lock);
4054 return 0;
4056 break;
4058 case VIDIOC_QUERYSTD: /* why is this useful? */
4060 v4l2_std_id *std = arg;
4062 dprintk(3,
4063 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4064 ZR_DEVNAME(zr), (unsigned long long)*std);
4066 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4067 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4068 zr->card.norms == 3)) {
4069 return 0;
4072 return -EINVAL;
4074 break;
4076 case VIDIOC_TRY_FMT:
4078 struct v4l2_format *fmt = arg;
4079 int res = 0;
4081 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4082 ZR_DEVNAME(zr), fmt->type);
4084 switch (fmt->type) {
4085 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4086 mutex_lock(&zr->resource_lock);
4088 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4089 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4090 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4091 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4092 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4093 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4094 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4095 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4097 mutex_unlock(&zr->resource_lock);
4098 break;
4100 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4101 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4102 if (fmt->fmt.pix.bytesperline > 0)
4103 return -EINVAL;
4105 mutex_lock(&zr->resource_lock);
4107 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4108 settings = fh->jpg_settings;
4110 /* we actually need to set 'real' parameters now */
4111 if ((fmt->fmt.pix.height * 2) >
4112 BUZ_MAX_HEIGHT)
4113 settings.TmpDcm = 1;
4114 else
4115 settings.TmpDcm = 2;
4116 settings.decimation = 0;
4117 if (fmt->fmt.pix.height <=
4118 fh->jpg_settings.img_height / 2)
4119 settings.VerDcm = 2;
4120 else
4121 settings.VerDcm = 1;
4122 if (fmt->fmt.pix.width <=
4123 fh->jpg_settings.img_width / 4)
4124 settings.HorDcm = 4;
4125 else if (fmt->fmt.pix.width <=
4126 fh->jpg_settings.img_width / 2)
4127 settings.HorDcm = 2;
4128 else
4129 settings.HorDcm = 1;
4130 if (settings.TmpDcm == 1)
4131 settings.field_per_buff = 2;
4132 else
4133 settings.field_per_buff = 1;
4135 /* check */
4136 if ((res =
4137 zoran_check_jpg_settings(zr,
4138 &settings)))
4139 goto tryfmt_unlock_and_return;
4141 /* tell the user what we actually did */
4142 fmt->fmt.pix.width =
4143 settings.img_width / settings.HorDcm;
4144 fmt->fmt.pix.height =
4145 settings.img_height * 2 /
4146 (settings.TmpDcm * settings.VerDcm);
4147 if (settings.TmpDcm == 1)
4148 fmt->fmt.pix.field =
4149 (fh->jpg_settings.
4150 odd_even ? V4L2_FIELD_SEQ_TB :
4151 V4L2_FIELD_SEQ_BT);
4152 else
4153 fmt->fmt.pix.field =
4154 (fh->jpg_settings.
4155 odd_even ? V4L2_FIELD_TOP :
4156 V4L2_FIELD_BOTTOM);
4158 fmt->fmt.pix.sizeimage =
4159 zoran_v4l2_calc_bufsize(&settings);
4160 } else if (fmt->type ==
4161 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4162 int i;
4164 for (i = 0; i < NUM_FORMATS; i++)
4165 if (zoran_formats[i].fourcc ==
4166 fmt->fmt.pix.pixelformat)
4167 break;
4168 if (i == NUM_FORMATS) {
4169 res = -EINVAL;
4170 goto tryfmt_unlock_and_return;
4173 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4174 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4175 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4176 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4177 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4178 fmt->fmt.pix.height =
4179 BUZ_MAX_HEIGHT;
4180 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4181 fmt->fmt.pix.height =
4182 BUZ_MIN_HEIGHT;
4183 } else {
4184 res = -EINVAL;
4185 goto tryfmt_unlock_and_return;
4187 tryfmt_unlock_and_return:
4188 mutex_unlock(&zr->resource_lock);
4190 return res;
4191 break;
4193 default:
4194 return -EINVAL;
4197 return 0;
4199 break;
4200 #endif
4202 default:
4203 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4204 ZR_DEVNAME(zr), cmd);
4205 return -ENOIOCTLCMD;
4206 break;
4209 return 0;
4213 static int
4214 zoran_ioctl (struct inode *inode,
4215 struct file *file,
4216 unsigned int cmd,
4217 unsigned long arg)
4219 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4222 static unsigned int
4223 zoran_poll (struct file *file,
4224 poll_table *wait)
4226 struct zoran_fh *fh = file->private_data;
4227 struct zoran *zr = fh->zr;
4228 int res = 0, frame;
4229 unsigned long flags;
4231 /* we should check whether buffers are ready to be synced on
4232 * (w/o waits - O_NONBLOCK) here
4233 * if ready for read (sync), return POLLIN|POLLRDNORM,
4234 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4235 * if error, return POLLERR,
4236 * if no buffers queued or so, return POLLNVAL
4239 mutex_lock(&zr->resource_lock);
4241 switch (fh->map_mode) {
4242 case ZORAN_MAP_MODE_RAW:
4243 poll_wait(file, &zr->v4l_capq, wait);
4244 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
4246 spin_lock_irqsave(&zr->spinlock, flags);
4247 dprintk(3,
4248 KERN_DEBUG
4249 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4250 ZR_DEVNAME(zr), __FUNCTION__,
4251 "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
4252 "UPMD"[zr->v4l_buffers.buffer[frame].state],
4253 zr->v4l_pend_tail, zr->v4l_pend_head);
4254 /* Process is the one capturing? */
4255 if (fh->v4l_buffers.active != ZORAN_FREE &&
4256 /* Buffer ready to DQBUF? */
4257 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4258 res = POLLIN | POLLRDNORM;
4259 spin_unlock_irqrestore(&zr->spinlock, flags);
4261 break;
4263 case ZORAN_MAP_MODE_JPG_REC:
4264 case ZORAN_MAP_MODE_JPG_PLAY:
4265 poll_wait(file, &zr->jpg_capq, wait);
4266 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4268 spin_lock_irqsave(&zr->spinlock, flags);
4269 dprintk(3,
4270 KERN_DEBUG
4271 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4272 ZR_DEVNAME(zr), __FUNCTION__,
4273 "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
4274 "UPMD"[zr->jpg_buffers.buffer[frame].state],
4275 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
4276 if (fh->jpg_buffers.active != ZORAN_FREE &&
4277 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4278 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4279 res = POLLIN | POLLRDNORM;
4280 else
4281 res = POLLOUT | POLLWRNORM;
4283 spin_unlock_irqrestore(&zr->spinlock, flags);
4285 break;
4287 default:
4288 dprintk(1,
4289 KERN_ERR
4290 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4291 ZR_DEVNAME(zr), fh->map_mode);
4292 res = POLLNVAL;
4295 mutex_unlock(&zr->resource_lock);
4297 return res;
4302 * This maps the buffers to user space.
4304 * Depending on the state of fh->map_mode
4305 * the V4L or the MJPEG buffers are mapped
4306 * per buffer or all together
4308 * Note that we need to connect to some
4309 * unmap signal event to unmap the de-allocate
4310 * the buffer accordingly (zoran_vm_close())
4313 static void
4314 zoran_vm_open (struct vm_area_struct *vma)
4316 struct zoran_mapping *map = vma->vm_private_data;
4318 map->count++;
4321 static void
4322 zoran_vm_close (struct vm_area_struct *vma)
4324 struct zoran_mapping *map = vma->vm_private_data;
4325 struct file *file = map->file;
4326 struct zoran_fh *fh = file->private_data;
4327 struct zoran *zr = fh->zr;
4328 int i;
4330 map->count--;
4331 if (map->count == 0) {
4332 switch (fh->map_mode) {
4333 case ZORAN_MAP_MODE_JPG_REC:
4334 case ZORAN_MAP_MODE_JPG_PLAY:
4336 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4337 ZR_DEVNAME(zr));
4339 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4340 if (fh->jpg_buffers.buffer[i].map == map) {
4341 fh->jpg_buffers.buffer[i].map =
4342 NULL;
4345 kfree(map);
4347 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4348 if (fh->jpg_buffers.buffer[i].map)
4349 break;
4350 if (i == fh->jpg_buffers.num_buffers) {
4351 mutex_lock(&zr->resource_lock);
4353 if (fh->jpg_buffers.active != ZORAN_FREE) {
4354 jpg_qbuf(file, -1, zr->codec_mode);
4355 zr->jpg_buffers.allocated = 0;
4356 zr->jpg_buffers.active =
4357 fh->jpg_buffers.active =
4358 ZORAN_FREE;
4360 //jpg_fbuffer_free(file);
4361 fh->jpg_buffers.allocated = 0;
4362 fh->jpg_buffers.ready_to_be_freed = 1;
4364 mutex_unlock(&zr->resource_lock);
4367 break;
4369 case ZORAN_MAP_MODE_RAW:
4371 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4372 ZR_DEVNAME(zr));
4374 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4375 if (fh->v4l_buffers.buffer[i].map == map) {
4376 /* unqueue/unmap */
4377 fh->v4l_buffers.buffer[i].map =
4378 NULL;
4381 kfree(map);
4383 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4384 if (fh->v4l_buffers.buffer[i].map)
4385 break;
4386 if (i == fh->v4l_buffers.num_buffers) {
4387 mutex_lock(&zr->resource_lock);
4389 if (fh->v4l_buffers.active != ZORAN_FREE) {
4390 long flags;
4392 spin_lock_irqsave(&zr->spinlock, flags);
4393 zr36057_set_memgrab(zr, 0);
4394 zr->v4l_buffers.allocated = 0;
4395 zr->v4l_buffers.active =
4396 fh->v4l_buffers.active =
4397 ZORAN_FREE;
4398 spin_unlock_irqrestore(&zr->spinlock, flags);
4400 //v4l_fbuffer_free(file);
4401 fh->v4l_buffers.allocated = 0;
4402 fh->v4l_buffers.ready_to_be_freed = 1;
4404 mutex_unlock(&zr->resource_lock);
4407 break;
4409 default:
4410 printk(KERN_ERR
4411 "%s: munmap() - internal error - unknown map mode %d\n",
4412 ZR_DEVNAME(zr), fh->map_mode);
4413 break;
4419 static struct vm_operations_struct zoran_vm_ops = {
4420 .open = zoran_vm_open,
4421 .close = zoran_vm_close,
4424 static int
4425 zoran_mmap (struct file *file,
4426 struct vm_area_struct *vma)
4428 struct zoran_fh *fh = file->private_data;
4429 struct zoran *zr = fh->zr;
4430 unsigned long size = (vma->vm_end - vma->vm_start);
4431 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4432 int i, j;
4433 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4434 int first, last;
4435 struct zoran_mapping *map;
4436 int res = 0;
4438 dprintk(3,
4439 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4440 ZR_DEVNAME(zr),
4441 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4442 vma->vm_start, vma->vm_end, size);
4444 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4445 !(vma->vm_flags & VM_WRITE)) {
4446 dprintk(1,
4447 KERN_ERR
4448 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4449 ZR_DEVNAME(zr));
4450 return -EINVAL;
4453 switch (fh->map_mode) {
4455 case ZORAN_MAP_MODE_JPG_REC:
4456 case ZORAN_MAP_MODE_JPG_PLAY:
4458 /* lock */
4459 mutex_lock(&zr->resource_lock);
4461 /* Map the MJPEG buffers */
4462 if (!fh->jpg_buffers.allocated) {
4463 dprintk(1,
4464 KERN_ERR
4465 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4466 ZR_DEVNAME(zr));
4467 res = -ENOMEM;
4468 goto jpg_mmap_unlock_and_return;
4471 first = offset / fh->jpg_buffers.buffer_size;
4472 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4473 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4474 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4475 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4476 last >= fh->jpg_buffers.num_buffers) {
4477 dprintk(1,
4478 KERN_ERR
4479 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4480 ZR_DEVNAME(zr), offset, size,
4481 fh->jpg_buffers.buffer_size,
4482 fh->jpg_buffers.num_buffers);
4483 res = -EINVAL;
4484 goto jpg_mmap_unlock_and_return;
4486 for (i = first; i <= last; i++) {
4487 if (fh->jpg_buffers.buffer[i].map) {
4488 dprintk(1,
4489 KERN_ERR
4490 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4491 ZR_DEVNAME(zr), i);
4492 res = -EBUSY;
4493 goto jpg_mmap_unlock_and_return;
4497 /* map these buffers (v4l_buffers[i]) */
4498 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4499 if (!map) {
4500 res = -ENOMEM;
4501 goto jpg_mmap_unlock_and_return;
4503 map->file = file;
4504 map->count = 1;
4506 vma->vm_ops = &zoran_vm_ops;
4507 vma->vm_flags |= VM_DONTEXPAND;
4508 vma->vm_private_data = map;
4510 for (i = first; i <= last; i++) {
4511 for (j = 0;
4512 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4513 j++) {
4514 fraglen =
4515 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4516 frag_tab[2 * j + 1]) & ~1) << 1;
4517 todo = size;
4518 if (todo > fraglen)
4519 todo = fraglen;
4520 pos =
4521 le32_to_cpu((unsigned long) fh->jpg_buffers.
4522 buffer[i].frag_tab[2 * j]);
4523 /* should just be pos on i386 */
4524 page = virt_to_phys(bus_to_virt(pos))
4525 >> PAGE_SHIFT;
4526 if (remap_pfn_range(vma, start, page,
4527 todo, PAGE_SHARED)) {
4528 dprintk(1,
4529 KERN_ERR
4530 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4531 ZR_DEVNAME(zr));
4532 res = -EAGAIN;
4533 goto jpg_mmap_unlock_and_return;
4535 size -= todo;
4536 start += todo;
4537 if (size == 0)
4538 break;
4539 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4540 frag_tab[2 * j + 1]) & 1)
4541 break; /* was last fragment */
4543 fh->jpg_buffers.buffer[i].map = map;
4544 if (size == 0)
4545 break;
4548 jpg_mmap_unlock_and_return:
4549 mutex_unlock(&zr->resource_lock);
4551 break;
4553 case ZORAN_MAP_MODE_RAW:
4555 mutex_lock(&zr->resource_lock);
4557 /* Map the V4L buffers */
4558 if (!fh->v4l_buffers.allocated) {
4559 dprintk(1,
4560 KERN_ERR
4561 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4562 ZR_DEVNAME(zr));
4563 res = -ENOMEM;
4564 goto v4l_mmap_unlock_and_return;
4567 first = offset / fh->v4l_buffers.buffer_size;
4568 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4569 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4570 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4571 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4572 last >= fh->v4l_buffers.buffer_size) {
4573 dprintk(1,
4574 KERN_ERR
4575 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4576 ZR_DEVNAME(zr), offset, size,
4577 fh->v4l_buffers.buffer_size,
4578 fh->v4l_buffers.num_buffers);
4579 res = -EINVAL;
4580 goto v4l_mmap_unlock_and_return;
4582 for (i = first; i <= last; i++) {
4583 if (fh->v4l_buffers.buffer[i].map) {
4584 dprintk(1,
4585 KERN_ERR
4586 "%s: mmap(V4L) - buffer %d already mapped\n",
4587 ZR_DEVNAME(zr), i);
4588 res = -EBUSY;
4589 goto v4l_mmap_unlock_and_return;
4593 /* map these buffers (v4l_buffers[i]) */
4594 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4595 if (!map) {
4596 res = -ENOMEM;
4597 goto v4l_mmap_unlock_and_return;
4599 map->file = file;
4600 map->count = 1;
4602 vma->vm_ops = &zoran_vm_ops;
4603 vma->vm_flags |= VM_DONTEXPAND;
4604 vma->vm_private_data = map;
4606 for (i = first; i <= last; i++) {
4607 todo = size;
4608 if (todo > fh->v4l_buffers.buffer_size)
4609 todo = fh->v4l_buffers.buffer_size;
4610 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4611 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4612 todo, PAGE_SHARED)) {
4613 dprintk(1,
4614 KERN_ERR
4615 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4616 ZR_DEVNAME(zr));
4617 res = -EAGAIN;
4618 goto v4l_mmap_unlock_and_return;
4620 size -= todo;
4621 start += todo;
4622 fh->v4l_buffers.buffer[i].map = map;
4623 if (size == 0)
4624 break;
4626 v4l_mmap_unlock_and_return:
4627 mutex_unlock(&zr->resource_lock);
4629 break;
4631 default:
4632 dprintk(1,
4633 KERN_ERR
4634 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4635 ZR_DEVNAME(zr), fh->map_mode);
4636 break;
4639 return 0;
4642 static const struct file_operations zoran_fops = {
4643 .owner = THIS_MODULE,
4644 .open = zoran_open,
4645 .release = zoran_close,
4646 .ioctl = zoran_ioctl,
4647 .compat_ioctl = v4l_compat_ioctl32,
4648 .llseek = no_llseek,
4649 .read = zoran_read,
4650 .write = zoran_write,
4651 .mmap = zoran_mmap,
4652 .poll = zoran_poll,
4655 struct video_device zoran_template __devinitdata = {
4656 .name = ZORAN_NAME,
4657 .type = ZORAN_VID_TYPE,
4658 #ifdef CONFIG_VIDEO_V4L2
4659 .type2 = ZORAN_V4L2_VID_FLAGS,
4660 #endif
4661 .fops = &zoran_fops,
4662 .release = &zoran_vdev_release,
4663 .minor = -1