zoran_driver.c 87.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
/*
 * 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>

62
#include <linux/videodev.h>
63
#include <media/v4l2-common.h>
64
#include <media/v4l2-ioctl.h>
L
Linus Torvalds 已提交
65 66
#include "videocodec.h"

67
#include <asm/byteorder.h>
L
Linus Torvalds 已提交
68 69 70 71
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>

I
Ingo Molnar 已提交
72
#include <linux/mutex.h>
L
Linus Torvalds 已提交
73 74 75 76
#include "zoran.h"
#include "zoran_device.h"
#include "zoran_card.h"

77 78 79 80

const struct zoran_format zoran_formats[] = {
	{
		.name = "15-bit RGB LE",
81 82
		.fourcc = V4L2_PIX_FMT_RGB555,
		.colorspace = V4L2_COLORSPACE_SRGB,
L
Linus Torvalds 已提交
83 84 85
		.depth = 15,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
86 87
		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
			   ZR36057_VFESPFR_LittleEndian,
L
Linus Torvalds 已提交
88
	}, {
89
		.name = "15-bit RGB BE",
90 91
		.fourcc = V4L2_PIX_FMT_RGB555X,
		.colorspace = V4L2_COLORSPACE_SRGB,
92 93 94 95 96 97
		.depth = 15,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
	}, {
		.name = "16-bit RGB LE",
98 99
		.fourcc = V4L2_PIX_FMT_RGB565,
		.colorspace = V4L2_COLORSPACE_SRGB,
L
Linus Torvalds 已提交
100 101 102
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
103 104 105 106
		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
			   ZR36057_VFESPFR_LittleEndian,
	}, {
		.name = "16-bit RGB BE",
107 108
		.fourcc = V4L2_PIX_FMT_RGB565X,
		.colorspace = V4L2_COLORSPACE_SRGB,
109 110 111 112
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
L
Linus Torvalds 已提交
113 114
	}, {
		.name = "24-bit RGB",
115 116
		.fourcc = V4L2_PIX_FMT_BGR24,
		.colorspace = V4L2_COLORSPACE_SRGB,
L
Linus Torvalds 已提交
117 118 119
		.depth = 24,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
120
		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
L
Linus Torvalds 已提交
121
	}, {
122
		.name = "32-bit RGB LE",
123 124
		.fourcc = V4L2_PIX_FMT_BGR32,
		.colorspace = V4L2_COLORSPACE_SRGB,
L
Linus Torvalds 已提交
125 126 127
		.depth = 32,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
128 129 130
		.vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
	}, {
		.name = "32-bit RGB BE",
131 132
		.fourcc = V4L2_PIX_FMT_RGB32,
		.colorspace = V4L2_COLORSPACE_SRGB,
133 134 135 136
		.depth = 32,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_RGB888,
L
Linus Torvalds 已提交
137 138
	}, {
		.name = "4:2:2, packed, YUYV",
139 140
		.fourcc = V4L2_PIX_FMT_YUYV,
		.colorspace = V4L2_COLORSPACE_SMPTE170M,
L
Linus Torvalds 已提交
141 142
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
143
			 ZORAN_FORMAT_OVERLAY,
144 145 146
		.vfespfr = ZR36057_VFESPFR_YUV422,
	}, {
		.name = "4:2:2, packed, UYVY",
147 148
		.fourcc = V4L2_PIX_FMT_UYVY,
		.colorspace = V4L2_COLORSPACE_SMPTE170M,
149 150 151 152
		.depth = 16,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_OVERLAY,
		.vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
L
Linus Torvalds 已提交
153 154
	}, {
		.name = "Hardware-encoded Motion-JPEG",
155 156
		.fourcc = V4L2_PIX_FMT_MJPEG,
		.colorspace = V4L2_COLORSPACE_SMPTE170M,
L
Linus Torvalds 已提交
157 158 159 160 161 162
		.depth = 0,
		.flags = ZORAN_FORMAT_CAPTURE |
			 ZORAN_FORMAT_PLAYBACK |
			 ZORAN_FORMAT_COMPRESSED,
	}
};
163
#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

	/* 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 */
189 190
static void v4l_fbuffer_free(struct zoran_fh *fh);
static void jpg_fbuffer_free(struct zoran_fh *fh);
L
Linus Torvalds 已提交
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/* Set mapping mode */
static void map_mode_raw(struct zoran_fh *fh)
{
	fh->map_mode = ZORAN_MAP_MODE_RAW;
	fh->buffers.buffer_size = v4l_bufsize;
	fh->buffers.num_buffers = v4l_nbufs;
}
static void map_mode_jpg(struct zoran_fh *fh, int play)
{
	fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
	fh->buffers.buffer_size = jpg_bufsize;
	fh->buffers.num_buffers = jpg_nbufs;
}
static inline const char *mode_name(enum zoran_map_mode mode)
{
	return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
}

L
Linus Torvalds 已提交
210 211 212 213 214 215
/*
 *   Allocate the V4L grab buffers
 *
 *   These have to be pysically contiguous.
 */

216
static int v4l_fbuffer_alloc(struct zoran_fh *fh)
L
Linus Torvalds 已提交
217 218 219 220 221
{
	struct zoran *zr = fh->zr;
	int i, off;
	unsigned char *mem;

222 223
	for (i = 0; i < fh->buffers.num_buffers; i++) {
		if (fh->buffers.buffer[i].v4l.fbuffer)
L
Linus Torvalds 已提交
224 225
			dprintk(2,
				KERN_WARNING
226 227
				"%s: %s - buffer %d already allocated!?\n",
				ZR_DEVNAME(zr), __func__, i);
L
Linus Torvalds 已提交
228 229

		//udelay(20);
230 231
		mem = kmalloc(fh->buffers.buffer_size,
			      GFP_KERNEL | __GFP_NOWARN);
232 233 234
		if (!mem) {
			dprintk(1,
				KERN_ERR
235 236
				"%s: %s - kmalloc for V4L buf %d failed\n",
				ZR_DEVNAME(zr), __func__, i);
237
			v4l_fbuffer_free(fh);
238
			return -ENOBUFS;
L
Linus Torvalds 已提交
239
		}
240 241 242 243
		fh->buffers.buffer[i].v4l.fbuffer = mem;
		fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
		fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
		for (off = 0; off < fh->buffers.buffer_size;
244
		     off += PAGE_SIZE)
245
			SetPageReserved(virt_to_page(mem + off));
246 247
		dprintk(4,
			KERN_INFO
248
			"%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
249
			ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
250
			(unsigned long long)virt_to_bus(mem));
L
Linus Torvalds 已提交
251 252
	}

253
	fh->buffers.allocated = 1;
L
Linus Torvalds 已提交
254 255 256 257 258

	return 0;
}

/* free the V4L grab buffers */
259
static void v4l_fbuffer_free(struct zoran_fh *fh)
L
Linus Torvalds 已提交
260 261 262 263 264
{
	struct zoran *zr = fh->zr;
	int i, off;
	unsigned char *mem;

265
	dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
266

267 268
	for (i = 0; i < fh->buffers.num_buffers; i++) {
		if (!fh->buffers.buffer[i].v4l.fbuffer)
L
Linus Torvalds 已提交
269 270
			continue;

271 272
		mem = fh->buffers.buffer[i].v4l.fbuffer;
		for (off = 0; off < fh->buffers.buffer_size;
273
		     off += PAGE_SIZE)
274
			ClearPageReserved(virt_to_page(mem + off));
275 276
		kfree(fh->buffers.buffer[i].v4l.fbuffer);
		fh->buffers.buffer[i].v4l.fbuffer = NULL;
L
Linus Torvalds 已提交
277 278
	}

279
	fh->buffers.allocated = 0;
L
Linus Torvalds 已提交
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
}

/*
 *   Allocate the MJPEG grab buffers.
 *
 *   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)
 *
 *   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.
 */

310
static int jpg_fbuffer_alloc(struct zoran_fh *fh)
L
Linus Torvalds 已提交
311 312 313
{
	struct zoran *zr = fh->zr;
	int i, j, off;
314
	u8 *mem;
L
Linus Torvalds 已提交
315

316 317
	for (i = 0; i < fh->buffers.num_buffers; i++) {
		if (fh->buffers.buffer[i].jpg.frag_tab)
L
Linus Torvalds 已提交
318 319
			dprintk(2,
				KERN_WARNING
320 321
				"%s: %s - buffer %d already allocated!?\n",
				ZR_DEVNAME(zr), __func__, i);
L
Linus Torvalds 已提交
322 323 324

		/* Allocate fragment table for this buffer */

325
		mem = (void *)get_zeroed_page(GFP_KERNEL);
L
Linus Torvalds 已提交
326 327 328
		if (mem == 0) {
			dprintk(1,
				KERN_ERR
329 330
				"%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
				ZR_DEVNAME(zr), __func__, i);
331
			jpg_fbuffer_free(fh);
L
Linus Torvalds 已提交
332 333
			return -ENOBUFS;
		}
334 335 336 337 338 339
		fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
		fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);

		if (fh->buffers.need_contiguous) {
			mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
			if (mem == NULL) {
L
Linus Torvalds 已提交
340 341
				dprintk(1,
					KERN_ERR
342 343
					"%s: %s - kmalloc failed for buffer %d\n",
					ZR_DEVNAME(zr), __func__, i);
344
				jpg_fbuffer_free(fh);
L
Linus Torvalds 已提交
345 346
				return -ENOBUFS;
			}
347 348 349 350 351
			fh->buffers.buffer[i].jpg.frag_tab[0] =
				cpu_to_le32(virt_to_bus(mem));
			fh->buffers.buffer[i].jpg.frag_tab[1] =
				cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
			for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
352
				SetPageReserved(virt_to_page(mem + off));
L
Linus Torvalds 已提交
353
		} else {
354
			/* jpg_bufsize is already page aligned */
355 356 357
			for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
				mem = (void *)get_zeroed_page(GFP_KERNEL);
				if (mem == NULL) {
L
Linus Torvalds 已提交
358 359
					dprintk(1,
						KERN_ERR
360 361
						"%s: %s - get_zeroed_page failed for buffer %d\n",
						ZR_DEVNAME(zr), __func__, i);
362
					jpg_fbuffer_free(fh);
L
Linus Torvalds 已提交
363 364 365
					return -ENOBUFS;
				}

366 367 368 369
				fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
					cpu_to_le32(virt_to_bus(mem));
				fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
					cpu_to_le32((PAGE_SIZE >> 2) << 1);
370
				SetPageReserved(virt_to_page(mem));
L
Linus Torvalds 已提交
371 372
			}

373
			fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
L
Linus Torvalds 已提交
374 375 376 377
		}
	}

	dprintk(4,
378 379
		KERN_DEBUG "%s: %s - %d KB allocated\n",
		ZR_DEVNAME(zr), __func__,
380
		(fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
L
Linus Torvalds 已提交
381

382
	fh->buffers.allocated = 1;
L
Linus Torvalds 已提交
383 384 385 386 387

	return 0;
}

/* free the MJPEG grab buffers */
388
static void jpg_fbuffer_free(struct zoran_fh *fh)
L
Linus Torvalds 已提交
389 390 391 392
{
	struct zoran *zr = fh->zr;
	int i, j, off;
	unsigned char *mem;
393
	__le32 frag_tab;
394
	struct zoran_buffer *buffer;
L
Linus Torvalds 已提交
395

396
	dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
397

398 399 400
	for (i = 0, buffer = &fh->buffers.buffer[0];
	     i < fh->buffers.num_buffers; i++, buffer++) {
		if (!buffer->jpg.frag_tab)
L
Linus Torvalds 已提交
401 402
			continue;

403 404
		if (fh->buffers.need_contiguous) {
			frag_tab = buffer->jpg.frag_tab[0];
405 406

			if (frag_tab) {
407 408
				mem = bus_to_virt(le32_to_cpu(frag_tab));
				for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
409
					ClearPageReserved(virt_to_page(mem + off));
410
				kfree(mem);
411 412
				buffer->jpg.frag_tab[0] = 0;
				buffer->jpg.frag_tab[1] = 0;
L
Linus Torvalds 已提交
413 414
			}
		} else {
415 416
			for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
				frag_tab = buffer->jpg.frag_tab[2 * j];
417 418

				if (!frag_tab)
L
Linus Torvalds 已提交
419
					break;
420 421
				ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
				free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
422 423
				buffer->jpg.frag_tab[2 * j] = 0;
				buffer->jpg.frag_tab[2 * j + 1] = 0;
L
Linus Torvalds 已提交
424 425 426
			}
		}

427 428
		free_page((unsigned long)buffer->jpg.frag_tab);
		buffer->jpg.frag_tab = NULL;
L
Linus Torvalds 已提交
429 430
	}

431
	fh->buffers.allocated = 0;
L
Linus Torvalds 已提交
432 433 434 435 436 437 438
}

