saa7134-video.c 61.4 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
/*
 *
 * 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.
 */

23 24 25
#include "saa7134.h"
#include "saa7134-reg.h"

L
Linus Torvalds 已提交
26 27 28 29 30
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
31
#include <linux/sort.h>
L
Linus Torvalds 已提交
32

33
#include <media/v4l2-common.h>
34
#include <media/v4l2-event.h>
35
#include <media/i2c/saa6588.h>
L
Linus Torvalds 已提交
36 37 38

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

39
unsigned int video_debug;
L
Linus Torvalds 已提交
40
static unsigned int gbuffers      = 8;
41
static unsigned int noninterlaced; /* 0 */
L
Linus Torvalds 已提交
42 43
static unsigned int gbufsize      = 720*576*4;
static unsigned int gbufsize_max  = 720*576*4;
44
static char secam[] = "--";
L
Linus Torvalds 已提交
45 46 47 48 49
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);
50
MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
51 52 53
module_param_string(secam, secam, sizeof(secam), 0644);
MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");

L
Linus Torvalds 已提交
54

55 56 57 58
#define video_dbg(fmt, arg...) do { \
	if (video_debug & 0x04) \
		printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
	} while (0)
L
Linus Torvalds 已提交
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 93 94 95 96
/* ------------------------------------------------------------------ */
/* 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 已提交
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 201 202 203 204
/* ------------------------------------------------------------------ */
/* 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,	\
205
		.vbi_v_start_0 = 7,	\
206
		.vbi_v_stop_0  = 23,	\
207
		.vbi_v_start_1 = 319,   \
L
Linus Torvalds 已提交
208 209 210 211
		.src_timing    = 4

#define NORM_525_60			\
		.h_start       = 0,	\
212
		.h_stop        = 719,	\
213 214 215 216 217 218
		.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 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

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,

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		.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 已提交
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 355 356 357 358
		.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,
359 360 361 362 363 364
		.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 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

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

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

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

388
static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
L
Linus Torvalds 已提交
389
{
390
	video_dbg("set tv norm = %s\n", norm->name);
L
Linus Torvalds 已提交
391 392 393 394 395 396 397 398
	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;

399
	dev->crop_bounds.top     = (norm->vbi_v_stop_0+1)*2;
L
Linus Torvalds 已提交
400 401 402 403 404 405 406
	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;

407
	saa7134_set_tvnorm_hw(dev);
408 409 410 411
}

static void video_mux(struct saa7134_dev *dev, int input)
{
412 413
	video_dbg("video input = %d [%s]\n",
		  input, saa7134_input_name[card_in(dev, input).type]);
414 415 416 417 418
	dev->ctl_input = input;
	set_tvnorm(dev, dev->tvnorm);
	saa7134_tvaudio_setinput(dev, &card_in(dev, input));
}

419 420

static void saa7134_set_decoder(struct saa7134_dev *dev)
421
{
422
	int luma_control, sync_control, chroma_ctrl1, mux;
423 424 425 426 427 428

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

	luma_control = norm->luma_control;
	sync_control = norm->sync_control;
429
	chroma_ctrl1 = norm->chroma_ctrl1;
430 431 432 433 434 435

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

436 437 438 439 440 441
	/* switch on auto standard detection */
	sync_control |= SAA7134_SYNC_CTRL_AUFD;
	chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
	chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
	luma_control &= ~SAA7134_LUMA_CTRL_LDEL;

L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	/* 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);

457 458 459 460 461 462
	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 已提交
463
	saa_writeb(SAA7134_DEC_CHROMA_HUE,        dev->ctl_hue);
464
	saa_writeb(SAA7134_CHROMA_CTRL1,          chroma_ctrl1);
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477
	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);
}

478 479 480 481
void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
{
	saa7134_set_decoder(dev);

482
	saa_call_all(dev, video, s_std, dev->tvnorm->id);
483 484
	/* Set the correct norm for the saa6752hs. This function
	   does nothing if there is no saa6752hs. */
485
	saa_call_empress(dev, video, s_std, dev->tvnorm->id);
486 487
}

L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
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 */
536
		video_dbg("yscale LPI yscale=%d\n", yscale);
L
Linus Torvalds 已提交
537 538 539 540 541 542
		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;
543
		video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
		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;
578 579
	video_dbg("prescale=%d xscale=%d yscale=%d\n",
		  prescale, xscale, yscale);
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	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);
621
		video_dbg("clip: %s winbits=%02x pos=%d\n",
L
Linus Torvalds 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
			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;
}

640 641 642 643 644 645 646 647 648 649 650 651
/* 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 已提交
652 653 654 655
static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
			  int nclips, int interlace)
{
	struct cliplist col[16], row[16];
656
	int cols = 0, rows = 0, i;
L
Linus Torvalds 已提交
657 658
	int div = interlace ? 2 : 1;

659 660
	memset(col, 0, sizeof(col));
	memset(row, 0, sizeof(row));
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
	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++;
	}
676 677
	sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
	sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
L
Linus Torvalds 已提交
678 679 680 681 682
	set_cliplist(dev,0x380,col,cols,"cols");
	set_cliplist(dev,0x384,row,rows,"rows");
	return 0;
}

683
static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
L
Linus Torvalds 已提交
684 685 686 687
{
	enum v4l2_field field;
	int maxw, maxh;

688
	if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
L
Linus Torvalds 已提交
689
		return -EINVAL;
690 691 692 693 694 695 696 697 698
	if (win->w.width < 48)
		win->w.width = 48;
	if (win->w.height < 32)
		win->w.height = 32;
	if (win->clipcount > 8)
		win->clipcount = 8;

	win->chromakey = 0;
	win->global_alpha = 0;
L
Linus Torvalds 已提交
699 700 701 702 703
	field = win->field;
	maxw  = dev->crop_current.width;
	maxh  = dev->crop_current.height;

	if (V4L2_FIELD_ANY == field) {
704 705 706 707 708 709 710 711 712 713
		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;
	default:
714 715
		field = V4L2_FIELD_INTERLACED;
		break;
716
	}
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725

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

726
static int start_preview(struct saa7134_dev *dev)
L
Linus Torvalds 已提交
727 728 729 730
{
	unsigned long base,control,bpl;
	int err;

731
	err = verify_preview(dev, &dev->win, false);
L
Linus Torvalds 已提交
732 733 734
	if (0 != err)
		return err;

735
	dev->ovfield = dev->win.field;
736
	video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
737 738 739
		dev->win.w.width, dev->win.w.height,
		dev->win.w.left, dev->win.w.top,
		dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
L
Linus Torvalds 已提交
740 741

	/* setup window + clipping */
742
	set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
L
Linus Torvalds 已提交
743
		 V4L2_FIELD_HAS_BOTH(dev->ovfield));
744
	setup_clipping(dev, dev->clips, dev->nclips,
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753
		       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;
754 755
	base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
	base += dev->ovfmt->depth/8         * dev->win.w.left;
L
Linus Torvalds 已提交
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
	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;
}

781
static int stop_preview(struct saa7134_dev *dev)
L
Linus Torvalds 已提交
782 783 784 785 786 787
{
	dev->ovenable = 0;
	saa7134_set_dmabits(dev);
	return 0;
}

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
/*
 * Media Controller helper functions
 */

