saa7134-video.c 67.5 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
/*
 *
 * device driver for philips saa7134 based TV cards
 * video4linux video interface
 *
 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
 *
 *  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/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
28
#include <linux/sort.h>
L
Linus Torvalds 已提交
29 30 31

#include "saa7134-reg.h"
#include "saa7134.h"
32
#include <media/v4l2-common.h>
33
#include <media/rds.h>
L
Linus Torvalds 已提交
34 35 36

/* ------------------------------------------------------------------ */

37
unsigned int video_debug;
L
Linus Torvalds 已提交
38
static unsigned int gbuffers      = 8;
39
static unsigned int noninterlaced; /* 0 */
L
Linus Torvalds 已提交
40 41
static unsigned int gbufsize      = 720*576*4;
static unsigned int gbufsize_max  = 720*576*4;
42
static char secam[] = "--";
L
Linus Torvalds 已提交
43 44 45 46 47
module_param(video_debug, int, 0644);
MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
module_param(gbuffers, int, 0444);
MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
module_param(noninterlaced, int, 0644);
48
MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
49 50 51
module_param_string(secam, secam, sizeof(secam), 0644);
MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");

L
Linus Torvalds 已提交
52

53
#define dprintk(fmt, arg...)	if (video_debug&0x04) \
L
Linus Torvalds 已提交
54 55
	printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/* ------------------------------------------------------------------ */
/* Defines for Video Output Port Register at address 0x191            */

/* Bit 0: VIP code T bit polarity */

#define VP_T_CODE_P_NON_INVERTED	0x00
#define VP_T_CODE_P_INVERTED		0x01

/* ------------------------------------------------------------------ */
/* Defines for Video Output Port Register at address 0x195            */

/* Bit 2: Video output clock delay control */

#define VP_CLK_CTRL2_NOT_DELAYED	0x00
#define VP_CLK_CTRL2_DELAYED		0x04

/* Bit 1: Video output clock invert control */

#define VP_CLK_CTRL1_NON_INVERTED	0x00
#define VP_CLK_CTRL1_INVERTED		0x02

/* ------------------------------------------------------------------ */
/* Defines for Video Output Port Register at address 0x196            */

/* Bits 2 to 0: VSYNC pin video vertical sync type */

#define VP_VS_TYPE_MASK			0x07

#define VP_VS_TYPE_OFF			0x00
#define VP_VS_TYPE_V123			0x01
#define VP_VS_TYPE_V_ITU		0x02
#define VP_VS_TYPE_VGATE_L		0x03
#define VP_VS_TYPE_RESERVED1		0x04
#define VP_VS_TYPE_RESERVED2		0x05
#define VP_VS_TYPE_F_ITU		0x06
#define VP_VS_TYPE_SC_FID		0x07

L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 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 189 190 191 192 193 194 195 196 197 198 199 200
/* ------------------------------------------------------------------ */
/* data structs for video                                             */

static int video_out[][9] = {
	[CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
};

static struct saa7134_format formats[] = {
	{
		.name     = "8 bpp gray",
		.fourcc   = V4L2_PIX_FMT_GREY,
		.depth    = 8,
		.pm       = 0x06,
	},{
		.name     = "15 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_RGB555,
		.depth    = 16,
		.pm       = 0x13 | 0x80,
	},{
		.name     = "15 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB555X,
		.depth    = 16,
		.pm       = 0x13 | 0x80,
		.bswap    = 1,
	},{
		.name     = "16 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_RGB565,
		.depth    = 16,
		.pm       = 0x10 | 0x80,
	},{
		.name     = "16 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB565X,
		.depth    = 16,
		.pm       = 0x10 | 0x80,
		.bswap    = 1,
	},{
		.name     = "24 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_BGR24,
		.depth    = 24,
		.pm       = 0x11,
	},{
		.name     = "24 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB24,
		.depth    = 24,
		.pm       = 0x11,
		.bswap    = 1,
	},{
		.name     = "32 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_BGR32,
		.depth    = 32,
		.pm       = 0x12,
	},{
		.name     = "32 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB32,
		.depth    = 32,
		.pm       = 0x12,
		.bswap    = 1,
		.wswap    = 1,
	},{
		.name     = "4:2:2 packed, YUYV",
		.fourcc   = V4L2_PIX_FMT_YUYV,
		.depth    = 16,
		.pm       = 0x00,
		.bswap    = 1,
		.yuv      = 1,
	},{
		.name     = "4:2:2 packed, UYVY",
		.fourcc   = V4L2_PIX_FMT_UYVY,
		.depth    = 16,
		.pm       = 0x00,
		.yuv      = 1,
	},{
		.name     = "4:2:2 planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV422P,
		.depth    = 16,
		.pm       = 0x09,
		.yuv      = 1,
		.planar   = 1,
		.hshift   = 1,
		.vshift   = 0,
	},{
		.name     = "4:2:0 planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV420,
		.depth    = 12,
		.pm       = 0x0a,
		.yuv      = 1,
		.planar   = 1,
		.hshift   = 1,
		.vshift   = 1,
	},{
		.name     = "4:2:0 planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YVU420,
		.depth    = 12,
		.pm       = 0x0a,
		.yuv      = 1,
		.planar   = 1,
		.uvswap   = 1,
		.hshift   = 1,
		.vshift   = 1,
	}
};
#define FORMATS ARRAY_SIZE(formats)

#define NORM_625_50			\
		.h_start       = 0,	\
		.h_stop        = 719,	\
		.video_v_start = 24,	\
		.video_v_stop  = 311,	\
201 202 203
		.vbi_v_start_0 = 7,	\
		.vbi_v_stop_0  = 22,	\
		.vbi_v_start_1 = 319,   \
L
Linus Torvalds 已提交
204 205 206 207
		.src_timing    = 4

#define NORM_525_60			\
		.h_start       = 0,	\
208
		.h_stop        = 719,	\
209 210 211 212 213 214
		.video_v_start = 23,	\
		.video_v_stop  = 262,	\
		.vbi_v_start_0 = 10,	\
		.vbi_v_stop_0  = 21,	\
		.vbi_v_start_1 = 273,	\
		.src_timing    = 7
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

static struct saa7134_tvnorm tvnorms[] = {
	{
		.name          = "PAL", /* autodetect */
		.id            = V4L2_STD_PAL,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0x81,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x06,
		.vgate_misc    = 0x1c,

	},{
		.name          = "PAL-BG",
		.id            = V4L2_STD_PAL_BG,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0x81,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x06,
		.vgate_misc    = 0x1c,

	},{
		.name          = "PAL-I",
		.id            = V4L2_STD_PAL_I,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0x81,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x06,
		.vgate_misc    = 0x1c,

	},{
		.name          = "PAL-DK",
		.id            = V4L2_STD_PAL_DK,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0x81,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x06,
		.vgate_misc    = 0x1c,

	},{
		.name          = "NTSC",
		.id            = V4L2_STD_NTSC,
		NORM_525_60,

		.sync_control  = 0x59,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0x89,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x0e,
		.vgate_misc    = 0x18,

	},{
		.name          = "SECAM",
		.id            = V4L2_STD_SECAM,
		NORM_625_50,

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
		.sync_control  = 0x18,
		.luma_control  = 0x1b,
		.chroma_ctrl1  = 0xd1,
		.chroma_gain   = 0x80,
		.chroma_ctrl2  = 0x00,
		.vgate_misc    = 0x1c,

	},{
		.name          = "SECAM-DK",
		.id            = V4L2_STD_SECAM_DK,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x1b,
		.chroma_ctrl1  = 0xd1,
		.chroma_gain   = 0x80,
		.chroma_ctrl2  = 0x00,
		.vgate_misc    = 0x1c,

	},{
		.name          = "SECAM-L",
		.id            = V4L2_STD_SECAM_L,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x1b,
		.chroma_ctrl1  = 0xd1,
		.chroma_gain   = 0x80,
		.chroma_ctrl2  = 0x00,
		.vgate_misc    = 0x1c,

	},{
		.name          = "SECAM-Lc",
		.id            = V4L2_STD_SECAM_LC,
		NORM_625_50,

		.sync_control  = 0x18,
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		.luma_control  = 0x1b,
		.chroma_ctrl1  = 0xd1,
		.chroma_gain   = 0x80,
		.chroma_ctrl2  = 0x00,
		.vgate_misc    = 0x1c,

	},{
		.name          = "PAL-M",
		.id            = V4L2_STD_PAL_M,
		NORM_525_60,

		.sync_control  = 0x59,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0xb9,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x0e,
		.vgate_misc    = 0x18,

	},{
		.name          = "PAL-Nc",
		.id            = V4L2_STD_PAL_Nc,
		NORM_625_50,

		.sync_control  = 0x18,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0xa1,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x06,
		.vgate_misc    = 0x1c,

	},{
		.name          = "PAL-60",
		.id            = V4L2_STD_PAL_60,

		.h_start       = 0,
		.h_stop        = 719,
355 356 357 358 359 360
		.video_v_start = 23,
		.video_v_stop  = 262,
		.vbi_v_start_0 = 10,
		.vbi_v_stop_0  = 21,
		.vbi_v_start_1 = 273,
		.src_timing    = 7,
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

		.sync_control  = 0x18,
		.luma_control  = 0x40,
		.chroma_ctrl1  = 0x81,
		.chroma_gain   = 0x2a,
		.chroma_ctrl2  = 0x06,
		.vgate_misc    = 0x1c,
	}
};
#define TVNORMS ARRAY_SIZE(tvnorms)

#define V4L2_CID_PRIVATE_INVERT      (V4L2_CID_PRIVATE_BASE + 0)
#define V4L2_CID_PRIVATE_Y_ODD       (V4L2_CID_PRIVATE_BASE + 1)
#define V4L2_CID_PRIVATE_Y_EVEN      (V4L2_CID_PRIVATE_BASE + 2)
#define V4L2_CID_PRIVATE_AUTOMUTE    (V4L2_CID_PRIVATE_BASE + 3)
#define V4L2_CID_PRIVATE_LASTP1      (V4L2_CID_PRIVATE_BASE + 4)

static const struct v4l2_queryctrl no_ctrl = {
	.name  = "42",
	.flags = V4L2_CTRL_FLAG_DISABLED,
};
static const struct v4l2_queryctrl video_ctrls[] = {
	/* --- video --- */
	{
		.id            = V4L2_CID_BRIGHTNESS,
		.name          = "Brightness",
		.minimum       = 0,
		.maximum       = 255,
		.step          = 1,
		.default_value = 128,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_CONTRAST,
		.name          = "Contrast",
		.minimum       = 0,
		.maximum       = 127,
		.step          = 1,
		.default_value = 68,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_SATURATION,
		.name          = "Saturation",
		.minimum       = 0,
		.maximum       = 127,
		.step          = 1,
		.default_value = 64,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_HUE,
		.name          = "Hue",
		.minimum       = -128,
		.maximum       = 127,
		.step          = 1,
		.default_value = 0,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
417 418
		.id            = V4L2_CID_HFLIP,
		.name          = "Mirror",
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		.minimum       = 0,
		.maximum       = 1,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
	},
	/* --- audio --- */
	{
		.id            = V4L2_CID_AUDIO_MUTE,
		.name          = "Mute",
		.minimum       = 0,
		.maximum       = 1,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
	},{
		.id            = V4L2_CID_AUDIO_VOLUME,
		.name          = "Volume",
		.minimum       = -15,
		.maximum       = 15,
		.step          = 1,
		.default_value = 0,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},
	/* --- private --- */
	{
		.id            = V4L2_CID_PRIVATE_INVERT,
		.name          = "Invert",
		.minimum       = 0,
		.maximum       = 1,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
	},{
		.id            = V4L2_CID_PRIVATE_Y_ODD,
		.name          = "y offset odd field",
		.minimum       = 0,
		.maximum       = 128,
451
		.step          = 1,
L
Linus Torvalds 已提交
452 453 454 455 456 457 458
		.default_value = 0,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_PRIVATE_Y_EVEN,
		.name          = "y offset even field",
		.minimum       = 0,
		.maximum       = 128,
459
		.step          = 1,
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
		.default_value = 0,
		.type          = V4L2_CTRL_TYPE_INTEGER,
	},{
		.id            = V4L2_CID_PRIVATE_AUTOMUTE,
		.name          = "automute",
		.minimum       = 0,
		.maximum       = 1,
		.default_value = 1,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
	}
};
static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);