/*
 *   V4L Buffer grabbing
 */

static int
439
zoran_v4l_set_format (struct zoran_fh           *fh,
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452
		      int                        width,
		      int                        height,
		      const struct zoran_format *format)
{
	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
453 454
			"%s: %s - wrong frame size (%dx%d)\n",
			ZR_DEVNAME(zr), __func__, width, height);
L
Linus Torvalds 已提交
455 456 457 458 459 460
		return -EINVAL;
	}

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

	/* Check against available buffer size */
461
	if (height * width * bpp > fh->buffers.buffer_size) {
L
Linus Torvalds 已提交
462 463
		dprintk(1,
			KERN_ERR
464 465
			"%s: %s - video buffer size (%d kB) is too small\n",
			ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473
		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
474 475
			"%s: %s - wrong frame alignment\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
476 477 478 479 480 481 482 483 484 485 486
		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;
}

487
static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
L
Linus Torvalds 已提交
488 489 490 491 492
{
	struct zoran *zr = fh->zr;
	unsigned long flags;
	int res = 0;

493
	if (!fh->buffers.allocated) {
L
Linus Torvalds 已提交
494 495
		dprintk(1,
			KERN_ERR
496 497
			"%s: %s - buffers not yet allocated\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
498 499 500 501
		res = -ENOMEM;
	}

	/* No grabbing outside the buffer range! */
502
	if (num >= fh->buffers.num_buffers || num < 0) {
L
Linus Torvalds 已提交
503 504
		dprintk(1,
			KERN_ERR
505 506
			"%s: %s - buffer %d is out of range\n",
			ZR_DEVNAME(zr), __func__, num);
L
Linus Torvalds 已提交
507 508 509 510 511
		res = -EINVAL;
	}

	spin_lock_irqsave(&zr->spinlock, flags);

512
	if (fh->buffers.active == ZORAN_FREE) {
L
Linus Torvalds 已提交
513
		if (zr->v4l_buffers.active == ZORAN_FREE) {
514 515
			zr->v4l_buffers = fh->buffers;
			fh->buffers.active = ZORAN_ACTIVE;
L
Linus Torvalds 已提交
516 517 518
		} else {
			dprintk(1,
				KERN_ERR
519 520
				"%s: %s - another session is already capturing\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530
			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) {
531
				fh->buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
532 533 534 535 536 537 538
				zr->v4l_buffers.allocated = 0;
			}
			res = -EBUSY;	/* what are you doing? */
			break;
		case BUZ_STATE_DONE:
			dprintk(2,
				KERN_WARNING
539 540
				"%s: %s - queueing buffer %d in state DONE!?\n",
				ZR_DEVNAME(zr), __func__, num);
L
Linus Torvalds 已提交
541 542 543
		case BUZ_STATE_USER:
			/* since there is at least one unused buffer there's room for at least
			 * one more pend[] entry */
544
			zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
L
Linus Torvalds 已提交
545 546 547 548
			zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
			zr->v4l_buffers.buffer[num].bs.length =
			    fh->v4l_settings.bytesperline *
			    zr->v4l_settings.height;
549
			fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
L
Linus Torvalds 已提交
550 551 552 553 554 555 556
			break;
		}
	}

	spin_unlock_irqrestore(&zr->spinlock, flags);

	if (!res && zr->v4l_buffers.active == ZORAN_FREE)
557
		zr->v4l_buffers.active = fh->buffers.active;
L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565

	return res;
}

/*
 * Sync on a V4L buffer
 */

566
static int v4l_sync(struct zoran_fh *fh, int frame)
L
Linus Torvalds 已提交
567 568 569 570
{
	struct zoran *zr = fh->zr;
	unsigned long flags;

571
	if (fh->buffers.active == ZORAN_FREE) {
L
Linus Torvalds 已提交
572 573
		dprintk(1,
			KERN_ERR
574 575
			"%s: %s - no grab active for this session\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
576 577 578 579
		return -EINVAL;
	}

	/* check passed-in frame number */
580
	if (frame >= fh->buffers.num_buffers || frame < 0) {
L
Linus Torvalds 已提交
581
		dprintk(1,
582 583
			KERN_ERR "%s: %s - frame %d is invalid\n",
			ZR_DEVNAME(zr), __func__, frame);
L
Linus Torvalds 已提交
584 585 586 587 588 589 590
		return -EINVAL;
	}

	/* Check if is buffer was queued at all */
	if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
		dprintk(1,
			KERN_ERR
591 592
			"%s: %s - attempt to sync on a buffer which was not queued?\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
593 594 595 596 597
		return -EPROTO;
	}

	/* wait on this buffer to get ready */
	if (!wait_event_interruptible_timeout(zr->v4l_capq,
598
		(zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
L
Linus Torvalds 已提交
599 600 601 602 603 604 605
		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,
606 607
			KERN_ERR "%s: %s - internal state error\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
608 609

	zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
610
	fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
L
Linus Torvalds 已提交
611 612 613 614 615 616 617

	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) {
618
			fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631
			zr->v4l_buffers.allocated = 0;
		}
	}

	spin_unlock_irqrestore(&zr->spinlock, flags);

	return 0;
}

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

632 633
static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
				 enum zoran_codec_mode mode)
L
Linus Torvalds 已提交
634 635 636 637 638 639
{
	struct zoran *zr = fh->zr;
	unsigned long flags;
	int res = 0;

	/* Check if buffers are allocated */
640
	if (!fh->buffers.allocated) {
L
Linus Torvalds 已提交
641 642
		dprintk(1,
			KERN_ERR
643 644
			"%s: %s - buffers not yet allocated\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
645 646 647 648
		return -ENOMEM;
	}

	/* No grabbing outside the buffer range! */
649
	if (num >= fh->buffers.num_buffers || num < 0) {
L
Linus Torvalds 已提交
650 651
		dprintk(1,
			KERN_ERR
652 653
			"%s: %s - buffer %d out of range\n",
			ZR_DEVNAME(zr), __func__, num);
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663
		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
664 665
			"%s: %s - codec in wrong mode\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
666 667 668
		return -EINVAL;
	}

669
	if (fh->buffers.active == ZORAN_FREE) {
L
Linus Torvalds 已提交
670
		if (zr->jpg_buffers.active == ZORAN_FREE) {
671 672
			zr->jpg_buffers = fh->buffers;
			fh->buffers.active = ZORAN_ACTIVE;
L
Linus Torvalds 已提交
673 674 675
		} else {
			dprintk(1,
				KERN_ERR
676 677
				"%s: %s - another session is already capturing\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686
			res = -EBUSY;
		}
	}

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

687 688
	spin_lock_irqsave(&zr->spinlock, flags);

L
Linus Torvalds 已提交
689 690 691 692 693
	if (!res) {
		switch (zr->jpg_buffers.buffer[num].state) {
		case BUZ_STATE_DONE:
			dprintk(2,
				KERN_WARNING
694 695
				"%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
696 697 698
		case BUZ_STATE_USER:
			/* since there is at least one unused buffer there's room for at
			 *least one more pend[] entry */
699
			zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
L
Linus Torvalds 已提交
700
			zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
701
			fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
L
Linus Torvalds 已提交
702 703 704 705 706 707
			zoran_feed_stat_com(zr);
			break;
		default:
		case BUZ_STATE_DMA:
		case BUZ_STATE_PEND:
			if (zr->jpg_buffers.active == ZORAN_FREE) {
708
				fh->buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716 717
				zr->jpg_buffers.allocated = 0;
			}
			res = -EBUSY;	/* what are you doing? */
			break;
		}
	}

	spin_unlock_irqrestore(&zr->spinlock, flags);

718 719
	if (!res && zr->jpg_buffers.active == ZORAN_FREE)
		zr->jpg_buffers.active = fh->buffers.active;
L
Linus Torvalds 已提交
720 721 722 723

	return res;
}

724
static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
L
Linus Torvalds 已提交
725 726 727 728 729 730 731
{
	struct zoran *zr = fh->zr;
	int res = 0;

	/* Does the user want to stop streaming? */
	if (frame < 0) {
		if (zr->codec_mode == mode) {
732
			if (fh->buffers.active == ZORAN_FREE) {
L
Linus Torvalds 已提交
733 734
				dprintk(1,
					KERN_ERR
735 736
					"%s: %s(-1) - session not active\n",
					ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
737 738
				return -EINVAL;
			}
739
			fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
740 741 742 743 744 745
			zr->jpg_buffers.allocated = 0;
			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
			return 0;
		} else {
			dprintk(1,
				KERN_ERR
746 747
				"%s: %s - stop streaming but not in streaming mode\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
748 749 750 751
			return -EINVAL;
		}
	}

752
	if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
L
Linus Torvalds 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765
		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
 */

766
static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
L
Linus Torvalds 已提交
767 768 769 770 771
{
	struct zoran *zr = fh->zr;
	unsigned long flags;
	int frame;

772
	if (fh->buffers.active == ZORAN_FREE) {
L
Linus Torvalds 已提交
773 774
		dprintk(1,
			KERN_ERR
775 776
			"%s: %s - capture is not currently active\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
777 778 779 780 781 782
		return -EINVAL;
	}
	if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
	    zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
		dprintk(1,
			KERN_ERR
783 784
			"%s: %s - codec not in streaming mode\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798
		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
799 800
			"%s: %s - timeout: codec isr=0x%02x\n",
			ZR_DEVNAME(zr), __func__, isr);
L
Linus Torvalds 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

		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 */
816 817
	if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
		dprintk(2,
818 819
			KERN_ERR "%s: %s - internal state error\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
820 821 822 823

	*bs = zr->jpg_buffers.buffer[frame].bs;
	bs->frame = frame;
	zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
824
	fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
L
Linus Torvalds 已提交
825 826 827 828 829 830

	spin_unlock_irqrestore(&zr->spinlock, flags);

	return 0;
}

831
static void zoran_open_init_session(struct zoran_fh *fh)
L
Linus Torvalds 已提交
832 833 834 835 836
{
	int i;
	struct zoran *zr = fh->zr;

	/* Per default, map the V4L Buffers */
837
	map_mode_raw(fh);
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848 849

	/* 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;
	/* jpg settings */
	fh->jpg_settings = zr->jpg_settings;

850 851 852 853 854
	/* buffers */
	memset(&fh->buffers, 0, sizeof(fh->buffers));
	for (i = 0; i < MAX_FRAME; i++) {
		fh->buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
		fh->buffers.buffer[i].bs.frame = i;
L
Linus Torvalds 已提交
855
	}
856 857
	fh->buffers.allocated = 0;
	fh->buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
858 859
}

860
static void zoran_close_end_session(struct zoran_fh *fh)
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872
{
	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;
	}

873 874 875 876
	if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
		/* v4l capture */
		if (fh->buffers.active != ZORAN_FREE) {
			unsigned long flags;
877

878 879 880 881 882 883
			spin_lock_irqsave(&zr->spinlock, flags);
			zr36057_set_memgrab(zr, 0);
			zr->v4l_buffers.allocated = 0;
			zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
			spin_unlock_irqrestore(&zr->spinlock, flags);
		}
L
Linus Torvalds 已提交
884

885 886
		/* v4l buffers */
		if (fh->buffers.allocated)
887
			v4l_fbuffer_free(fh);
888 889 890 891 892 893 894
	} else {
		/* jpg capture */
		if (fh->buffers.active != ZORAN_FREE) {
			zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
			zr->jpg_buffers.allocated = 0;
			zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
		}
L
Linus Torvalds 已提交
895

896 897
		/* jpg buffers */
		if (fh->buffers.allocated)
898
			jpg_fbuffer_free(fh);
L
Linus Torvalds 已提交
899 900 901 902 903 904 905
	}
}

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

906
static int zoran_open(struct file *file)
L
Linus Torvalds 已提交
907
{
908
	struct zoran *zr = video_drvdata(file);
L
Linus Torvalds 已提交
909
	struct zoran_fh *fh;
910 911
	int res, first_open = 0;

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

915
	lock_kernel();
L
Linus Torvalds 已提交
916

917 918 919 920 921 922 923
	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 已提交
924
	/* now, create the open()-specific file_ops struct */
P
 
Panagiotis Issaris 已提交
925
	fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
L
Linus Torvalds 已提交
926 927 928
	if (!fh) {
		dprintk(1,
			KERN_ERR
929 930
			"%s: %s - allocation of zoran_fh failed\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
931
		res = -ENOMEM;
932
		goto fail_unlock;
L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940
	}
	/* 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
941 942
			"%s: %s - allocation of overlay_mask failed\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
943
		res = -ENOMEM;
944
		goto fail_fh;
L
Linus Torvalds 已提交
945 946 947 948 949
	}

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

I
Ingo Molnar 已提交
950
	/*mutex_unlock(&zr->resource_lock);*/
L
Linus Torvalds 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963

	/* 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;
964
	zoran_open_init_session(fh);
965
	unlock_kernel();
L
Linus Torvalds 已提交
966 967 968

	return 0;

969 970 971
fail_fh:
	kfree(fh);
fail_unlock:
972
	unlock_kernel();
L
Linus Torvalds 已提交
973

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

L
Linus Torvalds 已提交
977 978 979 980
	return res;
}

static int
981
zoran_close(struct file  *file)
L
Linus Torvalds 已提交
982 983 984 985
{
	struct zoran_fh *fh = file->private_data;
	struct zoran *zr = fh->zr;

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

	/* kernel locks (fs/device.c), so don't do that ourselves
	 * (prevents deadlocks) */
I
Ingo Molnar 已提交
991
	/*mutex_lock(&zr->resource_lock);*/
L
Linus Torvalds 已提交
992

993
	zoran_close_end_session(fh);
L
Linus Torvalds 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004

	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);

1005
		if (zr36067_debug > 1)
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
			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 */
1021 1022
			struct v4l2_routing route = { 2, 0 };

1023 1024
			decoder_call(zr, video, s_stream, 0);
			encoder_call(zr, video, s_routing, &route);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030 1031
		}
	}

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

1032
	dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
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

	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;
}

1060
static int setup_fbuffer(struct zoran_fh *fh,
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	       void                      *base,
	       const struct zoran_format *fmt,
	       int                        width,
	       int                        height,
	       int                        bytesperline)
{
	struct zoran *zr = fh->zr;

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

1073 1074 1075 1076 1077 1078 1079
	/* 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 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	/* 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
1094 1095
			"%s: %s - forced overlay turnoff because framebuffer changed\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102
		zr36057_overlay(zr, 0);
	}
#endif

	if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
		dprintk(1,
			KERN_ERR
1103 1104
			"%s: %s - no valid overlay format given\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109
		return -EINVAL;
	}
	if (height <= 0 || width <= 0 || bytesperline <= 0) {
		dprintk(1,
			KERN_ERR
1110 1111
			"%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
			ZR_DEVNAME(zr), __func__, width, height, bytesperline);
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116
		return -EINVAL;
	}
	if (bytesperline & 3) {
		dprintk(1,
			KERN_ERR
1117 1118
			"%s: %s - bytesperline (%d) must be 4-byte aligned\n",
			ZR_DEVNAME(zr), __func__, bytesperline);
L
Linus Torvalds 已提交
1119 1120 1121
		return -EINVAL;
	}

1122 1123 1124 1125
	zr->vbuf_base = (void *) ((unsigned long) base & ~3);
	zr->vbuf_height = height;
	zr->vbuf_width = width;
	zr->vbuf_depth = fmt->depth;
L
Linus Torvalds 已提交
1126
	zr->overlay_settings.format = fmt;
1127
	zr->vbuf_bytesperline = bytesperline;
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133 1134 1135

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

	return 0;
}


1136 1137
static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
	struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
L
Linus Torvalds 已提交
1138 1139
{
	struct zoran *zr = fh->zr;
1140
	struct v4l2_clip *vcp = NULL;
L
Linus Torvalds 已提交
1141 1142 1143
	int on, end;


1144
	if (!zr->vbuf_base) {
L
Linus Torvalds 已提交
1145 1146
		dprintk(1,
			KERN_ERR
1147 1148
			"%s: %s - frame buffer has to be set first\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1149 1150 1151 1152 1153 1154
		return -EINVAL;
	}

	if (!fh->overlay_settings.format) {
		dprintk(1,
			KERN_ERR
1155 1156
			"%s: %s - no overlay format set\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163
		return -EINVAL;
	}

	/*
	 * The video front end needs 4-byte alinged line sizes, we correct that
	 * silently here if necessary
	 */
1164
	if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169
		end = (x + width) & ~1;	/* round down */
		x = (x + 1) & ~1;	/* round up */
		width = end - x;
	}

1170
	if (zr->vbuf_depth == 24) {
L
Linus Torvalds 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
		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
1186 1187
			"%s: %s - width = %d or height = %d invalid\n",
			ZR_DEVNAME(zr), __func__, width, height);
L
Linus Torvalds 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
		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 */
1225
		vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
L
Linus Torvalds 已提交
1226 1227 1228
		if (vcp == NULL) {
			dprintk(1,
				KERN_ERR
1229 1230
				"%s: %s - Alloc of clip mask failed\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1231 1232 1233
			return -ENOMEM;
		}
		if (copy_from_user
1234
		    (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
L
Linus Torvalds 已提交
1235 1236 1237
			vfree(vcp);
			return -EFAULT;
		}
1238
		write_overlay_mask(fh, vcp, clipcount);
L
Linus Torvalds 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
		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);
}

1254
static int setup_overlay(struct zoran_fh *fh, int on)
L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
{
	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
1268 1269
			"%s: %s - overlay is already active for another session\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1270 1271 1272 1273 1274 1275
		return -EBUSY;
	}
	if (!on && zr->overlay_active != ZORAN_FREE &&
	    fh->overlay_active == ZORAN_FREE) {
		dprintk(1,
			KERN_ERR
1276 1277
			"%s: %s - you cannot cancel someone else's session\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
		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 {
1290
		if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
L
Linus Torvalds 已提交
1291 1292
			dprintk(1,
				KERN_ERR
1293 1294
				"%s: %s - buffer or window not set\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299
			return -EINVAL;
		}
		if (!fh->overlay_settings.format) {
			dprintk(1,
				KERN_ERR
1300 1301
				"%s: %s - no overlay format set\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
			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);
}

1318 1319 1320
/* get the status of a buffer in the clients buffer queue */
static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
				    struct v4l2_buffer *buf, int num)
L
Linus Torvalds 已提交
1321 1322
{
	struct zoran *zr = fh->zr;
1323
	unsigned long flags;
L
Linus Torvalds 已提交
1324 1325 1326 1327 1328 1329

	buf->flags = V4L2_BUF_FLAG_MAPPED;

	switch (fh->map_mode) {
	case ZORAN_MAP_MODE_RAW:
		/* check range */
1330 1331
		if (num < 0 || num >= fh->buffers.num_buffers ||
		    !fh->buffers.allocated) {
L
Linus Torvalds 已提交
1332 1333
			dprintk(1,
				KERN_ERR
1334 1335
				"%s: %s - wrong number or buffers not allocated\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1336 1337 1338
			return -EINVAL;
		}

1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		spin_lock_irqsave(&zr->spinlock, flags);
		dprintk(3,
			KERN_DEBUG
			"%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
			ZR_DEVNAME(zr), __func__,
			"FAL"[fh->buffers.active], num,
			"UPMD"[zr->v4l_buffers.buffer[num].state],
			fh->buffers.buffer[num].map ? 'Y' : 'N');
		spin_unlock_irqrestore(&zr->spinlock, flags);

L
Linus Torvalds 已提交
1349
		buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1350
		buf->length = fh->buffers.buffer_size;
L
Linus Torvalds 已提交
1351 1352

		/* get buffer */
1353 1354 1355 1356
		buf->bytesused = fh->buffers.buffer[num].bs.length;
		if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
		    fh->buffers.buffer[num].state == BUZ_STATE_USER) {
			buf->sequence = fh->buffers.buffer[num].bs.seq;
L
Linus Torvalds 已提交
1357
			buf->flags |= V4L2_BUF_FLAG_DONE;
1358
			buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
		} 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 */
1374 1375
		if (num < 0 || num >= fh->buffers.num_buffers ||
		    !fh->buffers.allocated) {
L
Linus Torvalds 已提交
1376 1377
			dprintk(1,
				KERN_ERR
1378 1379
				"%s: %s - wrong number or buffers not allocated\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385
			return -EINVAL;
		}

		buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
			      V4L2_BUF_TYPE_VIDEO_CAPTURE :
			      V4L2_BUF_TYPE_VIDEO_OUTPUT;
1386
		buf->length = fh->buffers.buffer_size;
L
Linus Torvalds 已提交
1387 1388

		/* these variables are only written after frame has been captured */
1389 1390 1391 1392 1393
		if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
		    fh->buffers.buffer[num].state == BUZ_STATE_USER) {
			buf->sequence = fh->buffers.buffer[num].bs.seq;
			buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
			buf->bytesused = fh->buffers.buffer[num].bs.length;
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399 1400
			buf->flags |= V4L2_BUF_FLAG_DONE;
		} else {
			buf->flags |= V4L2_BUF_FLAG_QUEUED;
		}

		/* which fields are these? */
		if (fh->jpg_settings.TmpDcm != 1)
1401 1402
			buf->field = fh->jpg_settings.odd_even ?
				V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
L
Linus Torvalds 已提交
1403
		else
1404 1405
			buf->field = fh->jpg_settings.odd_even ?
				V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
L
Linus Torvalds 已提交
1406 1407 1408 1409 1410 1411 1412

		break;

	default:

		dprintk(5,
			KERN_ERR
1413 1414
			"%s: %s - invalid buffer type|map_mode (%d|%d)\n",
			ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
		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,
1427
		v4l2_std_id norm)
L
Linus Torvalds 已提交
1428
{
1429
	int on;
L
Linus Torvalds 已提交
1430 1431 1432 1433 1434

	if (zr->v4l_buffers.active != ZORAN_FREE ||
	    zr->jpg_buffers.active != ZORAN_FREE) {
		dprintk(1,
			KERN_WARNING
1435 1436
			"%s: %s called while in playback/capture mode\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1437 1438 1439
		return -EBUSY;
	}

1440
	if (!(norm & zr->card.norms)) {
L
Linus Torvalds 已提交
1441
		dprintk(1,
1442 1443
			KERN_ERR "%s: %s - unsupported norm %llx\n",
			ZR_DEVNAME(zr), __func__, norm);
L
Linus Torvalds 已提交
1444 1445 1446
		return -EINVAL;
	}

1447 1448 1449
	if (norm == V4L2_STD_ALL) {
		int status = 0;
		v4l2_std_id std = 0;
L
Linus Torvalds 已提交
1450

1451
		decoder_call(zr, video, querystd, &std);
1452
		decoder_call(zr, tuner, s_std, std);
L
Linus Torvalds 已提交
1453 1454 1455 1456

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

1457
		decoder_call(zr, video, g_input_status, &status);
1458
		if (status & V4L2_IN_ST_NO_SIGNAL) {
L
Linus Torvalds 已提交
1459 1460
			dprintk(1,
				KERN_ERR
1461 1462
				"%s: %s - no norm detected\n",
				ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1463
			/* reset norm */
1464
			decoder_call(zr, tuner, s_std, zr->norm);
L
Linus Torvalds 已提交
1465 1466 1467
			return -EIO;
		}

1468
		norm = std;
L
Linus Torvalds 已提交
1469
	}
1470 1471 1472 1473 1474 1475
	if (norm & V4L2_STD_SECAM)
		zr->timing = zr->card.tvn[2];
	else if (norm & V4L2_STD_NTSC)
		zr->timing = zr->card.tvn[1];
	else
		zr->timing = zr->card.tvn[0];
L
Linus Torvalds 已提交
1476 1477 1478 1479 1480 1481 1482

	/* 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);

1483
	decoder_call(zr, tuner, s_std, norm);
1484
	encoder_call(zr, video, s_std_output, norm);
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498

	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)
{
1499
	struct v4l2_routing route = { 0, 0 };
L
Linus Torvalds 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508

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

	if (zr->v4l_buffers.active != ZORAN_FREE ||
	    zr->jpg_buffers.active != ZORAN_FREE) {
		dprintk(1,
			KERN_WARNING
1509 1510
			"%s: %s called while in playback/capture mode\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
1511 1512 1513 1514 1515 1516
		return -EBUSY;
	}

	if (input < 0 || input >= zr->card.inputs) {
		dprintk(1,
			KERN_ERR
1517 1518
			"%s: %s - unnsupported input %d\n",
			ZR_DEVNAME(zr), __func__, input);
L
Linus Torvalds 已提交
1519 1520 1521
		return -EINVAL;
	}

1522
	route.input = zr->card.input[input].muxsel;
L
Linus Torvalds 已提交
1523 1524
	zr->input = input;

1525
	decoder_call(zr, video, s_routing, &route);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533

	return 0;
}

/*
 *   ioctl routine
 */

1534
#ifdef CONFIG_VIDEO_V4L1_COMPAT
1535
static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
L
Linus Torvalds 已提交
1536
{
1537
	struct zoran_fh *fh = __fh;
L
Linus Torvalds 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
	struct zoran *zr = fh->zr;
	struct zoran_jpg_settings settings;

	switch (cmd) {
	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 已提交
1552
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1553

1554 1555 1556 1557 1558 1559 1560
		if (zr->norm & V4L2_STD_NTSC)
			bparams->norm = VIDEO_MODE_NTSC;
		else if (zr->norm & V4L2_STD_PAL)
			bparams->norm = VIDEO_MODE_PAL;
		else
			bparams->norm = VIDEO_MODE_SECAM;

L
Linus Torvalds 已提交
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
		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 已提交
1587
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
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

		bparams->VFIFO_FB = 0;

		return 0;
	}

	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 已提交
1622
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634

		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 */
1635
		if (zoran_check_jpg_settings(zr, &settings, 0)) {
L
Linus Torvalds 已提交
1636 1637 1638 1639 1640
			res = -EINVAL;
			goto sparams_unlock_and_return;
		}

		fh->jpg_settings = settings;
1641
sparams_unlock_and_return:
I
Ingo Molnar 已提交
1642
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
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

		return res;
	}

	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;

I
Ingo Molnar 已提交
1670
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1671

1672
		if (fh->buffers.allocated) {
L
Linus Torvalds 已提交
1673 1674
			dprintk(1,
				KERN_ERR
1675
				"%s: BUZIOC_REQBUFS - buffers already allocated\n",
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680
				ZR_DEVNAME(zr));
			res = -EBUSY;
			goto jpgreqbuf_unlock_and_return;
		}

1681 1682 1683 1684 1685
		/* The next mmap will map the MJPEG buffers - could
		 * also be *_PLAY, but it doesn't matter here */
		map_mode_jpg(fh, 0);
		fh->buffers.num_buffers = breq->count;
		fh->buffers.buffer_size = breq->size;
L
Linus Torvalds 已提交
1686

1687
		if (jpg_fbuffer_alloc(fh)) {
L
Linus Torvalds 已提交
1688 1689 1690 1691
			res = -ENOMEM;
			goto jpgreqbuf_unlock_and_return;
		}

1692
jpgreqbuf_unlock_and_return:
I
Ingo Molnar 已提交
1693
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704

		return res;
	}

	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 已提交
1705
		mutex_lock(&zr->resource_lock);
1706
		res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
I
Ingo Molnar 已提交
1707
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

		return res;
	}

	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 已提交
1719
		mutex_lock(&zr->resource_lock);
1720
		res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
I
Ingo Molnar 已提交
1721
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732

		return res;
	}

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

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

I
Ingo Molnar 已提交
1733
		mutex_lock(&zr->resource_lock);
1734 1735 1736 1737 1738 1739 1740

		if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
			dprintk(2, KERN_WARNING
				"%s: %s - not in jpg capture mode\n",
				ZR_DEVNAME(zr), __func__);
			res = -EINVAL;
		} else {
1741
			res = jpg_sync(fh, bsync);
1742
		}
I
Ingo Molnar 已提交
1743
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1744 1745 1746 1747 1748 1749 1750

		return res;
	}

	case BUZIOC_G_STATUS:
	{
		struct zoran_status *bstat = arg;
1751 1752 1753
		struct v4l2_routing route = { 0, 0 };
		int status = 0, res = 0;
		v4l2_std_id norm;
L
Linus Torvalds 已提交
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764

		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;
		}

1765
		route.input = zr->card.input[bstat->input].muxsel;
L
Linus Torvalds 已提交
1766

I
Ingo Molnar 已提交
1767
		mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777

		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;
		}

1778
		decoder_call(zr, video, s_routing, &route);
L
Linus Torvalds 已提交
1779 1780 1781 1782 1783

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

		/* Get status of video decoder */
1784 1785
		decoder_call(zr, video, querystd, &norm);
		decoder_call(zr, video, g_input_status, &status);
L
Linus Torvalds 已提交
1786 1787

		/* restore previous input and norm */
1788
		route.input = zr->card.input[zr->input].muxsel;
1789
		decoder_call(zr, video, s_routing, &route);
1790
gstat_unlock_and_return:
I
Ingo Molnar 已提交
1791
		mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
1792 1793 1794

		if (!res) {
			bstat->signal =
1795 1796
			    (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
			if (norm & V4L2_STD_NTSC)
L
Linus Torvalds 已提交
1797
				bstat->norm = VIDEO_MODE_NTSC;
1798
			else if (norm & V4L2_STD_SECAM)
L
Linus Torvalds 已提交
1799 1800 1801 1802 1803
				bstat->norm = VIDEO_MODE_SECAM;
			else
				bstat->norm = VIDEO_MODE_PAL;

			bstat->color =
1804
			    (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809
		}

		return res;
	}

1810 1811
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
1812
	}
1813
}
L
Linus Torvalds 已提交
1814

1815 1816 1817 1818 1819 1820 1821 1822 1823
static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int i, res = 0;


	mutex_lock(&zr->resource_lock);

1824
	if (fh->buffers.allocated) {
1825 1826 1827 1828 1829 1830 1831 1832
		dprintk(1,
			KERN_ERR
			"%s: VIDIOCGMBUF - buffers already allocated\n",
			ZR_DEVNAME(zr));
		res = -EINVAL;
		goto v4l1reqbuf_unlock_and_return;
	}

1833 1834 1835
	/* The next mmap will map the V4L buffers */
	map_mode_raw(fh);

1836
	if (v4l_fbuffer_alloc(fh)) {
1837 1838 1839 1840
		res = -ENOMEM;
		goto v4l1reqbuf_unlock_and_return;
	}

1841 1842 1843 1844 1845
	vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
	vmbuf->frames = fh->buffers.num_buffers;
	for (i = 0; i < vmbuf->frames; i++)
		vmbuf->offsets[i] = i * fh->buffers.buffer_size;

1846 1847 1848 1849 1850 1851 1852
v4l1reqbuf_unlock_and_return:
	mutex_unlock(&zr->resource_lock);

	return res;
}
#endif

1853 1854 1855 1856
static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1857

1858 1859 1860 1861 1862
	memset(cap, 0, sizeof(*cap));
	strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
	strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
	snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
		 pci_name(zr->pci_dev));
1863
	cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
1864
			   RELEASE_VERSION);
1865 1866
	cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
			    V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1867 1868
	return 0;
}
L
Linus Torvalds 已提交
1869

1870 1871 1872
static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
{
	int num = -1, i;
L
Linus Torvalds 已提交
1873

1874 1875 1876 1877 1878
	for (i = 0; i < NUM_FORMATS; i++) {
		if (zoran_formats[i].flags & flag)
			num++;
		if (num == fmt->index)
			break;
L
Linus Torvalds 已提交
1879
	}
1880 1881
	if (fmt->index < 0 /* late, but not too late */  || i == NUM_FORMATS)
		return -EINVAL;
L
Linus Torvalds 已提交
1882

1883 1884 1885 1886 1887 1888
	strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
	fmt->pixelformat = zoran_formats[i].fourcc;
	if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
		fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
	return 0;
}
L
Linus Torvalds 已提交
1889

1890 1891 1892 1893 1894
static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
					    struct v4l2_fmtdesc *f)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1895

1896 1897
	return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
}
L
Linus Torvalds 已提交
1898

1899 1900 1901 1902 1903
static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
					    struct v4l2_fmtdesc *f)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1904