static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
{
#ifdef CONFIG_MEDIA_CONTROLLER
	struct media_device *mdev = dev->media_dev;
	struct media_entity *source;
	struct media_link *link, *found_link = NULL;
	int ret, active_links = 0;

	if (!mdev || !dev->decoder)
		return 0;

	/*
	 * This will find the tuner that is connected into the decoder.
	 * Technically, this is not 100% correct, as the device may be
	 * using an analog input instead of the tuner. However, as we can't
	 * do DVB streaming while the DMA engine is being used for V4L2,
	 * this should be enough for the actual needs.
	 */
	list_for_each_entry(link, &dev->decoder->links, list) {
		if (link->sink->entity == dev->decoder) {
			found_link = link;
			if (link->flags & MEDIA_LNK_FL_ENABLED)
				active_links++;
			break;
		}
	}

	if (active_links == 1 || !found_link)
		return 0;

	source = found_link->source->entity;
	list_for_each_entry(link, &source->links, list) {
		struct media_entity *sink;
		int flags = 0;

		sink = link->sink->entity;

		if (sink == dev->decoder)
			flags = MEDIA_LNK_FL_ENABLED;

		ret = media_entity_setup_link(link, flags);
		if (ret) {
			pr_err("Couldn't change link %s->%s to %s. Error %d\n",
			       source->name, sink->name,
			       flags ? "enabled" : "disabled",
			       ret);
			return ret;
		}
	}
#endif
	return 0;
}

L
Linus Torvalds 已提交
845 846 847 848 849 850
/* ------------------------------------------------------------------ */

static int buffer_activate(struct saa7134_dev *dev,
			   struct saa7134_buf *buf,
			   struct saa7134_buf *next)
{
851
	struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
L
Linus Torvalds 已提交
852 853 854
	unsigned long base,control,bpl;
	unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */

855
	video_dbg("buffer_activate buf=%p\n", buf);
L
Linus Torvalds 已提交
856 857
	buf->top_seen = 0;

H
Hans Verkuil 已提交
858 859
	set_size(dev, TASK_A, dev->width, dev->height,
		 V4L2_FIELD_HAS_BOTH(dev->field));
860
	if (dev->fmt->yuv)
L
Linus Torvalds 已提交
861 862 863
		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
	else
		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
864
	saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
L
Linus Torvalds 已提交
865 866 867

	/* DMA: setup channel 0 (= Video Task A0) */
	base  = saa7134_buffer_base(buf);
868
	if (dev->fmt->planar)
H
Hans Verkuil 已提交
869
		bpl = dev->width;
L
Linus Torvalds 已提交
870
	else
H
Hans Verkuil 已提交
871
		bpl = (dev->width * dev->fmt->depth) / 8;
L
Linus Torvalds 已提交
872 873
	control = SAA7134_RS_CONTROL_BURST_16 |
		SAA7134_RS_CONTROL_ME |
H
Hans Verkuil 已提交
874
		(dmaq->pt.dma >> 12);
875
	if (dev->fmt->bswap)
L
Linus Torvalds 已提交
876
		control |= SAA7134_RS_CONTROL_BSWAP;
877
	if (dev->fmt->wswap)
L
Linus Torvalds 已提交
878
		control |= SAA7134_RS_CONTROL_WSWAP;
H
Hans Verkuil 已提交
879
	if (V4L2_FIELD_HAS_BOTH(dev->field)) {
L
Linus Torvalds 已提交
880 881 882 883 884 885 886 887 888 889 890 891
		/* 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);

892
	if (dev->fmt->planar) {
L
Linus Torvalds 已提交
893
		/* DMA: setup channel 4+5 (= planar task A) */
894
		bpl_uv   = bpl >> dev->fmt->hshift;
H
Hans Verkuil 已提交
895 896
		lines_uv = dev->height >> dev->fmt->vshift;
		base2    = base + bpl * dev->height;
L
Linus Torvalds 已提交
897
		base3    = base2 + bpl_uv * lines_uv;
898
		if (dev->fmt->uvswap)
L
Linus Torvalds 已提交
899
			tmp = base2, base2 = base3, base3 = tmp;
900
		video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
L
Linus Torvalds 已提交
901
			bpl_uv,lines_uv,base2,base3);
H
Hans Verkuil 已提交
902
		if (V4L2_FIELD_HAS_BOTH(dev->field)) {
L
Linus Torvalds 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
			/* 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);
H
Hans Verkuil 已提交
925
	mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
L
Linus Torvalds 已提交
926 927 928
	return 0;
}

H
Hans Verkuil 已提交
929
static int buffer_init(struct vb2_buffer *vb2)
L
Linus Torvalds 已提交
930
{
H
Hans Verkuil 已提交
931
	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
932 933
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
H
Hans Verkuil 已提交
934 935 936 937 938 939 940 941 942

	dmaq->curr = NULL;
	buf->activate = buffer_activate;
	return 0;
}

static int buffer_prepare(struct vb2_buffer *vb2)
{
	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
943
	struct saa7134_dev *dev = dmaq->dev;
944 945 946
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
	struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
L
Linus Torvalds 已提交
947 948
	unsigned int size;

949 950 951 952
	if (dma->sgl->offset) {
		pr_err("The buffer is not page-aligned\n");
		return -EINVAL;
	}
H
Hans Verkuil 已提交
953 954
	size = (dev->width * dev->height * dev->fmt->depth) >> 3;
	if (vb2_plane_size(vb2, 0) < size)
L
Linus Torvalds 已提交
955
		return -EINVAL;
H
Hans Verkuil 已提交
956 957

	vb2_set_plane_payload(vb2, 0, size);
958
	vbuf->field = dev->field;
H
Hans Verkuil 已提交
959 960 961 962 963

	return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
				    saa7134_buffer_startpage(buf));
}

964
static int queue_setup(struct vb2_queue *q,
H
Hans Verkuil 已提交
965
			   unsigned int *nbuffers, unsigned int *nplanes,
966
			   unsigned int sizes[], struct device *alloc_devs[])
H
Hans Verkuil 已提交
967 968 969 970 971
{
	struct saa7134_dmaqueue *dmaq = q->drv_priv;
	struct saa7134_dev *dev = dmaq->dev;
	int size = dev->fmt->depth * dev->width * dev->height >> 3;

972 973 974 975 976 977
	if (dev->width    < 48 ||
	    dev->height   < 32 ||
	    dev->width/4  > dev->crop_current.width  ||
	    dev->height/4 > dev->crop_current.height ||
	    dev->width    > dev->crop_bounds.width  ||
	    dev->height   > dev->crop_bounds.height)
L
Linus Torvalds 已提交
978 979
		return -EINVAL;

H
Hans Verkuil 已提交
980 981 982
	*nbuffers = saa7134_buffer_count(size, *nbuffers);
	*nplanes = 1;
	sizes[0] = size;
983 984 985

	saa7134_enable_analog_tuner(dev);

L
Linus Torvalds 已提交
986 987 988
	return 0;
}

H
Hans Verkuil 已提交
989 990 991 992
/*
 * move buffer to hardware queue
 */
void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
L
Linus Torvalds 已提交
993
{
H
Hans Verkuil 已提交
994
	struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
995
	struct saa7134_dev *dev = dmaq->dev;
996 997
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
L
Linus Torvalds 已提交
998

H
Hans Verkuil 已提交
999
	saa7134_buffer_queue(dev, dmaq, buf);
L
Linus Torvalds 已提交
1000
}
H
Hans Verkuil 已提交
1001
EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
L
Linus Torvalds 已提交
1002

H
Hans Verkuil 已提交
1003
int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
L
Linus Torvalds 已提交
1004
{
H
Hans Verkuil 已提交
1005
	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1006
	struct saa7134_dev *dev = dmaq->dev;
L
Linus Torvalds 已提交
1007

H
Hans Verkuil 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	/*
	 * Planar video capture and TS share the same DMA channel,
	 * so only one can be active at a time.
	 */
	if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
	    dmaq == &dev->video_q && dev->fmt->planar) {
		struct saa7134_buf *buf, *tmp;

		list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
			list_del(&buf->entry);
1018 1019
			vb2_buffer_done(&buf->vb2.vb2_buf,
					VB2_BUF_STATE_QUEUED);
H
Hans Verkuil 已提交
1020 1021
		}
		if (dmaq->curr) {
1022 1023
			vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
					VB2_BUF_STATE_QUEUED);
H
Hans Verkuil 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
			dmaq->curr = NULL;
		}
		return -EBUSY;
	}

	/* The SAA7134 has a 1K FIFO; the datasheet suggests that when
	 * configured conservatively, there's 22 usec of buffering for video.
	 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
	 * margin in case the FIFO is configured differently to the datasheet.
	 * Unfortunately, I lack register-level documentation to check the
	 * Linux FIFO setup and confirm the perfect value.
	 */
	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
		pm_qos_add_request(&dev->qos_request,
			PM_QOS_CPU_DMA_LATENCY, 20);
	dmaq->seq_nr = 0;

	return 0;
