zoran_driver.c 113.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*
 * Zoran zr36057/zr36067 PCI controller driver, for the
 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
 * Media Labs LML33/LML33R10.
 *
 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
 *
 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
 *
 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
 *
 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
 *
 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * Based on
 *
 * Miro DC10 driver
 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
 *
 * Iomega Buz driver version 1.0
 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
 *
 * buz.0.0.3
 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
 *
 * bttv - Bt848 frame grabber driver
 * Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
 *                        & Marcus Metzler (mocm@thp.uni-koeln.de)
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/version.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>

#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>

#include <linux/spinlock.h>
#define     MAP_NR(x)       virt_to_page(x)
#define     ZORAN_VID_TYPE  ( \
				VID_TYPE_CAPTURE | \
				VID_TYPE_OVERLAY | \
				VID_TYPE_CLIPPING | \
				VID_TYPE_FRAMERAM | \
				VID_TYPE_SCALES | \
				VID_TYPE_MJPEG_DECODER | \
				VID_TYPE_MJPEG_ENCODER \
			     )

#include <linux/videodev.h>
73
#include <media/v4l2-common.h>
74
#include <media/v4l2-ioctl.h>
L
Linus Torvalds 已提交
75 76
#include "videocodec.h"

77
#include <asm/byteorder.h>
L
Linus Torvalds 已提交
78 79 80 81 82 83
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>

#include <linux/video_decoder.h>
#include <linux/video_encoder.h>
I
Ingo Molnar 已提交
84
#include <linux/mutex.h>
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95
#include "zoran.h"
#include "zoran_device.h"
#include "zoran_card.h"

	/* we declare some card type definitions here, they mean
	 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
#define ZORAN_V4L2_VID_FLAGS ( \
				V4L2_CAP_STREAMING |\
				V4L2_CAP_VIDEO_CAPTURE |\
				V4L2_CAP_VIDEO_OUTPUT |\
				V4L2_CAP_VIDEO_OVERLAY \
96
			      )
L
Linus Torvalds 已提交
97 98


99
#if defined(CONFIG_VIDEO_V4L1_COMPAT)
100 101
#define ZFMT(pal, fcc, cs) \
	.palette = (pal), .fourcc = (fcc), .colorspace = (cs)
L
Linus Torvalds 已提交
102
#else
103
#define ZFMT(pal, fcc, cs) \
104
	.fourcc = (fcc), .colorspace = (cs)
L
Linus Torvalds 已提交
105
#endif
106 107 108 109 110 111

const struct zoran_format zoran_formats[] = {
	{
		.name = "15-bit RGB LE",
		ZFMT(VIDEO_PALETTE_RGB555,
		     V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
L
Linus Torvalds 已提交
112 113 114
		.depth = 15,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
115 116
		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
			   ZR36057_VFESPFR_LittleEndian,
L
Linus Torvalds 已提交
117
	}, {
118 119 120 121 122 123 124 125 126 127 128
		.name = "15-bit RGB BE",
		ZFMT(-1,
		     V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
		.depth = 15,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
	}, {
		.name = "16-bit RGB LE",
		ZFMT(VIDEO_PALETTE_RGB565,
		     V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
L
Linus Torvalds 已提交
129 130 131
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
132 133 134 135 136
		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
			   ZR36057_VFESPFR_LittleEndian,
	}, {
		.name = "16-bit RGB BE",
		ZFMT(-1,
137
		     V4L2_PIX_FMT_RGB565X, V4L2_COLORSPACE_SRGB),
138 139 140 141
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
L
Linus Torvalds 已提交
142 143
	}, {
		.name = "24-bit RGB",
144 145
		ZFMT(VIDEO_PALETTE_RGB24,
		     V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
L
Linus Torvalds 已提交
146 147 148
		.depth = 24,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
149
		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
L
Linus Torvalds 已提交
150
	}, {
151 152 153
		.name = "32-bit RGB LE",
		ZFMT(VIDEO_PALETTE_RGB32,
		     V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
L
Linus Torvalds 已提交
154 155 156
		.depth = 32,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
157 158 159 160 161 162 163 164 165
		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
	}, {
		.name = "32-bit RGB BE",
		ZFMT(-1,
		     V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
		.depth = 32,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_RGB888,
L
Linus Torvalds 已提交
166 167
	}, {
		.name = "4:2:2, packed, YUYV",
168 169
		ZFMT(VIDEO_PALETTE_YUV422,
		     V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
L
Linus Torvalds 已提交
170 171
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
172
			 ZORAN_FORMAT_OVERLAY,
173 174 175 176 177 178 179 180 181
		.vfespfr = ZR36057_VFESPFR_YUV422,
	}, {
		.name = "4:2:2, packed, UYVY",
		ZFMT(VIDEO_PALETTE_UYVY,
		     V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
L
Linus Torvalds 已提交
182 183
	}, {
		.name = "Hardware-encoded Motion-JPEG",
184 185
		ZFMT(-1,
		     V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
L
Linus Torvalds 已提交
186 187 188 189 190 191
		.depth = 0,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_PLAYBACK |
			 ZORAN_FORMAT_COMPRESSED,
	}
};
192
#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
L
Linus Torvalds 已提交
193 194 195 196

// RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined


197
static int lock_norm;	/* 0 = default 1 = Don't change TV standard (norm) */
198 199
module_param(lock_norm, int, 0644);
MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

	/* small helper function for calculating buffersizes for v4l2
	 * we calculate the nearest higher power-of-two, which
	 * will be the recommended buffersize */
static __u32
zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
{
	__u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
	__u32 num = (1024 * 512) / (div);
	__u32 result = 2;

	num--;
	while (num) {
		num >>= 1;
		result <<= 1;
	}

	if (result > jpg_bufsize)
		return jpg_bufsize;
	if (result < 8192)
		return 8192;
	return result;
}

/* forward references */
static void v4l_fbuffer_free(struct file *file);
static void jpg_fbuffer_free(struct file *file);

/*
 *   Allocate the V4L grab buffers
 *
 *   These have to be pysically contiguous.
 *   If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
 *   else we try to allocate them with bigphysarea_alloc_pages
 *   if the bigphysarea patch is present in the kernel,
 *   else we try to use high memory (if the user has bootet
 *   Linux with the necessary memory left over).
 */

static unsigned long
get_high_mem (unsigned long size)
{
/*
 * Check if there is usable memory at the end of Linux memory
 * of at least size. Return the physical address of this memory,
 * return 0 on failure.
 *
 * The idea is from Alexandro Rubini's book "Linux device drivers".
 * The driver from him which is downloadable from O'Reilly's
 * web site misses the "virt_to_phys(high_memory)" part
 * (and therefore doesn't work at all - at least with 2.2.x kernels).
 *
 * It should be unnecessary to mention that THIS IS DANGEROUS,
 * if more than one driver at a time has the idea to use this memory!!!!
 */

	volatile unsigned char __iomem *mem;
	unsigned char c;
	unsigned long hi_mem_ph;
	unsigned long i;

	/* Map the high memory to user space */

	hi_mem_ph = virt_to_phys(high_memory);

	mem = ioremap(hi_mem_ph, size);
	if (!mem) {
		dprintk(1,
			KERN_ERR "%s: get_high_mem() - ioremap failed\n",
			ZORAN_NAME);
		return 0;
	}

	for (i = 0; i < size; i++) {
		/* Check if it is memory */
		c = i & 0xff;
		writeb(c, mem + i);
		if (readb(mem + i) != c)
			break;
		c = 255 - c;
		writeb(c, mem + i);
		if (readb(mem + i) != c)
			break;
		writeb(0, mem + i);	/* zero out memory */

		/* give the kernel air to breath */
		if ((i & 0x3ffff) == 0x3ffff)
			schedule();
	}

	iounmap(mem);

	if (i != size) {
		dprintk(1,
			KERN_ERR
			"%s: get_high_mem() - requested %lu, avail %lu\n",
			ZORAN_NAME, size, i);
		return 0;
	}

	return hi_mem_ph;
}

static int
v4l_fbuffer_alloc (struct file *file)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int i, off;
	unsigned char *mem;
	unsigned long pmem = 0;

	/* we might have old buffers lying around... */
	if (fh->v4l_buffers.ready_to_be_freed) {
		v4l_fbuffer_free(file);
	}

	for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
		if (fh->v4l_buffers.buffer[i].fbuffer)
			dprintk(2,
				KERN_WARNING
				"%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
				ZR_DEVNAME(zr), i);

		//udelay(20);
		if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
			/* Use kmalloc */

328
			mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
A
Al Viro 已提交
329
			if (!mem) {
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
				dprintk(1,
					KERN_ERR
					"%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
					ZR_DEVNAME(zr), i);
				v4l_fbuffer_free(file);
				return -ENOBUFS;
			}
			fh->v4l_buffers.buffer[i].fbuffer = mem;
			fh->v4l_buffers.buffer[i].fbuffer_phys =
			    virt_to_phys(mem);
			fh->v4l_buffers.buffer[i].fbuffer_bus =
			    virt_to_bus(mem);
			for (off = 0; off < fh->v4l_buffers.buffer_size;
			     off += PAGE_SIZE)
				SetPageReserved(MAP_NR(mem + off));
			dprintk(4,
				KERN_INFO
347
				"%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
L
Linus Torvalds 已提交
348
				ZR_DEVNAME(zr), i, (unsigned long) mem,
349
				(unsigned long long)virt_to_bus(mem));
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		} else {

			/* Use high memory which has been left at boot time */

			/* Ok., Ok. this is an evil hack - we make
			 * the assumption that physical addresses are
			 * the same as bus addresses (true at least
			 * for Intel processors). The whole method of
			 * obtaining and using this memory is not very
			 * nice - but I hope it saves some poor users
			 * from kernel hacking, which might have even
			 * more evil results */

			if (i == 0) {
				int size =
				    fh->v4l_buffers.num_buffers *
				    fh->v4l_buffers.buffer_size;

				pmem = get_high_mem(size);
				if (pmem == 0) {
					dprintk(1,
						KERN_ERR
						"%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
						ZR_DEVNAME(zr), size >> 10);
					return -ENOBUFS;
				}
				fh->v4l_buffers.buffer[0].fbuffer = NULL;
				fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
				fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
				dprintk(4,
					KERN_INFO
					"%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
					ZR_DEVNAME(zr), size >> 10);
			} else {
				fh->v4l_buffers.buffer[i].fbuffer = NULL;
				fh->v4l_buffers.buffer[i].fbuffer_phys =
				    pmem + i * fh->v4l_buffers.buffer_size;
				fh->v4l_buffers.buffer[i].fbuffer_bus =
				    pmem + i * fh->v4l_buffers.buffer_size;
			}
		}
	}

	fh->v4l_buffers.allocated = 1;

	return 0;
}

/* free the V4L grab buffers */
static void
v4l_fbuffer_free (struct file *file)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int i, off;
	unsigned char *mem;

	dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));

	for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
		if (!fh->v4l_buffers.buffer[i].fbuffer)
			continue;

		if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
			mem = fh->v4l_buffers.buffer[i].fbuffer;
			for (off = 0; off < fh->v4l_buffers.buffer_size;
			     off += PAGE_SIZE)
				ClearPageReserved(MAP_NR(mem + off));
			kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
		}
		fh->v4l_buffers.buffer[i].fbuffer = NULL;
	}

	fh->v4l_buffers.allocated = 0;
	fh->v4l_buffers.ready_to_be_freed = 0;
}

/*
 *   Allocate the MJPEG grab buffers.
 *
 *   If the requested buffer size is smaller than MAX_KMALLOC_MEM,
 *   kmalloc is used to request a physically contiguous area,
 *   else we allocate the memory in framgents with get_zeroed_page.
 *
 *   If a Natoma chipset is present and this is a revision 1 zr36057,
 *   each MJPEG buffer needs to be physically contiguous.
 *   (RJ: This statement is from Dave Perks' original driver,
 *   I could never check it because I have a zr36067)
 *   The driver cares about this because it reduces the buffer
 *   size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
 *
 *   RJ: The contents grab buffers needs never be accessed in the driver.
 *       Therefore there is no need to allocate them with vmalloc in order
 *       to get a contiguous virtual memory space.
 *       I don't understand why many other drivers first allocate them with
 *       vmalloc (which uses internally also get_zeroed_page, but delivers you
 *       virtual addresses) and then again have to make a lot of efforts
 *       to get the physical address.
 *
 *   Ben Capper:
 *       On big-endian architectures (such as ppc) some extra steps
 *       are needed. When reading and writing to the stat_com array
 *       and fragment buffers, the device expects to see little-
 *       endian values. The use of cpu_to_le32() and le32_to_cpu()
 *       in this function (and one or two others in zoran_device.c)
 *       ensure that these values are always stored in little-endian
 *       form, regardless of architecture. The zr36057 does Very Bad
 *       Things on big endian architectures if the stat_com array
 *       and fragment buffers are not little-endian.
 */

static int
jpg_fbuffer_alloc (struct file *file)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int i, j, off;
	unsigned long mem;

	/* we might have old buffers lying around */
	if (fh->jpg_buffers.ready_to_be_freed) {
		jpg_fbuffer_free(file);
	}

	for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
		if (fh->jpg_buffers.buffer[i].frag_tab)
			dprintk(2,
				KERN_WARNING
				"%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
				ZR_DEVNAME(zr), i);

		/* Allocate fragment table for this buffer */

		mem = get_zeroed_page(GFP_KERNEL);
		if (mem == 0) {
			dprintk(1,
				KERN_ERR
				"%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
				ZR_DEVNAME(zr), i);
			jpg_fbuffer_free(file);
			return -ENOBUFS;
		}
492
		fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		fh->jpg_buffers.buffer[i].frag_tab_bus =
		    virt_to_bus((void *) mem);

		//if (alloc_contig) {
		if (fh->jpg_buffers.need_contiguous) {
			mem =
			    (unsigned long) kmalloc(fh->jpg_buffers.
						    buffer_size,
						    GFP_KERNEL);
			if (mem == 0) {
				dprintk(1,
					KERN_ERR
					"%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
					ZR_DEVNAME(zr), i);
				jpg_fbuffer_free(file);
				return -ENOBUFS;
			}
			fh->jpg_buffers.buffer[i].frag_tab[0] =
			    cpu_to_le32(virt_to_bus((void *) mem));
			fh->jpg_buffers.buffer[i].frag_tab[1] =
			    cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
			for (off = 0; off < fh->jpg_buffers.buffer_size;
			     off += PAGE_SIZE)
				SetPageReserved(MAP_NR(mem + off));
		} else {
			/* jpg_bufsize is allreay page aligned */
			for (j = 0;
			     j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
			     j++) {
				mem = get_zeroed_page(GFP_KERNEL);
				if (mem == 0) {
					dprintk(1,
						KERN_ERR
						"%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
						ZR_DEVNAME(zr), i);
					jpg_fbuffer_free(file);
					return -ENOBUFS;
				}

				fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
				    cpu_to_le32(virt_to_bus((void *) mem));
				fh->jpg_buffers.buffer[i].frag_tab[2 * j +
								   1] =
				    cpu_to_le32((PAGE_SIZE / 4) << 1);
				SetPageReserved(MAP_NR(mem));
			}

			fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
		}
	}

	dprintk(4,
		KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
		ZR_DEVNAME(zr),
		(fh->jpg_buffers.num_buffers *
		 fh->jpg_buffers.buffer_size) >> 10);

	fh->jpg_buffers.allocated = 1;

	return 0;
}