static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
{
	unsigned int i;

	for (i = 0; i < CTRLS; i++)
		if (video_ctrls[i].id == id)
			return video_ctrls+i;
	return NULL;
}

static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
{
	unsigned int i;

	for (i = 0; i < FORMATS; i++)
		if (formats[i].fourcc == fourcc)
			return formats+i;
	return NULL;
}

/* ----------------------------------------------------------------------- */
/* resource management                                                     */

static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
{
	if (fh->resources & bit)
		/* have it already allocated */
		return 1;

	/* is it free? */
503
	mutex_lock(&dev->lock);
L
Linus Torvalds 已提交
504 505
	if (dev->resources & bit) {
		/* no, someone else uses it */
506
		mutex_unlock(&dev->lock);
L
Linus Torvalds 已提交
507 508 509 510 511 512
		return 0;
	}
	/* it's free, grab it */
	fh->resources  |= bit;
	dev->resources |= bit;
	dprintk("res: get %d\n",bit);
513
	mutex_unlock(&dev->lock);
L
Linus Torvalds 已提交
514 515 516
	return 1;
}

517
static int res_check(struct saa7134_fh *fh, unsigned int bit)
L
Linus Torvalds 已提交
518 519 520 521
{
	return (fh->resources & bit);
}

522
static int res_locked(struct saa7134_dev *dev, unsigned int bit)
L
Linus Torvalds 已提交
523 524 525 526 527 528 529
{
	return (dev->resources & bit);
}

static
void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
{
530
	BUG_ON((fh->resources & bits) != bits);
L
Linus Torvalds 已提交
531

532
	mutex_lock(&dev->lock);
L
Linus Torvalds 已提交
533 534 535
	fh->resources  &= ~bits;
	dev->resources &= ~bits;
	dprintk("res: put %d\n",bits);
536
	mutex_unlock(&dev->lock);
L
Linus Torvalds 已提交
537 538 539 540
}

/* ------------------------------------------------------------------ */

541
static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
L
Linus Torvalds 已提交
542 543 544 545 546 547 548 549 550 551
{
	dprintk("set tv norm = %s\n",norm->name);
	dev->tvnorm = norm;

	/* setup cropping */
	dev->crop_bounds.left    = norm->h_start;
	dev->crop_defrect.left   = norm->h_start;
	dev->crop_bounds.width   = norm->h_stop - norm->h_start +1;
	dev->crop_defrect.width  = norm->h_stop - norm->h_start +1;

552
	dev->crop_bounds.top     = (norm->vbi_v_stop_0+1)*2;
L
Linus Torvalds 已提交
553 554 555 556 557 558 559
	dev->crop_defrect.top    = norm->video_v_start*2;
	dev->crop_bounds.height  = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
		- dev->crop_bounds.top;
	dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;

	dev->crop_current = dev->crop_defrect;

560
	saa7134_set_tvnorm_hw(dev);
561 562 563 564 565 566 567 568 569 570
}

static void video_mux(struct saa7134_dev *dev, int input)
{
	dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
	dev->ctl_input = input;
	set_tvnorm(dev, dev->tvnorm);
	saa7134_tvaudio_setinput(dev, &card_in(dev, input));
}

571 572

static void saa7134_set_decoder(struct saa7134_dev *dev)
573 574 575 576 577 578 579 580 581 582 583 584 585 586
{
	int luma_control, sync_control, mux;

	struct saa7134_tvnorm *norm = dev->tvnorm;
	mux = card_in(dev, dev->ctl_input).vmux;

	luma_control = norm->luma_control;
	sync_control = norm->sync_control;

	if (mux > 5)
		luma_control |= 0x80; /* svideo */
	if (noninterlaced || dev->nosignal)
		sync_control |= 0x20;

L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	/* setup video decoder */
	saa_writeb(SAA7134_INCR_DELAY,            0x08);
	saa_writeb(SAA7134_ANALOG_IN_CTRL1,       0xc0 | mux);
	saa_writeb(SAA7134_ANALOG_IN_CTRL2,       0x00);

	saa_writeb(SAA7134_ANALOG_IN_CTRL3,       0x90);
	saa_writeb(SAA7134_ANALOG_IN_CTRL4,       0x90);
	saa_writeb(SAA7134_HSYNC_START,           0xeb);
	saa_writeb(SAA7134_HSYNC_STOP,            0xe0);
	saa_writeb(SAA7134_SOURCE_TIMING1,        norm->src_timing);

	saa_writeb(SAA7134_SYNC_CTRL,             sync_control);
	saa_writeb(SAA7134_LUMA_CTRL,             luma_control);
	saa_writeb(SAA7134_DEC_LUMA_BRIGHT,       dev->ctl_bright);

602 603 604 605 606 607
	saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
		dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);

	saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
		dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);

L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	saa_writeb(SAA7134_DEC_CHROMA_HUE,        dev->ctl_hue);
	saa_writeb(SAA7134_CHROMA_CTRL1,          norm->chroma_ctrl1);
	saa_writeb(SAA7134_CHROMA_GAIN,           norm->chroma_gain);

	saa_writeb(SAA7134_CHROMA_CTRL2,          norm->chroma_ctrl2);
	saa_writeb(SAA7134_MODE_DELAY_CTRL,       0x00);

	saa_writeb(SAA7134_ANALOG_ADC,            0x01);
	saa_writeb(SAA7134_VGATE_START,           0x11);
	saa_writeb(SAA7134_VGATE_STOP,            0xfe);
	saa_writeb(SAA7134_MISC_VGATE_MSB,        norm->vgate_misc);
	saa_writeb(SAA7134_RAW_DATA_GAIN,         0x40);
	saa_writeb(SAA7134_RAW_DATA_OFFSET,       0x80);
}

623 624 625 626
void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
{
	saa7134_set_decoder(dev);

627
	if (card_in(dev, dev->ctl_input).tv)
628
		saa_call_all(dev, core, s_std, dev->tvnorm->id);
629 630
	/* Set the correct norm for the saa6752hs. This function
	   does nothing if there is no saa6752hs. */
631
	saa_call_empress(dev, core, s_std, dev->tvnorm->id);
632 633
}

L
Linus Torvalds 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
{
	static const struct {
		int xpsc;
		int xacl;
		int xc2_1;
		int xdcg;
		int vpfy;
	} vals[] = {
		/* XPSC XACL XC2_1 XDCG VPFY */
		{    1,   0,    0,    0,   0 },
		{    2,   2,    1,    2,   2 },
		{    3,   4,    1,    3,   2 },
		{    4,   8,    1,    4,   2 },
		{    5,   8,    1,    4,   2 },
		{    6,   8,    1,    4,   3 },
		{    7,   8,    1,    4,   3 },
		{    8,  15,    0,    4,   3 },
		{    9,  15,    0,    4,   3 },
		{   10,  16,    1,    5,   3 },
	};
	static const int count = ARRAY_SIZE(vals);
	int i;

	for (i = 0; i < count; i++)
		if (vals[i].xpsc == prescale)
			break;
	if (i == count)
		return;

	saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
	saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
	saa_writeb(SAA7134_LEVEL_CTRL(task),
		   (vals[i].xc2_1 << 3) | (vals[i].xdcg));
	saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
		   (vals[i].vpfy << 2) | vals[i].vpfy);
}

static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
{
	int val,mirror;

	saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale &  0xff);
	saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);

	mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
	if (yscale < 2048) {
		/* LPI */
		dprintk("yscale LPI yscale=%d\n",yscale);
		saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
		saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
		saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
	} else {
		/* ACM */
		val = 0x40 * 1024 / yscale;
		dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
		saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
		saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
		saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
	}
	saa_writeb(SAA7134_LUMA_BRIGHT(task),       0x80);
}

static void set_size(struct saa7134_dev *dev, int task,
		     int width, int height, int interlace)
{
	int prescale,xscale,yscale,y_even,y_odd;
	int h_start, h_stop, v_start, v_stop;
	int div = interlace ? 2 : 1;

	/* setup video scaler */
	h_start = dev->crop_current.left;
	v_start = dev->crop_current.top/2;
	h_stop  = (dev->crop_current.left + dev->crop_current.width -1);
	v_stop  = (dev->crop_current.top + dev->crop_current.height -1)/2;

	saa_writeb(SAA7134_VIDEO_H_START1(task), h_start &  0xff);
	saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
	saa_writeb(SAA7134_VIDEO_H_STOP1(task),  h_stop  &  0xff);
	saa_writeb(SAA7134_VIDEO_H_STOP2(task),  h_stop  >> 8);
	saa_writeb(SAA7134_VIDEO_V_START1(task), v_start &  0xff);
	saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
	saa_writeb(SAA7134_VIDEO_V_STOP1(task),  v_stop  &  0xff);
	saa_writeb(SAA7134_VIDEO_V_STOP2(task),  v_stop  >> 8);

	prescale = dev->crop_current.width / width;
	if (0 == prescale)
		prescale = 1;
	xscale = 1024 * dev->crop_current.width / prescale / width;
	yscale = 512 * div * dev->crop_current.height / height;
724
	dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
	set_h_prescale(dev,task,prescale);
	saa_writeb(SAA7134_H_SCALE_INC1(task),      xscale &  0xff);
	saa_writeb(SAA7134_H_SCALE_INC2(task),      xscale >> 8);
	set_v_scale(dev,task,yscale);

	saa_writeb(SAA7134_VIDEO_PIXELS1(task),     width  & 0xff);
	saa_writeb(SAA7134_VIDEO_PIXELS2(task),     width  >> 8);
	saa_writeb(SAA7134_VIDEO_LINES1(task),      height/div & 0xff);
	saa_writeb(SAA7134_VIDEO_LINES2(task),      height/div >> 8);

	/* deinterlace y offsets */
	y_odd  = dev->ctl_y_odd;
	y_even = dev->ctl_y_even;
	saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
	saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
	saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
	saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
}