L
Linus Torvalds 已提交
1043 1044
}

H
Hans Verkuil 已提交
1045
void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
L
Linus Torvalds 已提交
1046
{
H
Hans Verkuil 已提交
1047 1048 1049 1050
	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
	struct saa7134_dev *dev = dmaq->dev;

	saa7134_stop_streaming(dev, dmaq);
L
Linus Torvalds 已提交
1051

H
Hans Verkuil 已提交
1052 1053 1054
	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
		pm_qos_remove_request(&dev->qos_request);
L
Linus Torvalds 已提交
1055 1056
}

1057
static const struct vb2_ops vb2_qops = {
H
Hans Verkuil 已提交
1058 1059 1060 1061 1062 1063 1064 1065
	.queue_setup	= queue_setup,
	.buf_init	= buffer_init,
	.buf_prepare	= buffer_prepare,
	.buf_queue	= saa7134_vb2_buffer_queue,
	.wait_prepare	= vb2_ops_wait_prepare,
	.wait_finish	= vb2_ops_wait_finish,
	.start_streaming = saa7134_vb2_start_streaming,
	.stop_streaming = saa7134_vb2_stop_streaming,
L
Linus Torvalds 已提交
1066 1067 1068 1069
};

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

1070
static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1071
{
1072
	struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
L
Linus Torvalds 已提交
1073 1074 1075
	unsigned long flags;
	int restart_overlay = 0;

1076
	switch (ctrl->id) {
L
Linus Torvalds 已提交
1077
	case V4L2_CID_BRIGHTNESS:
1078 1079
		dev->ctl_bright = ctrl->val;
		saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
L
Linus Torvalds 已提交
1080 1081
		break;
	case V4L2_CID_HUE:
1082 1083
		dev->ctl_hue = ctrl->val;
		saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
L
Linus Torvalds 已提交
1084 1085
		break;
	case V4L2_CID_CONTRAST:
1086
		dev->ctl_contrast = ctrl->val;
L
Linus Torvalds 已提交
1087 1088 1089 1090
		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
		break;
	case V4L2_CID_SATURATION:
1091
		dev->ctl_saturation = ctrl->val;
L
Linus Torvalds 已提交
1092 1093 1094 1095
		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
		break;
	case V4L2_CID_AUDIO_MUTE:
1096
		dev->ctl_mute = ctrl->val;
L
Linus Torvalds 已提交
1097 1098 1099
		saa7134_tvaudio_setmute(dev);
		break;
	case V4L2_CID_AUDIO_VOLUME:
1100
		dev->ctl_volume = ctrl->val;
L
Linus Torvalds 已提交
1101 1102 1103
		saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
		break;
	case V4L2_CID_PRIVATE_INVERT:
1104
		dev->ctl_invert = ctrl->val;
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109
		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;
1110
	case V4L2_CID_HFLIP:
1111
		dev->ctl_mirror = ctrl->val;
L
Linus Torvalds 已提交
1112 1113 1114
		restart_overlay = 1;
		break;
	case V4L2_CID_PRIVATE_Y_EVEN:
1115
		dev->ctl_y_even = ctrl->val;
L
Linus Torvalds 已提交
1116 1117 1118
		restart_overlay = 1;
		break;
	case V4L2_CID_PRIVATE_Y_ODD:
1119
		dev->ctl_y_odd = ctrl->val;
L
Linus Torvalds 已提交
1120 1121 1122
		restart_overlay = 1;
		break;
	case V4L2_CID_PRIVATE_AUTOMUTE:
1123 1124 1125 1126 1127 1128
	{
		struct v4l2_priv_tun_config tda9887_cfg;

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

1129
		dev->ctl_automute = ctrl->val;
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134
		if (dev->tda9887_conf) {
			if (dev->ctl_automute)
				dev->tda9887_conf |= TDA9887_AUTOMUTE;
			else
				dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1135

1136
			saa_call_all(dev, tuner, s_config, &tda9887_cfg);
L
Linus Torvalds 已提交
1137 1138
		}
		break;
1139
	}
L
Linus Torvalds 已提交
1140
	default:
1141
		return -EINVAL;
L
Linus Torvalds 已提交
1142
	}
H
Hans Verkuil 已提交
1143
	if (restart_overlay && dev->overlay_owner) {
1144 1145 1146 1147
		spin_lock_irqsave(&dev->slock, flags);
		stop_preview(dev);
		start_preview(dev);
		spin_unlock_irqrestore(&dev->slock, flags);
L
Linus Torvalds 已提交
1148
	}
1149
	return 0;
1150
}
L
Linus Torvalds 已提交
1151 1152 1153

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

1154
static int video_open(struct file *file)
L
Linus Torvalds 已提交
1155
{
1156 1157
	struct video_device *vdev = video_devdata(file);
	struct saa7134_dev *dev = video_drvdata(file);
H
Hans Verkuil 已提交
1158
	int ret = v4l2_fh_open(file);
L
Linus Torvalds 已提交
1159

H
Hans Verkuil 已提交
1160 1161
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
1162

H
Hans Verkuil 已提交
1163
	mutex_lock(&dev->lock);
1164
	if (vdev->vfl_type == VFL_TYPE_RADIO) {
L
Linus Torvalds 已提交
1165
		/* switch to radio mode */
1166
		saa7134_tvaudio_setinput(dev, &card(dev).radio);
1167
		saa_call_all(dev, tuner, s_radio);
L
Linus Torvalds 已提交
1168 1169
	} else {
		/* switch to video/vbi mode */
1170
		video_mux(dev, dev->ctl_input);
L
Linus Torvalds 已提交
1171
	}
H
Hans Verkuil 已提交
1172
	mutex_unlock(&dev->lock);
1173

1174
	return 0;
L
Linus Torvalds 已提交
1175 1176
}