/* free the MJPEG grab buffers */
static void
jpg_fbuffer_free (struct file *file)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int i, j, off;
	unsigned char *mem;

	dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));

	for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
		if (!fh->jpg_buffers.buffer[i].frag_tab)
			continue;

		//if (alloc_contig) {
		if (fh->jpg_buffers.need_contiguous) {
			if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
				mem = (unsigned char *) bus_to_virt(le32_to_cpu(
					fh->jpg_buffers.buffer[i].frag_tab[0]));
				for (off = 0;
				     off < fh->jpg_buffers.buffer_size;
				     off += PAGE_SIZE)
					ClearPageReserved(MAP_NR
							  (mem + off));
580
				kfree(mem);
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
				fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
				fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
			}
		} else {
			for (j = 0;
			     j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
			     j++) {
				if (!fh->jpg_buffers.buffer[i].
				    frag_tab[2 * j])
					break;
				ClearPageReserved(MAP_NR
						  (bus_to_virt
						   (le32_to_cpu
						    (fh->jpg_buffers.
						     buffer[i].frag_tab[2 *
								       j]))));
				free_page((unsigned long)
					  bus_to_virt
599
						  (le32_to_cpu
L
Linus Torvalds 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
						   (fh->jpg_buffers.
						      buffer[i].
						      frag_tab[2 * j])));
				fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
				    0;
				fh->jpg_buffers.buffer[i].frag_tab[2 * j +
								   1] = 0;
			}
		}

		free_page((unsigned long) fh->jpg_buffers.buffer[i].
			  frag_tab);
		fh->jpg_buffers.buffer[i].frag_tab = NULL;
	}

	fh->jpg_buffers.allocated = 0;
	fh->jpg_buffers.ready_to_be_freed = 0;
}

/*
 *   V4L Buffer grabbing
 */

static int
zoran_v4l_set_format (struct file               *file,
		      int                        width,
		      int                        height,
		      const struct zoran_format *format)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int bpp;

	/* Check size and format of the grab wanted */

	if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
	    height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_set_format() - wrong frame size (%dx%d)\n",
			ZR_DEVNAME(zr), width, height);
		return -EINVAL;
	}

	bpp = (format->depth + 7) / 8;

	/* Check against available buffer size */
	if (height * width * bpp > fh->v4l_buffers.buffer_size) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
			ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
		return -EINVAL;
	}

	/* The video front end needs 4-byte alinged line sizes */

	if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_set_format() - wrong frame alingment\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	fh->v4l_settings.width = width;
	fh->v4l_settings.height = height;
	fh->v4l_settings.format = format;
	fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;

	return 0;
}

static int
zoran_v4l_queue_frame (struct file *file,
		       int          num)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	unsigned long flags;
	int res = 0;

	if (!fh->v4l_buffers.allocated) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_queue_frame() - buffers not yet allocated\n",
			ZR_DEVNAME(zr));
		res = -ENOMEM;
	}

	/* No grabbing outside the buffer range! */
	if (num >= fh->v4l_buffers.num_buffers || num < 0) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_queue_frame() - buffer %d is out of range\n",
			ZR_DEVNAME(zr), num);
		res = -EINVAL;
	}

	spin_lock_irqsave(&zr->spinlock, flags);

	if (fh->v4l_buffers.active == ZORAN_FREE) {
		if (zr->v4l_buffers.active == ZORAN_FREE) {
			zr->v4l_buffers = fh->v4l_buffers;
			fh->v4l_buffers.active = ZORAN_ACTIVE;
		} else {
			dprintk(1,
				KERN_ERR
				"%s: v4l_queue_frame() - another session is already capturing\n",
				ZR_DEVNAME(zr));
			res = -EBUSY;
		}
	}

	/* make sure a grab isn't going on currently with this buffer */
	if (!res) {
		switch (zr->v4l_buffers.buffer[num].state) {
		default:
		case BUZ_STATE_PEND:
			if (zr->v4l_buffers.active == ZORAN_FREE) {
				fh->v4l_buffers.active = ZORAN_FREE;
				zr->v4l_buffers.allocated = 0;
			}
			res = -EBUSY;	/* what are you doing? */
			break;
		case BUZ_STATE_DONE:
			dprintk(2,
				KERN_WARNING
				"%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
				ZR_DEVNAME(zr), num);
		case BUZ_STATE_USER:
			/* since there is at least one unused buffer there's room for at least
			 * one more pend[] entry */
			zr->v4l_pend[zr->v4l_pend_head++ &
					V4L_MASK_FRAME] = num;
			zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
			zr->v4l_buffers.buffer[num].bs.length =
			    fh->v4l_settings.bytesperline *
			    zr->v4l_settings.height;
			fh->v4l_buffers.buffer[num] =
			    zr->v4l_buffers.buffer[num];
			break;
		}
	}

	spin_unlock_irqrestore(&zr->spinlock, flags);

	if (!res && zr->v4l_buffers.active == ZORAN_FREE)
		zr->v4l_buffers.active = fh->v4l_buffers.active;

	return res;
}

static int
v4l_grab (struct file       *file,
	  struct video_mmap *mp)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int res = 0, i;

761
	for (i = 0; i < NUM_FORMATS; i++) {
L
Linus Torvalds 已提交
762 763 764 765 766
		if (zoran_formats[i].palette == mp->format &&
		    zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
		    !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
			break;
	}
767
	if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
L
Linus Torvalds 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
		dprintk(1,
			KERN_ERR
			"%s: v4l_grab() - wrong bytes-per-pixel format\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	/*
	 * To minimize the time spent in the IRQ routine, we avoid setting up
	 * the video front end there.
	 * If this grab has different parameters from a running streaming capture
	 * we stop the streaming capture and start it over again.
	 */
	if (zr->v4l_memgrab_active &&
	    (zr->v4l_settings.width != mp->width ||
	     zr->v4l_settings.height != mp->height ||
	     zr->v4l_settings.format->palette != mp->format)) {
		res = wait_grab_pending(zr);
		if (res)
			return res;
	}
	if ((res = zoran_v4l_set_format(file,
					mp->width,
					mp->height,
					&zoran_formats[i])))
		return res;
	zr->v4l_settings = fh->v4l_settings;

	/* queue the frame in the pending queue */
	if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
		fh->v4l_buffers.active = ZORAN_FREE;
		return res;
	}

	/* put the 36057 into frame grabbing mode */
	if (!res && !zr->v4l_memgrab_active)
		zr36057_set_memgrab(zr, 1);

	//dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));

	return res;
}

/*
 * Sync on a V4L buffer
 */

static int
v4l_sync (struct file *file,
	  int          frame)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	unsigned long flags;

	if (fh->v4l_buffers.active == ZORAN_FREE) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_sync() - no grab active for this session\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	/* check passed-in frame number */
	if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
		dprintk(1,
			KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
			ZR_DEVNAME(zr), frame);
		return -EINVAL;
	}

	/* Check if is buffer was queued at all */
	if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
		dprintk(1,
			KERN_ERR
			"%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
			ZR_DEVNAME(zr));
		return -EPROTO;
	}

	/* wait on this buffer to get ready */
	if (!wait_event_interruptible_timeout(zr->v4l_capq,
				(zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
				10*HZ))
		return -ETIME;
	if (signal_pending(current))
		return -ERESTARTSYS;

	/* buffer should now be in BUZ_STATE_DONE */
	if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
		dprintk(2,
			KERN_ERR "%s: v4l_sync() - internal state error\n",
			ZR_DEVNAME(zr));

	zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
	fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];

	spin_lock_irqsave(&zr->spinlock, flags);

	/* Check if streaming capture has finished */
	if (zr->v4l_pend_tail == zr->v4l_pend_head) {
		zr36057_set_memgrab(zr, 0);
		if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
			fh->v4l_buffers.active = zr->v4l_buffers.active =
			    ZORAN_FREE;
			zr->v4l_buffers.allocated = 0;
		}
	}

	spin_unlock_irqrestore(&zr->spinlock, flags);

	return 0;
}

/*
 *   Queue a MJPEG buffer for capture/playback
 */

static int
zoran_jpg_queue_frame (struct file          *file,
		       int                   num,
		       enum zoran_codec_mode mode)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	unsigned long flags;
	int res = 0;

	/* Check if buffers are allocated */
	if (!fh->jpg_buffers.allocated) {
		dprintk(1,
			KERN_ERR
			"%s: jpg_queue_frame() - buffers not yet allocated\n",
			ZR_DEVNAME(zr));
		return -ENOMEM;
	}

	/* No grabbing outside the buffer range! */
	if (num >= fh->jpg_buffers.num_buffers || num < 0) {
		dprintk(1,
			KERN_ERR
			"%s: jpg_queue_frame() - buffer %d out of range\n",
			ZR_DEVNAME(zr), num);
		return -EINVAL;
	}

	/* what is the codec mode right now? */
	if (zr->codec_mode == BUZ_MODE_IDLE) {
		zr->jpg_settings = fh->jpg_settings;
	} else if (zr->codec_mode != mode) {
		/* wrong codec mode active - invalid */
		dprintk(1,
			KERN_ERR
			"%s: jpg_queue_frame() - codec in wrong mode\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	if (fh->jpg_buffers.active == ZORAN_FREE) {
		if (zr->jpg_buffers.active == ZORAN_FREE) {
			zr->jpg_buffers = fh->jpg_buffers;
			fh->jpg_buffers.active = ZORAN_ACTIVE;
		} else {
			dprintk(1,
				KERN_ERR
				"%s: jpg_queue_frame() - another session is already capturing\n",
				ZR_DEVNAME(zr));
			res = -EBUSY;
		}
	}

	if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
		/* Ok load up the jpeg codec */
		zr36057_enable_jpg(zr, mode);
	}

944 945
	spin_lock_irqsave(&zr->spinlock, flags);

L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	if (!res) {
		switch (zr->jpg_buffers.buffer[num].state) {
		case BUZ_STATE_DONE:
			dprintk(2,
				KERN_WARNING
				"%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
				ZR_DEVNAME(zr));
		case BUZ_STATE_USER:
			/* since there is at least one unused buffer there's room for at
			 *least one more pend[] entry */
			zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
			    num;
			zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
			fh->jpg_buffers.buffer[num] =
			    zr->jpg_buffers.buffer[num];
			zoran_feed_stat_com(zr);
			break;
		default:
		case BUZ_STATE_DMA:
		case BUZ_STATE_PEND:
			if (zr->jpg_buffers.active == ZORAN_FREE) {
				fh->jpg_buffers.active = ZORAN_FREE;
				zr->jpg_buffers.allocated = 0;
			}
			res = -EBUSY;	/* what are you doing? */
			break;
		}
	}

	spin_unlock_irqrestore(&zr->spinlock, flags);

	if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
		zr->jpg_buffers.active = fh->jpg_buffers.active;
	}

	return res;
}

static int
jpg_qbuf (struct file          *file,
	  int                   frame,
	  enum zoran_codec_mode mode)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int res = 0;

	/* Does the user want to stop streaming? */
	if (frame < 0) {
		if (zr->codec_mode == mode) {
			if (fh->jpg_buffers.active == ZORAN_FREE) {
				dprintk(1,
					KERN_ERR
					"%s: jpg_qbuf(-1) - session not active\n",
					ZR_DEVNAME(zr));
				return -EINVAL;
			}
			fh->jpg_buffers.active = zr->jpg_buffers.active =
			    ZORAN_FREE;
			zr->jpg_buffers.allocated = 0;
			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
			return 0;
		} else {
			dprintk(1,
				KERN_ERR
				"%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}
	}

	if ((res = zoran_jpg_queue_frame(file, frame, mode)))
		return res;

	/* Start the jpeg codec when the first frame is queued  */
	if (!res && zr->jpg_que_head == 1)
		jpeg_start(zr);

	return res;
}

/*
 *   Sync on a MJPEG buffer
 */

static int
jpg_sync (struct file       *file,
	  struct zoran_sync *bs)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	unsigned long flags;
	int frame;

	if (fh->jpg_buffers.active == ZORAN_FREE) {
		dprintk(1,
			KERN_ERR
			"%s: jpg_sync() - capture is not currently active\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}
	if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
	    zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
		dprintk(1,
			KERN_ERR
			"%s: jpg_sync() - codec not in streaming mode\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}
	if (!wait_event_interruptible_timeout(zr->jpg_capq,
			(zr->jpg_que_tail != zr->jpg_dma_tail ||
			 zr->jpg_dma_tail == zr->jpg_dma_head),
			10*HZ)) {
		int isr;

		btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
		udelay(1);
		zr->codec->control(zr->codec, CODEC_G_STATUS,
					   sizeof(isr), &isr);
		dprintk(1,
			KERN_ERR
			"%s: jpg_sync() - timeout: codec isr=0x%02x\n",
			ZR_DEVNAME(zr), isr);

		return -ETIME;

	}
	if (signal_pending(current))
		return -ERESTARTSYS;

	spin_lock_irqsave(&zr->spinlock, flags);

	if (zr->jpg_dma_tail != zr->jpg_dma_head)
		frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
	else
		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];

	/* buffer should now be in BUZ_STATE_DONE */
1084 1085 1086 1087
	if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
		dprintk(2,
			KERN_ERR "%s: jpg_sync() - internal state error\n",
			ZR_DEVNAME(zr));
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

	*bs = zr->jpg_buffers.buffer[frame].bs;
	bs->frame = frame;
	zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
	fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];

	spin_unlock_irqrestore(&zr->spinlock, flags);

	return 0;
}

static void
zoran_open_init_session (struct file *file)
{
	int i;
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

	/* Per default, map the V4L Buffers */
	fh->map_mode = ZORAN_MAP_MODE_RAW;

	/* take over the card's current settings */
	fh->overlay_settings = zr->overlay_settings;
	fh->overlay_settings.is_set = 0;
	fh->overlay_settings.format = zr->overlay_settings.format;
	fh->overlay_active = ZORAN_FREE;

	/* v4l settings */
	fh->v4l_settings = zr->v4l_settings;

	/* v4l_buffers */
	memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
		fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
		fh->v4l_buffers.buffer[i].bs.frame = i;
	}
	fh->v4l_buffers.allocated = 0;
	fh->v4l_buffers.ready_to_be_freed = 0;
	fh->v4l_buffers.active = ZORAN_FREE;
	fh->v4l_buffers.buffer_size = v4l_bufsize;
	fh->v4l_buffers.num_buffers = v4l_nbufs;

	/* jpg settings */
	fh->jpg_settings = zr->jpg_settings;

	/* jpg_buffers */
	memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
	for (i = 0; i < BUZ_MAX_FRAME; i++) {
		fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
		fh->jpg_buffers.buffer[i].bs.frame = i;
	}
	fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
	fh->jpg_buffers.allocated = 0;
	fh->jpg_buffers.ready_to_be_freed = 0;
	fh->jpg_buffers.active = ZORAN_FREE;
	fh->jpg_buffers.buffer_size = jpg_bufsize;
	fh->jpg_buffers.num_buffers = jpg_nbufs;
}

static void
zoran_close_end_session (struct file *file)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

	/* overlay */
	if (fh->overlay_active != ZORAN_FREE) {
		fh->overlay_active = zr->overlay_active = ZORAN_FREE;
		zr->v4l_overlay_active = 0;
		if (!zr->v4l_memgrab_active)
			zr36057_overlay(zr, 0);
		zr->overlay_mask = NULL;
	}

	/* v4l capture */
	if (fh->v4l_buffers.active != ZORAN_FREE) {
1164
		unsigned long flags;
1165 1166

		spin_lock_irqsave(&zr->spinlock, flags);
L
Linus Torvalds 已提交
1167 1168 1169 1170
		zr36057_set_memgrab(zr, 0);
		zr->v4l_buffers.allocated = 0;
		zr->v4l_buffers.active = fh->v4l_buffers.active =
		    ZORAN_FREE;
1171
		spin_unlock_irqrestore(&zr->spinlock, flags);
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	}

	/* v4l buffers */
	if (fh->v4l_buffers.allocated ||
	    fh->v4l_buffers.ready_to_be_freed) {
		v4l_fbuffer_free(file);
	}

	/* jpg capture */
	if (fh->jpg_buffers.active != ZORAN_FREE) {
		zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
		zr->jpg_buffers.allocated = 0;
		zr->jpg_buffers.active = fh->jpg_buffers.active =
		    ZORAN_FREE;
	}

	/* jpg buffers */
	if (fh->jpg_buffers.allocated ||
	    fh->jpg_buffers.ready_to_be_freed) {
		jpg_fbuffer_free(file);
	}
}

/*
 *   Open a zoran card. Right now the flags stuff is just playing
 */