1905 1906
	return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
}
L
Linus Torvalds 已提交
1907

1908 1909 1910 1911 1912
static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
					    struct v4l2_fmtdesc *f)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1913

1914 1915
	return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
}
L
Linus Torvalds 已提交
1916

1917 1918 1919 1920 1921
static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1922

1923
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1924

1925
	fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
H
Hans Verkuil 已提交
1926
	fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1927 1928 1929 1930 1931
		(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 ?
1932
				V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1933 1934 1935 1936 1937
	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;
L
Linus Torvalds 已提交
1938

1939 1940 1941
	mutex_unlock(&zr->resource_lock);
	return 0;
}
L
Linus Torvalds 已提交
1942

1943 1944 1945 1946 1947
static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1948

1949 1950
	if (fh->map_mode != ZORAN_MAP_MODE_RAW)
		return zoran_g_fmt_vid_out(file, fh, fmt);
L
Linus Torvalds 已提交
1951

1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
	mutex_lock(&zr->resource_lock);
	fmt->fmt.pix.width = fh->v4l_settings.width;
	fmt->fmt.pix.height = fh->v4l_settings.height;
	fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
					fh->v4l_settings.height;
	fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
	fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
	fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
	if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
		fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
	else
		fmt->fmt.pix.field = V4L2_FIELD_TOP;
	mutex_unlock(&zr->resource_lock);
	return 0;
}
L
Linus Torvalds 已提交
1967