1177
static int video_release(struct file *file)
L
Linus Torvalds 已提交
1178
{
1179
	struct video_device *vdev = video_devdata(file);
1180
	struct saa7134_dev *dev = video_drvdata(file);
H
Hans Verkuil 已提交
1181
	struct v4l2_fh *fh = file->private_data;
1182
	struct saa6588_command cmd;
L
Linus Torvalds 已提交
1183 1184
	unsigned long flags;

H
Hans Verkuil 已提交
1185
	mutex_lock(&dev->lock);
1186 1187
	saa7134_tvaudio_close(dev);

L
Linus Torvalds 已提交
1188
	/* turn off overlay */
H
Hans Verkuil 已提交
1189
	if (fh == dev->overlay_owner) {
L
Linus Torvalds 已提交
1190
		spin_lock_irqsave(&dev->slock,flags);
1191
		stop_preview(dev);
L
Linus Torvalds 已提交
1192
		spin_unlock_irqrestore(&dev->slock,flags);
H
Hans Verkuil 已提交
1193
		dev->overlay_owner = NULL;
L
Linus Torvalds 已提交
1194 1195
	}

H
Hans Verkuil 已提交
1196 1197 1198 1199
	if (vdev->vfl_type == VFL_TYPE_RADIO)
		v4l2_fh_release(file);
	else
		_vb2_fop_release(file, NULL);
L
Linus Torvalds 已提交
1200

1201 1202 1203 1204 1205 1206
	/* 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);

1207
	saa_call_all(dev, core, s_power, 0);
1208
	if (vdev->vfl_type == VFL_TYPE_RADIO)
1209
		saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
H
Hans Verkuil 已提交
1210
	mutex_unlock(&dev->lock);
1211

L
Linus Torvalds 已提交
1212 1213 1214
	return 0;
}

1215 1216 1217
static ssize_t radio_read(struct file *file, char __user *data,
			 size_t count, loff_t *ppos)
{
1218
	struct saa7134_dev *dev = video_drvdata(file);
1219
	struct saa6588_command cmd;
1220 1221

	cmd.block_count = count/3;
1222
	cmd.nonblocking = file->f_flags & O_NONBLOCK;
1223 1224 1225 1226
	cmd.buffer = data;
	cmd.instance = file;
	cmd.result = -ENODEV;

H
Hans Verkuil 已提交
1227
	mutex_lock(&dev->lock);
1228
	saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
H
Hans Verkuil 已提交
1229
	mutex_unlock(&dev->lock);
1230 1231 1232 1233 1234 1235

	return cmd.result;
}

static unsigned int radio_poll(struct file *file, poll_table *wait)
{
1236
	struct saa7134_dev *dev = video_drvdata(file);
1237
	struct saa6588_command cmd;
1238
	unsigned int rc = v4l2_ctrl_poll(file, wait);
1239 1240 1241

	cmd.instance = file;
	cmd.event_list = wait;
1242
	cmd.result = 0;
H
Hans Verkuil 已提交
1243
	mutex_lock(&dev->lock);
1244
	saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
H
Hans Verkuil 已提交
1245
	mutex_unlock(&dev->lock);
1246

1247
	return rc | cmd.result;
1248 1249
}

L
Linus Torvalds 已提交
1250 1251
/* ------------------------------------------------------------------ */

1252
static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1253
						struct v4l2_format *f)
L
Linus Torvalds 已提交
1254
{
1255
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1256 1257
	struct saa7134_tvnorm *norm = dev->tvnorm;

1258
	memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
L
Linus Torvalds 已提交
1259 1260 1261 1262
	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;
1263 1264 1265
	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 已提交
1266 1267 1268
	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
	f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */

1269
	return 0;
L
Linus Torvalds 已提交
1270 1271
}

1272
static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1273
				struct v4l2_format *f)
L
Linus Torvalds 已提交
1274
{
1275
	struct saa7134_dev *dev = video_drvdata(file);
1276

1277 1278
	f->fmt.pix.width        = dev->width;
	f->fmt.pix.height       = dev->height;
H
Hans Verkuil 已提交
1279
	f->fmt.pix.field        = dev->field;
1280
	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
1281 1282 1283 1284 1285
	if (dev->fmt->planar)
		f->fmt.pix.bytesperline = f->fmt.pix.width;
	else
		f->fmt.pix.bytesperline =
			(f->fmt.pix.width * dev->fmt->depth) / 8;
1286
	f->fmt.pix.sizeimage =
1287
		(f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
1288
	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1289 1290 1291
	return 0;
}

1292
static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1293 1294
				struct v4l2_format *f)
{
1295
	struct saa7134_dev *dev = video_drvdata(file);
1296
	struct v4l2_clip __user *clips = f->fmt.win.clips;
1297 1298 1299
	u32 clipcount = f->fmt.win.clipcount;
	int err = 0;
	int i;
1300 1301

	if (saa7134_no_overlay > 0) {
1302
		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
L
Linus Torvalds 已提交
1303 1304
		return -EINVAL;
	}
1305
	f->fmt.win = dev->win;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
	f->fmt.win.clips = clips;
	if (clips == NULL)
		clipcount = 0;
	if (dev->nclips < clipcount)
		clipcount = dev->nclips;
	f->fmt.win.clipcount = clipcount;

	for (i = 0; !err && i < clipcount; i++) {
		if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
					sizeof(struct v4l2_rect)))
			err = -EFAULT;
	}
1318

1319
	return err;
L
Linus Torvalds 已提交
1320 1321
}

1322
static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1323
						struct v4l2_format *f)
L
Linus Torvalds 已提交
1324
{
1325
	struct saa7134_dev *dev = video_drvdata(file);
1326 1327 1328
	struct saa7134_format *fmt;
	enum v4l2_field field;
	unsigned int maxw, maxh;
L
Linus Torvalds 已提交
1329

1330 1331 1332
	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	if (NULL == fmt)
		return -EINVAL;
L
Linus Torvalds 已提交
1333

1334 1335 1336
	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 已提交
1337

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	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;
	default:
1349 1350
		field = V4L2_FIELD_INTERLACED;
		break;
1351
	}
L
Linus Torvalds 已提交
1352

1353
	f->fmt.pix.field = field;
1354 1355 1356 1357 1358 1359 1360 1361 1362
	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;
1363 1364 1365 1366 1367
	if (fmt->planar)
		f->fmt.pix.bytesperline = f->fmt.pix.width;
	else
		f->fmt.pix.bytesperline =
			(f->fmt.pix.width * fmt->depth) / 8;
1368
	f->fmt.pix.sizeimage =
1369
		(f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
1370
	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
L
Linus Torvalds 已提交
1371

1372 1373
	return 0;
}
L
Linus Torvalds 已提交
1374

1375
static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1376 1377
						struct v4l2_format *f)
{
1378
	struct saa7134_dev *dev = video_drvdata(file);
1379 1380

	if (saa7134_no_overlay > 0) {
1381
		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
L
Linus Torvalds 已提交
1382 1383
		return -EINVAL;
	}
1384

1385 1386 1387
	if (f->fmt.win.clips == NULL)
		f->fmt.win.clipcount = 0;
	return verify_preview(dev, &f->fmt.win, true);
L
Linus Torvalds 已提交
1388 1389
}

1390
static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1391
					struct v4l2_format *f)
1392
{
1393
	struct saa7134_dev *dev = video_drvdata(file);
1394 1395
	int err;

1396
	err = saa7134_try_fmt_vid_cap(file, priv, f);
1397 1398 1399
	if (0 != err)
		return err;

1400 1401 1402
	dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	dev->width = f->fmt.pix.width;
	dev->height = f->fmt.pix.height;
H
Hans Verkuil 已提交
1403
	dev->field = f->fmt.pix.field;
1404 1405 1406
	return 0;
}