1199
static int zoran_open(struct file *file)
L
Linus Torvalds 已提交
1200
{
1201
	struct zoran *zr = video_drvdata(file);
L
Linus Torvalds 已提交
1202
	struct zoran_fh *fh;
1203 1204 1205 1206
	int res, first_open = 0;

	dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
		ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
L
Linus Torvalds 已提交
1207

1208
	lock_kernel();
L
Linus Torvalds 已提交
1209 1210 1211

	/* see fs/device.c - the kernel already locks during open(),
	 * so locking ourselves only causes deadlocks */
I
Ingo Molnar 已提交
1212
	/*mutex_lock(&zr->resource_lock);*/
L
Linus Torvalds 已提交
1213

1214 1215 1216 1217 1218 1219 1220
	if (zr->user >= 2048) {
		dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
			ZR_DEVNAME(zr), zr->user);
		res = -EBUSY;
		goto fail_unlock;
	}

L
Linus Torvalds 已提交
1221 1222 1223 1224 1225
	if (!zr->decoder) {
		dprintk(1,
			KERN_ERR "%s: no TV decoder loaded for device!\n",
			ZR_DEVNAME(zr));
		res = -EIO;
1226
		goto fail_unlock;
L
Linus Torvalds 已提交
1227 1228
	}

1229
	if (!try_module_get(zr->decoder->driver->driver.owner)) {
L
Linus Torvalds 已提交
1230 1231
		dprintk(1,
			KERN_ERR
1232
			"%s: failed to grab ownership of video decoder\n",
L
Linus Torvalds 已提交
1233 1234
			ZR_DEVNAME(zr));
		res = -EIO;
1235
		goto fail_unlock;
L
Linus Torvalds 已提交
1236 1237
	}
	if (zr->encoder &&
1238
	    !try_module_get(zr->encoder->driver->driver.owner)) {
L
Linus Torvalds 已提交
1239 1240
		dprintk(1,
			KERN_ERR
1241
			"%s: failed to grab ownership of video encoder\n",
L
Linus Torvalds 已提交
1242 1243
			ZR_DEVNAME(zr));
		res = -EIO;
1244
		goto fail_decoder;
L
Linus Torvalds 已提交
1245 1246 1247
	}

	/* now, create the open()-specific file_ops struct */
P
 
Panagiotis Issaris 已提交
1248
	fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253 1254
	if (!fh) {
		dprintk(1,
			KERN_ERR
			"%s: zoran_open() - allocation of zoran_fh failed\n",
			ZR_DEVNAME(zr));
		res = -ENOMEM;
1255
		goto fail_encoder;
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	}
	/* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
	 * on norm-change! */
	fh->overlay_mask =
	    kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
	if (!fh->overlay_mask) {
		dprintk(1,
			KERN_ERR
			"%s: zoran_open() - allocation of overlay_mask failed\n",
			ZR_DEVNAME(zr));
		res = -ENOMEM;
1267
		goto fail_fh;
L
Linus Torvalds 已提交
1268 1269 1270 1271 1272
	}

	if (zr->user++ == 0)
		first_open = 1;

I
Ingo Molnar 已提交
1273
	/*mutex_unlock(&zr->resource_lock);*/
L
Linus Torvalds 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

	/* default setup - TODO: look at flags */
	if (first_open) {	/* First device open */
		zr36057_restart(zr);
		zoran_open_init_params(zr);
		zoran_init_hardware(zr);

		btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
	}

	/* set file_ops stuff */
	file->private_data = fh;
	fh->zr = zr;
	zoran_open_init_session(file);
1288
	unlock_kernel();
L
Linus Torvalds 已提交
1289 1290 1291

	return 0;

1292 1293 1294 1295 1296 1297 1298 1299
fail_fh:
	kfree(fh);
fail_encoder:
	if (zr->encoder)
		module_put(zr->encoder->driver->driver.owner);
fail_decoder:
	module_put(zr->decoder->driver->driver.owner);
fail_unlock:
1300
	unlock_kernel();
L
Linus Torvalds 已提交
1301

1302 1303 1304
	dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
		ZR_DEVNAME(zr), res, zr->user);

L
Linus Torvalds 已提交
1305 1306 1307 1308
	return res;
}

static int
1309
zoran_close(struct file  *file)
L
Linus Torvalds 已提交
1310 1311 1312 1313
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

1314 1315
	dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
		ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
L
Linus Torvalds 已提交
1316 1317 1318

	/* kernel locks (fs/device.c), so don't do that ourselves
	 * (prevents deadlocks) */
I
Ingo Molnar 已提交
1319
	/*mutex_lock(&zr->resource_lock);*/
L
Linus Torvalds 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332

	zoran_close_end_session(file);

	if (zr->user-- == 1) {	/* Last process */
		/* Clean up JPEG process */
		wake_up_interruptible(&zr->jpg_capq);
		zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
		zr->jpg_buffers.allocated = 0;
		zr->jpg_buffers.active = ZORAN_FREE;

		/* disable interrupts */
		btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);

1333
		if (zr36067_debug > 1)
L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
			print_interrupts(zr);

		/* Overlay off */
		zr->v4l_overlay_active = 0;
		zr36057_overlay(zr, 0);
		zr->overlay_mask = NULL;

		/* capture off */
		wake_up_interruptible(&zr->v4l_capq);
		zr36057_set_memgrab(zr, 0);
		zr->v4l_buffers.allocated = 0;
		zr->v4l_buffers.active = ZORAN_FREE;
		zoran_set_pci_master(zr, 0);

		if (!pass_through) {	/* Switch to color bar */
			int zero = 0, two = 2;
			decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
			encoder_command(zr, ENCODER_SET_INPUT, &two);
		}
	}

	file->private_data = NULL;
	kfree(fh->overlay_mask);
	kfree(fh);

	/* release locks on the i2c modules */
1360
	module_put(zr->decoder->driver->driver.owner);
1361 1362
	if (zr->encoder)
		module_put(zr->encoder->driver->driver.owner);
L
Linus Torvalds 已提交
1363

I
Ingo Molnar 已提交
1364
	/*mutex_unlock(&zr->resource_lock);*/
L
Linus Torvalds 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408

	dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));

	return 0;
}


static ssize_t
zoran_read (struct file *file,
	    char        __user *data,
	    size_t       count,
	    loff_t      *ppos)
{
	/* we simply don't support read() (yet)... */

	return -EINVAL;
}

static ssize_t
zoran_write (struct file *file,
	     const char  __user *data,
	     size_t       count,
	     loff_t      *ppos)
{
	/* ...and the same goes for write() */

	return -EINVAL;
}

static int
setup_fbuffer (struct file               *file,
	       void                      *base,
	       const struct zoran_format *fmt,
	       int                        width,
	       int                        height,
	       int                        bytesperline)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

	/* (Ronald) v4l/v4l2 guidelines */
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
		return -EPERM;

1409 1410 1411 1412 1413 1414 1415
	/* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
	   ALi Magik (that needs very low latency while the card needs a
	   higher value always) */

	if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
		return -ENXIO;

L
Linus Torvalds 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
	/* we need a bytesperline value, even if not given */
	if (!bytesperline)
		bytesperline = width * ((fmt->depth + 7) & ~7) / 8;

#if 0
	if (zr->overlay_active) {
		/* dzjee... stupid users... don't even bother to turn off
		 * overlay before changing the memory location...
		 * normally, we would return errors here. However, one of
		 * the tools that does this is... xawtv! and since xawtv
		 * is used by +/- 99% of the users, we'd rather be user-
		 * friendly and silently do as if nothing went wrong */
		dprintk(3,
			KERN_ERR
			"%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
			ZR_DEVNAME(zr));
		zr36057_overlay(zr, 0);
	}
#endif

	if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
		dprintk(1,
			KERN_ERR
			"%s: setup_fbuffer() - no valid overlay format given\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}
	if (height <= 0 || width <= 0 || bytesperline <= 0) {
		dprintk(1,
			KERN_ERR
			"%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
			ZR_DEVNAME(zr), width, height, bytesperline);
		return -EINVAL;
	}
	if (bytesperline & 3) {
		dprintk(1,
			KERN_ERR
			"%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
			ZR_DEVNAME(zr), bytesperline);
		return -EINVAL;
	}

	zr->buffer.base = (void *) ((unsigned long) base & ~3);
	zr->buffer.height = height;
	zr->buffer.width = width;
	zr->buffer.depth = fmt->depth;
	zr->overlay_settings.format = fmt;
	zr->buffer.bytesperline = bytesperline;

	/* The user should set new window parameters */
	zr->overlay_settings.is_set = 0;

	return 0;
}


static int
setup_window (struct file       *file,
	      int                x,
	      int                y,
	      int                width,
	      int                height,
	      struct video_clip __user *clips,
	      int                clipcount,
	      void              __user *bitmap)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	struct video_clip *vcp = NULL;
	int on, end;


	if (!zr->buffer.base) {
		dprintk(1,
			KERN_ERR
			"%s: setup_window() - frame buffer has to be set first\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	if (!fh->overlay_settings.format) {
		dprintk(1,
			KERN_ERR
			"%s: setup_window() - no overlay format set\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	/*
	 * The video front end needs 4-byte alinged line sizes, we correct that
	 * silently here if necessary
	 */
	if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
		end = (x + width) & ~1;	/* round down */
		x = (x + 1) & ~1;	/* round up */
		width = end - x;
	}

	if (zr->buffer.depth == 24) {
		end = (x + width) & ~3;	/* round down */
		x = (x + 3) & ~3;	/* round up */
		width = end - x;
	}

	if (width > BUZ_MAX_WIDTH)
		width = BUZ_MAX_WIDTH;
	if (height > BUZ_MAX_HEIGHT)
		height = BUZ_MAX_HEIGHT;

	/* Check for vaild parameters */
	if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
	    width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
		dprintk(1,
			KERN_ERR
			"%s: setup_window() - width = %d or height = %d invalid\n",
			ZR_DEVNAME(zr), width, height);
		return -EINVAL;
	}

	fh->overlay_settings.x = x;
	fh->overlay_settings.y = y;
	fh->overlay_settings.width = width;
	fh->overlay_settings.height = height;
	fh->overlay_settings.clipcount = clipcount;

	/*
	 * If an overlay is running, we have to switch it off
	 * and switch it on again in order to get the new settings in effect.
	 *
	 * We also want to avoid that the overlay mask is written
	 * when an overlay is running.
	 */

	on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
	    zr->overlay_active != ZORAN_FREE &&
	    fh->overlay_active != ZORAN_FREE;
	if (on)
		zr36057_overlay(zr, 0);

	/*
	 *   Write the overlay mask if clips are wanted.
	 *   We prefer a bitmap.
	 */
	if (bitmap) {
		/* fake value - it just means we want clips */
		fh->overlay_settings.clipcount = 1;

		if (copy_from_user(fh->overlay_mask, bitmap,
				   (width * height + 7) / 8)) {
			return -EFAULT;
		}
	} else if (clipcount > 0) {
		/* write our own bitmap from the clips */
		vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
		if (vcp == NULL) {
			dprintk(1,
				KERN_ERR
				"%s: setup_window() - Alloc of clip mask failed\n",
				ZR_DEVNAME(zr));
			return -ENOMEM;
		}
		if (copy_from_user
		    (vcp, clips, sizeof(struct video_clip) * clipcount)) {
			vfree(vcp);
			return -EFAULT;
		}
		write_overlay_mask(file, vcp, clipcount);
		vfree(vcp);
	}

	fh->overlay_settings.is_set = 1;
	if (fh->overlay_active != ZORAN_FREE &&
	    zr->overlay_active != ZORAN_FREE)
		zr->overlay_settings = fh->overlay_settings;

	if (on)
		zr36057_overlay(zr, 1);

	/* Make sure the changes come into effect */
	return wait_grab_pending(zr);
}

static int
setup_overlay (struct file *file,
	       int          on)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

	/* If there is nothing to do, return immediatly */
	if ((on && fh->overlay_active != ZORAN_FREE) ||
	    (!on && fh->overlay_active == ZORAN_FREE))
		return 0;

	/* check whether we're touching someone else's overlay */
	if (on && zr->overlay_active != ZORAN_FREE &&
	    fh->overlay_active == ZORAN_FREE) {
		dprintk(1,
			KERN_ERR
			"%s: setup_overlay() - overlay is already active for another session\n",
			ZR_DEVNAME(zr));
		return -EBUSY;
	}
	if (!on && zr->overlay_active != ZORAN_FREE &&
	    fh->overlay_active == ZORAN_FREE) {
		dprintk(1,
			KERN_ERR
			"%s: setup_overlay() - you cannot cancel someone else's session\n",
			ZR_DEVNAME(zr));
		return -EPERM;
	}

	if (on == 0) {
		zr->overlay_active = fh->overlay_active = ZORAN_FREE;
		zr->v4l_overlay_active = 0;
		/* When a grab is running, the video simply
		 * won't be switched on any more */
		if (!zr->v4l_memgrab_active)
			zr36057_overlay(zr, 0);
		zr->overlay_mask = NULL;
	} else {
		if (!zr->buffer.base || !fh->overlay_settings.is_set) {
			dprintk(1,
				KERN_ERR
				"%s: setup_overlay() - buffer or window not set\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}
		if (!fh->overlay_settings.format) {
			dprintk(1,
				KERN_ERR
				"%s: setup_overlay() - no overlay format set\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}
		zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
		zr->v4l_overlay_active = 1;
		zr->overlay_mask = fh->overlay_mask;
		zr->overlay_settings = fh->overlay_settings;
		if (!zr->v4l_memgrab_active)
			zr36057_overlay(zr, 1);
		/* When a grab is running, the video will be
		 * switched on when grab is finished */
	}

	/* Make sure the changes come into effect */
	return wait_grab_pending(zr);
}

	/* get the status of a buffer in the clients buffer queue */
static int
zoran_v4l2_buffer_status (struct file        *file,
			  struct v4l2_buffer *buf,
			  int                 num)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

	buf->flags = V4L2_BUF_FLAG_MAPPED;

	switch (fh->map_mode) {
	case ZORAN_MAP_MODE_RAW:

		/* check range */
		if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
		    !fh->v4l_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}

		buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf->length = fh->v4l_buffers.buffer_size;

		/* get buffer */
		buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
		if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
		    fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
			buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
			buf->flags |= V4L2_BUF_FLAG_DONE;
			buf->timestamp =
			    fh->v4l_buffers.buffer[num].bs.timestamp;
		} else {
			buf->flags |= V4L2_BUF_FLAG_QUEUED;
		}

		if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
			buf->field = V4L2_FIELD_TOP;
		else
			buf->field = V4L2_FIELD_INTERLACED;

		break;

	case ZORAN_MAP_MODE_JPG_REC:
	case ZORAN_MAP_MODE_JPG_PLAY:

		/* check range */
		if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
		    !fh->jpg_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}

		buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
			      V4L2_BUF_TYPE_VIDEO_CAPTURE :
			      V4L2_BUF_TYPE_VIDEO_OUTPUT;
		buf->length = fh->jpg_buffers.buffer_size;

		/* these variables are only written after frame has been captured */
		if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
		    fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
			buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
			buf->timestamp =
			    fh->jpg_buffers.buffer[num].bs.timestamp;
			buf->bytesused =
			    fh->jpg_buffers.buffer[num].bs.length;
			buf->flags |= V4L2_BUF_FLAG_DONE;
		} else {
			buf->flags |= V4L2_BUF_FLAG_QUEUED;
		}

		/* which fields are these? */
		if (fh->jpg_settings.TmpDcm != 1)
			buf->field =
			    fh->jpg_settings.
			    odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
		else
			buf->field =
			    fh->jpg_settings.
			    odd_even ? V4L2_FIELD_SEQ_TB :
			    V4L2_FIELD_SEQ_BT;

		break;

	default:

		dprintk(5,
			KERN_ERR
			"%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
			ZR_DEVNAME(zr), buf->type, fh->map_mode);
		return -EINVAL;
	}

	buf->memory = V4L2_MEMORY_MMAP;
	buf->index = num;
	buf->m.offset = buf->length * num;

	return 0;
}

static int
zoran_set_norm (struct zoran *zr,
1773
		int           norm) /* VIDEO_MODE_* */