1968 1969 1970 1971 1972
static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1973

1974
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1975

1976 1977 1978 1979 1980 1981 1982 1983
	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;
L
Linus Torvalds 已提交
1984

1985 1986 1987
	mutex_unlock(&zr->resource_lock);
	return 0;
}
L
Linus Torvalds 已提交
1988

1989 1990 1991 1992 1993
static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
1994

1995
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
1996

1997 1998 1999 2000 2001 2002 2003 2004
	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;
L
Linus Torvalds 已提交
2005

2006 2007 2008
	mutex_unlock(&zr->resource_lock);
	return 0;
}
L
Linus Torvalds 已提交
2009

2010 2011 2012 2013 2014 2015 2016
static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	struct zoran_jpg_settings settings;
	int res = 0;
L
Linus Torvalds 已提交
2017

2018 2019
	if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
		return -EINVAL;
L
Linus Torvalds 已提交
2020

2021 2022
	mutex_lock(&zr->resource_lock);
	settings = fh->jpg_settings;
L
Linus Torvalds 已提交
2023

2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
	/* 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;

2045 2046 2047 2048 2049 2050 2051 2052
	if (settings.HorDcm > 1) {
		settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
		settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
	} else {
		settings.img_x = 0;
		settings.img_width = BUZ_MAX_WIDTH;
	}

2053
	/* check */