1407
static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1408
					struct v4l2_format *f)
L
Linus Torvalds 已提交
1409
{
1410
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1411
	int err;
S
Steven Rostedt 已提交
1412
	unsigned long flags;
L
Linus Torvalds 已提交
1413

1414
	if (saa7134_no_overlay > 0) {
1415
		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1416 1417
		return -EINVAL;
	}
1418 1419 1420
	if (f->fmt.win.clips == NULL)
		f->fmt.win.clipcount = 0;
	err = verify_preview(dev, &f->fmt.win, true);
1421 1422
	if (0 != err)
		return err;
L
Linus Torvalds 已提交
1423

1424 1425
	dev->win    = f->fmt.win;
	dev->nclips = f->fmt.win.clipcount;
1426

1427
	if (copy_from_user(dev->clips, f->fmt.win.clips,
H
Hans Verkuil 已提交
1428
			   sizeof(struct v4l2_clip) * dev->nclips))
1429 1430
		return -EFAULT;

H
Hans Verkuil 已提交
1431
	if (priv == dev->overlay_owner) {
1432
		spin_lock_irqsave(&dev->slock, flags);
1433 1434
		stop_preview(dev);
		start_preview(dev);
1435 1436 1437 1438
		spin_unlock_irqrestore(&dev->slock, flags);
	}

	return 0;
L
Linus Torvalds 已提交
1439 1440
}

1441
int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
L
Linus Torvalds 已提交
1442
{
1443
	struct saa7134_dev *dev = video_drvdata(file);
1444
	unsigned int n;
L
Linus Torvalds 已提交
1445

1446 1447 1448
	n = i->index;
	if (n >= SAA7134_INPUT_MAX)
		return -EINVAL;
1449
	if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
1450 1451
		return -EINVAL;
	i->index = n;
1452
	strcpy(i->name, saa7134_input_name[card_in(dev, n).type]);
1453 1454 1455
	switch (card_in(dev, n).type) {
	case SAA7134_INPUT_TV:
	case SAA7134_INPUT_TV_MONO:
1456
		i->type = V4L2_INPUT_TYPE_TUNER;
1457 1458 1459 1460 1461
		break;
	default:
		i->type  = V4L2_INPUT_TYPE_CAMERA;
		break;
	}
1462 1463 1464 1465 1466 1467 1468
	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))
1469
			i->status |= V4L2_IN_ST_NO_SIGNAL;
1470 1471 1472
		if (0 != (v2 & 0x0e))
			i->status |= V4L2_IN_ST_MACROVISION;
	}
1473
	i->std = SAA7134_NORMS;
1474 1475
	return 0;
}
1476
EXPORT_SYMBOL_GPL(saa7134_enum_input);
L
Linus Torvalds 已提交
1477

1478
int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1479
{
1480
	struct saa7134_dev *dev = video_drvdata(file);
1481 1482 1483 1484

	*i = dev->ctl_input;
	return 0;
}
1485
EXPORT_SYMBOL_GPL(saa7134_g_input);
1486

1487
int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1488
{
1489
	struct saa7134_dev *dev = video_drvdata(file);
1490

1491
	if (i >= SAA7134_INPUT_MAX)
1492
		return -EINVAL;
1493
	if (card_in(dev, i).type == SAA7134_NO_INPUT)
1494 1495 1496 1497
		return -EINVAL;
	video_mux(dev, i);
	return 0;
}
1498
EXPORT_SYMBOL_GPL(saa7134_s_input);
1499

1500
int saa7134_querycap(struct file *file, void *priv,
1501 1502
					struct v4l2_capability *cap)
{
1503
	struct saa7134_dev *dev = video_drvdata(file);
1504 1505
	struct video_device *vdev = video_devdata(file);
	u32 radio_caps, video_caps, vbi_caps;
L
Linus Torvalds 已提交
1506

1507 1508 1509 1510 1511 1512
	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));
1513 1514 1515 1516 1517 1518

	cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
	if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
		cap->device_caps |= V4L2_CAP_TUNER;

	radio_caps = V4L2_CAP_RADIO;
1519
	if (dev->has_rds)
1520 1521 1522
		radio_caps |= V4L2_CAP_RDS_CAPTURE;

	video_caps = V4L2_CAP_VIDEO_CAPTURE;
H
Hans Verkuil 已提交
1523
	if (saa7134_no_overlay <= 0 && !is_empress(file))
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
		video_caps |= V4L2_CAP_VIDEO_OVERLAY;

	vbi_caps = V4L2_CAP_VBI_CAPTURE;

	switch (vdev->vfl_type) {
	case VFL_TYPE_RADIO:
		cap->device_caps |= radio_caps;
		break;
	case VFL_TYPE_GRABBER:
		cap->device_caps |= video_caps;
		break;
	case VFL_TYPE_VBI:
		cap->device_caps |= vbi_caps;
		break;
	}
	cap->capabilities = radio_caps | video_caps | vbi_caps |
		cap->device_caps | V4L2_CAP_DEVICE_CAPS;
	if (vdev->vfl_type == VFL_TYPE_RADIO) {
		cap->device_caps &= ~V4L2_CAP_STREAMING;
		if (!dev->has_rds)
			cap->device_caps &= ~V4L2_CAP_READWRITE;
	}
1546

1547
	return 0;
1548
}
1549
EXPORT_SYMBOL_GPL(saa7134_querycap);
1550

1551
int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1552
{
1553
	struct saa7134_dev *dev = video_drvdata(file);
H
Hans Verkuil 已提交
1554
	struct v4l2_fh *fh = priv;
1555 1556 1557
	unsigned long flags;
	unsigned int i;
	v4l2_std_id fixup;
1558

H
Hans Verkuil 已提交
1559
	if (is_empress(file) && dev->overlay_owner) {
1560 1561 1562 1563
		/* Don't change the std from the mpeg device
		   if overlay is active. */
		return -EBUSY;
	}
1564

1565
	for (i = 0; i < TVNORMS; i++)
1566
		if (id == tvnorms[i].id)
1567
			break;
1568

1569 1570
	if (i == TVNORMS)
		for (i = 0; i < TVNORMS; i++)
1571
			if (id & tvnorms[i].id)
1572 1573 1574
				break;
	if (i == TVNORMS)
		return -EINVAL;
1575

1576
	if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
		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;
		}
1588
		for (i = 0; i < TVNORMS; i++) {
1589 1590
			if (fixup == tvnorms[i].id)
				break;
1591 1592 1593
		}
		if (i == TVNORMS)
			return -EINVAL;
L
Linus Torvalds 已提交
1594
	}
1595

1596
	id = tvnorms[i].id;
1597

H
Hans Verkuil 已提交
1598
	if (!is_empress(file) && fh == dev->overlay_owner) {
1599
		spin_lock_irqsave(&dev->slock, flags);
1600
		stop_preview(dev);
1601 1602 1603 1604 1605
		spin_unlock_irqrestore(&dev->slock, flags);

		set_tvnorm(dev, &tvnorms[i]);

		spin_lock_irqsave(&dev->slock, flags);
1606
		start_preview(dev);
1607 1608 1609
		spin_unlock_irqrestore(&dev->slock, flags);
	} else
		set_tvnorm(dev, &tvnorms[i]);