L
Linus Torvalds 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
{
	int norm_encoder, on;

	if (zr->v4l_buffers.active != ZORAN_FREE ||
	    zr->jpg_buffers.active != ZORAN_FREE) {
		dprintk(1,
			KERN_WARNING
			"%s: set_norm() called while in playback/capture mode\n",
			ZR_DEVNAME(zr));
		return -EBUSY;
	}

	if (lock_norm && norm != zr->norm) {
		if (lock_norm > 1) {
			dprintk(1,
				KERN_WARNING
				"%s: set_norm() - TV standard is locked, can not switch norm\n",
				ZR_DEVNAME(zr));
			return -EPERM;
		} else {
			dprintk(1,
				KERN_WARNING
				"%s: set_norm() - TV standard is locked, norm was not changed\n",
				ZR_DEVNAME(zr));
			norm = zr->norm;
		}
	}

	if (norm != VIDEO_MODE_AUTO &&
	    (norm < 0 || norm >= zr->card.norms ||
	     !zr->card.tvn[norm])) {
		dprintk(1,
			KERN_ERR "%s: set_norm() - unsupported norm %d\n",
			ZR_DEVNAME(zr), norm);
		return -EINVAL;
	}

	if (norm == VIDEO_MODE_AUTO) {
		int status;

		/* if we have autodetect, ... */
		struct video_decoder_capability caps;
		decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
		if (!(caps.flags & VIDEO_DECODER_AUTO)) {
			dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}

		decoder_command(zr, DECODER_SET_NORM, &norm);

		/* let changes come into effect */
		ssleep(2);

		decoder_command(zr, DECODER_GET_STATUS, &status);
		if (!(status & DECODER_STATUS_GOOD)) {
			dprintk(1,
				KERN_ERR
				"%s: set_norm() - no norm detected\n",
				ZR_DEVNAME(zr));
			/* reset norm */
			decoder_command(zr, DECODER_SET_NORM, &zr->norm);
			return -EIO;
		}

		if (status & DECODER_STATUS_NTSC)
			norm = VIDEO_MODE_NTSC;
		else if (status & DECODER_STATUS_SECAM)
			norm = VIDEO_MODE_SECAM;
		else
			norm = VIDEO_MODE_PAL;
	}
	zr->timing = zr->card.tvn[norm];
	norm_encoder = norm;

	/* We switch overlay off and on since a change in the
	 * norm needs different VFE settings */
	on = zr->overlay_active && !zr->v4l_memgrab_active;
	if (on)
		zr36057_overlay(zr, 0);

	decoder_command(zr, DECODER_SET_NORM, &norm);
	encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);

	if (on)
		zr36057_overlay(zr, 1);

	/* Make sure the changes come into effect */
	zr->norm = norm;

	return 0;
}

static int
zoran_set_input (struct zoran *zr,
		 int           input)
{
	int realinput;

	if (input == zr->input) {
		return 0;
	}

	if (zr->v4l_buffers.active != ZORAN_FREE ||
	    zr->jpg_buffers.active != ZORAN_FREE) {
		dprintk(1,
			KERN_WARNING
			"%s: set_input() called while in playback/capture mode\n",
			ZR_DEVNAME(zr));
		return -EBUSY;
	}

	if (input < 0 || input >= zr->card.inputs) {
		dprintk(1,
			KERN_ERR
			"%s: set_input() - unnsupported input %d\n",
			ZR_DEVNAME(zr), input);
		return -EINVAL;
	}

	realinput = zr->card.input[input].muxsel;
	zr->input = input;

	decoder_command(zr, DECODER_SET_INPUT, &realinput);

	return 0;
}

/*
 *   ioctl routine
 */

1906
static long zoran_do_ioctl(struct file *file, unsigned int cmd, void *arg)
L
Linus Torvalds 已提交
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	/* CAREFUL: used in multiple places here */
	struct zoran_jpg_settings settings;

	/* we might have older buffers lying around... We don't want
	 * to wait, but we do want to try cleaning them up ASAP. So
	 * we try to obtain the lock and free them. If that fails, we
	 * don't do anything and wait for the next turn. In the end,
	 * zoran_close() or a new allocation will still free them...
	 * This is just a 'the sooner the better' extra 'feature'
	 *
	 * We don't free the buffers right on munmap() because that
	 * causes oopses (kfree() inside munmap() oopses for no
	 * apparent reason - it's also not reproduceable in any way,
	 * but moving the free code outside the munmap() handler fixes
	 * all this... If someone knows why, please explain me (Ronald)
	 */
J
Jean Delvare 已提交
1926
	if (mutex_trylock(&zr->resource_lock)) {
L
Linus Torvalds 已提交
1927 1928 1929 1930 1931 1932
		/* we obtained it! Let's try to free some things */
		if (fh->jpg_buffers.ready_to_be_freed)
			jpg_fbuffer_free(file);
		if (fh->v4l_buffers.ready_to_be_freed)
			v4l_fbuffer_free(file);

I
Ingo Molnar 已提交
1933
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
	}

	switch (cmd) {

	case VIDIOCGCAP:
	{
		struct video_capability *vcap = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));

		memset(vcap, 0, sizeof(struct video_capability));
1945
		strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
L
Linus Torvalds 已提交
1946 1947 1948 1949
		vcap->type = ZORAN_VID_TYPE;

		vcap->channels = zr->card.inputs;
		vcap->audios = 0;
I
Ingo Molnar 已提交
1950
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1951 1952 1953 1954
		vcap->maxwidth = BUZ_MAX_WIDTH;
		vcap->maxheight = BUZ_MAX_HEIGHT;
		vcap->minwidth = BUZ_MIN_WIDTH;
		vcap->minheight = BUZ_MIN_HEIGHT;
I
Ingo Molnar 已提交
1955
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

		return 0;
	}
		break;

	case VIDIOCGCHAN:
	{
		struct video_channel *vchan = arg;
		int channel = vchan->channel;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
			ZR_DEVNAME(zr), vchan->channel);

		memset(vchan, 0, sizeof(struct video_channel));
		if (channel > zr->card.inputs || channel < 0) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOCGCHAN on not existing channel %d\n",
				ZR_DEVNAME(zr), channel);
			return -EINVAL;
		}

		strcpy(vchan->name, zr->card.input[channel].name);

		vchan->tuners = 0;
		vchan->flags = 0;
		vchan->type = VIDEO_TYPE_CAMERA;
I
Ingo Molnar 已提交
1983
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1984
		vchan->norm = zr->norm;
I
Ingo Molnar 已提交
1985
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1986 1987 1988 1989 1990 1991 1992
		vchan->channel = channel;

		return 0;
	}
		break;

		/* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
1993
		 *
L
Linus Torvalds 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
		 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
		 * *                                 ^^^^^^^
		 * * The famos BTTV driver has it implemented with a struct video_channel argument
		 * * and we follow it for compatibility reasons
		 * *
		 * * BTW: this is the only way the user can set the norm!
		 */

	case VIDIOCSCHAN:
	{
		struct video_channel *vchan = arg;
		int res;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
			ZR_DEVNAME(zr), vchan->channel, vchan->norm);

I
Ingo Molnar 已提交
2012
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2013 2014 2015 2016 2017 2018 2019 2020
		if ((res = zoran_set_input(zr, vchan->channel)))
			goto schan_unlock_and_return;
		if ((res = zoran_set_norm(zr, vchan->norm)))
			goto schan_unlock_and_return;

		/* Make sure the changes come into effect */
		res = wait_grab_pending(zr);
	schan_unlock_and_return:
I
Ingo Molnar 已提交
2021
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
		return res;
	}
		break;

	case VIDIOCGPICT:
	{
		struct video_picture *vpict = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));

		memset(vpict, 0, sizeof(struct video_picture));
I
Ingo Molnar 已提交
2033
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
		vpict->hue = zr->hue;
		vpict->brightness = zr->brightness;
		vpict->contrast = zr->contrast;
		vpict->colour = zr->saturation;
		if (fh->overlay_settings.format) {
			vpict->depth = fh->overlay_settings.format->depth;
			vpict->palette = fh->overlay_settings.format->palette;
		} else {
			vpict->depth = 0;
		}
I
Ingo Molnar 已提交
2044
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

		return 0;
	}
		break;

	case VIDIOCSPICT:
	{
		struct video_picture *vpict = arg;
		int i;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
			ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
			vpict->colour, vpict->contrast, vpict->depth,
			vpict->palette);

2062
		for (i = 0; i < NUM_FORMATS; i++) {
L
Linus Torvalds 已提交
2063 2064 2065 2066 2067 2068 2069 2070
			const struct zoran_format *fmt = &zoran_formats[i];

			if (fmt->palette != -1 &&
			    fmt->flags & ZORAN_FORMAT_OVERLAY &&
			    fmt->palette == vpict->palette &&
			    fmt->depth == vpict->depth)
				break;
		}
2071
		if (i == NUM_FORMATS) {
L
Linus Torvalds 已提交
2072 2073 2074 2075 2076 2077 2078
			dprintk(1,
				KERN_ERR
				"%s: VIDIOCSPICT - Invalid palette %d\n",
				ZR_DEVNAME(zr), vpict->palette);
			return -EINVAL;
		}

I
Ingo Molnar 已提交
2079
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089

		decoder_command(zr, DECODER_SET_PICTURE, vpict);

		zr->hue = vpict->hue;
		zr->contrast = vpict->contrast;
		zr->saturation = vpict->colour;
		zr->brightness = vpict->brightness;

		fh->overlay_settings.format = &zoran_formats[i];

I
Ingo Molnar 已提交
2090
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102

		return 0;
	}
		break;

	case VIDIOCCAPTURE:
	{
		int *on = arg, res;

		dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
			ZR_DEVNAME(zr), *on);

I
Ingo Molnar 已提交
2103
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2104
		res = setup_overlay(file, *on);
I
Ingo Molnar 已提交
2105
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117

		return res;
	}
		break;

	case VIDIOCGWIN:
	{
		struct video_window *vwin = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));

		memset(vwin, 0, sizeof(struct video_window));
I
Ingo Molnar 已提交
2118
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2119 2120 2121 2122
		vwin->x = fh->overlay_settings.x;
		vwin->y = fh->overlay_settings.y;
		vwin->width = fh->overlay_settings.width;
		vwin->height = fh->overlay_settings.height;
I
Ingo Molnar 已提交
2123
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
		vwin->clipcount = 0;
		return 0;
	}
		break;

	case VIDIOCSWIN:
	{
		struct video_window *vwin = arg;
		int res;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
			ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
			vwin->height, vwin->clipcount);

I
Ingo Molnar 已提交
2140
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2141 2142 2143 2144
		res =
		    setup_window(file, vwin->x, vwin->y, vwin->width,
				 vwin->height, vwin->clips,
				 vwin->clipcount, NULL);
I
Ingo Molnar 已提交
2145
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156

		return res;
	}
		break;

	case VIDIOCGFBUF:
	{
		struct video_buffer *vbuf = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));

I
Ingo Molnar 已提交
2157
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2158
		*vbuf = zr->buffer;
I
Ingo Molnar 已提交
2159
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
		return 0;
	}
		break;

	case VIDIOCSFBUF:
	{
		struct video_buffer *vbuf = arg;
		int i, res = 0;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
			ZR_DEVNAME(zr), vbuf->base, vbuf->width,
			vbuf->height, vbuf->depth, vbuf->bytesperline);

2175
		for (i = 0; i < NUM_FORMATS; i++)
L
Linus Torvalds 已提交
2176 2177
			if (zoran_formats[i].depth == vbuf->depth)
				break;
2178
		if (i == NUM_FORMATS) {
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183 2184 2185
			dprintk(1,
				KERN_ERR
				"%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
				ZR_DEVNAME(zr), vbuf->depth);
			return -EINVAL;
		}

I
Ingo Molnar 已提交
2186
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2187 2188 2189 2190
		res =
		    setup_fbuffer(file, vbuf->base, &zoran_formats[i],
				  vbuf->width, vbuf->height,
				  vbuf->bytesperline);
I
Ingo Molnar 已提交
2191
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203

		return res;
	}
		break;

	case VIDIOCSYNC:
	{
		int *frame = arg, res;

		dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
			ZR_DEVNAME(zr), *frame);

I
Ingo Molnar 已提交
2204
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2205
		res = v4l_sync(file, *frame);
I
Ingo Molnar 已提交
2206
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
		if (!res)
			zr->v4l_sync_tail++;
		return res;
	}
		break;

	case VIDIOCMCAPTURE:
	{
		struct video_mmap *vmap = arg;
		int res;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
			ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
			vmap->format);

I
Ingo Molnar 已提交
2224
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2225
		res = v4l_grab(file, vmap);
I
Ingo Molnar 已提交
2226
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
		return res;
	}
		break;

	case VIDIOCGMBUF:
	{
		struct video_mbuf *vmbuf = arg;
		int i, res = 0;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));

		vmbuf->size =
		    fh->v4l_buffers.num_buffers *
		    fh->v4l_buffers.buffer_size;
		vmbuf->frames = fh->v4l_buffers.num_buffers;
		for (i = 0; i < vmbuf->frames; i++) {
			vmbuf->offsets[i] =
			    i * fh->v4l_buffers.buffer_size;
		}

I
Ingo Molnar 已提交
2247
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265

		if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOCGMBUF - buffers already allocated\n",
				ZR_DEVNAME(zr));
			res = -EINVAL;
			goto v4l1reqbuf_unlock_and_return;
		}

		if (v4l_fbuffer_alloc(file)) {
			res = -ENOMEM;
			goto v4l1reqbuf_unlock_and_return;
		}

		/* The next mmap will map the V4L buffers */
		fh->map_mode = ZORAN_MAP_MODE_RAW;
	v4l1reqbuf_unlock_and_return:
I
Ingo Molnar 已提交
2266
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319

		return res;
	}
		break;

	case VIDIOCGUNIT:
	{
		struct video_unit *vunit = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));

		vunit->video = zr->video_dev->minor;
		vunit->vbi = VIDEO_NO_UNIT;
		vunit->radio = VIDEO_NO_UNIT;
		vunit->audio = VIDEO_NO_UNIT;
		vunit->teletext = VIDEO_NO_UNIT;

		return 0;
	}
		break;

		/*
		 * RJ: In principal we could support subcaptures for V4L grabbing.
		 *     Not even the famous BTTV driver has them, however.
		 *     If there should be a strong demand, one could consider
		 *     to implement them.
		 */
	case VIDIOCGCAPTURE:
	{
		dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}
		break;

	case VIDIOCSCAPTURE:
	{
		dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}
		break;

	case BUZIOC_G_PARAMS:
	{
		struct zoran_params *bparams = arg;

		dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));

		memset(bparams, 0, sizeof(struct zoran_params));
		bparams->major_version = MAJOR_VERSION;
		bparams->minor_version = MINOR_VERSION;

I
Ingo Molnar 已提交
2320
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348

		bparams->norm = zr->norm;
		bparams->input = zr->input;

		bparams->decimation = fh->jpg_settings.decimation;
		bparams->HorDcm = fh->jpg_settings.HorDcm;
		bparams->VerDcm = fh->jpg_settings.VerDcm;
		bparams->TmpDcm = fh->jpg_settings.TmpDcm;
		bparams->field_per_buff = fh->jpg_settings.field_per_buff;
		bparams->img_x = fh->jpg_settings.img_x;
		bparams->img_y = fh->jpg_settings.img_y;
		bparams->img_width = fh->jpg_settings.img_width;
		bparams->img_height = fh->jpg_settings.img_height;
		bparams->odd_even = fh->jpg_settings.odd_even;

		bparams->quality = fh->jpg_settings.jpg_comp.quality;
		bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
		bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
		memcpy(bparams->APP_data,
		       fh->jpg_settings.jpg_comp.APP_data,
		       sizeof(bparams->APP_data));
		bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
		memcpy(bparams->COM_data,
		       fh->jpg_settings.jpg_comp.COM_data,
		       sizeof(bparams->COM_data));
		bparams->jpeg_markers =
		    fh->jpg_settings.jpg_comp.jpeg_markers;

