blob: ba838a42ec806c7e8fe1d55abb2ac1626585a28a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 *
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9 *
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11 *
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13 *
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 *
16 * Based on
17 *
18 * Miro DC10 driver
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20 *
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23 *
24 * buz.0.0.3
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26 *
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
30 *
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 */
46
47#include <linux/config.h>
48#include <linux/version.h>
49#include <linux/init.h>
50#include <linux/module.h>
51#include <linux/delay.h>
52#include <linux/slab.h>
53#include <linux/pci.h>
54#include <linux/vmalloc.h>
55#include <linux/wait.h>
56#include <linux/byteorder/generic.h>
57
58#include <linux/interrupt.h>
59#include <linux/i2c.h>
60#include <linux/i2c-algo-bit.h>
61
62#include <linux/spinlock.h>
63#define MAP_NR(x) virt_to_page(x)
64#define ZORAN_HARDWARE VID_HARDWARE_ZR36067
65#define ZORAN_VID_TYPE ( \
66 VID_TYPE_CAPTURE | \
67 VID_TYPE_OVERLAY | \
68 VID_TYPE_CLIPPING | \
69 VID_TYPE_FRAMERAM | \
70 VID_TYPE_SCALES | \
71 VID_TYPE_MJPEG_DECODER | \
72 VID_TYPE_MJPEG_ENCODER \
73 )
74
75#include <linux/videodev.h>
76#include "videocodec.h"
77
78#include <asm/io.h>
79#include <asm/uaccess.h>
80#include <linux/proc_fs.h>
81
82#include <linux/video_decoder.h>
83#include <linux/video_encoder.h>
84#include "zoran.h"
85#include "zoran_device.h"
86#include "zoran_card.h"
87
88#ifdef HAVE_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 \
96 )
97#endif
98
99#include <asm/byteorder.h>
100
101const struct zoran_format zoran_formats[] = {
102 {
103 .name = "15-bit RGB",
104 .palette = VIDEO_PALETTE_RGB555,
105#ifdef HAVE_V4L2
106#ifdef __LITTLE_ENDIAN
107 .fourcc = V4L2_PIX_FMT_RGB555,
108#else
109 .fourcc = V4L2_PIX_FMT_RGB555X,
110#endif
111 .colorspace = V4L2_COLORSPACE_SRGB,
112#endif
113 .depth = 15,
114 .flags = ZORAN_FORMAT_CAPTURE |
115 ZORAN_FORMAT_OVERLAY,
116 }, {
117 .name = "16-bit RGB",
118 .palette = VIDEO_PALETTE_RGB565,
119#ifdef HAVE_V4L2
120#ifdef __LITTLE_ENDIAN
121 .fourcc = V4L2_PIX_FMT_RGB565,
122#else
123 .fourcc = V4L2_PIX_FMT_RGB565X,
124#endif
125 .colorspace = V4L2_COLORSPACE_SRGB,
126#endif
127 .depth = 16,
128 .flags = ZORAN_FORMAT_CAPTURE |
129 ZORAN_FORMAT_OVERLAY,
130 }, {
131 .name = "24-bit RGB",
132 .palette = VIDEO_PALETTE_RGB24,
133#ifdef HAVE_V4L2
134#ifdef __LITTLE_ENDIAN
135 .fourcc = V4L2_PIX_FMT_BGR24,
136#else
137 .fourcc = V4L2_PIX_FMT_RGB24,
138#endif
139 .colorspace = V4L2_COLORSPACE_SRGB,
140#endif
141 .depth = 24,
142 .flags = ZORAN_FORMAT_CAPTURE |
143 ZORAN_FORMAT_OVERLAY,
144 }, {
145 .name = "32-bit RGB",
146 .palette = VIDEO_PALETTE_RGB32,
147#ifdef HAVE_V4L2
148#ifdef __LITTLE_ENDIAN
149 .fourcc = V4L2_PIX_FMT_BGR32,
150#else
151 .fourcc = V4L2_PIX_FMT_RGB32,
152#endif
153 .colorspace = V4L2_COLORSPACE_SRGB,
154#endif
155 .depth = 32,
156 .flags = ZORAN_FORMAT_CAPTURE |
157 ZORAN_FORMAT_OVERLAY,
158 }, {
159 .name = "4:2:2, packed, YUYV",
160 .palette = VIDEO_PALETTE_YUV422,
161#ifdef HAVE_V4L2
162 .fourcc = V4L2_PIX_FMT_YUYV,
163 .colorspace = V4L2_COLORSPACE_SMPTE170M,
164#endif
165 .depth = 16,
166 .flags = ZORAN_FORMAT_CAPTURE |
167 ZORAN_FORMAT_OVERLAY,
168 }, {
169 .name = "Hardware-encoded Motion-JPEG",
170 .palette = -1,
171#ifdef HAVE_V4L2
172 .fourcc = V4L2_PIX_FMT_MJPEG,
173 .colorspace = V4L2_COLORSPACE_SMPTE170M,
174#endif
175 .depth = 0,
176 .flags = ZORAN_FORMAT_CAPTURE |
177 ZORAN_FORMAT_PLAYBACK |
178 ZORAN_FORMAT_COMPRESSED,
179 }
180};
181static const int zoran_num_formats =
182 (sizeof(zoran_formats) / sizeof(struct zoran_format));
183
184// RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
185#if !defined(CONFIG_BIGPHYS_AREA)
186//#undef CONFIG_BIGPHYS_AREA
187#define BUZ_USE_HIMEM
188#endif
189
190#if defined(CONFIG_BIGPHYS_AREA)
191# include <linux/bigphysarea.h>
192#endif
193
194extern int *zr_debug;
195
196#define dprintk(num, format, args...) \
197 do { \
198 if (*zr_debug >= num) \
199 printk(format, ##args); \
200 } while (0)
201
202extern int v4l_nbufs;
203extern int v4l_bufsize;
204extern int jpg_nbufs;
205extern int jpg_bufsize;
206extern int pass_through;
207
208static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
209module_param(lock_norm, int, 0);
210MODULE_PARM_DESC(lock_norm, "Users can't change norm");
211
212#ifdef HAVE_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 */
216static __u32
217zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
218{
219 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
220 __u32 num = (1024 * 512) / (div);
221 __u32 result = 2;
222
223 num--;
224 while (num) {
225 num >>= 1;
226 result <<= 1;
227 }
228
229 if (result > jpg_bufsize)
230 return jpg_bufsize;
231 if (result < 8192)
232 return 8192;
233 return result;
234}
235#endif
236
237/* forward references */
238static void v4l_fbuffer_free(struct file *file);
239static void jpg_fbuffer_free(struct file *file);
240
241/*
242 * Allocate the V4L grab buffers
243 *
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).
250 */
251
252#if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
253static unsigned long
254get_high_mem (unsigned long size)
255{
256/*
257 * Check if there is usable memory at the end of Linux memory
258 * of at least size. Return the physical address of this memory,
259 * return 0 on failure.
260 *
261 * The idea is from Alexandro Rubini's book "Linux device drivers".
262 * The driver from him which is downloadable from O'Reilly's
263 * web site misses the "virt_to_phys(high_memory)" part
264 * (and therefore doesn't work at all - at least with 2.2.x kernels).
265 *
266 * It should be unnecessary to mention that THIS IS DANGEROUS,
267 * if more than one driver at a time has the idea to use this memory!!!!
268 */
269
270 volatile unsigned char __iomem *mem;
271 unsigned char c;
272 unsigned long hi_mem_ph;
273 unsigned long i;
274
275 /* Map the high memory to user space */
276
277 hi_mem_ph = virt_to_phys(high_memory);
278
279 mem = ioremap(hi_mem_ph, size);
280 if (!mem) {
281 dprintk(1,
282 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
283 ZORAN_NAME);
284 return 0;
285 }
286
287 for (i = 0; i < size; i++) {
288 /* Check if it is memory */
289 c = i & 0xff;
290 writeb(c, mem + i);
291 if (readb(mem + i) != c)
292 break;
293 c = 255 - c;
294 writeb(c, mem + i);
295 if (readb(mem + i) != c)
296 break;
297 writeb(0, mem + i); /* zero out memory */
298
299 /* give the kernel air to breath */
300 if ((i & 0x3ffff) == 0x3ffff)
301 schedule();
302 }
303
304 iounmap(mem);
305
306 if (i != size) {
307 dprintk(1,
308 KERN_ERR
309 "%s: get_high_mem() - requested %lu, avail %lu\n",
310 ZORAN_NAME, size, i);
311 return 0;
312 }
313
314 return hi_mem_ph;
315}
316#endif
317
318static int
319v4l_fbuffer_alloc (struct file *file)
320{
321 struct zoran_fh *fh = file->private_data;
322 struct zoran *zr = fh->zr;
323 int i, off;
324 unsigned char *mem;
325#if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
326 unsigned long pmem = 0;
327#endif
328
329 /* we might have old buffers lying around... */
330 if (fh->v4l_buffers.ready_to_be_freed) {
331 v4l_fbuffer_free(file);
332 }
333
334 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
335 if (fh->v4l_buffers.buffer[i].fbuffer)
336 dprintk(2,
337 KERN_WARNING
338 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
339 ZR_DEVNAME(zr), i);
340
341 //udelay(20);
342 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
343 /* Use kmalloc */
344
345 mem =
346 (unsigned char *) kmalloc(fh->v4l_buffers.
347 buffer_size,
348 GFP_KERNEL);
349 if (mem == 0) {
350 dprintk(1,
351 KERN_ERR
352 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
353 ZR_DEVNAME(zr), i);
354 v4l_fbuffer_free(file);
355 return -ENOBUFS;
356 }
357 fh->v4l_buffers.buffer[i].fbuffer = mem;
358 fh->v4l_buffers.buffer[i].fbuffer_phys =
359 virt_to_phys(mem);
360 fh->v4l_buffers.buffer[i].fbuffer_bus =
361 virt_to_bus(mem);
362 for (off = 0; off < fh->v4l_buffers.buffer_size;
363 off += PAGE_SIZE)
364 SetPageReserved(MAP_NR(mem + off));
365 dprintk(4,
366 KERN_INFO
367 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
368 ZR_DEVNAME(zr), i, (unsigned long) mem,
369 virt_to_bus(mem));
370 } else {
371#if defined(CONFIG_BIGPHYS_AREA)
372 /* Use bigphysarea_alloc_pages */
373
374 int n =
375 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
376 1) / PAGE_SIZE;
377
378 mem =
379 (unsigned char *) bigphysarea_alloc_pages(n, 0,
380 GFP_KERNEL);
381 if (mem == 0) {
382 dprintk(1,
383 KERN_ERR
384 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
385 ZR_DEVNAME(zr), i);
386 v4l_fbuffer_free(file);
387 return -ENOBUFS;
388 }
389 fh->v4l_buffers.buffer[i].fbuffer = mem;
390 fh->v4l_buffers.buffer[i].fbuffer_phys =
391 virt_to_phys(mem);
392 fh->v4l_buffers.buffer[i].fbuffer_bus =
393 virt_to_bus(mem);
394 dprintk(4,
395 KERN_INFO
396 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
397 ZR_DEVNAME(zr), i, (unsigned) mem,
398 (unsigned) virt_to_bus(mem));
399
400 /* Zero out the allocated memory */
401 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
402 fh->v4l_buffers.buffer_size);
403#elif defined(BUZ_USE_HIMEM)
404
405 /* Use high memory which has been left at boot time */
406
407 /* Ok., Ok. this is an evil hack - we make
408 * the assumption that physical addresses are
409 * the same as bus addresses (true at least
410 * for Intel processors). The whole method of
411 * obtaining and using this memory is not very
412 * nice - but I hope it saves some poor users
413 * from kernel hacking, which might have even
414 * more evil results */
415
416 if (i == 0) {
417 int size =
418 fh->v4l_buffers.num_buffers *
419 fh->v4l_buffers.buffer_size;
420
421 pmem = get_high_mem(size);
422 if (pmem == 0) {
423 dprintk(1,
424 KERN_ERR
425 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
426 ZR_DEVNAME(zr), size >> 10);
427 return -ENOBUFS;
428 }
429 fh->v4l_buffers.buffer[0].fbuffer = NULL;
430 fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
431 fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
432 dprintk(4,
433 KERN_INFO
434 "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
435 ZR_DEVNAME(zr), size >> 10);
436 } else {
437 fh->v4l_buffers.buffer[i].fbuffer = NULL;
438 fh->v4l_buffers.buffer[i].fbuffer_phys =
439 pmem + i * fh->v4l_buffers.buffer_size;
440 fh->v4l_buffers.buffer[i].fbuffer_bus =
441 pmem + i * fh->v4l_buffers.buffer_size;
442 }
443#else
444 /* No bigphysarea present, usage of high memory disabled,
445 * but user wants buffers of more than MAX_KMALLOC_MEM */
446 dprintk(1,
447 KERN_ERR
448 "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
449 ZR_DEVNAME(zr));
450 dprintk(1,
451 KERN_ERR
452 "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
453 ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
454 fh->v4l_buffers.buffer_size >> 10);
455 return -ENOBUFS;
456#endif
457 }
458 }
459
460 fh->v4l_buffers.allocated = 1;
461
462 return 0;
463}
464
465/* free the V4L grab buffers */
466static void
467v4l_fbuffer_free (struct file *file)
468{
469 struct zoran_fh *fh = file->private_data;
470 struct zoran *zr = fh->zr;
471 int i, off;
472 unsigned char *mem;
473
474 dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
475
476 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
477 if (!fh->v4l_buffers.buffer[i].fbuffer)
478 continue;
479
480 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
481 mem = fh->v4l_buffers.buffer[i].fbuffer;
482 for (off = 0; off < fh->v4l_buffers.buffer_size;
483 off += PAGE_SIZE)
484 ClearPageReserved(MAP_NR(mem + off));
485 kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
486 }
487#if defined(CONFIG_BIGPHYS_AREA)
488 else
489 bigphysarea_free_pages((void *) fh->v4l_buffers.
490 buffer[i].fbuffer);
491#endif
492 fh->v4l_buffers.buffer[i].fbuffer = NULL;
493 }
494
495 fh->v4l_buffers.allocated = 0;
496 fh->v4l_buffers.ready_to_be_freed = 0;
497}
498
499/*
500 * Allocate the MJPEG grab buffers.
501 *
502 * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
503 * kmalloc is used to request a physically contiguous area,
504 * else we allocate the memory in framgents with get_zeroed_page.
505 *
506 * If a Natoma chipset is present and this is a revision 1 zr36057,
507 * each MJPEG buffer needs to be physically contiguous.
508 * (RJ: This statement is from Dave Perks' original driver,
509 * I could never check it because I have a zr36067)
510 * The driver cares about this because it reduces the buffer
511 * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
512 *
513 * RJ: The contents grab buffers needs never be accessed in the driver.
514 * Therefore there is no need to allocate them with vmalloc in order
515 * to get a contiguous virtual memory space.
516 * I don't understand why many other drivers first allocate them with
517 * vmalloc (which uses internally also get_zeroed_page, but delivers you
518 * virtual addresses) and then again have to make a lot of efforts
519 * to get the physical address.
520 *
521 * Ben Capper:
522 * On big-endian architectures (such as ppc) some extra steps
523 * are needed. When reading and writing to the stat_com array
524 * and fragment buffers, the device expects to see little-
525 * endian values. The use of cpu_to_le32() and le32_to_cpu()
526 * in this function (and one or two others in zoran_device.c)
527 * ensure that these values are always stored in little-endian
528 * form, regardless of architecture. The zr36057 does Very Bad
529 * Things on big endian architectures if the stat_com array
530 * and fragment buffers are not little-endian.
531 */
532
533static int
534jpg_fbuffer_alloc (struct file *file)
535{
536 struct zoran_fh *fh = file->private_data;
537 struct zoran *zr = fh->zr;
538 int i, j, off;
539 unsigned long mem;
540
541 /* we might have old buffers lying around */
542 if (fh->jpg_buffers.ready_to_be_freed) {
543 jpg_fbuffer_free(file);
544 }
545
546 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
547 if (fh->jpg_buffers.buffer[i].frag_tab)
548 dprintk(2,
549 KERN_WARNING
550 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
551 ZR_DEVNAME(zr), i);
552
553 /* Allocate fragment table for this buffer */
554
555 mem = get_zeroed_page(GFP_KERNEL);
556 if (mem == 0) {
557 dprintk(1,
558 KERN_ERR
559 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
560 ZR_DEVNAME(zr), i);
561 jpg_fbuffer_free(file);
562 return -ENOBUFS;
563 }
564 memset((void *) mem, 0, PAGE_SIZE);
565 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
566 fh->jpg_buffers.buffer[i].frag_tab_bus =
567 virt_to_bus((void *) mem);
568
569 //if (alloc_contig) {
570 if (fh->jpg_buffers.need_contiguous) {
571 mem =
572 (unsigned long) kmalloc(fh->jpg_buffers.
573 buffer_size,
574 GFP_KERNEL);
575 if (mem == 0) {
576 dprintk(1,
577 KERN_ERR
578 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
579 ZR_DEVNAME(zr), i);
580 jpg_fbuffer_free(file);
581 return -ENOBUFS;
582 }
583 fh->jpg_buffers.buffer[i].frag_tab[0] =
584 cpu_to_le32(virt_to_bus((void *) mem));
585 fh->jpg_buffers.buffer[i].frag_tab[1] =
586 cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
587 for (off = 0; off < fh->jpg_buffers.buffer_size;
588 off += PAGE_SIZE)
589 SetPageReserved(MAP_NR(mem + off));
590 } else {
591 /* jpg_bufsize is allreay page aligned */
592 for (j = 0;
593 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
594 j++) {
595 mem = get_zeroed_page(GFP_KERNEL);
596 if (mem == 0) {
597 dprintk(1,
598 KERN_ERR
599 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
600 ZR_DEVNAME(zr), i);
601 jpg_fbuffer_free(file);
602 return -ENOBUFS;
603 }
604
605 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
606 cpu_to_le32(virt_to_bus((void *) mem));
607 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
608 1] =
609 cpu_to_le32((PAGE_SIZE / 4) << 1);
610 SetPageReserved(MAP_NR(mem));
611 }
612
613 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
614 }
615 }
616
617 dprintk(4,
618 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
619 ZR_DEVNAME(zr),
620 (fh->jpg_buffers.num_buffers *
621 fh->jpg_buffers.buffer_size) >> 10);
622
623 fh->jpg_buffers.allocated = 1;
624
625 return 0;
626}
627
628/* free the MJPEG grab buffers */
629static void
630jpg_fbuffer_free (struct file *file)
631{
632 struct zoran_fh *fh = file->private_data;
633 struct zoran *zr = fh->zr;
634 int i, j, off;
635 unsigned char *mem;
636
637 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
638
639 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
640 if (!fh->jpg_buffers.buffer[i].frag_tab)
641 continue;
642
643 //if (alloc_contig) {
644 if (fh->jpg_buffers.need_contiguous) {
645 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
646 mem = (unsigned char *) bus_to_virt(le32_to_cpu(
647 fh->jpg_buffers.buffer[i].frag_tab[0]));
648 for (off = 0;
649 off < fh->jpg_buffers.buffer_size;
650 off += PAGE_SIZE)
651 ClearPageReserved(MAP_NR
652 (mem + off));
653 kfree((void *) mem);
654 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
655 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
656 }
657 } else {
658 for (j = 0;
659 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
660 j++) {
661 if (!fh->jpg_buffers.buffer[i].
662 frag_tab[2 * j])
663 break;
664 ClearPageReserved(MAP_NR
665 (bus_to_virt
666 (le32_to_cpu
667 (fh->jpg_buffers.
668 buffer[i].frag_tab[2 *
669 j]))));
670 free_page((unsigned long)
671 bus_to_virt
672 (le32_to_cpu
673 (fh->jpg_buffers.
674 buffer[i].
675 frag_tab[2 * j])));
676 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
677 0;
678 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
679 1] = 0;
680 }
681 }
682
683 free_page((unsigned long) fh->jpg_buffers.buffer[i].
684 frag_tab);
685 fh->jpg_buffers.buffer[i].frag_tab = NULL;
686 }
687
688 fh->jpg_buffers.allocated = 0;
689 fh->jpg_buffers.ready_to_be_freed = 0;
690}
691
692/*
693 * V4L Buffer grabbing
694 */
695
696static int
697zoran_v4l_set_format (struct file *file,
698 int width,
699 int height,
700 const struct zoran_format *format)
701{
702 struct zoran_fh *fh = file->private_data;
703 struct zoran *zr = fh->zr;
704 int bpp;
705
706 /* Check size and format of the grab wanted */
707
708 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
709 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
710 dprintk(1,
711 KERN_ERR
712 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
713 ZR_DEVNAME(zr), width, height);
714 return -EINVAL;
715 }
716
717 bpp = (format->depth + 7) / 8;
718
719 /* Check against available buffer size */
720 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
721 dprintk(1,
722 KERN_ERR
723 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
724 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
725 return -EINVAL;
726 }
727
728 /* The video front end needs 4-byte alinged line sizes */
729
730 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
731 dprintk(1,
732 KERN_ERR
733 "%s: v4l_set_format() - wrong frame alingment\n",
734 ZR_DEVNAME(zr));
735 return -EINVAL;
736 }
737
738 fh->v4l_settings.width = width;
739 fh->v4l_settings.height = height;
740 fh->v4l_settings.format = format;
741 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
742
743 return 0;
744}
745
746static int
747zoran_v4l_queue_frame (struct file *file,
748 int num)
749{
750 struct zoran_fh *fh = file->private_data;
751 struct zoran *zr = fh->zr;
752 unsigned long flags;
753 int res = 0;
754
755 if (!fh->v4l_buffers.allocated) {
756 dprintk(1,
757 KERN_ERR
758 "%s: v4l_queue_frame() - buffers not yet allocated\n",
759 ZR_DEVNAME(zr));
760 res = -ENOMEM;
761 }
762
763 /* No grabbing outside the buffer range! */
764 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
765 dprintk(1,
766 KERN_ERR
767 "%s: v4l_queue_frame() - buffer %d is out of range\n",
768 ZR_DEVNAME(zr), num);
769 res = -EINVAL;
770 }
771
772 spin_lock_irqsave(&zr->spinlock, flags);
773
774 if (fh->v4l_buffers.active == ZORAN_FREE) {
775 if (zr->v4l_buffers.active == ZORAN_FREE) {
776 zr->v4l_buffers = fh->v4l_buffers;
777 fh->v4l_buffers.active = ZORAN_ACTIVE;
778 } else {
779 dprintk(1,
780 KERN_ERR
781 "%s: v4l_queue_frame() - another session is already capturing\n",
782 ZR_DEVNAME(zr));
783 res = -EBUSY;
784 }
785 }
786
787 /* make sure a grab isn't going on currently with this buffer */
788 if (!res) {
789 switch (zr->v4l_buffers.buffer[num].state) {
790 default:
791 case BUZ_STATE_PEND:
792 if (zr->v4l_buffers.active == ZORAN_FREE) {
793 fh->v4l_buffers.active = ZORAN_FREE;
794 zr->v4l_buffers.allocated = 0;
795 }
796 res = -EBUSY; /* what are you doing? */
797 break;
798 case BUZ_STATE_DONE:
799 dprintk(2,
800 KERN_WARNING
801 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
802 ZR_DEVNAME(zr), num);
803 case BUZ_STATE_USER:
804 /* since there is at least one unused buffer there's room for at least
805 * one more pend[] entry */
806 zr->v4l_pend[zr->v4l_pend_head++ &
807 V4L_MASK_FRAME] = num;
808 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
809 zr->v4l_buffers.buffer[num].bs.length =
810 fh->v4l_settings.bytesperline *
811 zr->v4l_settings.height;
812 fh->v4l_buffers.buffer[num] =
813 zr->v4l_buffers.buffer[num];
814 break;
815 }
816 }
817
818 spin_unlock_irqrestore(&zr->spinlock, flags);
819
820 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
821 zr->v4l_buffers.active = fh->v4l_buffers.active;
822
823 return res;
824}
825
826static int
827v4l_grab (struct file *file,
828 struct video_mmap *mp)
829{
830 struct zoran_fh *fh = file->private_data;
831 struct zoran *zr = fh->zr;
832 int res = 0, i;
833
834 for (i = 0; i < zoran_num_formats; i++) {
835 if (zoran_formats[i].palette == mp->format &&
836 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
837 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
838 break;
839 }
840 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
841 dprintk(1,
842 KERN_ERR
843 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
844 ZR_DEVNAME(zr));
845 return -EINVAL;
846 }
847
848 /*
849 * To minimize the time spent in the IRQ routine, we avoid setting up
850 * the video front end there.
851 * If this grab has different parameters from a running streaming capture
852 * we stop the streaming capture and start it over again.
853 */
854 if (zr->v4l_memgrab_active &&
855 (zr->v4l_settings.width != mp->width ||
856 zr->v4l_settings.height != mp->height ||
857 zr->v4l_settings.format->palette != mp->format)) {
858 res = wait_grab_pending(zr);
859 if (res)
860 return res;
861 }
862 if ((res = zoran_v4l_set_format(file,
863 mp->width,
864 mp->height,
865 &zoran_formats[i])))
866 return res;
867 zr->v4l_settings = fh->v4l_settings;
868
869 /* queue the frame in the pending queue */
870 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
871 fh->v4l_buffers.active = ZORAN_FREE;
872 return res;
873 }
874
875 /* put the 36057 into frame grabbing mode */
876 if (!res && !zr->v4l_memgrab_active)
877 zr36057_set_memgrab(zr, 1);
878
879 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
880
881 return res;
882}
883
884/*
885 * Sync on a V4L buffer
886 */
887
888static int
889v4l_sync (struct file *file,
890 int frame)
891{
892 struct zoran_fh *fh = file->private_data;
893 struct zoran *zr = fh->zr;
894 unsigned long flags;
895
896 if (fh->v4l_buffers.active == ZORAN_FREE) {
897 dprintk(1,
898 KERN_ERR
899 "%s: v4l_sync() - no grab active for this session\n",
900 ZR_DEVNAME(zr));
901 return -EINVAL;
902 }
903
904 /* check passed-in frame number */
905 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
906 dprintk(1,
907 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
908 ZR_DEVNAME(zr), frame);
909 return -EINVAL;
910 }
911
912 /* Check if is buffer was queued at all */
913 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
914 dprintk(1,
915 KERN_ERR
916 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
917 ZR_DEVNAME(zr));
918 return -EPROTO;
919 }
920
921 /* wait on this buffer to get ready */
922 if (!wait_event_interruptible_timeout(zr->v4l_capq,
923 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
924 10*HZ))
925 return -ETIME;
926 if (signal_pending(current))
927 return -ERESTARTSYS;
928
929 /* buffer should now be in BUZ_STATE_DONE */
930 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
931 dprintk(2,
932 KERN_ERR "%s: v4l_sync() - internal state error\n",
933 ZR_DEVNAME(zr));
934
935 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
936 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
937
938 spin_lock_irqsave(&zr->spinlock, flags);
939
940 /* Check if streaming capture has finished */
941 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
942 zr36057_set_memgrab(zr, 0);
943 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
944 fh->v4l_buffers.active = zr->v4l_buffers.active =
945 ZORAN_FREE;
946 zr->v4l_buffers.allocated = 0;
947 }
948 }
949
950 spin_unlock_irqrestore(&zr->spinlock, flags);
951
952 return 0;
953}
954
955/*
956 * Queue a MJPEG buffer for capture/playback
957 */
958
959static int
960zoran_jpg_queue_frame (struct file *file,
961 int num,
962 enum zoran_codec_mode mode)
963{
964 struct zoran_fh *fh = file->private_data;
965 struct zoran *zr = fh->zr;
966 unsigned long flags;
967 int res = 0;
968
969 /* Check if buffers are allocated */
970 if (!fh->jpg_buffers.allocated) {
971 dprintk(1,
972 KERN_ERR
973 "%s: jpg_queue_frame() - buffers not yet allocated\n",
974 ZR_DEVNAME(zr));
975 return -ENOMEM;
976 }
977
978 /* No grabbing outside the buffer range! */
979 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
980 dprintk(1,
981 KERN_ERR
982 "%s: jpg_queue_frame() - buffer %d out of range\n",
983 ZR_DEVNAME(zr), num);
984 return -EINVAL;
985 }
986
987 /* what is the codec mode right now? */
988 if (zr->codec_mode == BUZ_MODE_IDLE) {
989 zr->jpg_settings = fh->jpg_settings;
990 } else if (zr->codec_mode != mode) {
991 /* wrong codec mode active - invalid */
992 dprintk(1,
993 KERN_ERR
994 "%s: jpg_queue_frame() - codec in wrong mode\n",
995 ZR_DEVNAME(zr));
996 return -EINVAL;
997 }
998
999 spin_lock_irqsave(&zr->spinlock, flags);
1000
1001 if (fh->jpg_buffers.active == ZORAN_FREE) {
1002 if (zr->jpg_buffers.active == ZORAN_FREE) {
1003 zr->jpg_buffers = fh->jpg_buffers;
1004 fh->jpg_buffers.active = ZORAN_ACTIVE;
1005 } else {
1006 dprintk(1,
1007 KERN_ERR
1008 "%s: jpg_queue_frame() - another session is already capturing\n",
1009 ZR_DEVNAME(zr));
1010 res = -EBUSY;
1011 }
1012 }
1013
1014 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1015 /* Ok load up the jpeg codec */
1016 zr36057_enable_jpg(zr, mode);
1017 }
1018
1019 if (!res) {
1020 switch (zr->jpg_buffers.buffer[num].state) {
1021 case BUZ_STATE_DONE:
1022 dprintk(2,
1023 KERN_WARNING
1024 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1025 ZR_DEVNAME(zr));
1026 case BUZ_STATE_USER:
1027 /* since there is at least one unused buffer there's room for at
1028 *least one more pend[] entry */
1029 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1030 num;
1031 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1032 fh->jpg_buffers.buffer[num] =
1033 zr->jpg_buffers.buffer[num];
1034 zoran_feed_stat_com(zr);
1035 break;
1036 default:
1037 case BUZ_STATE_DMA:
1038 case BUZ_STATE_PEND:
1039 if (zr->jpg_buffers.active == ZORAN_FREE) {
1040 fh->jpg_buffers.active = ZORAN_FREE;
1041 zr->jpg_buffers.allocated = 0;
1042 }
1043 res = -EBUSY; /* what are you doing? */
1044 break;
1045 }
1046 }
1047
1048 spin_unlock_irqrestore(&zr->spinlock, flags);
1049
1050 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1051 zr->jpg_buffers.active = fh->jpg_buffers.active;
1052 }
1053
1054 return res;
1055}
1056
1057static int
1058jpg_qbuf (struct file *file,
1059 int frame,
1060 enum zoran_codec_mode mode)
1061{
1062 struct zoran_fh *fh = file->private_data;
1063 struct zoran *zr = fh->zr;
1064 int res = 0;
1065
1066 /* Does the user want to stop streaming? */
1067 if (frame < 0) {
1068 if (zr->codec_mode == mode) {
1069 if (fh->jpg_buffers.active == ZORAN_FREE) {
1070 dprintk(1,
1071 KERN_ERR
1072 "%s: jpg_qbuf(-1) - session not active\n",
1073 ZR_DEVNAME(zr));
1074 return -EINVAL;
1075 }
1076 fh->jpg_buffers.active = zr->jpg_buffers.active =
1077 ZORAN_FREE;
1078 zr->jpg_buffers.allocated = 0;
1079 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1080 return 0;
1081 } else {
1082 dprintk(1,
1083 KERN_ERR
1084 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1085 ZR_DEVNAME(zr));
1086 return -EINVAL;
1087 }
1088 }
1089
1090 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1091 return res;
1092
1093 /* Start the jpeg codec when the first frame is queued */
1094 if (!res && zr->jpg_que_head == 1)
1095 jpeg_start(zr);
1096
1097 return res;
1098}
1099
1100/*
1101 * Sync on a MJPEG buffer
1102 */
1103
1104static int
1105jpg_sync (struct file *file,
1106 struct zoran_sync *bs)
1107{
1108 struct zoran_fh *fh = file->private_data;
1109 struct zoran *zr = fh->zr;
1110 unsigned long flags;
1111 int frame;
1112
1113 if (fh->jpg_buffers.active == ZORAN_FREE) {
1114 dprintk(1,
1115 KERN_ERR
1116 "%s: jpg_sync() - capture is not currently active\n",
1117 ZR_DEVNAME(zr));
1118 return -EINVAL;
1119 }
1120 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1121 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1122 dprintk(1,
1123 KERN_ERR
1124 "%s: jpg_sync() - codec not in streaming mode\n",
1125 ZR_DEVNAME(zr));
1126 return -EINVAL;
1127 }
1128 if (!wait_event_interruptible_timeout(zr->jpg_capq,
1129 (zr->jpg_que_tail != zr->jpg_dma_tail ||
1130 zr->jpg_dma_tail == zr->jpg_dma_head),
1131 10*HZ)) {
1132 int isr;
1133
1134 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1135 udelay(1);
1136 zr->codec->control(zr->codec, CODEC_G_STATUS,
1137 sizeof(isr), &isr);
1138 dprintk(1,
1139 KERN_ERR
1140 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1141 ZR_DEVNAME(zr), isr);
1142
1143 return -ETIME;
1144
1145 }
1146 if (signal_pending(current))
1147 return -ERESTARTSYS;
1148
1149 spin_lock_irqsave(&zr->spinlock, flags);
1150
1151 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1152 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1153 else
1154 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1155
1156 /* buffer should now be in BUZ_STATE_DONE */
1157 if (*zr_debug > 0)
1158 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1159 dprintk(2,
1160 KERN_ERR
1161 "%s: jpg_sync() - internal state error\n",
1162 ZR_DEVNAME(zr));
1163
1164 *bs = zr->jpg_buffers.buffer[frame].bs;
1165 bs->frame = frame;
1166 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1167 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1168
1169 spin_unlock_irqrestore(&zr->spinlock, flags);
1170
1171 return 0;
1172}
1173
1174static void
1175zoran_open_init_session (struct file *file)
1176{
1177 int i;
1178 struct zoran_fh *fh = file->private_data;
1179 struct zoran *zr = fh->zr;
1180
1181 /* Per default, map the V4L Buffers */
1182 fh->map_mode = ZORAN_MAP_MODE_RAW;
1183
1184 /* take over the card's current settings */
1185 fh->overlay_settings = zr->overlay_settings;
1186 fh->overlay_settings.is_set = 0;
1187 fh->overlay_settings.format = zr->overlay_settings.format;
1188 fh->overlay_active = ZORAN_FREE;
1189
1190 /* v4l settings */
1191 fh->v4l_settings = zr->v4l_settings;
1192
1193 /* v4l_buffers */
1194 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1195 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1196 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1197 fh->v4l_buffers.buffer[i].bs.frame = i;
1198 }
1199 fh->v4l_buffers.allocated = 0;
1200 fh->v4l_buffers.ready_to_be_freed = 0;
1201 fh->v4l_buffers.active = ZORAN_FREE;
1202 fh->v4l_buffers.buffer_size = v4l_bufsize;
1203 fh->v4l_buffers.num_buffers = v4l_nbufs;
1204
1205 /* jpg settings */
1206 fh->jpg_settings = zr->jpg_settings;
1207
1208 /* jpg_buffers */
1209 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1210 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1211 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1212 fh->jpg_buffers.buffer[i].bs.frame = i;
1213 }
1214 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1215 fh->jpg_buffers.allocated = 0;
1216 fh->jpg_buffers.ready_to_be_freed = 0;
1217 fh->jpg_buffers.active = ZORAN_FREE;
1218 fh->jpg_buffers.buffer_size = jpg_bufsize;
1219 fh->jpg_buffers.num_buffers = jpg_nbufs;
1220}
1221
1222static void
1223zoran_close_end_session (struct file *file)
1224{
1225 struct zoran_fh *fh = file->private_data;
1226 struct zoran *zr = fh->zr;
1227
1228 /* overlay */
1229 if (fh->overlay_active != ZORAN_FREE) {
1230 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1231 zr->v4l_overlay_active = 0;
1232 if (!zr->v4l_memgrab_active)
1233 zr36057_overlay(zr, 0);
1234 zr->overlay_mask = NULL;
1235 }
1236
1237 /* v4l capture */
1238 if (fh->v4l_buffers.active != ZORAN_FREE) {
1239 zr36057_set_memgrab(zr, 0);
1240 zr->v4l_buffers.allocated = 0;
1241 zr->v4l_buffers.active = fh->v4l_buffers.active =
1242 ZORAN_FREE;
1243 }
1244
1245 /* v4l buffers */
1246 if (fh->v4l_buffers.allocated ||
1247 fh->v4l_buffers.ready_to_be_freed) {
1248 v4l_fbuffer_free(file);
1249 }
1250
1251 /* jpg capture */
1252 if (fh->jpg_buffers.active != ZORAN_FREE) {
1253 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1254 zr->jpg_buffers.allocated = 0;
1255 zr->jpg_buffers.active = fh->jpg_buffers.active =
1256 ZORAN_FREE;
1257 }
1258
1259 /* jpg buffers */
1260 if (fh->jpg_buffers.allocated ||
1261 fh->jpg_buffers.ready_to_be_freed) {
1262 jpg_fbuffer_free(file);
1263 }
1264}
1265
1266/*
1267 * Open a zoran card. Right now the flags stuff is just playing
1268 */
1269
1270static int
1271zoran_open (struct inode *inode,
1272 struct file *file)
1273{
1274 unsigned int minor = iminor(inode);
1275 struct zoran *zr = NULL;
1276 struct zoran_fh *fh;
1277 int i, res, first_open = 0, have_module_locks = 0;
1278
1279 /* find the device */
1280 for (i = 0; i < zoran_num; i++) {
1281 if (zoran[i].video_dev->minor == minor) {
1282 zr = &zoran[i];
1283 break;
1284 }
1285 }
1286
1287 if (!zr) {
1288 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1289 res = -ENODEV;
1290 goto open_unlock_and_return;
1291 }
1292
1293 /* see fs/device.c - the kernel already locks during open(),
1294 * so locking ourselves only causes deadlocks */
1295 /*down(&zr->resource_lock);*/
1296
1297 if (!zr->decoder) {
1298 dprintk(1,
1299 KERN_ERR "%s: no TV decoder loaded for device!\n",
1300 ZR_DEVNAME(zr));
1301 res = -EIO;
1302 goto open_unlock_and_return;
1303 }
1304
1305 /* try to grab a module lock */
1306 if (!try_module_get(THIS_MODULE)) {
1307 dprintk(1,
1308 KERN_ERR
1309 "%s: failed to acquire my own lock! PANIC!\n",
1310 ZR_DEVNAME(zr));
1311 res = -ENODEV;
1312 goto open_unlock_and_return;
1313 }
1314 if (!try_module_get(zr->decoder->driver->owner)) {
1315 dprintk(1,
1316 KERN_ERR
1317 "%s: failed to grab ownership of i2c decoder\n",
1318 ZR_DEVNAME(zr));
1319 res = -EIO;
1320 module_put(THIS_MODULE);
1321 goto open_unlock_and_return;
1322 }
1323 if (zr->encoder &&
1324 !try_module_get(zr->encoder->driver->owner)) {
1325 dprintk(1,
1326 KERN_ERR
1327 "%s: failed to grab ownership of i2c encoder\n",
1328 ZR_DEVNAME(zr));
1329 res = -EIO;
1330 module_put(zr->decoder->driver->owner);
1331 module_put(THIS_MODULE);
1332 goto open_unlock_and_return;
1333 }
1334
1335 have_module_locks = 1;
1336
1337 if (zr->user >= 2048) {
1338 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1339 ZR_DEVNAME(zr), zr->user);
1340 res = -EBUSY;
1341 goto open_unlock_and_return;
1342 }
1343
1344 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1345 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1346
1347 /* now, create the open()-specific file_ops struct */
1348 fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1349 if (!fh) {
1350 dprintk(1,
1351 KERN_ERR
1352 "%s: zoran_open() - allocation of zoran_fh failed\n",
1353 ZR_DEVNAME(zr));
1354 res = -ENOMEM;
1355 goto open_unlock_and_return;
1356 }
1357 memset(fh, 0, sizeof(struct zoran_fh));
1358 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1359 * on norm-change! */
1360 fh->overlay_mask =
1361 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1362 if (!fh->overlay_mask) {
1363 dprintk(1,
1364 KERN_ERR
1365 "%s: zoran_open() - allocation of overlay_mask failed\n",
1366 ZR_DEVNAME(zr));
1367 kfree(fh);
1368 res = -ENOMEM;
1369 goto open_unlock_and_return;
1370 }
1371
1372 if (zr->user++ == 0)
1373 first_open = 1;
1374
1375 /*up(&zr->resource_lock);*/
1376
1377 /* default setup - TODO: look at flags */
1378 if (first_open) { /* First device open */
1379 zr36057_restart(zr);
1380 zoran_open_init_params(zr);
1381 zoran_init_hardware(zr);
1382
1383 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1384 }
1385
1386 /* set file_ops stuff */
1387 file->private_data = fh;
1388 fh->zr = zr;
1389 zoran_open_init_session(file);
1390
1391 return 0;
1392
1393open_unlock_and_return:
1394 /* if we grabbed locks, release them accordingly */
1395 if (have_module_locks) {
1396 module_put(zr->decoder->driver->owner);
1397 if (zr->encoder) {
1398 module_put(zr->encoder->driver->owner);
1399 }
1400 module_put(THIS_MODULE);
1401 }
1402
1403 /* if there's no device found, we didn't obtain the lock either */
1404 if (zr) {
1405 /*up(&zr->resource_lock);*/
1406 }
1407
1408 return res;
1409}
1410
1411static int
1412zoran_close (struct inode *inode,
1413 struct file *file)
1414{
1415 struct zoran_fh *fh = file->private_data;
1416 struct zoran *zr = fh->zr;
1417
1418 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1419 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1420
1421 /* kernel locks (fs/device.c), so don't do that ourselves
1422 * (prevents deadlocks) */
1423 /*down(&zr->resource_lock);*/
1424
1425 zoran_close_end_session(file);
1426
1427 if (zr->user-- == 1) { /* Last process */
1428 /* Clean up JPEG process */
1429 wake_up_interruptible(&zr->jpg_capq);
1430 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1431 zr->jpg_buffers.allocated = 0;
1432 zr->jpg_buffers.active = ZORAN_FREE;
1433
1434 /* disable interrupts */
1435 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1436
1437 if (*zr_debug > 1)
1438 print_interrupts(zr);
1439
1440 /* Overlay off */
1441 zr->v4l_overlay_active = 0;
1442 zr36057_overlay(zr, 0);
1443 zr->overlay_mask = NULL;
1444
1445 /* capture off */
1446 wake_up_interruptible(&zr->v4l_capq);
1447 zr36057_set_memgrab(zr, 0);
1448 zr->v4l_buffers.allocated = 0;
1449 zr->v4l_buffers.active = ZORAN_FREE;
1450 zoran_set_pci_master(zr, 0);
1451
1452 if (!pass_through) { /* Switch to color bar */
1453 int zero = 0, two = 2;
1454 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1455 encoder_command(zr, ENCODER_SET_INPUT, &two);
1456 }
1457 }
1458
1459 file->private_data = NULL;
1460 kfree(fh->overlay_mask);
1461 kfree(fh);
1462
1463 /* release locks on the i2c modules */
1464 module_put(zr->decoder->driver->owner);
1465 if (zr->encoder) {
1466 module_put(zr->encoder->driver->owner);
1467 }
1468 module_put(THIS_MODULE);
1469
1470 /*up(&zr->resource_lock);*/
1471
1472 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1473
1474 return 0;
1475}
1476
1477
1478static ssize_t
1479zoran_read (struct file *file,
1480 char __user *data,
1481 size_t count,
1482 loff_t *ppos)
1483{
1484 /* we simply don't support read() (yet)... */
1485
1486 return -EINVAL;
1487}
1488
1489static ssize_t
1490zoran_write (struct file *file,
1491 const char __user *data,
1492 size_t count,
1493 loff_t *ppos)
1494{
1495 /* ...and the same goes for write() */
1496
1497 return -EINVAL;
1498}
1499
1500static int
1501setup_fbuffer (struct file *file,
1502 void *base,
1503 const struct zoran_format *fmt,
1504 int width,
1505 int height,
1506 int bytesperline)
1507{
1508 struct zoran_fh *fh = file->private_data;
1509 struct zoran *zr = fh->zr;
1510
1511 /* (Ronald) v4l/v4l2 guidelines */
1512 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1513 return -EPERM;
1514
1515 /* we need a bytesperline value, even if not given */
1516 if (!bytesperline)
1517 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1518
1519#if 0
1520 if (zr->overlay_active) {
1521 /* dzjee... stupid users... don't even bother to turn off
1522 * overlay before changing the memory location...
1523 * normally, we would return errors here. However, one of
1524 * the tools that does this is... xawtv! and since xawtv
1525 * is used by +/- 99% of the users, we'd rather be user-
1526 * friendly and silently do as if nothing went wrong */
1527 dprintk(3,
1528 KERN_ERR
1529 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1530 ZR_DEVNAME(zr));
1531 zr36057_overlay(zr, 0);
1532 }
1533#endif
1534
1535 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1536 dprintk(1,
1537 KERN_ERR
1538 "%s: setup_fbuffer() - no valid overlay format given\n",
1539 ZR_DEVNAME(zr));
1540 return -EINVAL;
1541 }
1542 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1543 dprintk(1,
1544 KERN_ERR
1545 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1546 ZR_DEVNAME(zr), width, height, bytesperline);
1547 return -EINVAL;
1548 }
1549 if (bytesperline & 3) {
1550 dprintk(1,
1551 KERN_ERR
1552 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1553 ZR_DEVNAME(zr), bytesperline);
1554 return -EINVAL;
1555 }
1556
1557 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1558 zr->buffer.height = height;
1559 zr->buffer.width = width;
1560 zr->buffer.depth = fmt->depth;
1561 zr->overlay_settings.format = fmt;
1562 zr->buffer.bytesperline = bytesperline;
1563
1564 /* The user should set new window parameters */
1565 zr->overlay_settings.is_set = 0;
1566
1567 return 0;
1568}
1569
1570
1571static int
1572setup_window (struct file *file,
1573 int x,
1574 int y,
1575 int width,
1576 int height,
1577 struct video_clip __user *clips,
1578 int clipcount,
1579 void __user *bitmap)
1580{
1581 struct zoran_fh *fh = file->private_data;
1582 struct zoran *zr = fh->zr;
1583 struct video_clip *vcp = NULL;
1584 int on, end;
1585
1586
1587 if (!zr->buffer.base) {
1588 dprintk(1,
1589 KERN_ERR
1590 "%s: setup_window() - frame buffer has to be set first\n",
1591 ZR_DEVNAME(zr));
1592 return -EINVAL;
1593 }
1594
1595 if (!fh->overlay_settings.format) {
1596 dprintk(1,
1597 KERN_ERR
1598 "%s: setup_window() - no overlay format set\n",
1599 ZR_DEVNAME(zr));
1600 return -EINVAL;
1601 }
1602
1603 /*
1604 * The video front end needs 4-byte alinged line sizes, we correct that
1605 * silently here if necessary
1606 */
1607 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1608 end = (x + width) & ~1; /* round down */
1609 x = (x + 1) & ~1; /* round up */
1610 width = end - x;
1611 }
1612
1613 if (zr->buffer.depth == 24) {
1614 end = (x + width) & ~3; /* round down */
1615 x = (x + 3) & ~3; /* round up */
1616 width = end - x;
1617 }
1618
1619 if (width > BUZ_MAX_WIDTH)
1620 width = BUZ_MAX_WIDTH;
1621 if (height > BUZ_MAX_HEIGHT)
1622 height = BUZ_MAX_HEIGHT;
1623
1624 /* Check for vaild parameters */
1625 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1626 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1627 dprintk(1,
1628 KERN_ERR
1629 "%s: setup_window() - width = %d or height = %d invalid\n",
1630 ZR_DEVNAME(zr), width, height);
1631 return -EINVAL;
1632 }
1633
1634 fh->overlay_settings.x = x;
1635 fh->overlay_settings.y = y;
1636 fh->overlay_settings.width = width;
1637 fh->overlay_settings.height = height;
1638 fh->overlay_settings.clipcount = clipcount;
1639
1640 /*
1641 * If an overlay is running, we have to switch it off
1642 * and switch it on again in order to get the new settings in effect.
1643 *
1644 * We also want to avoid that the overlay mask is written
1645 * when an overlay is running.
1646 */
1647
1648 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1649 zr->overlay_active != ZORAN_FREE &&
1650 fh->overlay_active != ZORAN_FREE;
1651 if (on)
1652 zr36057_overlay(zr, 0);
1653
1654 /*
1655 * Write the overlay mask if clips are wanted.
1656 * We prefer a bitmap.
1657 */
1658 if (bitmap) {
1659 /* fake value - it just means we want clips */
1660 fh->overlay_settings.clipcount = 1;
1661
1662 if (copy_from_user(fh->overlay_mask, bitmap,
1663 (width * height + 7) / 8)) {
1664 return -EFAULT;
1665 }
1666 } else if (clipcount > 0) {
1667 /* write our own bitmap from the clips */
1668 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1669 if (vcp == NULL) {
1670 dprintk(1,
1671 KERN_ERR
1672 "%s: setup_window() - Alloc of clip mask failed\n",
1673 ZR_DEVNAME(zr));
1674 return -ENOMEM;
1675 }
1676 if (copy_from_user
1677 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1678 vfree(vcp);
1679 return -EFAULT;
1680 }
1681 write_overlay_mask(file, vcp, clipcount);
1682 vfree(vcp);
1683 }
1684
1685 fh->overlay_settings.is_set = 1;
1686 if (fh->overlay_active != ZORAN_FREE &&
1687 zr->overlay_active != ZORAN_FREE)
1688 zr->overlay_settings = fh->overlay_settings;
1689
1690 if (on)
1691 zr36057_overlay(zr, 1);
1692
1693 /* Make sure the changes come into effect */
1694 return wait_grab_pending(zr);
1695}
1696
1697static int
1698setup_overlay (struct file *file,
1699 int on)
1700{
1701 struct zoran_fh *fh = file->private_data;
1702 struct zoran *zr = fh->zr;
1703
1704 /* If there is nothing to do, return immediatly */
1705 if ((on && fh->overlay_active != ZORAN_FREE) ||
1706 (!on && fh->overlay_active == ZORAN_FREE))
1707 return 0;
1708
1709 /* check whether we're touching someone else's overlay */
1710 if (on && zr->overlay_active != ZORAN_FREE &&
1711 fh->overlay_active == ZORAN_FREE) {
1712 dprintk(1,
1713 KERN_ERR
1714 "%s: setup_overlay() - overlay is already active for another session\n",
1715 ZR_DEVNAME(zr));
1716 return -EBUSY;
1717 }
1718 if (!on && zr->overlay_active != ZORAN_FREE &&
1719 fh->overlay_active == ZORAN_FREE) {
1720 dprintk(1,
1721 KERN_ERR
1722 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1723 ZR_DEVNAME(zr));
1724 return -EPERM;
1725 }
1726
1727 if (on == 0) {
1728 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1729 zr->v4l_overlay_active = 0;
1730 /* When a grab is running, the video simply
1731 * won't be switched on any more */
1732 if (!zr->v4l_memgrab_active)
1733 zr36057_overlay(zr, 0);
1734 zr->overlay_mask = NULL;
1735 } else {
1736 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1737 dprintk(1,
1738 KERN_ERR
1739 "%s: setup_overlay() - buffer or window not set\n",
1740 ZR_DEVNAME(zr));
1741 return -EINVAL;
1742 }
1743 if (!fh->overlay_settings.format) {
1744 dprintk(1,
1745 KERN_ERR
1746 "%s: setup_overlay() - no overlay format set\n",
1747 ZR_DEVNAME(zr));
1748 return -EINVAL;
1749 }
1750 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1751 zr->v4l_overlay_active = 1;
1752 zr->overlay_mask = fh->overlay_mask;
1753 zr->overlay_settings = fh->overlay_settings;
1754 if (!zr->v4l_memgrab_active)
1755 zr36057_overlay(zr, 1);
1756 /* When a grab is running, the video will be
1757 * switched on when grab is finished */
1758 }
1759
1760 /* Make sure the changes come into effect */
1761 return wait_grab_pending(zr);
1762}
1763
1764#ifdef HAVE_V4L2
1765 /* get the status of a buffer in the clients buffer queue */
1766static int
1767zoran_v4l2_buffer_status (struct file *file,
1768 struct v4l2_buffer *buf,
1769 int num)
1770{
1771 struct zoran_fh *fh = file->private_data;
1772 struct zoran *zr = fh->zr;
1773
1774 buf->flags = V4L2_BUF_FLAG_MAPPED;
1775
1776 switch (fh->map_mode) {
1777 case ZORAN_MAP_MODE_RAW:
1778
1779 /* check range */
1780 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1781 !fh->v4l_buffers.allocated) {
1782 dprintk(1,
1783 KERN_ERR
1784 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1785 ZR_DEVNAME(zr));
1786 return -EINVAL;
1787 }
1788
1789 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1790 buf->length = fh->v4l_buffers.buffer_size;
1791
1792 /* get buffer */
1793 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1794 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1795 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1796 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1797 buf->flags |= V4L2_BUF_FLAG_DONE;
1798 buf->timestamp =
1799 fh->v4l_buffers.buffer[num].bs.timestamp;
1800 } else {
1801 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1802 }
1803
1804 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1805 buf->field = V4L2_FIELD_TOP;
1806 else
1807 buf->field = V4L2_FIELD_INTERLACED;
1808
1809 break;
1810
1811 case ZORAN_MAP_MODE_JPG_REC:
1812 case ZORAN_MAP_MODE_JPG_PLAY:
1813
1814 /* check range */
1815 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1816 !fh->jpg_buffers.allocated) {
1817 dprintk(1,
1818 KERN_ERR
1819 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1820 ZR_DEVNAME(zr));
1821 return -EINVAL;
1822 }
1823
1824 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1825 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1826 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1827 buf->length = fh->jpg_buffers.buffer_size;
1828
1829 /* these variables are only written after frame has been captured */
1830 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1831 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1832 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1833 buf->timestamp =
1834 fh->jpg_buffers.buffer[num].bs.timestamp;
1835 buf->bytesused =
1836 fh->jpg_buffers.buffer[num].bs.length;
1837 buf->flags |= V4L2_BUF_FLAG_DONE;
1838 } else {
1839 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1840 }
1841
1842 /* which fields are these? */
1843 if (fh->jpg_settings.TmpDcm != 1)
1844 buf->field =
1845 fh->jpg_settings.
1846 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1847 else
1848 buf->field =
1849 fh->jpg_settings.
1850 odd_even ? V4L2_FIELD_SEQ_TB :
1851 V4L2_FIELD_SEQ_BT;
1852
1853 break;
1854
1855 default:
1856
1857 dprintk(5,
1858 KERN_ERR
1859 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1860 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1861 return -EINVAL;
1862 }
1863
1864 buf->memory = V4L2_MEMORY_MMAP;
1865 buf->index = num;
1866 buf->m.offset = buf->length * num;
1867
1868 return 0;
1869}
1870#endif
1871
1872static int
1873zoran_set_norm (struct zoran *zr,
1874 int norm) /* VIDEO_MODE_* */
1875{
1876 int norm_encoder, on;
1877
1878 if (zr->v4l_buffers.active != ZORAN_FREE ||
1879 zr->jpg_buffers.active != ZORAN_FREE) {
1880 dprintk(1,
1881 KERN_WARNING
1882 "%s: set_norm() called while in playback/capture mode\n",
1883 ZR_DEVNAME(zr));
1884 return -EBUSY;
1885 }
1886
1887 if (lock_norm && norm != zr->norm) {
1888 if (lock_norm > 1) {
1889 dprintk(1,
1890 KERN_WARNING
1891 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1892 ZR_DEVNAME(zr));
1893 return -EPERM;
1894 } else {
1895 dprintk(1,
1896 KERN_WARNING
1897 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1898 ZR_DEVNAME(zr));
1899 norm = zr->norm;
1900 }
1901 }
1902
1903 if (norm != VIDEO_MODE_AUTO &&
1904 (norm < 0 || norm >= zr->card.norms ||
1905 !zr->card.tvn[norm])) {
1906 dprintk(1,
1907 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1908 ZR_DEVNAME(zr), norm);
1909 return -EINVAL;
1910 }
1911
1912 if (norm == VIDEO_MODE_AUTO) {
1913 int status;
1914
1915 /* if we have autodetect, ... */
1916 struct video_decoder_capability caps;
1917 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1918 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1919 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1920 ZR_DEVNAME(zr));
1921 return -EINVAL;
1922 }
1923
1924 decoder_command(zr, DECODER_SET_NORM, &norm);
1925
1926 /* let changes come into effect */
1927 ssleep(2);
1928
1929 decoder_command(zr, DECODER_GET_STATUS, &status);
1930 if (!(status & DECODER_STATUS_GOOD)) {
1931 dprintk(1,
1932 KERN_ERR
1933 "%s: set_norm() - no norm detected\n",
1934 ZR_DEVNAME(zr));
1935 /* reset norm */
1936 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1937 return -EIO;
1938 }
1939
1940 if (status & DECODER_STATUS_NTSC)
1941 norm = VIDEO_MODE_NTSC;
1942 else if (status & DECODER_STATUS_SECAM)
1943 norm = VIDEO_MODE_SECAM;
1944 else
1945 norm = VIDEO_MODE_PAL;
1946 }
1947 zr->timing = zr->card.tvn[norm];
1948 norm_encoder = norm;
1949
1950 /* We switch overlay off and on since a change in the
1951 * norm needs different VFE settings */
1952 on = zr->overlay_active && !zr->v4l_memgrab_active;
1953 if (on)
1954 zr36057_overlay(zr, 0);
1955
1956 decoder_command(zr, DECODER_SET_NORM, &norm);
1957 encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
1958
1959 if (on)
1960 zr36057_overlay(zr, 1);
1961
1962 /* Make sure the changes come into effect */
1963 zr->norm = norm;
1964
1965 return 0;
1966}
1967
1968static int
1969zoran_set_input (struct zoran *zr,
1970 int input)
1971{
1972 int realinput;
1973
1974 if (input == zr->input) {
1975 return 0;
1976 }
1977
1978 if (zr->v4l_buffers.active != ZORAN_FREE ||
1979 zr->jpg_buffers.active != ZORAN_FREE) {
1980 dprintk(1,
1981 KERN_WARNING
1982 "%s: set_input() called while in playback/capture mode\n",
1983 ZR_DEVNAME(zr));
1984 return -EBUSY;
1985 }
1986
1987 if (input < 0 || input >= zr->card.inputs) {
1988 dprintk(1,
1989 KERN_ERR
1990 "%s: set_input() - unnsupported input %d\n",
1991 ZR_DEVNAME(zr), input);
1992 return -EINVAL;
1993 }
1994
1995 realinput = zr->card.input[input].muxsel;
1996 zr->input = input;
1997
1998 decoder_command(zr, DECODER_SET_INPUT, &realinput);
1999
2000 return 0;
2001}
2002
2003/*
2004 * ioctl routine
2005 */
2006
2007static int
2008zoran_do_ioctl (struct inode *inode,
2009 struct file *file,
2010 unsigned int cmd,
2011 void *arg)
2012{
2013 struct zoran_fh *fh = file->private_data;
2014 struct zoran *zr = fh->zr;
2015 /* CAREFUL: used in multiple places here */
2016 struct zoran_jpg_settings settings;
2017
2018 /* we might have older buffers lying around... We don't want
2019 * to wait, but we do want to try cleaning them up ASAP. So
2020 * we try to obtain the lock and free them. If that fails, we
2021 * don't do anything and wait for the next turn. In the end,
2022 * zoran_close() or a new allocation will still free them...
2023 * This is just a 'the sooner the better' extra 'feature'
2024 *
2025 * We don't free the buffers right on munmap() because that
2026 * causes oopses (kfree() inside munmap() oopses for no
2027 * apparent reason - it's also not reproduceable in any way,
2028 * but moving the free code outside the munmap() handler fixes
2029 * all this... If someone knows why, please explain me (Ronald)
2030 */
2031 if (!down_trylock(&zr->resource_lock)) {
2032 /* we obtained it! Let's try to free some things */
2033 if (fh->jpg_buffers.ready_to_be_freed)
2034 jpg_fbuffer_free(file);
2035 if (fh->v4l_buffers.ready_to_be_freed)
2036 v4l_fbuffer_free(file);
2037
2038 up(&zr->resource_lock);
2039 }
2040
2041 switch (cmd) {
2042
2043 case VIDIOCGCAP:
2044 {
2045 struct video_capability *vcap = arg;
2046
2047 dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
2048
2049 memset(vcap, 0, sizeof(struct video_capability));
2050 strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
2051 vcap->type = ZORAN_VID_TYPE;
2052
2053 vcap->channels = zr->card.inputs;
2054 vcap->audios = 0;
2055 down(&zr->resource_lock);
2056 vcap->maxwidth = BUZ_MAX_WIDTH;
2057 vcap->maxheight = BUZ_MAX_HEIGHT;
2058 vcap->minwidth = BUZ_MIN_WIDTH;
2059 vcap->minheight = BUZ_MIN_HEIGHT;
2060 up(&zr->resource_lock);
2061
2062 return 0;
2063 }
2064 break;
2065
2066 case VIDIOCGCHAN:
2067 {
2068 struct video_channel *vchan = arg;
2069 int channel = vchan->channel;
2070
2071 dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
2072 ZR_DEVNAME(zr), vchan->channel);
2073
2074 memset(vchan, 0, sizeof(struct video_channel));
2075 if (channel > zr->card.inputs || channel < 0) {
2076 dprintk(1,
2077 KERN_ERR
2078 "%s: VIDIOCGCHAN on not existing channel %d\n",
2079 ZR_DEVNAME(zr), channel);
2080 return -EINVAL;
2081 }
2082
2083 strcpy(vchan->name, zr->card.input[channel].name);
2084
2085 vchan->tuners = 0;
2086 vchan->flags = 0;
2087 vchan->type = VIDEO_TYPE_CAMERA;
2088 down(&zr->resource_lock);
2089 vchan->norm = zr->norm;
2090 up(&zr->resource_lock);
2091 vchan->channel = channel;
2092
2093 return 0;
2094 }
2095 break;
2096
2097 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
2098 *
2099 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
2100 * * ^^^^^^^
2101 * * The famos BTTV driver has it implemented with a struct video_channel argument
2102 * * and we follow it for compatibility reasons
2103 * *
2104 * * BTW: this is the only way the user can set the norm!
2105 */
2106
2107 case VIDIOCSCHAN:
2108 {
2109 struct video_channel *vchan = arg;
2110 int res;
2111
2112 dprintk(3,
2113 KERN_DEBUG
2114 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
2115 ZR_DEVNAME(zr), vchan->channel, vchan->norm);
2116
2117 down(&zr->resource_lock);
2118 if ((res = zoran_set_input(zr, vchan->channel)))
2119 goto schan_unlock_and_return;
2120 if ((res = zoran_set_norm(zr, vchan->norm)))
2121 goto schan_unlock_and_return;
2122
2123 /* Make sure the changes come into effect */
2124 res = wait_grab_pending(zr);
2125 schan_unlock_and_return:
2126 up(&zr->resource_lock);
2127 return res;
2128 }
2129 break;
2130
2131 case VIDIOCGPICT:
2132 {
2133 struct video_picture *vpict = arg;
2134
2135 dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
2136
2137 memset(vpict, 0, sizeof(struct video_picture));
2138 down(&zr->resource_lock);
2139 vpict->hue = zr->hue;
2140 vpict->brightness = zr->brightness;
2141 vpict->contrast = zr->contrast;
2142 vpict->colour = zr->saturation;
2143 if (fh->overlay_settings.format) {
2144 vpict->depth = fh->overlay_settings.format->depth;
2145 vpict->palette = fh->overlay_settings.format->palette;
2146 } else {
2147 vpict->depth = 0;
2148 }
2149 up(&zr->resource_lock);
2150
2151 return 0;
2152 }
2153 break;
2154
2155 case VIDIOCSPICT:
2156 {
2157 struct video_picture *vpict = arg;
2158 int i;
2159
2160 dprintk(3,
2161 KERN_DEBUG
2162 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
2163 ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
2164 vpict->colour, vpict->contrast, vpict->depth,
2165 vpict->palette);
2166
2167 for (i = 0; i < zoran_num_formats; i++) {
2168 const struct zoran_format *fmt = &zoran_formats[i];
2169
2170 if (fmt->palette != -1 &&
2171 fmt->flags & ZORAN_FORMAT_OVERLAY &&
2172 fmt->palette == vpict->palette &&
2173 fmt->depth == vpict->depth)
2174 break;
2175 }
2176 if (i == zoran_num_formats) {
2177 dprintk(1,
2178 KERN_ERR
2179 "%s: VIDIOCSPICT - Invalid palette %d\n",
2180 ZR_DEVNAME(zr), vpict->palette);
2181 return -EINVAL;
2182 }
2183
2184 down(&zr->resource_lock);
2185
2186 decoder_command(zr, DECODER_SET_PICTURE, vpict);
2187
2188 zr->hue = vpict->hue;
2189 zr->contrast = vpict->contrast;
2190 zr->saturation = vpict->colour;
2191 zr->brightness = vpict->brightness;
2192
2193 fh->overlay_settings.format = &zoran_formats[i];
2194
2195 up(&zr->resource_lock);
2196
2197 return 0;
2198 }
2199 break;
2200
2201 case VIDIOCCAPTURE:
2202 {
2203 int *on = arg, res;
2204
2205 dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
2206 ZR_DEVNAME(zr), *on);
2207
2208 down(&zr->resource_lock);
2209 res = setup_overlay(file, *on);
2210 up(&zr->resource_lock);
2211
2212 return res;
2213 }
2214 break;
2215
2216 case VIDIOCGWIN:
2217 {
2218 struct video_window *vwin = arg;
2219
2220 dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
2221
2222 memset(vwin, 0, sizeof(struct video_window));
2223 down(&zr->resource_lock);
2224 vwin->x = fh->overlay_settings.x;
2225 vwin->y = fh->overlay_settings.y;
2226 vwin->width = fh->overlay_settings.width;
2227 vwin->height = fh->overlay_settings.height;
2228 up(&zr->resource_lock);
2229 vwin->clipcount = 0;
2230 return 0;
2231 }
2232 break;
2233
2234 case VIDIOCSWIN:
2235 {
2236 struct video_window *vwin = arg;
2237 int res;
2238
2239 dprintk(3,
2240 KERN_DEBUG
2241 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
2242 ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
2243 vwin->height, vwin->clipcount);
2244
2245 down(&zr->resource_lock);
2246 res =
2247 setup_window(file, vwin->x, vwin->y, vwin->width,
2248 vwin->height, vwin->clips,
2249 vwin->clipcount, NULL);
2250 up(&zr->resource_lock);
2251
2252 return res;
2253 }
2254 break;
2255
2256 case VIDIOCGFBUF:
2257 {
2258 struct video_buffer *vbuf = arg;
2259
2260 dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
2261
2262 down(&zr->resource_lock);
2263 *vbuf = zr->buffer;
2264 up(&zr->resource_lock);
2265 return 0;
2266 }
2267 break;
2268
2269 case VIDIOCSFBUF:
2270 {
2271 struct video_buffer *vbuf = arg;
2272 int i, res = 0;
2273
2274 dprintk(3,
2275 KERN_DEBUG
2276 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
2277 ZR_DEVNAME(zr), vbuf->base, vbuf->width,
2278 vbuf->height, vbuf->depth, vbuf->bytesperline);
2279
2280 for (i = 0; i < zoran_num_formats; i++)
2281 if (zoran_formats[i].depth == vbuf->depth)
2282 break;
2283 if (i == zoran_num_formats) {
2284 dprintk(1,
2285 KERN_ERR
2286 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2287 ZR_DEVNAME(zr), vbuf->depth);
2288 return -EINVAL;
2289 }
2290
2291 down(&zr->resource_lock);
2292 res =
2293 setup_fbuffer(file, vbuf->base, &zoran_formats[i],
2294 vbuf->width, vbuf->height,
2295 vbuf->bytesperline);
2296 up(&zr->resource_lock);
2297
2298 return res;
2299 }
2300 break;
2301
2302 case VIDIOCSYNC:
2303 {
2304 int *frame = arg, res;
2305
2306 dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
2307 ZR_DEVNAME(zr), *frame);
2308
2309 down(&zr->resource_lock);
2310 res = v4l_sync(file, *frame);
2311 up(&zr->resource_lock);
2312 if (!res)
2313 zr->v4l_sync_tail++;
2314 return res;
2315 }
2316 break;
2317
2318 case VIDIOCMCAPTURE:
2319 {
2320 struct video_mmap *vmap = arg;
2321 int res;
2322
2323 dprintk(3,
2324 KERN_DEBUG
2325 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2326 ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
2327 vmap->format);
2328
2329 down(&zr->resource_lock);
2330 res = v4l_grab(file, vmap);
2331 up(&zr->resource_lock);
2332 return res;
2333 }
2334 break;
2335
2336 case VIDIOCGMBUF:
2337 {
2338 struct video_mbuf *vmbuf = arg;
2339 int i, res = 0;
2340
2341 dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
2342
2343 vmbuf->size =
2344 fh->v4l_buffers.num_buffers *
2345 fh->v4l_buffers.buffer_size;
2346 vmbuf->frames = fh->v4l_buffers.num_buffers;
2347 for (i = 0; i < vmbuf->frames; i++) {
2348 vmbuf->offsets[i] =
2349 i * fh->v4l_buffers.buffer_size;
2350 }
2351
2352 down(&zr->resource_lock);
2353
2354 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2355 dprintk(1,
2356 KERN_ERR
2357 "%s: VIDIOCGMBUF - buffers already allocated\n",
2358 ZR_DEVNAME(zr));
2359 res = -EINVAL;
2360 goto v4l1reqbuf_unlock_and_return;
2361 }
2362
2363 if (v4l_fbuffer_alloc(file)) {
2364 res = -ENOMEM;
2365 goto v4l1reqbuf_unlock_and_return;
2366 }
2367
2368 /* The next mmap will map the V4L buffers */
2369 fh->map_mode = ZORAN_MAP_MODE_RAW;
2370 v4l1reqbuf_unlock_and_return:
2371 up(&zr->resource_lock);
2372
2373 return res;
2374 }
2375 break;
2376
2377 case VIDIOCGUNIT:
2378 {
2379 struct video_unit *vunit = arg;
2380
2381 dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
2382
2383 vunit->video = zr->video_dev->minor;
2384 vunit->vbi = VIDEO_NO_UNIT;
2385 vunit->radio = VIDEO_NO_UNIT;
2386 vunit->audio = VIDEO_NO_UNIT;
2387 vunit->teletext = VIDEO_NO_UNIT;
2388
2389 return 0;
2390 }
2391 break;
2392
2393 /*
2394 * RJ: In principal we could support subcaptures for V4L grabbing.
2395 * Not even the famous BTTV driver has them, however.
2396 * If there should be a strong demand, one could consider
2397 * to implement them.
2398 */
2399 case VIDIOCGCAPTURE:
2400 {
2401 dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
2402 ZR_DEVNAME(zr));
2403 return -EINVAL;
2404 }
2405 break;
2406
2407 case VIDIOCSCAPTURE:
2408 {
2409 dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
2410 ZR_DEVNAME(zr));
2411 return -EINVAL;
2412 }
2413 break;
2414
2415 case BUZIOC_G_PARAMS:
2416 {
2417 struct zoran_params *bparams = arg;
2418
2419 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
2420
2421 memset(bparams, 0, sizeof(struct zoran_params));
2422 bparams->major_version = MAJOR_VERSION;
2423 bparams->minor_version = MINOR_VERSION;
2424
2425 down(&zr->resource_lock);
2426
2427 bparams->norm = zr->norm;
2428 bparams->input = zr->input;
2429
2430 bparams->decimation = fh->jpg_settings.decimation;
2431 bparams->HorDcm = fh->jpg_settings.HorDcm;
2432 bparams->VerDcm = fh->jpg_settings.VerDcm;
2433 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
2434 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
2435 bparams->img_x = fh->jpg_settings.img_x;
2436 bparams->img_y = fh->jpg_settings.img_y;
2437 bparams->img_width = fh->jpg_settings.img_width;
2438 bparams->img_height = fh->jpg_settings.img_height;
2439 bparams->odd_even = fh->jpg_settings.odd_even;
2440
2441 bparams->quality = fh->jpg_settings.jpg_comp.quality;
2442 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
2443 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2444 memcpy(bparams->APP_data,
2445 fh->jpg_settings.jpg_comp.APP_data,
2446 sizeof(bparams->APP_data));
2447 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
2448 memcpy(bparams->COM_data,
2449 fh->jpg_settings.jpg_comp.COM_data,
2450 sizeof(bparams->COM_data));
2451 bparams->jpeg_markers =
2452 fh->jpg_settings.jpg_comp.jpeg_markers;
2453
2454 up(&zr->resource_lock);
2455
2456 bparams->VFIFO_FB = 0;
2457
2458 return 0;
2459 }
2460 break;
2461
2462 case BUZIOC_S_PARAMS:
2463 {
2464 struct zoran_params *bparams = arg;
2465 int res = 0;
2466
2467 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
2468
2469 settings.decimation = bparams->decimation;
2470 settings.HorDcm = bparams->HorDcm;
2471 settings.VerDcm = bparams->VerDcm;
2472 settings.TmpDcm = bparams->TmpDcm;
2473 settings.field_per_buff = bparams->field_per_buff;
2474 settings.img_x = bparams->img_x;
2475 settings.img_y = bparams->img_y;
2476 settings.img_width = bparams->img_width;
2477 settings.img_height = bparams->img_height;
2478 settings.odd_even = bparams->odd_even;
2479
2480 settings.jpg_comp.quality = bparams->quality;
2481 settings.jpg_comp.APPn = bparams->APPn;
2482 settings.jpg_comp.APP_len = bparams->APP_len;
2483 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
2484 sizeof(bparams->APP_data));
2485 settings.jpg_comp.COM_len = bparams->COM_len;
2486 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
2487 sizeof(bparams->COM_data));
2488 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
2489
2490 down(&zr->resource_lock);
2491
2492 if (zr->codec_mode != BUZ_MODE_IDLE) {
2493 dprintk(1,
2494 KERN_ERR
2495 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2496 ZR_DEVNAME(zr));
2497 res = -EINVAL;
2498 goto sparams_unlock_and_return;
2499 }
2500
2501 /* Check the params first before overwriting our
2502 * nternal values */
2503 if (zoran_check_jpg_settings(zr, &settings)) {
2504 res = -EINVAL;
2505 goto sparams_unlock_and_return;
2506 }
2507
2508 fh->jpg_settings = settings;
2509 sparams_unlock_and_return:
2510 up(&zr->resource_lock);
2511
2512 return res;
2513 }
2514 break;
2515
2516 case BUZIOC_REQBUFS:
2517 {
2518 struct zoran_requestbuffers *breq = arg;
2519 int res = 0;
2520
2521 dprintk(3,
2522 KERN_DEBUG
2523 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2524 ZR_DEVNAME(zr), breq->count, breq->size);
2525
2526 /* Enforce reasonable lower and upper limits */
2527 if (breq->count < 4)
2528 breq->count = 4; /* Could be choosen smaller */
2529 if (breq->count > jpg_nbufs)
2530 breq->count = jpg_nbufs;
2531 breq->size = PAGE_ALIGN(breq->size);
2532 if (breq->size < 8192)
2533 breq->size = 8192; /* Arbitrary */
2534 /* breq->size is limited by 1 page for the stat_com
2535 * tables to a Maximum of 2 MB */
2536 if (breq->size > jpg_bufsize)
2537 breq->size = jpg_bufsize;
2538 if (fh->jpg_buffers.need_contiguous &&
2539 breq->size > MAX_KMALLOC_MEM)
2540 breq->size = MAX_KMALLOC_MEM;
2541
2542 down(&zr->resource_lock);
2543
2544 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
2545 dprintk(1,
2546 KERN_ERR
2547 "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
2548 ZR_DEVNAME(zr));
2549 res = -EBUSY;
2550 goto jpgreqbuf_unlock_and_return;
2551 }
2552
2553 fh->jpg_buffers.num_buffers = breq->count;
2554 fh->jpg_buffers.buffer_size = breq->size;
2555
2556 if (jpg_fbuffer_alloc(file)) {
2557 res = -ENOMEM;
2558 goto jpgreqbuf_unlock_and_return;
2559 }
2560
2561 /* The next mmap will map the MJPEG buffers - could
2562 * also be *_PLAY, but it doesn't matter here */
2563 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
2564 jpgreqbuf_unlock_and_return:
2565 up(&zr->resource_lock);
2566
2567 return res;
2568 }
2569 break;
2570
2571 case BUZIOC_QBUF_CAPT:
2572 {
2573 int *frame = arg, res;
2574
2575 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2576 ZR_DEVNAME(zr), *frame);
2577
2578 down(&zr->resource_lock);
2579 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
2580 up(&zr->resource_lock);
2581
2582 return res;
2583 }
2584 break;
2585
2586 case BUZIOC_QBUF_PLAY:
2587 {
2588 int *frame = arg, res;
2589
2590 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2591 ZR_DEVNAME(zr), *frame);
2592
2593 down(&zr->resource_lock);
2594 res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
2595 up(&zr->resource_lock);
2596
2597 return res;
2598 }
2599 break;
2600
2601 case BUZIOC_SYNC:
2602 {
2603 struct zoran_sync *bsync = arg;
2604 int res;
2605
2606 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
2607
2608 down(&zr->resource_lock);
2609 res = jpg_sync(file, bsync);
2610 up(&zr->resource_lock);
2611
2612 return res;
2613 }
2614 break;
2615
2616 case BUZIOC_G_STATUS:
2617 {
2618 struct zoran_status *bstat = arg;
2619 int norm, input, status, res = 0;
2620
2621 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
2622
2623 if (zr->codec_mode != BUZ_MODE_IDLE) {
2624 dprintk(1,
2625 KERN_ERR
2626 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2627 ZR_DEVNAME(zr));
2628 return -EINVAL;
2629 }
2630
2631 input = zr->card.input[bstat->input].muxsel;
2632 norm = VIDEO_MODE_AUTO;
2633
2634 down(&zr->resource_lock);
2635
2636 if (zr->codec_mode != BUZ_MODE_IDLE) {
2637 dprintk(1,
2638 KERN_ERR
2639 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2640 ZR_DEVNAME(zr));
2641 res = -EINVAL;
2642 goto gstat_unlock_and_return;
2643 }
2644
2645 decoder_command(zr, DECODER_SET_INPUT, &input);
2646 decoder_command(zr, DECODER_SET_NORM, &norm);
2647
2648 /* sleep 1 second */
2649 ssleep(1);
2650
2651 /* Get status of video decoder */
2652 decoder_command(zr, DECODER_GET_STATUS, &status);
2653
2654 /* restore previous input and norm */
2655 input = zr->card.input[zr->input].muxsel;
2656 decoder_command(zr, DECODER_SET_INPUT, &input);
2657 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
2658 gstat_unlock_and_return:
2659 up(&zr->resource_lock);
2660
2661 if (!res) {
2662 bstat->signal =
2663 (status & DECODER_STATUS_GOOD) ? 1 : 0;
2664 if (status & DECODER_STATUS_NTSC)
2665 bstat->norm = VIDEO_MODE_NTSC;
2666 else if (status & DECODER_STATUS_SECAM)
2667 bstat->norm = VIDEO_MODE_SECAM;
2668 else
2669 bstat->norm = VIDEO_MODE_PAL;
2670
2671 bstat->color =
2672 (status & DECODER_STATUS_COLOR) ? 1 : 0;
2673 }
2674
2675 return res;
2676 }
2677 break;
2678
2679#ifdef HAVE_V4L2
2680
2681 /* The new video4linux2 capture interface - much nicer than video4linux1, since
2682 * it allows for integrating the JPEG capturing calls inside standard v4l2
2683 */
2684
2685 case VIDIOC_QUERYCAP:
2686 {
2687 struct v4l2_capability *cap = arg;
2688
2689 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
2690
2691 memset(cap, 0, sizeof(*cap));
2692 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
2693 strncpy(cap->driver, "zoran", sizeof(cap->driver));
2694 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
2695 pci_name(zr->pci_dev));
2696 cap->version =
2697 KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
2698 RELEASE_VERSION);
2699 cap->capabilities = ZORAN_V4L2_VID_FLAGS;
2700
2701 return 0;
2702 }
2703 break;
2704
2705 case VIDIOC_ENUM_FMT:
2706 {
2707 struct v4l2_fmtdesc *fmt = arg;
2708 int index = fmt->index, num = -1, i, flag = 0, type =
2709 fmt->type;
2710
2711 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
2712 ZR_DEVNAME(zr), fmt->index);
2713
2714 switch (fmt->type) {
2715 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2716 flag = ZORAN_FORMAT_CAPTURE;
2717 break;
2718 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2719 flag = ZORAN_FORMAT_PLAYBACK;
2720 break;
2721 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2722 flag = ZORAN_FORMAT_OVERLAY;
2723 break;
2724 default:
2725 dprintk(1,
2726 KERN_ERR
2727 "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
2728 ZR_DEVNAME(zr), fmt->type);
2729 return -EINVAL;
2730 }
2731
2732 for (i = 0; i < zoran_num_formats; i++) {
2733 if (zoran_formats[i].flags & flag)
2734 num++;
2735 if (num == fmt->index)
2736 break;
2737 }
2738 if (fmt->index < 0 /* late, but not too late */ ||
2739 i == zoran_num_formats)
2740 return -EINVAL;
2741
2742 memset(fmt, 0, sizeof(*fmt));
2743 fmt->index = index;
2744 fmt->type = type;
2745 strncpy(fmt->description, zoran_formats[i].name, 31);
2746 fmt->pixelformat = zoran_formats[i].fourcc;
2747 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
2748 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
2749
2750 return 0;
2751 }
2752 break;
2753
2754 case VIDIOC_G_FMT:
2755 {
2756 struct v4l2_format *fmt = arg;
2757 int type = fmt->type;
2758
2759 dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
2760
2761 memset(fmt, 0, sizeof(*fmt));
2762 fmt->type = type;
2763
2764 switch (fmt->type) {
2765 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2766
2767 down(&zr->resource_lock);
2768
2769 fmt->fmt.win.w.left = fh->overlay_settings.x;
2770 fmt->fmt.win.w.top = fh->overlay_settings.y;
2771 fmt->fmt.win.w.width = fh->overlay_settings.width;
2772 fmt->fmt.win.w.height =
2773 fh->overlay_settings.height;
2774 if (fh->overlay_settings.width * 2 >
2775 BUZ_MAX_HEIGHT)
2776 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
2777 else
2778 fmt->fmt.win.field = V4L2_FIELD_TOP;
2779
2780 up(&zr->resource_lock);
2781
2782 break;
2783
2784 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2785 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2786
2787 down(&zr->resource_lock);
2788
2789 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2790 fh->map_mode == ZORAN_MAP_MODE_RAW) {
2791
2792 fmt->fmt.pix.width =
2793 fh->v4l_settings.width;
2794 fmt->fmt.pix.height =
2795 fh->v4l_settings.height;
2796 fmt->fmt.pix.sizeimage =
2797 fh->v4l_buffers.buffer_size;
2798 fmt->fmt.pix.pixelformat =
2799 fh->v4l_settings.format->fourcc;
2800 fmt->fmt.pix.colorspace =
2801 fh->v4l_settings.format->colorspace;
2802 fmt->fmt.pix.bytesperline = 0;
2803 if (BUZ_MAX_HEIGHT <
2804 (fh->v4l_settings.height * 2))
2805 fmt->fmt.pix.field =
2806 V4L2_FIELD_INTERLACED;
2807 else
2808 fmt->fmt.pix.field =
2809 V4L2_FIELD_TOP;
2810
2811 } else {
2812
2813 fmt->fmt.pix.width =
2814 fh->jpg_settings.img_width /
2815 fh->jpg_settings.HorDcm;
2816 fmt->fmt.pix.height =
2817 fh->jpg_settings.img_height /
2818 (fh->jpg_settings.VerDcm *
2819 fh->jpg_settings.TmpDcm);
2820 fmt->fmt.pix.sizeimage =
2821 zoran_v4l2_calc_bufsize(&fh->
2822 jpg_settings);
2823 fmt->fmt.pix.pixelformat =
2824 V4L2_PIX_FMT_MJPEG;
2825 if (fh->jpg_settings.TmpDcm == 1)
2826 fmt->fmt.pix.field =
2827 (fh->jpg_settings.
2828 odd_even ? V4L2_FIELD_SEQ_BT :
2829 V4L2_FIELD_SEQ_BT);
2830 else
2831 fmt->fmt.pix.field =
2832 (fh->jpg_settings.
2833 odd_even ? V4L2_FIELD_TOP :
2834 V4L2_FIELD_BOTTOM);
2835
2836 fmt->fmt.pix.bytesperline = 0;
2837 fmt->fmt.pix.colorspace =
2838 V4L2_COLORSPACE_SMPTE170M;
2839 }
2840
2841 up(&zr->resource_lock);
2842
2843 break;
2844
2845 default:
2846 dprintk(1,
2847 KERN_ERR
2848 "%s: VIDIOC_G_FMT - unsupported type %d\n",
2849 ZR_DEVNAME(zr), fmt->type);
2850 return -EINVAL;
2851 }
2852 return 0;
2853 }
2854 break;
2855
2856 case VIDIOC_S_FMT:
2857 {
2858 struct v4l2_format *fmt = arg;
2859 int i, res = 0;
2860 __u32 printformat;
2861
2862 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
2863 ZR_DEVNAME(zr), fmt->type);
2864
2865 switch (fmt->type) {
2866 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2867
2868 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2869 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2870 fmt->fmt.win.w.width,
2871 fmt->fmt.win.w.height,
2872 fmt->fmt.win.clipcount,
2873 fmt->fmt.win.bitmap);
2874 down(&zr->resource_lock);
2875 res =
2876 setup_window(file, fmt->fmt.win.w.left,
2877 fmt->fmt.win.w.top,
2878 fmt->fmt.win.w.width,
2879 fmt->fmt.win.w.height,
2880 (struct video_clip __user *)
2881 fmt->fmt.win.clips,
2882 fmt->fmt.win.clipcount,
2883 fmt->fmt.win.bitmap);
2884 up(&zr->resource_lock);
2885 return res;
2886 break;
2887
2888 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2889 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2890
2891 printformat =
2892 __cpu_to_le32(fmt->fmt.pix.pixelformat);
2893 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2894 fmt->fmt.pix.width, fmt->fmt.pix.height,
2895 fmt->fmt.pix.pixelformat,
2896 (char *) &printformat);
2897
2898 if (fmt->fmt.pix.bytesperline > 0) {
2899 dprintk(5,
2900 KERN_ERR "%s: bpl not supported\n",
2901 ZR_DEVNAME(zr));
2902 return -EINVAL;
2903 }
2904
2905 /* we can be requested to do JPEG/raw playback/capture */
2906 if (!
2907 (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2908 (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2909 fmt->fmt.pix.pixelformat ==
2910 V4L2_PIX_FMT_MJPEG))) {
2911 dprintk(1,
2912 KERN_ERR
2913 "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
2914 ZR_DEVNAME(zr), fmt->type,
2915 fmt->fmt.pix.pixelformat,
2916 (char *) &printformat);
2917 return -EINVAL;
2918 }
2919
2920 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
2921 down(&zr->resource_lock);
2922
2923 settings = fh->jpg_settings;
2924
2925 if (fh->v4l_buffers.allocated ||
2926 fh->jpg_buffers.allocated) {
2927 dprintk(1,
2928 KERN_ERR
2929 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2930 ZR_DEVNAME(zr));
2931 res = -EBUSY;
2932 goto sfmtjpg_unlock_and_return;
2933 }
2934
2935 /* we actually need to set 'real' parameters now */
2936 if ((fmt->fmt.pix.height * 2) >
2937 BUZ_MAX_HEIGHT)
2938 settings.TmpDcm = 1;
2939 else
2940 settings.TmpDcm = 2;
2941 settings.decimation = 0;
2942 if (fmt->fmt.pix.height <=
2943 fh->jpg_settings.img_height / 2)
2944 settings.VerDcm = 2;
2945 else
2946 settings.VerDcm = 1;
2947 if (fmt->fmt.pix.width <=
2948 fh->jpg_settings.img_width / 4)
2949 settings.HorDcm = 4;
2950 else if (fmt->fmt.pix.width <=
2951 fh->jpg_settings.img_width / 2)
2952 settings.HorDcm = 2;
2953 else
2954 settings.HorDcm = 1;
2955 if (settings.TmpDcm == 1)
2956 settings.field_per_buff = 2;
2957 else
2958 settings.field_per_buff = 1;
2959
2960 /* check */
2961 if ((res =
2962 zoran_check_jpg_settings(zr,
2963 &settings)))
2964 goto sfmtjpg_unlock_and_return;
2965
2966 /* it's ok, so set them */
2967 fh->jpg_settings = settings;
2968
2969 /* tell the user what we actually did */
2970 fmt->fmt.pix.width =
2971 settings.img_width / settings.HorDcm;
2972 fmt->fmt.pix.height =
2973 settings.img_height * 2 /
2974 (settings.TmpDcm * settings.VerDcm);
2975 if (settings.TmpDcm == 1)
2976 fmt->fmt.pix.field =
2977 (fh->jpg_settings.
2978 odd_even ? V4L2_FIELD_SEQ_TB :
2979 V4L2_FIELD_SEQ_BT);
2980 else
2981 fmt->fmt.pix.field =
2982 (fh->jpg_settings.
2983 odd_even ? V4L2_FIELD_TOP :
2984 V4L2_FIELD_BOTTOM);
2985 fh->jpg_buffers.buffer_size =
2986 zoran_v4l2_calc_bufsize(&fh->
2987 jpg_settings);
2988 fmt->fmt.pix.sizeimage =
2989 fh->jpg_buffers.buffer_size;
2990
2991 /* we hereby abuse this variable to show that
2992 * we're gonna do mjpeg capture */
2993 fh->map_mode =
2994 (fmt->type ==
2995 V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
2996 ZORAN_MAP_MODE_JPG_REC :
2997 ZORAN_MAP_MODE_JPG_PLAY;
2998 sfmtjpg_unlock_and_return:
2999 up(&zr->resource_lock);
3000 } else {
3001 for (i = 0; i < zoran_num_formats; i++)
3002 if (fmt->fmt.pix.pixelformat ==
3003 zoran_formats[i].fourcc)
3004 break;
3005 if (i == zoran_num_formats) {
3006 dprintk(1,
3007 KERN_ERR
3008 "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
3009 ZR_DEVNAME(zr),
3010 fmt->fmt.pix.pixelformat,
3011 (char *) &printformat);
3012 return -EINVAL;
3013 }
3014 down(&zr->resource_lock);
3015 if (fh->jpg_buffers.allocated ||
3016 (fh->v4l_buffers.allocated &&
3017 fh->v4l_buffers.active !=
3018 ZORAN_FREE)) {
3019 dprintk(1,
3020 KERN_ERR
3021 "%s: VIDIOC_S_FMT - cannot change capture mode\n",
3022 ZR_DEVNAME(zr));
3023 res = -EBUSY;
3024 goto sfmtv4l_unlock_and_return;
3025 }
3026 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
3027 fmt->fmt.pix.height =
3028 BUZ_MAX_HEIGHT;
3029 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
3030 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
3031
3032 if ((res =
3033 zoran_v4l_set_format(file,
3034 fmt->fmt.pix.
3035 width,
3036 fmt->fmt.pix.
3037 height,
3038 &zoran_formats
3039 [i])))
3040 goto sfmtv4l_unlock_and_return;
3041
3042 /* tell the user the
3043 * results/missing stuff */
3044 fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
3045 ;
3046 if (BUZ_MAX_HEIGHT <
3047 (fh->v4l_settings.height * 2))
3048 fmt->fmt.pix.field =
3049 V4L2_FIELD_INTERLACED;
3050 else
3051 fmt->fmt.pix.field =
3052 V4L2_FIELD_TOP;
3053
3054 fh->map_mode = ZORAN_MAP_MODE_RAW;
3055 sfmtv4l_unlock_and_return:
3056 up(&zr->resource_lock);
3057 }
3058
3059 break;
3060
3061 default:
3062 dprintk(3, "unsupported\n");
3063 dprintk(1,
3064 KERN_ERR
3065 "%s: VIDIOC_S_FMT - unsupported type %d\n",
3066 ZR_DEVNAME(zr), fmt->type);
3067 return -EINVAL;
3068 }
3069
3070 return res;
3071 }
3072 break;
3073
3074 case VIDIOC_G_FBUF:
3075 {
3076 struct v4l2_framebuffer *fb = arg;
3077
3078 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
3079
3080 memset(fb, 0, sizeof(*fb));
3081 down(&zr->resource_lock);
3082 fb->base = zr->buffer.base;
3083 fb->fmt.width = zr->buffer.width;
3084 fb->fmt.height = zr->buffer.height;
3085 if (zr->overlay_settings.format) {
3086 fb->fmt.pixelformat =
3087 fh->overlay_settings.format->fourcc;
3088 }
3089 fb->fmt.bytesperline = zr->buffer.bytesperline;
3090 up(&zr->resource_lock);
3091 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
3092 fb->fmt.field = V4L2_FIELD_INTERLACED;
3093 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
3094 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3095
3096 return 0;
3097 }
3098 break;
3099
3100 case VIDIOC_S_FBUF:
3101 {
3102 int i, res = 0;
3103 struct v4l2_framebuffer *fb = arg;
3104 __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
3105
3106 dprintk(3,
3107 KERN_DEBUG
3108 "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
3109 ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
3110 fb->fmt.bytesperline, fb->fmt.pixelformat,
3111 (char *) &printformat);
3112
3113 for (i = 0; i < zoran_num_formats; i++)
3114 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
3115 break;
3116 if (i == zoran_num_formats) {
3117 dprintk(1,
3118 KERN_ERR
3119 "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
3120 ZR_DEVNAME(zr), fb->fmt.pixelformat,
3121 (char *) &printformat);
3122 return -EINVAL;
3123 }
3124
3125 down(&zr->resource_lock);
3126 res =
3127 setup_fbuffer(file, fb->base, &zoran_formats[i],
3128 fb->fmt.width, fb->fmt.height,
3129 fb->fmt.bytesperline);
3130 up(&zr->resource_lock);
3131
3132 return res;
3133 }
3134 break;
3135
3136 case VIDIOC_OVERLAY:
3137 {
3138 int *on = arg, res;
3139
3140 dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
3141 ZR_DEVNAME(zr), *on);
3142
3143 down(&zr->resource_lock);
3144 res = setup_overlay(file, *on);
3145 up(&zr->resource_lock);
3146
3147 return res;
3148 }
3149 break;
3150
3151 case VIDIOC_REQBUFS:
3152 {
3153 struct v4l2_requestbuffers *req = arg;
3154 int res = 0;
3155
3156 dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
3157 ZR_DEVNAME(zr), req->type);
3158
3159 if (req->memory != V4L2_MEMORY_MMAP) {
3160 dprintk(1,
3161 KERN_ERR
3162 "%s: only MEMORY_MMAP capture is supported, not %d\n",
3163 ZR_DEVNAME(zr), req->memory);
3164 return -EINVAL;
3165 }
3166
3167 down(&zr->resource_lock);
3168
3169 if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
3170 dprintk(1,
3171 KERN_ERR
3172 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
3173 ZR_DEVNAME(zr));
3174 res = -EBUSY;
3175 goto v4l2reqbuf_unlock_and_return;
3176 }
3177
3178 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
3179 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3180
3181 /* control user input */
3182 if (req->count < 2)
3183 req->count = 2;
3184 if (req->count > v4l_nbufs)
3185 req->count = v4l_nbufs;
3186 fh->v4l_buffers.num_buffers = req->count;
3187
3188 if (v4l_fbuffer_alloc(file)) {
3189 res = -ENOMEM;
3190 goto v4l2reqbuf_unlock_and_return;
3191 }
3192
3193 /* The next mmap will map the V4L buffers */
3194 fh->map_mode = ZORAN_MAP_MODE_RAW;
3195
3196 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
3197 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3198
3199 /* we need to calculate size ourselves now */
3200 if (req->count < 4)
3201 req->count = 4;
3202 if (req->count > jpg_nbufs)
3203 req->count = jpg_nbufs;
3204 fh->jpg_buffers.num_buffers = req->count;
3205 fh->jpg_buffers.buffer_size =
3206 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3207
3208 if (jpg_fbuffer_alloc(file)) {
3209 res = -ENOMEM;
3210 goto v4l2reqbuf_unlock_and_return;
3211 }
3212
3213 /* The next mmap will map the MJPEG buffers */
3214 if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
3215 fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
3216 else
3217 fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
3218
3219 } else {
3220 dprintk(1,
3221 KERN_ERR
3222 "%s: VIDIOC_REQBUFS - unknown type %d\n",
3223 ZR_DEVNAME(zr), req->type);
3224 res = -EINVAL;
3225 goto v4l2reqbuf_unlock_and_return;
3226 }
3227 v4l2reqbuf_unlock_and_return:
3228 up(&zr->resource_lock);
3229
3230 return 0;
3231 }
3232 break;
3233
3234 case VIDIOC_QUERYBUF:
3235 {
3236 struct v4l2_buffer *buf = arg;
3237 __u32 type = buf->type;
3238 int index = buf->index, res;
3239
3240 dprintk(3,
3241 KERN_DEBUG
3242 "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
3243 ZR_DEVNAME(zr), buf->index, buf->type);
3244
3245 memset(buf, 0, sizeof(buf));
3246 buf->type = type;
3247 buf->index = index;
3248
3249 down(&zr->resource_lock);
3250 res = zoran_v4l2_buffer_status(file, buf, buf->index);
3251 up(&zr->resource_lock);
3252
3253 return res;
3254 }
3255 break;
3256
3257 case VIDIOC_QBUF:
3258 {
3259 struct v4l2_buffer *buf = arg;
3260 int res = 0, codec_mode, buf_type;
3261
3262 dprintk(3,
3263 KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
3264 ZR_DEVNAME(zr), buf->type, buf->index);
3265
3266 down(&zr->resource_lock);
3267
3268 switch (fh->map_mode) {
3269 case ZORAN_MAP_MODE_RAW:
3270 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3271 dprintk(1,
3272 KERN_ERR
3273 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3274 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3275 res = -EINVAL;
3276 goto qbuf_unlock_and_return;
3277 }
3278
3279 res = zoran_v4l_queue_frame(file, buf->index);
3280 if (res)
3281 goto qbuf_unlock_and_return;
3282 if (!zr->v4l_memgrab_active &&
3283 fh->v4l_buffers.active == ZORAN_LOCKED)
3284 zr36057_set_memgrab(zr, 1);
3285 break;
3286
3287 case ZORAN_MAP_MODE_JPG_REC:
3288 case ZORAN_MAP_MODE_JPG_PLAY:
3289 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
3290 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3291 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
3292 } else {
3293 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3294 codec_mode = BUZ_MODE_MOTION_COMPRESS;
3295 }
3296
3297 if (buf->type != buf_type) {
3298 dprintk(1,
3299 KERN_ERR
3300 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3301 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3302 res = -EINVAL;
3303 goto qbuf_unlock_and_return;
3304 }
3305
3306 res =
3307 zoran_jpg_queue_frame(file, buf->index,
3308 codec_mode);
3309 if (res != 0)
3310 goto qbuf_unlock_and_return;
3311 if (zr->codec_mode == BUZ_MODE_IDLE &&
3312 fh->jpg_buffers.active == ZORAN_LOCKED) {
3313 zr36057_enable_jpg(zr, codec_mode);
3314 }
3315 break;
3316
3317 default:
3318 dprintk(1,
3319 KERN_ERR
3320 "%s: VIDIOC_QBUF - unsupported type %d\n",
3321 ZR_DEVNAME(zr), buf->type);
3322 res = -EINVAL;
3323 goto qbuf_unlock_and_return;
3324 }
3325 qbuf_unlock_and_return:
3326 up(&zr->resource_lock);
3327
3328 return res;
3329 }
3330 break;
3331
3332 case VIDIOC_DQBUF:
3333 {
3334 struct v4l2_buffer *buf = arg;
3335 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
3336
3337 dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
3338 ZR_DEVNAME(zr), buf->type);
3339
3340 down(&zr->resource_lock);
3341
3342 switch (fh->map_mode) {
3343 case ZORAN_MAP_MODE_RAW:
3344 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3345 dprintk(1,
3346 KERN_ERR
3347 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3348 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3349 res = -EINVAL;
3350 goto dqbuf_unlock_and_return;
3351 }
3352
3353 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3354 if (file->f_flags & O_NONBLOCK &&
3355 zr->v4l_buffers.buffer[num].state !=
3356 BUZ_STATE_DONE) {
3357 res = -EAGAIN;
3358 goto dqbuf_unlock_and_return;
3359 }
3360 res = v4l_sync(file, num);
3361 if (res)
3362 goto dqbuf_unlock_and_return;
3363 else
3364 zr->v4l_sync_tail++;
3365 res = zoran_v4l2_buffer_status(file, buf, num);
3366 break;
3367
3368 case ZORAN_MAP_MODE_JPG_REC:
3369 case ZORAN_MAP_MODE_JPG_PLAY:
3370 {
3371 struct zoran_sync bs;
3372
3373 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
3374 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3375 else
3376 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3377
3378 if (buf->type != buf_type) {
3379 dprintk(1,
3380 KERN_ERR
3381 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3382 ZR_DEVNAME(zr), buf->type, fh->map_mode);
3383 res = -EINVAL;
3384 goto dqbuf_unlock_and_return;
3385 }
3386
3387 num =
3388 zr->jpg_pend[zr->
3389 jpg_que_tail & BUZ_MASK_FRAME];
3390
3391 if (file->f_flags & O_NONBLOCK &&
3392 zr->jpg_buffers.buffer[num].state !=
3393 BUZ_STATE_DONE) {
3394 res = -EAGAIN;
3395 goto dqbuf_unlock_and_return;
3396 }
3397 res = jpg_sync(file, &bs);
3398 if (res)
3399 goto dqbuf_unlock_and_return;
3400 res =
3401 zoran_v4l2_buffer_status(file, buf, bs.frame);
3402 break;
3403 }
3404
3405 default:
3406 dprintk(1,
3407 KERN_ERR
3408 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3409 ZR_DEVNAME(zr), buf->type);
3410 res = -EINVAL;
3411 goto dqbuf_unlock_and_return;
3412 }
3413 dqbuf_unlock_and_return:
3414 up(&zr->resource_lock);
3415
3416 return res;
3417 }
3418 break;
3419
3420 case VIDIOC_STREAMON:
3421 {
3422 int res = 0;
3423
3424 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
3425
3426 down(&zr->resource_lock);
3427
3428 switch (fh->map_mode) {
3429 case ZORAN_MAP_MODE_RAW: /* raw capture */
3430 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
3431 fh->v4l_buffers.active != ZORAN_ACTIVE) {
3432 res = -EBUSY;
3433 goto strmon_unlock_and_return;
3434 }
3435
3436 zr->v4l_buffers.active = fh->v4l_buffers.active =
3437 ZORAN_LOCKED;
3438 zr->v4l_settings = fh->v4l_settings;
3439
3440 zr->v4l_sync_tail = zr->v4l_pend_tail;
3441 if (!zr->v4l_memgrab_active &&
3442 zr->v4l_pend_head != zr->v4l_pend_tail) {
3443 zr36057_set_memgrab(zr, 1);
3444 }
3445 break;
3446
3447 case ZORAN_MAP_MODE_JPG_REC:
3448 case ZORAN_MAP_MODE_JPG_PLAY:
3449 /* what is the codec mode right now? */
3450 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
3451 fh->jpg_buffers.active != ZORAN_ACTIVE) {
3452 res = -EBUSY;
3453 goto strmon_unlock_and_return;
3454 }
3455
3456 zr->jpg_buffers.active = fh->jpg_buffers.active =
3457 ZORAN_LOCKED;
3458
3459 if (zr->jpg_que_head != zr->jpg_que_tail) {
3460 /* Start the jpeg codec when the first frame is queued */
3461 jpeg_start(zr);
3462 }
3463
3464 break;
3465 default:
3466 dprintk(1,
3467 KERN_ERR
3468 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3469 ZR_DEVNAME(zr), fh->map_mode);
3470 res = -EINVAL;
3471 goto strmon_unlock_and_return;
3472 }
3473 strmon_unlock_and_return:
3474 up(&zr->resource_lock);
3475
3476 return res;
3477 }
3478 break;
3479
3480 case VIDIOC_STREAMOFF:
3481 {
3482 int i, res = 0;
3483
3484 dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
3485
3486 down(&zr->resource_lock);
3487
3488 switch (fh->map_mode) {
3489 case ZORAN_MAP_MODE_RAW: /* raw capture */
3490 if (fh->v4l_buffers.active == ZORAN_FREE &&
3491 zr->v4l_buffers.active != ZORAN_FREE) {
3492 res = -EPERM; /* stay off other's settings! */
3493 goto strmoff_unlock_and_return;
3494 }
3495 if (zr->v4l_buffers.active == ZORAN_FREE)
3496 goto strmoff_unlock_and_return;
3497
3498 /* unload capture */
3499 if (zr->v4l_memgrab_active)
3500 zr36057_set_memgrab(zr, 0);
3501
3502 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
3503 zr->v4l_buffers.buffer[i].state =
3504 BUZ_STATE_USER;
3505 fh->v4l_buffers = zr->v4l_buffers;
3506
3507 zr->v4l_buffers.active = fh->v4l_buffers.active =
3508 ZORAN_FREE;
3509
3510 zr->v4l_grab_seq = 0;
3511 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
3512 zr->v4l_sync_tail = 0;
3513
3514 break;
3515
3516 case ZORAN_MAP_MODE_JPG_REC:
3517 case ZORAN_MAP_MODE_JPG_PLAY:
3518 if (fh->jpg_buffers.active == ZORAN_FREE &&
3519 zr->jpg_buffers.active != ZORAN_FREE) {
3520 res = -EPERM; /* stay off other's settings! */
3521 goto strmoff_unlock_and_return;
3522 }
3523 if (zr->jpg_buffers.active == ZORAN_FREE)
3524 goto strmoff_unlock_and_return;
3525
3526 res =
3527 jpg_qbuf(file, -1,
3528 (fh->map_mode ==
3529 ZORAN_MAP_MODE_JPG_REC) ?
3530 BUZ_MODE_MOTION_COMPRESS :
3531 BUZ_MODE_MOTION_DECOMPRESS);
3532 if (res)
3533 goto strmoff_unlock_and_return;
3534 break;
3535 default:
3536 dprintk(1,
3537 KERN_ERR
3538 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3539 ZR_DEVNAME(zr), fh->map_mode);
3540 res = -EINVAL;
3541 goto strmoff_unlock_and_return;
3542 }
3543 strmoff_unlock_and_return:
3544 up(&zr->resource_lock);
3545
3546 return res;
3547 }
3548 break;
3549
3550 case VIDIOC_QUERYCTRL:
3551 {
3552 struct v4l2_queryctrl *ctrl = arg;
3553
3554 dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
3555 ZR_DEVNAME(zr), ctrl->id);
3556
3557 /* we only support hue/saturation/contrast/brightness */
3558 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3559 ctrl->id > V4L2_CID_HUE)
3560 return -EINVAL;
3561 else {
3562 int id = ctrl->id;
3563 memset(ctrl, 0, sizeof(*ctrl));
3564 ctrl->id = id;
3565 }
3566
3567 switch (ctrl->id) {
3568 case V4L2_CID_BRIGHTNESS:
3569 strncpy(ctrl->name, "Brightness", 31);
3570 break;
3571 case V4L2_CID_CONTRAST:
3572 strncpy(ctrl->name, "Contrast", 31);
3573 break;
3574 case V4L2_CID_SATURATION:
3575 strncpy(ctrl->name, "Saturation", 31);
3576 break;
3577 case V4L2_CID_HUE:
3578 strncpy(ctrl->name, "Hue", 31);
3579 break;
3580 }
3581
3582 ctrl->minimum = 0;
3583 ctrl->maximum = 65535;
3584 ctrl->step = 1;
3585 ctrl->default_value = 32768;
3586 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
3587
3588 return 0;
3589 }
3590 break;
3591
3592 case VIDIOC_G_CTRL:
3593 {
3594 struct v4l2_control *ctrl = arg;
3595
3596 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
3597 ZR_DEVNAME(zr), ctrl->id);
3598
3599 /* we only support hue/saturation/contrast/brightness */
3600 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3601 ctrl->id > V4L2_CID_HUE)
3602 return -EINVAL;
3603
3604 down(&zr->resource_lock);
3605 switch (ctrl->id) {
3606 case V4L2_CID_BRIGHTNESS:
3607 ctrl->value = zr->brightness;
3608 break;
3609 case V4L2_CID_CONTRAST:
3610 ctrl->value = zr->contrast;
3611 break;
3612 case V4L2_CID_SATURATION:
3613 ctrl->value = zr->saturation;
3614 break;
3615 case V4L2_CID_HUE:
3616 ctrl->value = zr->hue;
3617 break;
3618 }
3619 up(&zr->resource_lock);
3620
3621 return 0;
3622 }
3623 break;
3624
3625 case VIDIOC_S_CTRL:
3626 {
3627 struct v4l2_control *ctrl = arg;
3628 struct video_picture pict;
3629
3630 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
3631 ZR_DEVNAME(zr), ctrl->id);
3632
3633 /* we only support hue/saturation/contrast/brightness */
3634 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
3635 ctrl->id > V4L2_CID_HUE)
3636 return -EINVAL;
3637
3638 if (ctrl->value < 0 || ctrl->value > 65535) {
3639 dprintk(1,
3640 KERN_ERR
3641 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3642 ZR_DEVNAME(zr), ctrl->value, ctrl->id);
3643 return -EINVAL;
3644 }
3645
3646 down(&zr->resource_lock);
3647 switch (ctrl->id) {
3648 case V4L2_CID_BRIGHTNESS:
3649 zr->brightness = ctrl->value;
3650 break;
3651 case V4L2_CID_CONTRAST:
3652 zr->contrast = ctrl->value;
3653 break;
3654 case V4L2_CID_SATURATION:
3655 zr->saturation = ctrl->value;
3656 break;
3657 case V4L2_CID_HUE:
3658 zr->hue = ctrl->value;
3659 break;
3660 }
3661 pict.brightness = zr->brightness;
3662 pict.contrast = zr->contrast;
3663 pict.colour = zr->saturation;
3664 pict.hue = zr->hue;
3665
3666 decoder_command(zr, DECODER_SET_PICTURE, &pict);
3667
3668 up(&zr->resource_lock);
3669
3670 return 0;
3671 }
3672 break;
3673
3674 case VIDIOC_ENUMSTD:
3675 {
3676 struct v4l2_standard *std = arg;
3677
3678 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
3679 ZR_DEVNAME(zr), std->index);
3680
3681 if (std->index < 0 || std->index >= (zr->card.norms + 1))
3682 return -EINVAL;
3683 else {
3684 int id = std->index;
3685 memset(std, 0, sizeof(*std));
3686 std->index = id;
3687 }
3688
3689 if (std->index == zr->card.norms) {
3690 /* if we have autodetect, ... */
3691 struct video_decoder_capability caps;
3692 decoder_command(zr, DECODER_GET_CAPABILITIES,
3693 &caps);
3694 if (caps.flags & VIDEO_DECODER_AUTO) {
3695 std->id = V4L2_STD_ALL;
3696 strncpy(std->name, "Autodetect", 31);
3697 return 0;
3698 } else
3699 return -EINVAL;
3700 }
3701 switch (std->index) {
3702 case 0:
3703 std->id = V4L2_STD_PAL;
3704 strncpy(std->name, "PAL", 31);
3705 std->frameperiod.numerator = 1;
3706 std->frameperiod.denominator = 25;
3707 std->framelines = zr->card.tvn[0]->Ht;
3708 break;
3709 case 1:
3710 std->id = V4L2_STD_NTSC;
3711 strncpy(std->name, "NTSC", 31);
3712 std->frameperiod.numerator = 1001;
3713 std->frameperiod.denominator = 30000;
3714 std->framelines = zr->card.tvn[1]->Ht;
3715 break;
3716 case 2:
3717 std->id = V4L2_STD_SECAM;
3718 strncpy(std->name, "SECAM", 31);
3719 std->frameperiod.numerator = 1;
3720 std->frameperiod.denominator = 25;
3721 std->framelines = zr->card.tvn[2]->Ht;
3722 break;
3723 }
3724
3725 return 0;
3726 }
3727 break;
3728
3729 case VIDIOC_G_STD:
3730 {
3731 v4l2_std_id *std = arg;
3732 int norm;
3733
3734 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
3735
3736 down(&zr->resource_lock);
3737 norm = zr->norm;
3738 up(&zr->resource_lock);
3739
3740 switch (norm) {
3741 case VIDEO_MODE_PAL:
3742 *std = V4L2_STD_PAL;
3743 break;
3744 case VIDEO_MODE_NTSC:
3745 *std = V4L2_STD_NTSC;
3746 break;
3747 case VIDEO_MODE_SECAM:
3748 *std = V4L2_STD_SECAM;
3749 break;
3750 }
3751
3752 return 0;
3753 }
3754 break;
3755
3756 case VIDIOC_S_STD:
3757 {
3758 int norm = -1, res = 0;
3759 v4l2_std_id *std = arg;
3760
3761 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
3762 ZR_DEVNAME(zr), (unsigned long long)*std);
3763
3764 if (*std == V4L2_STD_PAL)
3765 norm = VIDEO_MODE_PAL;
3766 else if (*std == V4L2_STD_NTSC)
3767 norm = VIDEO_MODE_NTSC;
3768 else if (*std == V4L2_STD_SECAM)
3769 norm = VIDEO_MODE_SECAM;
3770 else if (*std == V4L2_STD_ALL)
3771 norm = VIDEO_MODE_AUTO;
3772 else {
3773 dprintk(1,
3774 KERN_ERR
3775 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3776 ZR_DEVNAME(zr), (unsigned long long)*std);
3777 return -EINVAL;
3778 }
3779
3780 down(&zr->resource_lock);
3781 if ((res = zoran_set_norm(zr, norm)))
3782 goto sstd_unlock_and_return;
3783
3784 res = wait_grab_pending(zr);
3785 sstd_unlock_and_return:
3786 up(&zr->resource_lock);
3787 return res;
3788 }
3789 break;
3790
3791 case VIDIOC_ENUMINPUT:
3792 {
3793 struct v4l2_input *inp = arg;
3794 int status;
3795
3796 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
3797 ZR_DEVNAME(zr), inp->index);
3798
3799 if (inp->index < 0 || inp->index >= zr->card.inputs)
3800 return -EINVAL;
3801 else {
3802 int id = inp->index;
3803 memset(inp, 0, sizeof(*inp));
3804 inp->index = id;
3805 }
3806
3807 strncpy(inp->name, zr->card.input[inp->index].name,
3808 sizeof(inp->name) - 1);
3809 inp->type = V4L2_INPUT_TYPE_CAMERA;
3810 inp->std = V4L2_STD_ALL;
3811
3812 /* Get status of video decoder */
3813 down(&zr->resource_lock);
3814 decoder_command(zr, DECODER_GET_STATUS, &status);
3815 up(&zr->resource_lock);
3816
3817 if (!(status & DECODER_STATUS_GOOD)) {
3818 inp->status |= V4L2_IN_ST_NO_POWER;
3819 inp->status |= V4L2_IN_ST_NO_SIGNAL;
3820 }
3821 if (!(status & DECODER_STATUS_COLOR))
3822 inp->status |= V4L2_IN_ST_NO_COLOR;
3823
3824 return 0;
3825 }
3826 break;
3827
3828 case VIDIOC_G_INPUT:
3829 {
3830 int *input = arg;
3831
3832 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
3833
3834 down(&zr->resource_lock);
3835 *input = zr->input;
3836 up(&zr->resource_lock);
3837
3838 return 0;
3839 }
3840 break;
3841
3842 case VIDIOC_S_INPUT:
3843 {
3844 int *input = arg, res = 0;
3845
3846 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
3847 ZR_DEVNAME(zr), *input);
3848
3849 down(&zr->resource_lock);
3850 if ((res = zoran_set_input(zr, *input)))
3851 goto sinput_unlock_and_return;
3852
3853 /* Make sure the changes come into effect */
3854 res = wait_grab_pending(zr);
3855 sinput_unlock_and_return:
3856 up(&zr->resource_lock);
3857 return res;
3858 }
3859 break;
3860
3861 case VIDIOC_ENUMOUTPUT:
3862 {
3863 struct v4l2_output *outp = arg;
3864
3865 dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
3866 ZR_DEVNAME(zr), outp->index);
3867
3868 if (outp->index != 0)
3869 return -EINVAL;
3870
3871 memset(outp, 0, sizeof(*outp));
3872 outp->index = 0;
3873 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3874 strncpy(outp->name, "Autodetect", 31);
3875
3876 return 0;
3877 }
3878 break;
3879
3880 case VIDIOC_G_OUTPUT:
3881 {
3882 int *output = arg;
3883
3884 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
3885
3886 *output = 0;
3887
3888 return 0;
3889 }
3890 break;
3891
3892 case VIDIOC_S_OUTPUT:
3893 {
3894 int *output = arg;
3895
3896 dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
3897 ZR_DEVNAME(zr), *output);
3898
3899 if (*output != 0)
3900 return -EINVAL;
3901
3902 return 0;
3903 }
3904 break;
3905
3906 /* cropping (sub-frame capture) */
3907 case VIDIOC_CROPCAP:
3908 {
3909 struct v4l2_cropcap *cropcap = arg;
3910 int type = cropcap->type, res = 0;
3911
3912 dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
3913 ZR_DEVNAME(zr), cropcap->type);
3914
3915 memset(cropcap, 0, sizeof(*cropcap));
3916 cropcap->type = type;
3917
3918 down(&zr->resource_lock);
3919
3920 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3921 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3922 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3923 dprintk(1,
3924 KERN_ERR
3925 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3926 ZR_DEVNAME(zr));
3927 res = -EINVAL;
3928 goto cropcap_unlock_and_return;
3929 }
3930
3931 cropcap->bounds.top = cropcap->bounds.left = 0;
3932 cropcap->bounds.width = BUZ_MAX_WIDTH;
3933 cropcap->bounds.height = BUZ_MAX_HEIGHT;
3934 cropcap->defrect.top = cropcap->defrect.left = 0;
3935 cropcap->defrect.width = BUZ_MIN_WIDTH;
3936 cropcap->defrect.height = BUZ_MIN_HEIGHT;
3937 cropcap_unlock_and_return:
3938 up(&zr->resource_lock);
3939 return res;
3940 }
3941 break;
3942
3943 case VIDIOC_G_CROP:
3944 {
3945 struct v4l2_crop *crop = arg;
3946 int type = crop->type, res = 0;
3947
3948 dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
3949 ZR_DEVNAME(zr), crop->type);
3950
3951 memset(crop, 0, sizeof(*crop));
3952 crop->type = type;
3953
3954 down(&zr->resource_lock);
3955
3956 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
3957 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
3958 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
3959 dprintk(1,
3960 KERN_ERR
3961 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3962 ZR_DEVNAME(zr));
3963 res = -EINVAL;
3964 goto gcrop_unlock_and_return;
3965 }
3966
3967 crop->c.top = fh->jpg_settings.img_y;
3968 crop->c.left = fh->jpg_settings.img_x;
3969 crop->c.width = fh->jpg_settings.img_width;
3970 crop->c.height = fh->jpg_settings.img_height;
3971
3972 gcrop_unlock_and_return:
3973 up(&zr->resource_lock);
3974
3975 return res;
3976 }
3977 break;
3978
3979 case VIDIOC_S_CROP:
3980 {
3981 struct v4l2_crop *crop = arg;
3982 int res = 0;
3983
3984 settings = fh->jpg_settings;
3985
3986 dprintk(3,
3987 KERN_ERR
3988 "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
3989 ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
3990 crop->c.width, crop->c.height);
3991
3992 down(&zr->resource_lock);
3993
3994 if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
3995 dprintk(1,
3996 KERN_ERR
3997 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3998 ZR_DEVNAME(zr));
3999 res = -EBUSY;
4000 goto scrop_unlock_and_return;
4001 }
4002
4003 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
4004 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4005 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
4006 dprintk(1,
4007 KERN_ERR
4008 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
4009 ZR_DEVNAME(zr));
4010 res = -EINVAL;
4011 goto scrop_unlock_and_return;
4012 }
4013
4014 /* move into a form that we understand */
4015 settings.img_x = crop->c.left;
4016 settings.img_y = crop->c.top;
4017 settings.img_width = crop->c.width;
4018 settings.img_height = crop->c.height;
4019
4020 /* check validity */
4021 if ((res = zoran_check_jpg_settings(zr, &settings)))
4022 goto scrop_unlock_and_return;
4023
4024 /* accept */
4025 fh->jpg_settings = settings;
4026
4027 scrop_unlock_and_return:
4028 up(&zr->resource_lock);
4029 return res;
4030 }
4031 break;
4032
4033 case VIDIOC_G_JPEGCOMP:
4034 {
4035 struct v4l2_jpegcompression *params = arg;
4036
4037 dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
4038 ZR_DEVNAME(zr));
4039
4040 memset(params, 0, sizeof(*params));
4041
4042 down(&zr->resource_lock);
4043
4044 params->quality = fh->jpg_settings.jpg_comp.quality;
4045 params->APPn = fh->jpg_settings.jpg_comp.APPn;
4046 memcpy(params->APP_data,
4047 fh->jpg_settings.jpg_comp.APP_data,
4048 fh->jpg_settings.jpg_comp.APP_len);
4049 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
4050 memcpy(params->COM_data,
4051 fh->jpg_settings.jpg_comp.COM_data,
4052 fh->jpg_settings.jpg_comp.COM_len);
4053 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
4054 params->jpeg_markers =
4055 fh->jpg_settings.jpg_comp.jpeg_markers;
4056
4057 up(&zr->resource_lock);
4058
4059 return 0;
4060 }
4061 break;
4062
4063 case VIDIOC_S_JPEGCOMP:
4064 {
4065 struct v4l2_jpegcompression *params = arg;
4066 int res = 0;
4067
4068 settings = fh->jpg_settings;
4069
4070 dprintk(3,
4071 KERN_DEBUG
4072 "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
4073 ZR_DEVNAME(zr), params->quality, params->APPn,
4074 params->APP_len, params->COM_len);
4075
4076 settings.jpg_comp = *params;
4077
4078 down(&zr->resource_lock);
4079
4080 if (fh->v4l_buffers.active != ZORAN_FREE ||
4081 fh->jpg_buffers.active != ZORAN_FREE) {
4082 dprintk(1,
4083 KERN_WARNING
4084 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
4085 ZR_DEVNAME(zr));
4086 res = -EBUSY;
4087 goto sjpegc_unlock_and_return;
4088 }
4089
4090 if ((res = zoran_check_jpg_settings(zr, &settings)))
4091 goto sjpegc_unlock_and_return;
4092 if (!fh->jpg_buffers.allocated)
4093 fh->jpg_buffers.buffer_size =
4094 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
4095 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
4096 sjpegc_unlock_and_return:
4097 up(&zr->resource_lock);
4098
4099 return 0;
4100 }
4101 break;
4102
4103 case VIDIOC_QUERYSTD: /* why is this useful? */
4104 {
4105 v4l2_std_id *std = arg;
4106
4107 dprintk(3,
4108 KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
4109 ZR_DEVNAME(zr), (unsigned long long)*std);
4110
4111 if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
4112 *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
4113 zr->card.norms == 3)) {
4114 return 0;
4115 }
4116
4117 return -EINVAL;
4118 }
4119 break;
4120
4121 case VIDIOC_TRY_FMT:
4122 {
4123 struct v4l2_format *fmt = arg;
4124 int res = 0;
4125
4126 dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
4127 ZR_DEVNAME(zr), fmt->type);
4128
4129 switch (fmt->type) {
4130 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4131 down(&zr->resource_lock);
4132
4133 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
4134 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
4135 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
4136 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
4137 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
4138 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
4139 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
4140 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
4141
4142 up(&zr->resource_lock);
4143 break;
4144
4145 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4146 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4147 if (fmt->fmt.pix.bytesperline > 0)
4148 return -EINVAL;
4149
4150 down(&zr->resource_lock);
4151
4152 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
4153 settings = fh->jpg_settings;
4154
4155 /* we actually need to set 'real' parameters now */
4156 if ((fmt->fmt.pix.height * 2) >
4157 BUZ_MAX_HEIGHT)
4158 settings.TmpDcm = 1;
4159 else
4160 settings.TmpDcm = 2;
4161 settings.decimation = 0;
4162 if (fmt->fmt.pix.height <=
4163 fh->jpg_settings.img_height / 2)
4164 settings.VerDcm = 2;
4165 else
4166 settings.VerDcm = 1;
4167 if (fmt->fmt.pix.width <=
4168 fh->jpg_settings.img_width / 4)
4169 settings.HorDcm = 4;
4170 else if (fmt->fmt.pix.width <=
4171 fh->jpg_settings.img_width / 2)
4172 settings.HorDcm = 2;
4173 else
4174 settings.HorDcm = 1;
4175 if (settings.TmpDcm == 1)
4176 settings.field_per_buff = 2;
4177 else
4178 settings.field_per_buff = 1;
4179
4180 /* check */
4181 if ((res =
4182 zoran_check_jpg_settings(zr,
4183 &settings)))
4184 goto tryfmt_unlock_and_return;
4185
4186 /* tell the user what we actually did */
4187 fmt->fmt.pix.width =
4188 settings.img_width / settings.HorDcm;
4189 fmt->fmt.pix.height =
4190 settings.img_height * 2 /
4191 (settings.TmpDcm * settings.VerDcm);
4192 if (settings.TmpDcm == 1)
4193 fmt->fmt.pix.field =
4194 (fh->jpg_settings.
4195 odd_even ? V4L2_FIELD_SEQ_TB :
4196 V4L2_FIELD_SEQ_BT);
4197 else
4198 fmt->fmt.pix.field =
4199 (fh->jpg_settings.
4200 odd_even ? V4L2_FIELD_TOP :
4201 V4L2_FIELD_BOTTOM);
4202
4203 fmt->fmt.pix.sizeimage =
4204 zoran_v4l2_calc_bufsize(&settings);
4205 } else if (fmt->type ==
4206 V4L2_BUF_TYPE_VIDEO_CAPTURE) {
4207 int i;
4208
4209 for (i = 0; i < zoran_num_formats; i++)
4210 if (zoran_formats[i].fourcc ==
4211 fmt->fmt.pix.pixelformat)
4212 break;
4213 if (i == zoran_num_formats) {
4214 res = -EINVAL;
4215 goto tryfmt_unlock_and_return;
4216 }
4217
4218 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
4219 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
4220 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
4221 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
4222 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
4223 fmt->fmt.pix.height =
4224 BUZ_MAX_HEIGHT;
4225 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
4226 fmt->fmt.pix.height =
4227 BUZ_MIN_HEIGHT;
4228 } else {
4229 res = -EINVAL;
4230 goto tryfmt_unlock_and_return;
4231 }
4232 tryfmt_unlock_and_return:
4233 up(&zr->resource_lock);
4234
4235 return res;
4236 break;
4237
4238 default:
4239 return -EINVAL;
4240 }
4241
4242 return 0;
4243 }
4244 break;
4245#endif
4246
4247 default:
4248 dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
4249 ZR_DEVNAME(zr), cmd);
4250 return -ENOIOCTLCMD;
4251 break;
4252
4253 }
4254 return 0;
4255}
4256
4257
4258static int
4259zoran_ioctl (struct inode *inode,
4260 struct file *file,
4261 unsigned int cmd,
4262 unsigned long arg)
4263{
4264 return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
4265}
4266
4267static unsigned int
4268zoran_poll (struct file *file,
4269 poll_table *wait)
4270{
4271 struct zoran_fh *fh = file->private_data;
4272 struct zoran *zr = fh->zr;
4273 wait_queue_head_t *queue = NULL;
4274 int res = 0, frame;
4275
4276 /* we should check whether buffers are ready to be synced on
4277 * (w/o waits - O_NONBLOCK) here
4278 * if ready for read (sync), return POLLIN|POLLRDNORM,
4279 * if ready for write (sync), return POLLOUT|POLLWRNORM,
4280 * if error, return POLLERR,
4281 * if no buffers queued or so, return POLLNVAL
4282 */
4283
4284 down(&zr->resource_lock);
4285
4286 switch (fh->map_mode) {
4287 case ZORAN_MAP_MODE_RAW:
4288 if (fh->v4l_buffers.active == ZORAN_FREE ||
4289 zr->v4l_pend_head == zr->v4l_pend_tail) {
4290 dprintk(1,
4291 "%s: zoran_poll() - no buffers queued\n",
4292 ZR_DEVNAME(zr));
4293 res = POLLNVAL;
4294 goto poll_unlock_and_return;
4295 }
4296 queue = &zr->v4l_capq;
4297 frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
4298 poll_wait(file, queue, wait);
4299 if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
4300 res = POLLIN | POLLRDNORM;
4301 break;
4302
4303 case ZORAN_MAP_MODE_JPG_REC:
4304 case ZORAN_MAP_MODE_JPG_PLAY:
4305 if (fh->jpg_buffers.active == ZORAN_FREE ||
4306 zr->jpg_que_head == zr->jpg_que_tail) {
4307 dprintk(1,
4308 "%s: zoran_poll() - no buffers queued\n",
4309 ZR_DEVNAME(zr));
4310 res = POLLNVAL;
4311 goto poll_unlock_and_return;
4312 }
4313 queue = &zr->jpg_capq;
4314 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4315 poll_wait(file, queue, wait);
4316 if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
4317 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
4318 res = POLLIN | POLLRDNORM;
4319 else
4320 res = POLLOUT | POLLWRNORM;
4321 }
4322 break;
4323
4324 default:
4325 dprintk(1,
4326 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
4327 ZR_DEVNAME(zr), fh->map_mode);
4328 res = POLLNVAL;
4329 goto poll_unlock_and_return;
4330 }
4331
4332poll_unlock_and_return:
4333 up(&zr->resource_lock);
4334
4335 return res;
4336}
4337
4338
4339/*
4340 * This maps the buffers to user space.
4341 *
4342 * Depending on the state of fh->map_mode
4343 * the V4L or the MJPEG buffers are mapped
4344 * per buffer or all together
4345 *
4346 * Note that we need to connect to some
4347 * unmap signal event to unmap the de-allocate
4348 * the buffer accordingly (zoran_vm_close())
4349 */
4350
4351static void
4352zoran_vm_open (struct vm_area_struct *vma)
4353{
4354 struct zoran_mapping *map = vma->vm_private_data;
4355
4356 map->count++;
4357}
4358
4359static void
4360zoran_vm_close (struct vm_area_struct *vma)
4361{
4362 struct zoran_mapping *map = vma->vm_private_data;
4363 struct file *file = map->file;
4364 struct zoran_fh *fh = file->private_data;
4365 struct zoran *zr = fh->zr;
4366 int i;
4367
4368 map->count--;
4369 if (map->count == 0) {
4370 switch (fh->map_mode) {
4371 case ZORAN_MAP_MODE_JPG_REC:
4372 case ZORAN_MAP_MODE_JPG_PLAY:
4373
4374 dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
4375 ZR_DEVNAME(zr));
4376
4377 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
4378 if (fh->jpg_buffers.buffer[i].map == map) {
4379 fh->jpg_buffers.buffer[i].map =
4380 NULL;
4381 }
4382 }
4383 kfree(map);
4384
4385 for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
4386 if (fh->jpg_buffers.buffer[i].map)
4387 break;
4388 if (i == fh->jpg_buffers.num_buffers) {
4389 down(&zr->resource_lock);
4390
4391 if (fh->jpg_buffers.active != ZORAN_FREE) {
4392 jpg_qbuf(file, -1, zr->codec_mode);
4393 zr->jpg_buffers.allocated = 0;
4394 zr->jpg_buffers.active =
4395 fh->jpg_buffers.active =
4396 ZORAN_FREE;
4397 }
4398 //jpg_fbuffer_free(file);
4399 fh->jpg_buffers.allocated = 0;
4400 fh->jpg_buffers.ready_to_be_freed = 1;
4401
4402 up(&zr->resource_lock);
4403 }
4404
4405 break;
4406
4407 case ZORAN_MAP_MODE_RAW:
4408
4409 dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
4410 ZR_DEVNAME(zr));
4411
4412 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
4413 if (fh->v4l_buffers.buffer[i].map == map) {
4414 /* unqueue/unmap */
4415 fh->v4l_buffers.buffer[i].map =
4416 NULL;
4417 }
4418 }
4419 kfree(map);
4420
4421 for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
4422 if (fh->v4l_buffers.buffer[i].map)
4423 break;
4424 if (i == fh->v4l_buffers.num_buffers) {
4425 down(&zr->resource_lock);
4426
4427 if (fh->v4l_buffers.active != ZORAN_FREE) {
4428 zr36057_set_memgrab(zr, 0);
4429 zr->v4l_buffers.allocated = 0;
4430 zr->v4l_buffers.active =
4431 fh->v4l_buffers.active =
4432 ZORAN_FREE;
4433 }
4434 //v4l_fbuffer_free(file);
4435 fh->v4l_buffers.allocated = 0;
4436 fh->v4l_buffers.ready_to_be_freed = 1;
4437
4438 up(&zr->resource_lock);
4439 }
4440
4441 break;
4442
4443 default:
4444 printk(KERN_ERR
4445 "%s: munmap() - internal error - unknown map mode %d\n",
4446 ZR_DEVNAME(zr), fh->map_mode);
4447 break;
4448
4449 }
4450 }
4451}
4452
4453static struct vm_operations_struct zoran_vm_ops = {
4454 .open = zoran_vm_open,
4455 .close = zoran_vm_close,
4456};
4457
4458static int
4459zoran_mmap (struct file *file,
4460 struct vm_area_struct *vma)
4461{
4462 struct zoran_fh *fh = file->private_data;
4463 struct zoran *zr = fh->zr;
4464 unsigned long size = (vma->vm_end - vma->vm_start);
4465 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
4466 int i, j;
4467 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
4468 int first, last;
4469 struct zoran_mapping *map;
4470 int res = 0;
4471
4472 dprintk(3,
4473 KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
4474 ZR_DEVNAME(zr),
4475 fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
4476 vma->vm_start, vma->vm_end, size);
4477
4478 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
4479 !(vma->vm_flags & VM_WRITE)) {
4480 dprintk(1,
4481 KERN_ERR
4482 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
4483 ZR_DEVNAME(zr));
4484 return -EINVAL;
4485 }
4486
4487 switch (fh->map_mode) {
4488
4489 case ZORAN_MAP_MODE_JPG_REC:
4490 case ZORAN_MAP_MODE_JPG_PLAY:
4491
4492 /* lock */
4493 down(&zr->resource_lock);
4494
4495 /* Map the MJPEG buffers */
4496 if (!fh->jpg_buffers.allocated) {
4497 dprintk(1,
4498 KERN_ERR
4499 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
4500 ZR_DEVNAME(zr));
4501 res = -ENOMEM;
4502 goto jpg_mmap_unlock_and_return;
4503 }
4504
4505 first = offset / fh->jpg_buffers.buffer_size;
4506 last = first - 1 + size / fh->jpg_buffers.buffer_size;
4507 if (offset % fh->jpg_buffers.buffer_size != 0 ||
4508 size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
4509 last < 0 || first >= fh->jpg_buffers.num_buffers ||
4510 last >= fh->jpg_buffers.num_buffers) {
4511 dprintk(1,
4512 KERN_ERR
4513 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4514 ZR_DEVNAME(zr), offset, size,
4515 fh->jpg_buffers.buffer_size,
4516 fh->jpg_buffers.num_buffers);
4517 res = -EINVAL;
4518 goto jpg_mmap_unlock_and_return;
4519 }
4520 for (i = first; i <= last; i++) {
4521 if (fh->jpg_buffers.buffer[i].map) {
4522 dprintk(1,
4523 KERN_ERR
4524 "%s: mmap(MJPEG) - buffer %d already mapped\n",
4525 ZR_DEVNAME(zr), i);
4526 res = -EBUSY;
4527 goto jpg_mmap_unlock_and_return;
4528 }
4529 }
4530
4531 /* map these buffers (v4l_buffers[i]) */
4532 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4533 if (!map) {
4534 res = -ENOMEM;
4535 goto jpg_mmap_unlock_and_return;
4536 }
4537 map->file = file;
4538 map->count = 1;
4539
4540 vma->vm_ops = &zoran_vm_ops;
4541 vma->vm_flags |= VM_DONTEXPAND;
4542 vma->vm_private_data = map;
4543
4544 for (i = first; i <= last; i++) {
4545 for (j = 0;
4546 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
4547 j++) {
4548 fraglen =
4549 (le32_to_cpu(fh->jpg_buffers.buffer[i].
4550 frag_tab[2 * j + 1]) & ~1) << 1;
4551 todo = size;
4552 if (todo > fraglen)
4553 todo = fraglen;
4554 pos =
4555 le32_to_cpu((unsigned long) fh->jpg_buffers.
4556 buffer[i].frag_tab[2 * j]);
4557 /* should just be pos on i386 */
4558 page = virt_to_phys(bus_to_virt(pos))
4559 >> PAGE_SHIFT;
4560 if (remap_pfn_range(vma, start, page,
4561 todo, PAGE_SHARED)) {
4562 dprintk(1,
4563 KERN_ERR
4564 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
4565 ZR_DEVNAME(zr));
4566 res = -EAGAIN;
4567 goto jpg_mmap_unlock_and_return;
4568 }
4569 size -= todo;
4570 start += todo;
4571 if (size == 0)
4572 break;
4573 if (le32_to_cpu(fh->jpg_buffers.buffer[i].
4574 frag_tab[2 * j + 1]) & 1)
4575 break; /* was last fragment */
4576 }
4577 fh->jpg_buffers.buffer[i].map = map;
4578 if (size == 0)
4579 break;
4580
4581 }
4582 jpg_mmap_unlock_and_return:
4583 up(&zr->resource_lock);
4584
4585 break;
4586
4587 case ZORAN_MAP_MODE_RAW:
4588
4589 down(&zr->resource_lock);
4590
4591 /* Map the V4L buffers */
4592 if (!fh->v4l_buffers.allocated) {
4593 dprintk(1,
4594 KERN_ERR
4595 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4596 ZR_DEVNAME(zr));
4597 res = -ENOMEM;
4598 goto v4l_mmap_unlock_and_return;
4599 }
4600
4601 first = offset / fh->v4l_buffers.buffer_size;
4602 last = first - 1 + size / fh->v4l_buffers.buffer_size;
4603 if (offset % fh->v4l_buffers.buffer_size != 0 ||
4604 size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
4605 last < 0 || first >= fh->v4l_buffers.num_buffers ||
4606 last >= fh->v4l_buffers.buffer_size) {
4607 dprintk(1,
4608 KERN_ERR
4609 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4610 ZR_DEVNAME(zr), offset, size,
4611 fh->v4l_buffers.buffer_size,
4612 fh->v4l_buffers.num_buffers);
4613 res = -EINVAL;
4614 goto v4l_mmap_unlock_and_return;
4615 }
4616 for (i = first; i <= last; i++) {
4617 if (fh->v4l_buffers.buffer[i].map) {
4618 dprintk(1,
4619 KERN_ERR
4620 "%s: mmap(V4L) - buffer %d already mapped\n",
4621 ZR_DEVNAME(zr), i);
4622 res = -EBUSY;
4623 goto v4l_mmap_unlock_and_return;
4624 }
4625 }
4626
4627 /* map these buffers (v4l_buffers[i]) */
4628 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
4629 if (!map) {
4630 res = -ENOMEM;
4631 goto v4l_mmap_unlock_and_return;
4632 }
4633 map->file = file;
4634 map->count = 1;
4635
4636 vma->vm_ops = &zoran_vm_ops;
4637 vma->vm_flags |= VM_DONTEXPAND;
4638 vma->vm_private_data = map;
4639
4640 for (i = first; i <= last; i++) {
4641 todo = size;
4642 if (todo > fh->v4l_buffers.buffer_size)
4643 todo = fh->v4l_buffers.buffer_size;
4644 page = fh->v4l_buffers.buffer[i].fbuffer_phys;
4645 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
4646 todo, PAGE_SHARED)) {
4647 dprintk(1,
4648 KERN_ERR
4649 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4650 ZR_DEVNAME(zr));
4651 res = -EAGAIN;
4652 goto v4l_mmap_unlock_and_return;
4653 }
4654 size -= todo;
4655 start += todo;
4656 fh->v4l_buffers.buffer[i].map = map;
4657 if (size == 0)
4658 break;
4659 }
4660 v4l_mmap_unlock_and_return:
4661 up(&zr->resource_lock);
4662
4663 break;
4664
4665 default:
4666 dprintk(1,
4667 KERN_ERR
4668 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4669 ZR_DEVNAME(zr), fh->map_mode);
4670 break;
4671 }
4672
4673 return 0;
4674}
4675
4676static struct file_operations zoran_fops = {
4677 .owner = THIS_MODULE,
4678 .open = zoran_open,
4679 .release = zoran_close,
4680 .ioctl = zoran_ioctl,
4681 .llseek = no_llseek,
4682 .read = zoran_read,
4683 .write = zoran_write,
4684 .mmap = zoran_mmap,
4685 .poll = zoran_poll,
4686};
4687
4688struct video_device zoran_template __devinitdata = {
4689 .name = ZORAN_NAME,
4690 .type = ZORAN_VID_TYPE,
4691#ifdef HAVE_V4L2
4692 .type2 = ZORAN_V4L2_VID_FLAGS,
4693#endif
4694 .hardware = ZORAN_HARDWARE,
4695 .fops = &zoran_fops,
4696 .release = &zoran_vdev_release,
4697 .minor = -1
4698};
4699