1610

1611 1612 1613
	saa7134_tvaudio_do_scan(dev);
	return 0;
}
1614
EXPORT_SYMBOL_GPL(saa7134_s_std);
1615

1616
int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1617
{
1618
	struct saa7134_dev *dev = video_drvdata(file);
1619 1620 1621 1622

	*id = dev->tvnorm->id;
	return 0;
}
1623
EXPORT_SYMBOL_GPL(saa7134_g_std);
L
Linus Torvalds 已提交
1624

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
{
	static v4l2_std_id stds[] = {
		V4L2_STD_UNKNOWN,
		V4L2_STD_NTSC,
		V4L2_STD_PAL,
		V4L2_STD_SECAM };

	v4l2_std_id result = 0;

	u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
	u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);

	if (!(st2 & 0x1)) /* RDCAP == 0 */
		result = V4L2_STD_UNKNOWN;
	else
		result = stds[st1 & 0x03];

	return result;
}

int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
{
	struct saa7134_dev *dev = video_drvdata(file);
	*std &= saa7134_read_std(dev);
	return 0;
}
EXPORT_SYMBOL_GPL(saa7134_querystd);

1654
static int saa7134_cropcap(struct file *file, void *priv,
1655 1656
					struct v4l2_cropcap *cap)
{
1657
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1658

1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
	if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
		return -EINVAL;
	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 已提交
1671
	}
1672 1673
	return 0;
}
L
Linus Torvalds 已提交
1674

1675
static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
1676
{
1677
	struct saa7134_dev *dev = video_drvdata(file);
1678

1679 1680
	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1681
		return -EINVAL;
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695

	switch (sel->target) {
	case V4L2_SEL_TGT_CROP:
		sel->r = dev->crop_current;
		break;
	case V4L2_SEL_TGT_CROP_DEFAULT:
		sel->r = dev->crop_defrect;
		break;
	case V4L2_SEL_TGT_CROP_BOUNDS:
		sel->r  = dev->crop_bounds;
		break;
	default:
		return -EINVAL;
	}
1696 1697 1698
	return 0;
}

1699
static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
1700
{
1701
	struct saa7134_dev *dev = video_drvdata(file);
1702
	struct v4l2_rect *b = &dev->crop_bounds;
1703
	struct v4l2_rect *c = &dev->crop_current;
1704

1705 1706 1707 1708 1709
	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
		return -EINVAL;

	if (sel->target != V4L2_SEL_TGT_CROP)
1710 1711
		return -EINVAL;

H
Hans Verkuil 已提交
1712
	if (dev->overlay_owner)
1713
		return -EBUSY;
H
Hans Verkuil 已提交
1714
	if (vb2_is_streaming(&dev->video_vbq))
1715 1716
		return -EBUSY;

1717
	*c = sel->r;
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
	if (c->top < b->top)
		c->top = b->top;
	if (c->top > b->top + b->height)
		c->top = b->top + b->height;
	if (c->height > b->top - c->top + b->height)
		c->height = b->top - c->top + b->height;

	if (c->left < b->left)
		c->left = b->left;
	if (c->left > b->left + b->width)
		c->left = b->left + b->width;
	if (c->width > b->left - c->left + b->width)
		c->width = b->left - c->left + b->width;
1731
	sel->r = *c;
1732 1733 1734
	return 0;
}

1735
int saa7134_g_tuner(struct file *file, void *priv,
1736 1737
					struct v4l2_tuner *t)
{
1738
	struct saa7134_dev *dev = video_drvdata(file);
1739
	int n;
L
Linus Torvalds 已提交
1740

1741 1742 1743
	if (0 != t->index)
		return -EINVAL;
	memset(t, 0, sizeof(*t));
1744
	for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1745 1746
		if (card_in(dev, n).type == SAA7134_INPUT_TV ||
		    card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
1747
			break;
1748 1749 1750
	}
	if (n == SAA7134_INPUT_MAX)
		return -EINVAL;
1751
	if (card_in(dev, n).type != SAA7134_NO_INPUT) {
1752 1753
		strcpy(t->name, "Television");
		t->type = V4L2_TUNER_ANALOG_TV;
1754
		saa_call_all(dev, tuner, g_tuner, t);
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
		t->capability = V4L2_TUNER_CAP_NORM |
			V4L2_TUNER_CAP_STEREO |
			V4L2_TUNER_CAP_LANG1 |
			V4L2_TUNER_CAP_LANG2;
		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;
}
1766
EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1767

1768
int saa7134_s_tuner(struct file *file, void *priv,
1769
					const struct v4l2_tuner *t)
1770
{
1771
	struct saa7134_dev *dev = video_drvdata(file);
1772
	int rx, mode;
1773

1774 1775 1776
	if (0 != t->index)
		return -EINVAL;

1777 1778 1779
	mode = dev->thread.mode;
	if (UNSET == mode) {
		rx   = saa7134_tvaudio_getstereo(dev);
1780
		mode = saa7134_tvaudio_rx2mode(rx);
L
Linus Torvalds 已提交
1781
	}
1782 1783
	if (mode != t->audmode)
		dev->thread.mode = t->audmode;
1784

1785 1786
	return 0;
}
1787
EXPORT_SYMBOL_GPL(saa7134_s_tuner);
L
Linus Torvalds 已提交
1788

1789
int saa7134_g_frequency(struct file *file, void *priv,
1790 1791
					struct v4l2_frequency *f)
{
1792
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1793

1794 1795 1796 1797
	if (0 != f->tuner)
		return -EINVAL;