2054
	res = zoran_check_jpg_settings(zr, &settings, 1);
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
	if (res)
		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);
L
Linus Torvalds 已提交
2068

2069
	fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
2070 2071
	fmt->fmt.pix.bytesperline = 0;
	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2072 2073 2074 2075
tryfmt_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2076

2077 2078 2079 2080 2081
static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
2082
	int bpp;
2083
	int i;
L
Linus Torvalds 已提交
2084

2085 2086
	if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
		return zoran_try_fmt_vid_out(file, fh, fmt);
L
Linus Torvalds 已提交
2087

2088
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2089

2090 2091 2092
	for (i = 0; i < NUM_FORMATS; i++)
		if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
			break;
L
Linus Torvalds 已提交
2093

2094
	if (i == NUM_FORMATS) {
I
Ingo Molnar 已提交
2095
		mutex_unlock(&zr->resource_lock);
2096
		return -EINVAL;
L
Linus Torvalds 已提交
2097 2098
	}

2099 2100
	bpp = (zoran_formats[i].depth + 7) / 8;
	fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
2101 2102 2103 2104 2105 2106 2107 2108 2109
	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;
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2110

2111 2112
	return 0;
}
L
Linus Torvalds 已提交
2113

2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res;

	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);
	mutex_lock(&zr->resource_lock);
2128 2129 2130 2131
	res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
			   fmt->fmt.win.w.width, fmt->fmt.win.w.height,
			   (struct v4l2_clip __user *)fmt->fmt.win.clips,
			   fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
2132 2133 2134
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2135

2136 2137 2138 2139 2140 2141 2142 2143
static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	__le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
	struct zoran_jpg_settings settings;
	int res = 0;
L
Linus Torvalds 已提交
2144

2145 2146 2147 2148 2149 2150
	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);
	if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
		return -EINVAL;
L
Linus Torvalds 已提交
2151

2152
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2153

2154
	if (fh->buffers.allocated) {
2155
		dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2156
			ZR_DEVNAME(zr));
2157 2158 2159
		res = -EBUSY;
		goto sfmtjpg_unlock_and_return;
	}
L
Linus Torvalds 已提交
2160

2161 2162
	settings = fh->jpg_settings;

2163
	/* we actually need to set 'real' parameters now */
2164
	if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
		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;

2184 2185 2186 2187 2188 2189 2190 2191
	if (settings.HorDcm > 1) {
		settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
		settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
	} else {
		settings.img_x = 0;
		settings.img_width = BUZ_MAX_WIDTH;
	}

2192
	/* check */
2193
	res = zoran_check_jpg_settings(zr, &settings, 0);
2194 2195 2196 2197 2198 2199
	if (res)
		goto sfmtjpg_unlock_and_return;

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

2200 2201 2202
	map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
	fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);

2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
	/* 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.bytesperline = 0;
2214
	fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
2215 2216 2217 2218 2219 2220
	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;

sfmtjpg_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2221

2222 2223 2224 2225 2226 2227 2228
static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
					struct v4l2_format *fmt)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int i;
	int res = 0;
L
Linus Torvalds 已提交
2229

2230 2231
	if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
		return zoran_s_fmt_vid_out(file, fh, fmt);
L
Linus Torvalds 已提交
2232

2233 2234 2235 2236 2237 2238 2239
	for (i = 0; i < NUM_FORMATS; i++)
		if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
			break;
	if (i == NUM_FORMATS) {
		dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
			ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
		return -EINVAL;
L
Linus Torvalds 已提交
2240
	}
2241

2242
	mutex_lock(&zr->resource_lock);
2243 2244 2245

	if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
	    fh->buffers.active != ZORAN_FREE) {
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
		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;

2256 2257 2258 2259
	map_mode_raw(fh);

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

2263
	/* tell the user the results/missing stuff */
2264 2265 2266 2267 2268 2269 2270
	fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
	fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
	fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
	if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
		fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
	else
		fmt->fmt.pix.field = V4L2_FIELD_TOP;
L
Linus Torvalds 已提交
2271

2272 2273 2274 2275
sfmtv4l_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2276

2277 2278 2279 2280 2281
static int zoran_g_fbuf(struct file *file, void *__fh,
		struct v4l2_framebuffer *fb)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
2282

2283 2284
	memset(fb, 0, sizeof(*fb));
	mutex_lock(&zr->resource_lock);
2285 2286 2287
	fb->base = zr->vbuf_base;
	fb->fmt.width = zr->vbuf_width;
	fb->fmt.height = zr->vbuf_height;
2288 2289
	if (zr->overlay_settings.format)
		fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
2290
	fb->fmt.bytesperline = zr->vbuf_bytesperline;
2291 2292 2293 2294 2295
	mutex_unlock(&zr->resource_lock);
	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;
L
Linus Torvalds 已提交
2296

2297 2298
	return 0;
}
L
Linus Torvalds 已提交
2299

2300 2301 2302 2303 2304 2305 2306
static int zoran_s_fbuf(struct file *file, void *__fh,
		struct v4l2_framebuffer *fb)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int i, res = 0;
	__le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
L
Linus Torvalds 已提交
2307

2308 2309
	for (i = 0; i < NUM_FORMATS; i++)
		if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
L
Linus Torvalds 已提交
2310
			break;
2311 2312 2313 2314 2315
	if (i == NUM_FORMATS) {
		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;
L
Linus Torvalds 已提交
2316 2317
	}

2318
	mutex_lock(&zr->resource_lock);
2319 2320
	res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
			    fb->fmt.height, fb->fmt.bytesperline);
2321
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2322

2323 2324
	return res;
}
L
Linus Torvalds 已提交
2325

2326 2327 2328 2329 2330
static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res;
L
Linus Torvalds 已提交
2331

2332
	mutex_lock(&zr->resource_lock);
2333
	res = setup_overlay(fh, on);
2334
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2335

2336 2337
	return res;
}
L
Linus Torvalds 已提交
2338

2339 2340
static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);

2341 2342 2343 2344 2345
static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res = 0;
L
Linus Torvalds 已提交
2346

2347
	if (req->memory != V4L2_MEMORY_MMAP) {
2348
		dprintk(2,
2349 2350 2351 2352 2353
				KERN_ERR
				"%s: only MEMORY_MMAP capture is supported, not %d\n",
				ZR_DEVNAME(zr), req->memory);
		return -EINVAL;
	}
L
Linus Torvalds 已提交
2354

