/drivers/media/video/zoran_driver.c
C | 4699 lines | 3647 code | 696 blank | 356 comment | 637 complexity | fd87df4f679a777ad9eb248f9464fc3f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
Large files files are truncated, but you can click here to view the full file
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 *
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9 *
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11 *
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13 *
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 *
16 * Based on
17 *
18 * Miro DC10 driver
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20 *
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23 *
24 * buz.0.0.3
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26 *
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
30 *
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 */
46
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
56#include <linux/interrupt.h>
57#include <linux/i2c.h>
58#include <linux/i2c-algo-bit.h>
59
60#include <linux/spinlock.h>
61#define MAP_NR(x) virt_to_page(x)
62#define ZORAN_HARDWARE VID_HARDWARE_ZR36067
63#define ZORAN_VID_TYPE ( \
64 VID_TYPE_CAPTURE | \
65 VID_TYPE_OVERLAY | \
66 VID_TYPE_CLIPPING | \
67 VID_TYPE_FRAMERAM | \
68 VID_TYPE_SCALES | \
69 VID_TYPE_MJPEG_DECODER | \
70 VID_TYPE_MJPEG_ENCODER \
71 )
72
73#include <linux/videodev.h>
74#include "videocodec.h"
75
76#include <asm/io.h>
77#include <asm/uaccess.h>
78#include <linux/proc_fs.h>
79
80#include <linux/video_decoder.h>
81#include <linux/video_encoder.h>
82#include "zoran.h"
83#include "zoran_device.h"
84#include "zoran_card.h"
85
86#ifdef HAVE_V4L2
87 /* we declare some card type definitions here, they mean
88 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
89#define ZORAN_V4L2_VID_FLAGS ( \
90 V4L2_CAP_STREAMING |\
91 V4L2_CAP_VIDEO_CAPTURE |\
92 V4L2_CAP_VIDEO_OUTPUT |\
93 V4L2_CAP_VIDEO_OVERLAY \
94 )
95#endif
96
97#include <asm/byteorder.h>
98
99const struct zoran_format zoran_formats[] = {
100 {
101 .name = "15-bit RGB",
102 .palette = VIDEO_PALETTE_RGB555,
103#ifdef HAVE_V4L2
104#ifdef __LITTLE_ENDIAN
105 .fourcc = V4L2_PIX_FMT_RGB555,
106#else
107 .fourcc = V4L2_PIX_FMT_RGB555X,
108#endif
109 .colorspace = V4L2_COLORSPACE_SRGB,
110#endif
111 .depth = 15,
112 .flags = ZORAN_FORMAT_CAPTURE |
113 ZORAN_FORMAT_OVERLAY,
114 }, {
115 .name = "16-bit RGB",
116 .palette = VIDEO_PALETTE_RGB565,
117#ifdef HAVE_V4L2
118#ifdef __LITTLE_ENDIAN
119 .fourcc = V4L2_PIX_FMT_RGB565,
120#else
121 .fourcc = V4L2_PIX_FMT_RGB565X,
122#endif
123 .colorspace = V4L2_COLORSPACE_SRGB,
124#endif
125 .depth = 16,
126 .flags = ZORAN_FORMAT_CAPTURE |
127 ZORAN_FORMAT_OVERLAY,
128 }, {
129 .name = "24-bit RGB",
130 .palette = VIDEO_PALETTE_RGB24,
131#ifdef HAVE_V4L2
132#ifdef __LITTLE_ENDIAN
133 .fourcc = V4L2_PIX_FMT_BGR24,
134#else
135 .fourcc = V4L2_PIX_FMT_RGB24,
136#endif
137 .colorspace = V4L2_COLORSPACE_SRGB,
138#endif
139 .depth = 24,
140 .flags = ZORAN_FORMAT_CAPTURE |
141 ZORAN_FORMAT_OVERLAY,
142 }, {
143 .name = "32-bit RGB",
144 .palette = VIDEO_PALETTE_RGB32,
145#ifdef HAVE_V4L2
146#ifdef __LITTLE_ENDIAN
147 .fourcc = V4L2_PIX_FMT_BGR32,
148#else
149 .fourcc = V4L2_PIX_FMT_RGB32,
150#endif
151 .colorspace = V4L2_COLORSPACE_SRGB,
152#endif
153 .depth = 32,
154 .flags = ZORAN_FORMAT_CAPTURE |
155 ZORAN_FORMAT_OVERLAY,
156 }, {
157 .name = "4:2:2, packed, YUYV",
158 .palette = VIDEO_PALETTE_YUV422,
159#ifdef HAVE_V4L2
160 .fourcc = V4L2_PIX_FMT_YUYV,
161 .colorspace = V4L2_COLORSPACE_SMPTE170M,
162#endif
163 .depth = 16,
164 .flags = ZORAN_FORMAT_CAPTURE |
165 ZORAN_FORMAT_OVERLAY,
166 }, {
167 .name = "Hardware-encoded Motion-JPEG",
168 .palette = -1,
169#ifdef HAVE_V4L2
170 .fourcc = V4L2_PIX_FMT_MJPEG,
171 .colorspace = V4L2_COLORSPACE_SMPTE170M,
172#endif
173 .depth = 0,
174 .flags = ZORAN_FORMAT_CAPTURE |
175 ZORAN_FORMAT_PLAYBACK |
176 ZORAN_FORMAT_COMPRESSED,
177 }
178};
179const int zoran_num_formats =
180 (sizeof(zoran_formats) / sizeof(struct zoran_format));
181
182// RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
183#if !defined(CONFIG_BIGPHYS_AREA)
184//#undef CONFIG_BIGPHYS_AREA
185#define BUZ_USE_HIMEM
186#endif
187
188#if defined(CONFIG_BIGPHYS_AREA)
189# include <linux/bigphysarea.h>
190#endif
191
192extern int *zr_debug;
193
194#define dprintk(num, format, args...) \
195 do { \
196 if (*zr_debug >= num) \
197 printk(format, ##args); \
198 } while (0)
199
200extern int v4l_nbufs;
201extern int v4l_bufsize;
202extern int jpg_nbufs;
203extern int jpg_bufsize;
204extern int pass_through;
205
206static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
207MODULE_PARM(lock_norm, "i");
208MODULE_PARM_DESC(lock_norm, "Users can't change norm");
209
210#ifdef HAVE_V4L2
211 /* small helper function for calculating buffersizes for v4l2
212 * we calculate the nearest higher power-of-two, which
213 * will be the recommended buffersize */
214static __u32
215zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
216{
217 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
218 __u32 num = (1024 * 512) / (div);
219 __u32 result = 2;
220
221 num--;
222 while (num) {
223 num >>= 1;
224 result <<= 1;
225 }
226
227 if (result > jpg_bufsize)
228 return jpg_bufsize;
229 if (result < 8192)
230 return 8192;
231 return result;
232}
233#endif
234
235/* forward references */
236static void v4l_fbuffer_free(struct file *file);
237static void jpg_fbuffer_free(struct file *file);
238
239/*
240 * Allocate the V4L grab buffers
241 *
242 * These have to be pysically contiguous.
243 * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
244 * else we try to allocate them with bigphysarea_alloc_pages
245 * if the bigphysarea patch is present in the kernel,
246 * else we try to use high memory (if the user has bootet
247 * Linux with the necessary memory left over).
248 */
249
250#if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
251static unsigned long
252get_high_mem (unsigned long size)
253{
254/*
255 * Check if there is usable memory at the end of Linux memory
256 * of at least size. Return the physical address of this memory,
257 * return 0 on failure.
258 *
259 * The idea is from Alexandro Rubini's book "Linux device drivers".
260 * The driver from him which is downloadable from O'Reilly's
261 * web site misses the "virt_to_phys(high_memory)" part
262 * (and therefore doesn't work at all - at least with 2.2.x kernels).
263 *
264 * It should be unnecessary to mention that THIS IS DANGEROUS,
265 * if more than one driver at a time has the idea to use this memory!!!!
266 */
267
268 volatile unsigned char *mem;
269 unsigned char c;
270 unsigned long hi_mem_ph;
271 unsigned long i;
272
273 /* Map the high memory to user space */
274
275 hi_mem_ph = virt_to_phys(high_memory);
276
277 mem = ioremap(hi_mem_ph, size);
278 if (!mem) {
279 dprintk(1,
280 KERN_ERR "%s: get_high_mem() - ioremap failed\n",
281 ZORAN_NAME);
282 return 0;
283 }
284
285 for (i = 0; i < size; i++) {
286 /* Check if it is memory */
287 c = i & 0xff;
288 mem[i] = c;
289 if (mem[i] != c)
290 break;
291 c = 255 - c;
292 mem[i] = c;
293 if (mem[i] != c)
294 break;
295 mem[i] = 0; /* zero out memory */
296
297 /* give the kernel air to breath */
298 if ((i & 0x3ffff) == 0x3ffff)
299 schedule();
300 }
301
302 iounmap((void *) mem);
303
304 if (i != size) {
305 dprintk(1,
306 KERN_ERR
307 "%s: get_high_mem() - requested %lu, avail %lu\n",
308 ZORAN_NAME, size, i);
309 return 0;
310 }
311
312 return hi_mem_ph;
313}
314#endif
315
316static int
317v4l_fbuffer_alloc (struct file *file)
318{
319 struct zoran_fh *fh = file->private_data;
320 struct zoran *zr = fh->zr;
321 int i, off;
322 unsigned char *mem;
323#if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
324 unsigned long pmem = 0;
325#endif
326
327 /* we might have old buffers lying around... */
328 if (fh->v4l_buffers.ready_to_be_freed) {
329 v4l_fbuffer_free(file);
330 }
331
332 for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
333 if (fh->v4l_buffers.buffer[i].fbuffer)
334 dprintk(2,
335 KERN_WARNING
336 "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
337 ZR_DEVNAME(zr), i);
338
339 //udelay(20);
340 if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
341 /* Use kmalloc */
342
343 mem =
344 (unsigned char *) kmalloc(fh->v4l_buffers.
345 buffer_size,
346 GFP_KERNEL);
347 if (mem == 0) {
348 dprintk(1,
349 KERN_ERR
350 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
351 ZR_DEVNAME(zr), i);
352 v4l_fbuffer_free(file);
353 return -ENOBUFS;
354 }
355 fh->v4l_buffers.buffer[i].fbuffer = mem;
356 fh->v4l_buffers.buffer[i].fbuffer_phys =
357 virt_to_phys(mem);
358 fh->v4l_buffers.buffer[i].fbuffer_bus =
359 virt_to_bus(mem);
360 for (off = 0; off < fh->v4l_buffers.buffer_size;
361 off += PAGE_SIZE)
362 SetPageReserved(MAP_NR(mem + off));
363 dprintk(4,
364 KERN_INFO
365 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
366 ZR_DEVNAME(zr), i, (unsigned long) mem,
367 virt_to_bus(mem));
368 } else {
369#if defined(CONFIG_BIGPHYS_AREA)
370 /* Use bigphysarea_alloc_pages */
371
372 int n =
373 (fh->v4l_buffers.buffer_size + PAGE_SIZE -
374 1) / PAGE_SIZE;
375
376 mem =
377 (unsigned char *) bigphysarea_alloc_pages(n, 0,
378 GFP_KERNEL);
379 if (mem == 0) {
380 dprintk(1,
381 KERN_ERR
382 "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
383 ZR_DEVNAME(zr), i);
384 v4l_fbuffer_free(file);
385 return -ENOBUFS;
386 }
387 fh->v4l_buffers.buffer[i].fbuffer = mem;
388 fh->v4l_buffers.buffer[i].fbuffer_phys =
389 virt_to_phys(mem);
390 fh->v4l_buffers.buffer[i].fbuffer_bus =
391 virt_to_bus(mem);
392 dprintk(4,
393 KERN_INFO
394 "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
395 ZR_DEVNAME(zr), i, (unsigned) mem,
396 (unsigned) virt_to_bus(mem));
397
398 /* Zero out the allocated memory */
399 memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
400 fh->v4l_buffers.buffer_size);
401#elif defined(BUZ_USE_HIMEM)
402
403 /* Use high memory which has been left at boot time */
404
405 /* Ok., Ok. this is an evil hack - we make
406 * the assumption that physical addresses are
407 * the same as bus addresses (true at least
408 * for Intel processors). The whole method of
409 * obtaining and using this memory is not very
410 * nice - but I hope it saves some poor users
411 * from kernel hacking, which might have even
412 * more evil results */
413
414 if (i == 0) {
415 int size =
416 fh->v4l_buffers.num_buffers *
417 fh->v4l_buffers.buffer_size;
418
419 pmem = get_high_mem(size);
420 if (pmem == 0) {
421 dprintk(1,
422 KERN_ERR
423 "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
424 ZR_DEVNAME(zr), size >> 10);
425 return -ENOBUFS;
426 }
427 fh->v4l_buffers.buffer[0].fbuffer = 0;
428 fh->v4l_buffers.buffer[0].fbuffer_phys =
429 pmem;
430 fh->v4l_buffers.buffer[0].fbuffer_bus =
431 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 = 0;
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 */
522
523static int
524jpg_fbuffer_alloc (struct file *file)
525{
526 struct zoran_fh *fh = file->private_data;
527 struct zoran *zr = fh->zr;
528 int i, j, off;
529 unsigned long mem;
530
531 /* we might have old buffers lying around */
532 if (fh->jpg_buffers.ready_to_be_freed) {
533 jpg_fbuffer_free(file);
534 }
535
536 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
537 if (fh->jpg_buffers.buffer[i].frag_tab)
538 dprintk(2,
539 KERN_WARNING
540 "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
541 ZR_DEVNAME(zr), i);
542
543 /* Allocate fragment table for this buffer */
544
545 mem = get_zeroed_page(GFP_KERNEL);
546 if (mem == 0) {
547 dprintk(1,
548 KERN_ERR
549 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
550 ZR_DEVNAME(zr), i);
551 jpg_fbuffer_free(file);
552 return -ENOBUFS;
553 }
554 memset((void *) mem, 0, PAGE_SIZE);
555 fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
556 fh->jpg_buffers.buffer[i].frag_tab_bus =
557 virt_to_bus((void *) mem);
558
559 //if (alloc_contig) {
560 if (fh->jpg_buffers.need_contiguous) {
561 mem =
562 (unsigned long) kmalloc(fh->jpg_buffers.
563 buffer_size,
564 GFP_KERNEL);
565 if (mem == 0) {
566 dprintk(1,
567 KERN_ERR
568 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
569 ZR_DEVNAME(zr), i);
570 jpg_fbuffer_free(file);
571 return -ENOBUFS;
572 }
573 fh->jpg_buffers.buffer[i].frag_tab[0] =
574 virt_to_bus((void *) mem);
575 fh->jpg_buffers.buffer[i].frag_tab[1] =
576 ((fh->jpg_buffers.buffer_size / 4) << 1) | 1;
577 for (off = 0; off < fh->jpg_buffers.buffer_size;
578 off += PAGE_SIZE)
579 SetPageReserved(MAP_NR(mem + off));
580 } else {
581 /* jpg_bufsize is allreay page aligned */
582 for (j = 0;
583 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
584 j++) {
585 mem = get_zeroed_page(GFP_KERNEL);
586 if (mem == 0) {
587 dprintk(1,
588 KERN_ERR
589 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
590 ZR_DEVNAME(zr), i);
591 jpg_fbuffer_free(file);
592 return -ENOBUFS;
593 }
594
595 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
596 virt_to_bus((void *) mem);
597 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
598 1] =
599 (PAGE_SIZE / 4) << 1;
600 SetPageReserved(MAP_NR(mem));
601 }
602
603 fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= 1;
604 }
605 }
606
607 dprintk(4,
608 KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
609 ZR_DEVNAME(zr),
610 (fh->jpg_buffers.num_buffers *
611 fh->jpg_buffers.buffer_size) >> 10);
612
613 fh->jpg_buffers.allocated = 1;
614
615 return 0;
616}
617
618/* free the MJPEG grab buffers */
619static void
620jpg_fbuffer_free (struct file *file)
621{
622 struct zoran_fh *fh = file->private_data;
623 struct zoran *zr = fh->zr;
624 int i, j, off;
625 unsigned char *mem;
626
627 dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
628
629 for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
630 if (!fh->jpg_buffers.buffer[i].frag_tab)
631 continue;
632
633 //if (alloc_contig) {
634 if (fh->jpg_buffers.need_contiguous) {
635 if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
636 mem =
637 (unsigned char *) bus_to_virt(fh->
638 jpg_buffers.
639 buffer
640 [i].
641 frag_tab
642 [0]);
643 for (off = 0;
644 off < fh->jpg_buffers.buffer_size;
645 off += PAGE_SIZE)
646 ClearPageReserved(MAP_NR
647 (mem + off));
648 kfree((void *) mem);
649 fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
650 fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
651 }
652 } else {
653 for (j = 0;
654 j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
655 j++) {
656 if (!fh->jpg_buffers.buffer[i].
657 frag_tab[2 * j])
658 break;
659 ClearPageReserved(MAP_NR
660 (bus_to_virt
661 (fh->jpg_buffers.
662 buffer[i].frag_tab[2 *
663 j])));
664 free_page((unsigned long)
665 bus_to_virt(fh->jpg_buffers.
666 buffer[i].
667 frag_tab[2 * j]));
668 fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
669 0;
670 fh->jpg_buffers.buffer[i].frag_tab[2 * j +
671 1] = 0;
672 }
673 }
674
675 free_page((unsigned long) fh->jpg_buffers.buffer[i].
676 frag_tab);
677 fh->jpg_buffers.buffer[i].frag_tab = NULL;
678 }
679
680 fh->jpg_buffers.allocated = 0;
681 fh->jpg_buffers.ready_to_be_freed = 0;
682}
683
684/*
685 * V4L Buffer grabbing
686 */
687
688static int
689zoran_v4l_set_format (struct file *file,
690 int width,
691 int height,
692 const struct zoran_format *format)
693{
694 struct zoran_fh *fh = file->private_data;
695 struct zoran *zr = fh->zr;
696 int bpp;
697
698 /* Check size and format of the grab wanted */
699
700 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
701 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
702 dprintk(1,
703 KERN_ERR
704 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
705 ZR_DEVNAME(zr), width, height);
706 return -EINVAL;
707 }
708
709 bpp = (format->depth + 7) / 8;
710
711 /* Check against available buffer size */
712 if (height * width * bpp > fh->v4l_buffers.buffer_size) {
713 dprintk(1,
714 KERN_ERR
715 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
716 ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
717 return -EINVAL;
718 }
719
720 /* The video front end needs 4-byte alinged line sizes */
721
722 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
723 dprintk(1,
724 KERN_ERR
725 "%s: v4l_set_format() - wrong frame alingment\n",
726 ZR_DEVNAME(zr));
727 return -EINVAL;
728 }
729
730 fh->v4l_settings.width = width;
731 fh->v4l_settings.height = height;
732 fh->v4l_settings.format = format;
733 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
734
735 return 0;
736}
737
738static int
739zoran_v4l_queue_frame (struct file *file,
740 int num)
741{
742 struct zoran_fh *fh = file->private_data;
743 struct zoran *zr = fh->zr;
744 unsigned long flags;
745 int res = 0;
746
747 if (!fh->v4l_buffers.allocated) {
748 dprintk(1,
749 KERN_ERR
750 "%s: v4l_queue_frame() - buffers not yet allocated\n",
751 ZR_DEVNAME(zr));
752 res = -ENOMEM;
753 }
754
755 /* No grabbing outside the buffer range! */
756 if (num >= fh->v4l_buffers.num_buffers || num < 0) {
757 dprintk(1,
758 KERN_ERR
759 "%s: v4l_queue_frame() - buffer %d is out of range\n",
760 ZR_DEVNAME(zr), num);
761 res = -EINVAL;
762 }
763
764 spin_lock_irqsave(&zr->spinlock, flags);
765
766 if (fh->v4l_buffers.active == ZORAN_FREE) {
767 if (zr->v4l_buffers.active == ZORAN_FREE) {
768 zr->v4l_buffers = fh->v4l_buffers;
769 fh->v4l_buffers.active = ZORAN_ACTIVE;
770 } else {
771 dprintk(1,
772 KERN_ERR
773 "%s: v4l_queue_frame() - another session is already capturing\n",
774 ZR_DEVNAME(zr));
775 res = -EBUSY;
776 }
777 }
778
779 /* make sure a grab isn't going on currently with this buffer */
780 if (!res) {
781 switch (zr->v4l_buffers.buffer[num].state) {
782 default:
783 case BUZ_STATE_PEND:
784 if (zr->v4l_buffers.active == ZORAN_FREE) {
785 fh->v4l_buffers.active = ZORAN_FREE;
786 zr->v4l_buffers.allocated = 0;
787 }
788 res = -EBUSY; /* what are you doing? */
789 break;
790 case BUZ_STATE_DONE:
791 dprintk(2,
792 KERN_WARNING
793 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
794 ZR_DEVNAME(zr), num);
795 case BUZ_STATE_USER:
796 /* since there is at least one unused buffer there's room for at least
797 * one more pend[] entry */
798 zr->v4l_pend[zr->v4l_pend_head++ &
799 V4L_MASK_FRAME] = num;
800 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
801 zr->v4l_buffers.buffer[num].bs.length =
802 fh->v4l_settings.bytesperline *
803 zr->v4l_settings.height;
804 fh->v4l_buffers.buffer[num] =
805 zr->v4l_buffers.buffer[num];
806 break;
807 }
808 }
809
810 spin_unlock_irqrestore(&zr->spinlock, flags);
811
812 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
813 zr->v4l_buffers.active = fh->v4l_buffers.active;
814
815 return res;
816}
817
818static int
819v4l_grab (struct file *file,
820 struct video_mmap *mp)
821{
822 struct zoran_fh *fh = file->private_data;
823 struct zoran *zr = fh->zr;
824 int res = 0, i;
825
826 for (i = 0; i < zoran_num_formats; i++) {
827 if (zoran_formats[i].palette == mp->format &&
828 zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
829 !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
830 break;
831 }
832 if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
833 dprintk(1,
834 KERN_ERR
835 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
836 ZR_DEVNAME(zr));
837 return -EINVAL;
838 }
839
840 /*
841 * To minimize the time spent in the IRQ routine, we avoid setting up
842 * the video front end there.
843 * If this grab has different parameters from a running streaming capture
844 * we stop the streaming capture and start it over again.
845 */
846 if (zr->v4l_memgrab_active &&
847 (zr->v4l_settings.width != mp->width ||
848 zr->v4l_settings.height != mp->height ||
849 zr->v4l_settings.format->palette != mp->format)) {
850 res = wait_grab_pending(zr);
851 if (res)
852 return res;
853 }
854 if ((res = zoran_v4l_set_format(file,
855 mp->width,
856 mp->height,
857 &zoran_formats[i])))
858 return res;
859 zr->v4l_settings = fh->v4l_settings;
860
861 /* queue the frame in the pending queue */
862 if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
863 fh->v4l_buffers.active = ZORAN_FREE;
864 return res;
865 }
866
867 /* put the 36057 into frame grabbing mode */
868 if (!res && !zr->v4l_memgrab_active)
869 zr36057_set_memgrab(zr, 1);
870
871 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
872
873 return res;
874}
875
876/*
877 * Sync on a V4L buffer
878 */
879
880static int
881v4l_sync (struct file *file,
882 int frame)
883{
884 struct zoran_fh *fh = file->private_data;
885 struct zoran *zr = fh->zr;
886 unsigned long flags;
887
888 if (fh->v4l_buffers.active == ZORAN_FREE) {
889 dprintk(1,
890 KERN_ERR
891 "%s: v4l_sync() - no grab active for this session\n",
892 ZR_DEVNAME(zr));
893 return -EINVAL;
894 }
895
896 /* check passed-in frame number */
897 if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
898 dprintk(1,
899 KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
900 ZR_DEVNAME(zr), frame);
901 return -EINVAL;
902 }
903
904 /* Check if is buffer was queued at all */
905 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
906 dprintk(1,
907 KERN_ERR
908 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
909 ZR_DEVNAME(zr));
910 return -EPROTO;
911 }
912
913 /* wait on this buffer to get ready */
914 while (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_PEND) {
915 if (!interruptible_sleep_on_timeout(&zr->v4l_capq, 10 * HZ))
916 return -ETIME;
917 else if (signal_pending(current))
918 return -ERESTARTSYS;
919 }
920
921 /* buffer should now be in BUZ_STATE_DONE */
922 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
923 dprintk(2,
924 KERN_ERR "%s: v4l_sync() - internal state error\n",
925 ZR_DEVNAME(zr));
926
927 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
928 fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
929
930 spin_lock_irqsave(&zr->spinlock, flags);
931
932 /* Check if streaming capture has finished */
933 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
934 zr36057_set_memgrab(zr, 0);
935 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
936 fh->v4l_buffers.active = zr->v4l_buffers.active =
937 ZORAN_FREE;
938 zr->v4l_buffers.allocated = 0;
939 }
940 }
941
942 spin_unlock_irqrestore(&zr->spinlock, flags);
943
944 return 0;
945}
946
947/*
948 * Queue a MJPEG buffer for capture/playback
949 */
950
951static int
952zoran_jpg_queue_frame (struct file *file,
953 int num,
954 enum zoran_codec_mode mode)
955{
956 struct zoran_fh *fh = file->private_data;
957 struct zoran *zr = fh->zr;
958 unsigned long flags;
959 int res = 0;
960
961 /* Check if buffers are allocated */
962 if (!fh->jpg_buffers.allocated) {
963 dprintk(1,
964 KERN_ERR
965 "%s: jpg_queue_frame() - buffers not yet allocated\n",
966 ZR_DEVNAME(zr));
967 return -ENOMEM;
968 }
969
970 /* No grabbing outside the buffer range! */
971 if (num >= fh->jpg_buffers.num_buffers || num < 0) {
972 dprintk(1,
973 KERN_ERR
974 "%s: jpg_queue_frame() - buffer %d out of range\n",
975 ZR_DEVNAME(zr), num);
976 return -EINVAL;
977 }
978
979 /* what is the codec mode right now? */
980 if (zr->codec_mode == BUZ_MODE_IDLE) {
981 zr->jpg_settings = fh->jpg_settings;
982 } else if (zr->codec_mode != mode) {
983 /* wrong codec mode active - invalid */
984 dprintk(1,
985 KERN_ERR
986 "%s: jpg_queue_frame() - codec in wrong mode\n",
987 ZR_DEVNAME(zr));
988 return -EINVAL;
989 }
990
991 spin_lock_irqsave(&zr->spinlock, flags);
992
993 if (fh->jpg_buffers.active == ZORAN_FREE) {
994 if (zr->jpg_buffers.active == ZORAN_FREE) {
995 zr->jpg_buffers = fh->jpg_buffers;
996 fh->jpg_buffers.active = ZORAN_ACTIVE;
997 } else {
998 dprintk(1,
999 KERN_ERR
1000 "%s: jpg_queue_frame() - another session is already capturing\n",
1001 ZR_DEVNAME(zr));
1002 res = -EBUSY;
1003 }
1004 }
1005
1006 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
1007 /* Ok load up the jpeg codec */
1008 zr36057_enable_jpg(zr, mode);
1009 }
1010
1011 if (!res) {
1012 switch (zr->jpg_buffers.buffer[num].state) {
1013 case BUZ_STATE_DONE:
1014 dprintk(2,
1015 KERN_WARNING
1016 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
1017 ZR_DEVNAME(zr));
1018 case BUZ_STATE_USER:
1019 /* since there is at least one unused buffer there's room for at
1020 *least one more pend[] entry */
1021 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
1022 num;
1023 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1024 fh->jpg_buffers.buffer[num] =
1025 zr->jpg_buffers.buffer[num];
1026 zoran_feed_stat_com(zr);
1027 break;
1028 default:
1029 case BUZ_STATE_DMA:
1030 case BUZ_STATE_PEND:
1031 if (zr->jpg_buffers.active == ZORAN_FREE) {
1032 fh->jpg_buffers.active = ZORAN_FREE;
1033 zr->jpg_buffers.allocated = 0;
1034 }
1035 res = -EBUSY; /* what are you doing? */
1036 break;
1037 }
1038 }
1039
1040 spin_unlock_irqrestore(&zr->spinlock, flags);
1041
1042 if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
1043 zr->jpg_buffers.active = fh->jpg_buffers.active;
1044 }
1045
1046 return res;
1047}
1048
1049static int
1050jpg_qbuf (struct file *file,
1051 int frame,
1052 enum zoran_codec_mode mode)
1053{
1054 struct zoran_fh *fh = file->private_data;
1055 struct zoran *zr = fh->zr;
1056 int res = 0;
1057
1058 /* Does the user want to stop streaming? */
1059 if (frame < 0) {
1060 if (zr->codec_mode == mode) {
1061 if (fh->jpg_buffers.active == ZORAN_FREE) {
1062 dprintk(1,
1063 KERN_ERR
1064 "%s: jpg_qbuf(-1) - session not active\n",
1065 ZR_DEVNAME(zr));
1066 return -EINVAL;
1067 }
1068 fh->jpg_buffers.active = zr->jpg_buffers.active =
1069 ZORAN_FREE;
1070 zr->jpg_buffers.allocated = 0;
1071 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1072 return 0;
1073 } else {
1074 dprintk(1,
1075 KERN_ERR
1076 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
1077 ZR_DEVNAME(zr));
1078 return -EINVAL;
1079 }
1080 }
1081
1082 if ((res = zoran_jpg_queue_frame(file, frame, mode)))
1083 return res;
1084
1085 /* Start the jpeg codec when the first frame is queued */
1086 if (!res && zr->jpg_que_head == 1)
1087 jpeg_start(zr);
1088
1089 return res;
1090}
1091
1092/*
1093 * Sync on a MJPEG buffer
1094 */
1095
1096static int
1097jpg_sync (struct file *file,
1098 struct zoran_sync *bs)
1099{
1100 struct zoran_fh *fh = file->private_data;
1101 struct zoran *zr = fh->zr;
1102 unsigned long flags;
1103 int frame, timeout;
1104
1105 if (fh->jpg_buffers.active == ZORAN_FREE) {
1106 dprintk(1,
1107 KERN_ERR
1108 "%s: jpg_sync() - capture is not currently active\n",
1109 ZR_DEVNAME(zr));
1110 return -EINVAL;
1111 }
1112 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
1113 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
1114 dprintk(1,
1115 KERN_ERR
1116 "%s: jpg_sync() - codec not in streaming mode\n",
1117 ZR_DEVNAME(zr));
1118 return -EINVAL;
1119 }
1120 while (zr->jpg_que_tail == zr->jpg_dma_tail) {
1121 if (zr->jpg_dma_tail == zr->jpg_dma_head)
1122 break;
1123
1124 timeout =
1125 interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
1126 if (!timeout) {
1127 int isr;
1128
1129 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1130 udelay(1);
1131 zr->codec->control(zr->codec, CODEC_G_STATUS,
1132 sizeof(isr), &isr);
1133 dprintk(1,
1134 KERN_ERR
1135 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
1136 ZR_DEVNAME(zr), isr);
1137
1138 return -ETIME;
1139
1140 } else if (signal_pending(current))
1141 return -ERESTARTSYS;
1142 }
1143
1144 spin_lock_irqsave(&zr->spinlock, flags);
1145
1146 if (zr->jpg_dma_tail != zr->jpg_dma_head)
1147 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
1148 else
1149 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1150
1151 /* buffer should now be in BUZ_STATE_DONE */
1152 if (*zr_debug > 0)
1153 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
1154 dprintk(2,
1155 KERN_ERR
1156 "%s: jpg_sync() - internal state error\n",
1157 ZR_DEVNAME(zr));
1158
1159 *bs = zr->jpg_buffers.buffer[frame].bs;
1160 bs->frame = frame;
1161 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1162 fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1163
1164 spin_unlock_irqrestore(&zr->spinlock, flags);
1165
1166 return 0;
1167}
1168
1169static void
1170zoran_open_init_session (struct file *file)
1171{
1172 int i;
1173 struct zoran_fh *fh = file->private_data;
1174 struct zoran *zr = fh->zr;
1175
1176 /* Per default, map the V4L Buffers */
1177 fh->map_mode = ZORAN_MAP_MODE_RAW;
1178
1179 /* take over the card's current settings */
1180 fh->overlay_settings = zr->overlay_settings;
1181 fh->overlay_settings.is_set = 0;
1182 fh->overlay_settings.format = zr->overlay_settings.format;
1183 fh->overlay_active = ZORAN_FREE;
1184
1185 /* v4l settings */
1186 fh->v4l_settings = zr->v4l_settings;
1187
1188 /* v4l_buffers */
1189 memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
1190 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1191 fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1192 fh->v4l_buffers.buffer[i].bs.frame = i;
1193 }
1194 fh->v4l_buffers.allocated = 0;
1195 fh->v4l_buffers.ready_to_be_freed = 0;
1196 fh->v4l_buffers.active = ZORAN_FREE;
1197 fh->v4l_buffers.buffer_size = v4l_bufsize;
1198 fh->v4l_buffers.num_buffers = v4l_nbufs;
1199
1200 /* jpg settings */
1201 fh->jpg_settings = zr->jpg_settings;
1202
1203 /* jpg_buffers */
1204 memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
1205 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1206 fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1207 fh->jpg_buffers.buffer[i].bs.frame = i;
1208 }
1209 fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
1210 fh->jpg_buffers.allocated = 0;
1211 fh->jpg_buffers.ready_to_be_freed = 0;
1212 fh->jpg_buffers.active = ZORAN_FREE;
1213 fh->jpg_buffers.buffer_size = jpg_bufsize;
1214 fh->jpg_buffers.num_buffers = jpg_nbufs;
1215}
1216
1217static void
1218zoran_close_end_session (struct file *file)
1219{
1220 struct zoran_fh *fh = file->private_data;
1221 struct zoran *zr = fh->zr;
1222
1223 /* overlay */
1224 if (fh->overlay_active != ZORAN_FREE) {
1225 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
1226 zr->v4l_overlay_active = 0;
1227 if (!zr->v4l_memgrab_active)
1228 zr36057_overlay(zr, 0);
1229 zr->overlay_mask = NULL;
1230 }
1231
1232 /* v4l capture */
1233 if (fh->v4l_buffers.active != ZORAN_FREE) {
1234 zr36057_set_memgrab(zr, 0);
1235 zr->v4l_buffers.allocated = 0;
1236 zr->v4l_buffers.active = fh->v4l_buffers.active =
1237 ZORAN_FREE;
1238 }
1239
1240 /* v4l buffers */
1241 if (fh->v4l_buffers.allocated ||
1242 fh->v4l_buffers.ready_to_be_freed) {
1243 v4l_fbuffer_free(file);
1244 }
1245
1246 /* jpg capture */
1247 if (fh->jpg_buffers.active != ZORAN_FREE) {
1248 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1249 zr->jpg_buffers.allocated = 0;
1250 zr->jpg_buffers.active = fh->jpg_buffers.active =
1251 ZORAN_FREE;
1252 }
1253
1254 /* jpg buffers */
1255 if (fh->jpg_buffers.allocated ||
1256 fh->jpg_buffers.ready_to_be_freed) {
1257 jpg_fbuffer_free(file);
1258 }
1259}
1260
1261/*
1262 * Open a zoran card. Right now the flags stuff is just playing
1263 */
1264
1265static int
1266zoran_open (struct inode *inode,
1267 struct file *file)
1268{
1269 unsigned int minor = iminor(inode);
1270 struct zoran *zr = NULL;
1271 struct zoran_fh *fh;
1272 int i, res, first_open = 0, have_module_locks = 0;
1273
1274 /* find the device */
1275 for (i = 0; i < zoran_num; i++) {
1276 if (zoran[i].video_dev->minor == minor) {
1277 zr = &zoran[i];
1278 break;
1279 }
1280 }
1281
1282 if (!zr) {
1283 dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
1284 res = -ENODEV;
1285 goto open_unlock_and_return;
1286 }
1287
1288 /* see fs/device.c - the kernel already locks during open(),
1289 * so locking ourselves only causes deadlocks */
1290 /*down(&zr->resource_lock);*/
1291
1292 if (!zr->decoder) {
1293 dprintk(1,
1294 KERN_ERR "%s: no TV decoder loaded for device!\n",
1295 ZR_DEVNAME(zr));
1296 res = -EIO;
1297 goto open_unlock_and_return;
1298 }
1299
1300 /* try to grab a module lock */
1301 if (!try_module_get(THIS_MODULE)) {
1302 dprintk(1,
1303 KERN_ERR
1304 "%s: failed to acquire my own lock! PANIC!\n",
1305 ZR_DEVNAME(zr));
1306 res = -ENODEV;
1307 goto open_unlock_and_return;
1308 }
1309 if (!try_module_get(zr->decoder->driver->owner)) {
1310 dprintk(1,
1311 KERN_ERR
1312 "%s: failed to grab ownership of i2c decoder\n",
1313 ZR_DEVNAME(zr));
1314 res = -EIO;
1315 module_put(THIS_MODULE);
1316 goto open_unlock_and_return;
1317 }
1318 if (zr->encoder &&
1319 !try_module_get(zr->encoder->driver->owner)) {
1320 dprintk(1,
1321 KERN_ERR
1322 "%s: failed to grab ownership of i2c encoder\n",
1323 ZR_DEVNAME(zr));
1324 res = -EIO;
1325 module_put(zr->decoder->driver->owner);
1326 module_put(THIS_MODULE);
1327 goto open_unlock_and_return;
1328 }
1329
1330 have_module_locks = 1;
1331
1332 if (zr->user >= 2048) {
1333 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
1334 ZR_DEVNAME(zr), zr->user);
1335 res = -EBUSY;
1336 goto open_unlock_and_return;
1337 }
1338
1339 dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1340 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1341
1342 /* now, create the open()-specific file_ops struct */
1343 fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1344 if (!fh) {
1345 dprintk(1,
1346 KERN_ERR
1347 "%s: zoran_open() - allocation of zoran_fh failed\n",
1348 ZR_DEVNAME(zr));
1349 res = -ENOMEM;
1350 goto open_unlock_and_return;
1351 }
1352 memset(fh, 0, sizeof(struct zoran_fh));
1353 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1354 * on norm-change! */
1355 fh->overlay_mask =
1356 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
1357 if (!fh->overlay_mask) {
1358 dprintk(1,
1359 KERN_ERR
1360 "%s: zoran_open() - allocation of overlay_mask failed\n",
1361 ZR_DEVNAME(zr));
1362 kfree(fh);
1363 res = -ENOMEM;
1364 goto open_unlock_and_return;
1365 }
1366
1367 if (zr->user++ == 0)
1368 first_open = 1;
1369
1370 /*up(&zr->resource_lock);*/
1371
1372 /* default setup - TODO: look at flags */
1373 if (first_open) { /* First device open */
1374 zr36057_restart(zr);
1375 zoran_open_init_params(zr);
1376 zoran_init_hardware(zr);
1377
1378 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
1379 }
1380
1381 /* set file_ops stuff */
1382 file->private_data = fh;
1383 fh->zr = zr;
1384 zoran_open_init_session(file);
1385
1386 return 0;
1387
1388open_unlock_and_return:
1389 /* if we grabbed locks, release them accordingly */
1390 if (have_module_locks) {
1391 module_put(zr->decoder->driver->owner);
1392 if (zr->encoder) {
1393 module_put(zr->encoder->driver->owner);
1394 }
1395 module_put(THIS_MODULE);
1396 }
1397
1398 /* if there's no device found, we didn't obtain the lock either */
1399 if (zr) {
1400 /*up(&zr->resource_lock);*/
1401 }
1402
1403 return res;
1404}
1405
1406static int
1407zoran_close (struct inode *inode,
1408 struct file *file)
1409{
1410 struct zoran_fh *fh = file->private_data;
1411 struct zoran *zr = fh->zr;
1412
1413 dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1414 ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
1415
1416 /* kernel locks (fs/device.c), so don't do that ourselves
1417 * (prevents deadlocks) */
1418 /*down(&zr->resource_lock);*/
1419
1420 zoran_close_end_session(file);
1421
1422 if (zr->user-- == 1) { /* Last process */
1423 /* Clean up JPEG process */
1424 wake_up_interruptible(&zr->jpg_capq);
1425 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1426 zr->jpg_buffers.allocated = 0;
1427 zr->jpg_buffers.active = ZORAN_FREE;
1428
1429 /* disable interrupts */
1430 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1431
1432 if (*zr_debug > 1)
1433 print_interrupts(zr);
1434
1435 /* Overlay off */
1436 zr->v4l_overlay_active = 0;
1437 zr36057_overlay(zr, 0);
1438 zr->overlay_mask = NULL;
1439
1440 /* capture off */
1441 wake_up_interruptible(&zr->v4l_capq);
1442 zr36057_set_memgrab(zr, 0);
1443 zr->v4l_buffers.allocated = 0;
1444 zr->v4l_buffers.active = ZORAN_FREE;
1445 zoran_set_pci_master(zr, 0);
1446
1447 if (!pass_through) { /* Switch to color bar */
1448 int zero = 0, two = 2;
1449 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1450 encoder_command(zr, ENCODER_SET_INPUT, &two);
1451 }
1452 }
1453
1454 file->private_data = NULL;
1455 kfree(fh->overlay_mask);
1456 kfree(fh);
1457
1458 /* release locks on the i2c modules */
1459 module_put(zr->decoder->driver->owner);
1460 if (zr->encoder) {
1461 module_put(zr->encoder->driver->owner);
1462 }
1463 module_put(THIS_MODULE);
1464
1465 /*up(&zr->resource_lock);*/
1466
1467 dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
1468
1469 return 0;
1470}
1471
1472
1473static ssize_t
1474zoran_read (struct file *file,
1475 char *data,
1476 size_t count,
1477 loff_t *ppos)
1478{
1479 /* we simply don't support read() (yet)... */
1480
1481 return -EINVAL;
1482}
1483
1484static ssize_t
1485zoran_write (struct file *file,
1486 const char *data,
1487 size_t count,
1488 loff_t *ppos)
1489{
1490 /* ...and the same goes for write() */
1491
1492 return -EINVAL;
1493}
1494
1495static int
1496setup_fbuffer (struct file *file,
1497 void *base,
1498 const struct zoran_format *fmt,
1499 int width,
1500 int height,
1501 int bytesperline)
1502{
1503 struct zoran_fh *fh = file->private_data;
1504 struct zoran *zr = fh->zr;
1505
1506 /* (Ronald) v4l/v4l2 guidelines */
1507 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1508 return -EPERM;
1509
1510 /* we need a bytesperline value, even if not given */
1511 if (!bytesperline)
1512 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1513
1514#if 0
1515 if (zr->overlay_active) {
1516 /* dzjee... stupid users... don't even bother to turn off
1517 * overlay before changing the memory location...
1518 * normally, we would return errors here. However, one of
1519 * the tools that does this is... xawtv! and since xawtv
1520 * is used by +/- 99% of the users, we'd rather be user-
1521 * friendly and silently do as if nothing went wrong */
1522 dprintk(3,
1523 KERN_ERR
1524 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1525 ZR_DEVNAME(zr));
1526 zr36057_overlay(zr, 0);
1527 }
1528#endif
1529
1530 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1531 dprintk(1,
1532 KERN_ERR
1533 "%s: setup_fbuffer() - no valid overlay format given\n",
1534 ZR_DEVNAME(zr));
1535 return -EINVAL;
1536 }
1537 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1538 dprintk(1,
1539 KERN_ERR
1540 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1541 ZR_DEVNAME(zr), width, height, bytesperline);
1542 return -EINVAL;
1543 }
1544 if (bytesperline & 3) {
1545 dprintk(1,
1546 KERN_ERR
1547 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1548 ZR_DEVNAME(zr), bytesperline);
1549 return -EINVAL;
1550 }
1551
1552 zr->buffer.base = (void *) ((unsigned long) base & ~3);
1553 zr->buffer.height = height;
1554 zr->buffer.width = width;
1555 zr->buffer.depth = fmt->depth;
1556 zr->overlay_settings.format = fmt;
1557 zr->buffer.bytesperline = bytesperline;
1558
1559 /* The user should set new window parameters */
1560 zr->overlay_settings.is_set = 0;
1561
1562 return 0;
1563}
1564
1565
1566static int
1567setup_window (struct file *file,
1568 int x,
1569 int y,
1570 int width,
1571 int height,
1572 struct video_clip *clips,
1573 int clipcount,
1574 void *bitmap)
1575{
1576 struct zoran_fh *fh = file->private_data;
1577 struct zoran *zr = fh->zr;
1578 struct video_clip *vcp = NULL;
1579 int on, end;
1580
1581
1582 if (!zr->buffer.base) {
1583 dprintk(1,
1584 KERN_ERR
1585 "%s: setup_window() - frame buffer has to be set first\n",
1586 ZR_DEVNAME(zr));
1587 return -EINVAL;
1588 }
1589
1590 if (!fh->overlay_settings.format) {
1591 dprintk(1,
1592 KERN_ERR
1593 "%s: setup_window() - no overlay format set\n",
1594 ZR_DEVNAME(zr));
1595 return -EINVAL;
1596 }
1597
1598 /*
1599 * The video front end needs 4-byte alinged line sizes, we correct that
1600 * silently here if necessary
1601 */
1602 if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
1603 end = (x + width) & ~1; /* round down */
1604 x = (x + 1) & ~1; /* round up */
1605 width = end - x;
1606 }
1607
1608 if (zr->buffer.depth == 24) {
1609 end = (x + width) & ~3; /* round down */
1610 x = (x + 3) & ~3; /* round up */
1611 width = end - x;
1612 }
1613
1614 if (width > BUZ_MAX_WIDTH)
1615 width = BUZ_MAX_WIDTH;
1616 if (height > BUZ_MAX_HEIGHT)
1617 height = BUZ_MAX_HEIGHT;
1618
1619 /* Check for vaild parameters */
1620 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1621 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1622 dprintk(1,
1623 KERN_ERR
1624 "%s: setup_window() - width = %d or height = %d invalid\n",
1625 ZR_DEVNAME(zr), width, height);
1626 return -EINVAL;
1627 }
1628
1629 fh->overlay_settings.x = x;
1630 fh->overlay_settings.y = y;
1631 fh->overlay_settings.width = width;
1632 fh->overlay_settings.height = height;
1633 fh->overlay_settings.clipcount = clipcount;
1634
1635 /*
1636 * If an overlay is running, we have to switch it off
1637 * and switch it on again in order to get the new settings in effect.
1638 *
1639 * We also want to avoid that the overlay mask is written
1640 * when an overlay is running.
1641 */
1642
1643 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1644 zr->overlay_active != ZORAN_FREE &&
1645 fh->overlay_active != ZORAN_FREE;
1646 if (on)
1647 zr36057_overlay(zr, 0);
1648
1649 /*
1650 * Write the overlay mask if clips are wanted.
1651 * We prefer a bitmap.
1652 */
1653 if (bitmap) {
1654 /* fake value - it just means we want clips */
1655 fh->overlay_settings.clipcount = 1;
1656
1657 if (copy_from_user(fh->overlay_mask, bitmap,
1658 (width * height + 7) / 8)) {
1659 return -EFAULT;
1660 }
1661 } else if (clipcount > 0) {
1662 /* write our own bitmap from the clips */
1663 vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
1664 if (vcp == NULL) {
1665 dprintk(1,
1666 KERN_ERR
1667 "%s: setup_window() - Alloc of clip mask failed\n",
1668 ZR_DEVNAME(zr));
1669 return -ENOMEM;
1670 }
1671 if (copy_from_user
1672 (vcp, clips, sizeof(struct video_clip) * clipcount)) {
1673 vfree(vcp);
1674 return -EFAULT;
1675 }
1676 write_overlay_mask(file, vcp, clipcount);
1677 vfree(vcp);
1678 }
1679
1680 fh->overlay_settings.is_set = 1;
1681 if (fh->overlay_active != ZORAN_FREE &&
1682 zr->overlay_active != ZORAN_FREE)
1683 zr->overlay_settings = fh->overlay_settings;
1684
1685 if (on)
1686 zr36057_overlay(zr, 1);
1687
1688 /* Make sure the changes come into effect */
1689 return wait_grab_pending(zr);
1690}
1691
1692static int
1693setup_overlay (struct file *file,
1694 int on)
1695{
1696 struct zoran_fh *fh = file->private_data;
1697 struct zoran *zr = fh->zr;
1698
1699 /* If there is nothing to do, return immediatly */
1700 if ((on && fh->overlay_active != ZORAN_FREE) ||
1701 (!on && fh->overlay_active == ZORAN_FREE))
1702 return 0;
1703
1704 /* check whether we're touching someone else's overlay */
1705 if (on && zr->overlay_active != ZORAN_FREE &&
1706 fh->overlay_active == ZORAN_FREE) {
1707 dprintk(1,
1708 KERN_ERR
1709 "%s: setup_overlay() - overlay is already active for another session\n",
1710 ZR_DEVNAME(zr));
1711 return -EBUSY;
1712 }
1713 if (!on && zr->overlay_active != ZORAN_FREE &&
1714 fh->overlay_active == ZORAN_FREE) {
1715 dprintk(1,
1716 KERN_ERR
1717 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1718 ZR_DEVNAME(zr));
1719 return -EPERM;
1720 }
1721
1722 if (on == 0) {
1723 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1724 zr->v4l_overlay_active = 0;
1725 /* When a grab is running, the video simply
1726 * won't be switched on any more */
1727 if (!zr->v4l_memgrab_active)
1728 zr36057_overlay(zr, 0);
1729 zr->overlay_mask = NULL;
1730 } else {
1731 if (!zr->buffer.base || !fh->overlay_settings.is_set) {
1732 dprintk(1,
1733 KERN_ERR
1734 "%s: setup_overlay() - buffer or window not set\n",
1735 ZR_DEVNAME(zr));
1736 return -EINVAL;
1737 }
1738 if (!fh->overlay_settings.format) {
1739 dprintk(1,
1740 KERN_ERR
1741 "%s: setup_overlay() - no overlay format set\n",
1742 ZR_DEVNAME(zr));
1743 return -EINVAL;
1744 }
1745 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1746 zr->v4l_overlay_active = 1;
1747 zr->overlay_mask = fh->overlay_mask;
1748 zr->overlay_settings = fh->overlay_settings;
1749 if (!zr->v4l_memgrab_active)
1750 zr36057_overlay(zr, 1);
1751 /* When a grab is running, the video will be
1752 * switched on when grab is finished */
1753 }
1754
1755 /* Make sure the changes come into effect */
1756 return wait_grab_pending(zr);
1757}
1758
1759#ifdef HAVE_V4L2
1760 /* get the status of a buffer in the clients buffer queue */
1761static int
1762zoran_v4l2_buffer_status (struct file *file,
1763 struct v4l2_buffer *buf,
1764 int num)
1765{
1766 struct zoran_fh *fh = file->private_data;
1767 struct zoran *zr = fh->zr;
1768
1769 buf->flags = V4L2_BUF_FLAG_MAPPED;
1770
1771 switch (fh->map_mode) {
1772 case ZORAN_MAP_MODE_RAW:
1773
1774 /* check range */
1775 if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
1776 !fh->v4l_buffers.allocated) {
1777 dprintk(1,
1778 KERN_ERR
1779 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1780 ZR_DEVNAME(zr));
1781 return -EINVAL;
1782 }
1783
1784 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1785 buf->length = fh->v4l_buffers.buffer_size;
1786
1787 /* get buffer */
1788 buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
1789 if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
1790 fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
1791 buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
1792 buf->flags |= V4L2_BUF_FLAG_DONE;
1793 buf->timestamp =
1794 fh->v4l_buffers.buffer[num].bs.timestamp;
1795 } else {
1796 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1797 }
1798
1799 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1800 buf->field = V4L2_FIELD_TOP;
1801 else
1802 buf->field = V4L2_FIELD_INTERLACED;
1803
1804 break;
1805
1806 case ZORAN_MAP_MODE_JPG_REC:
1807 case ZORAN_MAP_MODE_JPG_PLAY:
1808
1809 /* check range */
1810 if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
1811 !fh->jpg_buffers.allocated) {
1812 dprintk(1,
1813 KERN_ERR
1814 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1815 ZR_DEVNAME(zr));
1816 return -EINVAL;
1817 }
1818
1819 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1820 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1821 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1822 buf->length = fh->jpg_buffers.buffer_size;
1823
1824 /* these variables are only written after frame has been captured */
1825 if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
1826 fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
1827 buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
1828 buf->timestamp =
1829 fh->jpg_buffers.buffer[num].bs.timestamp;
1830 buf->bytesused =
1831 fh->jpg_buffers.buffer[num].bs.length;
1832 buf->flags |= V4L2_BUF_FLAG_DONE;
1833 } else {
1834 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1835 }
1836
1837 /* which fields are these? */
1838 if (fh->jpg_settings.TmpDcm != 1)
1839 buf->field =
1840 fh->jpg_settings.
1841 odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1842 else
1843 buf->field =
1844 fh->jpg_settings.
1845 odd_even ? V4L2_FIELD_SEQ_TB :
1846 V4L2_FIELD_SEQ_BT;
1847
1848 break;
1849
1850 default:
1851
1852 dprintk(5,
1853 KERN_ERR
1854 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1855 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1856 return -EINVAL;
1857 }
1858
1859 buf->memory = V4L2_MEMORY_MMAP;
1860 buf->index = num;
1861 buf->m.offset = buf->length * num;
1862
1863 return 0;
1864}
1865#endif
1866
1867static int
1868zoran_set_norm (struct zoran *zr,
1869 int norm) /* VIDEO_MODE_* */
1870{
1871 int norm_encoder, on;
1872
1873 if (zr->v4l_buffers.active != ZORAN_FREE ||
1874 zr->jpg_buffers.active != ZORAN_FREE) {
1875 dprintk(1,
1876 KERN_WARNING
1877 "%s: set_norm() called while in playback/capture mode\n",
1878 ZR_DEVNAME(zr));
1879 return -EBUSY;
1880 }
1881
1882 if (lock_norm && norm != zr->norm) {
1883 if (lock_norm > 1) {
1884 dprintk(1,
1885 KERN_WARNING
1886 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1887 ZR_DEVNAME(zr));
1888 return -EPERM;
1889 } else {
1890 dprintk(1,
1891 KERN_WARNING
1892 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1893 ZR_DEVNAME(zr));
1894 norm = zr->norm;
1895 }
1896 }
1897
1898 if (norm != VIDEO_MODE_AUTO &&
1899 (norm < 0 || norm >= zr->card.norms ||
1900 !zr->card.tvn[norm])) {
1901 dprintk(1,
1902 KERN_ERR "%s: set_norm() - unsupported norm %d\n",
1903 ZR_DEVNAME(zr), norm);
1904 return -EINVAL;
1905 }
1906
1907 if (norm == VIDEO_MODE_AUTO) {
1908 int status;
1909
1910 /* if we have autodetect, ... */
1911 struct video_decoder_capability caps;
1912 decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
1913 if (!(caps.flags & VIDEO_DECODER_AUTO)) {
1914 dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
1915 ZR_DEVNAME(zr));
1916 return -EINVAL;
1917 }
1918
1919 decoder_command(zr, DECODER_SET_NORM, &norm);
1920
1921 /* let changes come into effect */
1922 current->state = TASK_UNINTERRUPTIBLE;
1923 schedule_timeout(2 * HZ);
1924
1925 decoder_command(zr, DECODER_GET_STATUS, &status);
1926 if (!(status & DECODER_STATUS_GOOD)) {
1927 dprintk(1,
1928 KERN_ERR
1929 "%s: set_norm() - no norm detected\n",
1930 ZR_DEVNAME(zr));
1931 /* reset norm */
1932 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1933 return -EIO;
1934 }
1935
1936 if (status & DECODER_STATUS_NTSC)
1937 norm = VIDEO_MODE_NTSC;
1938 else if (status & DECODER_STATUS_SECAM)
1939 norm = VIDEO_MODE_SECAM;
1940 else
1941 norm = VIDEO_MODE_PAL;
1942 }
1943 zr->timing = zr->card.tvn[norm];
1944 norm_encoder = norm;
1945
1946 /* We switch overlay …
Large files files are truncated, but you can click here to view the full file