/* ------------------------------------------------------------------ */

struct cliplist {
	__u16 position;
	__u8  enable;
	__u8  disable;
};

static void set_cliplist(struct saa7134_dev *dev, int reg,
			struct cliplist *cl, int entries, char *name)
{
	__u8 winbits = 0;
	int i;

	for (i = 0; i < entries; i++) {
		winbits |= cl[i].enable;
		winbits &= ~cl[i].disable;
		if (i < 15 && cl[i].position == cl[i+1].position)
			continue;
		saa_writeb(reg + 0, winbits);
		saa_writeb(reg + 2, cl[i].position & 0xff);
		saa_writeb(reg + 3, cl[i].position >> 8);
		dprintk("clip: %s winbits=%02x pos=%d\n",
			name,winbits,cl[i].position);
		reg += 8;
	}
	for (; reg < 0x400; reg += 8) {
		saa_writeb(reg+ 0, 0);
		saa_writeb(reg + 1, 0);
		saa_writeb(reg + 2, 0);
		saa_writeb(reg + 3, 0);
	}
}

static int clip_range(int val)
{
	if (val < 0)
		val = 0;
	return val;
}

785 786 787 788 789 790 791 792 793 794 795 796
/* Sort into smallest position first order */
static int cliplist_cmp(const void *a, const void *b)
{
	const struct cliplist *cla = a;
	const struct cliplist *clb = b;
	if (cla->position < clb->position)
		return -1;
	if (cla->position > clb->position)
		return 1;
	return 0;
}

L
Linus Torvalds 已提交
797 798 799 800
static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
			  int nclips, int interlace)
{
	struct cliplist col[16], row[16];
801
	int cols = 0, rows = 0, i;
L
Linus Torvalds 已提交
802 803
	int div = interlace ? 2 : 1;

804 805
	memset(col, 0, sizeof(col));
	memset(row, 0, sizeof(row));
L
Linus Torvalds 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	for (i = 0; i < nclips && i < 8; i++) {
		col[cols].position = clip_range(clips[i].c.left);
		col[cols].enable   = (1 << i);
		cols++;
		col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
		col[cols].disable  = (1 << i);
		cols++;
		row[rows].position = clip_range(clips[i].c.top / div);
		row[rows].enable   = (1 << i);
		rows++;
		row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
						/ div);
		row[rows].disable  = (1 << i);
		rows++;
	}
821 822
	sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
	sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
L
Linus Torvalds 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	set_cliplist(dev,0x380,col,cols,"cols");
	set_cliplist(dev,0x384,row,rows,"rows");
	return 0;
}

static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
{
	enum v4l2_field field;
	int maxw, maxh;

	if (NULL == dev->ovbuf.base)
		return -EINVAL;
	if (NULL == dev->ovfmt)
		return -EINVAL;
	if (win->w.width < 48 || win->w.height <  32)
		return -EINVAL;
	if (win->clipcount > 2048)
		return -EINVAL;

	field = win->field;
	maxw  = dev->crop_current.width;
	maxh  = dev->crop_current.height;

	if (V4L2_FIELD_ANY == field) {
847 848 849 850 851 852 853 854 855 856 857 858 859 860
		field = (win->w.height > maxh/2)
			? V4L2_FIELD_INTERLACED
			: V4L2_FIELD_TOP;
	}
	switch (field) {
	case V4L2_FIELD_TOP:
	case V4L2_FIELD_BOTTOM:
		maxh = maxh / 2;
		break;
	case V4L2_FIELD_INTERLACED:
		break;
	default:
		return -EINVAL;
	}
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

	win->field = field;
	if (win->w.width > maxw)
		win->w.width = maxw;
	if (win->w.height > maxh)
		win->w.height = maxh;
	return 0;
}

static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
{
	unsigned long base,control,bpl;
	int err;

	err = verify_preview(dev,&fh->win);
	if (0 != err)
		return err;

	dev->ovfield = fh->win.field;
	dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
		fh->win.w.width,fh->win.w.height,
		fh->win.w.left,fh->win.w.top,
		dev->ovfmt->name,v4l2_field_names[dev->ovfield]);

	/* setup window + clipping */
	set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
		 V4L2_FIELD_HAS_BOTH(dev->ovfield));
	setup_clipping(dev,fh->clips,fh->nclips,
		       V4L2_FIELD_HAS_BOTH(dev->ovfield));
	if (dev->ovfmt->yuv)
		saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
	else
		saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
	saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);

	/* dma: setup channel 1 (= Video Task B) */
	base  = (unsigned long)dev->ovbuf.base;
	base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
	base += dev->ovfmt->depth/8         * fh->win.w.left;
	bpl   = dev->ovbuf.fmt.bytesperline;
	control = SAA7134_RS_CONTROL_BURST_16;
	if (dev->ovfmt->bswap)
		control |= SAA7134_RS_CONTROL_BSWAP;
	if (dev->ovfmt->wswap)
		control |= SAA7134_RS_CONTROL_WSWAP;
	if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
		saa_writel(SAA7134_RS_BA1(1),base);
		saa_writel(SAA7134_RS_BA2(1),base+bpl);
		saa_writel(SAA7134_RS_PITCH(1),bpl*2);
		saa_writel(SAA7134_RS_CONTROL(1),control);
	} else {
		saa_writel(SAA7134_RS_BA1(1),base);
		saa_writel(SAA7134_RS_BA2(1),base);
		saa_writel(SAA7134_RS_PITCH(1),bpl);
		saa_writel(SAA7134_RS_CONTROL(1),control);
	}

	/* start dma */
	dev->ovenable = 1;
	saa7134_set_dmabits(dev);

	return 0;
}

static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
{
	dev->ovenable = 0;
	saa7134_set_dmabits(dev);
	return 0;
}

/* ------------------------------------------------------------------ */

static int buffer_activate(struct saa7134_dev *dev,
			   struct saa7134_buf *buf,
			   struct saa7134_buf *next)
{
	unsigned long base,control,bpl;
	unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */

	dprintk("buffer_activate buf=%p\n",buf);
942
	buf->vb.state = VIDEOBUF_ACTIVE;
L
Linus Torvalds 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	buf->top_seen = 0;

	set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
		 V4L2_FIELD_HAS_BOTH(buf->vb.field));
	if (buf->fmt->yuv)
		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
	else
		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
	saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);

	/* DMA: setup channel 0 (= Video Task A0) */
	base  = saa7134_buffer_base(buf);
	if (buf->fmt->planar)
		bpl = buf->vb.width;
	else
		bpl = (buf->vb.width * buf->fmt->depth) / 8;
	control = SAA7134_RS_CONTROL_BURST_16 |
		SAA7134_RS_CONTROL_ME |
		(buf->pt->dma >> 12);
	if (buf->fmt->bswap)
		control |= SAA7134_RS_CONTROL_BSWAP;
	if (buf->fmt->wswap)
		control |= SAA7134_RS_CONTROL_WSWAP;
	if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
		/* interlaced */
		saa_writel(SAA7134_RS_BA1(0),base);
		saa_writel(SAA7134_RS_BA2(0),base+bpl);
		saa_writel(SAA7134_RS_PITCH(0),bpl*2);
	} else {
		/* non-interlaced */
		saa_writel(SAA7134_RS_BA1(0),base);
		saa_writel(SAA7134_RS_BA2(0),base);
		saa_writel(SAA7134_RS_PITCH(0),bpl);
	}
	saa_writel(SAA7134_RS_CONTROL(0),control);

	if (buf->fmt->planar) {
		/* DMA: setup channel 4+5 (= planar task A) */
		bpl_uv   = bpl >> buf->fmt->hshift;
		lines_uv = buf->vb.height >> buf->fmt->vshift;
		base2    = base + bpl * buf->vb.height;
		base3    = base2 + bpl_uv * lines_uv;
		if (buf->fmt->uvswap)
			tmp = base2, base2 = base3, base3 = tmp;
		dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
			bpl_uv,lines_uv,base2,base3);
		if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
			/* interlaced */
			saa_writel(SAA7134_RS_BA1(4),base2);
			saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
			saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
			saa_writel(SAA7134_RS_BA1(5),base3);
			saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
			saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
		} else {
			/* non-interlaced */
			saa_writel(SAA7134_RS_BA1(4),base2);
			saa_writel(SAA7134_RS_BA2(4),base2);
			saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
			saa_writel(SAA7134_RS_BA1(5),base3);
			saa_writel(SAA7134_RS_BA2(5),base3);
			saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
		}
		saa_writel(SAA7134_RS_CONTROL(4),control);
		saa_writel(SAA7134_RS_CONTROL(5),control);
	}

	/* start DMA */
	saa7134_set_dmabits(dev);
	mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
	return 0;
}

static int buffer_prepare(struct videobuf_queue *q,
			  struct videobuf_buffer *vb,
			  enum v4l2_field field)
{
	struct saa7134_fh *fh = q->priv_data;
	struct saa7134_dev *dev = fh->dev;
	struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
	unsigned int size;
	int err;

	/* sanity checks */
	if (NULL == fh->fmt)
		return -EINVAL;
	if (fh->width    < 48 ||
	    fh->height   < 32 ||
	    fh->width/4  > dev->crop_current.width  ||
	    fh->height/4 > dev->crop_current.height ||
	    fh->width    > dev->crop_bounds.width  ||
	    fh->height   > dev->crop_bounds.height)
		return -EINVAL;
	size = (fh->width * fh->height * fh->fmt->depth) >> 3;
	if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
		return -EINVAL;

	dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
		vb->i,fh->width,fh->height,size,v4l2_field_names[field],
		fh->fmt->name);
	if (buf->vb.width  != fh->width  ||
	    buf->vb.height != fh->height ||
	    buf->vb.size   != size       ||
	    buf->vb.field  != field      ||
	    buf->fmt       != fh->fmt) {
1048
		saa7134_dma_free(q,buf);
L
Linus Torvalds 已提交
1049 1050
	}

1051
	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1052 1053
		struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);