2355 2356
	if (req->count == 0)
		return zoran_streamoff(file, fh, req->type);
L
Linus Torvalds 已提交
2357

2358
	mutex_lock(&zr->resource_lock);
2359
	if (fh->buffers.allocated) {
2360
		dprintk(2,
L
Linus Torvalds 已提交
2361
				KERN_ERR
2362
				"%s: VIDIOC_REQBUFS - buffers already allocated\n",
2363 2364 2365
				ZR_DEVNAME(zr));
		res = -EBUSY;
		goto v4l2reqbuf_unlock_and_return;
L
Linus Torvalds 已提交
2366 2367
	}

2368
	if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2369
	    req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2370 2371 2372 2373 2374
		/* control user input */
		if (req->count < 2)
			req->count = 2;
		if (req->count > v4l_nbufs)
			req->count = v4l_nbufs;
2375 2376 2377 2378

		/* The next mmap will map the V4L buffers */
		map_mode_raw(fh);
		fh->buffers.num_buffers = req->count;
L
Linus Torvalds 已提交
2379

2380
		if (v4l_fbuffer_alloc(fh)) {
2381 2382 2383 2384
			res = -ENOMEM;
			goto v4l2reqbuf_unlock_and_return;
		}
	} else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2385
		   fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2386 2387 2388 2389 2390
		/* we need to calculate size ourselves now */
		if (req->count < 4)
			req->count = 4;
		if (req->count > jpg_nbufs)
			req->count = jpg_nbufs;
2391 2392 2393 2394 2395

		/* The next mmap will map the MJPEG buffers */
		map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
		fh->buffers.num_buffers = req->count;
		fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
L
Linus Torvalds 已提交
2396

2397
		if (jpg_fbuffer_alloc(fh)) {
2398 2399 2400 2401 2402
			res = -ENOMEM;
			goto v4l2reqbuf_unlock_and_return;
		}
	} else {
		dprintk(1,
L
Linus Torvalds 已提交
2403
				KERN_ERR
2404 2405 2406 2407
				"%s: VIDIOC_REQBUFS - unknown type %d\n",
				ZR_DEVNAME(zr), req->type);
		res = -EINVAL;
		goto v4l2reqbuf_unlock_and_return;
L
Linus Torvalds 已提交
2408
	}
2409 2410
v4l2reqbuf_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2411

2412 2413
	return res;
}
L
Linus Torvalds 已提交
2414

2415 2416 2417 2418
static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
2419
	int res;
L
Linus Torvalds 已提交
2420

2421
	mutex_lock(&zr->resource_lock);
2422
	res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2423
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2424

2425 2426
	return res;
}
L
Linus Torvalds 已提交
2427

2428 2429 2430 2431 2432
static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res = 0, codec_mode, buf_type;
L
Linus Torvalds 已提交
2433

2434
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2435

2436 2437 2438 2439 2440 2441
	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);
L
Linus Torvalds 已提交
2442
			res = -EINVAL;
2443
			goto qbuf_unlock_and_return;
L
Linus Torvalds 已提交
2444 2445
		}

2446
		res = zoran_v4l_queue_frame(fh, buf->index);
2447 2448
		if (res)
			goto qbuf_unlock_and_return;
2449
		if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2450
			zr36057_set_memgrab(zr, 1);
L
Linus Torvalds 已提交
2451 2452
		break;

2453 2454 2455 2456 2457 2458 2459 2460
	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;
L
Linus Torvalds 已提交
2461 2462
		}

2463 2464 2465 2466 2467 2468
		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;
L
Linus Torvalds 已提交
2469 2470
		}

2471
		res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
2472 2473 2474
		if (res != 0)
			goto qbuf_unlock_and_return;
		if (zr->codec_mode == BUZ_MODE_IDLE &&
2475
		    fh->buffers.active == ZORAN_LOCKED)
2476
			zr36057_enable_jpg(zr, codec_mode);
2477

2478
		break;
L
Linus Torvalds 已提交
2479

2480 2481 2482 2483 2484
	default:
		dprintk(1, KERN_ERR
			"%s: VIDIOC_QBUF - unsupported type %d\n",
			ZR_DEVNAME(zr), buf->type);
		res = -EINVAL;
L
Linus Torvalds 已提交
2485
		break;
2486 2487 2488
	}
qbuf_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2489

2490 2491
	return res;
}
L
Linus Torvalds 已提交
2492

2493 2494 2495 2496 2497
static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res = 0, buf_type, num = -1;	/* compiler borks here (?) */
L
Linus Torvalds 已提交
2498

2499
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2500

2501 2502 2503 2504 2505 2506 2507 2508
	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;
L
Linus Torvalds 已提交
2509 2510
		}

2511 2512 2513 2514 2515
		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;
L
Linus Torvalds 已提交
2516
		}
2517
		res = v4l_sync(fh, num);
2518 2519 2520
		if (res)
			goto dqbuf_unlock_and_return;
		zr->v4l_sync_tail++;
2521
		res = zoran_v4l2_buffer_status(fh, buf, num);
L
Linus Torvalds 已提交
2522 2523
		break;

2524 2525
	case ZORAN_MAP_MODE_JPG_REC:
	case ZORAN_MAP_MODE_JPG_PLAY:
L
Linus Torvalds 已提交
2526
	{
2527
		struct zoran_sync bs;
L
Linus Torvalds 已提交
2528

2529 2530 2531 2532
		if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
			buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
		else
			buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
L
Linus Torvalds 已提交
2533

2534 2535 2536 2537 2538 2539
		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;
L
Linus Torvalds 已提交
2540 2541
		}

2542
		num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
L
Linus Torvalds 已提交
2543

2544 2545 2546 2547 2548
		if (file->f_flags & O_NONBLOCK &&
		    zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
			res = -EAGAIN;
			goto dqbuf_unlock_and_return;
		}
2549
		res = jpg_sync(fh, &bs);
2550 2551
		if (res)
			goto dqbuf_unlock_and_return;
2552
		res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2553
		break;
L
Linus Torvalds 已提交
2554
	}
2555 2556 2557 2558 2559 2560

	default:
		dprintk(1, KERN_ERR
			"%s: VIDIOC_DQBUF - unsupported type %d\n",
			ZR_DEVNAME(zr), buf->type);
		res = -EINVAL;
L
Linus Torvalds 已提交
2561
		break;
2562 2563 2564
	}
dqbuf_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2565

2566 2567
	return res;
}
L
Linus Torvalds 已提交
2568

2569 2570 2571 2572 2573
static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res = 0;
L
Linus Torvalds 已提交
2574

2575
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2576

2577 2578 2579
	switch (fh->map_mode) {
	case ZORAN_MAP_MODE_RAW:	/* raw capture */
		if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2580
		    fh->buffers.active != ZORAN_ACTIVE) {
2581 2582
			res = -EBUSY;
			goto strmon_unlock_and_return;
L
Linus Torvalds 已提交
2583 2584
		}

2585
		zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2586 2587 2588 2589 2590 2591 2592
		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);
		}
L
Linus Torvalds 已提交
2593 2594
		break;

2595 2596 2597 2598
	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 ||
2599
		    fh->buffers.active != ZORAN_ACTIVE) {
2600 2601 2602
			res = -EBUSY;
			goto strmon_unlock_and_return;
		}
L
Linus Torvalds 已提交
2603

2604
		zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
L
Linus Torvalds 已提交
2605

2606 2607 2608
		if (zr->jpg_que_head != zr->jpg_que_tail) {
			/* Start the jpeg codec when the first frame is queued  */
			jpeg_start(zr);
L
Linus Torvalds 已提交
2609
		}
2610
		break;
L
Linus Torvalds 已提交
2611

2612 2613 2614 2615 2616 2617
	default:
		dprintk(1,
			KERN_ERR
			"%s: VIDIOC_STREAMON - invalid map mode %d\n",
			ZR_DEVNAME(zr), fh->map_mode);
		res = -EINVAL;
L
Linus Torvalds 已提交
2618
		break;
2619 2620 2621
	}
strmon_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2622

2623 2624 2625 2626 2627 2628 2629 2630
	return res;
}

static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int i, res = 0;
2631
	unsigned long flags;
L
Linus Torvalds 已提交
2632

2633
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2634

2635 2636
	switch (fh->map_mode) {
	case ZORAN_MAP_MODE_RAW:	/* raw capture */
2637
		if (fh->buffers.active == ZORAN_FREE &&
2638 2639 2640
		    zr->v4l_buffers.active != ZORAN_FREE) {
			res = -EPERM;	/* stay off other's settings! */
			goto strmoff_unlock_and_return;
L
Linus Torvalds 已提交
2641
		}
2642 2643
		if (zr->v4l_buffers.active == ZORAN_FREE)
			goto strmoff_unlock_and_return;
L
Linus Torvalds 已提交
2644

2645
		spin_lock_irqsave(&zr->spinlock, flags);
2646 2647
		/* unload capture */
		if (zr->v4l_memgrab_active) {
L
Linus Torvalds 已提交
2648

2649
			zr36057_set_memgrab(zr, 0);
L
Linus Torvalds 已提交
2650 2651
		}

2652
		for (i = 0; i < fh->buffers.num_buffers; i++)
2653
			zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2654
		fh->buffers = zr->v4l_buffers;
L
Linus Torvalds 已提交
2655

2656
		zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
2657

2658 2659 2660
		zr->v4l_grab_seq = 0;
		zr->v4l_pend_head = zr->v4l_pend_tail = 0;
		zr->v4l_sync_tail = 0;
L
Linus Torvalds 已提交
2661

2662 2663
		spin_unlock_irqrestore(&zr->spinlock, flags);

2664
		break;
L
Linus Torvalds 已提交
2665

2666 2667
	case ZORAN_MAP_MODE_JPG_REC:
	case ZORAN_MAP_MODE_JPG_PLAY:
2668
		if (fh->buffers.active == ZORAN_FREE &&
2669 2670 2671 2672 2673 2674 2675
		    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;

2676
		res = jpg_qbuf(fh, -1,
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
			     (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;
L
Linus Torvalds 已提交
2688
		break;
2689 2690 2691
	}
strmoff_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2692

2693 2694
	return res;
}
L
Linus Torvalds 已提交
2695

2696 2697 2698
static int zoran_queryctrl(struct file *file, void *__fh,
					struct v4l2_queryctrl *ctrl)
{
2699 2700 2701
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;

2702 2703 2704 2705
	/* we only support hue/saturation/contrast/brightness */
	if (ctrl->id < V4L2_CID_BRIGHTNESS ||
	    ctrl->id > V4L2_CID_HUE)
		return -EINVAL;
L
Linus Torvalds 已提交
2706

2707
	decoder_call(zr, core, queryctrl, ctrl);
L
Linus Torvalds 已提交
2708

2709 2710
	return 0;
}
L
Linus Torvalds 已提交
2711

2712 2713 2714 2715
static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
2716

2717 2718 2719 2720
	/* we only support hue/saturation/contrast/brightness */
	if (ctrl->id < V4L2_CID_BRIGHTNESS ||
	    ctrl->id > V4L2_CID_HUE)
		return -EINVAL;
L
Linus Torvalds 已提交
2721

2722
	mutex_lock(&zr->resource_lock);
2723
	decoder_call(zr, core, g_ctrl, ctrl);
2724
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2725

2726 2727
	return 0;
}
L
Linus Torvalds 已提交
2728

2729 2730 2731 2732
static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
2733

2734 2735 2736 2737
	/* we only support hue/saturation/contrast/brightness */
	if (ctrl->id < V4L2_CID_BRIGHTNESS ||
	    ctrl->id > V4L2_CID_HUE)
		return -EINVAL;
L
Linus Torvalds 已提交
2738

2739
	mutex_lock(&zr->resource_lock);
2740
	decoder_call(zr, core, s_ctrl, ctrl);
2741
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2742

2743 2744
	return 0;
}
L
Linus Torvalds 已提交
2745