I
Ingo Molnar 已提交
2349
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384

		bparams->VFIFO_FB = 0;

		return 0;
	}
		break;

	case BUZIOC_S_PARAMS:
	{
		struct zoran_params *bparams = arg;
		int res = 0;

		dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));

		settings.decimation = bparams->decimation;
		settings.HorDcm = bparams->HorDcm;
		settings.VerDcm = bparams->VerDcm;
		settings.TmpDcm = bparams->TmpDcm;
		settings.field_per_buff = bparams->field_per_buff;
		settings.img_x = bparams->img_x;
		settings.img_y = bparams->img_y;
		settings.img_width = bparams->img_width;
		settings.img_height = bparams->img_height;
		settings.odd_even = bparams->odd_even;

		settings.jpg_comp.quality = bparams->quality;
		settings.jpg_comp.APPn = bparams->APPn;
		settings.jpg_comp.APP_len = bparams->APP_len;
		memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
		       sizeof(bparams->APP_data));
		settings.jpg_comp.COM_len = bparams->COM_len;
		memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
		       sizeof(bparams->COM_data));
		settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;

I
Ingo Molnar 已提交
2385
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404

		if (zr->codec_mode != BUZ_MODE_IDLE) {
			dprintk(1,
				KERN_ERR
				"%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
				ZR_DEVNAME(zr));
			res = -EINVAL;
			goto sparams_unlock_and_return;
		}

		/* Check the params first before overwriting our
		 * nternal values */
		if (zoran_check_jpg_settings(zr, &settings)) {
			res = -EINVAL;
			goto sparams_unlock_and_return;
		}

		fh->jpg_settings = settings;
	sparams_unlock_and_return:
I
Ingo Molnar 已提交
2405
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436

		return res;
	}
		break;

	case BUZIOC_REQBUFS:
	{
		struct zoran_requestbuffers *breq = arg;
		int res = 0;

		dprintk(3,
			KERN_DEBUG
			"%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
			ZR_DEVNAME(zr), breq->count, breq->size);

		/* Enforce reasonable lower and upper limits */
		if (breq->count < 4)
			breq->count = 4;	/* Could be choosen smaller */
		if (breq->count > jpg_nbufs)
			breq->count = jpg_nbufs;
		breq->size = PAGE_ALIGN(breq->size);
		if (breq->size < 8192)
			breq->size = 8192;	/* Arbitrary */
		/* breq->size is limited by 1 page for the stat_com
		 * tables to a Maximum of 2 MB */
		if (breq->size > jpg_bufsize)
			breq->size = jpg_bufsize;
		if (fh->jpg_buffers.need_contiguous &&
		    breq->size > MAX_KMALLOC_MEM)
			breq->size = MAX_KMALLOC_MEM;

I
Ingo Molnar 已提交
2437
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459

		if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: BUZIOC_REQBUFS - buffers allready allocated\n",
				ZR_DEVNAME(zr));
			res = -EBUSY;
			goto jpgreqbuf_unlock_and_return;
		}

		fh->jpg_buffers.num_buffers = breq->count;
		fh->jpg_buffers.buffer_size = breq->size;

		if (jpg_fbuffer_alloc(file)) {
			res = -ENOMEM;
			goto jpgreqbuf_unlock_and_return;
		}

		/* The next mmap will map the MJPEG buffers - could
		 * also be *_PLAY, but it doesn't matter here */
		fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
	jpgreqbuf_unlock_and_return:
I
Ingo Molnar 已提交
2460
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472

		return res;
	}
		break;

	case BUZIOC_QBUF_CAPT:
	{
		int *frame = arg, res;

		dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
			ZR_DEVNAME(zr), *frame);

I
Ingo Molnar 已提交
2473
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2474
		res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
I
Ingo Molnar 已提交
2475
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487

		return res;
	}
		break;

	case BUZIOC_QBUF_PLAY:
	{
		int *frame = arg, res;

		dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
			ZR_DEVNAME(zr), *frame);

I
Ingo Molnar 已提交
2488
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2489
		res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
I
Ingo Molnar 已提交
2490
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502

		return res;
	}
		break;

	case BUZIOC_SYNC:
	{
		struct zoran_sync *bsync = arg;
		int res;

		dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));

I
Ingo Molnar 已提交
2503
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2504
		res = jpg_sync(file, bsync);
I
Ingo Molnar 已提交
2505
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528

		return res;
	}
		break;

	case BUZIOC_G_STATUS:
	{
		struct zoran_status *bstat = arg;
		int norm, input, status, res = 0;

		dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));

		if (zr->codec_mode != BUZ_MODE_IDLE) {
			dprintk(1,
				KERN_ERR
				"%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
				ZR_DEVNAME(zr));
			return -EINVAL;
		}

		input = zr->card.input[bstat->input].muxsel;
		norm = VIDEO_MODE_AUTO;

I
Ingo Molnar 已提交
2529
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553

		if (zr->codec_mode != BUZ_MODE_IDLE) {
			dprintk(1,
				KERN_ERR
				"%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
				ZR_DEVNAME(zr));
			res = -EINVAL;
			goto gstat_unlock_and_return;
		}

		decoder_command(zr, DECODER_SET_INPUT, &input);
		decoder_command(zr, DECODER_SET_NORM, &norm);

		/* sleep 1 second */
		ssleep(1);

		/* Get status of video decoder */
		decoder_command(zr, DECODER_GET_STATUS, &status);

		/* restore previous input and norm */
		input = zr->card.input[zr->input].muxsel;
		decoder_command(zr, DECODER_SET_INPUT, &input);
		decoder_command(zr, DECODER_SET_NORM, &zr->norm);
	gstat_unlock_and_return:
I
Ingo Molnar 已提交
2554
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584

		if (!res) {
			bstat->signal =
			    (status & DECODER_STATUS_GOOD) ? 1 : 0;
			if (status & DECODER_STATUS_NTSC)
				bstat->norm = VIDEO_MODE_NTSC;
			else if (status & DECODER_STATUS_SECAM)
				bstat->norm = VIDEO_MODE_SECAM;
			else
				bstat->norm = VIDEO_MODE_PAL;

			bstat->color =
			    (status & DECODER_STATUS_COLOR) ? 1 : 0;
		}

		return res;
	}
		break;

		/* The new video4linux2 capture interface - much nicer than video4linux1, since
		 * it allows for integrating the JPEG capturing calls inside standard v4l2
		 */

	case VIDIOC_QUERYCAP:
	{
		struct v4l2_capability *cap = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));

		memset(cap, 0, sizeof(*cap));
2585 2586
		strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
		strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
L
Linus Torvalds 已提交
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
		snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
			 pci_name(zr->pci_dev));
		cap->version =
		    KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
				   RELEASE_VERSION);
		cap->capabilities = ZORAN_V4L2_VID_FLAGS;

		return 0;
	}
		break;

	case VIDIOC_ENUM_FMT:
	{
		struct v4l2_fmtdesc *fmt = arg;
		int index = fmt->index, num = -1, i, flag = 0, type =
		    fmt->type;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
			ZR_DEVNAME(zr), fmt->index);

		switch (fmt->type) {
		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
			flag = ZORAN_FORMAT_CAPTURE;
			break;
		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
			flag = ZORAN_FORMAT_PLAYBACK;
			break;
		case V4L2_BUF_TYPE_VIDEO_OVERLAY:
			flag = ZORAN_FORMAT_OVERLAY;
			break;
		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_ENUM_FMT - unknown type %d\n",
				ZR_DEVNAME(zr), fmt->type);
			return -EINVAL;
		}

2625
		for (i = 0; i < NUM_FORMATS; i++) {
L
Linus Torvalds 已提交
2626 2627 2628 2629 2630 2631
			if (zoran_formats[i].flags & flag)
				num++;
			if (num == fmt->index)
				break;
		}
		if (fmt->index < 0 /* late, but not too late */  ||
2632
		    i == NUM_FORMATS)
L
Linus Torvalds 已提交
2633 2634 2635 2636 2637
			return -EINVAL;

		memset(fmt, 0, sizeof(*fmt));
		fmt->index = index;
		fmt->type = type;
2638
		strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
L
Linus Torvalds 已提交
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
		fmt->pixelformat = zoran_formats[i].fourcc;
		if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
			fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;

		return 0;
	}
		break;

	case VIDIOC_G_FMT:
	{
		struct v4l2_format *fmt = arg;
		int type = fmt->type;

		dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));

		memset(fmt, 0, sizeof(*fmt));
		fmt->type = type;

		switch (fmt->type) {
		case V4L2_BUF_TYPE_VIDEO_OVERLAY:

I
Ingo Molnar 已提交
2660
			mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672

			fmt->fmt.win.w.left = fh->overlay_settings.x;
			fmt->fmt.win.w.top = fh->overlay_settings.y;
			fmt->fmt.win.w.width = fh->overlay_settings.width;
			fmt->fmt.win.w.height =
			    fh->overlay_settings.height;
			if (fh->overlay_settings.width * 2 >
			    BUZ_MAX_HEIGHT)
				fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
			else
				fmt->fmt.win.field = V4L2_FIELD_TOP;

I
Ingo Molnar 已提交
2673
			mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2674 2675 2676 2677 2678 2679

			break;

		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		case V4L2_BUF_TYPE_VIDEO_OUTPUT:

I
Ingo Molnar 已提交
2680
			mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689

			if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
			    fh->map_mode == ZORAN_MAP_MODE_RAW) {

				fmt->fmt.pix.width =
				    fh->v4l_settings.width;
				fmt->fmt.pix.height =
				    fh->v4l_settings.height;
				fmt->fmt.pix.sizeimage =
2690 2691
				    fh->v4l_settings.bytesperline *
				    fh->v4l_settings.height;
L
Linus Torvalds 已提交
2692 2693 2694 2695
				fmt->fmt.pix.pixelformat =
				    fh->v4l_settings.format->fourcc;
				fmt->fmt.pix.colorspace =
				    fh->v4l_settings.format->colorspace;
2696 2697
				fmt->fmt.pix.bytesperline =
				    fh->v4l_settings.bytesperline;
L
Linus Torvalds 已提交
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
				if (BUZ_MAX_HEIGHT <
				    (fh->v4l_settings.height * 2))
					fmt->fmt.pix.field =
					    V4L2_FIELD_INTERLACED;
				else
					fmt->fmt.pix.field =
					    V4L2_FIELD_TOP;

			} else {

				fmt->fmt.pix.width =
				    fh->jpg_settings.img_width /
				    fh->jpg_settings.HorDcm;
				fmt->fmt.pix.height =
				    fh->jpg_settings.img_height /
				    (fh->jpg_settings.VerDcm *
				     fh->jpg_settings.TmpDcm);
				fmt->fmt.pix.sizeimage =
				    zoran_v4l2_calc_bufsize(&fh->
							    jpg_settings);
				fmt->fmt.pix.pixelformat =
				    V4L2_PIX_FMT_MJPEG;
				if (fh->jpg_settings.TmpDcm == 1)
					fmt->fmt.pix.field =
					    (fh->jpg_settings.
					     odd_even ? V4L2_FIELD_SEQ_BT :
					     V4L2_FIELD_SEQ_BT);
				else
					fmt->fmt.pix.field =
					    (fh->jpg_settings.
					     odd_even ? V4L2_FIELD_TOP :
					     V4L2_FIELD_BOTTOM);

				fmt->fmt.pix.bytesperline = 0;
				fmt->fmt.pix.colorspace =
				    V4L2_COLORSPACE_SMPTE170M;
			}

I
Ingo Molnar 已提交
2736
			mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754

			break;

		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_G_FMT - unsupported type %d\n",
				ZR_DEVNAME(zr), fmt->type);
			return -EINVAL;
		}
		return 0;
	}
		break;

	case VIDIOC_S_FMT:
	{
		struct v4l2_format *fmt = arg;
		int i, res = 0;
2755
		__le32 printformat;
L
Linus Torvalds 已提交
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768

		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
			ZR_DEVNAME(zr), fmt->type);

		switch (fmt->type) {
		case V4L2_BUF_TYPE_VIDEO_OVERLAY:

			dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
				fmt->fmt.win.w.left, fmt->fmt.win.w.top,
				fmt->fmt.win.w.width,
				fmt->fmt.win.w.height,
				fmt->fmt.win.clipcount,
				fmt->fmt.win.bitmap);
I
Ingo Molnar 已提交
2769
			mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778
			res =
			    setup_window(file, fmt->fmt.win.w.left,
					 fmt->fmt.win.w.top,
					 fmt->fmt.win.w.width,
					 fmt->fmt.win.w.height,
					 (struct video_clip __user *)
					   fmt->fmt.win.clips,
					 fmt->fmt.win.clipcount,
					 fmt->fmt.win.bitmap);
I
Ingo Molnar 已提交
2779
			mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
			return res;
			break;

		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		case V4L2_BUF_TYPE_VIDEO_OUTPUT:

			printformat =
			    __cpu_to_le32(fmt->fmt.pix.pixelformat);
			dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
				fmt->fmt.pix.width, fmt->fmt.pix.height,
				fmt->fmt.pix.pixelformat,
				(char *) &printformat);

			/* we can be requested to do JPEG/raw playback/capture */
			if (!
			    (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
			     (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
			      fmt->fmt.pix.pixelformat ==
			      V4L2_PIX_FMT_MJPEG))) {
				dprintk(1,
					KERN_ERR
					"%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
					ZR_DEVNAME(zr), fmt->type,
					fmt->fmt.pix.pixelformat,
					(char *) &printformat);
				return -EINVAL;
			}

			if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
I
Ingo Molnar 已提交
2809
				mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875

				settings = fh->jpg_settings;

				if (fh->v4l_buffers.allocated ||
				    fh->jpg_buffers.allocated) {
					dprintk(1,
						KERN_ERR
						"%s: VIDIOC_S_FMT - cannot change capture mode\n",
						ZR_DEVNAME(zr));
					res = -EBUSY;
					goto sfmtjpg_unlock_and_return;
				}

				/* we actually need to set 'real' parameters now */
				if ((fmt->fmt.pix.height * 2) >
				    BUZ_MAX_HEIGHT)
					settings.TmpDcm = 1;
				else
					settings.TmpDcm = 2;
				settings.decimation = 0;
				if (fmt->fmt.pix.height <=
				    fh->jpg_settings.img_height / 2)
					settings.VerDcm = 2;
				else
					settings.VerDcm = 1;
				if (fmt->fmt.pix.width <=
				    fh->jpg_settings.img_width / 4)
					settings.HorDcm = 4;
				else if (fmt->fmt.pix.width <=
					 fh->jpg_settings.img_width / 2)
					settings.HorDcm = 2;
				else
					settings.HorDcm = 1;
				if (settings.TmpDcm == 1)
					settings.field_per_buff = 2;
				else
					settings.field_per_buff = 1;

				/* check */
				if ((res =
				     zoran_check_jpg_settings(zr,
							      &settings)))
					goto sfmtjpg_unlock_and_return;

				/* it's ok, so set them */
				fh->jpg_settings = settings;

				/* tell the user what we actually did */
				fmt->fmt.pix.width =
				    settings.img_width / settings.HorDcm;
				fmt->fmt.pix.height =
				    settings.img_height * 2 /
				    (settings.TmpDcm * settings.VerDcm);
				if (settings.TmpDcm == 1)
					fmt->fmt.pix.field =
					    (fh->jpg_settings.
					     odd_even ? V4L2_FIELD_SEQ_TB :
					     V4L2_FIELD_SEQ_BT);
				else
					fmt->fmt.pix.field =
					    (fh->jpg_settings.
					     odd_even ? V4L2_FIELD_TOP :
					     V4L2_FIELD_BOTTOM);
				fh->jpg_buffers.buffer_size =
				    zoran_v4l2_calc_bufsize(&fh->
							    jpg_settings);
2876
				fmt->fmt.pix.bytesperline = 0;
L
Linus Torvalds 已提交
2877 2878
				fmt->fmt.pix.sizeimage =
				    fh->jpg_buffers.buffer_size;
2879 2880
				fmt->fmt.pix.colorspace =
				    V4L2_COLORSPACE_SMPTE170M;
