PageRenderTime 114ms CodeModel.GetById 11ms app.highlight 87ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/media/video/bt8xx/bttv-driver.c

https://gitlab.com/TeamCarbonXtreme/android_kernel_samsung_msm7x27
C | 4686 lines | 3673 code | 648 blank | 365 comment | 526 complexity | 3ad34d4704e6da8f65252d4e13f90884 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2
   3    bttv - Bt848 frame grabber driver
   4
   5    Copyright (C) 1996,97,98 Ralph  Metzler <rjkm@thp.uni-koeln.de>
   6			   & Marcus Metzler <mocm@thp.uni-koeln.de>
   7    (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
   8
   9    some v4l2 code lines are taken from Justin's bttv2 driver which is
  10    (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
  11
  12    V4L1 removal from:
  13    (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
  14
  15    Fixes to be fully V4L2 compliant by
  16    (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  17
  18    Cropping and overscan support
  19    Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
  20    Sponsored by OPQ Systems AB
  21
  22    This program is free software; you can redistribute it and/or modify
  23    it under the terms of the GNU General Public License as published by
  24    the Free Software Foundation; either version 2 of the License, or
  25    (at your option) any later version.
  26
  27    This program is distributed in the hope that it will be useful,
  28    but WITHOUT ANY WARRANTY; without even the implied warranty of
  29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30    GNU General Public License for more details.
  31
  32    You should have received a copy of the GNU General Public License
  33    along with this program; if not, write to the Free Software
  34    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35*/
  36
  37#include <linux/init.h>
  38#include <linux/module.h>
  39#include <linux/delay.h>
  40#include <linux/slab.h>
  41#include <linux/errno.h>
  42#include <linux/fs.h>
  43#include <linux/kernel.h>
  44#include <linux/sched.h>
  45#include <linux/smp_lock.h>
  46#include <linux/interrupt.h>
  47#include <linux/kdev_t.h>
  48#include "bttvp.h"
  49#include <media/v4l2-common.h>
  50#include <media/v4l2-ioctl.h>
  51#include <media/tvaudio.h>
  52#include <media/msp3400.h>
  53
  54#include <linux/dma-mapping.h>
  55
  56#include <asm/io.h>
  57#include <asm/byteorder.h>
  58
  59#include <media/rds.h>
  60
  61
  62unsigned int bttv_num;			/* number of Bt848s in use */
  63struct bttv *bttvs[BTTV_MAX];
  64
  65unsigned int bttv_debug;
  66unsigned int bttv_verbose = 1;
  67unsigned int bttv_gpio;
  68
  69/* config variables */
  70#ifdef __BIG_ENDIAN
  71static unsigned int bigendian=1;
  72#else
  73static unsigned int bigendian;
  74#endif
  75static unsigned int radio[BTTV_MAX];
  76static unsigned int irq_debug;
  77static unsigned int gbuffers = 8;
  78static unsigned int gbufsize = 0x208000;
  79static unsigned int reset_crop = 1;
  80
  81static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  82static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  83static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  84static int debug_latency;
  85static int disable_ir;
  86
  87static unsigned int fdsr;
  88
  89/* options */
  90static unsigned int combfilter;
  91static unsigned int lumafilter;
  92static unsigned int automute    = 1;
  93static unsigned int chroma_agc;
  94static unsigned int adc_crush   = 1;
  95static unsigned int whitecrush_upper = 0xCF;
  96static unsigned int whitecrush_lower = 0x7F;
  97static unsigned int vcr_hack;
  98static unsigned int irq_iswitch;
  99static unsigned int uv_ratio    = 50;
 100static unsigned int full_luma_range;
 101static unsigned int coring;
 102
 103/* API features (turn on/off stuff for testing) */
 104static unsigned int v4l2        = 1;
 105
 106/* insmod args */
 107module_param(bttv_verbose,      int, 0644);
 108module_param(bttv_gpio,         int, 0644);
 109module_param(bttv_debug,        int, 0644);
 110module_param(irq_debug,         int, 0644);
 111module_param(debug_latency,     int, 0644);
 112module_param(disable_ir,        int, 0444);
 113
 114module_param(fdsr,              int, 0444);
 115module_param(gbuffers,          int, 0444);
 116module_param(gbufsize,          int, 0444);
 117module_param(reset_crop,        int, 0444);
 118
 119module_param(v4l2,              int, 0644);
 120module_param(bigendian,         int, 0644);
 121module_param(irq_iswitch,       int, 0644);
 122module_param(combfilter,        int, 0444);
 123module_param(lumafilter,        int, 0444);
 124module_param(automute,          int, 0444);
 125module_param(chroma_agc,        int, 0444);
 126module_param(adc_crush,         int, 0444);
 127module_param(whitecrush_upper,  int, 0444);
 128module_param(whitecrush_lower,  int, 0444);
 129module_param(vcr_hack,          int, 0444);
 130module_param(uv_ratio,          int, 0444);
 131module_param(full_luma_range,   int, 0444);
 132module_param(coring,            int, 0444);
 133
 134module_param_array(radio,       int, NULL, 0444);
 135module_param_array(video_nr,    int, NULL, 0444);
 136module_param_array(radio_nr,    int, NULL, 0444);
 137module_param_array(vbi_nr,      int, NULL, 0444);
 138
 139MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
 140MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
 141MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
 142MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
 143MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
 144MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
 145MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
 146MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
 147MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
 148MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
 149		 "is 1 (yes) for compatibility with older applications");
 150MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
 151MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
 152MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
 153MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
 154MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
 155MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
 156MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
 157MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
 158MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
 159MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
 160MODULE_PARM_DESC(video_nr, "video device numbers");
 161MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
 162MODULE_PARM_DESC(radio_nr, "radio device numbers");
 163
 164MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
 165MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
 166MODULE_LICENSE("GPL");
 167
 168/* ----------------------------------------------------------------------- */
 169/* sysfs                                                                   */
 170
 171static ssize_t show_card(struct device *cd,
 172			 struct device_attribute *attr, char *buf)
 173{
 174	struct video_device *vfd = container_of(cd, struct video_device, dev);
 175	struct bttv *btv = video_get_drvdata(vfd);
 176	return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
 177}
 178static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
 179
 180/* ----------------------------------------------------------------------- */
 181/* dvb auto-load setup                                                     */
 182#if defined(CONFIG_MODULES) && defined(MODULE)
 183static void request_module_async(struct work_struct *work)
 184{
 185	request_module("dvb-bt8xx");
 186}
 187
 188static void request_modules(struct bttv *dev)
 189{
 190	INIT_WORK(&dev->request_module_wk, request_module_async);
 191	schedule_work(&dev->request_module_wk);
 192}
 193#else
 194#define request_modules(dev)
 195#endif /* CONFIG_MODULES */
 196
 197
 198/* ----------------------------------------------------------------------- */
 199/* static data                                                             */
 200
 201/* special timing tables from conexant... */
 202static u8 SRAM_Table[][60] =
 203{
 204	/* PAL digital input over GPIO[7:0] */
 205	{
 206		45, // 45 bytes following
 207		0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
 208		0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
 209		0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
 210		0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
 211		0x37,0x00,0xAF,0x21,0x00
 212	},
 213	/* NTSC digital input over GPIO[7:0] */
 214	{
 215		51, // 51 bytes following
 216		0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
 217		0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
 218		0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
 219		0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
 220		0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
 221		0x00,
 222	},
 223	// TGB_NTSC392 // quartzsight
 224	// This table has been modified to be used for Fusion Rev D
 225	{
 226		0x2A, // size of table = 42
 227		0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
 228		0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
 229		0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
 230		0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
 231		0x20, 0x00
 232	}
 233};
 234
 235/* minhdelayx1	first video pixel we can capture on a line and
 236   hdelayx1	start of active video, both relative to rising edge of
 237		/HRESET pulse (0H) in 1 / fCLKx1.
 238   swidth	width of active video and
 239   totalwidth	total line width, both in 1 / fCLKx1.
 240   sqwidth	total line width in square pixels.
 241   vdelay	start of active video in 2 * field lines relative to
 242		trailing edge of /VRESET pulse (VDELAY register).
 243   sheight	height of active video in 2 * field lines.
 244   videostart0	ITU-R frame line number of the line corresponding
 245		to vdelay in the first field. */
 246#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth,	 \
 247		vdelay, sheight, videostart0)				 \
 248	.cropcap.bounds.left = minhdelayx1,				 \
 249	/* * 2 because vertically we count field lines times two, */	 \
 250	/* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */		 \
 251	.cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
 252	/* 4 is a safety margin at the end of the line. */		 \
 253	.cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4,	 \
 254	.cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY,	 \
 255	.cropcap.defrect.left = hdelayx1,				 \
 256	.cropcap.defrect.top = (videostart0) * 2,			 \
 257	.cropcap.defrect.width = swidth,				 \
 258	.cropcap.defrect.height = sheight,				 \
 259	.cropcap.pixelaspect.numerator = totalwidth,			 \
 260	.cropcap.pixelaspect.denominator = sqwidth,
 261
 262const struct bttv_tvnorm bttv_tvnorms[] = {
 263	/* PAL-BDGHI */
 264	/* max. active video is actually 922, but 924 is divisible by 4 and 3! */
 265	/* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
 266	{
 267		.v4l2_id        = V4L2_STD_PAL,
 268		.name           = "PAL",
 269		.Fsc            = 35468950,
 270		.swidth         = 924,
 271		.sheight        = 576,
 272		.totalwidth     = 1135,
 273		.adelay         = 0x7f,
 274		.bdelay         = 0x72,
 275		.iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
 276		.scaledtwidth   = 1135,
 277		.hdelayx1       = 186,
 278		.hactivex1      = 924,
 279		.vdelay         = 0x20,
 280		.vbipack        = 255, /* min (2048 / 4, 0x1ff) & 0xff */
 281		.sram           = 0,
 282		/* ITU-R frame line number of the first VBI line
 283		   we can capture, of the first and second field.
 284		   The last line is determined by cropcap.bounds. */
 285		.vbistart       = { 7, 320 },
 286		CROPCAP(/* minhdelayx1 */ 68,
 287			/* hdelayx1 */ 186,
 288			/* Should be (768 * 1135 + 944 / 2) / 944.
 289			   cropcap.defrect is used for image width
 290			   checks, so we keep the old value 924. */
 291			/* swidth */ 924,
 292			/* totalwidth */ 1135,
 293			/* sqwidth */ 944,
 294			/* vdelay */ 0x20,
 295			/* sheight */ 576,
 296			/* videostart0 */ 23)
 297		/* bt878 (and bt848?) can capture another
 298		   line below active video. */
 299		.cropcap.bounds.height = (576 + 2) + 0x20 - 2,
 300	},{
 301		.v4l2_id        = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
 302		.name           = "NTSC",
 303		.Fsc            = 28636363,
 304		.swidth         = 768,
 305		.sheight        = 480,
 306		.totalwidth     = 910,
 307		.adelay         = 0x68,
 308		.bdelay         = 0x5d,
 309		.iform          = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
 310		.scaledtwidth   = 910,
 311		.hdelayx1       = 128,
 312		.hactivex1      = 910,
 313		.vdelay         = 0x1a,
 314		.vbipack        = 144, /* min (1600 / 4, 0x1ff) & 0xff */
 315		.sram           = 1,
 316		.vbistart	= { 10, 273 },
 317		CROPCAP(/* minhdelayx1 */ 68,
 318			/* hdelayx1 */ 128,
 319			/* Should be (640 * 910 + 780 / 2) / 780? */
 320			/* swidth */ 768,
 321			/* totalwidth */ 910,
 322			/* sqwidth */ 780,
 323			/* vdelay */ 0x1a,
 324			/* sheight */ 480,
 325			/* videostart0 */ 23)
 326	},{
 327		.v4l2_id        = V4L2_STD_SECAM,
 328		.name           = "SECAM",
 329		.Fsc            = 35468950,
 330		.swidth         = 924,
 331		.sheight        = 576,
 332		.totalwidth     = 1135,
 333		.adelay         = 0x7f,
 334		.bdelay         = 0xb0,
 335		.iform          = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
 336		.scaledtwidth   = 1135,
 337		.hdelayx1       = 186,
 338		.hactivex1      = 922,
 339		.vdelay         = 0x20,
 340		.vbipack        = 255,
 341		.sram           = 0, /* like PAL, correct? */
 342		.vbistart	= { 7, 320 },
 343		CROPCAP(/* minhdelayx1 */ 68,
 344			/* hdelayx1 */ 186,
 345			/* swidth */ 924,
 346			/* totalwidth */ 1135,
 347			/* sqwidth */ 944,
 348			/* vdelay */ 0x20,
 349			/* sheight */ 576,
 350			/* videostart0 */ 23)
 351	},{
 352		.v4l2_id        = V4L2_STD_PAL_Nc,
 353		.name           = "PAL-Nc",
 354		.Fsc            = 28636363,
 355		.swidth         = 640,
 356		.sheight        = 576,
 357		.totalwidth     = 910,
 358		.adelay         = 0x68,
 359		.bdelay         = 0x5d,
 360		.iform          = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
 361		.scaledtwidth   = 780,
 362		.hdelayx1       = 130,
 363		.hactivex1      = 734,
 364		.vdelay         = 0x1a,
 365		.vbipack        = 144,
 366		.sram           = -1,
 367		.vbistart	= { 7, 320 },
 368		CROPCAP(/* minhdelayx1 */ 68,
 369			/* hdelayx1 */ 130,
 370			/* swidth */ (640 * 910 + 780 / 2) / 780,
 371			/* totalwidth */ 910,
 372			/* sqwidth */ 780,
 373			/* vdelay */ 0x1a,
 374			/* sheight */ 576,
 375			/* videostart0 */ 23)
 376	},{
 377		.v4l2_id        = V4L2_STD_PAL_M,
 378		.name           = "PAL-M",
 379		.Fsc            = 28636363,
 380		.swidth         = 640,
 381		.sheight        = 480,
 382		.totalwidth     = 910,
 383		.adelay         = 0x68,
 384		.bdelay         = 0x5d,
 385		.iform          = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
 386		.scaledtwidth   = 780,
 387		.hdelayx1       = 135,
 388		.hactivex1      = 754,
 389		.vdelay         = 0x1a,
 390		.vbipack        = 144,
 391		.sram           = -1,
 392		.vbistart	= { 10, 273 },
 393		CROPCAP(/* minhdelayx1 */ 68,
 394			/* hdelayx1 */ 135,
 395			/* swidth */ (640 * 910 + 780 / 2) / 780,
 396			/* totalwidth */ 910,
 397			/* sqwidth */ 780,
 398			/* vdelay */ 0x1a,
 399			/* sheight */ 480,
 400			/* videostart0 */ 23)
 401	},{
 402		.v4l2_id        = V4L2_STD_PAL_N,
 403		.name           = "PAL-N",
 404		.Fsc            = 35468950,
 405		.swidth         = 768,
 406		.sheight        = 576,
 407		.totalwidth     = 1135,
 408		.adelay         = 0x7f,
 409		.bdelay         = 0x72,
 410		.iform          = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
 411		.scaledtwidth   = 944,
 412		.hdelayx1       = 186,
 413		.hactivex1      = 922,
 414		.vdelay         = 0x20,
 415		.vbipack        = 144,
 416		.sram           = -1,
 417		.vbistart       = { 7, 320 },
 418		CROPCAP(/* minhdelayx1 */ 68,
 419			/* hdelayx1 */ 186,
 420			/* swidth */ (768 * 1135 + 944 / 2) / 944,
 421			/* totalwidth */ 1135,
 422			/* sqwidth */ 944,
 423			/* vdelay */ 0x20,
 424			/* sheight */ 576,
 425			/* videostart0 */ 23)
 426	},{
 427		.v4l2_id        = V4L2_STD_NTSC_M_JP,
 428		.name           = "NTSC-JP",
 429		.Fsc            = 28636363,
 430		.swidth         = 640,
 431		.sheight        = 480,
 432		.totalwidth     = 910,
 433		.adelay         = 0x68,
 434		.bdelay         = 0x5d,
 435		.iform          = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
 436		.scaledtwidth   = 780,
 437		.hdelayx1       = 135,
 438		.hactivex1      = 754,
 439		.vdelay         = 0x16,
 440		.vbipack        = 144,
 441		.sram           = -1,
 442		.vbistart       = { 10, 273 },
 443		CROPCAP(/* minhdelayx1 */ 68,
 444			/* hdelayx1 */ 135,
 445			/* swidth */ (640 * 910 + 780 / 2) / 780,
 446			/* totalwidth */ 910,
 447			/* sqwidth */ 780,
 448			/* vdelay */ 0x16,
 449			/* sheight */ 480,
 450			/* videostart0 */ 23)
 451	},{
 452		/* that one hopefully works with the strange timing
 453		 * which video recorders produce when playing a NTSC
 454		 * tape on a PAL TV ... */
 455		.v4l2_id        = V4L2_STD_PAL_60,
 456		.name           = "PAL-60",
 457		.Fsc            = 35468950,
 458		.swidth         = 924,
 459		.sheight        = 480,
 460		.totalwidth     = 1135,
 461		.adelay         = 0x7f,
 462		.bdelay         = 0x72,
 463		.iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
 464		.scaledtwidth   = 1135,
 465		.hdelayx1       = 186,
 466		.hactivex1      = 924,
 467		.vdelay         = 0x1a,
 468		.vbipack        = 255,
 469		.vtotal         = 524,
 470		.sram           = -1,
 471		.vbistart	= { 10, 273 },
 472		CROPCAP(/* minhdelayx1 */ 68,
 473			/* hdelayx1 */ 186,
 474			/* swidth */ 924,
 475			/* totalwidth */ 1135,
 476			/* sqwidth */ 944,
 477			/* vdelay */ 0x1a,
 478			/* sheight */ 480,
 479			/* videostart0 */ 23)
 480	}
 481};
 482static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
 483
 484/* ----------------------------------------------------------------------- */
 485/* bttv format list
 486   packed pixel formats must come first */
 487static const struct bttv_format formats[] = {
 488	{
 489		.name     = "8 bpp, gray",
 490		.fourcc   = V4L2_PIX_FMT_GREY,
 491		.btformat = BT848_COLOR_FMT_Y8,
 492		.depth    = 8,
 493		.flags    = FORMAT_FLAGS_PACKED,
 494	},{
 495		.name     = "8 bpp, dithered color",
 496		.fourcc   = V4L2_PIX_FMT_HI240,
 497		.btformat = BT848_COLOR_FMT_RGB8,
 498		.depth    = 8,
 499		.flags    = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
 500	},{
 501		.name     = "15 bpp RGB, le",
 502		.fourcc   = V4L2_PIX_FMT_RGB555,
 503		.btformat = BT848_COLOR_FMT_RGB15,
 504		.depth    = 16,
 505		.flags    = FORMAT_FLAGS_PACKED,
 506	},{
 507		.name     = "15 bpp RGB, be",
 508		.fourcc   = V4L2_PIX_FMT_RGB555X,
 509		.btformat = BT848_COLOR_FMT_RGB15,
 510		.btswap   = 0x03, /* byteswap */
 511		.depth    = 16,
 512		.flags    = FORMAT_FLAGS_PACKED,
 513	},{
 514		.name     = "16 bpp RGB, le",
 515		.fourcc   = V4L2_PIX_FMT_RGB565,
 516		.btformat = BT848_COLOR_FMT_RGB16,
 517		.depth    = 16,
 518		.flags    = FORMAT_FLAGS_PACKED,
 519	},{
 520		.name     = "16 bpp RGB, be",
 521		.fourcc   = V4L2_PIX_FMT_RGB565X,
 522		.btformat = BT848_COLOR_FMT_RGB16,
 523		.btswap   = 0x03, /* byteswap */
 524		.depth    = 16,
 525		.flags    = FORMAT_FLAGS_PACKED,
 526	},{
 527		.name     = "24 bpp RGB, le",
 528		.fourcc   = V4L2_PIX_FMT_BGR24,
 529		.btformat = BT848_COLOR_FMT_RGB24,
 530		.depth    = 24,
 531		.flags    = FORMAT_FLAGS_PACKED,
 532	},{
 533		.name     = "32 bpp RGB, le",
 534		.fourcc   = V4L2_PIX_FMT_BGR32,
 535		.btformat = BT848_COLOR_FMT_RGB32,
 536		.depth    = 32,
 537		.flags    = FORMAT_FLAGS_PACKED,
 538	},{
 539		.name     = "32 bpp RGB, be",
 540		.fourcc   = V4L2_PIX_FMT_RGB32,
 541		.btformat = BT848_COLOR_FMT_RGB32,
 542		.btswap   = 0x0f, /* byte+word swap */
 543		.depth    = 32,
 544		.flags    = FORMAT_FLAGS_PACKED,
 545	},{
 546		.name     = "4:2:2, packed, YUYV",
 547		.fourcc   = V4L2_PIX_FMT_YUYV,
 548		.btformat = BT848_COLOR_FMT_YUY2,
 549		.depth    = 16,
 550		.flags    = FORMAT_FLAGS_PACKED,
 551	},{
 552		.name     = "4:2:2, packed, YUYV",
 553		.fourcc   = V4L2_PIX_FMT_YUYV,
 554		.btformat = BT848_COLOR_FMT_YUY2,
 555		.depth    = 16,
 556		.flags    = FORMAT_FLAGS_PACKED,
 557	},{
 558		.name     = "4:2:2, packed, UYVY",
 559		.fourcc   = V4L2_PIX_FMT_UYVY,
 560		.btformat = BT848_COLOR_FMT_YUY2,
 561		.btswap   = 0x03, /* byteswap */
 562		.depth    = 16,
 563		.flags    = FORMAT_FLAGS_PACKED,
 564	},{
 565		.name     = "4:2:2, planar, Y-Cb-Cr",
 566		.fourcc   = V4L2_PIX_FMT_YUV422P,
 567		.btformat = BT848_COLOR_FMT_YCrCb422,
 568		.depth    = 16,
 569		.flags    = FORMAT_FLAGS_PLANAR,
 570		.hshift   = 1,
 571		.vshift   = 0,
 572	},{
 573		.name     = "4:2:0, planar, Y-Cb-Cr",
 574		.fourcc   = V4L2_PIX_FMT_YUV420,
 575		.btformat = BT848_COLOR_FMT_YCrCb422,
 576		.depth    = 12,
 577		.flags    = FORMAT_FLAGS_PLANAR,
 578		.hshift   = 1,
 579		.vshift   = 1,
 580	},{
 581		.name     = "4:2:0, planar, Y-Cr-Cb",
 582		.fourcc   = V4L2_PIX_FMT_YVU420,
 583		.btformat = BT848_COLOR_FMT_YCrCb422,
 584		.depth    = 12,
 585		.flags    = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
 586		.hshift   = 1,
 587		.vshift   = 1,
 588	},{
 589		.name     = "4:1:1, planar, Y-Cb-Cr",
 590		.fourcc   = V4L2_PIX_FMT_YUV411P,
 591		.btformat = BT848_COLOR_FMT_YCrCb411,
 592		.depth    = 12,
 593		.flags    = FORMAT_FLAGS_PLANAR,
 594		.hshift   = 2,
 595		.vshift   = 0,
 596	},{
 597		.name     = "4:1:0, planar, Y-Cb-Cr",
 598		.fourcc   = V4L2_PIX_FMT_YUV410,
 599		.btformat = BT848_COLOR_FMT_YCrCb411,
 600		.depth    = 9,
 601		.flags    = FORMAT_FLAGS_PLANAR,
 602		.hshift   = 2,
 603		.vshift   = 2,
 604	},{
 605		.name     = "4:1:0, planar, Y-Cr-Cb",
 606		.fourcc   = V4L2_PIX_FMT_YVU410,
 607		.btformat = BT848_COLOR_FMT_YCrCb411,
 608		.depth    = 9,
 609		.flags    = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
 610		.hshift   = 2,
 611		.vshift   = 2,
 612	},{
 613		.name     = "raw scanlines",
 614		.fourcc   = -1,
 615		.btformat = BT848_COLOR_FMT_RAW,
 616		.depth    = 8,
 617		.flags    = FORMAT_FLAGS_RAW,
 618	}
 619};
 620static const unsigned int FORMATS = ARRAY_SIZE(formats);
 621
 622/* ----------------------------------------------------------------------- */
 623
 624#define V4L2_CID_PRIVATE_CHROMA_AGC  (V4L2_CID_PRIVATE_BASE + 0)
 625#define V4L2_CID_PRIVATE_COMBFILTER  (V4L2_CID_PRIVATE_BASE + 1)
 626#define V4L2_CID_PRIVATE_AUTOMUTE    (V4L2_CID_PRIVATE_BASE + 2)
 627#define V4L2_CID_PRIVATE_LUMAFILTER  (V4L2_CID_PRIVATE_BASE + 3)
 628#define V4L2_CID_PRIVATE_AGC_CRUSH   (V4L2_CID_PRIVATE_BASE + 4)
 629#define V4L2_CID_PRIVATE_VCR_HACK    (V4L2_CID_PRIVATE_BASE + 5)
 630#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER   (V4L2_CID_PRIVATE_BASE + 6)
 631#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER   (V4L2_CID_PRIVATE_BASE + 7)
 632#define V4L2_CID_PRIVATE_UV_RATIO    (V4L2_CID_PRIVATE_BASE + 8)
 633#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE    (V4L2_CID_PRIVATE_BASE + 9)
 634#define V4L2_CID_PRIVATE_CORING      (V4L2_CID_PRIVATE_BASE + 10)
 635#define V4L2_CID_PRIVATE_LASTP1      (V4L2_CID_PRIVATE_BASE + 11)
 636
 637static const struct v4l2_queryctrl no_ctl = {
 638	.name  = "42",
 639	.flags = V4L2_CTRL_FLAG_DISABLED,
 640};
 641static const struct v4l2_queryctrl bttv_ctls[] = {
 642	/* --- video --- */
 643	{
 644		.id            = V4L2_CID_BRIGHTNESS,
 645		.name          = "Brightness",
 646		.minimum       = 0,
 647		.maximum       = 65535,
 648		.step          = 256,
 649		.default_value = 32768,
 650		.type          = V4L2_CTRL_TYPE_INTEGER,
 651	},{
 652		.id            = V4L2_CID_CONTRAST,
 653		.name          = "Contrast",
 654		.minimum       = 0,
 655		.maximum       = 65535,
 656		.step          = 128,
 657		.default_value = 32768,
 658		.type          = V4L2_CTRL_TYPE_INTEGER,
 659	},{
 660		.id            = V4L2_CID_SATURATION,
 661		.name          = "Saturation",
 662		.minimum       = 0,
 663		.maximum       = 65535,
 664		.step          = 128,
 665		.default_value = 32768,
 666		.type          = V4L2_CTRL_TYPE_INTEGER,
 667	},{
 668		.id            = V4L2_CID_HUE,
 669		.name          = "Hue",
 670		.minimum       = 0,
 671		.maximum       = 65535,
 672		.step          = 256,
 673		.default_value = 32768,
 674		.type          = V4L2_CTRL_TYPE_INTEGER,
 675	},
 676	/* --- audio --- */
 677	{
 678		.id            = V4L2_CID_AUDIO_MUTE,
 679		.name          = "Mute",
 680		.minimum       = 0,
 681		.maximum       = 1,
 682		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 683	},{
 684		.id            = V4L2_CID_AUDIO_VOLUME,
 685		.name          = "Volume",
 686		.minimum       = 0,
 687		.maximum       = 65535,
 688		.step          = 65535/100,
 689		.default_value = 65535,
 690		.type          = V4L2_CTRL_TYPE_INTEGER,
 691	},{
 692		.id            = V4L2_CID_AUDIO_BALANCE,
 693		.name          = "Balance",
 694		.minimum       = 0,
 695		.maximum       = 65535,
 696		.step          = 65535/100,
 697		.default_value = 32768,
 698		.type          = V4L2_CTRL_TYPE_INTEGER,
 699	},{
 700		.id            = V4L2_CID_AUDIO_BASS,
 701		.name          = "Bass",
 702		.minimum       = 0,
 703		.maximum       = 65535,
 704		.step          = 65535/100,
 705		.default_value = 32768,
 706		.type          = V4L2_CTRL_TYPE_INTEGER,
 707	},{
 708		.id            = V4L2_CID_AUDIO_TREBLE,
 709		.name          = "Treble",
 710		.minimum       = 0,
 711		.maximum       = 65535,
 712		.step          = 65535/100,
 713		.default_value = 32768,
 714		.type          = V4L2_CTRL_TYPE_INTEGER,
 715	},
 716	/* --- private --- */
 717	{
 718		.id            = V4L2_CID_PRIVATE_CHROMA_AGC,
 719		.name          = "chroma agc",
 720		.minimum       = 0,
 721		.maximum       = 1,
 722		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 723	},{
 724		.id            = V4L2_CID_PRIVATE_COMBFILTER,
 725		.name          = "combfilter",
 726		.minimum       = 0,
 727		.maximum       = 1,
 728		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 729	},{
 730		.id            = V4L2_CID_PRIVATE_AUTOMUTE,
 731		.name          = "automute",
 732		.minimum       = 0,
 733		.maximum       = 1,
 734		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 735	},{
 736		.id            = V4L2_CID_PRIVATE_LUMAFILTER,
 737		.name          = "luma decimation filter",
 738		.minimum       = 0,
 739		.maximum       = 1,
 740		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 741	},{
 742		.id            = V4L2_CID_PRIVATE_AGC_CRUSH,
 743		.name          = "agc crush",
 744		.minimum       = 0,
 745		.maximum       = 1,
 746		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 747	},{
 748		.id            = V4L2_CID_PRIVATE_VCR_HACK,
 749		.name          = "vcr hack",
 750		.minimum       = 0,
 751		.maximum       = 1,
 752		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 753	},{
 754		.id            = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
 755		.name          = "whitecrush upper",
 756		.minimum       = 0,
 757		.maximum       = 255,
 758		.step          = 1,
 759		.default_value = 0xCF,
 760		.type          = V4L2_CTRL_TYPE_INTEGER,
 761	},{
 762		.id            = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
 763		.name          = "whitecrush lower",
 764		.minimum       = 0,
 765		.maximum       = 255,
 766		.step          = 1,
 767		.default_value = 0x7F,
 768		.type          = V4L2_CTRL_TYPE_INTEGER,
 769	},{
 770		.id            = V4L2_CID_PRIVATE_UV_RATIO,
 771		.name          = "uv ratio",
 772		.minimum       = 0,
 773		.maximum       = 100,
 774		.step          = 1,
 775		.default_value = 50,
 776		.type          = V4L2_CTRL_TYPE_INTEGER,
 777	},{
 778		.id            = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
 779		.name          = "full luma range",
 780		.minimum       = 0,
 781		.maximum       = 1,
 782		.type          = V4L2_CTRL_TYPE_BOOLEAN,
 783	},{
 784		.id            = V4L2_CID_PRIVATE_CORING,
 785		.name          = "coring",
 786		.minimum       = 0,
 787		.maximum       = 3,
 788		.step          = 1,
 789		.default_value = 0,
 790		.type          = V4L2_CTRL_TYPE_INTEGER,
 791	}
 792
 793
 794
 795};
 796
 797static const struct v4l2_queryctrl *ctrl_by_id(int id)
 798{
 799	int i;
 800
 801	for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
 802		if (bttv_ctls[i].id == id)
 803			return bttv_ctls+i;
 804
 805	return NULL;
 806}
 807
 808/* ----------------------------------------------------------------------- */
 809/* resource management                                                     */
 810
 811/*
 812   RESOURCE_    allocated by                freed by
 813
 814   VIDEO_READ   bttv_read 1)                bttv_read 2)
 815
 816   VIDEO_STREAM VIDIOC_STREAMON             VIDIOC_STREAMOFF
 817		 VIDIOC_QBUF 1)              bttv_release
 818		 VIDIOCMCAPTURE 1)
 819
 820   OVERLAY	 VIDIOCCAPTURE on            VIDIOCCAPTURE off
 821		 VIDIOC_OVERLAY on           VIDIOC_OVERLAY off
 822		 3)                          bttv_release
 823
 824   VBI		 VIDIOC_STREAMON             VIDIOC_STREAMOFF
 825		 VIDIOC_QBUF 1)              bttv_release
 826		 bttv_read, bttv_poll 1) 4)
 827
 828   1) The resource must be allocated when we enter buffer prepare functions
 829      and remain allocated while buffers are in the DMA queue.
 830   2) This is a single frame read.
 831   3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
 832      RESOURCE_OVERLAY is allocated.
 833   4) This is a continuous read, implies VIDIOC_STREAMON.
 834
 835   Note this driver permits video input and standard changes regardless if
 836   resources are allocated.
 837*/
 838
 839#define VBI_RESOURCES (RESOURCE_VBI)
 840#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
 841			 RESOURCE_VIDEO_STREAM | \
 842			 RESOURCE_OVERLAY)
 843
 844static
 845int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
 846{
 847	int xbits; /* mutual exclusive resources */
 848
 849	if (fh->resources & bit)
 850		/* have it already allocated */
 851		return 1;
 852
 853	xbits = bit;
 854	if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
 855		xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
 856
 857	/* is it free? */
 858	mutex_lock(&btv->lock);
 859	if (btv->resources & xbits) {
 860		/* no, someone else uses it */
 861		goto fail;
 862	}
 863
 864	if ((bit & VIDEO_RESOURCES)
 865	    && 0 == (btv->resources & VIDEO_RESOURCES)) {
 866		/* Do crop - use current, don't - use default parameters. */
 867		__s32 top = btv->crop[!!fh->do_crop].rect.top;
 868
 869		if (btv->vbi_end > top)
 870			goto fail;
 871
 872		/* We cannot capture the same line as video and VBI data.
 873		   Claim scan lines crop[].rect.top to bottom. */
 874		btv->crop_start = top;
 875	} else if (bit & VBI_RESOURCES) {
 876		__s32 end = fh->vbi_fmt.end;
 877
 878		if (end > btv->crop_start)
 879			goto fail;
 880
 881		/* Claim scan lines above fh->vbi_fmt.end. */
 882		btv->vbi_end = end;
 883	}
 884
 885	/* it's free, grab it */
 886	fh->resources  |= bit;
 887	btv->resources |= bit;
 888	mutex_unlock(&btv->lock);
 889	return 1;
 890
 891 fail:
 892	mutex_unlock(&btv->lock);
 893	return 0;
 894}
 895
 896static
 897int check_btres(struct bttv_fh *fh, int bit)
 898{
 899	return (fh->resources & bit);
 900}
 901
 902static
 903int locked_btres(struct bttv *btv, int bit)
 904{
 905	return (btv->resources & bit);
 906}
 907
 908/* Call with btv->lock down. */
 909static void
 910disclaim_vbi_lines(struct bttv *btv)
 911{
 912	btv->vbi_end = 0;
 913}
 914
 915/* Call with btv->lock down. */
 916static void
 917disclaim_video_lines(struct bttv *btv)
 918{
 919	const struct bttv_tvnorm *tvnorm;
 920	u8 crop;
 921
 922	tvnorm = &bttv_tvnorms[btv->tvnorm];
 923	btv->crop_start = tvnorm->cropcap.bounds.top
 924		+ tvnorm->cropcap.bounds.height;
 925
 926	/* VBI capturing ends at VDELAY, start of video capturing, no
 927	   matter how many lines the VBI RISC program expects. When video
 928	   capturing is off, it shall no longer "preempt" VBI capturing,
 929	   so we set VDELAY to maximum. */
 930	crop = btread(BT848_E_CROP) | 0xc0;
 931	btwrite(crop, BT848_E_CROP);
 932	btwrite(0xfe, BT848_E_VDELAY_LO);
 933	btwrite(crop, BT848_O_CROP);
 934	btwrite(0xfe, BT848_O_VDELAY_LO);
 935}
 936
 937static
 938void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
 939{
 940	if ((fh->resources & bits) != bits) {
 941		/* trying to free ressources not allocated by us ... */
 942		printk("bttv: BUG! (btres)\n");
 943	}
 944	mutex_lock(&btv->lock);
 945	fh->resources  &= ~bits;
 946	btv->resources &= ~bits;
 947
 948	bits = btv->resources;
 949
 950	if (0 == (bits & VIDEO_RESOURCES))
 951		disclaim_video_lines(btv);
 952
 953	if (0 == (bits & VBI_RESOURCES))
 954		disclaim_vbi_lines(btv);
 955
 956	mutex_unlock(&btv->lock);
 957}
 958
 959/* ----------------------------------------------------------------------- */
 960/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC          */
 961
 962/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
 963   PLL_X = Reference pre-divider (0=1, 1=2)
 964   PLL_C = Post divider (0=6, 1=4)
 965   PLL_I = Integer input
 966   PLL_F = Fractional input
 967
 968   F_input = 28.636363 MHz:
 969   PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
 970*/
 971
 972static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
 973{
 974	unsigned char fl, fh, fi;
 975
 976	/* prevent overflows */
 977	fin/=4;
 978	fout/=4;
 979
 980	fout*=12;
 981	fi=fout/fin;
 982
 983	fout=(fout%fin)*256;
 984	fh=fout/fin;
 985
 986	fout=(fout%fin)*256;
 987	fl=fout/fin;
 988
 989	btwrite(fl, BT848_PLL_F_LO);
 990	btwrite(fh, BT848_PLL_F_HI);
 991	btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
 992}
 993
 994static void set_pll(struct bttv *btv)
 995{
 996	int i;
 997
 998	if (!btv->pll.pll_crystal)
 999		return;
1000
1001	if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1002		dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
1003		return;
1004	}
1005
1006	if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1007		/* no PLL needed */
1008		if (btv->pll.pll_current == 0)
1009			return;
1010		bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1011			btv->c.nr,btv->pll.pll_ifreq);
1012		btwrite(0x00,BT848_TGCTRL);
1013		btwrite(0x00,BT848_PLL_XCI);
1014		btv->pll.pll_current = 0;
1015		return;
1016	}
1017
1018	bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1019		btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1020	set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1021
1022	for (i=0; i<10; i++) {
1023		/*  Let other people run while the PLL stabilizes */
1024		bttv_printk(".");
1025		msleep(10);
1026
1027		if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1028			btwrite(0,BT848_DSTATUS);
1029		} else {
1030			btwrite(0x08,BT848_TGCTRL);
1031			btv->pll.pll_current = btv->pll.pll_ofreq;
1032			bttv_printk(" ok\n");
1033			return;
1034		}
1035	}
1036	btv->pll.pll_current = -1;
1037	bttv_printk("failed\n");
1038	return;
1039}
1040
1041/* used to switch between the bt848's analog/digital video capture modes */
1042static void bt848A_set_timing(struct bttv *btv)
1043{
1044	int i, len;
1045	int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1046	int fsc       = bttv_tvnorms[btv->tvnorm].Fsc;
1047
1048	if (btv->input == btv->dig) {
1049		dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1050			btv->c.nr,table_idx);
1051
1052		/* timing change...reset timing generator address */
1053		btwrite(0x00, BT848_TGCTRL);
1054		btwrite(0x02, BT848_TGCTRL);
1055		btwrite(0x00, BT848_TGCTRL);
1056
1057		len=SRAM_Table[table_idx][0];
1058		for(i = 1; i <= len; i++)
1059			btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1060		btv->pll.pll_ofreq = 27000000;
1061
1062		set_pll(btv);
1063		btwrite(0x11, BT848_TGCTRL);
1064		btwrite(0x41, BT848_DVSIF);
1065	} else {
1066		btv->pll.pll_ofreq = fsc;
1067		set_pll(btv);
1068		btwrite(0x0, BT848_DVSIF);
1069	}
1070}
1071
1072/* ----------------------------------------------------------------------- */
1073
1074static void bt848_bright(struct bttv *btv, int bright)
1075{
1076	int value;
1077
1078	// printk("bttv: set bright: %d\n",bright); // DEBUG
1079	btv->bright = bright;
1080
1081	/* We want -128 to 127 we get 0-65535 */
1082	value = (bright >> 8) - 128;
1083	btwrite(value & 0xff, BT848_BRIGHT);
1084}
1085
1086static void bt848_hue(struct bttv *btv, int hue)
1087{
1088	int value;
1089
1090	btv->hue = hue;
1091
1092	/* -128 to 127 */
1093	value = (hue >> 8) - 128;
1094	btwrite(value & 0xff, BT848_HUE);
1095}
1096
1097static void bt848_contrast(struct bttv *btv, int cont)
1098{
1099	int value,hibit;
1100
1101	btv->contrast = cont;
1102
1103	/* 0-511 */
1104	value = (cont  >> 7);
1105	hibit = (value >> 6) & 4;
1106	btwrite(value & 0xff, BT848_CONTRAST_LO);
1107	btaor(hibit, ~4, BT848_E_CONTROL);
1108	btaor(hibit, ~4, BT848_O_CONTROL);
1109}
1110
1111static void bt848_sat(struct bttv *btv, int color)
1112{
1113	int val_u,val_v,hibits;
1114
1115	btv->saturation = color;
1116
1117	/* 0-511 for the color */
1118	val_u   = ((color * btv->opt_uv_ratio) / 50) >> 7;
1119	val_v   = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1120	hibits  = (val_u >> 7) & 2;
1121	hibits |= (val_v >> 8) & 1;
1122	btwrite(val_u & 0xff, BT848_SAT_U_LO);
1123	btwrite(val_v & 0xff, BT848_SAT_V_LO);
1124	btaor(hibits, ~3, BT848_E_CONTROL);
1125	btaor(hibits, ~3, BT848_O_CONTROL);
1126}
1127
1128/* ----------------------------------------------------------------------- */
1129
1130static int
1131video_mux(struct bttv *btv, unsigned int input)
1132{
1133	int mux,mask2;
1134
1135	if (input >= bttv_tvcards[btv->c.type].video_inputs)
1136		return -EINVAL;
1137
1138	/* needed by RemoteVideo MX */
1139	mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1140	if (mask2)
1141		gpio_inout(mask2,mask2);
1142
1143	if (input == btv->svhs)  {
1144		btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1145		btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1146	} else {
1147		btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1148		btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1149	}
1150	mux = bttv_muxsel(btv, input);
1151	btaor(mux<<5, ~(3<<5), BT848_IFORM);
1152	dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1153		btv->c.nr,input,mux);
1154
1155	/* card specific hook */
1156	if(bttv_tvcards[btv->c.type].muxsel_hook)
1157		bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1158	return 0;
1159}
1160
1161static char *audio_modes[] = {
1162	"audio: tuner", "audio: radio", "audio: extern",
1163	"audio: intern", "audio: mute"
1164};
1165
1166static int
1167audio_mux(struct bttv *btv, int input, int mute)
1168{
1169	int gpio_val, signal;
1170	struct v4l2_control ctrl;
1171
1172	gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1173		   bttv_tvcards[btv->c.type].gpiomask);
1174	signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1175
1176	btv->mute = mute;
1177	btv->audio = input;
1178
1179	/* automute */
1180	mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1181
1182	if (mute)
1183		gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1184	else
1185		gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1186
1187	switch (btv->c.type) {
1188	case BTTV_BOARD_VOODOOTV_FM:
1189	case BTTV_BOARD_VOODOOTV_200:
1190		gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1191		break;
1192
1193	default:
1194		gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1195	}
1196
1197	if (bttv_gpio)
1198		bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1199	if (in_interrupt())
1200		return 0;
1201
1202	ctrl.id = V4L2_CID_AUDIO_MUTE;
1203	ctrl.value = btv->mute;
1204	bttv_call_all(btv, core, s_ctrl, &ctrl);
1205	if (btv->sd_msp34xx) {
1206		u32 in;
1207
1208		/* Note: the inputs tuner/radio/extern/intern are translated
1209		   to msp routings. This assumes common behavior for all msp3400
1210		   based TV cards. When this assumption fails, then the
1211		   specific MSP routing must be added to the card table.
1212		   For now this is sufficient. */
1213		switch (input) {
1214		case TVAUDIO_INPUT_RADIO:
1215			in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1216				    MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1217			break;
1218		case TVAUDIO_INPUT_EXTERN:
1219			in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1220				    MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1221			break;
1222		case TVAUDIO_INPUT_INTERN:
1223			/* Yes, this is the same input as for RADIO. I doubt
1224			   if this is ever used. The only board with an INTERN
1225			   input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1226			   that was tested. My guess is that the whole INTERN
1227			   input does not work. */
1228			in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1229				    MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1230			break;
1231		case TVAUDIO_INPUT_TUNER:
1232		default:
1233			/* This is the only card that uses TUNER2, and afaik,
1234			   is the only difference between the VOODOOTV_FM
1235			   and VOODOOTV_200 */
1236			if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1237				in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1238					MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1239			else
1240				in = MSP_INPUT_DEFAULT;
1241			break;
1242		}
1243		v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1244			       in, MSP_OUTPUT_DEFAULT, 0);
1245	}
1246	if (btv->sd_tvaudio) {
1247		v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1248				input, 0, 0);
1249	}
1250	return 0;
1251}
1252
1253static inline int
1254audio_mute(struct bttv *btv, int mute)
1255{
1256	return audio_mux(btv, btv->audio, mute);
1257}
1258
1259static inline int
1260audio_input(struct bttv *btv, int input)
1261{
1262	return audio_mux(btv, input, btv->mute);
1263}
1264
1265static void
1266bttv_crop_calc_limits(struct bttv_crop *c)
1267{
1268	/* Scale factor min. 1:1, max. 16:1. Min. image size
1269	   48 x 32. Scaled width must be a multiple of 4. */
1270
1271	if (1) {
1272		/* For bug compatibility with VIDIOCGCAP and image
1273		   size checks in earlier driver versions. */
1274		c->min_scaled_width = 48;
1275		c->min_scaled_height = 32;
1276	} else {
1277		c->min_scaled_width =
1278			(max(48, c->rect.width >> 4) + 3) & ~3;
1279		c->min_scaled_height =
1280			max(32, c->rect.height >> 4);
1281	}
1282
1283	c->max_scaled_width  = c->rect.width & ~3;
1284	c->max_scaled_height = c->rect.height;
1285}
1286
1287static void
1288bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1289{
1290	c->rect = bttv_tvnorms[norm].cropcap.defrect;
1291	bttv_crop_calc_limits(c);
1292}
1293
1294/* Call with btv->lock down. */
1295static int
1296set_tvnorm(struct bttv *btv, unsigned int norm)
1297{
1298	const struct bttv_tvnorm *tvnorm;
1299	v4l2_std_id id;
1300
1301	BUG_ON(norm >= BTTV_TVNORMS);
1302	BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1303
1304	tvnorm = &bttv_tvnorms[norm];
1305
1306	if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1307		    sizeof (tvnorm->cropcap))) {
1308		bttv_crop_reset(&btv->crop[0], norm);
1309		btv->crop[1] = btv->crop[0]; /* current = default */
1310
1311		if (0 == (btv->resources & VIDEO_RESOURCES)) {
1312			btv->crop_start = tvnorm->cropcap.bounds.top
1313				+ tvnorm->cropcap.bounds.height;
1314		}
1315	}
1316
1317	btv->tvnorm = norm;
1318
1319	btwrite(tvnorm->adelay, BT848_ADELAY);
1320	btwrite(tvnorm->bdelay, BT848_BDELAY);
1321	btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1322	      BT848_IFORM);
1323	btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1324	btwrite(1, BT848_VBI_PACK_DEL);
1325	bt848A_set_timing(btv);
1326
1327	switch (btv->c.type) {
1328	case BTTV_BOARD_VOODOOTV_FM:
1329	case BTTV_BOARD_VOODOOTV_200:
1330		bttv_tda9880_setnorm(btv, gpio_read());
1331		break;
1332	}
1333	id = tvnorm->v4l2_id;
1334	bttv_call_all(btv, core, s_std, id);
1335
1336	return 0;
1337}
1338
1339/* Call with btv->lock down. */
1340static void
1341set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1342{
1343	unsigned long flags;
1344
1345	btv->input = input;
1346	if (irq_iswitch) {
1347		spin_lock_irqsave(&btv->s_lock,flags);
1348		if (btv->curr.frame_irq) {
1349			/* active capture -> delayed input switch */
1350			btv->new_input = input;
1351		} else {
1352			video_mux(btv,input);
1353		}
1354		spin_unlock_irqrestore(&btv->s_lock,flags);
1355	} else {
1356		video_mux(btv,input);
1357	}
1358	audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1359			 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1360	set_tvnorm(btv, norm);
1361}
1362
1363static void init_irqreg(struct bttv *btv)
1364{
1365	/* clear status */
1366	btwrite(0xfffffUL, BT848_INT_STAT);
1367
1368	if (bttv_tvcards[btv->c.type].no_video) {
1369		/* i2c only */
1370		btwrite(BT848_INT_I2CDONE,
1371			BT848_INT_MASK);
1372	} else {
1373		/* full video */
1374		btwrite((btv->triton1)  |
1375			(btv->gpioirq ? BT848_INT_GPINT : 0) |
1376			BT848_INT_SCERR |
1377			(fdsr ? BT848_INT_FDSR : 0) |
1378			BT848_INT_RISCI | BT848_INT_OCERR |
1379			BT848_INT_FMTCHG|BT848_INT_HLOCK|
1380			BT848_INT_I2CDONE,
1381			BT848_INT_MASK);
1382	}
1383}
1384
1385static void init_bt848(struct bttv *btv)
1386{
1387	int val;
1388
1389	if (bttv_tvcards[btv->c.type].no_video) {
1390		/* very basic init only */
1391		init_irqreg(btv);
1392		return;
1393	}
1394
1395	btwrite(0x00, BT848_CAP_CTL);
1396	btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1397	btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1398
1399	/* set planar and packed mode trigger points and         */
1400	/* set rising edge of inverted GPINTR pin as irq trigger */
1401	btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1402		BT848_GPIO_DMA_CTL_PLTP1_16|
1403		BT848_GPIO_DMA_CTL_PLTP23_16|
1404		BT848_GPIO_DMA_CTL_GPINTC|
1405		BT848_GPIO_DMA_CTL_GPINTI,
1406		BT848_GPIO_DMA_CTL);
1407
1408	val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1409	btwrite(val, BT848_E_SCLOOP);
1410	btwrite(val, BT848_O_SCLOOP);
1411
1412	btwrite(0x20, BT848_E_VSCALE_HI);
1413	btwrite(0x20, BT848_O_VSCALE_HI);
1414	btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1415		BT848_ADC);
1416
1417	btwrite(whitecrush_upper, BT848_WC_UP);
1418	btwrite(whitecrush_lower, BT848_WC_DOWN);
1419
1420	if (btv->opt_lumafilter) {
1421		btwrite(0, BT848_E_CONTROL);
1422		btwrite(0, BT848_O_CONTROL);
1423	} else {
1424		btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1425		btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1426	}
1427
1428	bt848_bright(btv,   btv->bright);
1429	bt848_hue(btv,      btv->hue);
1430	bt848_contrast(btv, btv->contrast);
1431	bt848_sat(btv,      btv->saturation);
1432
1433	/* interrupt */
1434	init_irqreg(btv);
1435}
1436
1437static void bttv_reinit_bt848(struct bttv *btv)
1438{
1439	unsigned long flags;
1440
1441	if (bttv_verbose)
1442		printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1443	spin_lock_irqsave(&btv->s_lock,flags);
1444	btv->errors=0;
1445	bttv_set_dma(btv,0);
1446	spin_unlock_irqrestore(&btv->s_lock,flags);
1447
1448	init_bt848(btv);
1449	btv->pll.pll_current = -1;
1450	set_input(btv, btv->input, btv->tvnorm);
1451}
1452
1453static int bttv_g_ctrl(struct file *file, void *priv,
1454					struct v4l2_control *c)
1455{
1456	struct bttv_fh *fh = priv;
1457	struct bttv *btv = fh->btv;
1458
1459	switch (c->id) {
1460	case V4L2_CID_BRIGHTNESS:
1461		c->value = btv->bright;
1462		break;
1463	case V4L2_CID_HUE:
1464		c->value = btv->hue;
1465		break;
1466	case V4L2_CID_CONTRAST:
1467		c->value = btv->contrast;
1468		break;
1469	case V4L2_CID_SATURATION:
1470		c->value = btv->saturation;
1471		break;
1472
1473	case V4L2_CID_AUDIO_MUTE:
1474	case V4L2_CID_AUDIO_VOLUME:
1475	case V4L2_CID_AUDIO_BALANCE:
1476	case V4L2_CID_AUDIO_BASS:
1477	case V4L2_CID_AUDIO_TREBLE:
1478		bttv_call_all(btv, core, g_ctrl, c);
1479		break;
1480
1481	case V4L2_CID_PRIVATE_CHROMA_AGC:
1482		c->value = btv->opt_chroma_agc;
1483		break;
1484	case V4L2_CID_PRIVATE_COMBFILTER:
1485		c->value = btv->opt_combfilter;
1486		break;
1487	case V4L2_CID_PRIVATE_LUMAFILTER:
1488		c->value = btv->opt_lumafilter;
1489		break;
1490	case V4L2_CID_PRIVATE_AUTOMUTE:
1491		c->value = btv->opt_automute;
1492		break;
1493	case V4L2_CID_PRIVATE_AGC_CRUSH:
1494		c->value = btv->opt_adc_crush;
1495		break;
1496	case V4L2_CID_PRIVATE_VCR_HACK:
1497		c->value = btv->opt_vcr_hack;
1498		break;
1499	case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1500		c->value = btv->opt_whitecrush_upper;
1501		break;
1502	case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1503		c->value = btv->opt_whitecrush_lower;
1504		break;
1505	case V4L2_CID_PRIVATE_UV_RATIO:
1506		c->value = btv->opt_uv_ratio;
1507		break;
1508	case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1509		c->value = btv->opt_full_luma_range;
1510		break;
1511	case V4L2_CID_PRIVATE_CORING:
1512		c->value = btv->opt_coring;
1513		break;
1514	default:
1515		return -EINVAL;
1516	}
1517	return 0;
1518}
1519
1520static int bttv_s_ctrl(struct file *file, void *f,
1521					struct v4l2_control *c)
1522{
1523	int err;
1524	int val;
1525	struct bttv_fh *fh = f;
1526	struct bttv *btv = fh->btv;
1527
1528	err = v4l2_prio_check(&btv->prio, fh->prio);
1529	if (0 != err)
1530		return err;
1531
1532	switch (c->id) {
1533	case V4L2_CID_BRIGHTNESS:
1534		bt848_bright(btv, c->value);
1535		break;
1536	case V4L2_CID_HUE:
1537		bt848_hue(btv, c->value);
1538		break;
1539	case V4L2_CID_CONTRAST:
1540		bt848_contrast(btv, c->value);
1541		break;
1542	case V4L2_CID_SATURATION:
1543		bt848_sat(btv, c->value);
1544		break;
1545	case V4L2_CID_AUDIO_MUTE:
1546		audio_mute(btv, c->value);
1547		/* fall through */
1548	case V4L2_CID_AUDIO_VOLUME:
1549		if (btv->volume_gpio)
1550			btv->volume_gpio(btv, c->value);
1551
1552		bttv_call_all(btv, core, s_ctrl, c);
1553		break;
1554	case V4L2_CID_AUDIO_BALANCE:
1555	case V4L2_CID_AUDIO_BASS:
1556	case V4L2_CID_AUDIO_TREBLE:
1557		bttv_call_all(btv, core, s_ctrl, c);
1558		break;
1559
1560	case V4L2_CID_PRIVATE_CHROMA_AGC:
1561		btv->opt_chroma_agc = c->value;
1562		val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1563		btwrite(val, BT848_E_SCLOOP);
1564		btwrite(val, BT848_O_SCLOOP);
1565		break;
1566	case V4L2_CID_PRIVATE_COMBFILTER:
1567		btv->opt_combfilter = c->value;
1568		break;
1569	case V4L2_CID_PRIVATE_LUMAFILTER:
1570		btv->opt_lumafilter = c->value;
1571		if (btv->opt_lumafilter) {
1572			btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1573			btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1574		} else {
1575			btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1576			btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1577		}
1578		break;
1579	case V4L2_CID_PRIVATE_AUTOMUTE:
1580		btv->opt_automute = c->value;
1581		break;
1582	case V4L2_CID_PRIVATE_AGC_CRUSH:
1583		btv->opt_adc_crush = c->value;
1584		btwrite(BT848_ADC_RESERVED |
1585				(btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1586				BT848_ADC);
1587		break;
1588	case V4L2_CID_PRIVATE_VCR_HACK:
1589		btv->opt_vcr_hack = c->value;
1590		break;
1591	case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1592		btv->opt_whitecrush_upper = c->value;
1593		btwrite(c->value, BT848_WC_UP);
1594		break;
1595	case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1596		btv->opt_whitecrush_lower = c->value;
1597		btwrite(c->value, BT848_WC_DOWN);
1598		break;
1599	case V4L2_CID_PRIVATE_UV_RATIO:
1600		btv->opt_uv_ratio = c->value;
1601		bt848_sat(btv, btv->saturation);
1602		break;
1603	case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1604		btv->opt_full_luma_range = c->value;
1605		btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1606		break;
1607	case V4L2_CID_PRIVATE_CORING:
1608		btv->opt_coring = c->value;
1609		btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1610		break;
1611	default:
1612		return -EINVAL;
1613	}
1614	return 0;
1615}
1616
1617/* ----------------------------------------------------------------------- */
1618
1619void bttv_gpio_tracking(struct bttv *btv, char *comment)
1620{
1621	unsigned int outbits, data;
1622	outbits = btread(BT848_GPIO_OUT_EN);
1623	data    = btread(BT848_GPIO_DATA);
1624	printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1625	       btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1626}
1627
1628static void bttv_field_count(struct bttv *btv)
1629{
1630	int need_count = 0;
1631
1632	if (btv->users)
1633		need_count++;
1634
1635	if (need_count) {
1636		/* start field counter */
1637		btor(BT848_INT_VSYNC,BT848_INT_MASK);
1638	} else {
1639		/* stop field counter */
1640		btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1641		btv->field_count = 0;
1642	}
1643}
1644
1645static const struct bttv_format*
1646format_by_fourcc(int fourcc)
1647{
1648	unsigned int i;
1649
1650	for (i = 0; i < FORMATS; i++) {
1651		if (-1 == formats[i].fourcc)
1652			continue;
1653		if (formats[i].fourcc == fourcc)
1654			return formats+i;
1655	}
1656	return NULL;
1657}
1658
1659/* ----------------------------------------------------------------------- */
1660/* misc helpers                                                            */
1661
1662static int
1663bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1664		    struct bttv_buffer *new)
1665{
1666	struct bttv_buffer *old;
1667	unsigned long flags;
1668	int retval = 0;
1669
1670	dprintk("switch_overlay: enter [new=%p]\n",new);
1671	if (new)
1672		new->vb.state = VIDEOBUF_DONE;
1673	spin_lock_irqsave(&btv->s_lock,flags);
1674	old = btv->screen;
1675	btv->screen = new;
1676	btv->loop_irq |= 1;
1677	bttv_set_dma(btv, 0x03);
1678	spin_unlock_irqrestore(&btv->s_lock,flags);
1679	if (NULL != old) {
1680		dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1681		bttv_dma_free(&fh->cap,btv, old);
1682		kfree(old);
1683	}
1684	if (NULL == new)
1685		free_btres(btv,fh,RESOURCE_OVERLAY);
1686	dprintk("switch_overlay: done\n");
1687	return retval;
1688}
1689
1690/* ----------------------------------------------------------------------- */
1691/* video4linux (1) interface                                               */
1692
1693static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1694			       struct bttv_buffer *buf,
1695			       const struct bttv_format *fmt,
1696			       unsigned int width, unsigned int height,
1697			       enum v4l2_field field)
1698{
1699	struct bttv_fh *fh = q->priv_data;
1700	int redo_dma_risc = 0;
1701	struct bttv_crop c;
1702	int norm;
1703	int rc;
1704
1705	/* check settings */
1706	if (NULL == fmt)
1707		return -EINVAL;
1708	if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1709		width  = RAW_BPL;
1710		height = RAW_LINES*2;
1711		if (width*height > buf->vb.bsize)
1712			return -EINVAL;
1713		buf->vb.size = buf->vb.bsize;
1714
1715		/* Make sure tvnorm and vbi_end remain consistent
1716		   until we're done. */
1717		mutex_lock(&btv->lock);
1718
1719		norm = btv->tvnorm;
1720
1721		/* In this mode capturing always starts at defrect.top
1722		   (default VDELAY), ignoring cropping parameters. */
1723		if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1724			mutex_unlock(&btv->lock);
1725			return -EINVAL;
1726		}
1727
1728		mutex_unlock(&btv->lock);
1729
1730		c.rect = bttv_tvnorms[norm].cropcap.defrect;
1731	} else {
1732		mutex_lock(&btv->lock);
1733
1734		norm = btv->tvnorm;
1735		c = btv->crop[!!fh->do_crop];
1736
1737		mutex_unlock(&btv->lock);
1738
1739		if (width < c.min_scaled_width ||
1740		    width > c.max_scaled_width ||
1741		    height < c.min_scaled_height)
1742			return -EINVAL;
1743
1744		switch (field) {
1745		case V4L2_FIELD_TOP:
1746		case V4L2_FIELD_BOTTOM:
1747		case V4L2_FIELD_ALTERNATE:
1748			/* btv->crop counts frame lines. Max. scale
1749			   factor is 16:1 for frames, 8:1 for fields. */
1750			if (height * 2 > c.max_scaled_height)
1751				return -EINVAL;
1752			break;
1753
1754		default:
1755			if (height > c.max_scaled_height)
1756				return -EINVAL;
1757			break;
1758		}
1759
1760		buf->vb.size = (width * height * fmt->depth) >> 3;
1761		if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
1762			return -EINVAL;
1763	}
1764
1765	/* alloc + fill struct bttv_buffer (if changed) */
1766	if (buf->vb.width != width || buf->vb.height != height ||
1767	    buf->vb.field != field ||
1768	    buf->tvnorm != norm || buf->fmt != fmt ||
1769	    buf->crop.top != c.rect.top ||
1770	    buf->crop.left != c.rect.left ||
1771	    buf->crop.width != c.rect.width ||
1772	    buf->crop.height != c.rect.height) {
1773		buf->vb.width  = width;
1774		buf->vb.height = height;
1775		buf->vb.field  = field;
1776		buf->tvnorm    = norm;
1777		buf->fmt       = fmt;
1778		buf->crop      = c.rect;
1779		redo_dma_risc = 1;
1780	}
1781
1782	/* alloc risc memory */
1783	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1784		redo_dma_risc = 1;
1785		if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1786			goto fail;
1787	}
1788
1789	if (redo_dma_risc)
1790		if (0 != (rc = bttv_buffer_risc(btv,buf)))
1791			goto fail;
1792
1793	buf->vb.state = VIDEOBUF_PREPARED;
1794

Large files files are truncated, but you can click here to view the full file