2746 2747 2748 2749 2750 2751
static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;

	mutex_lock(&zr->resource_lock);
2752
	*std = zr->norm;
2753 2754 2755
	mutex_unlock(&zr->resource_lock);
	return 0;
}
L
Linus Torvalds 已提交
2756

2757 2758 2759 2760
static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
2761
	int res = 0;
L
Linus Torvalds 已提交
2762

2763
	mutex_lock(&zr->resource_lock);
2764
	res = zoran_set_norm(zr, *std);
2765 2766
	if (res)
		goto sstd_unlock_and_return;
L
Linus Torvalds 已提交
2767

2768 2769 2770 2771 2772
	res = wait_grab_pending(zr);
sstd_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2773

2774 2775 2776 2777 2778
static int zoran_enum_input(struct file *file, void *__fh,
				 struct v4l2_input *inp)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
2779

2780 2781 2782 2783 2784 2785
	if (inp->index < 0 || inp->index >= zr->card.inputs)
		return -EINVAL;
	else {
		int id = inp->index;
		memset(inp, 0, sizeof(*inp));
		inp->index = id;
L
Linus Torvalds 已提交
2786 2787
	}

2788 2789 2790 2791
	strncpy(inp->name, zr->card.input[inp->index].name,
		sizeof(inp->name) - 1);
	inp->type = V4L2_INPUT_TYPE_CAMERA;
	inp->std = V4L2_STD_ALL;
L
Linus Torvalds 已提交
2792

2793 2794
	/* Get status of video decoder */
	mutex_lock(&zr->resource_lock);
2795
	decoder_call(zr, video, g_input_status, &inp->status);
2796 2797 2798
	mutex_unlock(&zr->resource_lock);
	return 0;
}
L
Linus Torvalds 已提交
2799

2800 2801 2802 2803
static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
L
Linus Torvalds 已提交
2804

2805 2806 2807
	mutex_lock(&zr->resource_lock);
	*input = zr->input;
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2808

2809 2810
	return 0;
}
L
Linus Torvalds 已提交
2811

2812 2813 2814 2815 2816
static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res;
L
Linus Torvalds 已提交
2817

2818 2819 2820 2821
	mutex_lock(&zr->resource_lock);
	res = zoran_set_input(zr, input);
	if (res)
		goto sinput_unlock_and_return;
L
Linus Torvalds 已提交
2822

2823 2824 2825 2826 2827 2828
	/* Make sure the changes come into effect */
	res = wait_grab_pending(zr);
sinput_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2829

2830 2831 2832 2833 2834
static int zoran_enum_output(struct file *file, void *__fh,
				  struct v4l2_output *outp)
{
	if (outp->index != 0)
		return -EINVAL;
L
Linus Torvalds 已提交
2835

2836 2837 2838 2839
	memset(outp, 0, sizeof(*outp));
	outp->index = 0;
	outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
	strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
L
Linus Torvalds 已提交
2840

2841 2842
	return 0;
}
L
Linus Torvalds 已提交
2843

2844 2845 2846 2847 2848 2849
static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
{
	*output = 0;

	return 0;
}
L
Linus Torvalds 已提交
2850

2851 2852 2853 2854
static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
{
	if (output != 0)
		return -EINVAL;
L
Linus Torvalds 已提交
2855

2856 2857
	return 0;
}
L
Linus Torvalds 已提交
2858

2859 2860 2861 2862 2863 2864 2865
/* cropping (sub-frame capture) */
static int zoran_cropcap(struct file *file, void *__fh,
					struct v4l2_cropcap *cropcap)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int type = cropcap->type, res = 0;
L
Linus Torvalds 已提交
2866

2867 2868
	memset(cropcap, 0, sizeof(*cropcap));
	cropcap->type = type;
L
Linus Torvalds 已提交
2869

2870
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2871

2872 2873 2874 2875 2876
	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",
L
Linus Torvalds 已提交
2877
			ZR_DEVNAME(zr));
2878 2879 2880
		res = -EINVAL;
		goto cropcap_unlock_and_return;
	}
L
Linus Torvalds 已提交
2881

2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
	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:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2892

2893 2894 2895 2896 2897
static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int type = crop->type, res = 0;
L
Linus Torvalds 已提交
2898

2899 2900
	memset(crop, 0, sizeof(*crop));
	crop->type = type;
L
Linus Torvalds 已提交
2901

2902
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2903

2904 2905 2906 2907 2908 2909 2910 2911 2912
	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;
L
Linus Torvalds 已提交
2913 2914
	}

2915 2916 2917 2918
	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;
L
Linus Torvalds 已提交
2919

2920 2921
gcrop_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2922

2923 2924
	return res;
}
L
Linus Torvalds 已提交
2925

2926 2927 2928 2929 2930 2931
static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res = 0;
	struct zoran_jpg_settings settings;
L
Linus Torvalds 已提交
2932

2933
	settings = fh->jpg_settings;
L
Linus Torvalds 已提交
2934

2935
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2936

2937
	if (fh->buffers.allocated) {
2938 2939 2940 2941 2942
		dprintk(1, KERN_ERR
			"%s: VIDIOC_S_CROP - cannot change settings while active\n",
			ZR_DEVNAME(zr));
		res = -EBUSY;
		goto scrop_unlock_and_return;
L
Linus Torvalds 已提交
2943 2944
	}

2945 2946 2947 2948 2949 2950 2951 2952 2953
	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;
	}
L
Linus Torvalds 已提交
2954

2955 2956 2957 2958 2959
	/* 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;
L
Linus Torvalds 已提交
2960

2961
	/* check validity */
2962
	res = zoran_check_jpg_settings(zr, &settings, 0);
2963 2964
	if (res)
		goto scrop_unlock_and_return;
L
Linus Torvalds 已提交
2965

2966 2967
	/* accept */
	fh->jpg_settings = settings;
L
Linus Torvalds 已提交
2968

2969 2970 2971 2972
scrop_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
	return res;
}
L
Linus Torvalds 已提交
2973

2974 2975 2976 2977 2978 2979
static int zoran_g_jpegcomp(struct file *file, void *__fh,
					struct v4l2_jpegcompression *params)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	memset(params, 0, sizeof(*params));
L
Linus Torvalds 已提交
2980

2981
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
2982

2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
	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;
L
Linus Torvalds 已提交
2995

2996
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
2997

2998 2999
	return 0;
}
L
Linus Torvalds 已提交
3000

3001 3002 3003 3004 3005 3006 3007
static int zoran_s_jpegcomp(struct file *file, void *__fh,
					struct v4l2_jpegcompression *params)
{
	struct zoran_fh *fh = __fh;
	struct zoran *zr = fh->zr;
	int res = 0;
	struct zoran_jpg_settings settings;
L
Linus Torvalds 已提交
3008

3009
	settings = fh->jpg_settings;
L
Linus Torvalds 已提交
3010

3011
	settings.jpg_comp = *params;
L
Linus Torvalds 已提交
3012

3013
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3014

3015
	if (fh->buffers.active != ZORAN_FREE) {
3016 3017 3018 3019 3020
		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;
L
Linus Torvalds 已提交
3021 3022
	}

3023
	res = zoran_check_jpg_settings(zr, &settings, 0);
3024 3025
	if (res)
		goto sjpegc_unlock_and_return;
3026 3027 3028
	if (!fh->buffers.allocated)
		fh->buffers.buffer_size =
			zoran_v4l2_calc_bufsize(&fh->jpg_settings);
3029 3030 3031
	fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
sjpegc_unlock_and_return:
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3032

3033
	return res;
L
Linus Torvalds 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042
}

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;
3043
	unsigned long flags;
L
Linus Torvalds 已提交
3044 3045 3046 3047 3048 3049 3050 3051 3052

	/* 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 已提交
3053
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3054 3055 3056

	switch (fh->map_mode) {
	case ZORAN_MAP_MODE_RAW:
3057 3058 3059 3060 3061 3062 3063
		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",
3064
			ZR_DEVNAME(zr), __func__,
3065
			"FAL"[fh->buffers.active], zr->v4l_sync_tail,
3066 3067 3068
			"UPMD"[zr->v4l_buffers.buffer[frame].state],
			zr->v4l_pend_tail, zr->v4l_pend_head);
		/* Process is the one capturing? */
3069
		if (fh->buffers.active != ZORAN_FREE &&
3070 3071
		    /* Buffer ready to DQBUF? */
		    zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
L
Linus Torvalds 已提交
3072
			res = POLLIN | POLLRDNORM;
3073 3074
		spin_unlock_irqrestore(&zr->spinlock, flags);

L
Linus Torvalds 已提交
3075 3076 3077 3078
		break;

	case ZORAN_MAP_MODE_JPG_REC:
	case ZORAN_MAP_MODE_JPG_PLAY:
3079
		poll_wait(file, &zr->jpg_capq, wait);
L
Linus Torvalds 已提交
3080
		frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
3081 3082 3083 3084 3085

		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",
3086
			ZR_DEVNAME(zr), __func__,
3087
			"FAL"[fh->buffers.active], zr->jpg_que_tail,
3088 3089
			"UPMD"[zr->jpg_buffers.buffer[frame].state],
			zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
3090
		if (fh->buffers.active != ZORAN_FREE &&
3091
		    zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
L
Linus Torvalds 已提交
3092 3093 3094 3095 3096
			if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
				res = POLLIN | POLLRDNORM;
			else
				res = POLLOUT | POLLWRNORM;
		}
3097 3098
		spin_unlock_irqrestore(&zr->spinlock, flags);

L
Linus Torvalds 已提交
3099 3100 3101 3102
		break;

	default:
		dprintk(1,
3103
			KERN_ERR
3104 3105
			"%s: %s - internal error, unknown map_mode=%d\n",
			ZR_DEVNAME(zr), __func__, fh->map_mode);
L
Linus Torvalds 已提交
3106 3107 3108
		res = POLLNVAL;
	}