L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059
		buf->vb.width  = fh->width;
		buf->vb.height = fh->height;
		buf->vb.size   = size;
		buf->vb.field  = field;
		buf->fmt       = fh->fmt;
		buf->pt        = &fh->pt_cap;
1060
		dev->video_q.curr = NULL;
L
Linus Torvalds 已提交
1061

1062
		err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
L
Linus Torvalds 已提交
1063 1064 1065
		if (err)
			goto oops;
		err = saa7134_pgtable_build(dev->pci,buf->pt,
1066 1067
					    dma->sglist,
					    dma->sglen,
L
Linus Torvalds 已提交
1068 1069 1070 1071
					    saa7134_buffer_startpage(buf));
		if (err)
			goto oops;
	}
1072
	buf->vb.state = VIDEOBUF_PREPARED;
L
Linus Torvalds 已提交
1073 1074 1075 1076
	buf->activate = buffer_activate;
	return 0;

 oops:
1077
	saa7134_dma_free(q,buf);
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	return err;
}

static int
buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
{
	struct saa7134_fh *fh = q->priv_data;

	*size = fh->fmt->depth * fh->width * fh->height >> 3;
	if (0 == *count)
		*count = gbuffers;
	*count = saa7134_buffer_count(*size,*count);
	return 0;
}

static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
	struct saa7134_fh *fh = q->priv_data;
	struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);

	saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
}

static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
	struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);

1105
	saa7134_dma_free(q,buf);
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
}

static struct videobuf_queue_ops video_qops = {
	.buf_setup    = buffer_setup,
	.buf_prepare  = buffer_prepare,
	.buf_queue    = buffer_queue,
	.buf_release  = buffer_release,
};

/* ------------------------------------------------------------------ */

1117
int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
{
	const struct v4l2_queryctrl* ctrl;

	ctrl = ctrl_by_id(c->id);
	if (NULL == ctrl)
		return -EINVAL;
	switch (c->id) {
	case V4L2_CID_BRIGHTNESS:
		c->value = dev->ctl_bright;
		break;
	case V4L2_CID_HUE:
		c->value = dev->ctl_hue;
		break;
	case V4L2_CID_CONTRAST:
		c->value = dev->ctl_contrast;
		break;
	case V4L2_CID_SATURATION:
		c->value = dev->ctl_saturation;
		break;
	case V4L2_CID_AUDIO_MUTE:
		c->value = dev->ctl_mute;
		break;
	case V4L2_CID_AUDIO_VOLUME:
		c->value = dev->ctl_volume;
		break;
	case V4L2_CID_PRIVATE_INVERT:
		c->value = dev->ctl_invert;
		break;
1146
	case V4L2_CID_HFLIP:
L
Linus Torvalds 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
		c->value = dev->ctl_mirror;
		break;
	case V4L2_CID_PRIVATE_Y_EVEN:
		c->value = dev->ctl_y_even;
		break;
	case V4L2_CID_PRIVATE_Y_ODD:
		c->value = dev->ctl_y_odd;
		break;
	case V4L2_CID_PRIVATE_AUTOMUTE:
		c->value = dev->ctl_automute;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}
1163
EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
L
Linus Torvalds 已提交
1164

1165 1166 1167 1168 1169 1170 1171 1172
static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
{
	struct saa7134_fh *fh = priv;

	return saa7134_g_ctrl_internal(fh->dev, fh, c);
}

int saa7134_s_ctrl_internal(struct saa7134_dev *dev,  struct saa7134_fh *fh, struct v4l2_control *c)
L
Linus Torvalds 已提交
1173 1174 1175 1176
{
	const struct v4l2_queryctrl* ctrl;
	unsigned long flags;
	int restart_overlay = 0;
1177
	int err;
1178

1179 1180 1181 1182 1183 1184 1185 1186 1187
	/* When called from the empress code fh == NULL.
	   That needs to be fixed somehow, but for now this is
	   good enough. */
	if (fh) {
		err = v4l2_prio_check(&dev->prio, &fh->prio);
		if (0 != err)
			return err;
	}
	err = -EINVAL;
L
Linus Torvalds 已提交
1188

1189
	mutex_lock(&dev->lock);
1190

L
Linus Torvalds 已提交
1191 1192
	ctrl = ctrl_by_id(c->id);
	if (NULL == ctrl)
1193 1194
		goto error;

L
Linus Torvalds 已提交
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 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_BOOLEAN:
	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_INTEGER:
		if (c->value < ctrl->minimum)
			c->value = ctrl->minimum;
		if (c->value > ctrl->maximum)
			c->value = ctrl->maximum;
		break;
	default:
		/* nothing */;
	};
	switch (c->id) {
	case V4L2_CID_BRIGHTNESS:
		dev->ctl_bright = c->value;
		saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
		break;
	case V4L2_CID_HUE:
		dev->ctl_hue = c->value;
		saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
		break;
	case V4L2_CID_CONTRAST:
		dev->ctl_contrast = c->value;
		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
		break;
	case V4L2_CID_SATURATION:
		dev->ctl_saturation = c->value;
		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
		break;
	case V4L2_CID_AUDIO_MUTE:
		dev->ctl_mute = c->value;
		saa7134_tvaudio_setmute(dev);
		break;
	case V4L2_CID_AUDIO_VOLUME:
		dev->ctl_volume = c->value;
		saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
		break;
	case V4L2_CID_PRIVATE_INVERT:
		dev->ctl_invert = c->value;
		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
		break;
1242
	case V4L2_CID_HFLIP:
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
		dev->ctl_mirror = c->value;
		restart_overlay = 1;
		break;
	case V4L2_CID_PRIVATE_Y_EVEN:
		dev->ctl_y_even = c->value;
		restart_overlay = 1;
		break;
	case V4L2_CID_PRIVATE_Y_ODD:
		dev->ctl_y_odd = c->value;
		restart_overlay = 1;
		break;
	case V4L2_CID_PRIVATE_AUTOMUTE:
1255 1256 1257 1258 1259 1260
	{
		struct v4l2_priv_tun_config tda9887_cfg;

		tda9887_cfg.tuner = TUNER_TDA9887;
		tda9887_cfg.priv = &dev->tda9887_conf;

L
Linus Torvalds 已提交
1261 1262 1263 1264 1265 1266
		dev->ctl_automute = c->value;
		if (dev->tda9887_conf) {
			if (dev->ctl_automute)
				dev->tda9887_conf |= TDA9887_AUTOMUTE;
			else
				dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1267

1268
			saa_call_all(dev, tuner, s_config, &tda9887_cfg);
L
Linus Torvalds 已提交
1269 1270
		}
		break;
1271
	}
L
Linus Torvalds 已提交
1272
	default:
1273
		goto error;
L
Linus Torvalds 已提交
1274 1275 1276 1277 1278 1279 1280
	}
	if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
		spin_lock_irqsave(&dev->slock,flags);
		stop_preview(dev,fh);
		start_preview(dev,fh);
		spin_unlock_irqrestore(&dev->slock,flags);
	}
1281 1282 1283
	err = 0;

error:
1284
	mutex_unlock(&dev->lock);
1285
	return err;
L
Linus Torvalds 已提交
1286
}
1287 1288 1289 1290 1291 1292 1293 1294
EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);

static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
{
	struct saa7134_fh *fh = f;

	return saa7134_s_ctrl_internal(fh->dev, fh, c);
}
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

/* ------------------------------------------------------------------ */

static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
{
	struct videobuf_queue* q = NULL;

	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		q = &fh->cap;
		break;
	case V4L2_BUF_TYPE_VBI_CAPTURE:
		q = &fh->vbi;
		break;
	default:
		BUG();
	}
	return q;
}

static int saa7134_resource(struct saa7134_fh *fh)
{
1317 1318
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return RESOURCE_VIDEO;
L
Linus Torvalds 已提交
1319

1320 1321 1322 1323 1324
	if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		return RESOURCE_VBI;

	BUG();
	return 0;
L
Linus Torvalds 已提交
1325 1326
}

1327
static int video_open(struct file *file)
L
Linus Torvalds 已提交
1328
{
1329 1330
	struct video_device *vdev = video_devdata(file);
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1331
	struct saa7134_fh *fh;
1332
	enum v4l2_buf_type type = 0;
L
Linus Torvalds 已提交
1333
	int radio = 0;
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
	switch (vdev->vfl_type) {
	case VFL_TYPE_GRABBER:
		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		break;
	case VFL_TYPE_VBI:
		type = V4L2_BUF_TYPE_VBI_CAPTURE;
		break;
	case VFL_TYPE_RADIO:
		radio = 1;
		break;
L
Linus Torvalds 已提交
1345 1346
	}

1347 1348
	dprintk("open dev=%s radio=%d type=%s\n", video_device_node_name(vdev),
		radio, v4l2_type_names[type]);
L
Linus Torvalds 已提交
1349 1350

	/* allocate + initialize per filehandle data */
P
 
Panagiotis Issaris 已提交
1351
	fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1352
	if (NULL == fh)
L
Linus Torvalds 已提交
1353
		return -ENOMEM;
1354

L
Linus Torvalds 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363
	file->private_data = fh;
	fh->dev      = dev;
	fh->radio    = radio;
	fh->type     = type;
	fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
	fh->width    = 720;
	fh->height   = 576;
	v4l2_prio_open(&dev->prio,&fh->prio);

1364 1365
	videobuf_queue_sg_init(&fh->cap, &video_qops,
			    &dev->pci->dev, &dev->slock,
L
Linus Torvalds 已提交
1366 1367 1368 1369
			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
			    V4L2_FIELD_INTERLACED,
			    sizeof(struct saa7134_buf),
			    fh);
1370 1371
	videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
			    &dev->pci->dev, &dev->slock,
L
Linus Torvalds 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
			    V4L2_BUF_TYPE_VBI_CAPTURE,
			    V4L2_FIELD_SEQ_TB,
			    sizeof(struct saa7134_buf),
			    fh);
	saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
	saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);

	if (fh->radio) {
		/* switch to radio mode */
		saa7134_tvaudio_setinput(dev,&card(dev).radio);
1382
		saa_call_all(dev, tuner, s_radio);
L
Linus Torvalds 已提交
1383 1384 1385 1386
	} else {
		/* switch to video/vbi mode */
		video_mux(dev,dev->ctl_input);
	}
1387
	return 0;
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
}

static ssize_t
video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
	struct saa7134_fh *fh = file->private_data;

	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		if (res_locked(fh->dev,RESOURCE_VIDEO))
			return -EBUSY;
		return videobuf_read_one(saa7134_queue(fh),
					 data, count, ppos,
					 file->f_flags & O_NONBLOCK);
	case V4L2_BUF_TYPE_VBI_CAPTURE:
		if (!res_get(fh->dev,fh,RESOURCE_VBI))
			return -EBUSY;
		return videobuf_read_stream(saa7134_queue(fh),
					    data, count, ppos, 1,
					    file->f_flags & O_NONBLOCK);
		break;
	default:
		BUG();
		return 0;
	}
}