	saa_call_all(dev, tuner, g_frequency, f);
1798

1799 1800
	return 0;
}
1801
EXPORT_SYMBOL_GPL(saa7134_g_frequency);
L
Linus Torvalds 已提交
1802

1803
int saa7134_s_frequency(struct file *file, void *priv,
1804
					const struct v4l2_frequency *f)
1805
{
1806
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1807

1808 1809
	if (0 != f->tuner)
		return -EINVAL;
L
Linus Torvalds 已提交
1810

1811
	saa_call_all(dev, tuner, s_frequency, f);
L
Linus Torvalds 已提交
1812

1813 1814 1815
	saa7134_tvaudio_do_scan(dev);
	return 0;
}
1816
EXPORT_SYMBOL_GPL(saa7134_s_frequency);
L
Linus Torvalds 已提交
1817

1818
static int saa7134_enum_fmt_vid_cap(struct file *file, void  *priv,
1819
					struct v4l2_fmtdesc *f)
1820
{
1821 1822
	if (f->index >= FORMATS)
		return -EINVAL;
L
Linus Torvalds 已提交
1823

1824 1825
	strlcpy(f->description, formats[f->index].name,
		sizeof(f->description));
L
Linus Torvalds 已提交
1826

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

1829 1830 1831
	return 0;
}

1832
static int saa7134_enum_fmt_vid_overlay(struct file *file, void  *priv,
1833 1834 1835
					struct v4l2_fmtdesc *f)
{
	if (saa7134_no_overlay > 0) {
1836
		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1837 1838
		return -EINVAL;
	}
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850

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

1851
static int saa7134_g_fbuf(struct file *file, void *f,
1852 1853
				struct v4l2_framebuffer *fb)
{
1854
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1855

1856 1857
	*fb = dev->ovbuf;
	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1858

1859 1860
	return 0;
}
L
Linus Torvalds 已提交
1861

1862
static int saa7134_s_fbuf(struct file *file, void *f,
1863
					const struct v4l2_framebuffer *fb)
1864
{
1865
	struct saa7134_dev *dev = video_drvdata(file);
1866 1867 1868 1869 1870
	struct saa7134_format *fmt;

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

1872 1873 1874 1875
	/* check args */
	fmt = format_by_fourcc(fb->fmt.pixelformat);
	if (NULL == fmt)
		return -EINVAL;
L
Linus Torvalds 已提交
1876

1877 1878 1879 1880 1881 1882 1883 1884
	/* 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 已提交
1885

1886
static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1887
{
1888
	struct saa7134_dev *dev = video_drvdata(file);
1889 1890 1891 1892
	unsigned long flags;

	if (on) {
		if (saa7134_no_overlay > 0) {
1893
			video_dbg("no_overlay\n");
1894 1895
			return -EINVAL;
		}
L
Linus Torvalds 已提交
1896

H
Hans Verkuil 已提交
1897
		if (dev->overlay_owner && priv != dev->overlay_owner)
L
Linus Torvalds 已提交
1898
			return -EBUSY;
H
Hans Verkuil 已提交
1899
		dev->overlay_owner = priv;
1900
		spin_lock_irqsave(&dev->slock, flags);
1901
		start_preview(dev);
1902
		spin_unlock_irqrestore(&dev->slock, flags);
L
Linus Torvalds 已提交
1903
	}
1904
	if (!on) {
H
Hans Verkuil 已提交
1905
		if (priv != dev->overlay_owner)
1906 1907
			return -EINVAL;
		spin_lock_irqsave(&dev->slock, flags);
1908
		stop_preview(dev);
1909
		spin_unlock_irqrestore(&dev->slock, flags);
H
Hans Verkuil 已提交
1910
		dev->overlay_owner = NULL;
L
Linus Torvalds 已提交
1911
	}
1912 1913 1914
	return 0;
}

1915 1916
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int vidioc_g_register (struct file *file, void *priv,
1917
			      struct v4l2_dbg_register *reg)
1918
{
1919
	struct saa7134_dev *dev = video_drvdata(file);
1920

1921
	reg->val = saa_readb(reg->reg & 0xffffff);
1922
	reg->size = 1;
1923 1924 1925 1926
	return 0;
}

static int vidioc_s_register (struct file *file, void *priv,
1927
				const struct v4l2_dbg_register *reg)
1928
{
1929
	struct saa7134_dev *dev = video_drvdata(file);
1930

1931
	saa_writeb(reg->reg & 0xffffff, reg->val);
1932 1933 1934 1935
	return 0;
}
#endif

1936 1937 1938
static int radio_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *t)
{
1939
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
1940

1941 1942
	if (0 != t->index)
		return -EINVAL;
L
Linus Torvalds 已提交
1943

1944 1945
	strcpy(t->name, "Radio");

1946
	saa_call_all(dev, tuner, g_tuner, t);
1947
	t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1948 1949 1950 1951
	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;
1952
	}
1953 1954 1955
	return 0;
}
static int radio_s_tuner(struct file *file, void *priv,
1956
					const struct v4l2_tuner *t)
1957
{
1958
	struct saa7134_dev *dev = video_drvdata(file);
1959

1960 1961 1962
	if (0 != t->index)
		return -EINVAL;

1963
	saa_call_all(dev, tuner, s_tuner, t);
1964 1965
	return 0;
}
1966

1967
static const struct v4l2_file_operations video_fops =
L
Linus Torvalds 已提交
1968 1969 1970 1971
{
	.owner	  = THIS_MODULE,
	.open	  = video_open,
	.release  = video_release,
H
Hans Verkuil 已提交
1972 1973 1974 1975
	.read	  = vb2_fop_read,
	.poll     = vb2_fop_poll,
	.mmap	  = vb2_fop_mmap,
	.unlocked_ioctl	  = video_ioctl2,
L
Linus Torvalds 已提交
1976 1977
};

1978
static const struct v4l2_ioctl_ops video_ioctl_ops = {
1979
	.vidioc_querycap		= saa7134_querycap,
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
	.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,
1991
	.vidioc_cropcap			= saa7134_cropcap,
H
Hans Verkuil 已提交
1992 1993 1994 1995
	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
	.vidioc_querybuf		= vb2_ioctl_querybuf,
	.vidioc_qbuf			= vb2_ioctl_qbuf,
	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1996
	.vidioc_expbuf			= vb2_ioctl_expbuf,
1997
	.vidioc_s_std			= saa7134_s_std,
1998
	.vidioc_g_std			= saa7134_g_std,
1999
	.vidioc_querystd		= saa7134_querystd,
2000 2001 2002
	.vidioc_enum_input		= saa7134_enum_input,
	.vidioc_g_input			= saa7134_g_input,
	.vidioc_s_input			= saa7134_s_input,
H
Hans Verkuil 已提交
2003 2004
	.vidioc_streamon		= vb2_ioctl_streamon,
	.vidioc_streamoff		= vb2_ioctl_streamoff,
2005 2006
	.vidioc_g_tuner			= saa7134_g_tuner,
	.vidioc_s_tuner			= saa7134_s_tuner,
2007 2008
	.vidioc_g_selection		= saa7134_g_selection,
	.vidioc_s_selection		= saa7134_s_selection,
2009 2010 2011 2012 2013
	.vidioc_g_fbuf			= saa7134_g_fbuf,
	.vidioc_s_fbuf			= saa7134_s_fbuf,
	.vidioc_overlay			= saa7134_overlay,
	.vidioc_g_frequency		= saa7134_g_frequency,
	.vidioc_s_frequency		= saa7134_s_frequency,
2014 2015 2016 2017
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register              = vidioc_g_register,
	.vidioc_s_register              = vidioc_s_register,
#endif
2018 2019 2020
	.vidioc_log_status		= v4l2_ctrl_log_status,
	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
L
Linus Torvalds 已提交
2021 2022
};

2023
static const struct v4l2_file_operations radio_fops = {
2024 2025
	.owner	  = THIS_MODULE,
	.open	  = video_open,
2026
	.read     = radio_read,
2027
	.release  = video_release,
H
Hans Verkuil 已提交
2028
	.unlocked_ioctl	= video_ioctl2,
2029
	.poll     = radio_poll,
2030 2031 2032
};

static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2033
	.vidioc_querycap	= saa7134_querycap,
2034 2035
	.vidioc_g_tuner		= radio_g_tuner,
	.vidioc_s_tuner		= radio_s_tuner,
2036 2037
	.vidioc_g_frequency	= saa7134_g_frequency,
	.vidioc_s_frequency	= saa7134_s_frequency,
2038 2039
	.vidioc_subscribe_event	= v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
L
Linus Torvalds 已提交
2040 2041
};

2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
/* ----------------------------------------------------------- */
/* exported stuff                                              */

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

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

2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
	.s_ctrl = saa7134_s_ctrl,
};

static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
	.ops = &saa7134_ctrl_ops,
	.id = V4L2_CID_PRIVATE_INVERT,
	.name = "Invert",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
};

static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
	.ops = &saa7134_ctrl_ops,
	.id = V4L2_CID_PRIVATE_Y_ODD,
	.name = "Y Offset Odd Field",
	.type = V4L2_CTRL_TYPE_INTEGER,
	.min = 0,
	.max = 128,
	.step = 1,
};

