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