static unsigned int
video_poll(struct file *file, struct poll_table_struct *wait)
{
	struct saa7134_fh *fh = file->private_data;
	struct videobuf_buffer *buf = NULL;
1420
	unsigned int rc = 0;
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425

	if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
		return videobuf_poll_stream(file, &fh->vbi, wait);

	if (res_check(fh,RESOURCE_VIDEO)) {
1426
		mutex_lock(&fh->cap.vb_lock);
L
Linus Torvalds 已提交
1427 1428 1429
		if (!list_empty(&fh->cap.stream))
			buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
	} else {
1430
		mutex_lock(&fh->cap.vb_lock);
L
Linus Torvalds 已提交
1431
		if (UNSET == fh->cap.read_off) {
1432
			/* need to capture a new frame */
1433 1434 1435 1436
			if (res_locked(fh->dev,RESOURCE_VIDEO))
				goto err;
			if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
				goto err;
1437 1438
			fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
			fh->cap.read_off = 0;
L
Linus Torvalds 已提交
1439 1440 1441 1442 1443
		}
		buf = fh->cap.read_buf;
	}

	if (!buf)
1444
		goto err;
L
Linus Torvalds 已提交
1445 1446

	poll_wait(file, &buf->done, wait);
1447 1448
	if (buf->state == VIDEOBUF_DONE ||
	    buf->state == VIDEOBUF_ERROR)
1449 1450 1451
		rc = POLLIN|POLLRDNORM;
	mutex_unlock(&fh->cap.vb_lock);
	return rc;
1452 1453 1454 1455

err:
	mutex_unlock(&fh->cap.vb_lock);
	return POLLERR;
L
Linus Torvalds 已提交
1456 1457
}

1458
static int video_release(struct file *file)
L
Linus Torvalds 已提交
1459 1460 1461
{
	struct saa7134_fh  *fh  = file->private_data;
	struct saa7134_dev *dev = fh->dev;
1462
	struct rds_command cmd;
L
Linus Torvalds 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
	unsigned long flags;

	/* turn off overlay */
	if (res_check(fh, RESOURCE_OVERLAY)) {
		spin_lock_irqsave(&dev->slock,flags);
		stop_preview(dev,fh);
		spin_unlock_irqrestore(&dev->slock,flags);
		res_free(dev,fh,RESOURCE_OVERLAY);
	}

	/* stop video capture */
	if (res_check(fh, RESOURCE_VIDEO)) {
		videobuf_streamoff(&fh->cap);
		res_free(dev,fh,RESOURCE_VIDEO);
	}
	if (fh->cap.read_buf) {
		buffer_release(&fh->cap,fh->cap.read_buf);
		kfree(fh->cap.read_buf);
	}

	/* stop vbi capture */
	if (res_check(fh, RESOURCE_VBI)) {
1485
		videobuf_stop(&fh->vbi);
L
Linus Torvalds 已提交
1486 1487 1488
		res_free(dev,fh,RESOURCE_VBI);
	}

1489 1490 1491 1492 1493 1494
	/* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
	saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
	saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
	saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
	saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);

1495
	saa_call_all(dev, core, s_power, 0);
1496 1497
	if (fh->radio)
		saa_call_all(dev, core, ioctl, RDS_CMD_CLOSE, &cmd);
1498

L
Linus Torvalds 已提交
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	/* free stuff */
	videobuf_mmap_free(&fh->cap);
	videobuf_mmap_free(&fh->vbi);
	saa7134_pgtable_free(dev->pci,&fh->pt_cap);
	saa7134_pgtable_free(dev->pci,&fh->pt_vbi);

	v4l2_prio_close(&dev->prio,&fh->prio);
	file->private_data = NULL;
	kfree(fh);
	return 0;
}

1511
static int video_mmap(struct file *file, struct vm_area_struct * vma)
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517
{
	struct saa7134_fh *fh = file->private_data;

	return videobuf_mmap_mapper(saa7134_queue(fh), vma);
}

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
static ssize_t radio_read(struct file *file, char __user *data,
			 size_t count, loff_t *ppos)
{
	struct saa7134_fh *fh = file->private_data;
	struct saa7134_dev *dev = fh->dev;
	struct rds_command cmd;

	cmd.block_count = count/3;
	cmd.buffer = data;
	cmd.instance = file;
	cmd.result = -ENODEV;

	saa_call_all(dev, core, ioctl, RDS_CMD_READ, &cmd);

	return cmd.result;
}

static unsigned int radio_poll(struct file *file, poll_table *wait)
{
	struct saa7134_fh *fh = file->private_data;
	struct saa7134_dev *dev = fh->dev;
	struct rds_command cmd;

	cmd.instance = file;
	cmd.event_list = wait;
	cmd.result = -ENODEV;
	saa_call_all(dev, core, ioctl, RDS_CMD_POLL, &cmd);

	return cmd.result;
}

L
Linus Torvalds 已提交
1549 1550
/* ------------------------------------------------------------------ */

1551
static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1552
						struct v4l2_format *f)
L
Linus Torvalds 已提交
1553
{
1554 1555
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561
	struct saa7134_tvnorm *norm = dev->tvnorm;

	f->fmt.vbi.sampling_rate = 6750000 * 4;
	f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
	f->fmt.vbi.offset = 64 * 4;
1562 1563 1564
	f->fmt.vbi.start[0] = norm->vbi_v_start_0;
	f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
	f->fmt.vbi.start[1] = norm->vbi_v_start_1;
L
Linus Torvalds 已提交
1565 1566 1567
	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
	f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */

1568
	return 0;
L
Linus Torvalds 已提交
1569 1570
}

1571
static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1572
				struct v4l2_format *f)
L
Linus Torvalds 已提交
1573
{
1574 1575
	struct saa7134_fh *fh = priv;

1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
	f->fmt.pix.width        = fh->width;
	f->fmt.pix.height       = fh->height;
	f->fmt.pix.field        = fh->cap.field;
	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
	f->fmt.pix.bytesperline =
		(f->fmt.pix.width * fh->fmt->depth) >> 3;
	f->fmt.pix.sizeimage =
		f->fmt.pix.height * f->fmt.pix.bytesperline;
	return 0;
}

1587
static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1588 1589 1590 1591 1592 1593
				struct v4l2_format *f)
{
	struct saa7134_fh *fh = priv;

	if (saa7134_no_overlay > 0) {
		printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
L
Linus Torvalds 已提交
1594 1595
		return -EINVAL;
	}
1596 1597 1598
	f->fmt.win = fh->win;

	return 0;
L
Linus Torvalds 已提交
1599 1600
}

1601
static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1602
						struct v4l2_format *f)
L
Linus Torvalds 已提交
1603
{
1604 1605
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
1606 1607 1608
	struct saa7134_format *fmt;
	enum v4l2_field field;
	unsigned int maxw, maxh;
L
Linus Torvalds 已提交
1609

1610 1611 1612
	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	if (NULL == fmt)
		return -EINVAL;
L
Linus Torvalds 已提交
1613

1614 1615 1616
	field = f->fmt.pix.field;
	maxw  = min(dev->crop_current.width*4,  dev->crop_bounds.width);
	maxh  = min(dev->crop_current.height*4, dev->crop_bounds.height);
L
Linus Torvalds 已提交
1617

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
	if (V4L2_FIELD_ANY == field) {
		field = (f->fmt.pix.height > maxh/2)
			? V4L2_FIELD_INTERLACED
			: V4L2_FIELD_BOTTOM;
	}
	switch (field) {
	case V4L2_FIELD_TOP:
	case V4L2_FIELD_BOTTOM:
		maxh = maxh / 2;
		break;
	case V4L2_FIELD_INTERLACED:
		break;
	default:
		return -EINVAL;
	}
L
Linus Torvalds 已提交
1633

1634
	f->fmt.pix.field = field;
1635 1636 1637 1638 1639 1640 1641 1642 1643
	if (f->fmt.pix.width  < 48)
		f->fmt.pix.width  = 48;
	if (f->fmt.pix.height < 32)
		f->fmt.pix.height = 32;
	if (f->fmt.pix.width > maxw)
		f->fmt.pix.width = maxw;
	if (f->fmt.pix.height > maxh)
		f->fmt.pix.height = maxh;
	f->fmt.pix.width &= ~0x03;
1644 1645 1646 1647
	f->fmt.pix.bytesperline =
		(f->fmt.pix.width * fmt->depth) >> 3;
	f->fmt.pix.sizeimage =
		f->fmt.pix.height * f->fmt.pix.bytesperline;
L
Linus Torvalds 已提交
1648

1649 1650
	return 0;
}
L
Linus Torvalds 已提交
1651

1652
static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1653 1654 1655 1656 1657 1658 1659
						struct v4l2_format *f)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;

	if (saa7134_no_overlay > 0) {
		printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
L
Linus Torvalds 已提交
1660 1661
		return -EINVAL;
	}
1662 1663

	return verify_preview(dev, &f->fmt.win);
L
Linus Torvalds 已提交
1664 1665
}

1666
static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1667
					struct v4l2_format *f)
1668 1669 1670 1671
{
	struct saa7134_fh *fh = priv;
	int err;

1672
	err = saa7134_try_fmt_vid_cap(file, priv, f);
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	if (0 != err)
		return err;

	fh->fmt       = format_by_fourcc(f->fmt.pix.pixelformat);
	fh->width     = f->fmt.pix.width;
	fh->height    = f->fmt.pix.height;
	fh->cap.field = f->fmt.pix.field;
	return 0;
}

1683
static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1684
					struct v4l2_format *f)
L
Linus Torvalds 已提交
1685
{
1686 1687
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
1688
	int err;
S
Steven Rostedt 已提交
1689
	unsigned long flags;
L
Linus Torvalds 已提交
1690

1691 1692 1693 1694 1695 1696 1697
	if (saa7134_no_overlay > 0) {
		printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
		return -EINVAL;
	}
	err = verify_preview(dev, &f->fmt.win);
	if (0 != err)
		return err;
L
Linus Torvalds 已提交
1698

1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	mutex_lock(&dev->lock);

	fh->win    = f->fmt.win;
	fh->nclips = f->fmt.win.clipcount;

	if (fh->nclips > 8)
		fh->nclips = 8;

	if (copy_from_user(fh->clips, f->fmt.win.clips,
			   sizeof(struct v4l2_clip)*fh->nclips)) {
1709
		mutex_unlock(&dev->lock);
1710
		return -EFAULT;
L
Linus Torvalds 已提交
1711
	}
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721

	if (res_check(fh, RESOURCE_OVERLAY)) {
		spin_lock_irqsave(&dev->slock, flags);
		stop_preview(dev, fh);
		start_preview(dev, fh);
		spin_unlock_irqrestore(&dev->slock, flags);
	}

	mutex_unlock(&dev->lock);
	return 0;
L
Linus Torvalds 已提交
1722 1723
}