static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
	.ops = &saa7134_ctrl_ops,
	.id = V4L2_CID_PRIVATE_Y_EVEN,
	.name = "Y Offset Even Field",
	.type = V4L2_CTRL_TYPE_INTEGER,
	.min = 0,
	.max = 128,
	.step = 1,
};

static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
	.ops = &saa7134_ctrl_ops,
	.id = V4L2_CID_PRIVATE_AUTOMUTE,
	.name = "Automute",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
	.def = 1,
};

L
Linus Torvalds 已提交
2103 2104
int saa7134_video_init1(struct saa7134_dev *dev)
{
2105
	struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
H
Hans Verkuil 已提交
2106 2107
	struct vb2_queue *q;
	int ret;
2108

L
Linus Torvalds 已提交
2109 2110 2111
	/* sanitycheck insmod options */
	if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
		gbuffers = 2;
2112
	if (gbufsize > gbufsize_max)
L
Linus Torvalds 已提交
2113 2114 2115
		gbufsize = gbufsize_max;
	gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
	v4l2_ctrl_handler_init(hdl, 11);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_CONTRAST, 0, 127, 1, 68);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_SATURATION, 0, 127, 1, 64);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_HUE, -128, 127, 1, 0);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_HFLIP, 0, 1, 1, 0);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
			V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
	if (hdl->error)
		return hdl->error;
	if (card_has_radio(dev)) {
		hdl = &dev->radio_ctrl_handler;
		v4l2_ctrl_handler_init(hdl, 2);
		v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
				v4l2_ctrl_radio_filter);
		if (hdl->error)
			return hdl->error;
	}
	dev->ctl_mute       = 1;

	if (dev->tda9887_conf && saa7134_ctrl_automute.def)
L
Linus Torvalds 已提交
2148 2149 2150
		dev->tda9887_conf |= TDA9887_AUTOMUTE;
	dev->automute       = 0;

2151
	INIT_LIST_HEAD(&dev->video_q.queue);
L
Linus Torvalds 已提交
2152 2153 2154 2155
	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;
2156 2157 2158
	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
	dev->width    = 720;
	dev->height   = 576;
H
Hans Verkuil 已提交
2159
	dev->field = V4L2_FIELD_INTERLACED;
2160 2161 2162 2163 2164 2165 2166
	dev->win.w.width = dev->width;
	dev->win.w.height = dev->height;
	dev->win.field = V4L2_FIELD_INTERLACED;
	dev->ovbuf.fmt.width = dev->width;
	dev->ovbuf.fmt.height = dev->height;
	dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
	dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
L
Linus Torvalds 已提交
2167

2168 2169 2170
	if (saa7134_boards[dev->board].video_out)
		saa7134_videoport_init(dev);

H
Hans Verkuil 已提交
2171 2172 2173
	q = &dev->video_vbq;
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	/*
2174 2175 2176 2177 2178
	 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
	 * engine cannot handle transfers that do not start at the beginning
	 * of a page. A user-provided pointer can start anywhere in a page, so
	 * USERPTR support is a no-go unless the application knows about these
	 * limitations and has special support for this.
H
Hans Verkuil 已提交
2179
	 */
2180
	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2181 2182
	if (saa7134_userptr)
		q->io_modes |= VB2_USERPTR;
H
Hans Verkuil 已提交
2183 2184 2185 2186 2187 2188 2189
	q->drv_priv = &dev->video_q;
	q->ops = &vb2_qops;
	q->gfp_flags = GFP_DMA32;
	q->mem_ops = &vb2_dma_sg_memops;
	q->buf_struct_size = sizeof(struct saa7134_buf);
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	q->lock = &dev->lock;
2190
	q->dev = &dev->pci->dev;
H
Hans Verkuil 已提交
2191 2192 2193
	ret = vb2_queue_init(q);
	if (ret)
		return ret;
2194
	saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
H
Hans Verkuil 已提交
2195 2196 2197 2198 2199

	q = &dev->vbi_vbq;
	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
	/* Don't add VB2_USERPTR, see comment above */
	q->io_modes = VB2_MMAP | VB2_READ;
2200 2201
	if (saa7134_userptr)
		q->io_modes |= VB2_USERPTR;
H
Hans Verkuil 已提交
2202 2203 2204 2205 2206 2207 2208
	q->drv_priv = &dev->vbi_q;
	q->ops = &saa7134_vbi_qops;
	q->gfp_flags = GFP_DMA32;
	q->mem_ops = &vb2_dma_sg_memops;
	q->buf_struct_size = sizeof(struct saa7134_buf);
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	q->lock = &dev->lock;
2209
	q->dev = &dev->pci->dev;
H
Hans Verkuil 已提交
2210 2211 2212
	ret = vb2_queue_init(q);
	if (ret)
		return ret;
2213
	saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2214

2215 2216 2217
	return 0;
}

2218 2219 2220
void saa7134_video_fini(struct saa7134_dev *dev)
{
	/* free stuff */
H
Hans Verkuil 已提交
2221
	vb2_queue_release(&dev->video_vbq);
2222
	saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
H
Hans Verkuil 已提交
2223
	vb2_queue_release(&dev->vbi_vbq);
2224
	saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2225 2226 2227
	v4l2_ctrl_handler_free(&dev->ctrl_handler);
	if (card_has_radio(dev))
		v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2228 2229
}

2230 2231 2232 2233 2234 2235
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;
2236 2237

	/* Configure videoport */
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
	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 已提交
2259

2260 2261 2262
	/* Start videoport */
	saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);

L
Linus Torvalds 已提交
2263 2264 2265 2266 2267 2268 2269 2270
	return 0;
}

int saa7134_video_init2(struct saa7134_dev *dev)
{
	/* init video hw */
	set_tvnorm(dev,&tvnorms[0]);
	video_mux(dev,0);
2271
	v4l2_ctrl_handler_setup(&dev->ctrl_handler);
L
Linus Torvalds 已提交
2272 2273 2274 2275 2276
	saa7134_tvaudio_setmute(dev);
	saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
	return 0;
}

2277
void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
L
Linus Torvalds 已提交
2278 2279 2280 2281 2282 2283 2284
{
	static const char *st[] = {
		"(no signal)", "NTSC", "PAL", "SECAM" };
	u32 st1,st2;

	st1 = saa_readb(SAA7134_STATUS_VIDEO1);
	st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2285
	video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
L
Linus Torvalds 已提交
2286 2287 2288
		(st1 & 0x40) ? "not locked" : "locked",
		(st2 & 0x40) ? "no"         : "yes",
		st[st1 & 0x03]);
2289
	dev->nosignal = (st1 & 0x40) || (st2 & 0x40)  || !(st2 & 0x1);
L
Linus Torvalds 已提交
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299

	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);
	}
2300 2301 2302 2303 2304 2305

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

L
Linus Torvalds 已提交
2306 2307 2308 2309
	if (dev->mops && dev->mops->signal_change)
		dev->mops->signal_change(dev);
}

2310

L
Linus Torvalds 已提交
2311 2312 2313 2314 2315 2316
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) {
H
Hans Verkuil 已提交
2317
		field = dev->field;
L
Linus Torvalds 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
		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;
		}
H
Hans Verkuil 已提交
2333
		saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
L
Linus Torvalds 已提交
2334
	}
2335
	saa7134_buffer_next(dev, &dev->video_q);
L
Linus Torvalds 已提交
2336 2337 2338 2339

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