]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/zoran/zoran_driver.c
Merge branch 'topic/lx6464es' into for-linus
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / zoran / zoran_driver.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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>
1da177e4
LT
55
56#include <linux/interrupt.h>
57#include <linux/i2c.h>
58#include <linux/i2c-algo-bit.h>
59
60#include <linux/spinlock.h>
1da177e4 61
7e0a16f6 62#include <linux/videodev.h>
5e87efa3 63#include <media/v4l2-common.h>
35ea11ff 64#include <media/v4l2-ioctl.h>
1da177e4
LT
65#include "videocodec.h"
66
9a6ab769 67#include <asm/byteorder.h>
1da177e4
LT
68#include <asm/io.h>
69#include <asm/uaccess.h>
70#include <linux/proc_fs.h>
71
384c3689 72#include <linux/mutex.h>
1da177e4
LT
73#include "zoran.h"
74#include "zoran_device.h"
75#include "zoran_card.h"
76
603d6f2c
TP
77
78const struct zoran_format zoran_formats[] = {
79 {
80 .name = "15-bit RGB LE",
602dd48a
HV
81 .fourcc = V4L2_PIX_FMT_RGB555,
82 .colorspace = V4L2_COLORSPACE_SRGB,
1da177e4
LT
83 .depth = 15,
84 .flags = ZORAN_FORMAT_CAPTURE |
85 ZORAN_FORMAT_OVERLAY,
603d6f2c
TP
86 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
87 ZR36057_VFESPFR_LittleEndian,
1da177e4 88 }, {
603d6f2c 89 .name = "15-bit RGB BE",
602dd48a
HV
90 .fourcc = V4L2_PIX_FMT_RGB555X,
91 .colorspace = V4L2_COLORSPACE_SRGB,
603d6f2c
TP
92 .depth = 15,
93 .flags = ZORAN_FORMAT_CAPTURE |
94 ZORAN_FORMAT_OVERLAY,
95 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
96 }, {
97 .name = "16-bit RGB LE",
602dd48a
HV
98 .fourcc = V4L2_PIX_FMT_RGB565,
99 .colorspace = V4L2_COLORSPACE_SRGB,
1da177e4
LT
100 .depth = 16,
101 .flags = ZORAN_FORMAT_CAPTURE |
102 ZORAN_FORMAT_OVERLAY,
603d6f2c
TP
103 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
104 ZR36057_VFESPFR_LittleEndian,
105 }, {
106 .name = "16-bit RGB BE",
602dd48a
HV
107 .fourcc = V4L2_PIX_FMT_RGB565X,
108 .colorspace = V4L2_COLORSPACE_SRGB,
603d6f2c
TP
109 .depth = 16,
110 .flags = ZORAN_FORMAT_CAPTURE |
111 ZORAN_FORMAT_OVERLAY,
112 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
1da177e4
LT
113 }, {
114 .name = "24-bit RGB",
602dd48a
HV
115 .fourcc = V4L2_PIX_FMT_BGR24,
116 .colorspace = V4L2_COLORSPACE_SRGB,
1da177e4
LT
117 .depth = 24,
118 .flags = ZORAN_FORMAT_CAPTURE |
119 ZORAN_FORMAT_OVERLAY,
603d6f2c 120 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
1da177e4 121 }, {
603d6f2c 122 .name = "32-bit RGB LE",
602dd48a
HV
123 .fourcc = V4L2_PIX_FMT_BGR32,
124 .colorspace = V4L2_COLORSPACE_SRGB,
1da177e4
LT
125 .depth = 32,
126 .flags = ZORAN_FORMAT_CAPTURE |
127 ZORAN_FORMAT_OVERLAY,
603d6f2c
TP
128 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
129 }, {
130 .name = "32-bit RGB BE",
602dd48a
HV
131 .fourcc = V4L2_PIX_FMT_RGB32,
132 .colorspace = V4L2_COLORSPACE_SRGB,
603d6f2c
TP
133 .depth = 32,
134 .flags = ZORAN_FORMAT_CAPTURE |
135 ZORAN_FORMAT_OVERLAY,
136 .vfespfr = ZR36057_VFESPFR_RGB888,
1da177e4
LT
137 }, {
138 .name = "4:2:2, packed, YUYV",
602dd48a
HV
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1da177e4
LT
141 .depth = 16,
142 .flags = ZORAN_FORMAT_CAPTURE |
d56410e0 143 ZORAN_FORMAT_OVERLAY,
603d6f2c
TP
144 .vfespfr = ZR36057_VFESPFR_YUV422,
145 }, {
146 .name = "4:2:2, packed, UYVY",
602dd48a
HV
147 .fourcc = V4L2_PIX_FMT_UYVY,
148 .colorspace = V4L2_COLORSPACE_SMPTE170M,
603d6f2c
TP
149 .depth = 16,
150 .flags = ZORAN_FORMAT_CAPTURE |
151 ZORAN_FORMAT_OVERLAY,
152 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
1da177e4
LT
153 }, {
154 .name = "Hardware-encoded Motion-JPEG",
602dd48a
HV
155 .fourcc = V4L2_PIX_FMT_MJPEG,
156 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1da177e4
LT
157 .depth = 0,
158 .flags = ZORAN_FORMAT_CAPTURE |
159 ZORAN_FORMAT_PLAYBACK |
160 ZORAN_FORMAT_COMPRESSED,
161 }
162};
603d6f2c 163#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
1da177e4 164
1da177e4
LT
165 /* small helper function for calculating buffersizes for v4l2
166 * we calculate the nearest higher power-of-two, which
167 * will be the recommended buffersize */
168static __u32
169zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
170{
171 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
172 __u32 num = (1024 * 512) / (div);
173 __u32 result = 2;
174
175 num--;
176 while (num) {
177 num >>= 1;
178 result <<= 1;
179 }
180
181 if (result > jpg_bufsize)
182 return jpg_bufsize;
183 if (result < 8192)
184 return 8192;
185 return result;
186}
1da177e4
LT
187
188/* forward references */
e5ee3f64
TP
189static void v4l_fbuffer_free(struct zoran_fh *fh);
190static void jpg_fbuffer_free(struct zoran_fh *fh);
1da177e4 191
1159b7f1
TP
192/* Set mapping mode */
193static void map_mode_raw(struct zoran_fh *fh)
194{
195 fh->map_mode = ZORAN_MAP_MODE_RAW;
196 fh->buffers.buffer_size = v4l_bufsize;
197 fh->buffers.num_buffers = v4l_nbufs;
198}
199static void map_mode_jpg(struct zoran_fh *fh, int play)
200{
201 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
202 fh->buffers.buffer_size = jpg_bufsize;
203 fh->buffers.num_buffers = jpg_nbufs;
204}
205static inline const char *mode_name(enum zoran_map_mode mode)
206{
207 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
208}
209
1da177e4
LT
210/*
211 * Allocate the V4L grab buffers
212 *
213 * These have to be pysically contiguous.
1da177e4
LT
214 */
215
e5ee3f64 216static int v4l_fbuffer_alloc(struct zoran_fh *fh)
1da177e4 217{
1da177e4
LT
218 struct zoran *zr = fh->zr;
219 int i, off;
220 unsigned char *mem;
1da177e4 221
1159b7f1
TP
222 for (i = 0; i < fh->buffers.num_buffers; i++) {
223 if (fh->buffers.buffer[i].v4l.fbuffer)
1da177e4
LT
224 dprintk(2,
225 KERN_WARNING
c61402ba
TP
226 "%s: %s - buffer %d already allocated!?\n",
227 ZR_DEVNAME(zr), __func__, i);
1da177e4
LT
228
229 //udelay(20);
ee9a9d66
JD
230 mem = kmalloc(fh->buffers.buffer_size,
231 GFP_KERNEL | __GFP_NOWARN);
b80696b7
HV
232 if (!mem) {
233 dprintk(1,
234 KERN_ERR
c61402ba
TP
235 "%s: %s - kmalloc for V4L buf %d failed\n",
236 ZR_DEVNAME(zr), __func__, i);
e5ee3f64 237 v4l_fbuffer_free(fh);
b80696b7 238 return -ENOBUFS;
1da177e4 239 }
1159b7f1
TP
240 fh->buffers.buffer[i].v4l.fbuffer = mem;
241 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
242 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
243 for (off = 0; off < fh->buffers.buffer_size;
b80696b7 244 off += PAGE_SIZE)
602dd48a 245 SetPageReserved(virt_to_page(mem + off));
b80696b7
HV
246 dprintk(4,
247 KERN_INFO
8737f66e 248 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
c61402ba 249 ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
8737f66e 250 (unsigned long long)virt_to_bus(mem));
1da177e4
LT
251 }
252
1159b7f1 253 fh->buffers.allocated = 1;
1da177e4
LT
254
255 return 0;
256}
257
258/* free the V4L grab buffers */
e5ee3f64 259static void v4l_fbuffer_free(struct zoran_fh *fh)
1da177e4 260{
1da177e4
LT
261 struct zoran *zr = fh->zr;
262 int i, off;
263 unsigned char *mem;
264
c61402ba 265 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
1da177e4 266
1159b7f1
TP
267 for (i = 0; i < fh->buffers.num_buffers; i++) {
268 if (!fh->buffers.buffer[i].v4l.fbuffer)
1da177e4
LT
269 continue;
270
1159b7f1
TP
271 mem = fh->buffers.buffer[i].v4l.fbuffer;
272 for (off = 0; off < fh->buffers.buffer_size;
b80696b7 273 off += PAGE_SIZE)
602dd48a 274 ClearPageReserved(virt_to_page(mem + off));
1159b7f1
TP
275 kfree(fh->buffers.buffer[i].v4l.fbuffer);
276 fh->buffers.buffer[i].v4l.fbuffer = NULL;
1da177e4
LT
277 }
278
1159b7f1 279 fh->buffers.allocated = 0;
1da177e4
LT
280}
281
282/*
283 * Allocate the MJPEG grab buffers.
284 *
1da177e4
LT
285 * If a Natoma chipset is present and this is a revision 1 zr36057,
286 * each MJPEG buffer needs to be physically contiguous.
287 * (RJ: This statement is from Dave Perks' original driver,
288 * I could never check it because I have a zr36067)
1da177e4
LT
289 *
290 * RJ: The contents grab buffers needs never be accessed in the driver.
291 * Therefore there is no need to allocate them with vmalloc in order
292 * to get a contiguous virtual memory space.
293 * I don't understand why many other drivers first allocate them with
294 * vmalloc (which uses internally also get_zeroed_page, but delivers you
295 * virtual addresses) and then again have to make a lot of efforts
296 * to get the physical address.
297 *
298 * Ben Capper:
299 * On big-endian architectures (such as ppc) some extra steps
300 * are needed. When reading and writing to the stat_com array
301 * and fragment buffers, the device expects to see little-
302 * endian values. The use of cpu_to_le32() and le32_to_cpu()
303 * in this function (and one or two others in zoran_device.c)
304 * ensure that these values are always stored in little-endian
305 * form, regardless of architecture. The zr36057 does Very Bad
306 * Things on big endian architectures if the stat_com array
307 * and fragment buffers are not little-endian.
308 */
309
e5ee3f64 310static int jpg_fbuffer_alloc(struct zoran_fh *fh)
1da177e4 311{
1da177e4
LT
312 struct zoran *zr = fh->zr;
313 int i, j, off;
1159b7f1 314 u8 *mem;
1da177e4 315
1159b7f1
TP
316 for (i = 0; i < fh->buffers.num_buffers; i++) {
317 if (fh->buffers.buffer[i].jpg.frag_tab)
1da177e4
LT
318 dprintk(2,
319 KERN_WARNING
c61402ba
TP
320 "%s: %s - buffer %d already allocated!?\n",
321 ZR_DEVNAME(zr), __func__, i);
1da177e4
LT
322
323 /* Allocate fragment table for this buffer */
324
1159b7f1 325 mem = (void *)get_zeroed_page(GFP_KERNEL);
1da177e4
LT
326 if (mem == 0) {
327 dprintk(1,
328 KERN_ERR
c61402ba
TP
329 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
330 ZR_DEVNAME(zr), __func__, i);
e5ee3f64 331 jpg_fbuffer_free(fh);
1da177e4
LT
332 return -ENOBUFS;
333 }
1159b7f1
TP
334 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
335 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
336
337 if (fh->buffers.need_contiguous) {
338 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
339 if (mem == NULL) {
1da177e4
LT
340 dprintk(1,
341 KERN_ERR
c61402ba
TP
342 "%s: %s - kmalloc failed for buffer %d\n",
343 ZR_DEVNAME(zr), __func__, i);
e5ee3f64 344 jpg_fbuffer_free(fh);
1da177e4
LT
345 return -ENOBUFS;
346 }
1159b7f1
TP
347 fh->buffers.buffer[i].jpg.frag_tab[0] =
348 cpu_to_le32(virt_to_bus(mem));
349 fh->buffers.buffer[i].jpg.frag_tab[1] =
350 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
351 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
602dd48a 352 SetPageReserved(virt_to_page(mem + off));
1da177e4 353 } else {
b80696b7 354 /* jpg_bufsize is already page aligned */
1159b7f1
TP
355 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
356 mem = (void *)get_zeroed_page(GFP_KERNEL);
357 if (mem == NULL) {
1da177e4
LT
358 dprintk(1,
359 KERN_ERR
c61402ba
TP
360 "%s: %s - get_zeroed_page failed for buffer %d\n",
361 ZR_DEVNAME(zr), __func__, i);
e5ee3f64 362 jpg_fbuffer_free(fh);
1da177e4
LT
363 return -ENOBUFS;
364 }
365
1159b7f1
TP
366 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
367 cpu_to_le32(virt_to_bus(mem));
368 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
369 cpu_to_le32((PAGE_SIZE >> 2) << 1);
602dd48a 370 SetPageReserved(virt_to_page(mem));
1da177e4
LT
371 }
372
1159b7f1 373 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
1da177e4
LT
374 }
375 }
376
377 dprintk(4,
c61402ba
TP
378 KERN_DEBUG "%s: %s - %d KB allocated\n",
379 ZR_DEVNAME(zr), __func__,
1159b7f1 380 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
1da177e4 381
1159b7f1 382 fh->buffers.allocated = 1;
1da177e4
LT
383
384 return 0;
385}
386
387/* free the MJPEG grab buffers */
e5ee3f64 388static void jpg_fbuffer_free(struct zoran_fh *fh)
1da177e4 389{
1da177e4
LT
390 struct zoran *zr = fh->zr;
391 int i, j, off;
392 unsigned char *mem;
602dd48a 393 __le32 frag_tab;
1159b7f1 394 struct zoran_buffer *buffer;
1da177e4 395
c61402ba 396 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
1da177e4 397
1159b7f1
TP
398 for (i = 0, buffer = &fh->buffers.buffer[0];
399 i < fh->buffers.num_buffers; i++, buffer++) {
400 if (!buffer->jpg.frag_tab)
1da177e4
LT
401 continue;
402
1159b7f1
TP
403 if (fh->buffers.need_contiguous) {
404 frag_tab = buffer->jpg.frag_tab[0];
602dd48a
HV
405
406 if (frag_tab) {
1159b7f1
TP
407 mem = bus_to_virt(le32_to_cpu(frag_tab));
408 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
602dd48a 409 ClearPageReserved(virt_to_page(mem + off));
f9101210 410 kfree(mem);
1159b7f1
TP
411 buffer->jpg.frag_tab[0] = 0;
412 buffer->jpg.frag_tab[1] = 0;
1da177e4
LT
413 }
414 } else {
1159b7f1
TP
415 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
416 frag_tab = buffer->jpg.frag_tab[2 * j];
602dd48a
HV
417
418 if (!frag_tab)
1da177e4 419 break;
602dd48a
HV
420 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
421 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
1159b7f1
TP
422 buffer->jpg.frag_tab[2 * j] = 0;
423 buffer->jpg.frag_tab[2 * j + 1] = 0;
1da177e4
LT
424 }
425 }
426
1159b7f1
TP
427 free_page((unsigned long)buffer->jpg.frag_tab);
428 buffer->jpg.frag_tab = NULL;
1da177e4
LT
429 }
430
1159b7f1 431 fh->buffers.allocated = 0;
1da177e4
LT
432}
433
434/*
435 * V4L Buffer grabbing
436 */
437
438static int
1159b7f1 439zoran_v4l_set_format (struct zoran_fh *fh,
1da177e4
LT
440 int width,
441 int height,
442 const struct zoran_format *format)
443{
1da177e4
LT
444 struct zoran *zr = fh->zr;
445 int bpp;
446
447 /* Check size and format of the grab wanted */
448
449 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
450 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
451 dprintk(1,
452 KERN_ERR
c61402ba
TP
453 "%s: %s - wrong frame size (%dx%d)\n",
454 ZR_DEVNAME(zr), __func__, width, height);
1da177e4
LT
455 return -EINVAL;
456 }
457
458 bpp = (format->depth + 7) / 8;
459
460 /* Check against available buffer size */
1159b7f1 461 if (height * width * bpp > fh->buffers.buffer_size) {
1da177e4
LT
462 dprintk(1,
463 KERN_ERR
c61402ba
TP
464 "%s: %s - video buffer size (%d kB) is too small\n",
465 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
1da177e4
LT
466 return -EINVAL;
467 }
468
469 /* The video front end needs 4-byte alinged line sizes */
470
471 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
472 dprintk(1,
473 KERN_ERR
c61402ba
TP
474 "%s: %s - wrong frame alignment\n",
475 ZR_DEVNAME(zr), __func__);
1da177e4
LT
476 return -EINVAL;
477 }
478
479 fh->v4l_settings.width = width;
480 fh->v4l_settings.height = height;
481 fh->v4l_settings.format = format;
482 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
483
484 return 0;
485}
486
e5ee3f64 487static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
1da177e4 488{
1da177e4
LT
489 struct zoran *zr = fh->zr;
490 unsigned long flags;
491 int res = 0;
492
1159b7f1 493 if (!fh->buffers.allocated) {
1da177e4
LT
494 dprintk(1,
495 KERN_ERR
c61402ba
TP
496 "%s: %s - buffers not yet allocated\n",
497 ZR_DEVNAME(zr), __func__);
1da177e4
LT
498 res = -ENOMEM;
499 }
500
501 /* No grabbing outside the buffer range! */
1159b7f1 502 if (num >= fh->buffers.num_buffers || num < 0) {
1da177e4
LT
503 dprintk(1,
504 KERN_ERR
c61402ba
TP
505 "%s: %s - buffer %d is out of range\n",
506 ZR_DEVNAME(zr), __func__, num);
1da177e4
LT
507 res = -EINVAL;
508 }
509
510 spin_lock_irqsave(&zr->spinlock, flags);
511
1159b7f1 512 if (fh->buffers.active == ZORAN_FREE) {
1da177e4 513 if (zr->v4l_buffers.active == ZORAN_FREE) {
1159b7f1
TP
514 zr->v4l_buffers = fh->buffers;
515 fh->buffers.active = ZORAN_ACTIVE;
1da177e4
LT
516 } else {
517 dprintk(1,
518 KERN_ERR
c61402ba
TP
519 "%s: %s - another session is already capturing\n",
520 ZR_DEVNAME(zr), __func__);
1da177e4
LT
521 res = -EBUSY;
522 }
523 }
524
525 /* make sure a grab isn't going on currently with this buffer */
526 if (!res) {
527 switch (zr->v4l_buffers.buffer[num].state) {
528 default:
529 case BUZ_STATE_PEND:
530 if (zr->v4l_buffers.active == ZORAN_FREE) {
1159b7f1 531 fh->buffers.active = ZORAN_FREE;
1da177e4
LT
532 zr->v4l_buffers.allocated = 0;
533 }
534 res = -EBUSY; /* what are you doing? */
535 break;
536 case BUZ_STATE_DONE:
537 dprintk(2,
538 KERN_WARNING
c61402ba
TP
539 "%s: %s - queueing buffer %d in state DONE!?\n",
540 ZR_DEVNAME(zr), __func__, num);
1da177e4
LT
541 case BUZ_STATE_USER:
542 /* since there is at least one unused buffer there's room for at least
543 * one more pend[] entry */
1159b7f1 544 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
1da177e4
LT
545 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
546 zr->v4l_buffers.buffer[num].bs.length =
547 fh->v4l_settings.bytesperline *
548 zr->v4l_settings.height;
1159b7f1 549 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
1da177e4
LT
550 break;
551 }
552 }
553
554 spin_unlock_irqrestore(&zr->spinlock, flags);
555
556 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
1159b7f1 557 zr->v4l_buffers.active = fh->buffers.active;
1da177e4
LT
558
559 return res;
560}
561
1da177e4
LT
562/*
563 * Sync on a V4L buffer
564 */
565
e5ee3f64 566static int v4l_sync(struct zoran_fh *fh, int frame)
1da177e4 567{
1da177e4
LT
568 struct zoran *zr = fh->zr;
569 unsigned long flags;
570
1159b7f1 571 if (fh->buffers.active == ZORAN_FREE) {
1da177e4
LT
572 dprintk(1,
573 KERN_ERR
c61402ba
TP
574 "%s: %s - no grab active for this session\n",
575 ZR_DEVNAME(zr), __func__);
1da177e4
LT
576 return -EINVAL;
577 }
578
579 /* check passed-in frame number */
1159b7f1 580 if (frame >= fh->buffers.num_buffers || frame < 0) {
1da177e4 581 dprintk(1,
c61402ba
TP
582 KERN_ERR "%s: %s - frame %d is invalid\n",
583 ZR_DEVNAME(zr), __func__, frame);
1da177e4
LT
584 return -EINVAL;
585 }
586
587 /* Check if is buffer was queued at all */
588 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
589 dprintk(1,
590 KERN_ERR
c61402ba
TP
591 "%s: %s - attempt to sync on a buffer which was not queued?\n",
592 ZR_DEVNAME(zr), __func__);
1da177e4
LT
593 return -EPROTO;
594 }
595
596 /* wait on this buffer to get ready */
597 if (!wait_event_interruptible_timeout(zr->v4l_capq,
1159b7f1 598 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
1da177e4
LT
599 return -ETIME;
600 if (signal_pending(current))
601 return -ERESTARTSYS;
602
603 /* buffer should now be in BUZ_STATE_DONE */
604 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
605 dprintk(2,
c61402ba
TP
606 KERN_ERR "%s: %s - internal state error\n",
607 ZR_DEVNAME(zr), __func__);
1da177e4
LT
608
609 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
1159b7f1 610 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
1da177e4
LT
611
612 spin_lock_irqsave(&zr->spinlock, flags);
613
614 /* Check if streaming capture has finished */
615 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
616 zr36057_set_memgrab(zr, 0);
617 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
1159b7f1 618 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
1da177e4
LT
619 zr->v4l_buffers.allocated = 0;
620 }
621 }
622
623 spin_unlock_irqrestore(&zr->spinlock, flags);
624
625 return 0;
626}
627
628/*
629 * Queue a MJPEG buffer for capture/playback
630 */
631
e5ee3f64
TP
632static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
633 enum zoran_codec_mode mode)
1da177e4 634{
1da177e4
LT
635 struct zoran *zr = fh->zr;
636 unsigned long flags;
637 int res = 0;
638
639 /* Check if buffers are allocated */
1159b7f1 640 if (!fh->buffers.allocated) {
1da177e4
LT
641 dprintk(1,
642 KERN_ERR
c61402ba
TP
643 "%s: %s - buffers not yet allocated\n",
644 ZR_DEVNAME(zr), __func__);
1da177e4
LT
645 return -ENOMEM;
646 }
647
648 /* No grabbing outside the buffer range! */
1159b7f1 649 if (num >= fh->buffers.num_buffers || num < 0) {
1da177e4
LT
650 dprintk(1,
651 KERN_ERR
c61402ba
TP
652 "%s: %s - buffer %d out of range\n",
653 ZR_DEVNAME(zr), __func__, num);
1da177e4
LT
654 return -EINVAL;
655 }
656
657 /* what is the codec mode right now? */
658 if (zr->codec_mode == BUZ_MODE_IDLE) {
659 zr->jpg_settings = fh->jpg_settings;
660 } else if (zr->codec_mode != mode) {
661 /* wrong codec mode active - invalid */
662 dprintk(1,
663 KERN_ERR
c61402ba
TP
664 "%s: %s - codec in wrong mode\n",
665 ZR_DEVNAME(zr), __func__);
1da177e4
LT
666 return -EINVAL;
667 }
668
1159b7f1 669 if (fh->buffers.active == ZORAN_FREE) {
1da177e4 670 if (zr->jpg_buffers.active == ZORAN_FREE) {
1159b7f1
TP
671 zr->jpg_buffers = fh->buffers;
672 fh->buffers.active = ZORAN_ACTIVE;
1da177e4
LT
673 } else {
674 dprintk(1,
675 KERN_ERR
c61402ba
TP
676 "%s: %s - another session is already capturing\n",
677 ZR_DEVNAME(zr), __func__);
1da177e4
LT
678 res = -EBUSY;
679 }
680 }
681
682 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
683 /* Ok load up the jpeg codec */
684 zr36057_enable_jpg(zr, mode);
685 }
686
8bc3efcf
RB
687 spin_lock_irqsave(&zr->spinlock, flags);
688
1da177e4
LT
689 if (!res) {
690 switch (zr->jpg_buffers.buffer[num].state) {
691 case BUZ_STATE_DONE:
692 dprintk(2,
693 KERN_WARNING
c61402ba
TP
694 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
695 ZR_DEVNAME(zr), __func__);
1da177e4
LT
696 case BUZ_STATE_USER:
697 /* since there is at least one unused buffer there's room for at
698 *least one more pend[] entry */
1159b7f1 699 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
1da177e4 700 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
1159b7f1 701 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
1da177e4
LT
702 zoran_feed_stat_com(zr);
703 break;
704 default:
705 case BUZ_STATE_DMA:
706 case BUZ_STATE_PEND:
707 if (zr->jpg_buffers.active == ZORAN_FREE) {
1159b7f1 708 fh->buffers.active = ZORAN_FREE;
1da177e4
LT
709 zr->jpg_buffers.allocated = 0;
710 }
711 res = -EBUSY; /* what are you doing? */
712 break;
713 }
714 }
715
716 spin_unlock_irqrestore(&zr->spinlock, flags);
717
1159b7f1
TP
718 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
719 zr->jpg_buffers.active = fh->buffers.active;
1da177e4
LT
720
721 return res;
722}
723
e5ee3f64 724static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
1da177e4 725{
1da177e4
LT
726 struct zoran *zr = fh->zr;
727 int res = 0;
728
729 /* Does the user want to stop streaming? */
730 if (frame < 0) {
731 if (zr->codec_mode == mode) {
1159b7f1 732 if (fh->buffers.active == ZORAN_FREE) {
1da177e4
LT
733 dprintk(1,
734 KERN_ERR
c61402ba
TP
735 "%s: %s(-1) - session not active\n",
736 ZR_DEVNAME(zr), __func__);
1da177e4
LT
737 return -EINVAL;
738 }
1159b7f1 739 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
1da177e4
LT
740 zr->jpg_buffers.allocated = 0;
741 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
742 return 0;
743 } else {
744 dprintk(1,
745 KERN_ERR
c61402ba
TP
746 "%s: %s - stop streaming but not in streaming mode\n",
747 ZR_DEVNAME(zr), __func__);
1da177e4
LT
748 return -EINVAL;
749 }
750 }
751
e5ee3f64 752 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
1da177e4
LT
753 return res;
754
755 /* Start the jpeg codec when the first frame is queued */
756 if (!res && zr->jpg_que_head == 1)
757 jpeg_start(zr);
758
759 return res;
760}
761
762/*
763 * Sync on a MJPEG buffer
764 */
765
e5ee3f64 766static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
1da177e4 767{
1da177e4
LT
768 struct zoran *zr = fh->zr;
769 unsigned long flags;
770 int frame;
771
1159b7f1 772 if (fh->buffers.active == ZORAN_FREE) {
1da177e4
LT
773 dprintk(1,
774 KERN_ERR
c61402ba
TP
775 "%s: %s - capture is not currently active\n",
776 ZR_DEVNAME(zr), __func__);
1da177e4
LT
777 return -EINVAL;
778 }
779 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
780 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
781 dprintk(1,
782 KERN_ERR
c61402ba
TP
783 "%s: %s - codec not in streaming mode\n",
784 ZR_DEVNAME(zr), __func__);
1da177e4
LT
785 return -EINVAL;
786 }
787 if (!wait_event_interruptible_timeout(zr->jpg_capq,
788 (zr->jpg_que_tail != zr->jpg_dma_tail ||
789 zr->jpg_dma_tail == zr->jpg_dma_head),
790 10*HZ)) {
791 int isr;
792
793 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
794 udelay(1);
795 zr->codec->control(zr->codec, CODEC_G_STATUS,
796 sizeof(isr), &isr);
797 dprintk(1,
798 KERN_ERR
c61402ba
TP
799 "%s: %s - timeout: codec isr=0x%02x\n",
800 ZR_DEVNAME(zr), __func__, isr);
1da177e4
LT
801
802 return -ETIME;
803
804 }
805 if (signal_pending(current))
806 return -ERESTARTSYS;
807
808 spin_lock_irqsave(&zr->spinlock, flags);
809
810 if (zr->jpg_dma_tail != zr->jpg_dma_head)
811 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
812 else
813 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
814
815 /* buffer should now be in BUZ_STATE_DONE */
18b548ca
JD
816 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
817 dprintk(2,
c61402ba
TP
818 KERN_ERR "%s: %s - internal state error\n",
819 ZR_DEVNAME(zr), __func__);
1da177e4
LT
820
821 *bs = zr->jpg_buffers.buffer[frame].bs;
822 bs->frame = frame;
823 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
1159b7f1 824 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
1da177e4
LT
825
826 spin_unlock_irqrestore(&zr->spinlock, flags);
827
828 return 0;
829}
830
e5ee3f64 831static void zoran_open_init_session(struct zoran_fh *fh)
1da177e4
LT
832{
833 int i;
1da177e4
LT
834 struct zoran *zr = fh->zr;
835
836 /* Per default, map the V4L Buffers */
1159b7f1 837 map_mode_raw(fh);
1da177e4
LT
838
839 /* take over the card's current settings */
840 fh->overlay_settings = zr->overlay_settings;
841 fh->overlay_settings.is_set = 0;
842 fh->overlay_settings.format = zr->overlay_settings.format;
843 fh->overlay_active = ZORAN_FREE;
844
845 /* v4l settings */
846 fh->v4l_settings = zr->v4l_settings;
1da177e4
LT
847 /* jpg settings */
848 fh->jpg_settings = zr->jpg_settings;
849
1159b7f1
TP
850 /* buffers */
851 memset(&fh->buffers, 0, sizeof(fh->buffers));
852 for (i = 0; i < MAX_FRAME; i++) {
853 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
854 fh->buffers.buffer[i].bs.frame = i;
1da177e4 855 }
1159b7f1
TP
856 fh->buffers.allocated = 0;
857 fh->buffers.active = ZORAN_FREE;
1da177e4
LT
858}
859
e5ee3f64 860static void zoran_close_end_session(struct zoran_fh *fh)
1da177e4 861{
1da177e4
LT
862 struct zoran *zr = fh->zr;
863
864 /* overlay */
865 if (fh->overlay_active != ZORAN_FREE) {
866 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
867 zr->v4l_overlay_active = 0;
868 if (!zr->v4l_memgrab_active)
869 zr36057_overlay(zr, 0);
870 zr->overlay_mask = NULL;
871 }
872
1159b7f1
TP
873 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
874 /* v4l capture */
875 if (fh->buffers.active != ZORAN_FREE) {
876 unsigned long flags;
9896bbc1 877
1159b7f1
TP
878 spin_lock_irqsave(&zr->spinlock, flags);
879 zr36057_set_memgrab(zr, 0);
880 zr->v4l_buffers.allocated = 0;
881 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
882 spin_unlock_irqrestore(&zr->spinlock, flags);
883 }
1da177e4 884
1159b7f1
TP
885 /* v4l buffers */
886 if (fh->buffers.allocated)
e5ee3f64 887 v4l_fbuffer_free(fh);
1159b7f1
TP
888 } else {
889 /* jpg capture */
890 if (fh->buffers.active != ZORAN_FREE) {
891 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
892 zr->jpg_buffers.allocated = 0;
893 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
894 }
1da177e4 895
1159b7f1
TP
896 /* jpg buffers */
897 if (fh->buffers.allocated)
e5ee3f64 898 jpg_fbuffer_free(fh);
1da177e4 899 }
1da177e4
LT
900}
901
902/*
903 * Open a zoran card. Right now the flags stuff is just playing
904 */
905
601139e0 906static int zoran_open(struct file *file)
1da177e4 907{
601139e0 908 struct zoran *zr = video_drvdata(file);
1da177e4 909 struct zoran_fh *fh;
fc96ab73
TP
910 int res, first_open = 0;
911
c61402ba
TP
912 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
913 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
1da177e4 914
d56dc612 915 lock_kernel();
1da177e4 916
fc96ab73
TP
917 if (zr->user >= 2048) {
918 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
919 ZR_DEVNAME(zr), zr->user);
920 res = -EBUSY;
921 goto fail_unlock;
922 }
923
1da177e4 924 /* now, create the open()-specific file_ops struct */
7408187d 925 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
1da177e4
LT
926 if (!fh) {
927 dprintk(1,
928 KERN_ERR
c61402ba
TP
929 "%s: %s - allocation of zoran_fh failed\n",
930 ZR_DEVNAME(zr), __func__);
1da177e4 931 res = -ENOMEM;
0ab6e1c3 932 goto fail_unlock;
1da177e4 933 }
1da177e4
LT
934 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
935 * on norm-change! */
936 fh->overlay_mask =
937 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
938 if (!fh->overlay_mask) {
939 dprintk(1,
940 KERN_ERR
c61402ba
TP
941 "%s: %s - allocation of overlay_mask failed\n",
942 ZR_DEVNAME(zr), __func__);
1da177e4 943 res = -ENOMEM;
fc96ab73 944 goto fail_fh;
1da177e4
LT
945 }
946
947 if (zr->user++ == 0)
948 first_open = 1;
949
384c3689 950 /*mutex_unlock(&zr->resource_lock);*/
1da177e4
LT
951
952 /* default setup - TODO: look at flags */
953 if (first_open) { /* First device open */
954 zr36057_restart(zr);
955 zoran_open_init_params(zr);
956 zoran_init_hardware(zr);
957
958 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
959 }
960
961 /* set file_ops stuff */
962 file->private_data = fh;
963 fh->zr = zr;
e5ee3f64 964 zoran_open_init_session(fh);
d56dc612 965 unlock_kernel();
1da177e4
LT
966
967 return 0;
968
fc96ab73
TP
969fail_fh:
970 kfree(fh);
fc96ab73 971fail_unlock:
d56dc612 972 unlock_kernel();
1da177e4 973
fc96ab73
TP
974 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
975 ZR_DEVNAME(zr), res, zr->user);
976
1da177e4
LT
977 return res;
978}
979
980static int
bec43661 981zoran_close(struct file *file)
1da177e4
LT
982{
983 struct zoran_fh *fh = file->private_data;
984 struct zoran *zr = fh->zr;
985
c61402ba
TP
986 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
987 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
1da177e4
LT
988
989 /* kernel locks (fs/device.c), so don't do that ourselves
990 * (prevents deadlocks) */
384c3689 991 /*mutex_lock(&zr->resource_lock);*/
1da177e4 992
e5ee3f64 993 zoran_close_end_session(fh);
1da177e4
LT
994
995 if (zr->user-- == 1) { /* Last process */
996 /* Clean up JPEG process */
997 wake_up_interruptible(&zr->jpg_capq);
998 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
999 zr->jpg_buffers.allocated = 0;
1000 zr->jpg_buffers.active = ZORAN_FREE;
1001
1002 /* disable interrupts */
1003 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1004
18b548ca 1005 if (zr36067_debug > 1)
1da177e4
LT
1006 print_interrupts(zr);
1007
1008 /* Overlay off */
1009 zr->v4l_overlay_active = 0;
1010 zr36057_overlay(zr, 0);
1011 zr->overlay_mask = NULL;
1012
1013 /* capture off */
1014 wake_up_interruptible(&zr->v4l_capq);
1015 zr36057_set_memgrab(zr, 0);
1016 zr->v4l_buffers.allocated = 0;
1017 zr->v4l_buffers.active = ZORAN_FREE;
1018 zoran_set_pci_master(zr, 0);
1019
1020 if (!pass_through) { /* Switch to color bar */
0ab6e1c3 1021 decoder_call(zr, video, s_stream, 0);
5325b427 1022 encoder_call(zr, video, s_routing, 2, 0, 0);
1da177e4
LT
1023 }
1024 }
1025
1026 file->private_data = NULL;
1027 kfree(fh->overlay_mask);
1028 kfree(fh);
1029
c61402ba 1030 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
1da177e4
LT
1031
1032 return 0;
1033}
1034
1035
1036static ssize_t
1037zoran_read (struct file *file,
1038 char __user *data,
1039 size_t count,
1040 loff_t *ppos)
1041{
1042 /* we simply don't support read() (yet)... */
1043
1044 return -EINVAL;
1045}
1046
1047static ssize_t
1048zoran_write (struct file *file,
1049 const char __user *data,
1050 size_t count,
1051 loff_t *ppos)
1052{
1053 /* ...and the same goes for write() */
1054
1055 return -EINVAL;
1056}
1057
e5ee3f64 1058static int setup_fbuffer(struct zoran_fh *fh,
1da177e4
LT
1059 void *base,
1060 const struct zoran_format *fmt,
1061 int width,
1062 int height,
1063 int bytesperline)
1064{
1da177e4
LT
1065 struct zoran *zr = fh->zr;
1066
1067 /* (Ronald) v4l/v4l2 guidelines */
1068 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1069 return -EPERM;
1070
e355880b
AC
1071 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1072 ALi Magik (that needs very low latency while the card needs a
1073 higher value always) */
1074
1075 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1076 return -ENXIO;
1077
1da177e4
LT
1078 /* we need a bytesperline value, even if not given */
1079 if (!bytesperline)
1080 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1081
1082#if 0
1083 if (zr->overlay_active) {
1084 /* dzjee... stupid users... don't even bother to turn off
1085 * overlay before changing the memory location...
1086 * normally, we would return errors here. However, one of
1087 * the tools that does this is... xawtv! and since xawtv
1088 * is used by +/- 99% of the users, we'd rather be user-
1089 * friendly and silently do as if nothing went wrong */
1090 dprintk(3,
1091 KERN_ERR
c61402ba
TP
1092 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1093 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1094 zr36057_overlay(zr, 0);
1095 }
1096#endif
1097
1098 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1099 dprintk(1,
1100 KERN_ERR
c61402ba
TP
1101 "%s: %s - no valid overlay format given\n",
1102 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1103 return -EINVAL;
1104 }
1105 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1106 dprintk(1,
1107 KERN_ERR
c61402ba
TP
1108 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1109 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
1da177e4
LT
1110 return -EINVAL;
1111 }
1112 if (bytesperline & 3) {
1113 dprintk(1,
1114 KERN_ERR
c61402ba
TP
1115 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1116 ZR_DEVNAME(zr), __func__, bytesperline);
1da177e4
LT
1117 return -EINVAL;
1118 }
1119
7f6adeaf
HV
1120 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1121 zr->vbuf_height = height;
1122 zr->vbuf_width = width;
1123 zr->vbuf_depth = fmt->depth;
1da177e4 1124 zr->overlay_settings.format = fmt;
7f6adeaf 1125 zr->vbuf_bytesperline = bytesperline;
1da177e4
LT
1126
1127 /* The user should set new window parameters */
1128 zr->overlay_settings.is_set = 0;
1129
1130 return 0;
1131}
1132
1133
e5ee3f64
TP
1134static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
1135 struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
1da177e4 1136{
1da177e4 1137 struct zoran *zr = fh->zr;
7f6adeaf 1138 struct v4l2_clip *vcp = NULL;
1da177e4
LT
1139 int on, end;
1140
1141
7f6adeaf 1142 if (!zr->vbuf_base) {
1da177e4
LT
1143 dprintk(1,
1144 KERN_ERR
c61402ba
TP
1145 "%s: %s - frame buffer has to be set first\n",
1146 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1147 return -EINVAL;
1148 }
1149
1150 if (!fh->overlay_settings.format) {
1151 dprintk(1,
1152 KERN_ERR
c61402ba
TP
1153 "%s: %s - no overlay format set\n",
1154 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1155 return -EINVAL;
1156 }
1157
1158 /*
1159 * The video front end needs 4-byte alinged line sizes, we correct that
1160 * silently here if necessary
1161 */
7f6adeaf 1162 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1da177e4
LT
1163 end = (x + width) & ~1; /* round down */
1164 x = (x + 1) & ~1; /* round up */
1165 width = end - x;
1166 }
1167
7f6adeaf 1168 if (zr->vbuf_depth == 24) {
1da177e4
LT
1169 end = (x + width) & ~3; /* round down */
1170 x = (x + 3) & ~3; /* round up */
1171 width = end - x;
1172 }
1173
1174 if (width > BUZ_MAX_WIDTH)
1175 width = BUZ_MAX_WIDTH;
1176 if (height > BUZ_MAX_HEIGHT)
1177 height = BUZ_MAX_HEIGHT;
1178
1179 /* Check for vaild parameters */
1180 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1181 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1182 dprintk(1,
1183 KERN_ERR
c61402ba
TP
1184 "%s: %s - width = %d or height = %d invalid\n",
1185 ZR_DEVNAME(zr), __func__, width, height);
1da177e4
LT
1186 return -EINVAL;
1187 }
1188
1189 fh->overlay_settings.x = x;
1190 fh->overlay_settings.y = y;
1191 fh->overlay_settings.width = width;
1192 fh->overlay_settings.height = height;
1193 fh->overlay_settings.clipcount = clipcount;
1194
1195 /*
1196 * If an overlay is running, we have to switch it off
1197 * and switch it on again in order to get the new settings in effect.
1198 *
1199 * We also want to avoid that the overlay mask is written
1200 * when an overlay is running.
1201 */
1202
1203 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1204 zr->overlay_active != ZORAN_FREE &&
1205 fh->overlay_active != ZORAN_FREE;
1206 if (on)
1207 zr36057_overlay(zr, 0);
1208
1209 /*
1210 * Write the overlay mask if clips are wanted.
1211 * We prefer a bitmap.
1212 */
1213 if (bitmap) {
1214 /* fake value - it just means we want clips */
1215 fh->overlay_settings.clipcount = 1;
1216
1217 if (copy_from_user(fh->overlay_mask, bitmap,
1218 (width * height + 7) / 8)) {
1219 return -EFAULT;
1220 }
1221 } else if (clipcount > 0) {
1222 /* write our own bitmap from the clips */
7f6adeaf 1223 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1da177e4
LT
1224 if (vcp == NULL) {
1225 dprintk(1,
1226 KERN_ERR
c61402ba
TP
1227 "%s: %s - Alloc of clip mask failed\n",
1228 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1229 return -ENOMEM;
1230 }
1231 if (copy_from_user
7f6adeaf 1232 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1da177e4
LT
1233 vfree(vcp);
1234 return -EFAULT;
1235 }
e5ee3f64 1236 write_overlay_mask(fh, vcp, clipcount);
1da177e4
LT
1237 vfree(vcp);
1238 }
1239
1240 fh->overlay_settings.is_set = 1;
1241 if (fh->overlay_active != ZORAN_FREE &&
1242 zr->overlay_active != ZORAN_FREE)
1243 zr->overlay_settings = fh->overlay_settings;
1244
1245 if (on)
1246 zr36057_overlay(zr, 1);
1247
1248 /* Make sure the changes come into effect */
1249 return wait_grab_pending(zr);
1250}
1251
e5ee3f64 1252static int setup_overlay(struct zoran_fh *fh, int on)
1da177e4 1253{
1da177e4
LT
1254 struct zoran *zr = fh->zr;
1255
1256 /* If there is nothing to do, return immediatly */
1257 if ((on && fh->overlay_active != ZORAN_FREE) ||
1258 (!on && fh->overlay_active == ZORAN_FREE))
1259 return 0;
1260
1261 /* check whether we're touching someone else's overlay */
1262 if (on && zr->overlay_active != ZORAN_FREE &&
1263 fh->overlay_active == ZORAN_FREE) {
1264 dprintk(1,
1265 KERN_ERR
c61402ba
TP
1266 "%s: %s - overlay is already active for another session\n",
1267 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1268 return -EBUSY;
1269 }
1270 if (!on && zr->overlay_active != ZORAN_FREE &&
1271 fh->overlay_active == ZORAN_FREE) {
1272 dprintk(1,
1273 KERN_ERR
c61402ba
TP
1274 "%s: %s - you cannot cancel someone else's session\n",
1275 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1276 return -EPERM;
1277 }
1278
1279 if (on == 0) {
1280 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1281 zr->v4l_overlay_active = 0;
1282 /* When a grab is running, the video simply
1283 * won't be switched on any more */
1284 if (!zr->v4l_memgrab_active)
1285 zr36057_overlay(zr, 0);
1286 zr->overlay_mask = NULL;
1287 } else {
7f6adeaf 1288 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1da177e4
LT
1289 dprintk(1,
1290 KERN_ERR
c61402ba
TP
1291 "%s: %s - buffer or window not set\n",
1292 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1293 return -EINVAL;
1294 }
1295 if (!fh->overlay_settings.format) {
1296 dprintk(1,
1297 KERN_ERR
c61402ba
TP
1298 "%s: %s - no overlay format set\n",
1299 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1300 return -EINVAL;
1301 }
1302 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1303 zr->v4l_overlay_active = 1;
1304 zr->overlay_mask = fh->overlay_mask;
1305 zr->overlay_settings = fh->overlay_settings;
1306 if (!zr->v4l_memgrab_active)
1307 zr36057_overlay(zr, 1);
1308 /* When a grab is running, the video will be
1309 * switched on when grab is finished */
1310 }
1311
1312 /* Make sure the changes come into effect */
1313 return wait_grab_pending(zr);
1314}
1315
e5ee3f64
TP
1316/* get the status of a buffer in the clients buffer queue */
1317static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1318 struct v4l2_buffer *buf, int num)
1da177e4 1319{
1da177e4 1320 struct zoran *zr = fh->zr;
1159b7f1 1321 unsigned long flags;
1da177e4
LT
1322
1323 buf->flags = V4L2_BUF_FLAG_MAPPED;
1324
1325 switch (fh->map_mode) {
1326 case ZORAN_MAP_MODE_RAW:
1da177e4 1327 /* check range */
1159b7f1
TP
1328 if (num < 0 || num >= fh->buffers.num_buffers ||
1329 !fh->buffers.allocated) {
1da177e4
LT
1330 dprintk(1,
1331 KERN_ERR
c61402ba
TP
1332 "%s: %s - wrong number or buffers not allocated\n",
1333 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1334 return -EINVAL;
1335 }
1336
1159b7f1
TP
1337 spin_lock_irqsave(&zr->spinlock, flags);
1338 dprintk(3,
1339 KERN_DEBUG
1340 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1341 ZR_DEVNAME(zr), __func__,
1342 "FAL"[fh->buffers.active], num,
1343 "UPMD"[zr->v4l_buffers.buffer[num].state],
1344 fh->buffers.buffer[num].map ? 'Y' : 'N');
1345 spin_unlock_irqrestore(&zr->spinlock, flags);
1346
1da177e4 1347 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1159b7f1 1348 buf->length = fh->buffers.buffer_size;
1da177e4
LT
1349
1350 /* get buffer */
1159b7f1
TP
1351 buf->bytesused = fh->buffers.buffer[num].bs.length;
1352 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1353 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1354 buf->sequence = fh->buffers.buffer[num].bs.seq;
1da177e4 1355 buf->flags |= V4L2_BUF_FLAG_DONE;
1159b7f1 1356 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1da177e4
LT
1357 } else {
1358 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1359 }
1360
1361 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1362 buf->field = V4L2_FIELD_TOP;
1363 else
1364 buf->field = V4L2_FIELD_INTERLACED;
1365
1366 break;
1367
1368 case ZORAN_MAP_MODE_JPG_REC:
1369 case ZORAN_MAP_MODE_JPG_PLAY:
1370
1371 /* check range */
1159b7f1
TP
1372 if (num < 0 || num >= fh->buffers.num_buffers ||
1373 !fh->buffers.allocated) {
1da177e4
LT
1374 dprintk(1,
1375 KERN_ERR
c61402ba
TP
1376 "%s: %s - wrong number or buffers not allocated\n",
1377 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1378 return -EINVAL;
1379 }
1380
1381 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1382 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1383 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1159b7f1 1384 buf->length = fh->buffers.buffer_size;
1da177e4
LT
1385
1386 /* these variables are only written after frame has been captured */
1159b7f1
TP
1387 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1388 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1389 buf->sequence = fh->buffers.buffer[num].bs.seq;
1390 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1391 buf->bytesused = fh->buffers.buffer[num].bs.length;
1da177e4
LT
1392 buf->flags |= V4L2_BUF_FLAG_DONE;
1393 } else {
1394 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1395 }
1396
1397 /* which fields are these? */
1398 if (fh->jpg_settings.TmpDcm != 1)
1159b7f1
TP
1399 buf->field = fh->jpg_settings.odd_even ?
1400 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1da177e4 1401 else
1159b7f1
TP
1402 buf->field = fh->jpg_settings.odd_even ?
1403 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1da177e4
LT
1404
1405 break;
1406
1407 default:
1408
1409 dprintk(5,
1410 KERN_ERR
c61402ba
TP
1411 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1412 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
1da177e4
LT
1413 return -EINVAL;
1414 }
1415
1416 buf->memory = V4L2_MEMORY_MMAP;
1417 buf->index = num;
1418 buf->m.offset = buf->length * num;
1419
1420 return 0;
1421}
1da177e4
LT
1422
1423static int
1424zoran_set_norm (struct zoran *zr,
107063c6 1425 v4l2_std_id norm)
1da177e4 1426{
107063c6 1427 int on;
1da177e4
LT
1428
1429 if (zr->v4l_buffers.active != ZORAN_FREE ||
1430 zr->jpg_buffers.active != ZORAN_FREE) {
1431 dprintk(1,
1432 KERN_WARNING
c61402ba
TP
1433 "%s: %s called while in playback/capture mode\n",
1434 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1435 return -EBUSY;
1436 }
1437
107063c6 1438 if (!(norm & zr->card.norms)) {
1da177e4 1439 dprintk(1,
c61402ba
TP
1440 KERN_ERR "%s: %s - unsupported norm %llx\n",
1441 ZR_DEVNAME(zr), __func__, norm);
1da177e4
LT
1442 return -EINVAL;
1443 }
1444
107063c6
HV
1445 if (norm == V4L2_STD_ALL) {
1446 int status = 0;
1447 v4l2_std_id std = 0;
1da177e4 1448
0ab6e1c3 1449 decoder_call(zr, video, querystd, &std);
f41737ec 1450 decoder_call(zr, core, s_std, std);
1da177e4
LT
1451
1452 /* let changes come into effect */
1453 ssleep(2);
1454
0ab6e1c3 1455 decoder_call(zr, video, g_input_status, &status);
107063c6 1456 if (status & V4L2_IN_ST_NO_SIGNAL) {
1da177e4
LT
1457 dprintk(1,
1458 KERN_ERR
c61402ba
TP
1459 "%s: %s - no norm detected\n",
1460 ZR_DEVNAME(zr), __func__);
1da177e4 1461 /* reset norm */
f41737ec 1462 decoder_call(zr, core, s_std, zr->norm);
1da177e4
LT
1463 return -EIO;
1464 }
1465
107063c6 1466 norm = std;
1da177e4 1467 }
107063c6
HV
1468 if (norm & V4L2_STD_SECAM)
1469 zr->timing = zr->card.tvn[2];
1470 else if (norm & V4L2_STD_NTSC)
1471 zr->timing = zr->card.tvn[1];
1472 else
1473 zr->timing = zr->card.tvn[0];
1da177e4
LT
1474
1475 /* We switch overlay off and on since a change in the
1476 * norm needs different VFE settings */
1477 on = zr->overlay_active && !zr->v4l_memgrab_active;
1478 if (on)
1479 zr36057_overlay(zr, 0);
1480
f41737ec 1481 decoder_call(zr, core, s_std, norm);
0ab6e1c3 1482 encoder_call(zr, video, s_std_output, norm);
1da177e4
LT
1483
1484 if (on)
1485 zr36057_overlay(zr, 1);
1486
1487 /* Make sure the changes come into effect */
1488 zr->norm = norm;
1489
1490 return 0;
1491}
1492
1493static int
1494zoran_set_input (struct zoran *zr,
1495 int input)
1496{
1da177e4
LT
1497 if (input == zr->input) {
1498 return 0;
1499 }
1500
1501 if (zr->v4l_buffers.active != ZORAN_FREE ||
1502 zr->jpg_buffers.active != ZORAN_FREE) {
1503 dprintk(1,
1504 KERN_WARNING
c61402ba
TP
1505 "%s: %s called while in playback/capture mode\n",
1506 ZR_DEVNAME(zr), __func__);
1da177e4
LT
1507 return -EBUSY;
1508 }
1509
1510 if (input < 0 || input >= zr->card.inputs) {
1511 dprintk(1,
1512 KERN_ERR
c61402ba
TP
1513 "%s: %s - unnsupported input %d\n",
1514 ZR_DEVNAME(zr), __func__, input);
1da177e4
LT
1515 return -EINVAL;
1516 }
1517
1da177e4
LT
1518 zr->input = input;
1519
5325b427
HV
1520 decoder_call(zr, video, s_routing,
1521 zr->card.input[input].muxsel, 0, 0);
1da177e4
LT
1522
1523 return 0;
1524}
1525
1526/*
1527 * ioctl routine
1528 */
1529
dcbd83b1 1530#ifdef CONFIG_VIDEO_V4L1_COMPAT
96b8e145 1531static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
1da177e4 1532{
96b8e145 1533 struct zoran_fh *fh = __fh;
1da177e4 1534 struct zoran *zr = fh->zr;
1da177e4
LT
1535 struct zoran_jpg_settings settings;
1536
1da177e4 1537 switch (cmd) {
1da177e4
LT
1538 case BUZIOC_G_PARAMS:
1539 {
1540 struct zoran_params *bparams = arg;
1541
1542 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1543
1544 memset(bparams, 0, sizeof(struct zoran_params));
1545 bparams->major_version = MAJOR_VERSION;
1546 bparams->minor_version = MINOR_VERSION;
1547
384c3689 1548 mutex_lock(&zr->resource_lock);
1da177e4 1549
107063c6
HV
1550 if (zr->norm & V4L2_STD_NTSC)
1551 bparams->norm = VIDEO_MODE_NTSC;
1552 else if (zr->norm & V4L2_STD_PAL)
1553 bparams->norm = VIDEO_MODE_PAL;
1554 else
1555 bparams->norm = VIDEO_MODE_SECAM;
1556
1da177e4
LT
1557 bparams->input = zr->input;
1558
1559 bparams->decimation = fh->jpg_settings.decimation;
1560 bparams->HorDcm = fh->jpg_settings.HorDcm;
1561 bparams->VerDcm = fh->jpg_settings.VerDcm;
1562 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1563 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1564 bparams->img_x = fh->jpg_settings.img_x;
1565 bparams->img_y = fh->jpg_settings.img_y;
1566 bparams->img_width = fh->jpg_settings.img_width;
1567 bparams->img_height = fh->jpg_settings.img_height;
1568 bparams->odd_even = fh->jpg_settings.odd_even;
1569
1570 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1571 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1572 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1573 memcpy(bparams->APP_data,
1574 fh->jpg_settings.jpg_comp.APP_data,
1575 sizeof(bparams->APP_data));
1576 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1577 memcpy(bparams->COM_data,
1578 fh->jpg_settings.jpg_comp.COM_data,
1579 sizeof(bparams->COM_data));
1580 bparams->jpeg_markers =
1581 fh->jpg_settings.jpg_comp.jpeg_markers;
1582
384c3689 1583 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1584
1585 bparams->VFIFO_FB = 0;
1586
1587 return 0;
1588 }
1da177e4
LT
1589
1590 case BUZIOC_S_PARAMS:
1591 {
1592 struct zoran_params *bparams = arg;
1593 int res = 0;
1594
1595 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1596
1597 settings.decimation = bparams->decimation;
1598 settings.HorDcm = bparams->HorDcm;
1599 settings.VerDcm = bparams->VerDcm;
1600 settings.TmpDcm = bparams->TmpDcm;
1601 settings.field_per_buff = bparams->field_per_buff;
1602 settings.img_x = bparams->img_x;
1603 settings.img_y = bparams->img_y;
1604 settings.img_width = bparams->img_width;
1605 settings.img_height = bparams->img_height;
1606 settings.odd_even = bparams->odd_even;
1607
1608 settings.jpg_comp.quality = bparams->quality;
1609 settings.jpg_comp.APPn = bparams->APPn;
1610 settings.jpg_comp.APP_len = bparams->APP_len;
1611 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1612 sizeof(bparams->APP_data));
1613 settings.jpg_comp.COM_len = bparams->COM_len;
1614 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1615 sizeof(bparams->COM_data));
1616 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1617
384c3689 1618 mutex_lock(&zr->resource_lock);
1da177e4
LT
1619
1620 if (zr->codec_mode != BUZ_MODE_IDLE) {
1621 dprintk(1,
1622 KERN_ERR
1623 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1624 ZR_DEVNAME(zr));
1625 res = -EINVAL;
1626 goto sparams_unlock_and_return;
1627 }
1628
1629 /* Check the params first before overwriting our
1630 * nternal values */
0ba514d5 1631 if (zoran_check_jpg_settings(zr, &settings, 0)) {
1da177e4
LT
1632 res = -EINVAL;
1633 goto sparams_unlock_and_return;
1634 }
1635
1636 fh->jpg_settings = settings;
96b8e145 1637sparams_unlock_and_return:
384c3689 1638 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1639
1640 return res;
1641 }
1da177e4
LT
1642
1643 case BUZIOC_REQBUFS:
1644 {
1645 struct zoran_requestbuffers *breq = arg;
1646 int res = 0;
1647
1648 dprintk(3,
1649 KERN_DEBUG
1650 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1651 ZR_DEVNAME(zr), breq->count, breq->size);
1652
1653 /* Enforce reasonable lower and upper limits */
1654 if (breq->count < 4)
1655 breq->count = 4; /* Could be choosen smaller */
1656 if (breq->count > jpg_nbufs)
1657 breq->count = jpg_nbufs;
1658 breq->size = PAGE_ALIGN(breq->size);
1659 if (breq->size < 8192)
1660 breq->size = 8192; /* Arbitrary */
1661 /* breq->size is limited by 1 page for the stat_com
1662 * tables to a Maximum of 2 MB */
1663 if (breq->size > jpg_bufsize)
1664 breq->size = jpg_bufsize;
1da177e4 1665
384c3689 1666 mutex_lock(&zr->resource_lock);
1da177e4 1667
1159b7f1 1668 if (fh->buffers.allocated) {
1da177e4
LT
1669 dprintk(1,
1670 KERN_ERR
b80696b7 1671 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
1da177e4
LT
1672 ZR_DEVNAME(zr));
1673 res = -EBUSY;
1674 goto jpgreqbuf_unlock_and_return;
1675 }
1676
1159b7f1
TP
1677 /* The next mmap will map the MJPEG buffers - could
1678 * also be *_PLAY, but it doesn't matter here */
1679 map_mode_jpg(fh, 0);
1680 fh->buffers.num_buffers = breq->count;
1681 fh->buffers.buffer_size = breq->size;
1da177e4 1682
e5ee3f64 1683 if (jpg_fbuffer_alloc(fh)) {
1da177e4
LT
1684 res = -ENOMEM;
1685 goto jpgreqbuf_unlock_and_return;
1686 }
1687
96b8e145 1688jpgreqbuf_unlock_and_return:
384c3689 1689 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1690
1691 return res;
1692 }
1da177e4
LT
1693
1694 case BUZIOC_QBUF_CAPT:
1695 {
1696 int *frame = arg, res;
1697
1698 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1699 ZR_DEVNAME(zr), *frame);
1700
384c3689 1701 mutex_lock(&zr->resource_lock);
e5ee3f64 1702 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
384c3689 1703 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1704
1705 return res;
1706 }
1da177e4
LT
1707
1708 case BUZIOC_QBUF_PLAY:
1709 {
1710 int *frame = arg, res;
1711
1712 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1713 ZR_DEVNAME(zr), *frame);
1714
384c3689 1715 mutex_lock(&zr->resource_lock);
e5ee3f64 1716 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
384c3689 1717 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1718
1719 return res;
1720 }
1da177e4
LT
1721
1722 case BUZIOC_SYNC:
1723 {
1724 struct zoran_sync *bsync = arg;
1725 int res;
1726
1727 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1728
384c3689 1729 mutex_lock(&zr->resource_lock);
1159b7f1
TP
1730
1731 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1732 dprintk(2, KERN_WARNING
1733 "%s: %s - not in jpg capture mode\n",
1734 ZR_DEVNAME(zr), __func__);
1735 res = -EINVAL;
1736 } else {
e5ee3f64 1737 res = jpg_sync(fh, bsync);
1159b7f1 1738 }
384c3689 1739 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1740
1741 return res;
1742 }
1da177e4
LT
1743
1744 case BUZIOC_G_STATUS:
1745 {
1746 struct zoran_status *bstat = arg;
107063c6
HV
1747 int status = 0, res = 0;
1748 v4l2_std_id norm;
1da177e4
LT
1749
1750 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1751
1752 if (zr->codec_mode != BUZ_MODE_IDLE) {
1753 dprintk(1,
1754 KERN_ERR
1755 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1756 ZR_DEVNAME(zr));
1757 return -EINVAL;
1758 }
1759
384c3689 1760 mutex_lock(&zr->resource_lock);
1da177e4
LT
1761
1762 if (zr->codec_mode != BUZ_MODE_IDLE) {
1763 dprintk(1,
1764 KERN_ERR
1765 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1766 ZR_DEVNAME(zr));
1767 res = -EINVAL;
1768 goto gstat_unlock_and_return;
1769 }
1770
5325b427
HV
1771 decoder_call(zr, video, s_routing,
1772 zr->card.input[bstat->input].muxsel, 0, 0);
1da177e4
LT
1773
1774 /* sleep 1 second */
1775 ssleep(1);
1776
1777 /* Get status of video decoder */
0ab6e1c3
HV
1778 decoder_call(zr, video, querystd, &norm);
1779 decoder_call(zr, video, g_input_status, &status);
1da177e4
LT
1780
1781 /* restore previous input and norm */
5325b427
HV
1782 decoder_call(zr, video, s_routing,
1783 zr->card.input[zr->input].muxsel, 0, 0);
96b8e145 1784gstat_unlock_and_return:
384c3689 1785 mutex_unlock(&zr->resource_lock);
1da177e4
LT
1786
1787 if (!res) {
1788 bstat->signal =
107063c6
HV
1789 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1790 if (norm & V4L2_STD_NTSC)
1da177e4 1791 bstat->norm = VIDEO_MODE_NTSC;
107063c6 1792 else if (norm & V4L2_STD_SECAM)
1da177e4
LT
1793 bstat->norm = VIDEO_MODE_SECAM;
1794 else
1795 bstat->norm = VIDEO_MODE_PAL;
1796
1797 bstat->color =
107063c6 1798 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
1da177e4
LT
1799 }
1800
1801 return res;
1802 }
1da177e4 1803
96b8e145
HV
1804 default:
1805 return -EINVAL;
1da177e4 1806 }
96b8e145 1807}
1da177e4 1808
dcbd83b1
HV
1809static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1810{
1811 struct zoran_fh *fh = __fh;
1812 struct zoran *zr = fh->zr;
1813 int i, res = 0;
1814
dcbd83b1
HV
1815
1816 mutex_lock(&zr->resource_lock);
1817
1159b7f1 1818 if (fh->buffers.allocated) {
dcbd83b1
HV
1819 dprintk(1,
1820 KERN_ERR
1821 "%s: VIDIOCGMBUF - buffers already allocated\n",
1822 ZR_DEVNAME(zr));
1823 res = -EINVAL;
1824 goto v4l1reqbuf_unlock_and_return;
1825 }
1826
1159b7f1
TP
1827 /* The next mmap will map the V4L buffers */
1828 map_mode_raw(fh);
1829
e5ee3f64 1830 if (v4l_fbuffer_alloc(fh)) {
dcbd83b1
HV
1831 res = -ENOMEM;
1832 goto v4l1reqbuf_unlock_and_return;
1833 }
1834
1159b7f1
TP
1835 vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1836 vmbuf->frames = fh->buffers.num_buffers;
1837 for (i = 0; i < vmbuf->frames; i++)
1838 vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1839
dcbd83b1
HV
1840v4l1reqbuf_unlock_and_return:
1841 mutex_unlock(&zr->resource_lock);
1842
1843 return res;
1844}
1845#endif
1846
96b8e145
HV
1847static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1848{
1849 struct zoran_fh *fh = __fh;
1850 struct zoran *zr = fh->zr;
1da177e4 1851
96b8e145
HV
1852 memset(cap, 0, sizeof(*cap));
1853 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1854 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1855 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1856 pci_name(zr->pci_dev));
602dd48a 1857 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
96b8e145 1858 RELEASE_VERSION);
602dd48a
HV
1859 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1860 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
96b8e145
HV
1861 return 0;
1862}
1da177e4 1863
96b8e145
HV
1864static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1865{
171f48e2
TP
1866 unsigned int num, i;
1867
1868 for (num = i = 0; i < NUM_FORMATS; i++) {
1869 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1870 strncpy(fmt->description, zoran_formats[i].name,
1871 sizeof(fmt->description) - 1);
1872 /* fmt struct pre-zeroed, so adding '\0' not neeed */
1873 fmt->pixelformat = zoran_formats[i].fourcc;
1874 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1875 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1876 return 0;
1877 }
1da177e4 1878 }
171f48e2 1879 return -EINVAL;
96b8e145 1880}
1da177e4 1881
96b8e145
HV
1882static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1883 struct v4l2_fmtdesc *f)
1884{
1885 struct zoran_fh *fh = __fh;
1886 struct zoran *zr = fh->zr;
1da177e4 1887
96b8e145
HV
1888 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1889}
1da177e4 1890
96b8e145
HV
1891static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1892 struct v4l2_fmtdesc *f)
1893{
1894 struct zoran_fh *fh = __fh;
1895 struct zoran *zr = fh->zr;
1da177e4 1896
96b8e145
HV
1897 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1898}
1da177e4 1899
96b8e145
HV
1900static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1901 struct v4l2_fmtdesc *f)
1902{
1903 struct zoran_fh *fh = __fh;
1904 struct zoran *zr = fh->zr;
1da177e4 1905
96b8e145
HV
1906 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1907}
1da177e4 1908
96b8e145
HV
1909static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1910 struct v4l2_format *fmt)
1911{
1912 struct zoran_fh *fh = __fh;
1913 struct zoran *zr = fh->zr;
1da177e4 1914
96b8e145 1915 mutex_lock(&zr->resource_lock);
1da177e4 1916
96b8e145 1917 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
5ca75b00 1918 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
96b8e145
HV
1919 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1920 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1921 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1922 if (fh->jpg_settings.TmpDcm == 1)
1923 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
3e66793b 1924 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
96b8e145
HV
1925 else
1926 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1927 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1928 fmt->fmt.pix.bytesperline = 0;
1929 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1da177e4 1930
96b8e145
HV
1931 mutex_unlock(&zr->resource_lock);
1932 return 0;
1933}
1da177e4 1934
96b8e145
HV
1935static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1936 struct v4l2_format *fmt)
1937{
1938 struct zoran_fh *fh = __fh;
1939 struct zoran *zr = fh->zr;
1da177e4 1940
96b8e145
HV
1941 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1942 return zoran_g_fmt_vid_out(file, fh, fmt);
1da177e4 1943
96b8e145
HV
1944 mutex_lock(&zr->resource_lock);
1945 fmt->fmt.pix.width = fh->v4l_settings.width;
1946 fmt->fmt.pix.height = fh->v4l_settings.height;
1947 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1948 fh->v4l_settings.height;
1949 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1950 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1951 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1952 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1953 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1954 else
1955 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1956 mutex_unlock(&zr->resource_lock);
1957 return 0;
1958}
1da177e4 1959
96b8e145
HV
1960static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1961 struct v4l2_format *fmt)
1962{
1963 struct zoran_fh *fh = __fh;
1964 struct zoran *zr = fh->zr;
1da177e4 1965
96b8e145 1966 mutex_lock(&zr->resource_lock);
1da177e4 1967
96b8e145
HV
1968 fmt->fmt.win.w.left = fh->overlay_settings.x;
1969 fmt->fmt.win.w.top = fh->overlay_settings.y;
1970 fmt->fmt.win.w.width = fh->overlay_settings.width;
1971 fmt->fmt.win.w.height = fh->overlay_settings.height;
1972 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1973 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1974 else
1975 fmt->fmt.win.field = V4L2_FIELD_TOP;
1da177e4 1976
96b8e145
HV
1977 mutex_unlock(&zr->resource_lock);
1978 return 0;
1979}
1da177e4 1980
96b8e145
HV
1981static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1982 struct v4l2_format *fmt)
1983{
1984 struct zoran_fh *fh = __fh;
1985 struct zoran *zr = fh->zr;
1da177e4 1986
96b8e145 1987 mutex_lock(&zr->resource_lock);
1da177e4 1988
96b8e145
HV
1989 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1990 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1991 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1992 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1993 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1994 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1995 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1996 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1da177e4 1997
96b8e145
HV
1998 mutex_unlock(&zr->resource_lock);
1999 return 0;
2000}
1da177e4 2001
96b8e145
HV
2002static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2003 struct v4l2_format *fmt)
2004{
2005 struct zoran_fh *fh = __fh;
2006 struct zoran *zr = fh->zr;
2007 struct zoran_jpg_settings settings;
2008 int res = 0;
1da177e4 2009
96b8e145
HV
2010 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2011 return -EINVAL;
1da177e4 2012
96b8e145
HV
2013 mutex_lock(&zr->resource_lock);
2014 settings = fh->jpg_settings;
1da177e4 2015
96b8e145
HV
2016 /* we actually need to set 'real' parameters now */
2017 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2018 settings.TmpDcm = 1;
2019 else
2020 settings.TmpDcm = 2;
2021 settings.decimation = 0;
2022 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2023 settings.VerDcm = 2;
2024 else
2025 settings.VerDcm = 1;
2026 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2027 settings.HorDcm = 4;
2028 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2029 settings.HorDcm = 2;
2030 else
2031 settings.HorDcm = 1;
2032 if (settings.TmpDcm == 1)
2033 settings.field_per_buff = 2;
2034 else
2035 settings.field_per_buff = 1;
2036
886fe23d
HV
2037 if (settings.HorDcm > 1) {
2038 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2039 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2040 } else {
2041 settings.img_x = 0;
2042 settings.img_width = BUZ_MAX_WIDTH;
2043 }
2044
96b8e145 2045 /* check */
0ba514d5 2046 res = zoran_check_jpg_settings(zr, &settings, 1);
96b8e145
HV
2047 if (res)
2048 goto tryfmt_unlock_and_return;
2049
2050 /* tell the user what we actually did */
2051 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2052 fmt->fmt.pix.height = settings.img_height * 2 /
2053 (settings.TmpDcm * settings.VerDcm);
2054 if (settings.TmpDcm == 1)
2055 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2056 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2057 else
2058 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2059 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1da177e4 2060
96b8e145 2061 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
886fe23d
HV
2062 fmt->fmt.pix.bytesperline = 0;
2063 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
96b8e145
HV
2064tryfmt_unlock_and_return:
2065 mutex_unlock(&zr->resource_lock);
2066 return res;
2067}
1da177e4 2068
96b8e145
HV
2069static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2070 struct v4l2_format *fmt)
2071{
2072 struct zoran_fh *fh = __fh;
2073 struct zoran *zr = fh->zr;
0ba514d5 2074 int bpp;
96b8e145 2075 int i;
1da177e4 2076
96b8e145
HV
2077 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2078 return zoran_try_fmt_vid_out(file, fh, fmt);
1da177e4 2079
96b8e145 2080 mutex_lock(&zr->resource_lock);
1da177e4 2081
96b8e145
HV
2082 for (i = 0; i < NUM_FORMATS; i++)
2083 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2084 break;
1da177e4 2085
96b8e145 2086 if (i == NUM_FORMATS) {
384c3689 2087 mutex_unlock(&zr->resource_lock);
96b8e145 2088 return -EINVAL;
1da177e4 2089 }
1da177e4 2090
0ba514d5
HV
2091 bpp = (zoran_formats[i].depth + 7) / 8;
2092 fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
96b8e145
HV
2093 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2094 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2095 if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
2096 fmt->fmt.pix.width = BUZ_MIN_WIDTH;
2097 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2098 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2099 if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
2100 fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
2101 mutex_unlock(&zr->resource_lock);
1da177e4 2102
96b8e145
HV
2103 return 0;
2104}
1da177e4 2105
96b8e145
HV
2106static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2107 struct v4l2_format *fmt)
2108{
2109 struct zoran_fh *fh = __fh;
2110 struct zoran *zr = fh->zr;
2111 int res;
2112
2113 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2114 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2115 fmt->fmt.win.w.width,
2116 fmt->fmt.win.w.height,
2117 fmt->fmt.win.clipcount,
2118 fmt->fmt.win.bitmap);
2119 mutex_lock(&zr->resource_lock);
e5ee3f64
TP
2120 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2121 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
2122 (struct v4l2_clip __user *)fmt->fmt.win.clips,
2123 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
96b8e145
HV
2124 mutex_unlock(&zr->resource_lock);
2125 return res;
2126}
1da177e4 2127
96b8e145
HV
2128static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2129 struct v4l2_format *fmt)
2130{
2131 struct zoran_fh *fh = __fh;
2132 struct zoran *zr = fh->zr;
2133 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2134 struct zoran_jpg_settings settings;
2135 int res = 0;
1da177e4 2136
96b8e145
HV
2137 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2138 fmt->fmt.pix.width, fmt->fmt.pix.height,
2139 fmt->fmt.pix.pixelformat,
2140 (char *) &printformat);
2141 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2142 return -EINVAL;
1da177e4 2143
96b8e145 2144 mutex_lock(&zr->resource_lock);
1da177e4 2145
1159b7f1 2146 if (fh->buffers.allocated) {
96b8e145 2147 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
1159b7f1 2148 ZR_DEVNAME(zr));
96b8e145
HV
2149 res = -EBUSY;
2150 goto sfmtjpg_unlock_and_return;
2151 }
1da177e4 2152
1159b7f1
TP
2153 settings = fh->jpg_settings;
2154
96b8e145 2155 /* we actually need to set 'real' parameters now */
7f37cc9b 2156 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
96b8e145
HV
2157 settings.TmpDcm = 1;
2158 else
2159 settings.TmpDcm = 2;
2160 settings.decimation = 0;
2161 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2162 settings.VerDcm = 2;
2163 else
2164 settings.VerDcm = 1;
2165 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2166 settings.HorDcm = 4;
2167 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2168 settings.HorDcm = 2;
2169 else
2170 settings.HorDcm = 1;
2171 if (settings.TmpDcm == 1)
2172 settings.field_per_buff = 2;
2173 else
2174 settings.field_per_buff = 1;
2175
0ba514d5
HV
2176 if (settings.HorDcm > 1) {
2177 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2178 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2179 } else {
2180 settings.img_x = 0;
2181 settings.img_width = BUZ_MAX_WIDTH;
2182 }
2183
96b8e145 2184 /* check */
0ba514d5 2185 res = zoran_check_jpg_settings(zr, &settings, 0);
96b8e145
HV
2186 if (res)
2187 goto sfmtjpg_unlock_and_return;
2188
2189 /* it's ok, so set them */
2190 fh->jpg_settings = settings;
2191
1159b7f1
TP
2192 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2193 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2194
96b8e145
HV
2195 /* tell the user what we actually did */
2196 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2197 fmt->fmt.pix.height = settings.img_height * 2 /
2198 (settings.TmpDcm * settings.VerDcm);
2199 if (settings.TmpDcm == 1)
2200 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2201 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2202 else
2203 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2204 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
96b8e145 2205 fmt->fmt.pix.bytesperline = 0;
1159b7f1 2206 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
96b8e145
HV
2207 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2208
96b8e145
HV
2209sfmtjpg_unlock_and_return:
2210 mutex_unlock(&zr->resource_lock);
2211 return res;
2212}
1da177e4 2213
96b8e145
HV
2214static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2215 struct v4l2_format *fmt)
2216{
2217 struct zoran_fh *fh = __fh;
2218 struct zoran *zr = fh->zr;
2219 int i;
2220 int res = 0;
1da177e4 2221
96b8e145
HV
2222 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2223 return zoran_s_fmt_vid_out(file, fh, fmt);
1da177e4 2224
96b8e145
HV
2225 for (i = 0; i < NUM_FORMATS; i++)
2226 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2227 break;
2228 if (i == NUM_FORMATS) {
2229 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2230 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2231 return -EINVAL;
1da177e4 2232 }
1159b7f1 2233
96b8e145 2234 mutex_lock(&zr->resource_lock);
1159b7f1
TP
2235
2236 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2237 fh->buffers.active != ZORAN_FREE) {
96b8e145
HV
2238 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2239 ZR_DEVNAME(zr));
2240 res = -EBUSY;
2241 goto sfmtv4l_unlock_and_return;
2242 }
2243 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2244 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2245 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2246 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2247
1159b7f1
TP
2248 map_mode_raw(fh);
2249
2250 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2251 &zoran_formats[i]);
96b8e145
HV
2252 if (res)
2253 goto sfmtv4l_unlock_and_return;
2254
1159b7f1 2255 /* tell the user the results/missing stuff */
96b8e145
HV
2256 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2257 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2258 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2259 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2260 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2261 else
2262 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1da177e4 2263
96b8e145
HV
2264sfmtv4l_unlock_and_return:
2265 mutex_unlock(&zr->resource_lock);
2266 return res;
2267}
1da177e4 2268
96b8e145
HV
2269static int zoran_g_fbuf(struct file *file, void *__fh,
2270 struct v4l2_framebuffer *fb)
2271{
2272 struct zoran_fh *fh = __fh;
2273 struct zoran *zr = fh->zr;
1da177e4 2274
96b8e145
HV
2275 memset(fb, 0, sizeof(*fb));
2276 mutex_lock(&zr->resource_lock);
7f6adeaf
HV
2277 fb->base = zr->vbuf_base;
2278 fb->fmt.width = zr->vbuf_width;
2279 fb->fmt.height = zr->vbuf_height;
96b8e145
HV
2280 if (zr->overlay_settings.format)
2281 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
7f6adeaf 2282 fb->fmt.bytesperline = zr->vbuf_bytesperline;
96b8e145
HV
2283 mutex_unlock(&zr->resource_lock);
2284 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2285 fb->fmt.field = V4L2_FIELD_INTERLACED;
2286 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2287 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1da177e4 2288
96b8e145
HV
2289 return 0;
2290}
1da177e4 2291
96b8e145
HV
2292static int zoran_s_fbuf(struct file *file, void *__fh,
2293 struct v4l2_framebuffer *fb)
2294{
2295 struct zoran_fh *fh = __fh;
2296 struct zoran *zr = fh->zr;
2297 int i, res = 0;
2298 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
1da177e4 2299
96b8e145
HV
2300 for (i = 0; i < NUM_FORMATS; i++)
2301 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
1da177e4 2302 break;
96b8e145
HV
2303 if (i == NUM_FORMATS) {
2304 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2305 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2306 (char *)&printformat);
2307 return -EINVAL;
1da177e4 2308 }
1da177e4 2309
96b8e145 2310 mutex_lock(&zr->resource_lock);
e5ee3f64
TP
2311 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2312 fb->fmt.height, fb->fmt.bytesperline);
96b8e145 2313 mutex_unlock(&zr->resource_lock);
1da177e4 2314
96b8e145
HV
2315 return res;
2316}
1da177e4 2317
96b8e145
HV
2318static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2319{
2320 struct zoran_fh *fh = __fh;
2321 struct zoran *zr = fh->zr;
2322 int res;
1da177e4 2323
96b8e145 2324 mutex_lock(&zr->resource_lock);
e5ee3f64 2325 res = setup_overlay(fh, on);
96b8e145 2326 mutex_unlock(&zr->resource_lock);
1da177e4 2327
96b8e145
HV
2328 return res;
2329}
1da177e4 2330
84c1b094
HV
2331static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2332
96b8e145
HV
2333static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2334{
2335 struct zoran_fh *fh = __fh;
2336 struct zoran *zr = fh->zr;
2337 int res = 0;
1da177e4 2338
96b8e145 2339 if (req->memory != V4L2_MEMORY_MMAP) {
84c1b094 2340 dprintk(2,
96b8e145
HV
2341 KERN_ERR
2342 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2343 ZR_DEVNAME(zr), req->memory);
2344 return -EINVAL;
2345 }
1da177e4 2346
84c1b094
HV
2347 if (req->count == 0)
2348 return zoran_streamoff(file, fh, req->type);
1da177e4 2349
84c1b094 2350 mutex_lock(&zr->resource_lock);
1159b7f1 2351 if (fh->buffers.allocated) {
84c1b094 2352 dprintk(2,
1da177e4 2353 KERN_ERR
7f37cc9b 2354 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
96b8e145
HV
2355 ZR_DEVNAME(zr));
2356 res = -EBUSY;
2357 goto v4l2reqbuf_unlock_and_return;
1da177e4 2358 }
1da177e4 2359
96b8e145 2360 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
1159b7f1 2361 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
96b8e145
HV
2362 /* control user input */
2363 if (req->count < 2)
2364 req->count = 2;
2365 if (req->count > v4l_nbufs)
2366 req->count = v4l_nbufs;
1159b7f1
TP
2367
2368 /* The next mmap will map the V4L buffers */
2369 map_mode_raw(fh);
2370 fh->buffers.num_buffers = req->count;
1da177e4 2371
e5ee3f64 2372 if (v4l_fbuffer_alloc(fh)) {
96b8e145
HV
2373 res = -ENOMEM;
2374 goto v4l2reqbuf_unlock_and_return;
2375 }
96b8e145 2376 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
1159b7f1 2377 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
96b8e145
HV
2378 /* we need to calculate size ourselves now */
2379 if (req->count < 4)
2380 req->count = 4;
2381 if (req->count > jpg_nbufs)
2382 req->count = jpg_nbufs;
1159b7f1
TP
2383
2384 /* The next mmap will map the MJPEG buffers */
2385 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2386 fh->buffers.num_buffers = req->count;
2387 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1da177e4 2388
e5ee3f64 2389 if (jpg_fbuffer_alloc(fh)) {
96b8e145
HV
2390 res = -ENOMEM;
2391 goto v4l2reqbuf_unlock_and_return;
2392 }
96b8e145
HV
2393 } else {
2394 dprintk(1,
1da177e4 2395 KERN_ERR
96b8e145
HV
2396 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2397 ZR_DEVNAME(zr), req->type);
2398 res = -EINVAL;
2399 goto v4l2reqbuf_unlock_and_return;
1da177e4 2400 }
96b8e145
HV
2401v4l2reqbuf_unlock_and_return:
2402 mutex_unlock(&zr->resource_lock);
1da177e4 2403
96b8e145
HV
2404 return res;
2405}
1da177e4 2406
96b8e145
HV
2407static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2408{
2409 struct zoran_fh *fh = __fh;
2410 struct zoran *zr = fh->zr;
9cfb6a3f 2411 int res;
1da177e4 2412
96b8e145 2413 mutex_lock(&zr->resource_lock);
e276f7b5 2414 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
96b8e145 2415 mutex_unlock(&zr->resource_lock);
1da177e4 2416
96b8e145
HV
2417 return res;
2418}
1da177e4 2419
96b8e145
HV
2420static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2421{
2422 struct zoran_fh *fh = __fh;
2423 struct zoran *zr = fh->zr;
2424 int res = 0, codec_mode, buf_type;
1da177e4 2425
96b8e145 2426 mutex_lock(&zr->resource_lock);
1da177e4 2427
96b8e145
HV
2428 switch (fh->map_mode) {
2429 case ZORAN_MAP_MODE_RAW:
2430 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2431 dprintk(1, KERN_ERR
2432 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2433 ZR_DEVNAME(zr), buf->type, fh->map_mode);
1da177e4 2434 res = -EINVAL;
96b8e145 2435 goto qbuf_unlock_and_return;
1da177e4 2436 }
1da177e4 2437
e5ee3f64 2438 res = zoran_v4l_queue_frame(fh, buf->index);
96b8e145
HV
2439 if (res)
2440 goto qbuf_unlock_and_return;
1159b7f1 2441 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
96b8e145 2442 zr36057_set_memgrab(zr, 1);
1da177e4
LT
2443 break;
2444
96b8e145
HV
2445 case ZORAN_MAP_MODE_JPG_REC:
2446 case ZORAN_MAP_MODE_JPG_PLAY:
2447 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2448 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2449 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2450 } else {
2451 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2452 codec_mode = BUZ_MODE_MOTION_COMPRESS;
1da177e4
LT
2453 }
2454
96b8e145
HV
2455 if (buf->type != buf_type) {
2456 dprintk(1, KERN_ERR
2457 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2458 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2459 res = -EINVAL;
2460 goto qbuf_unlock_and_return;
1da177e4
LT
2461 }
2462
e5ee3f64 2463 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
96b8e145
HV
2464 if (res != 0)
2465 goto qbuf_unlock_and_return;
2466 if (zr->codec_mode == BUZ_MODE_IDLE &&
1159b7f1 2467 fh->buffers.active == ZORAN_LOCKED)
96b8e145 2468 zr36057_enable_jpg(zr, codec_mode);
1159b7f1 2469
96b8e145 2470 break;
1da177e4 2471
96b8e145
HV
2472 default:
2473 dprintk(1, KERN_ERR
2474 "%s: VIDIOC_QBUF - unsupported type %d\n",
2475 ZR_DEVNAME(zr), buf->type);
2476 res = -EINVAL;
1da177e4 2477 break;
96b8e145
HV
2478 }
2479qbuf_unlock_and_return:
2480 mutex_unlock(&zr->resource_lock);
1da177e4 2481
96b8e145
HV
2482 return res;
2483}
1da177e4 2484
96b8e145
HV
2485static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2486{
2487 struct zoran_fh *fh = __fh;
2488 struct zoran *zr = fh->zr;
2489 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
1da177e4 2490
96b8e145 2491 mutex_lock(&zr->resource_lock);
1da177e4 2492
96b8e145
HV
2493 switch (fh->map_mode) {
2494 case ZORAN_MAP_MODE_RAW:
2495 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2496 dprintk(1, KERN_ERR
2497 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2498 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2499 res = -EINVAL;
2500 goto dqbuf_unlock_and_return;
1da177e4
LT
2501 }
2502
96b8e145
HV
2503 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2504 if (file->f_flags & O_NONBLOCK &&
2505 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2506 res = -EAGAIN;
2507 goto dqbuf_unlock_and_return;
1da177e4 2508 }
e5ee3f64 2509 res = v4l_sync(fh, num);
96b8e145
HV
2510 if (res)
2511 goto dqbuf_unlock_and_return;
2512 zr->v4l_sync_tail++;
e276f7b5 2513 res = zoran_v4l2_buffer_status(fh, buf, num);
1da177e4
LT
2514 break;
2515
96b8e145
HV
2516 case ZORAN_MAP_MODE_JPG_REC:
2517 case ZORAN_MAP_MODE_JPG_PLAY:
1da177e4 2518 {
96b8e145 2519 struct zoran_sync bs;
1da177e4 2520
96b8e145
HV
2521 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2522 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2523 else
2524 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1da177e4 2525
96b8e145
HV
2526 if (buf->type != buf_type) {
2527 dprintk(1, KERN_ERR
2528 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2529 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2530 res = -EINVAL;
2531 goto dqbuf_unlock_and_return;
1da177e4
LT
2532 }
2533
96b8e145 2534 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
1da177e4 2535
96b8e145
HV
2536 if (file->f_flags & O_NONBLOCK &&
2537 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2538 res = -EAGAIN;
2539 goto dqbuf_unlock_and_return;
2540 }
e5ee3f64 2541 res = jpg_sync(fh, &bs);
96b8e145
HV
2542 if (res)
2543 goto dqbuf_unlock_and_return;
e276f7b5 2544 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
96b8e145 2545 break;
1da177e4 2546 }
96b8e145
HV
2547
2548 default:
2549 dprintk(1, KERN_ERR
2550 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2551 ZR_DEVNAME(zr), buf->type);
2552 res = -EINVAL;
1da177e4 2553 break;
96b8e145
HV
2554 }
2555dqbuf_unlock_and_return:
2556 mutex_unlock(&zr->resource_lock);
1da177e4 2557
96b8e145
HV
2558 return res;
2559}
1da177e4 2560
96b8e145
HV
2561static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2562{
2563 struct zoran_fh *fh = __fh;
2564 struct zoran *zr = fh->zr;
2565 int res = 0;
1da177e4 2566
96b8e145 2567 mutex_lock(&zr->resource_lock);
1da177e4 2568
96b8e145
HV
2569 switch (fh->map_mode) {
2570 case ZORAN_MAP_MODE_RAW: /* raw capture */
2571 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
1159b7f1 2572 fh->buffers.active != ZORAN_ACTIVE) {
96b8e145
HV
2573 res = -EBUSY;
2574 goto strmon_unlock_and_return;
1da177e4
LT
2575 }
2576
1159b7f1 2577 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
96b8e145
HV
2578 zr->v4l_settings = fh->v4l_settings;
2579
2580 zr->v4l_sync_tail = zr->v4l_pend_tail;
2581 if (!zr->v4l_memgrab_active &&
2582 zr->v4l_pend_head != zr->v4l_pend_tail) {
2583 zr36057_set_memgrab(zr, 1);
2584 }
1da177e4
LT
2585 break;
2586
96b8e145
HV
2587 case ZORAN_MAP_MODE_JPG_REC:
2588 case ZORAN_MAP_MODE_JPG_PLAY:
2589 /* what is the codec mode right now? */
2590 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
1159b7f1 2591 fh->buffers.active != ZORAN_ACTIVE) {
96b8e145
HV
2592 res = -EBUSY;
2593 goto strmon_unlock_and_return;
2594 }
1da177e4 2595
1159b7f1 2596 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
1da177e4 2597
96b8e145
HV
2598 if (zr->jpg_que_head != zr->jpg_que_tail) {
2599 /* Start the jpeg codec when the first frame is queued */
2600 jpeg_start(zr);
1da177e4 2601 }
96b8e145 2602 break;
1da177e4 2603
96b8e145
HV
2604 default:
2605 dprintk(1,
2606 KERN_ERR
2607 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2608 ZR_DEVNAME(zr), fh->map_mode);
2609 res = -EINVAL;
1da177e4 2610 break;
96b8e145
HV
2611 }
2612strmon_unlock_and_return:
2613 mutex_unlock(&zr->resource_lock);
1da177e4 2614
96b8e145
HV
2615 return res;
2616}
2617
2618static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2619{
2620 struct zoran_fh *fh = __fh;
2621 struct zoran *zr = fh->zr;
2622 int i, res = 0;
1159b7f1 2623 unsigned long flags;
1da177e4 2624
96b8e145 2625 mutex_lock(&zr->resource_lock);
1da177e4 2626
96b8e145
HV
2627 switch (fh->map_mode) {
2628 case ZORAN_MAP_MODE_RAW: /* raw capture */
1159b7f1 2629 if (fh->buffers.active == ZORAN_FREE &&
96b8e145
HV
2630 zr->v4l_buffers.active != ZORAN_FREE) {
2631 res = -EPERM; /* stay off other's settings! */
2632 goto strmoff_unlock_and_return;
1da177e4 2633 }
96b8e145
HV
2634 if (zr->v4l_buffers.active == ZORAN_FREE)
2635 goto strmoff_unlock_and_return;
1da177e4 2636
1159b7f1 2637 spin_lock_irqsave(&zr->spinlock, flags);
96b8e145
HV
2638 /* unload capture */
2639 if (zr->v4l_memgrab_active) {
1da177e4 2640
96b8e145 2641 zr36057_set_memgrab(zr, 0);
1da177e4 2642 }
1da177e4 2643
1159b7f1 2644 for (i = 0; i < fh->buffers.num_buffers; i++)
96b8e145 2645 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
1159b7f1 2646 fh->buffers = zr->v4l_buffers;
1da177e4 2647
1159b7f1 2648 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
1da177e4 2649
96b8e145
HV
2650 zr->v4l_grab_seq = 0;
2651 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2652 zr->v4l_sync_tail = 0;
1da177e4 2653
1159b7f1
TP
2654 spin_unlock_irqrestore(&zr->spinlock, flags);
2655
96b8e145 2656 break;
1da177e4 2657
96b8e145
HV
2658 case ZORAN_MAP_MODE_JPG_REC:
2659 case ZORAN_MAP_MODE_JPG_PLAY:
1159b7f1 2660 if (fh->buffers.active == ZORAN_FREE &&
96b8e145
HV
2661 zr->jpg_buffers.active != ZORAN_FREE) {
2662 res = -EPERM; /* stay off other's settings! */
2663 goto strmoff_unlock_and_return;
2664 }
2665 if (zr->jpg_buffers.active == ZORAN_FREE)
2666 goto strmoff_unlock_and_return;
2667
e5ee3f64 2668 res = jpg_qbuf(fh, -1,
96b8e145
HV
2669 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2670 BUZ_MODE_MOTION_COMPRESS :
2671 BUZ_MODE_MOTION_DECOMPRESS);
2672 if (res)
2673 goto strmoff_unlock_and_return;
2674 break;
2675 default:
2676 dprintk(1, KERN_ERR
2677 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2678 ZR_DEVNAME(zr), fh->map_mode);
2679 res = -EINVAL;
1da177e4 2680 break;
96b8e145
HV
2681 }
2682strmoff_unlock_and_return:
2683 mutex_unlock(&zr->resource_lock);
1da177e4 2684
96b8e145
HV
2685 return res;
2686}
1da177e4 2687
96b8e145
HV
2688static int zoran_queryctrl(struct file *file, void *__fh,
2689 struct v4l2_queryctrl *ctrl)
2690{
107063c6
HV
2691 struct zoran_fh *fh = __fh;
2692 struct zoran *zr = fh->zr;
2693
96b8e145
HV
2694 /* we only support hue/saturation/contrast/brightness */
2695 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2696 ctrl->id > V4L2_CID_HUE)
2697 return -EINVAL;
1da177e4 2698
0ab6e1c3 2699 decoder_call(zr, core, queryctrl, ctrl);
1da177e4 2700
96b8e145
HV
2701 return 0;
2702}
1da177e4 2703
96b8e145
HV
2704static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2705{
2706 struct zoran_fh *fh = __fh;
2707 struct zoran *zr = fh->zr;
1da177e4 2708
96b8e145
HV
2709 /* we only support hue/saturation/contrast/brightness */
2710 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2711 ctrl->id > V4L2_CID_HUE)
2712 return -EINVAL;
1da177e4 2713
96b8e145 2714 mutex_lock(&zr->resource_lock);
0ab6e1c3 2715 decoder_call(zr, core, g_ctrl, ctrl);
96b8e145 2716 mutex_unlock(&zr->resource_lock);
1da177e4 2717
96b8e145
HV
2718 return 0;
2719}
1da177e4 2720
96b8e145
HV
2721static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2722{
2723 struct zoran_fh *fh = __fh;
2724 struct zoran *zr = fh->zr;
1da177e4 2725
96b8e145
HV
2726 /* we only support hue/saturation/contrast/brightness */
2727 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2728 ctrl->id > V4L2_CID_HUE)
2729 return -EINVAL;
1da177e4 2730
96b8e145 2731 mutex_lock(&zr->resource_lock);
0ab6e1c3 2732 decoder_call(zr, core, s_ctrl, ctrl);
96b8e145 2733 mutex_unlock(&zr->resource_lock);
1da177e4 2734
96b8e145
HV
2735 return 0;
2736}
1da177e4 2737
96b8e145
HV
2738static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2739{
2740 struct zoran_fh *fh = __fh;
2741 struct zoran *zr = fh->zr;
96b8e145
HV
2742
2743 mutex_lock(&zr->resource_lock);
107063c6 2744 *std = zr->norm;
96b8e145 2745 mutex_unlock(&zr->resource_lock);
96b8e145
HV
2746 return 0;
2747}
1da177e4 2748
96b8e145
HV
2749static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2750{
2751 struct zoran_fh *fh = __fh;
2752 struct zoran *zr = fh->zr;
107063c6 2753 int res = 0;
1da177e4 2754
96b8e145 2755 mutex_lock(&zr->resource_lock);
107063c6 2756 res = zoran_set_norm(zr, *std);
96b8e145
HV
2757 if (res)
2758 goto sstd_unlock_and_return;
1da177e4 2759
96b8e145
HV
2760 res = wait_grab_pending(zr);
2761sstd_unlock_and_return:
2762 mutex_unlock(&zr->resource_lock);
2763 return res;
2764}
1da177e4 2765
96b8e145
HV
2766static int zoran_enum_input(struct file *file, void *__fh,
2767 struct v4l2_input *inp)
2768{
2769 struct zoran_fh *fh = __fh;
2770 struct zoran *zr = fh->zr;
1da177e4 2771
96b8e145
HV
2772 if (inp->index < 0 || inp->index >= zr->card.inputs)
2773 return -EINVAL;
2774 else {
2775 int id = inp->index;
2776 memset(inp, 0, sizeof(*inp));
2777 inp->index = id;
1da177e4 2778 }
1da177e4 2779
96b8e145
HV
2780 strncpy(inp->name, zr->card.input[inp->index].name,
2781 sizeof(inp->name) - 1);
2782 inp->type = V4L2_INPUT_TYPE_CAMERA;
2783 inp->std = V4L2_STD_ALL;
1da177e4 2784
96b8e145
HV
2785 /* Get status of video decoder */
2786 mutex_lock(&zr->resource_lock);
0ab6e1c3 2787 decoder_call(zr, video, g_input_status, &inp->status);
96b8e145 2788 mutex_unlock(&zr->resource_lock);
96b8e145
HV
2789 return 0;
2790}
1da177e4 2791
96b8e145
HV
2792static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2793{
2794 struct zoran_fh *fh = __fh;
2795 struct zoran *zr = fh->zr;
1da177e4 2796
96b8e145
HV
2797 mutex_lock(&zr->resource_lock);
2798 *input = zr->input;
2799 mutex_unlock(&zr->resource_lock);
1da177e4 2800
96b8e145
HV
2801 return 0;
2802}
1da177e4 2803
96b8e145
HV
2804static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2805{
2806 struct zoran_fh *fh = __fh;
2807 struct zoran *zr = fh->zr;
2808 int res;
1da177e4 2809
96b8e145
HV
2810 mutex_lock(&zr->resource_lock);
2811 res = zoran_set_input(zr, input);
2812 if (res)
2813 goto sinput_unlock_and_return;
1da177e4 2814
96b8e145
HV
2815 /* Make sure the changes come into effect */
2816 res = wait_grab_pending(zr);
2817sinput_unlock_and_return:
2818 mutex_unlock(&zr->resource_lock);
2819 return res;
2820}
1da177e4 2821
96b8e145
HV
2822static int zoran_enum_output(struct file *file, void *__fh,
2823 struct v4l2_output *outp)
2824{
2825 if (outp->index != 0)
2826 return -EINVAL;
1da177e4 2827
96b8e145
HV
2828 memset(outp, 0, sizeof(*outp));
2829 outp->index = 0;
2830 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2831 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
1da177e4 2832
96b8e145
HV
2833 return 0;
2834}
1da177e4 2835
96b8e145
HV
2836static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2837{
2838 *output = 0;
2839
2840 return 0;
2841}
1da177e4 2842
96b8e145
HV
2843static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2844{
2845 if (output != 0)
2846 return -EINVAL;
1da177e4 2847
96b8e145
HV
2848 return 0;
2849}
1da177e4 2850
96b8e145
HV
2851/* cropping (sub-frame capture) */
2852static int zoran_cropcap(struct file *file, void *__fh,
2853 struct v4l2_cropcap *cropcap)
2854{
2855 struct zoran_fh *fh = __fh;
2856 struct zoran *zr = fh->zr;
2857 int type = cropcap->type, res = 0;
1da177e4 2858
96b8e145
HV
2859 memset(cropcap, 0, sizeof(*cropcap));
2860 cropcap->type = type;
1da177e4 2861
96b8e145 2862 mutex_lock(&zr->resource_lock);
1da177e4 2863
96b8e145
HV
2864 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2865 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2866 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2867 dprintk(1, KERN_ERR
2868 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
1da177e4 2869 ZR_DEVNAME(zr));
96b8e145
HV
2870 res = -EINVAL;
2871 goto cropcap_unlock_and_return;
2872 }
1da177e4 2873
96b8e145
HV
2874 cropcap->bounds.top = cropcap->bounds.left = 0;
2875 cropcap->bounds.width = BUZ_MAX_WIDTH;
2876 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2877 cropcap->defrect.top = cropcap->defrect.left = 0;
2878 cropcap->defrect.width = BUZ_MIN_WIDTH;
2879 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2880cropcap_unlock_and_return:
2881 mutex_unlock(&zr->resource_lock);
2882 return res;
2883}
1da177e4 2884
96b8e145
HV
2885static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2886{
2887 struct zoran_fh *fh = __fh;
2888 struct zoran *zr = fh->zr;
2889 int type = crop->type, res = 0;
1da177e4 2890
96b8e145
HV
2891 memset(crop, 0, sizeof(*crop));
2892 crop->type = type;
1da177e4 2893
96b8e145 2894 mutex_lock(&zr->resource_lock);
1da177e4 2895
96b8e145
HV
2896 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2897 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2898 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2899 dprintk(1,
2900 KERN_ERR
2901 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2902 ZR_DEVNAME(zr));
2903 res = -EINVAL;
2904 goto gcrop_unlock_and_return;
1da177e4 2905 }
1da177e4 2906
96b8e145
HV
2907 crop->c.top = fh->jpg_settings.img_y;
2908 crop->c.left = fh->jpg_settings.img_x;
2909 crop->c.width = fh->jpg_settings.img_width;
2910 crop->c.height = fh->jpg_settings.img_height;
1da177e4 2911
96b8e145
HV
2912gcrop_unlock_and_return:
2913 mutex_unlock(&zr->resource_lock);
1da177e4 2914
96b8e145
HV
2915 return res;
2916}
1da177e4 2917
96b8e145
HV
2918static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2919{
2920 struct zoran_fh *fh = __fh;
2921 struct zoran *zr = fh->zr;
2922 int res = 0;
2923 struct zoran_jpg_settings settings;
1da177e4 2924
96b8e145 2925 settings = fh->jpg_settings;
1da177e4 2926
96b8e145 2927 mutex_lock(&zr->resource_lock);
1da177e4 2928
1159b7f1 2929 if (fh->buffers.allocated) {
96b8e145
HV
2930 dprintk(1, KERN_ERR
2931 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2932 ZR_DEVNAME(zr));
2933 res = -EBUSY;
2934 goto scrop_unlock_and_return;
1da177e4 2935 }
1da177e4 2936
96b8e145
HV
2937 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2938 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2939 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2940 dprintk(1, KERN_ERR
2941 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2942 ZR_DEVNAME(zr));
2943 res = -EINVAL;
2944 goto scrop_unlock_and_return;
2945 }
1da177e4 2946
96b8e145
HV
2947 /* move into a form that we understand */
2948 settings.img_x = crop->c.left;
2949 settings.img_y = crop->c.top;
2950 settings.img_width = crop->c.width;
2951 settings.img_height = crop->c.height;
1da177e4 2952
96b8e145 2953 /* check validity */
0ba514d5 2954 res = zoran_check_jpg_settings(zr, &settings, 0);
96b8e145
HV
2955 if (res)
2956 goto scrop_unlock_and_return;
1da177e4 2957
96b8e145
HV
2958 /* accept */
2959 fh->jpg_settings = settings;
1da177e4 2960
96b8e145
HV
2961scrop_unlock_and_return:
2962 mutex_unlock(&zr->resource_lock);
2963 return res;
2964}
1da177e4 2965
96b8e145
HV
2966static int zoran_g_jpegcomp(struct file *file, void *__fh,
2967 struct v4l2_jpegcompression *params)
2968{
2969 struct zoran_fh *fh = __fh;
2970 struct zoran *zr = fh->zr;
2971 memset(params, 0, sizeof(*params));
1da177e4 2972
96b8e145 2973 mutex_lock(&zr->resource_lock);
1da177e4 2974
96b8e145
HV
2975 params->quality = fh->jpg_settings.jpg_comp.quality;
2976 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2977 memcpy(params->APP_data,
2978 fh->jpg_settings.jpg_comp.APP_data,
2979 fh->jpg_settings.jpg_comp.APP_len);
2980 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2981 memcpy(params->COM_data,
2982 fh->jpg_settings.jpg_comp.COM_data,
2983 fh->jpg_settings.jpg_comp.COM_len);
2984 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2985 params->jpeg_markers =
2986 fh->jpg_settings.jpg_comp.jpeg_markers;
1da177e4 2987
96b8e145 2988 mutex_unlock(&zr->resource_lock);
1da177e4 2989
96b8e145
HV
2990 return 0;
2991}
1da177e4 2992
96b8e145
HV
2993static int zoran_s_jpegcomp(struct file *file, void *__fh,
2994 struct v4l2_jpegcompression *params)
2995{
2996 struct zoran_fh *fh = __fh;
2997 struct zoran *zr = fh->zr;
2998 int res = 0;
2999 struct zoran_jpg_settings settings;
1da177e4 3000
96b8e145 3001 settings = fh->jpg_settings;
1da177e4 3002
96b8e145 3003 settings.jpg_comp = *params;
1da177e4 3004
96b8e145 3005 mutex_lock(&zr->resource_lock);
1da177e4 3006
1159b7f1 3007 if (fh->buffers.active != ZORAN_FREE) {
96b8e145
HV
3008 dprintk(1, KERN_WARNING
3009 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3010 ZR_DEVNAME(zr));
3011 res = -EBUSY;
3012 goto sjpegc_unlock_and_return;
1da177e4 3013 }
1da177e4 3014
0ba514d5 3015 res = zoran_check_jpg_settings(zr, &settings, 0);
96b8e145
HV
3016 if (res)
3017 goto sjpegc_unlock_and_return;
1159b7f1
TP
3018 if (!fh->buffers.allocated)
3019 fh->buffers.buffer_size =
3020 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
96b8e145
HV
3021 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3022sjpegc_unlock_and_return:
3023 mutex_unlock(&zr->resource_lock);
1da177e4 3024
27f79525 3025 return res;
1da177e4
LT
3026}
3027
1da177e4
LT
3028static unsigned int
3029zoran_poll (struct file *file,
3030 poll_table *wait)
3031{
3032 struct zoran_fh *fh = file->private_data;
3033 struct zoran *zr = fh->zr;
1da177e4 3034 int res = 0, frame;
e42af83f 3035 unsigned long flags;
1da177e4
LT
3036
3037 /* we should check whether buffers are ready to be synced on
3038 * (w/o waits - O_NONBLOCK) here
3039 * if ready for read (sync), return POLLIN|POLLRDNORM,
3040 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3041 * if error, return POLLERR,
3042 * if no buffers queued or so, return POLLNVAL
3043 */
3044
384c3689 3045 mutex_lock(&zr->resource_lock);
1da177e4
LT
3046
3047 switch (fh->map_mode) {
3048 case ZORAN_MAP_MODE_RAW:
e42af83f
TP
3049 poll_wait(file, &zr->v4l_capq, wait);
3050 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3051
3052 spin_lock_irqsave(&zr->spinlock, flags);
3053 dprintk(3,
3054 KERN_DEBUG
3055 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
7e28adb2 3056 ZR_DEVNAME(zr), __func__,
1159b7f1 3057 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
e42af83f
TP
3058 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3059 zr->v4l_pend_tail, zr->v4l_pend_head);
3060 /* Process is the one capturing? */
1159b7f1 3061 if (fh->buffers.active != ZORAN_FREE &&
e42af83f
TP
3062 /* Buffer ready to DQBUF? */
3063 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
1da177e4 3064 res = POLLIN | POLLRDNORM;
e42af83f
TP
3065 spin_unlock_irqrestore(&zr->spinlock, flags);
3066
1da177e4
LT
3067 break;
3068
3069 case ZORAN_MAP_MODE_JPG_REC:
3070 case ZORAN_MAP_MODE_JPG_PLAY:
e42af83f 3071 poll_wait(file, &zr->jpg_capq, wait);
1da177e4 3072 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
e42af83f
TP
3073
3074 spin_lock_irqsave(&zr->spinlock, flags);
3075 dprintk(3,
3076 KERN_DEBUG
3077 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
7e28adb2 3078 ZR_DEVNAME(zr), __func__,
1159b7f1 3079 "FAL"[fh->buffers.active], zr->jpg_que_tail,
e42af83f
TP
3080 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3081 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
1159b7f1 3082 if (fh->buffers.active != ZORAN_FREE &&
e42af83f 3083 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
1da177e4
LT
3084 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3085 res = POLLIN | POLLRDNORM;
3086 else
3087 res = POLLOUT | POLLWRNORM;
3088 }
e42af83f
TP
3089 spin_unlock_irqrestore(&zr->spinlock, flags);
3090
1da177e4
LT
3091 break;
3092
3093 default:
3094 dprintk(1,
e42af83f 3095 KERN_ERR
c61402ba
TP
3096 "%s: %s - internal error, unknown map_mode=%d\n",
3097 ZR_DEVNAME(zr), __func__, fh->map_mode);
1da177e4 3098 res = POLLNVAL;
1da177e4
LT
3099 }
3100
384c3689 3101 mutex_unlock(&zr->resource_lock);
1da177e4
LT
3102
3103 return res;
3104}
3105
3106
3107/*
3108 * This maps the buffers to user space.
3109 *
3110 * Depending on the state of fh->map_mode
3111 * the V4L or the MJPEG buffers are mapped
3112 * per buffer or all together
3113 *
3114 * Note that we need to connect to some
3115 * unmap signal event to unmap the de-allocate
3116 * the buffer accordingly (zoran_vm_close())
3117 */
3118
3119static void
3120zoran_vm_open (struct vm_area_struct *vma)
3121{
3122 struct zoran_mapping *map = vma->vm_private_data;
3123
3124 map->count++;
3125}
3126
3127static void
3128zoran_vm_close (struct vm_area_struct *vma)
3129{
3130 struct zoran_mapping *map = vma->vm_private_data;
e5ee3f64 3131 struct zoran_fh *fh = map->file->private_data;
1da177e4
LT
3132 struct zoran *zr = fh->zr;
3133 int i;
3134
1159b7f1
TP
3135 if (--map->count > 0)
3136 return;
1da177e4 3137
1159b7f1
TP
3138 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3139 __func__, mode_name(fh->map_mode));
1da177e4 3140
1159b7f1
TP
3141 for (i = 0; i < fh->buffers.num_buffers; i++) {
3142 if (fh->buffers.buffer[i].map == map)
3143 fh->buffers.buffer[i].map = NULL;
3144 }
3145 kfree(map);
1da177e4 3146
1159b7f1
TP
3147 /* Any buffers still mapped? */
3148 for (i = 0; i < fh->buffers.num_buffers; i++)
3149 if (fh->buffers.buffer[i].map)
3150 return;
1da177e4 3151
1159b7f1
TP
3152 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3153 __func__, mode_name(fh->map_mode));
1da177e4 3154
1159b7f1 3155 mutex_lock(&zr->resource_lock);
1da177e4 3156
1159b7f1
TP
3157 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3158 if (fh->buffers.active != ZORAN_FREE) {
3159 unsigned long flags;
1da177e4 3160
1159b7f1
TP
3161 spin_lock_irqsave(&zr->spinlock, flags);
3162 zr36057_set_memgrab(zr, 0);
3163 zr->v4l_buffers.allocated = 0;
3164 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3165 spin_unlock_irqrestore(&zr->spinlock, flags);
3166 }
e5ee3f64 3167 v4l_fbuffer_free(fh);
1159b7f1
TP
3168 } else {
3169 if (fh->buffers.active != ZORAN_FREE) {
e5ee3f64 3170 jpg_qbuf(fh, -1, zr->codec_mode);
1159b7f1
TP
3171 zr->jpg_buffers.allocated = 0;
3172 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
1da177e4 3173 }
e5ee3f64 3174 jpg_fbuffer_free(fh);
1da177e4 3175 }
1159b7f1
TP
3176
3177 mutex_unlock(&zr->resource_lock);
1da177e4
LT
3178}
3179
3180static struct vm_operations_struct zoran_vm_ops = {
3181 .open = zoran_vm_open,
3182 .close = zoran_vm_close,
3183};
3184
3185static int
3186zoran_mmap (struct file *file,
3187 struct vm_area_struct *vma)
3188{
3189 struct zoran_fh *fh = file->private_data;
3190 struct zoran *zr = fh->zr;
3191 unsigned long size = (vma->vm_end - vma->vm_start);
3192 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3193 int i, j;
3194 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3195 int first, last;
3196 struct zoran_mapping *map;
3197 int res = 0;
3198
3199 dprintk(3,
c61402ba
TP
3200 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3201 ZR_DEVNAME(zr), __func__,
1159b7f1 3202 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
1da177e4
LT
3203
3204 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3205 !(vma->vm_flags & VM_WRITE)) {
3206 dprintk(1,
3207 KERN_ERR
c61402ba
TP
3208 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3209 ZR_DEVNAME(zr), __func__);
1da177e4
LT
3210 return -EINVAL;
3211 }
3212
1159b7f1 3213 mutex_lock(&zr->resource_lock);
1da177e4 3214
1159b7f1
TP
3215 if (!fh->buffers.allocated) {
3216 dprintk(1,
3217 KERN_ERR
c61402ba
TP
3218 "%s: %s(%s) - buffers not yet allocated\n",
3219 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
1159b7f1
TP
3220 res = -ENOMEM;
3221 goto mmap_unlock_and_return;
3222 }
1da177e4 3223
1159b7f1
TP
3224 first = offset / fh->buffers.buffer_size;
3225 last = first - 1 + size / fh->buffers.buffer_size;
3226 if (offset % fh->buffers.buffer_size != 0 ||
3227 size % fh->buffers.buffer_size != 0 || first < 0 ||
3228 last < 0 || first >= fh->buffers.num_buffers ||
3229 last >= fh->buffers.buffer_size) {
3230 dprintk(1,
3231 KERN_ERR
c61402ba
TP
3232 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3233 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
1159b7f1
TP
3234 fh->buffers.buffer_size,
3235 fh->buffers.num_buffers);
3236 res = -EINVAL;
3237 goto mmap_unlock_and_return;
3238 }
1da177e4 3239
1159b7f1
TP
3240 /* Check if any buffers are already mapped */
3241 for (i = first; i <= last; i++) {
3242 if (fh->buffers.buffer[i].map) {
1da177e4
LT
3243 dprintk(1,
3244 KERN_ERR
c61402ba
TP
3245 "%s: %s(%s) - buffer %d already mapped\n",
3246 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
1159b7f1
TP
3247 res = -EBUSY;
3248 goto mmap_unlock_and_return;
1da177e4 3249 }
1159b7f1 3250 }
1da177e4 3251
1159b7f1
TP
3252 /* map these buffers */
3253 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3254 if (!map) {
3255 res = -ENOMEM;
3256 goto mmap_unlock_and_return;
3257 }
3258 map->file = file;
3259 map->count = 1;
3260
3261 vma->vm_ops = &zoran_vm_ops;
3262 vma->vm_flags |= VM_DONTEXPAND;
3263 vma->vm_private_data = map;
3264
3265 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1da177e4 3266 for (i = first; i <= last; i++) {
1159b7f1
TP
3267 todo = size;
3268 if (todo > fh->buffers.buffer_size)
3269 todo = fh->buffers.buffer_size;
3270 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3271 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3272 todo, PAGE_SHARED)) {
1da177e4
LT
3273 dprintk(1,
3274 KERN_ERR
c61402ba
TP
3275 "%s: %s(V4L) - remap_pfn_range failed\n",
3276 ZR_DEVNAME(zr), __func__);
1159b7f1
TP
3277 res = -EAGAIN;
3278 goto mmap_unlock_and_return;
1da177e4 3279 }
1159b7f1
TP
3280 size -= todo;
3281 start += todo;
3282 fh->buffers.buffer[i].map = map;
3283 if (size == 0)
3284 break;
1da177e4 3285 }
1159b7f1 3286 } else {
1da177e4
LT
3287 for (i = first; i <= last; i++) {
3288 for (j = 0;
1159b7f1 3289 j < fh->buffers.buffer_size / PAGE_SIZE;
1da177e4
LT
3290 j++) {
3291 fraglen =
1159b7f1 3292 (le32_to_cpu(fh->buffers.buffer[i].jpg.
1da177e4
LT
3293 frag_tab[2 * j + 1]) & ~1) << 1;
3294 todo = size;
3295 if (todo > fraglen)
3296 todo = fraglen;
3297 pos =
1159b7f1
TP
3298 le32_to_cpu(fh->buffers.
3299 buffer[i].jpg.frag_tab[2 * j]);
1da177e4
LT
3300 /* should just be pos on i386 */
3301 page = virt_to_phys(bus_to_virt(pos))
3302 >> PAGE_SHIFT;
3303 if (remap_pfn_range(vma, start, page,
3304 todo, PAGE_SHARED)) {
3305 dprintk(1,
3306 KERN_ERR
c61402ba
TP
3307 "%s: %s(V4L) - remap_pfn_range failed\n",
3308 ZR_DEVNAME(zr), __func__);
1da177e4 3309 res = -EAGAIN;
1159b7f1 3310 goto mmap_unlock_and_return;
1da177e4
LT
3311 }
3312 size -= todo;
3313 start += todo;
3314 if (size == 0)
3315 break;
1159b7f1 3316 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
1da177e4
LT
3317 frag_tab[2 * j + 1]) & 1)
3318 break; /* was last fragment */
3319 }
1159b7f1 3320 fh->buffers.buffer[i].map = map;
1da177e4
LT
3321 if (size == 0)
3322 break;
3323
3324 }
1da177e4
LT
3325 }
3326
1159b7f1
TP
3327mmap_unlock_and_return:
3328 mutex_unlock(&zr->resource_lock);
3329
1da177e4
LT
3330 return 0;
3331}
3332
96b8e145
HV
3333static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3334 .vidioc_querycap = zoran_querycap,
3335 .vidioc_cropcap = zoran_cropcap,
3336 .vidioc_s_crop = zoran_s_crop,
3337 .vidioc_g_crop = zoran_g_crop,
3338 .vidioc_enum_input = zoran_enum_input,
3339 .vidioc_g_input = zoran_g_input,
3340 .vidioc_s_input = zoran_s_input,
3341 .vidioc_enum_output = zoran_enum_output,
3342 .vidioc_g_output = zoran_g_output,
3343 .vidioc_s_output = zoran_s_output,
3344 .vidioc_g_fbuf = zoran_g_fbuf,
3345 .vidioc_s_fbuf = zoran_s_fbuf,
3346 .vidioc_g_std = zoran_g_std,
3347 .vidioc_s_std = zoran_s_std,
3348 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3349 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3350 .vidioc_overlay = zoran_overlay,
3351 .vidioc_reqbufs = zoran_reqbufs,
3352 .vidioc_querybuf = zoran_querybuf,
3353 .vidioc_qbuf = zoran_qbuf,
3354 .vidioc_dqbuf = zoran_dqbuf,
3355 .vidioc_streamon = zoran_streamon,
3356 .vidioc_streamoff = zoran_streamoff,
3357 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3358 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3359 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3360 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3361 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3362 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3363 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3364 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3365 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3366 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3367 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3368 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3369 .vidioc_queryctrl = zoran_queryctrl,
3370 .vidioc_s_ctrl = zoran_s_ctrl,
3371 .vidioc_g_ctrl = zoran_g_ctrl,
dcbd83b1 3372#ifdef CONFIG_VIDEO_V4L1_COMPAT
96b8e145 3373 .vidioc_default = zoran_default,
dcbd83b1
HV
3374 .vidiocgmbuf = zoran_vidiocgmbuf,
3375#endif
96b8e145
HV
3376};
3377
bec43661 3378static const struct v4l2_file_operations zoran_fops = {
1da177e4
LT
3379 .owner = THIS_MODULE,
3380 .open = zoran_open,
3381 .release = zoran_close,
96b8e145 3382 .ioctl = video_ioctl2,
1da177e4
LT
3383 .read = zoran_read,
3384 .write = zoran_write,
3385 .mmap = zoran_mmap,
3386 .poll = zoran_poll,
3387};
3388
3389struct video_device zoran_template __devinitdata = {
3390 .name = ZORAN_NAME,
1da177e4 3391 .fops = &zoran_fops,
96b8e145 3392 .ioctl_ops = &zoran_ioctl_ops,
1da177e4 3393 .release = &zoran_vdev_release,
96b8e145 3394 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
1da177e4
LT
3395 .minor = -1
3396};
3397