1724
int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1725 1726
{
	const struct v4l2_queryctrl *ctrl;
L
Linus Torvalds 已提交
1727

1728 1729 1730 1731 1732 1733 1734
	if ((c->id <  V4L2_CID_BASE ||
	     c->id >= V4L2_CID_LASTP1) &&
	    (c->id <  V4L2_CID_PRIVATE_BASE ||
	     c->id >= V4L2_CID_PRIVATE_LASTP1))
		return -EINVAL;
	ctrl = ctrl_by_id(c->id);
	*c = (NULL != ctrl) ? *ctrl : no_ctrl;
L
Linus Torvalds 已提交
1735 1736
	return 0;
}
1737
EXPORT_SYMBOL_GPL(saa7134_queryctrl);
L
Linus Torvalds 已提交
1738

1739
static int saa7134_enum_input(struct file *file, void *priv,
1740
					struct v4l2_input *i)
L
Linus Torvalds 已提交
1741
{
1742
	struct saa7134_fh *fh = priv;
L
Linus Torvalds 已提交
1743
	struct saa7134_dev *dev = fh->dev;
1744
	unsigned int n;
L
Linus Torvalds 已提交
1745

1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
	n = i->index;
	if (n >= SAA7134_INPUT_MAX)
		return -EINVAL;
	if (NULL == card_in(dev, i->index).name)
		return -EINVAL;
	memset(i, 0, sizeof(*i));
	i->index = n;
	i->type  = V4L2_INPUT_TYPE_CAMERA;
	strcpy(i->name, card_in(dev, n).name);
	if (card_in(dev, n).tv)
		i->type = V4L2_INPUT_TYPE_TUNER;
	i->audioset = 1;
	if (n == dev->ctl_input) {
		int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
		int v2 = saa_readb(SAA7134_STATUS_VIDEO2);

		if (0 != (v1 & 0x40))
			i->status |= V4L2_IN_ST_NO_H_LOCK;
		if (0 != (v2 & 0x40))
			i->status |= V4L2_IN_ST_NO_SYNC;
		if (0 != (v2 & 0x0e))
			i->status |= V4L2_IN_ST_MACROVISION;
	}
1769
	i->std = SAA7134_NORMS;
1770 1771
	return 0;
}
L
Linus Torvalds 已提交
1772

1773
static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1774 1775 1776 1777 1778 1779 1780 1781
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;

	*i = dev->ctl_input;
	return 0;
}

1782
static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1783 1784 1785
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
1786 1787 1788 1789 1790
	int err;

	err = v4l2_prio_check(&dev->prio, &fh->prio);
	if (0 != err)
		return err;
1791

1792
	if (i >= SAA7134_INPUT_MAX)
1793 1794 1795 1796 1797 1798 1799 1800 1801
		return -EINVAL;
	if (NULL == card_in(dev, i).name)
		return -EINVAL;
	mutex_lock(&dev->lock);
	video_mux(dev, i);
	mutex_unlock(&dev->lock);
	return 0;
}

1802
static int saa7134_querycap(struct file *file, void  *priv,
1803 1804 1805 1806
					struct v4l2_capability *cap)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
1807

1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
	unsigned int tuner_type = dev->tuner_type;

	strcpy(cap->driver, "saa7134");
	strlcpy(cap->card, saa7134_boards[dev->board].name,
		sizeof(cap->card));
	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
	cap->version = SAA7134_VERSION_CODE;
	cap->capabilities =
		V4L2_CAP_VIDEO_CAPTURE |
		V4L2_CAP_VBI_CAPTURE |
		V4L2_CAP_READWRITE |
		V4L2_CAP_STREAMING |
		V4L2_CAP_TUNER;
1821 1822
	if (dev->has_rds)
		cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
1823 1824 1825 1826 1827 1828 1829
	if (saa7134_no_overlay <= 0)
		cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;

	if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
		cap->capabilities &= ~V4L2_CAP_TUNER;
		return 0;
}
1830

1831
int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
1832 1833 1834 1835
{
	unsigned long flags;
	unsigned int i;
	v4l2_std_id fixup;
1836 1837
	int err;

1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
	/* When called from the empress code fh == NULL.
	   That needs to be fixed somehow, but for now this is
	   good enough. */
	if (fh) {
		err = v4l2_prio_check(&dev->prio, &fh->prio);
		if (0 != err)
			return err;
	} else if (res_locked(dev, RESOURCE_OVERLAY)) {
		/* Don't change the std from the mpeg device
		   if overlay is active. */
		return -EBUSY;
	}
1850

1851 1852 1853
	for (i = 0; i < TVNORMS; i++)
		if (*id == tvnorms[i].id)
			break;
1854

1855 1856 1857 1858 1859 1860
	if (i == TVNORMS)
		for (i = 0; i < TVNORMS; i++)
			if (*id & tvnorms[i].id)
				break;
	if (i == TVNORMS)
		return -EINVAL;
1861

1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
	if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
		if (secam[0] == 'L' || secam[0] == 'l') {
			if (secam[1] == 'C' || secam[1] == 'c')
				fixup = V4L2_STD_SECAM_LC;
			else
				fixup = V4L2_STD_SECAM_L;
		} else {
			if (secam[0] == 'D' || secam[0] == 'd')
				fixup = V4L2_STD_SECAM_DK;
			else
				fixup = V4L2_STD_SECAM;
		}
		for (i = 0; i < TVNORMS; i++)
			if (fixup == tvnorms[i].id)
				break;
L
Linus Torvalds 已提交
1877
	}
1878 1879 1880

	*id = tvnorms[i].id;

1881
	mutex_lock(&dev->lock);
1882
	if (fh && res_check(fh, RESOURCE_OVERLAY)) {
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
		spin_lock_irqsave(&dev->slock, flags);
		stop_preview(dev, fh);
		spin_unlock_irqrestore(&dev->slock, flags);

		set_tvnorm(dev, &tvnorms[i]);

		spin_lock_irqsave(&dev->slock, flags);
		start_preview(dev, fh);
		spin_unlock_irqrestore(&dev->slock, flags);
	} else
		set_tvnorm(dev, &tvnorms[i]);
1894

1895 1896 1897 1898
	saa7134_tvaudio_do_scan(dev);
	mutex_unlock(&dev->lock);
	return 0;
}
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
EXPORT_SYMBOL_GPL(saa7134_s_std_internal);

static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
{
	struct saa7134_fh *fh = priv;

	return saa7134_s_std_internal(fh->dev, fh, id);
}

static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;

	*id = dev->tvnorm->id;
	return 0;
}
L
Linus Torvalds 已提交
1916

1917
static int saa7134_cropcap(struct file *file, void *priv,
1918 1919 1920 1921
					struct v4l2_cropcap *cap)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
1922

1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
	if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
		return -EINVAL;
	cap->bounds  = dev->crop_bounds;
	cap->defrect = dev->crop_defrect;
	cap->pixelaspect.numerator   = 1;
	cap->pixelaspect.denominator = 1;
	if (dev->tvnorm->id & V4L2_STD_525_60) {
		cap->pixelaspect.numerator   = 11;
		cap->pixelaspect.denominator = 10;
	}
	if (dev->tvnorm->id & V4L2_STD_625_50) {
		cap->pixelaspect.numerator   = 54;
		cap->pixelaspect.denominator = 59;
L
Linus Torvalds 已提交
1937
	}
1938 1939
	return 0;
}
L
Linus Torvalds 已提交
1940

1941
static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;

	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
		return -EINVAL;
	crop->c = dev->crop_current;
	return 0;
}

1953
static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;
	struct v4l2_rect *b = &dev->crop_bounds;

	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
		return -EINVAL;
	if (crop->c.height < 0)
		return -EINVAL;
	if (crop->c.width < 0)
		return -EINVAL;

	if (res_locked(fh->dev, RESOURCE_OVERLAY))
		return -EBUSY;
	if (res_locked(fh->dev, RESOURCE_VIDEO))
		return -EBUSY;

	if (crop->c.top < b->top)
		crop->c.top = b->top;
	if (crop->c.top > b->top + b->height)
		crop->c.top = b->top + b->height;
	if (crop->c.height > b->top - crop->c.top + b->height)
		crop->c.height = b->top - crop->c.top + b->height;

	if (crop->c.left < b->left)
		crop->c.left = b->left;
	if (crop->c.left > b->left + b->width)
		crop->c.left = b->left + b->width;
	if (crop->c.width > b->left - crop->c.left + b->width)
		crop->c.width = b->left - crop->c.left + b->width;

	dev->crop_current = crop->c;
	return 0;
}

1990
static int saa7134_g_tuner(struct file *file, void *priv,
1991 1992 1993 1994 1995
					struct v4l2_tuner *t)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
	int n;
L
Linus Torvalds 已提交
1996

1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
	if (0 != t->index)
		return -EINVAL;
	memset(t, 0, sizeof(*t));
	for (n = 0; n < SAA7134_INPUT_MAX; n++)
		if (card_in(dev, n).tv)
			break;
	if (NULL != card_in(dev, n).name) {
		strcpy(t->name, "Television");
		t->type = V4L2_TUNER_ANALOG_TV;
		t->capability = V4L2_TUNER_CAP_NORM |
			V4L2_TUNER_CAP_STEREO |
			V4L2_TUNER_CAP_LANG1 |
			V4L2_TUNER_CAP_LANG2;
		t->rangehigh = 0xffffffffUL;
		t->rxsubchans = saa7134_tvaudio_getstereo(dev);
		t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
	}
	if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
		t->signal = 0xffff;
	return 0;
}
2018

2019
static int saa7134_s_tuner(struct file *file, void *priv,
2020 2021 2022 2023
					struct v4l2_tuner *t)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
2024 2025 2026 2027 2028
	int rx, mode, err;

	err = v4l2_prio_check(&dev->prio, &fh->prio);
	if (0 != err)
		return err;
2029

2030 2031 2032 2033
	mode = dev->thread.mode;
	if (UNSET == mode) {
		rx   = saa7134_tvaudio_getstereo(dev);
		mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
L
Linus Torvalds 已提交
2034
	}
2035 2036
	if (mode != t->audmode)
		dev->thread.mode = t->audmode;
2037

2038 2039
	return 0;
}
L
Linus Torvalds 已提交
2040

2041
static int saa7134_g_frequency(struct file *file, void *priv,
2042 2043 2044 2045
					struct v4l2_frequency *f)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
2046

2047 2048
	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
	f->frequency = dev->ctl_freq;
2049

2050 2051
	return 0;
}
L
Linus Torvalds 已提交
2052