I
Ingo Molnar 已提交
3109
	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138

	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;
3139
	struct zoran_fh *fh = map->file->private_data;
L
Linus Torvalds 已提交
3140 3141 3142
	struct zoran *zr = fh->zr;
	int i;

3143 3144
	if (--map->count > 0)
		return;
L
Linus Torvalds 已提交
3145

3146 3147
	dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
		__func__, mode_name(fh->map_mode));
L
Linus Torvalds 已提交
3148

3149 3150 3151 3152 3153
	for (i = 0; i < fh->buffers.num_buffers; i++) {
		if (fh->buffers.buffer[i].map == map)
			fh->buffers.buffer[i].map = NULL;
	}
	kfree(map);
L
Linus Torvalds 已提交
3154

3155 3156 3157 3158
	/* Any buffers still mapped? */
	for (i = 0; i < fh->buffers.num_buffers; i++)
		if (fh->buffers.buffer[i].map)
			return;
L
Linus Torvalds 已提交
3159

3160 3161
	dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
		__func__, mode_name(fh->map_mode));
L
Linus Torvalds 已提交
3162

3163
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3164

3165 3166 3167
	if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
		if (fh->buffers.active != ZORAN_FREE) {
			unsigned long flags;
L
Linus Torvalds 已提交
3168

3169 3170 3171 3172 3173 3174
			spin_lock_irqsave(&zr->spinlock, flags);
			zr36057_set_memgrab(zr, 0);
			zr->v4l_buffers.allocated = 0;
			zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
			spin_unlock_irqrestore(&zr->spinlock, flags);
		}
3175
		v4l_fbuffer_free(fh);
3176 3177
	} else {
		if (fh->buffers.active != ZORAN_FREE) {
3178
			jpg_qbuf(fh, -1, zr->codec_mode);
3179 3180
			zr->jpg_buffers.allocated = 0;
			zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
L
Linus Torvalds 已提交
3181
		}
3182
		jpg_fbuffer_free(fh);
L
Linus Torvalds 已提交
3183
	}
3184 3185

	mutex_unlock(&zr->resource_lock);
L
Linus Torvalds 已提交
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
}

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,
3208 3209
		KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
		ZR_DEVNAME(zr), __func__,
3210
		mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
L
Linus Torvalds 已提交
3211 3212 3213 3214 3215

	if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
	    !(vma->vm_flags & VM_WRITE)) {
		dprintk(1,
			KERN_ERR
3216 3217
			"%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
			ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
3218 3219 3220
		return -EINVAL;
	}

3221
	mutex_lock(&zr->resource_lock);
L
Linus Torvalds 已提交
3222

3223 3224 3225
	if (!fh->buffers.allocated) {
		dprintk(1,
			KERN_ERR
3226 3227
			"%s: %s(%s) - buffers not yet allocated\n",
			ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
3228 3229 3230
		res = -ENOMEM;
		goto mmap_unlock_and_return;
	}
L
Linus Torvalds 已提交
3231

3232 3233 3234 3235 3236 3237 3238 3239
	first = offset / fh->buffers.buffer_size;
	last = first - 1 + size / fh->buffers.buffer_size;
	if (offset % fh->buffers.buffer_size != 0 ||
	    size % fh->buffers.buffer_size != 0 || first < 0 ||
	    last < 0 || first >= fh->buffers.num_buffers ||
	    last >= fh->buffers.buffer_size) {
		dprintk(1,
			KERN_ERR
3240 3241
			"%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
			ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
3242 3243 3244 3245 3246
			fh->buffers.buffer_size,
			fh->buffers.num_buffers);
		res = -EINVAL;
		goto mmap_unlock_and_return;
	}
L
Linus Torvalds 已提交
3247

3248 3249 3250
	/* Check if any buffers are already mapped */
	for (i = first; i <= last; i++) {
		if (fh->buffers.buffer[i].map) {
L
Linus Torvalds 已提交
3251 3252
			dprintk(1,
				KERN_ERR
3253 3254
				"%s: %s(%s) - buffer %d already mapped\n",
				ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
3255 3256
			res = -EBUSY;
			goto mmap_unlock_and_return;
L
Linus Torvalds 已提交
3257
		}
3258
	}
L
Linus Torvalds 已提交
3259

3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273
	/* map these buffers */
	map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
	if (!map) {
		res = -ENOMEM;
		goto 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;

	if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
L
Linus Torvalds 已提交
3274
		for (i = first; i <= last; i++) {
3275 3276 3277 3278 3279 3280
			todo = size;
			if (todo > fh->buffers.buffer_size)
				todo = fh->buffers.buffer_size;
			page = fh->buffers.buffer[i].v4l.fbuffer_phys;
			if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
							todo, PAGE_SHARED)) {
L
Linus Torvalds 已提交
3281 3282
				dprintk(1,
					KERN_ERR
3283 3284
					"%s: %s(V4L) - remap_pfn_range failed\n",
					ZR_DEVNAME(zr), __func__);
3285 3286
				res = -EAGAIN;
				goto mmap_unlock_and_return;
L
Linus Torvalds 已提交
3287
			}
3288 3289 3290 3291 3292
			size -= todo;
			start += todo;
			fh->buffers.buffer[i].map = map;
			if (size == 0)
				break;
L
Linus Torvalds 已提交
3293
		}
3294
	} else {
L
Linus Torvalds 已提交
3295 3296
		for (i = first; i <= last; i++) {
			for (j = 0;
3297
			     j < fh->buffers.buffer_size / PAGE_SIZE;
L
Linus Torvalds 已提交
3298 3299
			     j++) {
				fraglen =
3300
				    (le32_to_cpu(fh->buffers.buffer[i].jpg.
L
Linus Torvalds 已提交
3301 3302 3303 3304 3305
				     frag_tab[2 * j + 1]) & ~1) << 1;
				todo = size;
				if (todo > fraglen)
					todo = fraglen;
				pos =
3306 3307
				    le32_to_cpu(fh->buffers.
				    buffer[i].jpg.frag_tab[2 * j]);
L
Linus Torvalds 已提交
3308 3309 3310 3311 3312 3313 3314
				/* 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
3315 3316
						"%s: %s(V4L) - remap_pfn_range failed\n",
						ZR_DEVNAME(zr), __func__);
L
Linus Torvalds 已提交
3317
					res = -EAGAIN;
3318
					goto mmap_unlock_and_return;
L
Linus Torvalds 已提交
3319 3320 3321 3322 3323
				}
				size -= todo;
				start += todo;
				if (size == 0)
					break;
3324
				if (le32_to_cpu(fh->buffers.buffer[i].jpg.
L
Linus Torvalds 已提交
3325 3326 3327
				    frag_tab[2 * j + 1]) & 1)
					break;	/* was last fragment */
			}
3328
			fh->buffers.buffer[i].map = map;
L
Linus Torvalds 已提交
3329 3330 3331 3332 3333 3334
			if (size == 0)
				break;

		}
	}

3335 3336 3337
mmap_unlock_and_return:
	mutex_unlock(&zr->resource_lock);

L
Linus Torvalds 已提交
3338 3339 3340
	return 0;
}

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 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
	.vidioc_querycap    		    = zoran_querycap,
	.vidioc_cropcap       		    = zoran_cropcap,
	.vidioc_s_crop       		    = zoran_s_crop,
	.vidioc_g_crop       		    = zoran_g_crop,
	.vidioc_enum_input     		    = zoran_enum_input,
	.vidioc_g_input      		    = zoran_g_input,
	.vidioc_s_input      		    = zoran_s_input,
	.vidioc_enum_output    		    = zoran_enum_output,
	.vidioc_g_output     		    = zoran_g_output,
	.vidioc_s_output     		    = zoran_s_output,
	.vidioc_g_fbuf			    = zoran_g_fbuf,
	.vidioc_s_fbuf			    = zoran_s_fbuf,
	.vidioc_g_std 			    = zoran_g_std,
	.vidioc_s_std 			    = zoran_s_std,
	.vidioc_g_jpegcomp 		    = zoran_g_jpegcomp,
	.vidioc_s_jpegcomp 		    = zoran_s_jpegcomp,
	.vidioc_overlay			    = zoran_overlay,
	.vidioc_reqbufs			    = zoran_reqbufs,
	.vidioc_querybuf		    = zoran_querybuf,
	.vidioc_qbuf			    = zoran_qbuf,
	.vidioc_dqbuf			    = zoran_dqbuf,
	.vidioc_streamon		    = zoran_streamon,
	.vidioc_streamoff		    = zoran_streamoff,
	.vidioc_enum_fmt_vid_cap 	    = zoran_enum_fmt_vid_cap,
	.vidioc_enum_fmt_vid_out 	    = zoran_enum_fmt_vid_out,
	.vidioc_enum_fmt_vid_overlay 	    = zoran_enum_fmt_vid_overlay,
	.vidioc_g_fmt_vid_cap 		    = zoran_g_fmt_vid_cap,
	.vidioc_g_fmt_vid_out               = zoran_g_fmt_vid_out,
	.vidioc_g_fmt_vid_overlay           = zoran_g_fmt_vid_overlay,
	.vidioc_s_fmt_vid_cap  		    = zoran_s_fmt_vid_cap,
	.vidioc_s_fmt_vid_out               = zoran_s_fmt_vid_out,
	.vidioc_s_fmt_vid_overlay           = zoran_s_fmt_vid_overlay,
	.vidioc_try_fmt_vid_cap  	    = zoran_try_fmt_vid_cap,
	.vidioc_try_fmt_vid_out 	    = zoran_try_fmt_vid_out,
	.vidioc_try_fmt_vid_overlay 	    = zoran_try_fmt_vid_overlay,
	.vidioc_queryctrl 		    = zoran_queryctrl,
	.vidioc_s_ctrl       		    = zoran_s_ctrl,
	.vidioc_g_ctrl       		    = zoran_g_ctrl,
3380
#ifdef CONFIG_VIDEO_V4L1_COMPAT
3381
	.vidioc_default 		    = zoran_default,
3382 3383
	.vidiocgmbuf 			    = zoran_vidiocgmbuf,
#endif
3384 3385
};

3386
static const struct v4l2_file_operations zoran_fops = {
L
Linus Torvalds 已提交
3387 3388 3389
	.owner = THIS_MODULE,
	.open = zoran_open,
	.release = zoran_close,
3390
	.ioctl = video_ioctl2,
L
Linus Torvalds 已提交
3391 3392 3393 3394 3395 3396 3397 3398 3399
	.read = zoran_read,
	.write = zoran_write,
	.mmap = zoran_mmap,
	.poll = zoran_poll,
};

struct video_device zoran_template __devinitdata = {
	.name = ZORAN_NAME,
	.fops = &zoran_fops,
3400
	.ioctl_ops = &zoran_ioctl_ops,
L
Linus Torvalds 已提交
3401
	.release = &zoran_vdev_release,
3402
	.tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
L
Linus Torvalds 已提交
3403 3404 3405
	.minor = -1
};