L
Linus Torvalds 已提交
2881 2882 2883 2884 2885 2886 2887 2888 2889

				/* we hereby abuse this variable to show that
				 * we're gonna do mjpeg capture */
				fh->map_mode =
				    (fmt->type ==
				     V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
				    ZORAN_MAP_MODE_JPG_REC :
				    ZORAN_MAP_MODE_JPG_PLAY;
			sfmtjpg_unlock_and_return:
I
Ingo Molnar 已提交
2890
				mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2891
			} else {
2892
				for (i = 0; i < NUM_FORMATS; i++)
L
Linus Torvalds 已提交
2893 2894 2895
					if (fmt->fmt.pix.pixelformat ==
					    zoran_formats[i].fourcc)
						break;
2896
				if (i == NUM_FORMATS) {
L
Linus Torvalds 已提交
2897 2898 2899 2900 2901 2902 2903 2904
					dprintk(1,
						KERN_ERR
						"%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
						ZR_DEVNAME(zr),
						fmt->fmt.pix.pixelformat,
						(char *) &printformat);
					return -EINVAL;
				}
I
Ingo Molnar 已提交
2905
				mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
				if (fh->jpg_buffers.allocated ||
				    (fh->v4l_buffers.allocated &&
				     fh->v4l_buffers.active !=
				     ZORAN_FREE)) {
					dprintk(1,
						KERN_ERR
						"%s: VIDIOC_S_FMT - cannot change capture mode\n",
						ZR_DEVNAME(zr));
					res = -EBUSY;
					goto sfmtv4l_unlock_and_return;
				}
				if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
					fmt->fmt.pix.height =
					    BUZ_MAX_HEIGHT;
				if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
					fmt->fmt.pix.width = BUZ_MAX_WIDTH;

				if ((res =
				     zoran_v4l_set_format(file,
							  fmt->fmt.pix.
							  width,
							  fmt->fmt.pix.
							  height,
							  &zoran_formats
							  [i])))
					goto sfmtv4l_unlock_and_return;

				/* tell the user the
				 * results/missing stuff */
2935 2936
				fmt->fmt.pix.bytesperline =
					fh->v4l_settings.bytesperline;
2937 2938 2939
				fmt->fmt.pix.sizeimage =
					fh->v4l_settings.height *
					fh->v4l_settings.bytesperline;
2940 2941
				fmt->fmt.pix.colorspace =
					fh->v4l_settings.format->colorspace;
L
Linus Torvalds 已提交
2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
				if (BUZ_MAX_HEIGHT <
				    (fh->v4l_settings.height * 2))
					fmt->fmt.pix.field =
					    V4L2_FIELD_INTERLACED;
				else
					fmt->fmt.pix.field =
					    V4L2_FIELD_TOP;

				fh->map_mode = ZORAN_MAP_MODE_RAW;
			sfmtv4l_unlock_and_return:
I
Ingo Molnar 已提交
2952
				mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
			}

			break;

		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_S_FMT - unsupported type %d\n",
				ZR_DEVNAME(zr), fmt->type);
			return -EINVAL;
		}

		return res;
	}
		break;

	case VIDIOC_G_FBUF:
	{
		struct v4l2_framebuffer *fb = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));

		memset(fb, 0, sizeof(*fb));
I
Ingo Molnar 已提交
2976
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2977 2978 2979 2980 2981 2982 2983 2984
		fb->base = zr->buffer.base;
		fb->fmt.width = zr->buffer.width;
		fb->fmt.height = zr->buffer.height;
		if (zr->overlay_settings.format) {
			fb->fmt.pixelformat =
				fh->overlay_settings.format->fourcc;
		}
		fb->fmt.bytesperline = zr->buffer.bytesperline;
I
Ingo Molnar 已提交
2985
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
		fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
		fb->fmt.field = V4L2_FIELD_INTERLACED;
		fb->flags = V4L2_FBUF_FLAG_OVERLAY;
		fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;

		return 0;
	}
		break;

	case VIDIOC_S_FBUF:
	{
		int i, res = 0;
		struct v4l2_framebuffer *fb = arg;
2999
		__le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
L
Linus Torvalds 已提交
3000 3001 3002 3003 3004 3005 3006 3007

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
			ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
			fb->fmt.bytesperline, fb->fmt.pixelformat,
			(char *) &printformat);

3008
		for (i = 0; i < NUM_FORMATS; i++)
L
Linus Torvalds 已提交
3009 3010
			if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
				break;
3011
		if (i == NUM_FORMATS) {
L
Linus Torvalds 已提交
3012 3013 3014 3015 3016 3017 3018 3019
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
				ZR_DEVNAME(zr), fb->fmt.pixelformat,
				(char *) &printformat);
			return -EINVAL;
		}

I
Ingo Molnar 已提交
3020
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3021 3022 3023 3024
		res =
		    setup_fbuffer(file, fb->base, &zoran_formats[i],
				  fb->fmt.width, fb->fmt.height,
				  fb->fmt.bytesperline);
I
Ingo Molnar 已提交
3025
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037

		return res;
	}
		break;

	case VIDIOC_OVERLAY:
	{
		int *on = arg, res;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
			ZR_DEVNAME(zr), *on);

I
Ingo Molnar 已提交
3038
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3039
		res = setup_overlay(file, *on);
I
Ingo Molnar 已提交
3040
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061

		return res;
	}
		break;

	case VIDIOC_REQBUFS:
	{
		struct v4l2_requestbuffers *req = arg;
		int res = 0;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
			ZR_DEVNAME(zr), req->type);

		if (req->memory != V4L2_MEMORY_MMAP) {
			dprintk(1,
				KERN_ERR
				"%s: only MEMORY_MMAP capture is supported, not %d\n",
				ZR_DEVNAME(zr), req->memory);
			return -EINVAL;
		}

I
Ingo Molnar 已提交
3062
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122

		if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_REQBUFS - buffers allready allocated\n",
				ZR_DEVNAME(zr));
			res = -EBUSY;
			goto v4l2reqbuf_unlock_and_return;
		}

		if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
		    req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {

			/* control user input */
			if (req->count < 2)
				req->count = 2;
			if (req->count > v4l_nbufs)
				req->count = v4l_nbufs;
			fh->v4l_buffers.num_buffers = req->count;

			if (v4l_fbuffer_alloc(file)) {
				res = -ENOMEM;
				goto v4l2reqbuf_unlock_and_return;
			}

			/* The next mmap will map the V4L buffers */
			fh->map_mode = ZORAN_MAP_MODE_RAW;

		} else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
			   fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {

			/* we need to calculate size ourselves now */
			if (req->count < 4)
				req->count = 4;
			if (req->count > jpg_nbufs)
				req->count = jpg_nbufs;
			fh->jpg_buffers.num_buffers = req->count;
			fh->jpg_buffers.buffer_size =
			    zoran_v4l2_calc_bufsize(&fh->jpg_settings);

			if (jpg_fbuffer_alloc(file)) {
				res = -ENOMEM;
				goto v4l2reqbuf_unlock_and_return;
			}

			/* The next mmap will map the MJPEG buffers */
			if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
				fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
			else
				fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;

		} else {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_REQBUFS - unknown type %d\n",
				ZR_DEVNAME(zr), req->type);
			res = -EINVAL;
			goto v4l2reqbuf_unlock_and_return;
		}
	v4l2reqbuf_unlock_and_return:
I
Ingo Molnar 已提交
3123
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139

		return 0;
	}
		break;

	case VIDIOC_QUERYBUF:
	{
		struct v4l2_buffer *buf = arg;
		__u32 type = buf->type;
		int index = buf->index, res;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
			ZR_DEVNAME(zr), buf->index, buf->type);

3140
		memset(buf, 0, sizeof(*buf));
L
Linus Torvalds 已提交
3141 3142 3143
		buf->type = type;
		buf->index = index;

I
Ingo Molnar 已提交
3144
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3145
		res = zoran_v4l2_buffer_status(file, buf, buf->index);
I
Ingo Molnar 已提交
3146
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160

		return res;
	}
		break;

	case VIDIOC_QBUF:
	{
		struct v4l2_buffer *buf = arg;
		int res = 0, codec_mode, buf_type;

		dprintk(3,
			KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
			ZR_DEVNAME(zr), buf->type, buf->index);

I
Ingo Molnar 已提交
3161
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220

		switch (fh->map_mode) {
		case ZORAN_MAP_MODE_RAW:
			if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
				dprintk(1,
					KERN_ERR
					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
					ZR_DEVNAME(zr), buf->type, fh->map_mode);
				res = -EINVAL;
				goto qbuf_unlock_and_return;
			}

			res = zoran_v4l_queue_frame(file, buf->index);
			if (res)
				goto qbuf_unlock_and_return;
			if (!zr->v4l_memgrab_active &&
			    fh->v4l_buffers.active == ZORAN_LOCKED)
				zr36057_set_memgrab(zr, 1);
			break;

		case ZORAN_MAP_MODE_JPG_REC:
		case ZORAN_MAP_MODE_JPG_PLAY:
			if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
				buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
				codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
			} else {
				buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
				codec_mode = BUZ_MODE_MOTION_COMPRESS;
			}

			if (buf->type != buf_type) {
				dprintk(1,
					KERN_ERR
					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
					ZR_DEVNAME(zr), buf->type, fh->map_mode);
				res = -EINVAL;
				goto qbuf_unlock_and_return;
			}

			res =
			    zoran_jpg_queue_frame(file, buf->index,
						  codec_mode);
			if (res != 0)
				goto qbuf_unlock_and_return;
			if (zr->codec_mode == BUZ_MODE_IDLE &&
			    fh->jpg_buffers.active == ZORAN_LOCKED) {
				zr36057_enable_jpg(zr, codec_mode);
			}
			break;

		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_QBUF - unsupported type %d\n",
				ZR_DEVNAME(zr), buf->type);
			res = -EINVAL;
			goto qbuf_unlock_and_return;
		}
	qbuf_unlock_and_return:
I
Ingo Molnar 已提交
3221
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234

		return res;
	}
		break;

	case VIDIOC_DQBUF:
	{
		struct v4l2_buffer *buf = arg;
		int res = 0, buf_type, num = -1;	/* compiler borks here (?) */

		dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
			ZR_DEVNAME(zr), buf->type);

I
Ingo Molnar 已提交
3235
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308

		switch (fh->map_mode) {
		case ZORAN_MAP_MODE_RAW:
			if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
				dprintk(1,
					KERN_ERR
					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
					ZR_DEVNAME(zr), buf->type, fh->map_mode);
				res = -EINVAL;
				goto dqbuf_unlock_and_return;
			}

			num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
			if (file->f_flags & O_NONBLOCK &&
			    zr->v4l_buffers.buffer[num].state !=
			    BUZ_STATE_DONE) {
				res = -EAGAIN;
				goto dqbuf_unlock_and_return;
			}
			res = v4l_sync(file, num);
			if (res)
				goto dqbuf_unlock_and_return;
			else
				zr->v4l_sync_tail++;
			res = zoran_v4l2_buffer_status(file, buf, num);
			break;

		case ZORAN_MAP_MODE_JPG_REC:
		case ZORAN_MAP_MODE_JPG_PLAY:
		{
			struct zoran_sync bs;

			if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
				buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
			else
				buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

			if (buf->type != buf_type) {
				dprintk(1,
					KERN_ERR
					"%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
					ZR_DEVNAME(zr), buf->type, fh->map_mode);
				res = -EINVAL;
				goto dqbuf_unlock_and_return;
			}

			num =
			    zr->jpg_pend[zr->
					 jpg_que_tail & BUZ_MASK_FRAME];

			if (file->f_flags & O_NONBLOCK &&
			    zr->jpg_buffers.buffer[num].state !=
			    BUZ_STATE_DONE) {
				res = -EAGAIN;
				goto dqbuf_unlock_and_return;
			}
			res = jpg_sync(file, &bs);
			if (res)
				goto dqbuf_unlock_and_return;
			res =
			    zoran_v4l2_buffer_status(file, buf, bs.frame);
			break;
		}

		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_DQBUF - unsupported type %d\n",
				ZR_DEVNAME(zr), buf->type);
			res = -EINVAL;
			goto dqbuf_unlock_and_return;
		}
	dqbuf_unlock_and_return:
I
Ingo Molnar 已提交
3309
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320

		return res;
	}
		break;

	case VIDIOC_STREAMON:
	{
		int res = 0;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));

I
Ingo Molnar 已提交
3321
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368

		switch (fh->map_mode) {
		case ZORAN_MAP_MODE_RAW:	/* raw capture */
			if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
			    fh->v4l_buffers.active != ZORAN_ACTIVE) {
				res = -EBUSY;
				goto strmon_unlock_and_return;
			}

			zr->v4l_buffers.active = fh->v4l_buffers.active =
			    ZORAN_LOCKED;
			zr->v4l_settings = fh->v4l_settings;

			zr->v4l_sync_tail = zr->v4l_pend_tail;
			if (!zr->v4l_memgrab_active &&
			    zr->v4l_pend_head != zr->v4l_pend_tail) {
				zr36057_set_memgrab(zr, 1);
			}
			break;

		case ZORAN_MAP_MODE_JPG_REC:
		case ZORAN_MAP_MODE_JPG_PLAY:
			/* what is the codec mode right now? */
			if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
			    fh->jpg_buffers.active != ZORAN_ACTIVE) {
				res = -EBUSY;
				goto strmon_unlock_and_return;
			}

			zr->jpg_buffers.active = fh->jpg_buffers.active =
			    ZORAN_LOCKED;

			if (zr->jpg_que_head != zr->jpg_que_tail) {
				/* Start the jpeg codec when the first frame is queued  */
				jpeg_start(zr);
			}

			break;
		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_STREAMON - invalid map mode %d\n",
				ZR_DEVNAME(zr), fh->map_mode);
			res = -EINVAL;
			goto strmon_unlock_and_return;
		}
	strmon_unlock_and_return:
I
Ingo Molnar 已提交
3369
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380

		return res;
	}
		break;

	case VIDIOC_STREAMOFF:
	{
		int i, res = 0;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));

I
Ingo Molnar 已提交
3381
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393

		switch (fh->map_mode) {
		case ZORAN_MAP_MODE_RAW:	/* raw capture */
			if (fh->v4l_buffers.active == ZORAN_FREE &&
			    zr->v4l_buffers.active != ZORAN_FREE) {
				res = -EPERM;	/* stay off other's settings! */
				goto strmoff_unlock_and_return;
			}
			if (zr->v4l_buffers.active == ZORAN_FREE)
				goto strmoff_unlock_and_return;

			/* unload capture */
3394
			if (zr->v4l_memgrab_active) {
3395
				unsigned long flags;
3396 3397

				spin_lock_irqsave(&zr->spinlock, flags);
L
Linus Torvalds 已提交
3398
				zr36057_set_memgrab(zr, 0);
3399 3400
				spin_unlock_irqrestore(&zr->spinlock, flags);
			}
L
Linus Torvalds 已提交
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443

			for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
				zr->v4l_buffers.buffer[i].state =
				    BUZ_STATE_USER;
			fh->v4l_buffers = zr->v4l_buffers;

			zr->v4l_buffers.active = fh->v4l_buffers.active =
			    ZORAN_FREE;

			zr->v4l_grab_seq = 0;
			zr->v4l_pend_head = zr->v4l_pend_tail = 0;
			zr->v4l_sync_tail = 0;

			break;

		case ZORAN_MAP_MODE_JPG_REC:
		case ZORAN_MAP_MODE_JPG_PLAY:
			if (fh->jpg_buffers.active == ZORAN_FREE &&
			    zr->jpg_buffers.active != ZORAN_FREE) {
				res = -EPERM;	/* stay off other's settings! */
				goto strmoff_unlock_and_return;
			}
			if (zr->jpg_buffers.active == ZORAN_FREE)
				goto strmoff_unlock_and_return;

			res =
			    jpg_qbuf(file, -1,
				     (fh->map_mode ==
				      ZORAN_MAP_MODE_JPG_REC) ?
				     BUZ_MODE_MOTION_COMPRESS :
				     BUZ_MODE_MOTION_DECOMPRESS);
			if (res)
				goto strmoff_unlock_and_return;
			break;
		default:
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
				ZR_DEVNAME(zr), fh->map_mode);
			res = -EINVAL;
			goto strmoff_unlock_and_return;
		}
	strmoff_unlock_and_return:
I
Ingo Molnar 已提交
3444
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468

		return res;
	}
		break;

	case VIDIOC_QUERYCTRL:
	{
		struct v4l2_queryctrl *ctrl = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
			ZR_DEVNAME(zr), ctrl->id);

		/* we only support hue/saturation/contrast/brightness */
		if (ctrl->id < V4L2_CID_BRIGHTNESS ||
		    ctrl->id > V4L2_CID_HUE)
			return -EINVAL;
		else {
			int id = ctrl->id;
			memset(ctrl, 0, sizeof(*ctrl));
			ctrl->id = id;
		}

		switch (ctrl->id) {
		case V4L2_CID_BRIGHTNESS:
3469
			strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
L
Linus Torvalds 已提交
3470 3471
			break;
		case V4L2_CID_CONTRAST:
3472
			strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
L
Linus Torvalds 已提交
3473 3474
			break;
		case V4L2_CID_SATURATION:
3475
			strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
L
Linus Torvalds 已提交
3476 3477
			break;
		case V4L2_CID_HUE:
3478
			strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
L
Linus Torvalds 已提交
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503
			break;
		}

		ctrl->minimum = 0;
		ctrl->maximum = 65535;
		ctrl->step = 1;
		ctrl->default_value = 32768;
		ctrl->type = V4L2_CTRL_TYPE_INTEGER;

		return 0;
	}
		break;

	case VIDIOC_G_CTRL:
	{
		struct v4l2_control *ctrl = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
			ZR_DEVNAME(zr), ctrl->id);

		/* we only support hue/saturation/contrast/brightness */
		if (ctrl->id < V4L2_CID_BRIGHTNESS ||
		    ctrl->id > V4L2_CID_HUE)
			return -EINVAL;

I
Ingo Molnar 已提交
3504
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
		switch (ctrl->id) {
		case V4L2_CID_BRIGHTNESS:
			ctrl->value = zr->brightness;
			break;
		case V4L2_CID_CONTRAST:
			ctrl->value = zr->contrast;
			break;
		case V4L2_CID_SATURATION:
			ctrl->value = zr->saturation;
			break;
		case V4L2_CID_HUE:
			ctrl->value = zr->hue;
			break;
		}
I
Ingo Molnar 已提交
3519
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545

		return 0;
	}
		break;

	case VIDIOC_S_CTRL:
	{
		struct v4l2_control *ctrl = arg;
		struct video_picture pict;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
			ZR_DEVNAME(zr), ctrl->id);

		/* we only support hue/saturation/contrast/brightness */
		if (ctrl->id < V4L2_CID_BRIGHTNESS ||
		    ctrl->id > V4L2_CID_HUE)
			return -EINVAL;

		if (ctrl->value < 0 || ctrl->value > 65535) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
				ZR_DEVNAME(zr), ctrl->value, ctrl->id);
			return -EINVAL;
		}

I
Ingo Molnar 已提交
3546
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
		switch (ctrl->id) {
		case V4L2_CID_BRIGHTNESS:
			zr->brightness = ctrl->value;
			break;
		case V4L2_CID_CONTRAST:
			zr->contrast = ctrl->value;
			break;
		case V4L2_CID_SATURATION:
			zr->saturation = ctrl->value;
			break;
		case V4L2_CID_HUE:
			zr->hue = ctrl->value;
			break;
		}
		pict.brightness = zr->brightness;
		pict.contrast = zr->contrast;
		pict.colour = zr->saturation;
		pict.hue = zr->hue;

		decoder_command(zr, DECODER_SET_PICTURE, &pict);

I
Ingo Molnar 已提交
3568
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595

		return 0;
	}
		break;

	case VIDIOC_ENUMSTD:
	{
		struct v4l2_standard *std = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
			ZR_DEVNAME(zr), std->index);

		if (std->index < 0 || std->index >= (zr->card.norms + 1))
			return -EINVAL;
		else {
			int id = std->index;
			memset(std, 0, sizeof(*std));
			std->index = id;
		}

		if (std->index == zr->card.norms) {
			/* if we have autodetect, ... */
			struct video_decoder_capability caps;
			decoder_command(zr, DECODER_GET_CAPABILITIES,
					&caps);
			if (caps.flags & VIDEO_DECODER_AUTO) {
				std->id = V4L2_STD_ALL;
3596
				strncpy(std->name, "Autodetect", sizeof(std->name)-1);
L
Linus Torvalds 已提交
3597 3598 3599 3600 3601 3602 3603
				return 0;
			} else
				return -EINVAL;
		}
		switch (std->index) {
		case 0:
			std->id = V4L2_STD_PAL;
3604
			strncpy(std->name, "PAL", sizeof(std->name)-1);
L
Linus Torvalds 已提交
3605 3606 3607 3608 3609 3610
			std->frameperiod.numerator = 1;
			std->frameperiod.denominator = 25;
			std->framelines = zr->card.tvn[0]->Ht;
			break;
		case 1:
			std->id = V4L2_STD_NTSC;
3611
			strncpy(std->name, "NTSC", sizeof(std->name)-1);
L
Linus Torvalds 已提交
3612 3613 3614 3615 3616 3617
			std->frameperiod.numerator = 1001;
			std->frameperiod.denominator = 30000;
			std->framelines = zr->card.tvn[1]->Ht;
			break;
		case 2:
			std->id = V4L2_STD_SECAM;
3618
			strncpy(std->name, "SECAM", sizeof(std->name)-1);
L
Linus Torvalds 已提交
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635
			std->frameperiod.numerator = 1;
			std->frameperiod.denominator = 25;
			std->framelines = zr->card.tvn[2]->Ht;
			break;
		}

		return 0;
	}
		break;

	case VIDIOC_G_STD:
	{
		v4l2_std_id *std = arg;
		int norm;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));

I
Ingo Molnar 已提交
3636
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3637
		norm = zr->norm;
I
Ingo Molnar 已提交
3638
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663

		switch (norm) {
		case VIDEO_MODE_PAL:
			*std = V4L2_STD_PAL;
			break;
		case VIDEO_MODE_NTSC:
			*std = V4L2_STD_NTSC;
			break;
		case VIDEO_MODE_SECAM:
			*std = V4L2_STD_SECAM;
			break;
		}

		return 0;
	}
		break;

	case VIDIOC_S_STD:
	{
		int norm = -1, res = 0;
		v4l2_std_id *std = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
			ZR_DEVNAME(zr), (unsigned long long)*std);

3664
		if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
L
Linus Torvalds 已提交
3665
			norm = VIDEO_MODE_PAL;
3666
		else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
L
Linus Torvalds 已提交
3667
			norm = VIDEO_MODE_NTSC;
3668
		else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
L
Linus Torvalds 已提交
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679
			norm = VIDEO_MODE_SECAM;
		else if (*std == V4L2_STD_ALL)
			norm = VIDEO_MODE_AUTO;
		else {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
				ZR_DEVNAME(zr), (unsigned long long)*std);
			return -EINVAL;
		}

I
Ingo Molnar 已提交
3680
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3681 3682 3683 3684 3685
		if ((res = zoran_set_norm(zr, norm)))
			goto sstd_unlock_and_return;

		res = wait_grab_pending(zr);
	sstd_unlock_and_return:
I
Ingo Molnar 已提交
3686
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
		return res;
	}
		break;

	case VIDIOC_ENUMINPUT:
	{
		struct v4l2_input *inp = arg;
		int status;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
			ZR_DEVNAME(zr), inp->index);

		if (inp->index < 0 || inp->index >= zr->card.inputs)
			return -EINVAL;
		else {
			int id = inp->index;
			memset(inp, 0, sizeof(*inp));
			inp->index = id;
		}

		strncpy(inp->name, zr->card.input[inp->index].name,
			sizeof(inp->name) - 1);
		inp->type = V4L2_INPUT_TYPE_CAMERA;
		inp->std = V4L2_STD_ALL;

		/* Get status of video decoder */
I
Ingo Molnar 已提交
3713
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3714
		decoder_command(zr, DECODER_GET_STATUS, &status);
I
Ingo Molnar 已提交
3715
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733

		if (!(status & DECODER_STATUS_GOOD)) {
			inp->status |= V4L2_IN_ST_NO_POWER;
			inp->status |= V4L2_IN_ST_NO_SIGNAL;
		}
		if (!(status & DECODER_STATUS_COLOR))
			inp->status |= V4L2_IN_ST_NO_COLOR;

		return 0;
	}
		break;

	case VIDIOC_G_INPUT:
	{
		int *input = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));

I
Ingo Molnar 已提交
3734
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3735
		*input = zr->input;
I
Ingo Molnar 已提交
3736
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748

		return 0;
	}
		break;

	case VIDIOC_S_INPUT:
	{
		int *input = arg, res = 0;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
			ZR_DEVNAME(zr), *input);

I
Ingo Molnar 已提交
3749
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3750 3751 3752 3753 3754 3755
		if ((res = zoran_set_input(zr, *input)))
			goto sinput_unlock_and_return;

		/* Make sure the changes come into effect */
		res = wait_grab_pending(zr);
	sinput_unlock_and_return:
I
Ingo Molnar 已提交
3756
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773
		return res;
	}
		break;

	case VIDIOC_ENUMOUTPUT:
	{
		struct v4l2_output *outp = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
			ZR_DEVNAME(zr), outp->index);

		if (outp->index != 0)
			return -EINVAL;

		memset(outp, 0, sizeof(*outp));
		outp->index = 0;
		outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
3774
		strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
L
Linus Torvalds 已提交
3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817

		return 0;
	}
		break;

	case VIDIOC_G_OUTPUT:
	{
		int *output = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));

		*output = 0;

		return 0;
	}
		break;

	case VIDIOC_S_OUTPUT:
	{
		int *output = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
			ZR_DEVNAME(zr), *output);

		if (*output != 0)
			return -EINVAL;

		return 0;
	}
		break;

		/* cropping (sub-frame capture) */
	case VIDIOC_CROPCAP:
	{
		struct v4l2_cropcap *cropcap = arg;
		int type = cropcap->type, res = 0;

		dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
			ZR_DEVNAME(zr), cropcap->type);

		memset(cropcap, 0, sizeof(*cropcap));
		cropcap->type = type;

I
Ingo Molnar 已提交
3818
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837

		if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
		    (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
		     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
				ZR_DEVNAME(zr));
			res = -EINVAL;
			goto cropcap_unlock_and_return;
		}

		cropcap->bounds.top = cropcap->bounds.left = 0;
		cropcap->bounds.width = BUZ_MAX_WIDTH;
		cropcap->bounds.height = BUZ_MAX_HEIGHT;
		cropcap->defrect.top = cropcap->defrect.left = 0;
		cropcap->defrect.width = BUZ_MIN_WIDTH;
		cropcap->defrect.height = BUZ_MIN_HEIGHT;
	cropcap_unlock_and_return:
I
Ingo Molnar 已提交
3838
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853
		return res;
	}
		break;

	case VIDIOC_G_CROP:
	{
		struct v4l2_crop *crop = arg;
		int type = crop->type, res = 0;

		dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
			ZR_DEVNAME(zr), crop->type);

		memset(crop, 0, sizeof(*crop));
		crop->type = type;

I
Ingo Molnar 已提交
3854
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872

		if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
		    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
		     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
				ZR_DEVNAME(zr));
			res = -EINVAL;
			goto gcrop_unlock_and_return;
		}

		crop->c.top = fh->jpg_settings.img_y;
		crop->c.left = fh->jpg_settings.img_x;
		crop->c.width = fh->jpg_settings.img_width;
		crop->c.height = fh->jpg_settings.img_height;

	gcrop_unlock_and_return:
I
Ingo Molnar 已提交
3873
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891

		return res;
	}
		break;

	case VIDIOC_S_CROP:
	{
		struct v4l2_crop *crop = arg;
		int res = 0;

		settings = fh->jpg_settings;

		dprintk(3,
			KERN_ERR
			"%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
			ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
			crop->c.width, crop->c.height);

I
Ingo Molnar 已提交
3892
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927

		if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_S_CROP - cannot change settings while active\n",
				ZR_DEVNAME(zr));
			res = -EBUSY;
			goto scrop_unlock_and_return;
		}

		if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
		    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
		     fh->map_mode == ZORAN_MAP_MODE_RAW)) {
			dprintk(1,
				KERN_ERR
				"%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
				ZR_DEVNAME(zr));
			res = -EINVAL;
			goto scrop_unlock_and_return;
		}

		/* move into a form that we understand */
		settings.img_x = crop->c.left;
		settings.img_y = crop->c.top;
		settings.img_width = crop->c.width;
		settings.img_height = crop->c.height;

		/* check validity */
		if ((res = zoran_check_jpg_settings(zr, &settings)))
			goto scrop_unlock_and_return;

		/* accept */
		fh->jpg_settings = settings;

	scrop_unlock_and_return:
I
Ingo Molnar 已提交
3928
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941
		return res;
	}
		break;

	case VIDIOC_G_JPEGCOMP:
	{
		struct v4l2_jpegcompression *params = arg;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
			ZR_DEVNAME(zr));

		memset(params, 0, sizeof(*params));

I
Ingo Molnar 已提交
3942
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956

		params->quality = fh->jpg_settings.jpg_comp.quality;
		params->APPn = fh->jpg_settings.jpg_comp.APPn;
		memcpy(params->APP_data,
		       fh->jpg_settings.jpg_comp.APP_data,
		       fh->jpg_settings.jpg_comp.APP_len);
		params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
		memcpy(params->COM_data,
		       fh->jpg_settings.jpg_comp.COM_data,
		       fh->jpg_settings.jpg_comp.COM_len);
		params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
		params->jpeg_markers =
		    fh->jpg_settings.jpg_comp.jpeg_markers;

I
Ingo Molnar 已提交
3957
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977

		return 0;
	}
		break;

	case VIDIOC_S_JPEGCOMP:
	{
		struct v4l2_jpegcompression *params = arg;
		int res = 0;

		settings = fh->jpg_settings;

		dprintk(3,
			KERN_DEBUG
			"%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
			ZR_DEVNAME(zr), params->quality, params->APPn,
			params->APP_len, params->COM_len);

		settings.jpg_comp = *params;

I
Ingo Molnar 已提交
3978
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996

		if (fh->v4l_buffers.active != ZORAN_FREE ||
		    fh->jpg_buffers.active != ZORAN_FREE) {
			dprintk(1,
				KERN_WARNING
				"%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
				ZR_DEVNAME(zr));
			res = -EBUSY;
			goto sjpegc_unlock_and_return;
		}

		if ((res = zoran_check_jpg_settings(zr, &settings)))
			goto sjpegc_unlock_and_return;
		if (!fh->jpg_buffers.allocated)
			fh->jpg_buffers.buffer_size =
			    zoran_v4l2_calc_bufsize(&fh->jpg_settings);
		fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
	sjpegc_unlock_and_return:
I
Ingo Molnar 已提交
3997
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030

		return 0;
	}
		break;

	case VIDIOC_QUERYSTD:	/* why is this useful? */
	{
		v4l2_std_id *std = arg;

		dprintk(3,
			KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
			ZR_DEVNAME(zr), (unsigned long long)*std);

		if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
		    *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
					     zr->card.norms == 3)) {
			return 0;
		}

		return -EINVAL;
	}
		break;

	case VIDIOC_TRY_FMT:
	{
		struct v4l2_format *fmt = arg;
		int res = 0;

		dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
			ZR_DEVNAME(zr), fmt->type);

		switch (fmt->type) {
		case V4L2_BUF_TYPE_VIDEO_OVERLAY:
I
Ingo Molnar 已提交
4031
			mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041

			if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
				fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
			if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
				fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
			if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
				fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
			if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
				fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;

I
Ingo Molnar 已提交
4042
			mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4043 4044 4045 4046 4047 4048 4049
			break;

		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
			if (fmt->fmt.pix.bytesperline > 0)
				return -EINVAL;

I
Ingo Molnar 已提交
4050
			mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108

			if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
				settings = fh->jpg_settings;

				/* we actually need to set 'real' parameters now */
				if ((fmt->fmt.pix.height * 2) >
				    BUZ_MAX_HEIGHT)
					settings.TmpDcm = 1;
				else
					settings.TmpDcm = 2;
				settings.decimation = 0;
				if (fmt->fmt.pix.height <=
				    fh->jpg_settings.img_height / 2)
					settings.VerDcm = 2;
				else
					settings.VerDcm = 1;
				if (fmt->fmt.pix.width <=
				    fh->jpg_settings.img_width / 4)
					settings.HorDcm = 4;
				else if (fmt->fmt.pix.width <=
					 fh->jpg_settings.img_width / 2)
					settings.HorDcm = 2;
				else
					settings.HorDcm = 1;
				if (settings.TmpDcm == 1)
					settings.field_per_buff = 2;
				else
					settings.field_per_buff = 1;

				/* check */
				if ((res =
				     zoran_check_jpg_settings(zr,
							      &settings)))
					goto tryfmt_unlock_and_return;

				/* tell the user what we actually did */
				fmt->fmt.pix.width =
				    settings.img_width / settings.HorDcm;
				fmt->fmt.pix.height =
				    settings.img_height * 2 /
				    (settings.TmpDcm * settings.VerDcm);
				if (settings.TmpDcm == 1)
					fmt->fmt.pix.field =
					    (fh->jpg_settings.
					     odd_even ? V4L2_FIELD_SEQ_TB :
					     V4L2_FIELD_SEQ_BT);
				else
					fmt->fmt.pix.field =
					    (fh->jpg_settings.
					     odd_even ? V4L2_FIELD_TOP :
					     V4L2_FIELD_BOTTOM);

				fmt->fmt.pix.sizeimage =
				    zoran_v4l2_calc_bufsize(&settings);
			} else if (fmt->type ==
				   V4L2_BUF_TYPE_VIDEO_CAPTURE) {
				int i;

4109
				for (i = 0; i < NUM_FORMATS; i++)
L
Linus Torvalds 已提交
4110 4111 4112
					if (zoran_formats[i].fourcc ==
					    fmt->fmt.pix.pixelformat)
						break;
4113
				if (i == NUM_FORMATS) {
L
Linus Torvalds 已提交
4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132
					res = -EINVAL;
					goto tryfmt_unlock_and_return;
				}

				if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
					fmt->fmt.pix.width = BUZ_MAX_WIDTH;
				if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
					fmt->fmt.pix.width = BUZ_MIN_WIDTH;
				if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
					fmt->fmt.pix.height =
					    BUZ_MAX_HEIGHT;
				if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
					fmt->fmt.pix.height =
					    BUZ_MIN_HEIGHT;
			} else {
				res = -EINVAL;
				goto tryfmt_unlock_and_return;
			}
		tryfmt_unlock_and_return:
I
Ingo Molnar 已提交
4133
			mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156

			return res;
			break;

		default:
			return -EINVAL;
		}

		return 0;
	}
		break;

	default:
		dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
			ZR_DEVNAME(zr), cmd);
		return -ENOIOCTLCMD;
		break;

	}
	return 0;
}


4157
static long
4158 4159 4160
zoran_ioctl(struct file  *file,
	    unsigned int  cmd,
	    unsigned long arg)
L
Linus Torvalds 已提交
4161
{
4162
	return video_usercopy(file, cmd, arg, zoran_do_ioctl);
L
Linus Torvalds 已提交
4163 4164 4165 4166 4167 4168 4169 4170 4171
}

static unsigned int
zoran_poll (struct file *file,
	    poll_table  *wait)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int res = 0, frame;
4172
	unsigned long flags;
L
Linus Torvalds 已提交
4173 4174 4175 4176 4177 4178 4179 4180 4181

	/* we should check whether buffers are ready to be synced on
	 * (w/o waits - O_NONBLOCK) here
	 * if ready for read (sync), return POLLIN|POLLRDNORM,
	 * if ready for write (sync), return POLLOUT|POLLWRNORM,
	 * if error, return POLLERR,
	 * if no buffers queued or so, return POLLNVAL
	 */

I
Ingo Molnar 已提交
4182
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4183 4184 4185

	switch (fh->map_mode) {
	case ZORAN_MAP_MODE_RAW:
4186 4187 4188 4189 4190 4191 4192
		poll_wait(file, &zr->v4l_capq, wait);
		frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];

		spin_lock_irqsave(&zr->spinlock, flags);
		dprintk(3,
			KERN_DEBUG
			"%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
4193
			ZR_DEVNAME(zr), __func__,
4194 4195 4196 4197 4198 4199 4200
			"FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
			"UPMD"[zr->v4l_buffers.buffer[frame].state],
			zr->v4l_pend_tail, zr->v4l_pend_head);
		/* Process is the one capturing? */
		if (fh->v4l_buffers.active != ZORAN_FREE &&
		    /* Buffer ready to DQBUF? */
		    zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
L
Linus Torvalds 已提交
4201
			res = POLLIN | POLLRDNORM;
4202 4203
		spin_unlock_irqrestore(&zr->spinlock, flags);

L
Linus Torvalds 已提交
4204 4205 4206 4207
		break;

	case ZORAN_MAP_MODE_JPG_REC:
	case ZORAN_MAP_MODE_JPG_PLAY:
4208
		poll_wait(file, &zr->jpg_capq, wait);
L
Linus Torvalds 已提交
4209
		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
4210 4211 4212 4213 4214

		spin_lock_irqsave(&zr->spinlock, flags);
		dprintk(3,
			KERN_DEBUG
			"%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
4215
			ZR_DEVNAME(zr), __func__,
4216 4217 4218 4219 4220
			"FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
			"UPMD"[zr->jpg_buffers.buffer[frame].state],
			zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
		if (fh->jpg_buffers.active != ZORAN_FREE &&
		    zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
L
Linus Torvalds 已提交
4221 4222 4223 4224 4225
			if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
				res = POLLIN | POLLRDNORM;
			else
				res = POLLOUT | POLLWRNORM;
		}
4226 4227
		spin_unlock_irqrestore(&zr->spinlock, flags);

L
Linus Torvalds 已提交
4228 4229 4230 4231
		break;

	default:
		dprintk(1,
4232
			KERN_ERR
L
Linus Torvalds 已提交
4233 4234 4235 4236 4237
			"%s: zoran_poll() - internal error, unknown map_mode=%d\n",
			ZR_DEVNAME(zr), fh->map_mode);
		res = POLLNVAL;
	}

I
Ingo Molnar 已提交
4238
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293

	return res;
}


/*
 * This maps the buffers to user space.
 *
 * Depending on the state of fh->map_mode
 * the V4L or the MJPEG buffers are mapped
 * per buffer or all together
 *
 * Note that we need to connect to some
 * unmap signal event to unmap the de-allocate
 * the buffer accordingly (zoran_vm_close())
 */

static void
zoran_vm_open (struct vm_area_struct *vma)
{
	struct zoran_mapping *map = vma->vm_private_data;

	map->count++;
}

static void
zoran_vm_close (struct vm_area_struct *vma)
{
	struct zoran_mapping *map = vma->vm_private_data;
	struct file *file = map->file;
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	int i;

	map->count--;
	if (map->count == 0) {
		switch (fh->map_mode) {
		case ZORAN_MAP_MODE_JPG_REC:
		case ZORAN_MAP_MODE_JPG_PLAY:

			dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
				ZR_DEVNAME(zr));

			for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
				if (fh->jpg_buffers.buffer[i].map == map) {
					fh->jpg_buffers.buffer[i].map =
					    NULL;
				}
			}
			kfree(map);

			for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
				if (fh->jpg_buffers.buffer[i].map)
					break;
			if (i == fh->jpg_buffers.num_buffers) {
I
Ingo Molnar 已提交
4294
				mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306

				if (fh->jpg_buffers.active != ZORAN_FREE) {
					jpg_qbuf(file, -1, zr->codec_mode);
					zr->jpg_buffers.allocated = 0;
					zr->jpg_buffers.active =
					    fh->jpg_buffers.active =
					    ZORAN_FREE;
				}
				//jpg_fbuffer_free(file);
				fh->jpg_buffers.allocated = 0;
				fh->jpg_buffers.ready_to_be_freed = 1;

I
Ingo Molnar 已提交
4307
				mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
			}

			break;

		case ZORAN_MAP_MODE_RAW:

			dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
				ZR_DEVNAME(zr));

			for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
				if (fh->v4l_buffers.buffer[i].map == map) {
					/* unqueue/unmap */
					fh->v4l_buffers.buffer[i].map =
					    NULL;
				}
			}
			kfree(map);

			for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
				if (fh->v4l_buffers.buffer[i].map)
					break;
			if (i == fh->v4l_buffers.num_buffers) {
I
Ingo Molnar 已提交
4330
				mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4331 4332

				if (fh->v4l_buffers.active != ZORAN_FREE) {
4333
					unsigned long flags;
4334 4335

					spin_lock_irqsave(&zr->spinlock, flags);
L
Linus Torvalds 已提交
4336 4337 4338 4339 4340
					zr36057_set_memgrab(zr, 0);
					zr->v4l_buffers.allocated = 0;
					zr->v4l_buffers.active =
					    fh->v4l_buffers.active =
					    ZORAN_FREE;
4341
					spin_unlock_irqrestore(&zr->spinlock, flags);
L
Linus Torvalds 已提交
4342 4343 4344 4345 4346
				}
				//v4l_fbuffer_free(file);
				fh->v4l_buffers.allocated = 0;
				fh->v4l_buffers.ready_to_be_freed = 1;

I
Ingo Molnar 已提交
4347
				mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401
			}

			break;

		default:
			printk(KERN_ERR
			       "%s: munmap() - internal error - unknown map mode %d\n",
			       ZR_DEVNAME(zr), fh->map_mode);
			break;

		}
	}
}

static struct vm_operations_struct zoran_vm_ops = {
	.open = zoran_vm_open,
	.close = zoran_vm_close,
};

static int
zoran_mmap (struct file           *file,
	    struct vm_area_struct *vma)
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;
	unsigned long size = (vma->vm_end - vma->vm_start);
	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
	int i, j;
	unsigned long page, start = vma->vm_start, todo, pos, fraglen;
	int first, last;
	struct zoran_mapping *map;
	int res = 0;

	dprintk(3,
		KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
		ZR_DEVNAME(zr),
		fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
		vma->vm_start, vma->vm_end, size);

	if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
	    !(vma->vm_flags & VM_WRITE)) {
		dprintk(1,
			KERN_ERR
			"%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
			ZR_DEVNAME(zr));
		return -EINVAL;
	}

	switch (fh->map_mode) {

	case ZORAN_MAP_MODE_JPG_REC:
	case ZORAN_MAP_MODE_JPG_PLAY:

		/* lock */
I
Ingo Molnar 已提交
4402
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463

		/* Map the MJPEG buffers */
		if (!fh->jpg_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
				ZR_DEVNAME(zr));
			res = -ENOMEM;
			goto jpg_mmap_unlock_and_return;
		}

		first = offset / fh->jpg_buffers.buffer_size;
		last = first - 1 + size / fh->jpg_buffers.buffer_size;
		if (offset % fh->jpg_buffers.buffer_size != 0 ||
		    size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
		    last < 0 || first >= fh->jpg_buffers.num_buffers ||
		    last >= fh->jpg_buffers.num_buffers) {
			dprintk(1,
				KERN_ERR
				"%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
				ZR_DEVNAME(zr), offset, size,
				fh->jpg_buffers.buffer_size,
				fh->jpg_buffers.num_buffers);
			res = -EINVAL;
			goto jpg_mmap_unlock_and_return;
		}
		for (i = first; i <= last; i++) {
			if (fh->jpg_buffers.buffer[i].map) {
				dprintk(1,
					KERN_ERR
					"%s: mmap(MJPEG) - buffer %d already mapped\n",
					ZR_DEVNAME(zr), i);
				res = -EBUSY;
				goto jpg_mmap_unlock_and_return;
			}
		}

		/* map these buffers (v4l_buffers[i]) */
		map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
		if (!map) {
			res = -ENOMEM;
			goto jpg_mmap_unlock_and_return;
		}
		map->file = file;
		map->count = 1;

		vma->vm_ops = &zoran_vm_ops;
		vma->vm_flags |= VM_DONTEXPAND;
		vma->vm_private_data = map;

		for (i = first; i <= last; i++) {
			for (j = 0;
			     j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
			     j++) {
				fraglen =
				    (le32_to_cpu(fh->jpg_buffers.buffer[i].
				     frag_tab[2 * j + 1]) & ~1) << 1;
				todo = size;
				if (todo > fraglen)
					todo = fraglen;
				pos =
4464
				    le32_to_cpu(fh->jpg_buffers.
L
Linus Torvalds 已提交
4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491
				    buffer[i].frag_tab[2 * j]);
				/* should just be pos on i386 */
				page = virt_to_phys(bus_to_virt(pos))
								>> PAGE_SHIFT;
				if (remap_pfn_range(vma, start, page,
							todo, PAGE_SHARED)) {
					dprintk(1,
						KERN_ERR
						"%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
						ZR_DEVNAME(zr));
					res = -EAGAIN;
					goto jpg_mmap_unlock_and_return;
				}
				size -= todo;
				start += todo;
				if (size == 0)
					break;
				if (le32_to_cpu(fh->jpg_buffers.buffer[i].
				    frag_tab[2 * j + 1]) & 1)
					break;	/* was last fragment */
			}
			fh->jpg_buffers.buffer[i].map = map;
			if (size == 0)
				break;

		}
	jpg_mmap_unlock_and_return:
I
Ingo Molnar 已提交
4492
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4493 4494 4495 4496 4497

		break;

	case ZORAN_MAP_MODE_RAW:

I
Ingo Molnar 已提交
4498
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569

		/* Map the V4L buffers */
		if (!fh->v4l_buffers.allocated) {
			dprintk(1,
				KERN_ERR
				"%s: zoran_mmap(V4L) - buffers not yet allocated\n",
				ZR_DEVNAME(zr));
			res = -ENOMEM;
			goto v4l_mmap_unlock_and_return;
		}

		first = offset / fh->v4l_buffers.buffer_size;
		last = first - 1 + size / fh->v4l_buffers.buffer_size;
		if (offset % fh->v4l_buffers.buffer_size != 0 ||
		    size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
		    last < 0 || first >= fh->v4l_buffers.num_buffers ||
		    last >= fh->v4l_buffers.buffer_size) {
			dprintk(1,
				KERN_ERR
				"%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
				ZR_DEVNAME(zr), offset, size,
				fh->v4l_buffers.buffer_size,
				fh->v4l_buffers.num_buffers);
			res = -EINVAL;
			goto v4l_mmap_unlock_and_return;
		}
		for (i = first; i <= last; i++) {
			if (fh->v4l_buffers.buffer[i].map) {
				dprintk(1,
					KERN_ERR
					"%s: mmap(V4L) - buffer %d already mapped\n",
					ZR_DEVNAME(zr), i);
				res = -EBUSY;
				goto v4l_mmap_unlock_and_return;
			}
		}

		/* map these buffers (v4l_buffers[i]) */
		map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
		if (!map) {
			res = -ENOMEM;
			goto v4l_mmap_unlock_and_return;
		}
		map->file = file;
		map->count = 1;

		vma->vm_ops = &zoran_vm_ops;
		vma->vm_flags |= VM_DONTEXPAND;
		vma->vm_private_data = map;

		for (i = first; i <= last; i++) {
			todo = size;
			if (todo > fh->v4l_buffers.buffer_size)
				todo = fh->v4l_buffers.buffer_size;
			page = fh->v4l_buffers.buffer[i].fbuffer_phys;
			if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
							todo, PAGE_SHARED)) {
				dprintk(1,
					KERN_ERR
					"%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
					ZR_DEVNAME(zr));
				res = -EAGAIN;
				goto v4l_mmap_unlock_and_return;
			}
			size -= todo;
			start += todo;
			fh->v4l_buffers.buffer[i].map = map;
			if (size == 0)
				break;
		}
	v4l_mmap_unlock_and_return:
I
Ingo Molnar 已提交
4570
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584

		break;

	default:
		dprintk(1,
			KERN_ERR
			"%s: zoran_mmap() - internal error - unknown map mode %d\n",
			ZR_DEVNAME(zr), fh->map_mode);
		break;
	}

	return 0;
}

4585
static const struct v4l2_file_operations zoran_fops = {
L
Linus Torvalds 已提交
4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
	.owner = THIS_MODULE,
	.open = zoran_open,
	.release = zoran_close,
	.ioctl = zoran_ioctl,
	.read = zoran_read,
	.write = zoran_write,
	.mmap = zoran_mmap,
	.poll = zoran_poll,
};

struct video_device zoran_template __devinitdata = {
	.name = ZORAN_NAME,
	.fops = &zoran_fops,
	.release = &zoran_vdev_release,
	.minor = -1
};