2053
static int saa7134_s_frequency(struct file *file, void *priv,
2054 2055 2056 2057
					struct v4l2_frequency *f)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
2058 2059 2060 2061 2062
	int err;

	err = v4l2_prio_check(&dev->prio, &fh->prio);
	if (0 != err)
		return err;
L
Linus Torvalds 已提交
2063

2064 2065 2066 2067 2068 2069 2070 2071
	if (0 != f->tuner)
		return -EINVAL;
	if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
		return -EINVAL;
	if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
		return -EINVAL;
	mutex_lock(&dev->lock);
	dev->ctl_freq = f->frequency;
L
Linus Torvalds 已提交
2072

2073
	saa_call_all(dev, tuner, s_frequency, f);
L
Linus Torvalds 已提交
2074

2075 2076 2077 2078
	saa7134_tvaudio_do_scan(dev);
	mutex_unlock(&dev->lock);
	return 0;
}
L
Linus Torvalds 已提交
2079

2080
static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2081 2082 2083 2084
{
	strcpy(a->name, "audio");
	return 0;
}
L
Linus Torvalds 已提交
2085

2086
static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2087 2088 2089
{
	return 0;
}
L
Linus Torvalds 已提交
2090

2091
static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2092 2093 2094 2095
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;

2096
	*p = v4l2_prio_max(&dev->prio);
2097 2098
	return 0;
}
2099

2100
static int saa7134_s_priority(struct file *file, void *f,
2101
					enum v4l2_priority prio)
2102 2103 2104
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;
2105

2106
	return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2107
}
L
Linus Torvalds 已提交
2108

2109
static int saa7134_enum_fmt_vid_cap(struct file *file, void  *priv,
2110
					struct v4l2_fmtdesc *f)
2111
{
2112 2113
	if (f->index >= FORMATS)
		return -EINVAL;
L
Linus Torvalds 已提交
2114

2115 2116
	strlcpy(f->description, formats[f->index].name,
		sizeof(f->description));
L
Linus Torvalds 已提交
2117

2118
	f->pixelformat = formats[f->index].fourcc;
L
Linus Torvalds 已提交
2119

2120 2121 2122
	return 0;
}

2123
static int saa7134_enum_fmt_vid_overlay(struct file *file, void  *priv,
2124 2125 2126 2127
					struct v4l2_fmtdesc *f)
{
	if (saa7134_no_overlay > 0) {
		printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2128 2129
		return -EINVAL;
	}
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141

	if ((f->index >= FORMATS) || formats[f->index].planar)
		return -EINVAL;

	strlcpy(f->description, formats[f->index].name,
		sizeof(f->description));

	f->pixelformat = formats[f->index].fourcc;

	return 0;
}

2142
static int saa7134_g_fbuf(struct file *file, void *f,
2143 2144 2145 2146
				struct v4l2_framebuffer *fb)
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
2147

2148 2149
	*fb = dev->ovbuf;
	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2150

2151 2152
	return 0;
}
L
Linus Torvalds 已提交
2153

2154
static int saa7134_s_fbuf(struct file *file, void *f,
2155 2156 2157 2158 2159 2160 2161 2162 2163
					struct v4l2_framebuffer *fb)
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;
	struct saa7134_format *fmt;

	if (!capable(CAP_SYS_ADMIN) &&
	   !capable(CAP_SYS_RAWIO))
		return -EPERM;
L
Linus Torvalds 已提交
2164

2165 2166 2167 2168
	/* check args */
	fmt = format_by_fourcc(fb->fmt.pixelformat);
	if (NULL == fmt)
		return -EINVAL;
L
Linus Torvalds 已提交
2169

2170 2171 2172 2173 2174 2175 2176 2177
	/* ok, accept it */
	dev->ovbuf = *fb;
	dev->ovfmt = fmt;
	if (0 == dev->ovbuf.fmt.bytesperline)
		dev->ovbuf.fmt.bytesperline =
			dev->ovbuf.fmt.width*fmt->depth/8;
	return 0;
}
L
Linus Torvalds 已提交
2178

2179
static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
{
	struct saa7134_fh *fh = f;
	struct saa7134_dev *dev = fh->dev;
	unsigned long flags;

	if (on) {
		if (saa7134_no_overlay > 0) {
			dprintk("no_overlay\n");
			return -EINVAL;
		}
L
Linus Torvalds 已提交
2190

2191
		if (!res_get(dev, fh, RESOURCE_OVERLAY))
L
Linus Torvalds 已提交
2192
			return -EBUSY;
2193 2194 2195
		spin_lock_irqsave(&dev->slock, flags);
		start_preview(dev, fh);
		spin_unlock_irqrestore(&dev->slock, flags);
L
Linus Torvalds 已提交
2196
	}
2197 2198 2199 2200 2201 2202 2203
	if (!on) {
		if (!res_check(fh, RESOURCE_OVERLAY))
			return -EINVAL;
		spin_lock_irqsave(&dev->slock, flags);
		stop_preview(dev, fh);
		spin_unlock_irqrestore(&dev->slock, flags);
		res_free(dev, fh, RESOURCE_OVERLAY);
L
Linus Torvalds 已提交
2204
	}
2205 2206 2207
	return 0;
}

2208 2209
#ifdef CONFIG_VIDEO_V4L1_COMPAT
static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2210
{
2211 2212 2213 2214
	struct saa7134_fh *fh = file->private_data;
	return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
}
#endif
L
Linus Torvalds 已提交
2215

2216
static int saa7134_reqbufs(struct file *file, void *priv,
2217 2218 2219 2220 2221
					struct v4l2_requestbuffers *p)
{
	struct saa7134_fh *fh = priv;
	return videobuf_reqbufs(saa7134_queue(fh), p);
}
2222

2223
static int saa7134_querybuf(struct file *file, void *priv,
2224 2225 2226 2227 2228
					struct v4l2_buffer *b)
{
	struct saa7134_fh *fh = priv;
	return videobuf_querybuf(saa7134_queue(fh), b);
}
2229

2230
static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2231 2232 2233 2234 2235
{
	struct saa7134_fh *fh = priv;
	return videobuf_qbuf(saa7134_queue(fh), b);
}

2236
static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2237 2238 2239 2240 2241 2242
{
	struct saa7134_fh *fh = priv;
	return videobuf_dqbuf(saa7134_queue(fh), b,
				file->f_flags & O_NONBLOCK);
}

2243
static int saa7134_streamon(struct file *file, void *priv,
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
					enum v4l2_buf_type type)
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
	int res = saa7134_resource(fh);

	if (!res_get(dev, fh, res))
		return -EBUSY;

	return videobuf_streamon(saa7134_queue(fh));
}

2256
static int saa7134_streamoff(struct file *file, void *priv,
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
					enum v4l2_buf_type type)
{
	int err;
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;
	int res = saa7134_resource(fh);

	err = videobuf_streamoff(saa7134_queue(fh));
	if (err < 0)
		return err;
	res_free(dev, fh, res);
L
Linus Torvalds 已提交
2268 2269 2270
	return 0;
}

2271
static int saa7134_g_parm(struct file *file, void *fh,
2272
				struct v4l2_streamparm *parm)
L
Linus Torvalds 已提交
2273
{
2274
	return 0;
L
Linus Torvalds 已提交
2275 2276
}

2277 2278
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int vidioc_g_register (struct file *file, void *priv,
2279
			      struct v4l2_dbg_register *reg)
2280 2281 2282 2283
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;

2284
	if (!v4l2_chip_match_host(&reg->match))
2285 2286
		return -EINVAL;
	reg->val = saa_readb(reg->reg);
2287
	reg->size = 1;
2288 2289 2290 2291
	return 0;
}

static int vidioc_s_register (struct file *file, void *priv,
2292
				struct v4l2_dbg_register *reg)
2293 2294 2295 2296
{
	struct saa7134_fh *fh = priv;
	struct saa7134_dev *dev = fh->dev;

2297
	if (!v4l2_chip_match_host(&reg->match))
2298 2299 2300 2301 2302 2303
		return -EINVAL;
	saa_writeb(reg->reg&0xffffff, reg->val);
	return 0;
}
#endif

2304 2305
static int radio_querycap(struct file *file, void *priv,
					struct v4l2_capability *cap)
L
Linus Torvalds 已提交
2306 2307 2308 2309
{
	struct saa7134_fh *fh = file->private_data;
	struct saa7134_dev *dev = fh->dev;

2310 2311 2312 2313 2314 2315 2316
	strcpy(cap->driver, "saa7134");
	strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
	cap->version = SAA7134_VERSION_CODE;
	cap->capabilities = V4L2_CAP_TUNER;
	return 0;
}
L
Linus Torvalds 已提交
2317

2318 2319 2320 2321 2322
static int radio_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *t)
{
	struct saa7134_fh *fh = file->private_data;
	struct saa7134_dev *dev = fh->dev;
L
Linus Torvalds 已提交
2323

2324 2325
	if (0 != t->index)
		return -EINVAL;
L
Linus Torvalds 已提交
2326

2327 2328 2329 2330
	memset(t, 0, sizeof(*t));
	strcpy(t->name, "Radio");
	t->type = V4L2_TUNER_RADIO;

2331
	saa_call_all(dev, tuner, g_tuner, t);
2332 2333 2334 2335
	if (dev->input->amux == TV) {
		t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
		t->rxsubchans = (saa_readb(0x529) & 0x08) ?
				V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2336
	}
2337 2338 2339 2340 2341 2342 2343
	return 0;
}
static int radio_s_tuner(struct file *file, void *priv,
					struct v4l2_tuner *t)
{
	struct saa7134_fh *fh = file->private_data;
	struct saa7134_dev *dev = fh->dev;
2344

2345 2346 2347
	if (0 != t->index)
		return -EINVAL;

2348
	saa_call_all(dev, tuner, s_tuner, t);
2349 2350
	return 0;
}
2351

2352 2353 2354 2355 2356
static int radio_enum_input(struct file *file, void *priv,
					struct v4l2_input *i)
{
	if (i->index != 0)
		return -EINVAL;
2357

2358 2359
	strcpy(i->name, "Radio");
	i->type = V4L2_INPUT_TYPE_TUNER;
L
Linus Torvalds 已提交
2360

2361 2362
	return 0;
}
L
Linus Torvalds 已提交
2363

2364 2365 2366 2367 2368
static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
}
L
Linus Torvalds 已提交
2369

2370 2371 2372 2373 2374 2375 2376
static int radio_g_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
{
	memset(a, 0, sizeof(*a));
	strcpy(a->name, "Radio");
	return 0;
}
L
Linus Torvalds 已提交
2377

2378 2379 2380 2381 2382
static int radio_s_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
{
	return 0;
}
L
Linus Torvalds 已提交
2383

2384 2385 2386 2387
static int radio_s_input(struct file *filp, void *priv, unsigned int i)
{
	return 0;
}
L
Linus Torvalds 已提交
2388

