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