2389 2390
static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
{
L
Linus Torvalds 已提交
2391 2392 2393
	return 0;
}

2394 2395
static int radio_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *c)
L
Linus Torvalds 已提交
2396
{
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
	const struct v4l2_queryctrl *ctrl;

	if (c->id <  V4L2_CID_BASE ||
	    c->id >= V4L2_CID_LASTP1)
		return -EINVAL;
	if (c->id == V4L2_CID_AUDIO_MUTE) {
		ctrl = ctrl_by_id(c->id);
		*c = *ctrl;
	} else
		*c = no_ctrl;
	return 0;
L
Linus Torvalds 已提交
2408 2409
}

2410
static const struct v4l2_file_operations video_fops =
L
Linus Torvalds 已提交
2411 2412 2413 2414 2415 2416 2417
{
	.owner	  = THIS_MODULE,
	.open	  = video_open,
	.release  = video_release,
	.read	  = video_read,
	.poll     = video_poll,
	.mmap	  = video_mmap,
2418
	.ioctl	  = video_ioctl2,
L
Linus Torvalds 已提交
2419 2420
};

2421
static const struct v4l2_ioctl_ops video_ioctl_ops = {
2422
	.vidioc_querycap		= saa7134_querycap,
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
	.vidioc_enum_fmt_vid_cap	= saa7134_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap		= saa7134_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap		= saa7134_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap		= saa7134_s_fmt_vid_cap,
	.vidioc_enum_fmt_vid_overlay	= saa7134_enum_fmt_vid_overlay,
	.vidioc_g_fmt_vid_overlay	= saa7134_g_fmt_vid_overlay,
	.vidioc_try_fmt_vid_overlay	= saa7134_try_fmt_vid_overlay,
	.vidioc_s_fmt_vid_overlay	= saa7134_s_fmt_vid_overlay,
	.vidioc_g_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
	.vidioc_try_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
	.vidioc_s_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
2434 2435 2436 2437 2438 2439 2440 2441
	.vidioc_g_audio			= saa7134_g_audio,
	.vidioc_s_audio			= saa7134_s_audio,
	.vidioc_cropcap			= saa7134_cropcap,
	.vidioc_reqbufs			= saa7134_reqbufs,
	.vidioc_querybuf		= saa7134_querybuf,
	.vidioc_qbuf			= saa7134_qbuf,
	.vidioc_dqbuf			= saa7134_dqbuf,
	.vidioc_s_std			= saa7134_s_std,
2442
	.vidioc_g_std			= saa7134_g_std,
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
	.vidioc_enum_input		= saa7134_enum_input,
	.vidioc_g_input			= saa7134_g_input,
	.vidioc_s_input			= saa7134_s_input,
	.vidioc_queryctrl		= saa7134_queryctrl,
	.vidioc_g_ctrl			= saa7134_g_ctrl,
	.vidioc_s_ctrl			= saa7134_s_ctrl,
	.vidioc_streamon		= saa7134_streamon,
	.vidioc_streamoff		= saa7134_streamoff,
	.vidioc_g_tuner			= saa7134_g_tuner,
	.vidioc_s_tuner			= saa7134_s_tuner,
2453 2454 2455
#ifdef CONFIG_VIDEO_V4L1_COMPAT
	.vidiocgmbuf			= vidiocgmbuf,
#endif
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
	.vidioc_g_crop			= saa7134_g_crop,
	.vidioc_s_crop			= saa7134_s_crop,
	.vidioc_g_fbuf			= saa7134_g_fbuf,
	.vidioc_s_fbuf			= saa7134_s_fbuf,
	.vidioc_overlay			= saa7134_overlay,
	.vidioc_g_priority		= saa7134_g_priority,
	.vidioc_s_priority		= saa7134_s_priority,
	.vidioc_g_parm			= saa7134_g_parm,
	.vidioc_g_frequency		= saa7134_g_frequency,
	.vidioc_s_frequency		= saa7134_s_frequency,
2466 2467 2468 2469
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register              = vidioc_g_register,
	.vidioc_s_register              = vidioc_s_register,
#endif
L
Linus Torvalds 已提交
2470 2471
};

2472
static const struct v4l2_file_operations radio_fops = {
2473 2474
	.owner	  = THIS_MODULE,
	.open	  = video_open,
2475
	.read     = radio_read,
2476 2477
	.release  = video_release,
	.ioctl	  = video_ioctl2,
2478
	.poll     = radio_poll,
2479 2480 2481
};

static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
	.vidioc_querycap	= radio_querycap,
	.vidioc_g_tuner		= radio_g_tuner,
	.vidioc_enum_input	= radio_enum_input,
	.vidioc_g_audio		= radio_g_audio,
	.vidioc_s_tuner		= radio_s_tuner,
	.vidioc_s_audio		= radio_s_audio,
	.vidioc_s_input		= radio_s_input,
	.vidioc_s_std		= radio_s_std,
	.vidioc_queryctrl	= radio_queryctrl,
	.vidioc_g_input		= radio_g_input,
2492 2493 2494 2495
	.vidioc_g_ctrl		= saa7134_g_ctrl,
	.vidioc_s_ctrl		= saa7134_s_ctrl,
	.vidioc_g_frequency	= saa7134_g_frequency,
	.vidioc_s_frequency	= saa7134_s_frequency,
L
Linus Torvalds 已提交
2496 2497
};

2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
/* ----------------------------------------------------------- */
/* exported stuff                                              */

struct video_device saa7134_video_template = {
	.name				= "saa7134-video",
	.fops				= &video_fops,
	.ioctl_ops 			= &video_ioctl_ops,
	.tvnorms			= SAA7134_NORMS,
	.current_norm			= V4L2_STD_PAL,
};

struct video_device saa7134_radio_template = {
	.name			= "saa7134-radio",
	.fops			= &radio_fops,
	.ioctl_ops 		= &radio_ioctl_ops,
};

L
Linus Torvalds 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
int saa7134_video_init1(struct saa7134_dev *dev)
{
	/* sanitycheck insmod options */
	if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
		gbuffers = 2;
	if (gbufsize < 0 || gbufsize > gbufsize_max)
		gbufsize = gbufsize_max;
	gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;

	/* put some sensible defaults into the data structures ... */
	dev->ctl_bright     = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
	dev->ctl_contrast   = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
	dev->ctl_hue        = ctrl_by_id(V4L2_CID_HUE)->default_value;
	dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
	dev->ctl_volume     = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
	dev->ctl_mute       = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
	dev->ctl_invert     = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
	dev->ctl_automute   = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;

	if (dev->tda9887_conf && dev->ctl_automute)
		dev->tda9887_conf |= TDA9887_AUTOMUTE;
	dev->automute       = 0;

2538
	INIT_LIST_HEAD(&dev->video_q.queue);
L
Linus Torvalds 已提交
2539 2540 2541 2542 2543
	init_timer(&dev->video_q.timeout);
	dev->video_q.timeout.function = saa7134_buffer_timeout;
	dev->video_q.timeout.data     = (unsigned long)(&dev->video_q);
	dev->video_q.dev              = dev;

2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
	if (saa7134_boards[dev->board].video_out)
		saa7134_videoport_init(dev);

	return 0;
}

int saa7134_videoport_init(struct saa7134_dev *dev)
{
	/* enable video output */
	int vo = saa7134_boards[dev->board].video_out;
	int video_reg;
	unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2556 2557

	/* Configure videoport */
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
	saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
	video_reg = video_out[vo][1];
	if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
		video_reg &= ~VP_T_CODE_P_INVERTED;
	saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
	saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
	saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
	video_reg = video_out[vo][5];
	if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
		video_reg &= ~VP_CLK_CTRL2_DELAYED;
	if (vid_port_opts & SET_CLOCK_INVERTED)
		video_reg |= VP_CLK_CTRL1_INVERTED;
	saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
	video_reg = video_out[vo][6];
	if (vid_port_opts & SET_VSYNC_OFF) {
		video_reg &= ~VP_VS_TYPE_MASK;
		video_reg |= VP_VS_TYPE_OFF;
	}
	saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
	saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
	saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
L
Linus Torvalds 已提交
2579

2580 2581 2582
	/* Start videoport */
	saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);

L
Linus Torvalds 已提交
2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
	return 0;
}

int saa7134_video_init2(struct saa7134_dev *dev)
{
	/* init video hw */
	set_tvnorm(dev,&tvnorms[0]);
	video_mux(dev,0);
	saa7134_tvaudio_setmute(dev);
	saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
	return 0;
}

2596
void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
{
	static const char *st[] = {
		"(no signal)", "NTSC", "PAL", "SECAM" };
	u32 st1,st2;

	st1 = saa_readb(SAA7134_STATUS_VIDEO1);
	st2 = saa_readb(SAA7134_STATUS_VIDEO2);
	dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
		(st1 & 0x40) ? "not locked" : "locked",
		(st2 & 0x40) ? "no"         : "yes",
		st[st1 & 0x03]);
2608
	dev->nosignal = (st1 & 0x40) || (st2 & 0x40)  || !(st2 & 0x1);
L
Linus Torvalds 已提交
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618

	if (dev->nosignal) {
		/* no video signal -> mute audio */
		if (dev->ctl_automute)
			dev->automute = 1;
		saa7134_tvaudio_setmute(dev);
	} else {
		/* wake up tvaudio audio carrier scan thread */
		saa7134_tvaudio_do_scan(dev);
	}
2619 2620 2621 2622 2623 2624

	if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
		saa_clearb(SAA7134_SYNC_CTRL, 0x20);
	else
		saa_setb(SAA7134_SYNC_CTRL, 0x20);

L
Linus Torvalds 已提交
2625 2626 2627 2628
	if (dev->mops && dev->mops->signal_change)
		dev->mops->signal_change(dev);
}

2629

L
Linus Torvalds 已提交
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
{
	enum v4l2_field field;

	spin_lock(&dev->slock);
	if (dev->video_q.curr) {
		dev->video_fieldcount++;
		field = dev->video_q.curr->vb.field;
		if (V4L2_FIELD_HAS_BOTH(field)) {
			/* make sure we have seen both fields */
			if ((status & 0x10) == 0x00) {
				dev->video_q.curr->top_seen = 1;
				goto done;
			}
			if (!dev->video_q.curr->top_seen)
				goto done;
		} else if (field == V4L2_FIELD_TOP) {
			if ((status & 0x10) != 0x10)
				goto done;
		} else if (field == V4L2_FIELD_BOTTOM) {
			if ((status & 0x10) != 0x00)
				goto done;
		}
		dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2654
		saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
L
Linus Torvalds 已提交
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
	}
	saa7134_buffer_next(dev,&dev->video_q);

 done:
	spin_unlock(&dev->slock);
}

/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */