bttv-driver.c 115.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*

    bttv - Bt848 frame grabber driver

    Copyright (C) 1996,97,98 Ralph  Metzler <rjkm@thp.uni-koeln.de>
6
			   & Marcus Metzler <mocm@thp.uni-koeln.de>
L
Linus Torvalds 已提交
7 8 9 10 11
    (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>

    some v4l2 code lines are taken from Justin's bttv2 driver which is
    (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>

12 13 14
    V4L1 removal from:
    (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>

15
    Fixes to be fully V4L2 compliant by
16
    (c) 2006 Mauro Carvalho Chehab <mchehab@kernel.org>
17

18 19 20 21
    Cropping and overscan support
    Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
    Sponsored by OPQ Systems AB

L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    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.
*/

37 38
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
39 40 41
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
42
#include <linux/slab.h>
L
Linus Torvalds 已提交
43 44 45 46 47 48
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/kdev_t.h>
49
#include "bttvp.h"
50
#include <media/v4l2-common.h>
51
#include <media/v4l2-ioctl.h>
52
#include <media/v4l2-event.h>
53
#include <media/i2c/tvaudio.h>
54
#include <media/drv-intf/msp3400.h>
55

M
Mauro Carvalho Chehab 已提交
56
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
57 58 59 60

#include <asm/io.h>
#include <asm/byteorder.h>

61
#include <media/i2c/saa6588.h>
62

63
#define BTTV_VERSION "0.9.19"
64

L
Linus Torvalds 已提交
65
unsigned int bttv_num;			/* number of Bt848s in use */
66
struct bttv *bttvs[BTTV_MAX];
L
Linus Torvalds 已提交
67

68
unsigned int bttv_debug;
L
Linus Torvalds 已提交
69
unsigned int bttv_verbose = 1;
70
unsigned int bttv_gpio;
L
Linus Torvalds 已提交
71 72 73 74 75

/* config variables */
#ifdef __BIG_ENDIAN
static unsigned int bigendian=1;
#else
76
static unsigned int bigendian;
L
Linus Torvalds 已提交
77 78
#endif
static unsigned int radio[BTTV_MAX];
79
static unsigned int irq_debug;
L
Linus Torvalds 已提交
80 81
static unsigned int gbuffers = 8;
static unsigned int gbufsize = 0x208000;
82
static unsigned int reset_crop = 1;
L
Linus Torvalds 已提交
83

84 85 86
static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
87
static int debug_latency;
88
static int disable_ir;
L
Linus Torvalds 已提交
89

90
static unsigned int fdsr;
L
Linus Torvalds 已提交
91 92

/* options */
93 94
static unsigned int combfilter;
static unsigned int lumafilter;
L
Linus Torvalds 已提交
95
static unsigned int automute    = 1;
96
static unsigned int chroma_agc;
97
static unsigned int agc_crush   = 1;
L
Linus Torvalds 已提交
98 99
static unsigned int whitecrush_upper = 0xCF;
static unsigned int whitecrush_lower = 0x7F;
100 101
static unsigned int vcr_hack;
static unsigned int irq_iswitch;
102
static unsigned int uv_ratio    = 50;
103 104
static unsigned int full_luma_range;
static unsigned int coring;
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114

/* API features (turn on/off stuff for testing) */
static unsigned int v4l2        = 1;

/* insmod args */
module_param(bttv_verbose,      int, 0644);
module_param(bttv_gpio,         int, 0644);
module_param(bttv_debug,        int, 0644);
module_param(irq_debug,         int, 0644);
module_param(debug_latency,     int, 0644);
115
module_param(disable_ir,        int, 0444);
L
Linus Torvalds 已提交
116 117 118 119

module_param(fdsr,              int, 0444);
module_param(gbuffers,          int, 0444);
module_param(gbufsize,          int, 0444);
120
module_param(reset_crop,        int, 0444);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128

module_param(v4l2,              int, 0644);
module_param(bigendian,         int, 0644);
module_param(irq_iswitch,       int, 0644);
module_param(combfilter,        int, 0444);
module_param(lumafilter,        int, 0444);
module_param(automute,          int, 0444);
module_param(chroma_agc,        int, 0444);
129
module_param(agc_crush,         int, 0444);
L
Linus Torvalds 已提交
130 131 132
module_param(whitecrush_upper,  int, 0444);
module_param(whitecrush_lower,  int, 0444);
module_param(vcr_hack,          int, 0444);
133 134 135
module_param(uv_ratio,          int, 0444);
module_param(full_luma_range,   int, 0444);
module_param(coring,            int, 0444);
L
Linus Torvalds 已提交
136

137 138 139 140
module_param_array(radio,       int, NULL, 0444);
module_param_array(video_nr,    int, NULL, 0444);
module_param_array(radio_nr,    int, NULL, 0444);
module_param_array(vbi_nr,      int, NULL, 0444);
L
Linus Torvalds 已提交
141

142 143 144 145 146 147
MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
148
MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
149 150
MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
151
MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
152 153 154 155 156 157 158 159 160 161
MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
162 163 164
MODULE_PARM_DESC(video_nr, "video device numbers");
MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
MODULE_PARM_DESC(radio_nr, "radio device numbers");
L
Linus Torvalds 已提交
165 166 167 168

MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
MODULE_LICENSE("GPL");
169
MODULE_VERSION(BTTV_VERSION);
L
Linus Torvalds 已提交
170

171 172 173 174 175 176 177 178 179 180 181
#define V4L2_CID_PRIVATE_COMBFILTER		(V4L2_CID_USER_BTTV_BASE + 0)
#define V4L2_CID_PRIVATE_AUTOMUTE		(V4L2_CID_USER_BTTV_BASE + 1)
#define V4L2_CID_PRIVATE_LUMAFILTER		(V4L2_CID_USER_BTTV_BASE + 2)
#define V4L2_CID_PRIVATE_AGC_CRUSH		(V4L2_CID_USER_BTTV_BASE + 3)
#define V4L2_CID_PRIVATE_VCR_HACK		(V4L2_CID_USER_BTTV_BASE + 4)
#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER	(V4L2_CID_USER_BTTV_BASE + 5)
#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER	(V4L2_CID_USER_BTTV_BASE + 6)
#define V4L2_CID_PRIVATE_UV_RATIO		(V4L2_CID_USER_BTTV_BASE + 7)
#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE	(V4L2_CID_USER_BTTV_BASE + 8)
#define V4L2_CID_PRIVATE_CORING			(V4L2_CID_USER_BTTV_BASE + 9)

L
Linus Torvalds 已提交
182 183 184
/* ----------------------------------------------------------------------- */
/* sysfs                                                                   */

185 186
static ssize_t show_card(struct device *cd,
			 struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
187
{
188
	struct video_device *vfd = to_video_device(cd);
189
	struct bttv *btv = video_get_drvdata(vfd);
L
Linus Torvalds 已提交
190 191
	return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
}
192
static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
L
Linus Torvalds 已提交
193

194 195 196 197 198 199 200 201 202 203 204 205 206
/* ----------------------------------------------------------------------- */
/* dvb auto-load setup                                                     */
#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
	request_module("dvb-bt8xx");
}

static void request_modules(struct bttv *dev)
{
	INIT_WORK(&dev->request_module_wk, request_module_async);
	schedule_work(&dev->request_module_wk);
}
207 208 209

static void flush_request_modules(struct bttv *dev)
{
210
	flush_work(&dev->request_module_wk);
211
}
212 213
#else
#define request_modules(dev)
214
#define flush_request_modules(dev) do {} while(0)
215 216 217
#endif /* CONFIG_MODULES */


L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/* ----------------------------------------------------------------------- */
/* static data                                                             */

/* special timing tables from conexant... */
static u8 SRAM_Table[][60] =
{
	/* PAL digital input over GPIO[7:0] */
	{
		45, // 45 bytes following
		0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
		0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
		0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
		0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
		0x37,0x00,0xAF,0x21,0x00
	},
	/* NTSC digital input over GPIO[7:0] */
	{
		51, // 51 bytes following
		0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
		0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
		0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
		0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
		0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
		0x00,
	},
	// TGB_NTSC392 // quartzsight
	// This table has been modified to be used for Fusion Rev D
	{
		0x2A, // size of table = 42
		0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
		0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
		0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
		0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
		0x20, 0x00
	}
};

255 256 257 258 259 260 261 262 263
/* minhdelayx1	first video pixel we can capture on a line and
   hdelayx1	start of active video, both relative to rising edge of
		/HRESET pulse (0H) in 1 / fCLKx1.
   swidth	width of active video and
   totalwidth	total line width, both in 1 / fCLKx1.
   sqwidth	total line width in square pixels.
   vdelay	start of active video in 2 * field lines relative to
		trailing edge of /VRESET pulse (VDELAY register).
   sheight	height of active video in 2 * field lines.
264
   extraheight	Added to sheight for cropcap.bounds.height only
265 266 267
   videostart0	ITU-R frame line number of the line corresponding
		to vdelay in the first field. */
#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth,	 \
268
		vdelay, sheight, extraheight, videostart0)		 \
269 270 271 272 273 274
	.cropcap.bounds.left = minhdelayx1,				 \
	/* * 2 because vertically we count field lines times two, */	 \
	/* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */		 \
	.cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
	/* 4 is a safety margin at the end of the line. */		 \
	.cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4,	 \
275 276
	.cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) -	 \
				 MIN_VDELAY,				 \
277 278 279 280 281 282 283
	.cropcap.defrect.left = hdelayx1,				 \
	.cropcap.defrect.top = (videostart0) * 2,			 \
	.cropcap.defrect.width = swidth,				 \
	.cropcap.defrect.height = sheight,				 \
	.cropcap.pixelaspect.numerator = totalwidth,			 \
	.cropcap.pixelaspect.denominator = sqwidth,

L
Linus Torvalds 已提交
284 285
const struct bttv_tvnorm bttv_tvnorms[] = {
	/* PAL-BDGHI */
286 287
	/* max. active video is actually 922, but 924 is divisible by 4 and 3! */
	/* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301
	{
		.v4l2_id        = V4L2_STD_PAL,
		.name           = "PAL",
		.Fsc            = 35468950,
		.swidth         = 924,
		.sheight        = 576,
		.totalwidth     = 1135,
		.adelay         = 0x7f,
		.bdelay         = 0x72,
		.iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
		.scaledtwidth   = 1135,
		.hdelayx1       = 186,
		.hactivex1      = 924,
		.vdelay         = 0x20,
302
		.vbipack        = 255, /* min (2048 / 4, 0x1ff) & 0xff */
L
Linus Torvalds 已提交
303
		.sram           = 0,
304
		/* ITU-R frame line number of the first VBI line
305 306 307 308 309 310 311 312 313 314 315 316
		   we can capture, of the first and second field.
		   The last line is determined by cropcap.bounds. */
		.vbistart       = { 7, 320 },
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 186,
			/* Should be (768 * 1135 + 944 / 2) / 944.
			   cropcap.defrect is used for image width
			   checks, so we keep the old value 924. */
			/* swidth */ 924,
			/* totalwidth */ 1135,
			/* sqwidth */ 944,
			/* vdelay */ 0x20,
317 318 319 320
			/* sheight */ 576,
			/* bt878 (and bt848?) can capture another
			   line below active video. */
			/* extraheight */ 2,
321
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
322
	},{
323
		.v4l2_id        = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335
		.name           = "NTSC",
		.Fsc            = 28636363,
		.swidth         = 768,
		.sheight        = 480,
		.totalwidth     = 910,
		.adelay         = 0x68,
		.bdelay         = 0x5d,
		.iform          = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
		.scaledtwidth   = 910,
		.hdelayx1       = 128,
		.hactivex1      = 910,
		.vdelay         = 0x1a,
336
		.vbipack        = 144, /* min (1600 / 4, 0x1ff) & 0xff */
L
Linus Torvalds 已提交
337
		.sram           = 1,
338
		.vbistart	= { 10, 273 },
339 340 341 342 343 344 345 346
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 128,
			/* Should be (640 * 910 + 780 / 2) / 780? */
			/* swidth */ 768,
			/* totalwidth */ 910,
			/* sqwidth */ 780,
			/* vdelay */ 0x1a,
			/* sheight */ 480,
347
			/* extraheight */ 0,
348
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	},{
		.v4l2_id        = V4L2_STD_SECAM,
		.name           = "SECAM",
		.Fsc            = 35468950,
		.swidth         = 924,
		.sheight        = 576,
		.totalwidth     = 1135,
		.adelay         = 0x7f,
		.bdelay         = 0xb0,
		.iform          = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
		.scaledtwidth   = 1135,
		.hdelayx1       = 186,
		.hactivex1      = 922,
		.vdelay         = 0x20,
		.vbipack        = 255,
		.sram           = 0, /* like PAL, correct? */
365
		.vbistart	= { 7, 320 },
366 367 368 369 370 371 372
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 186,
			/* swidth */ 924,
			/* totalwidth */ 1135,
			/* sqwidth */ 944,
			/* vdelay */ 0x20,
			/* sheight */ 576,
373
			/* extraheight */ 0,
374
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	},{
		.v4l2_id        = V4L2_STD_PAL_Nc,
		.name           = "PAL-Nc",
		.Fsc            = 28636363,
		.swidth         = 640,
		.sheight        = 576,
		.totalwidth     = 910,
		.adelay         = 0x68,
		.bdelay         = 0x5d,
		.iform          = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
		.scaledtwidth   = 780,
		.hdelayx1       = 130,
		.hactivex1      = 734,
		.vdelay         = 0x1a,
		.vbipack        = 144,
		.sram           = -1,
391
		.vbistart	= { 7, 320 },
392 393 394 395 396 397 398
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 130,
			/* swidth */ (640 * 910 + 780 / 2) / 780,
			/* totalwidth */ 910,
			/* sqwidth */ 780,
			/* vdelay */ 0x1a,
			/* sheight */ 576,
399
			/* extraheight */ 0,
400
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	},{
		.v4l2_id        = V4L2_STD_PAL_M,
		.name           = "PAL-M",
		.Fsc            = 28636363,
		.swidth         = 640,
		.sheight        = 480,
		.totalwidth     = 910,
		.adelay         = 0x68,
		.bdelay         = 0x5d,
		.iform          = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
		.scaledtwidth   = 780,
		.hdelayx1       = 135,
		.hactivex1      = 754,
		.vdelay         = 0x1a,
		.vbipack        = 144,
		.sram           = -1,
417
		.vbistart	= { 10, 273 },
418 419 420 421 422 423 424
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 135,
			/* swidth */ (640 * 910 + 780 / 2) / 780,
			/* totalwidth */ 910,
			/* sqwidth */ 780,
			/* vdelay */ 0x1a,
			/* sheight */ 480,
425
			/* extraheight */ 0,
426
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	},{
		.v4l2_id        = V4L2_STD_PAL_N,
		.name           = "PAL-N",
		.Fsc            = 35468950,
		.swidth         = 768,
		.sheight        = 576,
		.totalwidth     = 1135,
		.adelay         = 0x7f,
		.bdelay         = 0x72,
		.iform          = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
		.scaledtwidth   = 944,
		.hdelayx1       = 186,
		.hactivex1      = 922,
		.vdelay         = 0x20,
		.vbipack        = 144,
		.sram           = -1,
443 444 445 446 447 448 449 450
		.vbistart       = { 7, 320 },
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 186,
			/* swidth */ (768 * 1135 + 944 / 2) / 944,
			/* totalwidth */ 1135,
			/* sqwidth */ 944,
			/* vdelay */ 0x20,
			/* sheight */ 576,
451
			/* extraheight */ 0,
452
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	},{
		.v4l2_id        = V4L2_STD_NTSC_M_JP,
		.name           = "NTSC-JP",
		.Fsc            = 28636363,
		.swidth         = 640,
		.sheight        = 480,
		.totalwidth     = 910,
		.adelay         = 0x68,
		.bdelay         = 0x5d,
		.iform          = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
		.scaledtwidth   = 780,
		.hdelayx1       = 135,
		.hactivex1      = 754,
		.vdelay         = 0x16,
		.vbipack        = 144,
		.sram           = -1,
469 470 471 472 473 474 475 476
		.vbistart       = { 10, 273 },
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 135,
			/* swidth */ (640 * 910 + 780 / 2) / 780,
			/* totalwidth */ 910,
			/* sqwidth */ 780,
			/* vdelay */ 0x16,
			/* sheight */ 480,
477
			/* extraheight */ 0,
478
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	},{
		/* that one hopefully works with the strange timing
		 * which video recorders produce when playing a NTSC
		 * tape on a PAL TV ... */
		.v4l2_id        = V4L2_STD_PAL_60,
		.name           = "PAL-60",
		.Fsc            = 35468950,
		.swidth         = 924,
		.sheight        = 480,
		.totalwidth     = 1135,
		.adelay         = 0x7f,
		.bdelay         = 0x72,
		.iform          = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
		.scaledtwidth   = 1135,
		.hdelayx1       = 186,
		.hactivex1      = 924,
		.vdelay         = 0x1a,
		.vbipack        = 255,
		.vtotal         = 524,
		.sram           = -1,
499
		.vbistart	= { 10, 273 },
500 501 502 503 504 505 506
		CROPCAP(/* minhdelayx1 */ 68,
			/* hdelayx1 */ 186,
			/* swidth */ 924,
			/* totalwidth */ 1135,
			/* sqwidth */ 944,
			/* vdelay */ 0x1a,
			/* sheight */ 480,
507
			/* extraheight */ 0,
508
			/* videostart0 */ 23)
L
Linus Torvalds 已提交
509 510 511 512 513 514 515
	}
};
static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);

/* ----------------------------------------------------------------------- */
/* bttv format list
   packed pixel formats must come first */
516
static const struct bttv_format formats[] = {
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	{
		.name     = "8 bpp, gray",
		.fourcc   = V4L2_PIX_FMT_GREY,
		.btformat = BT848_COLOR_FMT_Y8,
		.depth    = 8,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "8 bpp, dithered color",
		.fourcc   = V4L2_PIX_FMT_HI240,
		.btformat = BT848_COLOR_FMT_RGB8,
		.depth    = 8,
		.flags    = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
	},{
		.name     = "15 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_RGB555,
		.btformat = BT848_COLOR_FMT_RGB15,
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "15 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB555X,
		.btformat = BT848_COLOR_FMT_RGB15,
		.btswap   = 0x03, /* byteswap */
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "16 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_RGB565,
		.btformat = BT848_COLOR_FMT_RGB16,
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "16 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB565X,
		.btformat = BT848_COLOR_FMT_RGB16,
		.btswap   = 0x03, /* byteswap */
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "24 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_BGR24,
		.btformat = BT848_COLOR_FMT_RGB24,
		.depth    = 24,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "32 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_BGR32,
		.btformat = BT848_COLOR_FMT_RGB32,
		.depth    = 32,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "32 bpp RGB, be",
		.fourcc   = V4L2_PIX_FMT_RGB32,
		.btformat = BT848_COLOR_FMT_RGB32,
		.btswap   = 0x0f, /* byte+word swap */
		.depth    = 32,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "4:2:2, packed, YUYV",
		.fourcc   = V4L2_PIX_FMT_YUYV,
		.btformat = BT848_COLOR_FMT_YUY2,
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "4:2:2, packed, UYVY",
		.fourcc   = V4L2_PIX_FMT_UYVY,
		.btformat = BT848_COLOR_FMT_YUY2,
		.btswap   = 0x03, /* byteswap */
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PACKED,
	},{
		.name     = "4:2:2, planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV422P,
		.btformat = BT848_COLOR_FMT_YCrCb422,
		.depth    = 16,
		.flags    = FORMAT_FLAGS_PLANAR,
		.hshift   = 1,
		.vshift   = 0,
	},{
		.name     = "4:2:0, planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV420,
		.btformat = BT848_COLOR_FMT_YCrCb422,
		.depth    = 12,
		.flags    = FORMAT_FLAGS_PLANAR,
		.hshift   = 1,
		.vshift   = 1,
	},{
		.name     = "4:2:0, planar, Y-Cr-Cb",
		.fourcc   = V4L2_PIX_FMT_YVU420,
		.btformat = BT848_COLOR_FMT_YCrCb422,
		.depth    = 12,
		.flags    = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
		.hshift   = 1,
		.vshift   = 1,
	},{
		.name     = "4:1:1, planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV411P,
		.btformat = BT848_COLOR_FMT_YCrCb411,
		.depth    = 12,
		.flags    = FORMAT_FLAGS_PLANAR,
		.hshift   = 2,
		.vshift   = 0,
	},{
		.name     = "4:1:0, planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV410,
		.btformat = BT848_COLOR_FMT_YCrCb411,
		.depth    = 9,
		.flags    = FORMAT_FLAGS_PLANAR,
		.hshift   = 2,
		.vshift   = 2,
	},{
		.name     = "4:1:0, planar, Y-Cr-Cb",
		.fourcc   = V4L2_PIX_FMT_YVU410,
		.btformat = BT848_COLOR_FMT_YCrCb411,
		.depth    = 9,
		.flags    = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
		.hshift   = 2,
		.vshift   = 2,
	},{
		.name     = "raw scanlines",
		.fourcc   = -1,
		.btformat = BT848_COLOR_FMT_RAW,
		.depth    = 8,
		.flags    = FORMAT_FLAGS_RAW,
	}
};
643
static const unsigned int FORMATS = ARRAY_SIZE(formats);
L
Linus Torvalds 已提交
644 645 646 647

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

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
/*
   RESOURCE_    allocated by                freed by

   VIDEO_READ   bttv_read 1)                bttv_read 2)

   VIDEO_STREAM VIDIOC_STREAMON             VIDIOC_STREAMOFF
		 VIDIOC_QBUF 1)              bttv_release
		 VIDIOCMCAPTURE 1)

   OVERLAY	 VIDIOCCAPTURE on            VIDIOCCAPTURE off
		 VIDIOC_OVERLAY on           VIDIOC_OVERLAY off
		 3)                          bttv_release

   VBI		 VIDIOC_STREAMON             VIDIOC_STREAMOFF
		 VIDIOC_QBUF 1)              bttv_release
		 bttv_read, bttv_poll 1) 4)

   1) The resource must be allocated when we enter buffer prepare functions
      and remain allocated while buffers are in the DMA queue.
   2) This is a single frame read.
   3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
      RESOURCE_OVERLAY is allocated.
   4) This is a continuous read, implies VIDIOC_STREAMON.

   Note this driver permits video input and standard changes regardless if
   resources are allocated.
*/

#define VBI_RESOURCES (RESOURCE_VBI)
#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
			 RESOURCE_VIDEO_STREAM | \
			 RESOURCE_OVERLAY)

L
Linus Torvalds 已提交
681
static
682
int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
L
Linus Torvalds 已提交
683
{
684 685
	int xbits; /* mutual exclusive resources */

L
Linus Torvalds 已提交
686 687 688 689
	if (fh->resources & bit)
		/* have it already allocated */
		return 1;

690 691 692 693
	xbits = bit;
	if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
		xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;

L
Linus Torvalds 已提交
694
	/* is it free? */
695
	if (btv->resources & xbits) {
L
Linus Torvalds 已提交
696
		/* no, someone else uses it */
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
		goto fail;
	}

	if ((bit & VIDEO_RESOURCES)
	    && 0 == (btv->resources & VIDEO_RESOURCES)) {
		/* Do crop - use current, don't - use default parameters. */
		__s32 top = btv->crop[!!fh->do_crop].rect.top;

		if (btv->vbi_end > top)
			goto fail;

		/* We cannot capture the same line as video and VBI data.
		   Claim scan lines crop[].rect.top to bottom. */
		btv->crop_start = top;
	} else if (bit & VBI_RESOURCES) {
		__s32 end = fh->vbi_fmt.end;

		if (end > btv->crop_start)
			goto fail;

		/* Claim scan lines above fh->vbi_fmt.end. */
		btv->vbi_end = end;
L
Linus Torvalds 已提交
719
	}
720

L
Linus Torvalds 已提交
721 722 723 724
	/* it's free, grab it */
	fh->resources  |= bit;
	btv->resources |= bit;
	return 1;
725 726 727

 fail:
	return 0;
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741
}

static
int check_btres(struct bttv_fh *fh, int bit)
{
	return (fh->resources & bit);
}

static
int locked_btres(struct bttv *btv, int bit)
{
	return (btv->resources & bit);
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
/* Call with btv->lock down. */
static void
disclaim_vbi_lines(struct bttv *btv)
{
	btv->vbi_end = 0;
}

/* Call with btv->lock down. */
static void
disclaim_video_lines(struct bttv *btv)
{
	const struct bttv_tvnorm *tvnorm;
	u8 crop;

	tvnorm = &bttv_tvnorms[btv->tvnorm];
	btv->crop_start = tvnorm->cropcap.bounds.top
		+ tvnorm->cropcap.bounds.height;

	/* VBI capturing ends at VDELAY, start of video capturing, no
	   matter how many lines the VBI RISC program expects. When video
	   capturing is off, it shall no longer "preempt" VBI capturing,
	   so we set VDELAY to maximum. */
	crop = btread(BT848_E_CROP) | 0xc0;
	btwrite(crop, BT848_E_CROP);
	btwrite(0xfe, BT848_E_VDELAY_LO);
	btwrite(crop, BT848_O_CROP);
	btwrite(0xfe, BT848_O_VDELAY_LO);
}

L
Linus Torvalds 已提交
771
static
772
void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
L
Linus Torvalds 已提交
773 774
{
	if ((fh->resources & bits) != bits) {
775 776
		/* trying to free resources not allocated by us ... */
		pr_err("BUG! (btres)\n");
L
Linus Torvalds 已提交
777 778 779
	}
	fh->resources  &= ~bits;
	btv->resources &= ~bits;
780 781 782 783 784 785 786 787

	bits = btv->resources;

	if (0 == (bits & VIDEO_RESOURCES))
		disclaim_video_lines(btv);

	if (0 == (bits & VBI_RESOURCES))
		disclaim_vbi_lines(btv);
L
Linus Torvalds 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
}

/* ----------------------------------------------------------------------- */
/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC          */

/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
   PLL_X = Reference pre-divider (0=1, 1=2)
   PLL_C = Post divider (0=6, 1=4)
   PLL_I = Integer input
   PLL_F = Fractional input

   F_input = 28.636363 MHz:
   PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
*/

static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
{
805
	unsigned char fl, fh, fi;
L
Linus Torvalds 已提交
806

807 808 809
	/* prevent overflows */
	fin/=4;
	fout/=4;
L
Linus Torvalds 已提交
810

811 812
	fout*=12;
	fi=fout/fin;
L
Linus Torvalds 已提交
813

814 815
	fout=(fout%fin)*256;
	fh=fout/fin;
L
Linus Torvalds 已提交
816

817 818
	fout=(fout%fin)*256;
	fl=fout/fin;
L
Linus Torvalds 已提交
819

820 821 822
	btwrite(fl, BT848_PLL_F_LO);
	btwrite(fh, BT848_PLL_F_HI);
	btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
L
Linus Torvalds 已提交
823 824 825 826
}

static void set_pll(struct bttv *btv)
{
827
	int i;
L
Linus Torvalds 已提交
828

829 830
	if (!btv->pll.pll_crystal)
		return;
L
Linus Torvalds 已提交
831 832

	if (btv->pll.pll_ofreq == btv->pll.pll_current) {
833
		dprintk("%d: PLL: no change required\n", btv->c.nr);
834 835
		return;
	}
L
Linus Torvalds 已提交
836

837 838 839
	if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
		/* no PLL needed */
		if (btv->pll.pll_current == 0)
840
			return;
841 842 843
		if (bttv_verbose)
			pr_info("%d: PLL can sleep, using XTAL (%d)\n",
				btv->c.nr, btv->pll.pll_ifreq);
844 845 846 847 848
		btwrite(0x00,BT848_TGCTRL);
		btwrite(0x00,BT848_PLL_XCI);
		btv->pll.pll_current = 0;
		return;
	}
L
Linus Torvalds 已提交
849

850 851 852 853
	if (bttv_verbose)
		pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
			btv->c.nr,
			btv->pll.pll_ifreq, btv->pll.pll_ofreq);
L
Linus Torvalds 已提交
854 855
	set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);

856
	for (i=0; i<10; i++) {
L
Linus Torvalds 已提交
857 858 859
		/*  Let other people run while the PLL stabilizes */
		msleep(10);

860
		if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
L
Linus Torvalds 已提交
861
			btwrite(0,BT848_DSTATUS);
862
		} else {
863 864
			btwrite(0x08,BT848_TGCTRL);
			btv->pll.pll_current = btv->pll.pll_ofreq;
865 866
			if (bttv_verbose)
				pr_info("PLL set ok\n");
867
			return;
868 869 870
		}
	}
	btv->pll.pll_current = -1;
871 872
	if (bttv_verbose)
		pr_info("Setting PLL failed\n");
873
	return;
L
Linus Torvalds 已提交
874 875 876 877 878 879 880 881 882
}

/* used to switch between the bt848's analog/digital video capture modes */
static void bt848A_set_timing(struct bttv *btv)
{
	int i, len;
	int table_idx = bttv_tvnorms[btv->tvnorm].sram;
	int fsc       = bttv_tvnorms[btv->tvnorm].Fsc;

883
	if (btv->input == btv->dig) {
884
		dprintk("%d: load digital timing table (table_idx=%d)\n",
L
Linus Torvalds 已提交
885 886 887
			btv->c.nr,table_idx);

		/* timing change...reset timing generator address */
888 889 890
		btwrite(0x00, BT848_TGCTRL);
		btwrite(0x02, BT848_TGCTRL);
		btwrite(0x00, BT848_TGCTRL);
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912

		len=SRAM_Table[table_idx][0];
		for(i = 1; i <= len; i++)
			btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
		btv->pll.pll_ofreq = 27000000;

		set_pll(btv);
		btwrite(0x11, BT848_TGCTRL);
		btwrite(0x41, BT848_DVSIF);
	} else {
		btv->pll.pll_ofreq = fsc;
		set_pll(btv);
		btwrite(0x0, BT848_DVSIF);
	}
}

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

static void bt848_bright(struct bttv *btv, int bright)
{
	int value;

913
	// printk("set bright: %d\n", bright); // DEBUG
L
Linus Torvalds 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	btv->bright = bright;

	/* We want -128 to 127 we get 0-65535 */
	value = (bright >> 8) - 128;
	btwrite(value & 0xff, BT848_BRIGHT);
}

static void bt848_hue(struct bttv *btv, int hue)
{
	int value;

	btv->hue = hue;

	/* -128 to 127 */
	value = (hue >> 8) - 128;
929
	btwrite(value & 0xff, BT848_HUE);
L
Linus Torvalds 已提交
930 931 932 933 934 935 936 937 938 939 940
}

static void bt848_contrast(struct bttv *btv, int cont)
{
	int value,hibit;

	btv->contrast = cont;

	/* 0-511 */
	value = (cont  >> 7);
	hibit = (value >> 6) & 4;
941 942 943
	btwrite(value & 0xff, BT848_CONTRAST_LO);
	btaor(hibit, ~4, BT848_E_CONTROL);
	btaor(hibit, ~4, BT848_O_CONTROL);
L
Linus Torvalds 已提交
944 945 946 947 948 949 950 951 952
}

static void bt848_sat(struct bttv *btv, int color)
{
	int val_u,val_v,hibits;

	btv->saturation = color;

	/* 0-511 for the color */
953 954
	val_u   = ((color * btv->opt_uv_ratio) / 50) >> 7;
	val_v   = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
955
	hibits  = (val_u >> 7) & 2;
L
Linus Torvalds 已提交
956
	hibits |= (val_v >> 8) & 1;
957 958 959 960
	btwrite(val_u & 0xff, BT848_SAT_U_LO);
	btwrite(val_v & 0xff, BT848_SAT_V_LO);
	btaor(hibits, ~3, BT848_E_CONTROL);
	btaor(hibits, ~3, BT848_O_CONTROL);
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968 969 970 971 972
}

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

static int
video_mux(struct bttv *btv, unsigned int input)
{
	int mux,mask2;

	if (input >= bttv_tvcards[btv->c.type].video_inputs)
		return -EINVAL;

973
	/* needed by RemoteVideo MX */
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982 983 984
	mask2 = bttv_tvcards[btv->c.type].gpiomask2;
	if (mask2)
		gpio_inout(mask2,mask2);

	if (input == btv->svhs)  {
		btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
		btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
	} else {
		btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
		btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
	}
985
	mux = bttv_muxsel(btv, input);
L
Linus Torvalds 已提交
986
	btaor(mux<<5, ~(3<<5), BT848_IFORM);
987
	dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995 996

	/* card specific hook */
	if(bttv_tvcards[btv->c.type].muxsel_hook)
		bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
	return 0;
}

static char *audio_modes[] = {
	"audio: tuner", "audio: radio", "audio: extern",
997
	"audio: intern", "audio: mute"
L
Linus Torvalds 已提交
998 999
};

1000 1001
static void
audio_mux_gpio(struct bttv *btv, int input, int mute)
L
Linus Torvalds 已提交
1002
{
1003
	int gpio_val, signal, mute_gpio;
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008

	gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
		   bttv_tvcards[btv->c.type].gpiomask);
	signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;

1009
	/* automute */
1010
	mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
1011
				&& !btv->has_radio_tuner);
1012

1013
	if (mute_gpio)
1014 1015 1016 1017
		gpio_val = bttv_tvcards[btv->c.type].gpiomute;
	else
		gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	switch (btv->c.type) {
	case BTTV_BOARD_VOODOOTV_FM:
	case BTTV_BOARD_VOODOOTV_200:
		gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
		break;

	default:
		gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
	}

L
Linus Torvalds 已提交
1028
	if (bttv_gpio)
1029
		bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
1030 1031 1032
}

static int
1033
audio_mute(struct bttv *btv, int mute)
1034 1035 1036
{
	struct v4l2_ctrl *ctrl;

1037
	audio_mux_gpio(btv, btv->audio_input, mute);
1038

1039
	if (btv->sd_msp34xx) {
1040 1041
		ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
		if (ctrl)
1042
			v4l2_ctrl_s_ctrl(ctrl, mute);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	}
	if (btv->sd_tvaudio) {
		ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
		if (ctrl)
			v4l2_ctrl_s_ctrl(ctrl, mute);
	}
	if (btv->sd_tda7432) {
		ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
		if (ctrl)
			v4l2_ctrl_s_ctrl(ctrl, mute);
	}
	return 0;
}

static int
audio_input(struct bttv *btv, int input)
{
	audio_mux_gpio(btv, input, btv->mute);

	if (btv->sd_msp34xx) {
		u32 in;
1064

1065 1066 1067 1068 1069 1070 1071
		/* Note: the inputs tuner/radio/extern/intern are translated
		   to msp routings. This assumes common behavior for all msp3400
		   based TV cards. When this assumption fails, then the
		   specific MSP routing must be added to the card table.
		   For now this is sufficient. */
		switch (input) {
		case TVAUDIO_INPUT_RADIO:
1072 1073 1074 1075 1076
			/* Some boards need the msp do to the radio demod */
			if (btv->radio_uses_msp_demodulator) {
				in = MSP_INPUT_DEFAULT;
				break;
			}
1077
			in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1078
				    MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1079 1080
			break;
		case TVAUDIO_INPUT_EXTERN:
1081
			in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1082
				    MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1083 1084 1085 1086 1087 1088 1089
			break;
		case TVAUDIO_INPUT_INTERN:
			/* Yes, this is the same input as for RADIO. I doubt
			   if this is ever used. The only board with an INTERN
			   input is the BTTV_BOARD_AVERMEDIA98. I wonder how
			   that was tested. My guess is that the whole INTERN
			   input does not work. */
1090
			in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1091
				    MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1092 1093 1094
			break;
		case TVAUDIO_INPUT_TUNER:
		default:
1095 1096 1097 1098
			/* This is the only card that uses TUNER2, and afaik,
			   is the only difference between the VOODOOTV_FM
			   and VOODOOTV_200 */
			if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1099
				in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1100 1101
					MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
			else
1102
				in = MSP_INPUT_DEFAULT;
1103 1104
			break;
		}
1105 1106
		v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
			       in, MSP_OUTPUT_DEFAULT, 0);
1107
	}
1108
	if (btv->sd_tvaudio) {
1109
		v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1110
				 input, 0, 0);
1111
	}
L
Linus Torvalds 已提交
1112 1113 1114
	return 0;
}

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
static void
bttv_crop_calc_limits(struct bttv_crop *c)
{
	/* Scale factor min. 1:1, max. 16:1. Min. image size
	   48 x 32. Scaled width must be a multiple of 4. */

	if (1) {
		/* For bug compatibility with VIDIOCGCAP and image
		   size checks in earlier driver versions. */
		c->min_scaled_width = 48;
		c->min_scaled_height = 32;
	} else {
		c->min_scaled_width =
1128
			(max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
1129
		c->min_scaled_height =
1130
			max_t(unsigned int, 32, c->rect.height >> 4);
1131 1132 1133 1134 1135 1136 1137
	}

	c->max_scaled_width  = c->rect.width & ~3;
	c->max_scaled_height = c->rect.height;
}

static void
1138
bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1139 1140 1141 1142 1143 1144
{
	c->rect = bttv_tvnorms[norm].cropcap.defrect;
	bttv_crop_calc_limits(c);
}

/* Call with btv->lock down. */
L
Linus Torvalds 已提交
1145 1146 1147 1148
static int
set_tvnorm(struct bttv *btv, unsigned int norm)
{
	const struct bttv_tvnorm *tvnorm;
1149
	v4l2_std_id id;
L
Linus Torvalds 已提交
1150

1151 1152
	BUG_ON(norm >= BTTV_TVNORMS);
	BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
L
Linus Torvalds 已提交
1153 1154 1155

	tvnorm = &bttv_tvnorms[norm];

1156
	if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1157
		    sizeof (tvnorm->cropcap))) {
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
		bttv_crop_reset(&btv->crop[0], norm);
		btv->crop[1] = btv->crop[0]; /* current = default */

		if (0 == (btv->resources & VIDEO_RESOURCES)) {
			btv->crop_start = tvnorm->cropcap.bounds.top
				+ tvnorm->cropcap.bounds.height;
		}
	}

	btv->tvnorm = norm;

L
Linus Torvalds 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177
	btwrite(tvnorm->adelay, BT848_ADELAY);
	btwrite(tvnorm->bdelay, BT848_BDELAY);
	btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
	      BT848_IFORM);
	btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
	btwrite(1, BT848_VBI_PACK_DEL);
	bt848A_set_timing(btv);

	switch (btv->c.type) {
1178
	case BTTV_BOARD_VOODOOTV_FM:
1179
	case BTTV_BOARD_VOODOOTV_200:
1180
		bttv_tda9880_setnorm(btv, gpio_read());
L
Linus Torvalds 已提交
1181 1182
		break;
	}
1183
	id = tvnorm->v4l2_id;
1184
	bttv_call_all(btv, video, s_std, id);
1185

L
Linus Torvalds 已提交
1186 1187 1188
	return 0;
}

1189
/* Call with btv->lock down. */
L
Linus Torvalds 已提交
1190
static void
1191
set_input(struct bttv *btv, unsigned int input, unsigned int norm)
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
{
	unsigned long flags;

	btv->input = input;
	if (irq_iswitch) {
		spin_lock_irqsave(&btv->s_lock,flags);
		if (btv->curr.frame_irq) {
			/* active capture -> delayed input switch */
			btv->new_input = input;
		} else {
			video_mux(btv,input);
		}
		spin_unlock_irqrestore(&btv->s_lock,flags);
	} else {
		video_mux(btv,input);
	}
1208 1209 1210
	btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
				TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
	audio_input(btv, btv->audio_input);
1211
	set_tvnorm(btv, norm);
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
}

static void init_irqreg(struct bttv *btv)
{
	/* clear status */
	btwrite(0xfffffUL, BT848_INT_STAT);

	if (bttv_tvcards[btv->c.type].no_video) {
		/* i2c only */
		btwrite(BT848_INT_I2CDONE,
			BT848_INT_MASK);
	} else {
		/* full video */
		btwrite((btv->triton1)  |
			(btv->gpioirq ? BT848_INT_GPINT : 0) |
			BT848_INT_SCERR |
			(fdsr ? BT848_INT_FDSR : 0) |
1229
			BT848_INT_RISCI | BT848_INT_OCERR |
L
Linus Torvalds 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
			BT848_INT_FMTCHG|BT848_INT_HLOCK|
			BT848_INT_I2CDONE,
			BT848_INT_MASK);
	}
}

static void init_bt848(struct bttv *btv)
{
	if (bttv_tvcards[btv->c.type].no_video) {
		/* very basic init only */
		init_irqreg(btv);
		return;
	}

	btwrite(0x00, BT848_CAP_CTL);
	btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
	btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);

1248 1249 1250 1251 1252 1253 1254 1255
	/* set planar and packed mode trigger points and         */
	/* set rising edge of inverted GPINTR pin as irq trigger */
	btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
		BT848_GPIO_DMA_CTL_PLTP1_16|
		BT848_GPIO_DMA_CTL_PLTP23_16|
		BT848_GPIO_DMA_CTL_GPINTC|
		BT848_GPIO_DMA_CTL_GPINTI,
		BT848_GPIO_DMA_CTL);
L
Linus Torvalds 已提交
1256

1257 1258
	btwrite(0x20, BT848_E_VSCALE_HI);
	btwrite(0x20, BT848_O_VSCALE_HI);
L
Linus Torvalds 已提交
1259

1260
	v4l2_ctrl_handler_setup(&btv->ctrl_handler);
L
Linus Torvalds 已提交
1261

1262
	/* interrupt */
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270
	init_irqreg(btv);
}

static void bttv_reinit_bt848(struct bttv *btv)
{
	unsigned long flags;

	if (bttv_verbose)
1271
		pr_info("%d: reset, reinitialize\n", btv->c.nr);
L
Linus Torvalds 已提交
1272 1273 1274 1275 1276 1277
	spin_lock_irqsave(&btv->s_lock,flags);
	btv->errors=0;
	bttv_set_dma(btv,0);
	spin_unlock_irqrestore(&btv->s_lock,flags);

	init_bt848(btv);
1278
	btv->pll.pll_current = -1;
1279
	set_input(btv, btv->input, btv->tvnorm);
L
Linus Torvalds 已提交
1280 1281
}

1282
static int bttv_s_ctrl(struct v4l2_ctrl *c)
1283
{
1284 1285
	struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
	int val;
1286 1287 1288

	switch (c->id) {
	case V4L2_CID_BRIGHTNESS:
1289
		bt848_bright(btv, c->val);
1290 1291
		break;
	case V4L2_CID_HUE:
1292
		bt848_hue(btv, c->val);
1293 1294
		break;
	case V4L2_CID_CONTRAST:
1295
		bt848_contrast(btv, c->val);
1296 1297
		break;
	case V4L2_CID_SATURATION:
1298
		bt848_sat(btv, c->val);
1299
		break;
1300
	case V4L2_CID_COLOR_KILLER:
1301
		if (c->val) {
1302 1303 1304 1305 1306 1307 1308
			btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
			btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
		} else {
			btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
			btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
		}
		break;
1309
	case V4L2_CID_AUDIO_MUTE:
1310
		audio_mute(btv, c->val);
1311
		btv->mute = c->val;
1312
		break;
1313 1314
	case V4L2_CID_AUDIO_VOLUME:
		btv->volume_gpio(btv, c->val);
1315 1316
		break;

1317 1318 1319 1320
	case V4L2_CID_CHROMA_AGC:
		val = c->val ? BT848_SCLOOP_CAGC : 0;
		btwrite(val, BT848_E_SCLOOP);
		btwrite(val, BT848_O_SCLOOP);
1321 1322
		break;
	case V4L2_CID_PRIVATE_COMBFILTER:
1323
		btv->opt_combfilter = c->val;
1324 1325
		break;
	case V4L2_CID_PRIVATE_LUMAFILTER:
1326
		if (c->val) {
1327 1328 1329 1330 1331 1332 1333 1334
			btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
			btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
		} else {
			btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
			btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
		}
		break;
	case V4L2_CID_PRIVATE_AUTOMUTE:
1335
		btv->opt_automute = c->val;
1336 1337 1338
		break;
	case V4L2_CID_PRIVATE_AGC_CRUSH:
		btwrite(BT848_ADC_RESERVED |
1339
				(c->val ? BT848_ADC_CRUSH : 0),
1340 1341 1342
				BT848_ADC);
		break;
	case V4L2_CID_PRIVATE_VCR_HACK:
1343
		btv->opt_vcr_hack = c->val;
1344 1345
		break;
	case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1346
		btwrite(c->val, BT848_WC_UP);
1347 1348
		break;
	case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1349
		btwrite(c->val, BT848_WC_DOWN);
1350 1351
		break;
	case V4L2_CID_PRIVATE_UV_RATIO:
1352
		btv->opt_uv_ratio = c->val;
1353 1354 1355
		bt848_sat(btv, btv->saturation);
		break;
	case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1356
		btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
1357 1358
		break;
	case V4L2_CID_PRIVATE_CORING:
1359
		btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
1360 1361 1362 1363 1364 1365 1366
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
/* ----------------------------------------------------------------------- */

static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
	.s_ctrl = bttv_s_ctrl,
};

static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_COMBFILTER,
	.name = "Comb Filter",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
	.def = 1,
};

static struct v4l2_ctrl_config bttv_ctrl_automute = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_AUTOMUTE,
	.name = "Auto Mute",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
	.def = 1,
};

static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_LUMAFILTER,
	.name = "Luma Decimation Filter",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
	.def = 1,
};

static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_AGC_CRUSH,
	.name = "AGC Crush",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
	.def = 1,
};

static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_VCR_HACK,
	.name = "VCR Hack",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
	.def = 1,
};

static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
	.name = "Whitecrush Lower",
	.type = V4L2_CTRL_TYPE_INTEGER,
	.min = 0,
	.max = 255,
	.step = 1,
	.def = 0x7f,
};

static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
	.name = "Whitecrush Upper",
	.type = V4L2_CTRL_TYPE_INTEGER,
	.min = 0,
	.max = 255,
	.step = 1,
	.def = 0xcf,
};

static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_UV_RATIO,
	.name = "UV Ratio",
	.type = V4L2_CTRL_TYPE_INTEGER,
	.min = 0,
	.max = 100,
	.step = 1,
	.def = 50,
};

static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
	.name = "Full Luma Range",
	.type = V4L2_CTRL_TYPE_BOOLEAN,
	.min = 0,
	.max = 1,
	.step = 1,
};

static struct v4l2_ctrl_config bttv_ctrl_coring = {
	.ops = &bttv_ctrl_ops,
	.id = V4L2_CID_PRIVATE_CORING,
	.name = "Coring",
	.type = V4L2_CTRL_TYPE_INTEGER,
	.min = 0,
	.max = 3,
	.step = 1,
};


L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488
/* ----------------------------------------------------------------------- */

void bttv_gpio_tracking(struct bttv *btv, char *comment)
{
	unsigned int outbits, data;
	outbits = btread(BT848_GPIO_OUT_EN);
	data    = btread(BT848_GPIO_DATA);
1489 1490
	pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
		 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
L
Linus Torvalds 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
}

static void bttv_field_count(struct bttv *btv)
{
	int need_count = 0;

	if (btv->users)
		need_count++;

	if (need_count) {
		/* start field counter */
		btor(BT848_INT_VSYNC,BT848_INT_MASK);
	} else {
		/* stop field counter */
		btand(~BT848_INT_VSYNC,BT848_INT_MASK);
		btv->field_count = 0;
	}
}

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

1515 1516
	for (i = 0; i < FORMATS; i++) {
		if (-1 == formats[i].fourcc)
L
Linus Torvalds 已提交
1517
			continue;
1518 1519
		if (formats[i].fourcc == fourcc)
			return formats+i;
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
	}
	return NULL;
}

/* ----------------------------------------------------------------------- */
/* misc helpers                                                            */

static int
bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
		    struct bttv_buffer *new)
{
	struct bttv_buffer *old;
	unsigned long flags;

1534
	dprintk("switch_overlay: enter [new=%p]\n", new);
L
Linus Torvalds 已提交
1535
	if (new)
1536
		new->vb.state = VIDEOBUF_DONE;
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542 1543
	spin_lock_irqsave(&btv->s_lock,flags);
	old = btv->screen;
	btv->screen = new;
	btv->loop_irq |= 1;
	bttv_set_dma(btv, 0x03);
	spin_unlock_irqrestore(&btv->s_lock,flags);
	if (NULL != old) {
1544 1545
		dprintk("switch_overlay: old=%p state is %d\n",
			old, old->vb.state);
1546
		bttv_dma_free(&fh->cap,btv, old);
L
Linus Torvalds 已提交
1547 1548
		kfree(old);
	}
1549
	if (NULL == new)
1550
		free_btres_lock(btv,fh,RESOURCE_OVERLAY);
L
Linus Torvalds 已提交
1551
	dprintk("switch_overlay: done\n");
1552
	return 0;
L
Linus Torvalds 已提交
1553 1554 1555 1556 1557
}

/* ----------------------------------------------------------------------- */
/* video4linux (1) interface                                               */

1558 1559
static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
			       struct bttv_buffer *buf,
1560
			       const struct bttv_format *fmt,
L
Linus Torvalds 已提交
1561 1562 1563
			       unsigned int width, unsigned int height,
			       enum v4l2_field field)
{
1564
	struct bttv_fh *fh = q->priv_data;
L
Linus Torvalds 已提交
1565
	int redo_dma_risc = 0;
1566 1567
	struct bttv_crop c;
	int norm;
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	int rc;

	/* check settings */
	if (NULL == fmt)
		return -EINVAL;
	if (fmt->btformat == BT848_COLOR_FMT_RAW) {
		width  = RAW_BPL;
		height = RAW_LINES*2;
		if (width*height > buf->vb.bsize)
			return -EINVAL;
		buf->vb.size = buf->vb.bsize;
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591

		/* Make sure tvnorm and vbi_end remain consistent
		   until we're done. */

		norm = btv->tvnorm;

		/* In this mode capturing always starts at defrect.top
		   (default VDELAY), ignoring cropping parameters. */
		if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
			return -EINVAL;
		}

		c.rect = bttv_tvnorms[norm].cropcap.defrect;
L
Linus Torvalds 已提交
1592
	} else {
1593 1594 1595 1596 1597 1598
		norm = btv->tvnorm;
		c = btv->crop[!!fh->do_crop];

		if (width < c.min_scaled_width ||
		    width > c.max_scaled_width ||
		    height < c.min_scaled_height)
L
Linus Torvalds 已提交
1599
			return -EINVAL;
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616

		switch (field) {
		case V4L2_FIELD_TOP:
		case V4L2_FIELD_BOTTOM:
		case V4L2_FIELD_ALTERNATE:
			/* btv->crop counts frame lines. Max. scale
			   factor is 16:1 for frames, 8:1 for fields. */
			if (height * 2 > c.max_scaled_height)
				return -EINVAL;
			break;

		default:
			if (height > c.max_scaled_height)
				return -EINVAL;
			break;
		}

L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622 1623 1624
		buf->vb.size = (width * height * fmt->depth) >> 3;
		if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
			return -EINVAL;
	}

	/* alloc + fill struct bttv_buffer (if changed) */
	if (buf->vb.width != width || buf->vb.height != height ||
	    buf->vb.field != field ||
1625 1626 1627 1628 1629
	    buf->tvnorm != norm || buf->fmt != fmt ||
	    buf->crop.top != c.rect.top ||
	    buf->crop.left != c.rect.left ||
	    buf->crop.width != c.rect.width ||
	    buf->crop.height != c.rect.height) {
L
Linus Torvalds 已提交
1630 1631 1632
		buf->vb.width  = width;
		buf->vb.height = height;
		buf->vb.field  = field;
1633
		buf->tvnorm    = norm;
L
Linus Torvalds 已提交
1634
		buf->fmt       = fmt;
1635
		buf->crop      = c.rect;
L
Linus Torvalds 已提交
1636 1637 1638 1639
		redo_dma_risc = 1;
	}

	/* alloc risc memory */
1640
	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
L
Linus Torvalds 已提交
1641
		redo_dma_risc = 1;
1642
		if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
L
Linus Torvalds 已提交
1643 1644 1645 1646 1647 1648 1649
			goto fail;
	}

	if (redo_dma_risc)
		if (0 != (rc = bttv_buffer_risc(btv,buf)))
			goto fail;

1650
	buf->vb.state = VIDEOBUF_PREPARED;
L
Linus Torvalds 已提交
1651 1652 1653
	return 0;

 fail:
1654
	bttv_dma_free(q,btv,buf);
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	return rc;
}

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

	*size = fh->fmt->depth*fh->width*fh->height >> 3;
	if (0 == *count)
		*count = gbuffers;
1666 1667
	if (*size * *count > gbuffers * gbufsize)
		*count = (gbuffers * gbufsize) / *size;
L
Linus Torvalds 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
	return 0;
}

static int
buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
	       enum v4l2_field field)
{
	struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
	struct bttv_fh *fh = q->priv_data;

1678
	return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
L
Linus Torvalds 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
				   fh->width, fh->height, field);
}

static void
buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
	struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
	struct bttv_fh *fh = q->priv_data;
	struct bttv    *btv = fh->btv;

1689
	buf->vb.state = VIDEOBUF_QUEUED;
L
Linus Torvalds 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
	list_add_tail(&buf->vb.queue,&btv->capture);
	if (!btv->curr.frame_irq) {
		btv->loop_irq |= 1;
		bttv_set_dma(btv, 0x03);
	}
}

static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
	struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
	struct bttv_fh *fh = q->priv_data;

1702
	bttv_dma_free(q,fh->btv,buf);
L
Linus Torvalds 已提交
1703 1704
}

1705
static const struct videobuf_queue_ops bttv_video_qops = {
L
Linus Torvalds 已提交
1706 1707 1708 1709 1710 1711
	.buf_setup    = buffer_setup,
	.buf_prepare  = buffer_prepare,
	.buf_queue    = buffer_queue,
	.buf_release  = buffer_release,
};

1712 1713 1714 1715 1716 1717
static void radio_enable(struct bttv *btv)
{
	/* Switch to the radio tuner */
	if (!btv->has_radio_tuner) {
		btv->has_radio_tuner = 1;
		bttv_call_all(btv, tuner, s_radio);
1718 1719
		btv->audio_input = TVAUDIO_INPUT_RADIO;
		audio_input(btv, btv->audio_input);
1720 1721 1722
	}
}

1723
static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
L
Linus Torvalds 已提交
1724
{
1725 1726 1727
	struct bttv_fh *fh  = priv;
	struct bttv *btv = fh->btv;
	unsigned int i;
L
Linus Torvalds 已提交
1728

1729
	for (i = 0; i < BTTV_TVNORMS; i++)
1730
		if (id & bttv_tvnorms[i].v4l2_id)
1731
			break;
1732 1733
	if (i == BTTV_TVNORMS)
		return -EINVAL;
1734
	btv->std = id;
1735
	set_tvnorm(btv, i);
1736
	return 0;
1737
}
L
Linus Torvalds 已提交
1738

1739 1740 1741 1742 1743 1744 1745 1746 1747
static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
	struct bttv_fh *fh  = priv;
	struct bttv *btv = fh->btv;

	*id = btv->std;
	return 0;
}

1748
static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1749 1750 1751
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;
L
Linus Torvalds 已提交
1752

1753
	if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
H
Hans Verkuil 已提交
1754
		*id &= V4L2_STD_625_50;
1755
	else
H
Hans Verkuil 已提交
1756
		*id &= V4L2_STD_525_60;
1757 1758
	return 0;
}
L
Linus Torvalds 已提交
1759

1760
static int bttv_enum_input(struct file *file, void *priv,
1761 1762 1763 1764
					struct v4l2_input *i)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
L
Linus Torvalds 已提交
1765

1766 1767
	if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
		return -EINVAL;
1768

1769
	i->type     = V4L2_INPUT_TYPE_CAMERA;
1770
	i->audioset = 0;
L
Linus Torvalds 已提交
1771

1772
	if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1773 1774 1775 1776 1777 1778 1779
		sprintf(i->name, "Television");
		i->type  = V4L2_INPUT_TYPE_TUNER;
		i->tuner = 0;
	} else if (i->index == btv->svhs) {
		sprintf(i->name, "S-Video");
	} else {
		sprintf(i->name, "Composite%d", i->index);
L
Linus Torvalds 已提交
1780 1781
	}

1782 1783 1784 1785 1786 1787
	if (i->index == btv->input) {
		__u32 dstatus = btread(BT848_DSTATUS);
		if (0 == (dstatus & BT848_DSTATUS_PRES))
			i->status |= V4L2_IN_ST_NO_SIGNAL;
		if (0 == (dstatus & BT848_DSTATUS_HLOC))
			i->status |= V4L2_IN_ST_NO_H_LOCK;
1788
	}
L
Linus Torvalds 已提交
1789

1790
	i->std = BTTV_NORMS;
1791
	return 0;
L
Linus Torvalds 已提交
1792 1793
}

1794
static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1795
{
1796 1797
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
1798

1799
	*i = btv->input;
1800

1801 1802
	return 0;
}
1803

1804
static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1805 1806 1807 1808
{
	struct bttv_fh *fh  = priv;
	struct bttv *btv = fh->btv;

1809 1810
	if (i >= bttv_tvcards[btv->c.type].video_inputs)
		return -EINVAL;
1811 1812 1813 1814 1815

	set_input(btv, i, btv->tvnorm);
	return 0;
}

1816
static int bttv_s_tuner(struct file *file, void *priv,
1817
					const struct v4l2_tuner *t)
1818 1819 1820 1821
{
	struct bttv_fh *fh  = priv;
	struct bttv *btv = fh->btv;

1822
	if (t->index)
1823 1824
		return -EINVAL;

1825
	bttv_call_all(btv, tuner, s_tuner, t);
1826

1827 1828 1829 1830 1831
	if (btv->audio_mode_gpio) {
		struct v4l2_tuner copy = *t;

		btv->audio_mode_gpio(btv, &copy, 1);
	}
1832 1833 1834
	return 0;
}

1835
static int bttv_g_frequency(struct file *file, void *priv,
1836 1837 1838 1839 1840
					struct v4l2_frequency *f)
{
	struct bttv_fh *fh  = priv;
	struct bttv *btv = fh->btv;

1841 1842
	if (f->tuner)
		return -EINVAL;
1843

1844 1845
	if (f->type == V4L2_TUNER_RADIO)
		radio_enable(btv);
1846 1847 1848
	f->frequency = f->type == V4L2_TUNER_RADIO ?
				btv->radio_freq : btv->tv_freq;

1849 1850 1851
	return 0;
}

1852
static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
1853
{
1854 1855
	struct v4l2_frequency new_freq = *f;

1856 1857 1858
	bttv_call_all(btv, tuner, s_frequency, f);
	/* s_frequency may clamp the frequency, so get the actual
	   frequency before assigning radio/tv_freq. */
1859 1860
	bttv_call_all(btv, tuner, g_frequency, &new_freq);
	if (new_freq.type == V4L2_TUNER_RADIO) {
1861
		radio_enable(btv);
1862
		btv->radio_freq = new_freq.frequency;
1863 1864 1865 1866
		if (btv->has_tea575x) {
			btv->tea.freq = btv->radio_freq;
			snd_tea575x_set_freq(&btv->tea);
		}
1867
	} else {
1868
		btv->tv_freq = new_freq.frequency;
1869 1870 1871
	}
}

1872
static int bttv_s_frequency(struct file *file, void *priv,
1873
					const struct v4l2_frequency *f)
1874 1875 1876 1877
{
	struct bttv_fh *fh  = priv;
	struct bttv *btv = fh->btv;

1878
	if (f->tuner)
1879
		return -EINVAL;
1880

1881
	bttv_set_frequency(btv, f);
1882 1883 1884
	return 0;
}

1885
static int bttv_log_status(struct file *file, void *f)
1886
{
1887
	struct video_device *vdev = video_devdata(file);
1888 1889 1890
	struct bttv_fh *fh  = f;
	struct bttv *btv = fh->btv;

1891
	v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
1892
	bttv_call_all(btv, core, log_status);
1893 1894 1895 1896
	return 0;
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
1897
static int bttv_g_register(struct file *file, void *f,
1898
					struct v4l2_dbg_register *reg)
1899 1900 1901 1902 1903 1904 1905
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;

	/* bt848 has a 12-bit register space */
	reg->reg &= 0xfff;
	reg->val = btread(reg->reg);
1906
	reg->size = 1;
1907 1908 1909 1910

	return 0;
}

1911
static int bttv_s_register(struct file *file, void *f,
1912
					const struct v4l2_dbg_register *reg)
1913 1914 1915 1916 1917
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;

	/* bt848 has a 12-bit register space */
1918
	btwrite(reg->val, reg->reg & 0xfff);
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959

	return 0;
}
#endif

/* Given cropping boundaries b and the scaled width and height of a
   single field or frame, which must not exceed hardware limits, this
   function adjusts the cropping parameters c. */
static void
bttv_crop_adjust	(struct bttv_crop *             c,
			 const struct v4l2_rect *	b,
			 __s32                          width,
			 __s32                          height,
			 enum v4l2_field                field)
{
	__s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
	__s32 max_left;
	__s32 max_top;

	if (width < c->min_scaled_width) {
		/* Max. hor. scale factor 16:1. */
		c->rect.width = width * 16;
	} else if (width > c->max_scaled_width) {
		/* Min. hor. scale factor 1:1. */
		c->rect.width = width;

		max_left = b->left + b->width - width;
		max_left = min(max_left, (__s32) MAX_HDELAY);
		if (c->rect.left > max_left)
			c->rect.left = max_left;
	}

	if (height < c->min_scaled_height) {
		/* Max. vert. scale factor 16:1, single fields 8:1. */
		c->rect.height = height * 16;
	} else if (frame_height > c->max_scaled_height) {
		/* Min. vert. scale factor 1:1.
		   Top and height count field lines times two. */
		c->rect.height = (frame_height + 1) & ~1;

		max_top = b->top + b->height - c->rect.height;
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
		if (c->rect.top > max_top)
			c->rect.top = max_top;
	}

	bttv_crop_calc_limits(c);
}

/* Returns an error if scaling to a frame or single field with the given
   width and height is not possible with the current cropping parameters
   and width aligned according to width_mask. If adjust_size is TRUE the
   function may adjust the width and/or height instead, rounding width
   to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
   also adjust the current cropping parameters to get closer to the
   desired image size. */
static int
1975
limit_scaled_size_lock       (struct bttv_fh *               fh,
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
			 __s32 *                        width,
			 __s32 *                        height,
			 enum v4l2_field                field,
			 unsigned int			width_mask,
			 unsigned int			width_bias,
			 int                            adjust_size,
			 int                            adjust_crop)
{
	struct bttv *btv = fh->btv;
	const struct v4l2_rect *b;
	struct bttv_crop *c;
	__s32 min_width;
	__s32 min_height;
	__s32 max_width;
	__s32 max_height;
	int rc;

	BUG_ON((int) width_mask >= 0 ||
	       width_bias >= (unsigned int) -width_mask);

	/* Make sure tvnorm, vbi_end and the current cropping parameters
	   remain consistent until we're done. */

	b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;

	/* Do crop - use current, don't - use default parameters. */
	c = &btv->crop[!!fh->do_crop];

	if (fh->do_crop
	    && adjust_size
	    && adjust_crop
	    && !locked_btres(btv, VIDEO_RESOURCES)) {
		min_width = 48;
		min_height = 32;

		/* We cannot scale up. When the scaled image is larger
		   than crop.rect we adjust the crop.rect as required
		   by the V4L2 spec, hence cropcap.bounds are our limit. */
2014
		max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 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
		max_height = b->height;

		/* We cannot capture the same line as video and VBI data.
		   Note btv->vbi_end is really a minimum, see
		   bttv_vbi_try_fmt(). */
		if (btv->vbi_end > b->top) {
			max_height -= btv->vbi_end - b->top;
			rc = -EBUSY;
			if (min_height > max_height)
				goto fail;
		}
	} else {
		rc = -EBUSY;
		if (btv->vbi_end > c->rect.top)
			goto fail;

		min_width  = c->min_scaled_width;
		min_height = c->min_scaled_height;
		max_width  = c->max_scaled_width;
		max_height = c->max_scaled_height;

		adjust_crop = 0;
	}

	min_width = (min_width - width_mask - 1) & width_mask;
	max_width = max_width & width_mask;

	/* Max. scale factor is 16:1 for frames, 8:1 for fields. */
	/* Min. scale factor is 1:1. */
	max_height >>= !V4L2_FIELD_HAS_BOTH(field);

	if (adjust_size) {
		*width = clamp(*width, min_width, max_width);
		*height = clamp(*height, min_height, max_height);

		/* Round after clamping to avoid overflow. */
		*width = (*width + width_bias) & width_mask;

		if (adjust_crop) {
			bttv_crop_adjust(c, b, *width, *height, field);

			if (btv->vbi_end > c->rect.top) {
				/* Move the crop window out of the way. */
				c->rect.top = btv->vbi_end;
			}
		}
	} else {
		rc = -EINVAL;
		if (*width  < min_width ||
		    *height < min_height ||
		    *width  > max_width ||
		    *height > max_height ||
		    0 != (*width & ~width_mask))
			goto fail;
	}

	rc = 0; /* success */

 fail:

	return rc;
}

/* Returns an error if the given overlay window dimensions are not
   possible with the current cropping parameters. If adjust_size is
   TRUE the function may adjust the window width and/or height
   instead, however it always rounds the horizontal position and
   width as btcx_align() does. If adjust_crop is TRUE the function
   may also adjust the current cropping parameters to get closer
   to the desired window size. */
static int
2086 2087
verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
			 int adjust_size, int adjust_crop)
L
Linus Torvalds 已提交
2088 2089
{
	enum v4l2_field field;
2090 2091
	unsigned int width_mask;
	int rc;
L
Linus Torvalds 已提交
2092

2093 2094 2095 2096
	if (win->w.width < 48)
		win->w.width = 48;
	if (win->w.height < 32)
		win->w.height = 32;
L
Linus Torvalds 已提交
2097
	if (win->clipcount > 2048)
2098
		win->clipcount = 2048;
L
Linus Torvalds 已提交
2099

2100 2101
	win->chromakey = 0;
	win->global_alpha = 0;
L
Linus Torvalds 已提交
2102 2103
	field = win->field;

2104 2105 2106 2107 2108 2109 2110 2111 2112
	switch (field) {
	case V4L2_FIELD_TOP:
	case V4L2_FIELD_BOTTOM:
	case V4L2_FIELD_INTERLACED:
		break;
	default:
		field = V4L2_FIELD_ANY;
		break;
	}
L
Linus Torvalds 已提交
2113
	if (V4L2_FIELD_ANY == field) {
2114 2115 2116 2117
		__s32 height2;

		height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
		field = (win->w.height > height2)
L
Linus Torvalds 已提交
2118 2119 2120
			? V4L2_FIELD_INTERLACED
			: V4L2_FIELD_TOP;
	}
2121
	win->field = field;
L
Linus Torvalds 已提交
2122

2123
	if (NULL == fh->ovfmt)
L
Linus Torvalds 已提交
2124
		return -EINVAL;
2125
	/* 4-byte alignment. */
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
	width_mask = ~0;
	switch (fh->ovfmt->depth) {
	case 8:
	case 24:
		width_mask = ~3;
		break;
	case 16:
		width_mask = ~1;
		break;
	case 32:
		break;
	default:
		BUG();
	}

	win->w.width -= win->w.left & ~width_mask;
	win->w.left = (win->w.left - width_mask - 1) & width_mask;

2144
	rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2145 2146 2147 2148 2149
			       field, width_mask,
			       /* width_bias: round down */ 0,
			       adjust_size, adjust_crop);
	if (0 != rc)
		return rc;
L
Linus Torvalds 已提交
2150 2151 2152
	return 0;
}

2153
static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
L
Linus Torvalds 已提交
2154 2155 2156 2157 2158 2159 2160 2161 2162
			struct v4l2_window *win, int fixup)
{
	struct v4l2_clip *clips = NULL;
	int n,size,retval = 0;

	if (NULL == fh->ovfmt)
		return -EINVAL;
	if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
		return -EINVAL;
2163
	retval = verify_window_lock(fh, win,
2164 2165
			       /* adjust_size */ fixup,
			       /* adjust_crop */ fixup);
L
Linus Torvalds 已提交
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
	if (0 != retval)
		return retval;

	/* copy clips  --  luckily v4l1 + v4l2 are binary
	   compatible here ...*/
	n = win->clipcount;
	size = sizeof(*clips)*(n+4);
	clips = kmalloc(size,GFP_KERNEL);
	if (NULL == clips)
		return -ENOMEM;
	if (n > 0) {
		if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
			kfree(clips);
			return -EFAULT;
		}
	}
2182

L
Linus Torvalds 已提交
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	/* clip against screen */
	if (NULL != btv->fbuf.base)
		n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
				      &win->w, clips, n);
	btcx_sort_clips(clips,n);

	/* 4-byte alignments */
	switch (fh->ovfmt->depth) {
	case 8:
	case 24:
		btcx_align(&win->w, clips, n, 3);
		break;
	case 16:
		btcx_align(&win->w, clips, n, 1);
		break;
	case 32:
		/* no alignment fixups needed */
		break;
	default:
		BUG();
	}

2205
	kfree(fh->ov.clips);
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210 2211
	fh->ov.clips    = clips;
	fh->ov.nclips   = n;

	fh->ov.w        = win->w;
	fh->ov.field    = win->field;
	fh->ov.setup_ok = 1;
2212

L
Linus Torvalds 已提交
2213 2214 2215 2216 2217 2218 2219 2220 2221
	btv->init.ov.w.width   = win->w.width;
	btv->init.ov.w.height  = win->w.height;
	btv->init.ov.field     = win->field;

	/* update overlay if needed */
	retval = 0;
	if (check_btres(fh, RESOURCE_OVERLAY)) {
		struct bttv_buffer *new;

2222
		new = videobuf_sg_alloc(sizeof(*new));
2223
		new->crop = btv->crop[!!fh->do_crop].rect;
L
Linus Torvalds 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
		bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
		retval = bttv_switch_overlay(btv,fh,new);
	}
	return retval;
}

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

static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
{
	struct videobuf_queue* q = NULL;

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

static int bttv_resource(struct bttv_fh *fh)
{
	int res = 0;

	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2255
		res = RESOURCE_VIDEO_STREAM;
L
Linus Torvalds 已提交
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
		break;
	case V4L2_BUF_TYPE_VBI_CAPTURE:
		res = RESOURCE_VBI;
		break;
	default:
		BUG();
	}
	return res;
}

static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
{
	struct videobuf_queue *q = bttv_queue(fh);
	int res = bttv_resource(fh);

	if (check_btres(fh,res))
		return -EBUSY;
	if (videobuf_queue_is_busy(q))
		return -EBUSY;
	fh->type = type;
	return 0;
}

2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
static void
pix_format_set_size     (struct v4l2_pix_format *       f,
			 const struct bttv_format *     fmt,
			 unsigned int                   width,
			 unsigned int                   height)
{
	f->width = width;
	f->height = height;

	if (fmt->flags & FORMAT_FLAGS_PLANAR) {
		f->bytesperline = width; /* Y plane */
		f->sizeimage = (width * height * fmt->depth) >> 3;
	} else {
		f->bytesperline = (width * fmt->depth) >> 3;
		f->sizeimage = height * f->bytesperline;
	}
}

2297
static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2298
					struct v4l2_format *f)
L
Linus Torvalds 已提交
2299
{
2300 2301 2302 2303 2304 2305
	struct bttv_fh *fh  = priv;

	pix_format_set_size(&f->fmt.pix, fh->fmt,
				fh->width, fh->height);
	f->fmt.pix.field        = fh->cap.field;
	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
H
Hans Verkuil 已提交
2306
	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
2307 2308

	return 0;
L
Linus Torvalds 已提交
2309 2310
}

2311
static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2312
					struct v4l2_format *f)
L
Linus Torvalds 已提交
2313
{
2314
	struct bttv_fh *fh  = priv;
L
Linus Torvalds 已提交
2315

2316 2317
	f->fmt.win.w     = fh->ov.w;
	f->fmt.win.field = fh->ov.field;
L
Linus Torvalds 已提交
2318

2319 2320
	return 0;
}
2321

2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
					unsigned int *width_mask,
					unsigned int *width_bias)
{
	if (fmt->flags & FORMAT_FLAGS_PLANAR) {
		*width_mask = ~15; /* width must be a multiple of 16 pixels */
		*width_bias = 8;   /* nearest */
	} else {
		*width_mask = ~3; /* width must be a multiple of 4 pixels */
		*width_bias = 2;  /* nearest */
	}
}

2335
static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2336 2337 2338 2339 2340 2341 2342
						struct v4l2_format *f)
{
	const struct bttv_format *fmt;
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
	enum v4l2_field field;
	__s32 width, height;
2343
	__s32 height2;
2344
	unsigned int width_mask, width_bias;
2345
	int rc;
L
Linus Torvalds 已提交
2346

2347 2348 2349
	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	if (NULL == fmt)
		return -EINVAL;
2350

2351
	field = f->fmt.pix.field;
2352

2353 2354 2355 2356 2357 2358
	switch (field) {
	case V4L2_FIELD_TOP:
	case V4L2_FIELD_BOTTOM:
	case V4L2_FIELD_ALTERNATE:
	case V4L2_FIELD_INTERLACED:
		break;
2359
	case V4L2_FIELD_SEQ_BT:
2360
	case V4L2_FIELD_SEQ_TB:
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
		if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
			field = V4L2_FIELD_SEQ_TB;
			break;
		}
		/* fall through */
	default: /* FIELD_ANY case */
		height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
		field = (f->fmt.pix.height > height2)
			? V4L2_FIELD_INTERLACED
			: V4L2_FIELD_BOTTOM;
2371
		break;
L
Linus Torvalds 已提交
2372
	}
2373 2374 2375 2376

	width = f->fmt.pix.width;
	height = f->fmt.pix.height;

2377
	bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2378
	rc = limit_scaled_size_lock(fh, &width, &height, field,
2379
			       width_mask, width_bias,
2380 2381 2382 2383 2384
			       /* adjust_size */ 1,
			       /* adjust_crop */ 0);
	if (0 != rc)
		return rc;

2385 2386 2387
	/* update data for the application */
	f->fmt.pix.field = field;
	pix_format_set_size(&f->fmt.pix, fmt, width, height);
H
Hans Verkuil 已提交
2388
	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2389 2390 2391 2392

	return 0;
}

2393
static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2394 2395 2396 2397
						struct v4l2_format *f)
{
	struct bttv_fh *fh = priv;

2398
	verify_window_lock(fh, &f->fmt.win,
2399 2400
			/* adjust_size */ 1,
			/* adjust_crop */ 0);
2401
	return 0;
L
Linus Torvalds 已提交
2402 2403
}

2404
static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2405
				struct v4l2_format *f)
L
Linus Torvalds 已提交
2406 2407
{
	int retval;
2408 2409 2410
	const struct bttv_format *fmt;
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
2411
	__s32 width, height;
2412
	unsigned int width_mask, width_bias;
2413
	enum v4l2_field field;
L
Linus Torvalds 已提交
2414

2415 2416 2417
	retval = bttv_switch_type(fh, f->type);
	if (0 != retval)
		return retval;
L
Linus Torvalds 已提交
2418

2419
	retval = bttv_try_fmt_vid_cap(file, priv, f);
2420 2421
	if (0 != retval)
		return retval;
L
Linus Torvalds 已提交
2422

2423 2424 2425 2426
	width = f->fmt.pix.width;
	height = f->fmt.pix.height;
	field = f->fmt.pix.field;

2427 2428
	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2429
	retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2430
			       width_mask, width_bias,
2431 2432 2433 2434 2435 2436 2437
			       /* adjust_size */ 1,
			       /* adjust_crop */ 1);
	if (0 != retval)
		return retval;

	f->fmt.pix.field = field;

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
	/* update our state informations */
	fh->fmt              = fmt;
	fh->cap.field        = f->fmt.pix.field;
	fh->cap.last         = V4L2_FIELD_NONE;
	fh->width            = f->fmt.pix.width;
	fh->height           = f->fmt.pix.height;
	btv->init.fmt        = fmt;
	btv->init.width      = f->fmt.pix.width;
	btv->init.height     = f->fmt.pix.height;

	return 0;
}

2451
static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2452 2453 2454 2455 2456
				struct v4l2_format *f)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;

2457
	if (no_overlay > 0) {
2458
		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
L
Linus Torvalds 已提交
2459
		return -EINVAL;
2460
	}
2461

2462
	return setup_window_lock(fh, btv, &f->fmt.win, 1);
2463 2464
}

2465
static int bttv_querycap(struct file *file, void  *priv,
2466
				struct v4l2_capability *cap)
L
Linus Torvalds 已提交
2467
{
2468
	struct video_device *vdev = video_devdata(file);
2469 2470
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
L
Linus Torvalds 已提交
2471

2472 2473
	if (0 == v4l2)
		return -EINVAL;
2474

2475 2476
	strscpy(cap->driver, "bttv", sizeof(cap->driver));
	strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
2477 2478 2479 2480 2481
	snprintf(cap->bus_info, sizeof(cap->bus_info),
		 "PCI:%s", pci_name(btv->c.pci));
	cap->capabilities =
		V4L2_CAP_VIDEO_CAPTURE |
		V4L2_CAP_READWRITE |
2482 2483
		V4L2_CAP_STREAMING |
		V4L2_CAP_DEVICE_CAPS;
2484 2485
	if (no_overlay <= 0)
		cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
H
Hans Verkuil 已提交
2486
	if (video_is_registered(&btv->vbi_dev))
2487
		cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
H
Hans Verkuil 已提交
2488
	if (video_is_registered(&btv->radio_dev))
2489
		cap->capabilities |= V4L2_CAP_RADIO;
2490

2491 2492 2493 2494 2495 2496
	/*
	 * No need to lock here: those vars are initialized during board
	 * probe and remains untouched during the rest of the driver lifecycle
	 */
	if (btv->has_saa6588)
		cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2497
	if (btv->tuner_type != TUNER_ABSENT)
2498
		cap->capabilities |= V4L2_CAP_TUNER;
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
	if (vdev->vfl_type == VFL_TYPE_GRABBER)
		cap->device_caps = cap->capabilities &
			(V4L2_CAP_VIDEO_CAPTURE |
			 V4L2_CAP_READWRITE |
			 V4L2_CAP_STREAMING |
			 V4L2_CAP_VIDEO_OVERLAY |
			 V4L2_CAP_TUNER);
	else if (vdev->vfl_type == VFL_TYPE_VBI)
		cap->device_caps = cap->capabilities &
			(V4L2_CAP_VBI_CAPTURE |
			 V4L2_CAP_READWRITE |
			 V4L2_CAP_STREAMING |
			 V4L2_CAP_TUNER);
	else {
		cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
		if (btv->has_saa6588)
			cap->device_caps |= V4L2_CAP_READWRITE |
						V4L2_CAP_RDS_CAPTURE;
2517 2518
		if (btv->has_tea575x)
			cap->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
2519
	}
2520 2521
	return 0;
}
L
Linus Torvalds 已提交
2522

2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
{
	int index = -1, i;

	for (i = 0; i < FORMATS; i++) {
		if (formats[i].fourcc != -1)
			index++;
		if ((unsigned int)index == f->index)
			break;
	}
	if (FORMATS == i)
		return -EINVAL;

	f->pixelformat = formats[i].fourcc;
2537
	strscpy(f->description, formats[i].name, sizeof(f->description));
2538 2539 2540 2541

	return i;
}

2542
static int bttv_enum_fmt_vid_cap(struct file *file, void  *priv,
2543 2544
				struct v4l2_fmtdesc *f)
{
2545
	int rc = bttv_enum_fmt_cap_ovr(f);
L
Linus Torvalds 已提交
2546

2547 2548
	if (rc < 0)
		return rc;
L
Linus Torvalds 已提交
2549

2550 2551
	return 0;
}
2552

2553
static int bttv_enum_fmt_vid_overlay(struct file *file, void  *priv,
2554 2555
					struct v4l2_fmtdesc *f)
{
2556 2557
	int rc;

2558
	if (no_overlay > 0) {
2559
		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2560
		return -EINVAL;
L
Linus Torvalds 已提交
2561 2562
	}

2563
	rc = bttv_enum_fmt_cap_ovr(f);
L
Linus Torvalds 已提交
2564

2565 2566
	if (rc < 0)
		return rc;
L
Linus Torvalds 已提交
2567

2568 2569
	if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
		return -EINVAL;
L
Linus Torvalds 已提交
2570

2571 2572 2573
	return 0;
}

2574
static int bttv_g_fbuf(struct file *file, void *f,
2575 2576 2577 2578 2579 2580 2581
				struct v4l2_framebuffer *fb)
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;

	*fb = btv->fbuf;
	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2582
	fb->flags = V4L2_FBUF_FLAG_PRIMARY;
2583 2584 2585 2586 2587
	if (fh->ovfmt)
		fb->fmt.pixelformat  = fh->ovfmt->fourcc;
	return 0;
}

2588
static int bttv_overlay(struct file *file, void *f, unsigned int on)
2589 2590 2591 2592
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;
	struct bttv_buffer *new;
2593
	int retval = 0;
2594 2595 2596

	if (on) {
		/* verify args */
2597
		if (unlikely(!btv->fbuf.base)) {
2598
			return -EINVAL;
2599 2600
		}
		if (unlikely(!fh->ov.setup_ok)) {
2601
			dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
2602
			retval = -EINVAL;
L
Linus Torvalds 已提交
2603
		}
2604 2605
		if (retval)
			return retval;
L
Linus Torvalds 已提交
2606
	}
2607

2608
	if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2609 2610 2611 2612
		return -EBUSY;

	if (on) {
		fh->ov.tvnorm = btv->tvnorm;
2613
		new = videobuf_sg_alloc(sizeof(*new));
2614
		new->crop = btv->crop[!!fh->do_crop].rect;
2615 2616 2617
		bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
	} else {
		new = NULL;
L
Linus Torvalds 已提交
2618 2619
	}

2620 2621 2622 2623 2624
	/* switch over */
	retval = bttv_switch_overlay(btv, fh, new);
	return retval;
}

2625
static int bttv_s_fbuf(struct file *file, void *f,
2626
				const struct v4l2_framebuffer *fb)
2627 2628 2629 2630 2631 2632 2633 2634 2635
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;
	const struct bttv_format *fmt;
	int retval;

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

2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
	/* check args */
	fmt = format_by_fourcc(fb->fmt.pixelformat);
	if (NULL == fmt)
		return -EINVAL;
	if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
		return -EINVAL;

	retval = -EINVAL;
	if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
		__s32 width = fb->fmt.width;
		__s32 height = fb->fmt.height;

2649
		retval = limit_scaled_size_lock(fh, &width, &height,
2650 2651 2652 2653 2654 2655 2656
					   V4L2_FIELD_INTERLACED,
					   /* width_mask */ ~3,
					   /* width_bias */ 2,
					   /* adjust_size */ 0,
					   /* adjust_crop */ 0);
		if (0 != retval)
			return retval;
L
Linus Torvalds 已提交
2657
	}
2658

2659 2660 2661 2662 2663 2664 2665 2666
	/* ok, accept it */
	btv->fbuf.base       = fb->base;
	btv->fbuf.fmt.width  = fb->fmt.width;
	btv->fbuf.fmt.height = fb->fmt.height;
	if (0 != fb->fmt.bytesperline)
		btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
	else
		btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2667

2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
	retval = 0;
	fh->ovfmt = fmt;
	btv->init.ovfmt = fmt;
	if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
		fh->ov.w.left   = 0;
		fh->ov.w.top    = 0;
		fh->ov.w.width  = fb->fmt.width;
		fh->ov.w.height = fb->fmt.height;
		btv->init.ov.w.width  = fb->fmt.width;
		btv->init.ov.w.height = fb->fmt.height;
2678 2679

		kfree(fh->ov.clips);
2680 2681 2682 2683 2684
		fh->ov.clips = NULL;
		fh->ov.nclips = 0;

		if (check_btres(fh, RESOURCE_OVERLAY)) {
			struct bttv_buffer *new;
2685

2686
			new = videobuf_sg_alloc(sizeof(*new));
2687
			new->crop = btv->crop[!!fh->do_crop].rect;
2688
			bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2689
			retval = bttv_switch_overlay(btv, fh, new);
2690 2691
		}
	}
2692 2693
	return retval;
}
L
Linus Torvalds 已提交
2694

2695
static int bttv_reqbufs(struct file *file, void *priv,
2696 2697 2698 2699 2700
				struct v4l2_requestbuffers *p)
{
	struct bttv_fh *fh = priv;
	return videobuf_reqbufs(bttv_queue(fh), p);
}
L
Linus Torvalds 已提交
2701

2702
static int bttv_querybuf(struct file *file, void *priv,
2703 2704 2705 2706 2707
				struct v4l2_buffer *b)
{
	struct bttv_fh *fh = priv;
	return videobuf_querybuf(bttv_queue(fh), b);
}
L
Linus Torvalds 已提交
2708

2709
static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2710 2711 2712 2713
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
	int res = bttv_resource(fh);
L
Linus Torvalds 已提交
2714

2715
	if (!check_alloc_btres_lock(btv, fh, res))
2716
		return -EBUSY;
2717

2718 2719
	return videobuf_qbuf(bttv_queue(fh), b);
}
L
Linus Torvalds 已提交
2720

2721
static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2722 2723 2724 2725 2726
{
	struct bttv_fh *fh = priv;
	return videobuf_dqbuf(bttv_queue(fh), b,
			file->f_flags & O_NONBLOCK);
}
L
Linus Torvalds 已提交
2727

2728
static int bttv_streamon(struct file *file, void *priv,
2729 2730 2731 2732 2733
					enum v4l2_buf_type type)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
	int res = bttv_resource(fh);
L
Linus Torvalds 已提交
2734

2735
	if (!check_alloc_btres_lock(btv, fh, res))
2736 2737 2738
		return -EBUSY;
	return videobuf_streamon(bttv_queue(fh));
}
L
Linus Torvalds 已提交
2739

2740

2741
static int bttv_streamoff(struct file *file, void *priv,
2742 2743 2744 2745 2746 2747
					enum v4l2_buf_type type)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
	int retval;
	int res = bttv_resource(fh);
2748

2749

2750 2751 2752
	retval = videobuf_streamoff(bttv_queue(fh));
	if (retval < 0)
		return retval;
2753
	free_btres_lock(btv, fh, res);
2754 2755
	return 0;
}
2756

2757
static int bttv_g_parm(struct file *file, void *f,
2758 2759 2760 2761
				struct v4l2_streamparm *parm)
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;
2762

2763 2764 2765
	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
	parm->parm.capture.readbuffers = gbuffers;
2766 2767
	v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
				    &parm->parm.capture.timeperframe);
2768

2769 2770
	return 0;
}
2771

2772
static int bttv_g_tuner(struct file *file, void *priv,
2773 2774 2775 2776
				struct v4l2_tuner *t)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
2777

2778 2779
	if (0 != t->index)
		return -EINVAL;
2780

2781
	t->rxsubchans = V4L2_TUNER_SUB_MONO;
2782
	t->capability = V4L2_TUNER_CAP_NORM;
2783
	bttv_call_all(btv, tuner, g_tuner, t);
2784
	strscpy(t->name, "Television", sizeof(t->name));
2785 2786 2787 2788 2789 2790
	t->type       = V4L2_TUNER_ANALOG_TV;
	if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
		t->signal = 0xffff;

	if (btv->audio_mode_gpio)
		btv->audio_mode_gpio(btv, t, 0);
2791

2792 2793
	return 0;
}
2794

2795 2796
static int bttv_g_pixelaspect(struct file *file, void *priv,
			      int type, struct v4l2_fract *f)
2797 2798 2799 2800
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;

2801
	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2802 2803
		return -EINVAL;

2804
	/* defrect and bounds are set via g_selection */
2805
	*f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
2806 2807
	return 0;
}
2808

2809
static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
2810 2811 2812 2813
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;

2814 2815
	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2816
		return -EINVAL;
2817

2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
	switch (sel->target) {
	case V4L2_SEL_TGT_CROP:
		/*
		 * No fh->do_crop = 1; because btv->crop[1] may be
		 * inconsistent with fh->width or fh->height and apps
		 * do not expect a change here.
		 */
		sel->r = btv->crop[!!fh->do_crop].rect;
		break;
	case V4L2_SEL_TGT_CROP_DEFAULT:
		sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
		break;
	case V4L2_SEL_TGT_CROP_BOUNDS:
		sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
		break;
	default:
		return -EINVAL;
	}
2836

2837 2838 2839
	return 0;
}

2840
static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
{
	struct bttv_fh *fh = f;
	struct bttv *btv = fh->btv;
	const struct v4l2_rect *b;
	int retval;
	struct bttv_crop c;
	__s32 b_left;
	__s32 b_top;
	__s32 b_right;
	__s32 b_bottom;
2851

2852 2853 2854 2855 2856
	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
		return -EINVAL;

	if (sel->target != V4L2_SEL_TGT_CROP)
2857
		return -EINVAL;
2858

2859 2860
	/* Make sure tvnorm, vbi_end and the current cropping
	   parameters remain consistent until we're done. Note
2861
	   read() may change vbi_end in check_alloc_btres_lock(). */
2862
	retval = -EBUSY;
2863

2864 2865 2866
	if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
		return retval;
	}
2867

2868
	b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2869

2870 2871 2872
	b_left = b->left;
	b_right = b_left + b->width;
	b_bottom = b->top + b->height;
2873

2874 2875 2876 2877
	b_top = max(b->top, btv->vbi_end);
	if (b_top + 32 >= b_bottom) {
		return retval;
	}
2878

2879
	/* Min. scaled size 48 x 32. */
2880
	c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
2881
	c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2882

2883
	c.rect.width = clamp_t(s32, sel->r.width,
2884
			     48, b_right - c.rect.left);
2885

2886
	c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
2887 2888
	/* Top and height must be a multiple of two. */
	c.rect.top = (c.rect.top + 1) & ~1;
2889

2890
	c.rect.height = clamp_t(s32, sel->r.height,
2891 2892
			      32, b_bottom - c.rect.top);
	c.rect.height = (c.rect.height + 1) & ~1;
2893

2894
	bttv_crop_calc_limits(&c);
2895

2896 2897
	sel->r = c.rect;

2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
	btv->crop[1] = c;

	fh->do_crop = 1;

	if (fh->width < c.min_scaled_width) {
		fh->width = c.min_scaled_width;
		btv->init.width = c.min_scaled_width;
	} else if (fh->width > c.max_scaled_width) {
		fh->width = c.max_scaled_width;
		btv->init.width = c.max_scaled_width;
2908 2909
	}

2910 2911 2912 2913 2914 2915
	if (fh->height < c.min_scaled_height) {
		fh->height = c.min_scaled_height;
		btv->init.height = c.min_scaled_height;
	} else if (fh->height > c.max_scaled_height) {
		fh->height = c.max_scaled_height;
		btv->init.height = c.max_scaled_height;
L
Linus Torvalds 已提交
2916 2917
	}

2918 2919 2920
	return 0;
}

L
Linus Torvalds 已提交
2921 2922 2923 2924 2925 2926 2927 2928
static ssize_t bttv_read(struct file *file, char __user *data,
			 size_t count, loff_t *ppos)
{
	struct bttv_fh *fh = file->private_data;
	int retval = 0;

	if (fh->btv->errors)
		bttv_reinit_bt848(fh->btv);
2929 2930
	dprintk("%d: read count=%d type=%s\n",
		fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
L
Linus Torvalds 已提交
2931 2932 2933

	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2934
		if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
2935 2936
			/* VIDEO_READ in use by another fh,
			   or VIDEO_STREAM by any fh. */
L
Linus Torvalds 已提交
2937
			return -EBUSY;
2938
		}
L
Linus Torvalds 已提交
2939 2940
		retval = videobuf_read_one(&fh->cap, data, count, ppos,
					   file->f_flags & O_NONBLOCK);
2941
		free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
L
Linus Torvalds 已提交
2942 2943
		break;
	case V4L2_BUF_TYPE_VBI_CAPTURE:
2944
		if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
L
Linus Torvalds 已提交
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
			return -EBUSY;
		retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
					      file->f_flags & O_NONBLOCK);
		break;
	default:
		BUG();
	}
	return retval;
}

A
Al Viro 已提交
2955
static __poll_t bttv_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
2956 2957 2958 2959
{
	struct bttv_fh *fh = file->private_data;
	struct bttv_buffer *buf;
	enum v4l2_field field;
A
Al Viro 已提交
2960
	__poll_t rc = 0;
A
Al Viro 已提交
2961
	__poll_t req_events = poll_requested_events(wait);
2962 2963

	if (v4l2_event_pending(&fh->fh))
2964 2965
		rc = EPOLLPRI;
	else if (req_events & EPOLLPRI)
2966 2967
		poll_wait(file, &fh->fh.wait, wait);

2968
	if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
2969
		return rc;
L
Linus Torvalds 已提交
2970 2971

	if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2972
		if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
2973
			return rc | EPOLLERR;
2974
		return rc | videobuf_poll_stream(file, &fh->vbi, wait);
L
Linus Torvalds 已提交
2975 2976
	}

2977
	if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
L
Linus Torvalds 已提交
2978 2979
		/* streaming capture */
		if (list_empty(&fh->cap.stream))
2980
			return rc | EPOLLERR;
L
Linus Torvalds 已提交
2981 2982 2983 2984 2985
		buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
	} else {
		/* read() capture */
		if (NULL == fh->cap.read_buf) {
			/* need to capture a new frame */
2986
			if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
2987
				return rc | EPOLLERR;
2988
			fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
2989
			if (NULL == fh->cap.read_buf)
2990
				return rc | EPOLLERR;
L
Linus Torvalds 已提交
2991 2992 2993
			fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
			field = videobuf_next_field(&fh->cap);
			if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2994 2995
				kfree (fh->cap.read_buf);
				fh->cap.read_buf = NULL;
2996
				return rc | EPOLLERR;
L
Linus Torvalds 已提交
2997 2998 2999 3000 3001 3002 3003 3004
			}
			fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
			fh->cap.read_off = 0;
		}
		buf = (struct bttv_buffer*)fh->cap.read_buf;
	}

	poll_wait(file, &buf->vb.done, wait);
3005 3006
	if (buf->vb.state == VIDEOBUF_DONE ||
	    buf->vb.state == VIDEOBUF_ERROR)
3007
		rc = rc | EPOLLIN|EPOLLRDNORM;
3008
	return rc;
L
Linus Torvalds 已提交
3009 3010
}

3011
static int bttv_open(struct file *file)
L
Linus Torvalds 已提交
3012
{
3013
	struct video_device *vdev = video_devdata(file);
3014
	struct bttv *btv = video_drvdata(file);
L
Linus Torvalds 已提交
3015 3016 3017
	struct bttv_fh *fh;
	enum v4l2_buf_type type = 0;

3018
	dprintk("open dev=%s\n", video_device_node_name(vdev));
L
Linus Torvalds 已提交
3019

3020
	if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3021
		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3022
	} else if (vdev->vfl_type == VFL_TYPE_VBI) {
3023 3024 3025
		type = V4L2_BUF_TYPE_VBI_CAPTURE;
	} else {
		WARN_ON(1);
L
Linus Torvalds 已提交
3026
		return -ENODEV;
3027
	}
L
Linus Torvalds 已提交
3028

3029 3030
	dprintk("%d: open called (type=%s)\n",
		btv->c.nr, v4l2_type_names[type]);
L
Linus Torvalds 已提交
3031 3032

	/* allocate per filehandle data */
3033 3034
	fh = kmalloc(sizeof(*fh), GFP_KERNEL);
	if (unlikely(!fh))
L
Linus Torvalds 已提交
3035
		return -ENOMEM;
3036
	btv->users++;
L
Linus Torvalds 已提交
3037
	file->private_data = fh;
3038

L
Linus Torvalds 已提交
3039
	*fh = btv->init;
3040
	v4l2_fh_init(&fh->fh, vdev);
3041

L
Linus Torvalds 已提交
3042 3043
	fh->type = type;
	fh->ov.setup_ok = 0;
3044

3045 3046
	videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
			    &btv->c.pci->dev, &btv->s_lock,
L
Linus Torvalds 已提交
3047 3048 3049
			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
			    V4L2_FIELD_INTERLACED,
			    sizeof(struct bttv_buffer),
3050
			    fh, &btv->lock);
3051 3052
	videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
			    &btv->c.pci->dev, &btv->s_lock,
L
Linus Torvalds 已提交
3053 3054 3055
			    V4L2_BUF_TYPE_VBI_CAPTURE,
			    V4L2_FIELD_SEQ_TB,
			    sizeof(struct bttv_buffer),
3056
			    fh, &btv->lock);
3057
	set_tvnorm(btv,btv->tvnorm);
3058
	set_input(btv, btv->input, btv->tvnorm);
3059
	audio_mute(btv, btv->mute);
3060 3061 3062 3063 3064

	/* The V4L2 spec requires one global set of cropping parameters
	   which only change on request. These are stored in btv->crop[1].
	   However for compatibility with V4L apps and cropping unaware
	   V4L2 apps we now reset the cropping parameters as seen through
3065
	   this fh, which is to say VIDIOC_G_SELECTION and scaling limit checks
3066 3067
	   will use btv->crop[0], the default cropping parameters for the
	   current video standard, and VIDIOC_S_FMT will not implicitely
3068
	   change the cropping parameters until VIDIOC_S_SELECTION has been
3069 3070 3071 3072 3073 3074 3075 3076
	   called. */
	fh->do_crop = !reset_crop; /* module parameter */

	/* Likewise there should be one global set of VBI capture
	   parameters, but for compatibility with V4L apps and earlier
	   driver versions each fh has its own parameters. */
	bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);

L
Linus Torvalds 已提交
3077
	bttv_field_count(btv);
3078
	v4l2_fh_add(&fh->fh);
L
Linus Torvalds 已提交
3079 3080 3081
	return 0;
}

3082
static int bttv_release(struct file *file)
L
Linus Torvalds 已提交
3083 3084 3085 3086 3087 3088 3089 3090 3091
{
	struct bttv_fh *fh = file->private_data;
	struct bttv *btv = fh->btv;

	/* turn off overlay */
	if (check_btres(fh, RESOURCE_OVERLAY))
		bttv_switch_overlay(btv,fh,NULL);

	/* stop video capture */
3092
	if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
L
Linus Torvalds 已提交
3093
		videobuf_streamoff(&fh->cap);
3094
		free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
L
Linus Torvalds 已提交
3095 3096 3097 3098 3099
	}
	if (fh->cap.read_buf) {
		buffer_release(&fh->cap,fh->cap.read_buf);
		kfree(fh->cap.read_buf);
	}
3100
	if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3101
		free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3102
	}
L
Linus Torvalds 已提交
3103 3104 3105

	/* stop vbi capture */
	if (check_btres(fh, RESOURCE_VBI)) {
3106
		videobuf_stop(&fh->vbi);
3107
		free_btres_lock(btv,fh,RESOURCE_VBI);
L
Linus Torvalds 已提交
3108 3109 3110
	}

	/* free stuff */
3111

L
Linus Torvalds 已提交
3112 3113 3114 3115 3116 3117
	videobuf_mmap_free(&fh->cap);
	videobuf_mmap_free(&fh->vbi);
	file->private_data = NULL;

	btv->users--;
	bttv_field_count(btv);
3118 3119

	if (!btv->users)
3120
		audio_mute(btv, btv->mute);
3121

3122 3123 3124
	v4l2_fh_del(&fh->fh);
	v4l2_fh_exit(&fh->fh);
	kfree(fh);
L
Linus Torvalds 已提交
3125 3126 3127 3128 3129 3130 3131 3132
	return 0;
}

static int
bttv_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct bttv_fh *fh = file->private_data;

3133
	dprintk("%d: mmap type=%s 0x%lx+%ld\n",
L
Linus Torvalds 已提交
3134 3135 3136 3137 3138
		fh->btv->c.nr, v4l2_type_names[fh->type],
		vma->vm_start, vma->vm_end - vma->vm_start);
	return videobuf_mmap_mapper(bttv_queue(fh),vma);
}

3139
static const struct v4l2_file_operations bttv_fops =
L
Linus Torvalds 已提交
3140
{
3141 3142 3143 3144 3145 3146 3147
	.owner		  = THIS_MODULE,
	.open		  = bttv_open,
	.release	  = bttv_release,
	.unlocked_ioctl	  = video_ioctl2,
	.read		  = bttv_read,
	.mmap		  = bttv_mmap,
	.poll		  = bttv_poll,
L
Linus Torvalds 已提交
3148 3149
};

3150
static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3151
	.vidioc_querycap                = bttv_querycap,
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
	.vidioc_enum_fmt_vid_cap        = bttv_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap           = bttv_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap         = bttv_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap           = bttv_s_fmt_vid_cap,
	.vidioc_enum_fmt_vid_overlay    = bttv_enum_fmt_vid_overlay,
	.vidioc_g_fmt_vid_overlay       = bttv_g_fmt_vid_overlay,
	.vidioc_try_fmt_vid_overlay     = bttv_try_fmt_vid_overlay,
	.vidioc_s_fmt_vid_overlay       = bttv_s_fmt_vid_overlay,
	.vidioc_g_fmt_vbi_cap           = bttv_g_fmt_vbi_cap,
	.vidioc_try_fmt_vbi_cap         = bttv_try_fmt_vbi_cap,
	.vidioc_s_fmt_vbi_cap           = bttv_s_fmt_vbi_cap,
3163
	.vidioc_g_pixelaspect           = bttv_g_pixelaspect,
3164 3165 3166 3167 3168
	.vidioc_reqbufs                 = bttv_reqbufs,
	.vidioc_querybuf                = bttv_querybuf,
	.vidioc_qbuf                    = bttv_qbuf,
	.vidioc_dqbuf                   = bttv_dqbuf,
	.vidioc_s_std                   = bttv_s_std,
3169
	.vidioc_g_std                   = bttv_g_std,
3170 3171 3172 3173 3174 3175 3176
	.vidioc_enum_input              = bttv_enum_input,
	.vidioc_g_input                 = bttv_g_input,
	.vidioc_s_input                 = bttv_s_input,
	.vidioc_streamon                = bttv_streamon,
	.vidioc_streamoff               = bttv_streamoff,
	.vidioc_g_tuner                 = bttv_g_tuner,
	.vidioc_s_tuner                 = bttv_s_tuner,
3177 3178
	.vidioc_g_selection             = bttv_g_selection,
	.vidioc_s_selection             = bttv_s_selection,
3179 3180 3181 3182 3183 3184 3185 3186
	.vidioc_g_fbuf                  = bttv_g_fbuf,
	.vidioc_s_fbuf                  = bttv_s_fbuf,
	.vidioc_overlay                 = bttv_overlay,
	.vidioc_g_parm                  = bttv_g_parm,
	.vidioc_g_frequency             = bttv_g_frequency,
	.vidioc_s_frequency             = bttv_s_frequency,
	.vidioc_log_status		= bttv_log_status,
	.vidioc_querystd		= bttv_querystd,
3187 3188
	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
3189
#ifdef CONFIG_VIDEO_ADV_DEBUG
3190 3191
	.vidioc_g_register		= bttv_g_register,
	.vidioc_s_register		= bttv_s_register,
3192
#endif
3193 3194 3195 3196 3197 3198
};

static struct video_device bttv_video_template = {
	.fops         = &bttv_fops,
	.ioctl_ops    = &bttv_ioctl_ops,
	.tvnorms      = BTTV_NORMS,
L
Linus Torvalds 已提交
3199 3200 3201 3202 3203
};

/* ----------------------------------------------------------------------- */
/* radio interface                                                         */

3204
static int radio_open(struct file *file)
L
Linus Torvalds 已提交
3205
{
3206
	struct video_device *vdev = video_devdata(file);
3207
	struct bttv *btv = video_drvdata(file);
3208
	struct bttv_fh *fh;
L
Linus Torvalds 已提交
3209

3210
	dprintk("open dev=%s\n", video_device_node_name(vdev));
L
Linus Torvalds 已提交
3211

3212
	dprintk("%d: open called (radio)\n", btv->c.nr);
3213 3214 3215

	/* allocate per filehandle data */
	fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3216
	if (unlikely(!fh))
3217 3218 3219
		return -ENOMEM;
	file->private_data = fh;
	*fh = btv->init;
3220
	v4l2_fh_init(&fh->fh, vdev);
3221

L
Linus Torvalds 已提交
3222
	btv->radio_user++;
3223
	audio_mute(btv, btv->mute);
3224

3225
	v4l2_fh_add(&fh->fh);
L
Linus Torvalds 已提交
3226

3227
	return 0;
L
Linus Torvalds 已提交
3228 3229
}

3230
static int radio_release(struct file *file)
L
Linus Torvalds 已提交
3231
{
3232 3233
	struct bttv_fh *fh = file->private_data;
	struct bttv *btv = fh->btv;
3234
	struct saa6588_command cmd;
L
Linus Torvalds 已提交
3235

3236
	file->private_data = NULL;
3237 3238
	v4l2_fh_del(&fh->fh);
	v4l2_fh_exit(&fh->fh);
3239 3240
	kfree(fh);

L
Linus Torvalds 已提交
3241
	btv->radio_user--;
3242

3243
	bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
3244

3245 3246
	if (btv->radio_user == 0)
		btv->has_radio_tuner = 0;
L
Linus Torvalds 已提交
3247 3248 3249
	return 0;
}

3250 3251 3252 3253
static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;
3254

3255 3256
	if (0 != t->index)
		return -EINVAL;
3257
	strscpy(t->name, "Radio", sizeof(t->name));
3258
	t->type = V4L2_TUNER_RADIO;
3259
	radio_enable(btv);
3260

3261
	bttv_call_all(btv, tuner, g_tuner, t);
3262 3263 3264 3265

	if (btv->audio_mode_gpio)
		btv->audio_mode_gpio(btv, t, 0);

3266 3267 3268
	if (btv->has_tea575x)
		return snd_tea575x_g_tuner(&btv->tea, t);

3269 3270 3271 3272
	return 0;
}

static int radio_s_tuner(struct file *file, void *priv,
3273
					const struct v4l2_tuner *t)
3274 3275 3276 3277 3278 3279 3280
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;

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

3281
	radio_enable(btv);
3282
	bttv_call_all(btv, tuner, s_tuner, t);
3283 3284 3285
	return 0;
}

3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309
static int radio_s_hw_freq_seek(struct file *file, void *priv,
					const struct v4l2_hw_freq_seek *a)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;

	if (btv->has_tea575x)
		return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);

	return -ENOTTY;
}

static int radio_enum_freq_bands(struct file *file, void *priv,
					 struct v4l2_frequency_band *band)
{
	struct bttv_fh *fh = priv;
	struct bttv *btv = fh->btv;

	if (btv->has_tea575x)
		return snd_tea575x_enum_freq_bands(&btv->tea, band);

	return -ENOTTY;
}

3310 3311 3312
static ssize_t radio_read(struct file *file, char __user *data,
			 size_t count, loff_t *ppos)
{
3313 3314
	struct bttv_fh *fh = file->private_data;
	struct bttv *btv = fh->btv;
3315
	struct saa6588_command cmd;
3316 3317 3318

	cmd.block_count = count / 3;
	cmd.nonblocking = file->f_flags & O_NONBLOCK;
3319 3320 3321
	cmd.buffer = data;
	cmd.instance = file;
	cmd.result = -ENODEV;
3322
	radio_enable(btv);
3323

3324
	bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
3325 3326 3327 3328

	return cmd.result;
}

A
Al Viro 已提交
3329
static __poll_t radio_poll(struct file *file, poll_table *wait)
3330
{
3331 3332
	struct bttv_fh *fh = file->private_data;
	struct bttv *btv = fh->btv;
A
Al Viro 已提交
3333
	__poll_t req_events = poll_requested_events(wait);
3334
	struct saa6588_command cmd;
A
Al Viro 已提交
3335
	__poll_t res = 0;
3336 3337

	if (v4l2_event_pending(&fh->fh))
3338 3339
		res = EPOLLPRI;
	else if (req_events & EPOLLPRI)
3340
		poll_wait(file, &fh->fh.wait, wait);
3341
	radio_enable(btv);
3342 3343
	cmd.instance = file;
	cmd.event_list = wait;
A
Al Viro 已提交
3344
	cmd.poll_mask = res;
3345
	bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
3346

A
Al Viro 已提交
3347
	return cmd.poll_mask;
3348 3349
}

3350
static const struct v4l2_file_operations radio_fops =
L
Linus Torvalds 已提交
3351 3352 3353
{
	.owner	  = THIS_MODULE,
	.open	  = radio_open,
3354
	.read     = radio_read,
L
Linus Torvalds 已提交
3355
	.release  = radio_release,
3356
	.unlocked_ioctl = video_ioctl2,
3357
	.poll     = radio_poll,
L
Linus Torvalds 已提交
3358 3359
};

3360
static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3361
	.vidioc_querycap        = bttv_querycap,
3362
	.vidioc_log_status	= bttv_log_status,
3363 3364
	.vidioc_g_tuner         = radio_g_tuner,
	.vidioc_s_tuner         = radio_s_tuner,
3365 3366
	.vidioc_g_frequency     = bttv_g_frequency,
	.vidioc_s_frequency     = bttv_s_frequency,
3367 3368
	.vidioc_s_hw_freq_seek	= radio_s_hw_freq_seek,
	.vidioc_enum_freq_bands	= radio_enum_freq_bands,
3369 3370
	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
L
Linus Torvalds 已提交
3371 3372
};

3373 3374 3375 3376 3377
static struct video_device radio_template = {
	.fops      = &radio_fops,
	.ioctl_ops = &radio_ioctl_ops,
};

L
Linus Torvalds 已提交
3378 3379 3380
/* ----------------------------------------------------------------------- */
/* some debug code                                                         */

A
Adrian Bunk 已提交
3381
static int bttv_risc_decode(u32 risc)
L
Linus Torvalds 已提交
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
{
	static char *instr[16] = {
		[ BT848_RISC_WRITE     >> 28 ] = "write",
		[ BT848_RISC_SKIP      >> 28 ] = "skip",
		[ BT848_RISC_WRITEC    >> 28 ] = "writec",
		[ BT848_RISC_JUMP      >> 28 ] = "jump",
		[ BT848_RISC_SYNC      >> 28 ] = "sync",
		[ BT848_RISC_WRITE123  >> 28 ] = "write123",
		[ BT848_RISC_SKIP123   >> 28 ] = "skip123",
		[ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
	};
	static int incr[16] = {
		[ BT848_RISC_WRITE     >> 28 ] = 2,
		[ BT848_RISC_JUMP      >> 28 ] = 2,
		[ BT848_RISC_SYNC      >> 28 ] = 2,
		[ BT848_RISC_WRITE123  >> 28 ] = 5,
		[ BT848_RISC_SKIP123   >> 28 ] = 2,
		[ BT848_RISC_WRITE1S23 >> 28 ] = 3,
	};
	static char *bits[] = {
		"be0",  "be1",  "be2",  "be3/resync",
		"set0", "set1", "set2", "set3",
		"clr0", "clr1", "clr2", "clr3",
		"irq",  "res",  "eol",  "sol",
	};
	int i;

3409
	pr_cont("0x%08x [ %s", risc,
L
Linus Torvalds 已提交
3410 3411 3412
	       instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
	for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
		if (risc & (1 << (i + 12)))
3413 3414
			pr_cont(" %s", bits[i]);
	pr_cont(" count=%d ]\n", risc & 0xfff);
L
Linus Torvalds 已提交
3415 3416 3417
	return incr[risc >> 28] ? incr[risc >> 28] : 1;
}

A
Adrian Bunk 已提交
3418 3419
static void bttv_risc_disasm(struct bttv *btv,
			     struct btcx_riscmem *risc)
L
Linus Torvalds 已提交
3420 3421 3422
{
	unsigned int i,j,n;

3423 3424
	pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
		btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
L
Linus Torvalds 已提交
3425
	for (i = 0; i < (risc->size >> 2); i += n) {
3426 3427 3428
		pr_info("%s:   0x%lx: ",
			btv->c.v4l2_dev.name,
			(unsigned long)(risc->dma + (i<<2)));
3429
		n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
L
Linus Torvalds 已提交
3430
		for (j = 1; j < n; j++)
3431 3432 3433 3434
			pr_info("%s:   0x%lx: 0x%08x [ arg #%d ]\n",
				btv->c.v4l2_dev.name,
				(unsigned long)(risc->dma + ((i+j)<<2)),
				risc->cpu[i+j], j);
L
Linus Torvalds 已提交
3435 3436 3437 3438 3439 3440 3441
		if (0 == risc->cpu[i])
			break;
	}
}

static void bttv_print_riscaddr(struct bttv *btv)
{
3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453
	pr_info("  main: %08llx\n", (unsigned long long)btv->main.dma);
	pr_info("  vbi : o=%08llx e=%08llx\n",
		btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
		btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
	pr_info("  cap : o=%08llx e=%08llx\n",
		btv->curr.top
		? (unsigned long long)btv->curr.top->top.dma : 0,
		btv->curr.bottom
		? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
	pr_info("  scr : o=%08llx e=%08llx\n",
		btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
		btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
L
Linus Torvalds 已提交
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485
	bttv_risc_disasm(btv, &btv->main);
}

/* ----------------------------------------------------------------------- */
/* irq handler                                                             */

static char *irq_name[] = {
	"FMTCHG",  // format change detected (525 vs. 625)
	"VSYNC",   // vertical sync (new field)
	"HSYNC",   // horizontal sync
	"OFLOW",   // chroma/luma AGC overflow
	"HLOCK",   // horizontal lock changed
	"VPRES",   // video presence changed
	"6", "7",
	"I2CDONE", // hw irc operation finished
	"GPINT",   // gpio port triggered irq
	"10",
	"RISCI",   // risc instruction triggered irq
	"FBUS",    // pixel data fifo dropped data (high pci bus latencies)
	"FTRGT",   // pixel data fifo overrun
	"FDSR",    // fifo data stream resyncronisation
	"PPERR",   // parity error (data transfer)
	"RIPERR",  // parity error (read risc instructions)
	"PABORT",  // pci abort
	"OCERR",   // risc instruction error
	"SCERR",   // syncronisation error
};

static void bttv_print_irqbits(u32 print, u32 mark)
{
	unsigned int i;

3486
	pr_cont("bits:");
L
Linus Torvalds 已提交
3487 3488
	for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
		if (print & (1 << i))
3489
			pr_cont(" %s", irq_name[i]);
L
Linus Torvalds 已提交
3490
		if (mark & (1 << i))
3491
			pr_cont("*");
L
Linus Torvalds 已提交
3492 3493 3494 3495 3496
	}
}

static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
{
3497 3498 3499 3500 3501 3502
	pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
		btv->c.nr,
		(unsigned long)btv->main.dma,
		(unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
		(unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
		(unsigned long)rc);
L
Linus Torvalds 已提交
3503 3504

	if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3505
		pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
3506
			  btv->c.nr);
L
Linus Torvalds 已提交
3507 3508
		return;
	}
3509 3510
	pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
		  btv->c.nr);
3511
	pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
3512
		  btv->c.nr);
L
Linus Torvalds 已提交
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
	dump_stack();
}

static int
bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
{
	struct bttv_buffer *item;

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

	/* capture request ? */
	if (!list_empty(&btv->capture)) {
		set->frame_irq = 1;
		item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
		if (V4L2_FIELD_HAS_TOP(item->vb.field))
			set->top    = item;
		if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
			set->bottom = item;

		/* capture request for other field ? */
		if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
		    (item->vb.queue.next != &btv->capture)) {
			item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562
			/* Mike Isely <isely@pobox.com> - Only check
			 * and set up the bottom field in the logic
			 * below.  Don't ever do the top field.  This
			 * of course means that if we set up the
			 * bottom field in the above code that we'll
			 * actually skip a field.  But that's OK.
			 * Having processed only a single buffer this
			 * time, then the next time around the first
			 * available buffer should be for a top field.
			 * That will then cause us here to set up a
			 * top then a bottom field in the normal way.
			 * The alternative to this understanding is
			 * that we set up the second available buffer
			 * as a top field, but that's out of order
			 * since this driver always processes the top
			 * field first - the effect will be the two
			 * buffers being returned in the wrong order,
			 * with the second buffer also being delayed
			 * by one field time (owing to the fifo nature
			 * of videobuf).  Worse still, we'll be stuck
			 * doing fields out of order now every time
			 * until something else causes a field to be
			 * dropped.  By effectively forcing a field to
			 * drop this way then we always get back into
			 * sync within a single frame time.  (Out of
			 * order fields can screw up deinterlacing
			 * algorithms.) */
L
Linus Torvalds 已提交
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
			if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
				if (NULL == set->bottom &&
				    V4L2_FIELD_BOTTOM == item->vb.field) {
					set->bottom = item;
				}
				if (NULL != set->top  &&  NULL != set->bottom)
					set->top_irq = 2;
			}
		}
	}

	/* screen overlay ? */
	if (NULL != btv->screen) {
		if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
			if (NULL == set->top && NULL == set->bottom) {
				set->top    = btv->screen;
				set->bottom = btv->screen;
			}
		} else {
			if (V4L2_FIELD_TOP == btv->screen->vb.field &&
			    NULL == set->top) {
				set->top = btv->screen;
			}
			if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
			    NULL == set->bottom) {
				set->bottom = btv->screen;
			}
		}
	}

3593 3594 3595
	dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
		btv->c.nr, set->top, set->bottom,
		btv->screen, set->frame_irq, set->top_irq);
L
Linus Torvalds 已提交
3596 3597 3598 3599 3600 3601 3602
	return 0;
}

static void
bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
		      struct bttv_buffer_set *curr, unsigned int state)
{
3603
	u64 ts = ktime_get_ns();
L
Linus Torvalds 已提交
3604 3605 3606 3607

	if (wakeup->top == wakeup->bottom) {
		if (NULL != wakeup->top && curr->top != wakeup->top) {
			if (irq_debug > 1)
3608 3609
				pr_debug("%d: wakeup: both=%p\n",
					 btv->c.nr, wakeup->top);
L
Linus Torvalds 已提交
3610 3611 3612 3613 3614 3615 3616 3617
			wakeup->top->vb.ts = ts;
			wakeup->top->vb.field_count = btv->field_count;
			wakeup->top->vb.state = state;
			wake_up(&wakeup->top->vb.done);
		}
	} else {
		if (NULL != wakeup->top && curr->top != wakeup->top) {
			if (irq_debug > 1)
3618 3619
				pr_debug("%d: wakeup: top=%p\n",
					 btv->c.nr, wakeup->top);
L
Linus Torvalds 已提交
3620 3621 3622 3623 3624 3625 3626
			wakeup->top->vb.ts = ts;
			wakeup->top->vb.field_count = btv->field_count;
			wakeup->top->vb.state = state;
			wake_up(&wakeup->top->vb.done);
		}
		if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
			if (irq_debug > 1)
3627 3628
				pr_debug("%d: wakeup: bottom=%p\n",
					 btv->c.nr, wakeup->bottom);
L
Linus Torvalds 已提交
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
			wakeup->bottom->vb.ts = ts;
			wakeup->bottom->vb.field_count = btv->field_count;
			wakeup->bottom->vb.state = state;
			wake_up(&wakeup->bottom->vb.done);
		}
	}
}

static void
bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
		    unsigned int state)
{
	if (NULL == wakeup)
		return;

3644
	wakeup->vb.ts = ktime_get_ns();
L
Linus Torvalds 已提交
3645 3646 3647 3648 3649
	wakeup->vb.field_count = btv->field_count;
	wakeup->vb.state = state;
	wake_up(&wakeup->vb.done);
}

3650
static void bttv_irq_timeout(struct timer_list *t)
L
Linus Torvalds 已提交
3651
{
3652
	struct bttv *btv = from_timer(btv, t, timeout);
L
Linus Torvalds 已提交
3653 3654 3655 3656 3657 3658
	struct bttv_buffer_set old,new;
	struct bttv_buffer *ovbi;
	struct bttv_buffer *item;
	unsigned long flags;

	if (bttv_verbose) {
3659 3660 3661
		pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
			btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
			btread(BT848_RISC_COUNT));
L
Linus Torvalds 已提交
3662
		bttv_print_irqbits(btread(BT848_INT_STAT),0);
3663
		pr_cont("\n");
L
Linus Torvalds 已提交
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679
	}

	spin_lock_irqsave(&btv->s_lock,flags);

	/* deactivate stuff */
	memset(&new,0,sizeof(new));
	old  = btv->curr;
	ovbi = btv->cvbi;
	btv->curr = new;
	btv->cvbi = NULL;
	btv->loop_irq = 0;
	bttv_buffer_activate_video(btv, &new);
	bttv_buffer_activate_vbi(btv,   NULL);
	bttv_set_dma(btv, 0);

	/* wake up */
3680 3681
	bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
	bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
L
Linus Torvalds 已提交
3682 3683 3684 3685 3686

	/* cancel all outstanding capture / vbi requests */
	while (!list_empty(&btv->capture)) {
		item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
		list_del(&item->vb.queue);
3687
		item->vb.state = VIDEOBUF_ERROR;
L
Linus Torvalds 已提交
3688 3689 3690 3691 3692
		wake_up(&item->vb.done);
	}
	while (!list_empty(&btv->vcapture)) {
		item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
		list_del(&item->vb.queue);
3693
		item->vb.state = VIDEOBUF_ERROR;
L
Linus Torvalds 已提交
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
		wake_up(&item->vb.done);
	}

	btv->errors++;
	spin_unlock_irqrestore(&btv->s_lock,flags);
}

static void
bttv_irq_wakeup_top(struct bttv *btv)
{
	struct bttv_buffer *wakeup = btv->curr.top;

	if (NULL == wakeup)
		return;

	spin_lock(&btv->s_lock);
	btv->curr.top_irq = 0;
	btv->curr.top = NULL;
	bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);

3714
	wakeup->vb.ts = ktime_get_ns();
L
Linus Torvalds 已提交
3715
	wakeup->vb.field_count = btv->field_count;
3716
	wakeup->vb.state = VIDEOBUF_DONE;
L
Linus Torvalds 已提交
3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764
	wake_up(&wakeup->vb.done);
	spin_unlock(&btv->s_lock);
}

static inline int is_active(struct btcx_riscmem *risc, u32 rc)
{
	if (rc < risc->dma)
		return 0;
	if (rc > risc->dma + risc->size)
		return 0;
	return 1;
}

static void
bttv_irq_switch_video(struct bttv *btv)
{
	struct bttv_buffer_set new;
	struct bttv_buffer_set old;
	dma_addr_t rc;

	spin_lock(&btv->s_lock);

	/* new buffer set */
	bttv_irq_next_video(btv, &new);
	rc = btread(BT848_RISC_COUNT);
	if ((btv->curr.top    && is_active(&btv->curr.top->top,       rc)) ||
	    (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
		btv->framedrop++;
		if (debug_latency)
			bttv_irq_debug_low_latency(btv, rc);
		spin_unlock(&btv->s_lock);
		return;
	}

	/* switch over */
	old = btv->curr;
	btv->curr = new;
	btv->loop_irq &= ~1;
	bttv_buffer_activate_video(btv, &new);
	bttv_set_dma(btv, 0);

	/* switch input */
	if (UNSET != btv->new_input) {
		video_mux(btv,btv->new_input);
		btv->new_input = UNSET;
	}

	/* wake up finished buffers */
3765
	bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
L
Linus Torvalds 已提交
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
	spin_unlock(&btv->s_lock);
}

static void
bttv_irq_switch_vbi(struct bttv *btv)
{
	struct bttv_buffer *new = NULL;
	struct bttv_buffer *old;
	u32 rc;

	spin_lock(&btv->s_lock);

	if (!list_empty(&btv->vcapture))
		new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
	old = btv->cvbi;

	rc = btread(BT848_RISC_COUNT);
	if (NULL != old && (is_active(&old->top,    rc) ||
			    is_active(&old->bottom, rc))) {
		btv->framedrop++;
		if (debug_latency)
			bttv_irq_debug_low_latency(btv, rc);
		spin_unlock(&btv->s_lock);
		return;
	}

	/* switch */
	btv->cvbi = new;
	btv->loop_irq &= ~4;
	bttv_buffer_activate_vbi(btv, new);
	bttv_set_dma(btv, 0);

3798
	bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
L
Linus Torvalds 已提交
3799 3800 3801
	spin_unlock(&btv->s_lock);
}

3802
static irqreturn_t bttv_irq(int irq, void *dev_id)
L
Linus Torvalds 已提交
3803 3804 3805 3806 3807 3808 3809 3810
{
	u32 stat,astat;
	u32 dstat;
	int count;
	struct bttv *btv;
	int handled = 0;

	btv=(struct bttv *)dev_id;
3811

L
Linus Torvalds 已提交
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825
	count=0;
	while (1) {
		/* get/clear interrupt status bits */
		stat=btread(BT848_INT_STAT);
		astat=stat&btread(BT848_INT_MASK);
		if (!astat)
			break;
		handled = 1;
		btwrite(stat,BT848_INT_STAT);

		/* get device status bits */
		dstat=btread(BT848_DSTATUS);

		if (irq_debug) {
3826 3827 3828
			pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
				 btv->c.nr, count, btv->field_count,
				 stat>>28, btread(BT848_RISC_COUNT));
L
Linus Torvalds 已提交
3829 3830
			bttv_print_irqbits(stat,astat);
			if (stat & BT848_INT_HLOCK)
3831 3832 3833
				pr_cont("   HLOC => %s",
					dstat & BT848_DSTATUS_HLOC
					? "yes" : "no");
L
Linus Torvalds 已提交
3834
			if (stat & BT848_INT_VPRES)
3835 3836 3837
				pr_cont("   PRES => %s",
					dstat & BT848_DSTATUS_PRES
					? "yes" : "no");
L
Linus Torvalds 已提交
3838
			if (stat & BT848_INT_FMTCHG)
3839 3840 3841 3842
				pr_cont("   NUML => %s",
					dstat & BT848_DSTATUS_NUML
					? "625" : "525");
			pr_cont("\n");
L
Linus Torvalds 已提交
3843 3844 3845
		}

		if (astat&BT848_INT_VSYNC)
3846
			btv->field_count++;
L
Linus Torvalds 已提交
3847

3848 3849
		if ((astat & BT848_INT_GPINT) && btv->remote) {
			bttv_input_irq(btv);
L
Linus Torvalds 已提交
3850 3851 3852 3853 3854 3855 3856
		}

		if (astat & BT848_INT_I2CDONE) {
			btv->i2c_done = stat;
			wake_up(&btv->i2c_queue);
		}

3857
		if ((astat & BT848_INT_RISCI)  &&  (stat & (4<<28)))
L
Linus Torvalds 已提交
3858 3859
			bttv_irq_switch_vbi(btv);

3860
		if ((astat & BT848_INT_RISCI)  &&  (stat & (2<<28)))
L
Linus Torvalds 已提交
3861 3862
			bttv_irq_wakeup_top(btv);

3863
		if ((astat & BT848_INT_RISCI)  &&  (stat & (1<<28)))
L
Linus Torvalds 已提交
3864 3865 3866
			bttv_irq_switch_video(btv);

		if ((astat & BT848_INT_HLOCK)  &&  btv->opt_automute)
3867 3868
			/* trigger automute */
			audio_mux_gpio(btv, btv->audio_input, btv->mute);
L
Linus Torvalds 已提交
3869 3870

		if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3871 3872 3873 3874 3875
			pr_info("%d: %s%s @ %08x,",
				btv->c.nr,
				(astat & BT848_INT_SCERR) ? "SCERR" : "",
				(astat & BT848_INT_OCERR) ? "OCERR" : "",
				btread(BT848_RISC_COUNT));
L
Linus Torvalds 已提交
3876
			bttv_print_irqbits(stat,astat);
3877
			pr_cont("\n");
L
Linus Torvalds 已提交
3878 3879 3880 3881
			if (bttv_debug)
				bttv_print_riscaddr(btv);
		}
		if (fdsr && astat & BT848_INT_FDSR) {
3882 3883
			pr_info("%d: FDSR @ %08x\n",
				btv->c.nr, btread(BT848_RISC_COUNT));
L
Linus Torvalds 已提交
3884 3885 3886 3887 3888 3889
			if (bttv_debug)
				bttv_print_riscaddr(btv);
		}

		count++;
		if (count > 4) {
3890 3891

			if (count > 8 || !(astat & BT848_INT_GPINT)) {
3892
				btwrite(0, BT848_INT_MASK);
3893

3894 3895
				pr_err("%d: IRQ lockup, cleared int mask [",
				       btv->c.nr);
3896
			} else {
3897 3898
				pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
				       btv->c.nr);
3899 3900 3901

				btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
						BT848_INT_MASK);
3902
			}
3903

L
Linus Torvalds 已提交
3904
			bttv_print_irqbits(stat,astat);
3905

3906
			pr_cont("]\n");
L
Linus Torvalds 已提交
3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
		}
	}
	btv->irq_total++;
	if (handled)
		btv->irq_me++;
	return IRQ_RETVAL(handled);
}


/* ----------------------------------------------------------------------- */
3917
/* initialization                                                          */
L
Linus Torvalds 已提交
3918

H
Hans Verkuil 已提交
3919 3920 3921 3922
static void vdev_init(struct bttv *btv,
		      struct video_device *vfd,
		      const struct video_device *template,
		      const char *type_name)
L
Linus Torvalds 已提交
3923 3924
{
	*vfd = *template;
3925
	vfd->v4l2_dev = &btv->c.v4l2_dev;
H
Hans Verkuil 已提交
3926
	vfd->release = video_device_release_empty;
3927
	video_set_drvdata(vfd, btv);
L
Linus Torvalds 已提交
3928 3929
	snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
		 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3930
		 type_name, bttv_tvcards[btv->c.type].name);
3931 3932 3933 3934 3935 3936
	if (btv->tuner_type == TUNER_ABSENT) {
		v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
		v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
		v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
		v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
	}
L
Linus Torvalds 已提交
3937 3938 3939 3940
}

static void bttv_unregister_video(struct bttv *btv)
{
H
Hans Verkuil 已提交
3941 3942 3943
	video_unregister_device(&btv->video_dev);
	video_unregister_device(&btv->vbi_dev);
	video_unregister_device(&btv->radio_dev);
L
Linus Torvalds 已提交
3944 3945 3946
}

/* register video4linux devices */
3947
static int bttv_register_video(struct bttv *btv)
L
Linus Torvalds 已提交
3948
{
3949
	if (no_overlay > 0)
3950
		pr_notice("Overlay support disabled\n");
3951

L
Linus Torvalds 已提交
3952
	/* video */
H
Hans Verkuil 已提交
3953
	vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
3954

H
Hans Verkuil 已提交
3955
	if (video_register_device(&btv->video_dev, VFL_TYPE_GRABBER,
3956
				  video_nr[btv->c.nr]) < 0)
L
Linus Torvalds 已提交
3957
		goto err;
3958
	pr_info("%d: registered device %s\n",
H
Hans Verkuil 已提交
3959 3960
		btv->c.nr, video_device_node_name(&btv->video_dev));
	if (device_create_file(&btv->video_dev.dev,
3961
				     &dev_attr_card)<0) {
3962
		pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
3963 3964
		goto err;
	}
L
Linus Torvalds 已提交
3965 3966

	/* vbi */
H
Hans Verkuil 已提交
3967
	vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
3968

H
Hans Verkuil 已提交
3969
	if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
3970
				  vbi_nr[btv->c.nr]) < 0)
L
Linus Torvalds 已提交
3971
		goto err;
3972
	pr_info("%d: registered device %s\n",
H
Hans Verkuil 已提交
3973
		btv->c.nr, video_device_node_name(&btv->vbi_dev));
L
Linus Torvalds 已提交
3974

3975
	if (!btv->has_radio)
L
Linus Torvalds 已提交
3976 3977
		return 0;
	/* radio */
H
Hans Verkuil 已提交
3978 3979 3980
	vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
	btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
	if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
3981
				  radio_nr[btv->c.nr]) < 0)
L
Linus Torvalds 已提交
3982
		goto err;
3983
	pr_info("%d: registered device %s\n",
H
Hans Verkuil 已提交
3984
		btv->c.nr, video_device_node_name(&btv->radio_dev));
L
Linus Torvalds 已提交
3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999

	/* all done */
	return 0;

 err:
	bttv_unregister_video(btv);
	return -1;
}


/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
/* response on cards with no firmware is not enabled by OF */
static void pci_set_command(struct pci_dev *dev)
{
#if defined(__powerpc__)
4000
	unsigned int cmd;
L
Linus Torvalds 已提交
4001

4002 4003 4004
	pci_read_config_dword(dev, PCI_COMMAND, &cmd);
	cmd = (cmd | PCI_COMMAND_MEMORY );
	pci_write_config_dword(dev, PCI_COMMAND, cmd);
L
Linus Torvalds 已提交
4005 4006 4007
#endif
}

4008
static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
L
Linus Torvalds 已提交
4009
{
4010 4011 4012 4013 4014
	struct v4l2_frequency init_freq = {
		.tuner = 0,
		.type = V4L2_TUNER_ANALOG_TV,
		.frequency = 980,
	};
L
Linus Torvalds 已提交
4015 4016 4017
	int result;
	unsigned char lat;
	struct bttv *btv;
4018
	struct v4l2_ctrl_handler *hdl;
L
Linus Torvalds 已提交
4019 4020 4021

	if (bttv_num == BTTV_MAX)
		return -ENOMEM;
4022
	pr_info("Bt8xx card found (%d)\n", bttv_num);
4023
	bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4024
	if (btv == NULL) {
4025
		pr_err("out of memory\n");
4026 4027
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
4028
	btv->c.nr  = bttv_num;
4029 4030
	snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
			"bttv%d", btv->c.nr);
L
Linus Torvalds 已提交
4031 4032

	/* initialize structs / fill in defaults */
4033
	mutex_init(&btv->lock);
4034 4035 4036 4037 4038 4039
	spin_lock_init(&btv->s_lock);
	spin_lock_init(&btv->gpio_lock);
	init_waitqueue_head(&btv->i2c_queue);
	INIT_LIST_HEAD(&btv->c.subs);
	INIT_LIST_HEAD(&btv->capture);
	INIT_LIST_HEAD(&btv->vcapture);
L
Linus Torvalds 已提交
4040

4041
	timer_setup(&btv->timeout, bttv_irq_timeout, 0);
L
Linus Torvalds 已提交
4042

4043 4044
	btv->i2c_rc = -1;
	btv->tuner_type  = UNSET;
L
Linus Torvalds 已提交
4045 4046 4047 4048 4049
	btv->new_input   = UNSET;
	btv->has_radio=radio[btv->c.nr];

	/* pci stuff (init, get irq/mmio, ... */
	btv->c.pci = dev;
4050
	btv->id  = dev->device;
L
Linus Torvalds 已提交
4051
	if (pci_enable_device(dev)) {
4052
		pr_warn("%d: Can't enable device\n", btv->c.nr);
L
Linus Torvalds 已提交
4053 4054
		return -EIO;
	}
4055
	if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4056
		pr_warn("%d: No suitable DMA available\n", btv->c.nr);
L
Linus Torvalds 已提交
4057
		return -EIO;
4058
	}
L
Linus Torvalds 已提交
4059 4060
	if (!request_mem_region(pci_resource_start(dev,0),
				pci_resource_len(dev,0),
4061
				btv->c.v4l2_dev.name)) {
4062 4063 4064
		pr_warn("%d: can't request iomem (0x%llx)\n",
			btv->c.nr,
			(unsigned long long)pci_resource_start(dev, 0));
L
Linus Torvalds 已提交
4065 4066
		return -EBUSY;
	}
4067
	pci_set_master(dev);
L
Linus Torvalds 已提交
4068
	pci_set_command(dev);
4069 4070 4071

	result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
	if (result < 0) {
4072
		pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
4073 4074
		goto fail0;
	}
4075 4076 4077 4078
	hdl = &btv->ctrl_handler;
	v4l2_ctrl_handler_init(hdl, 20);
	btv->c.v4l2_dev.ctrl_handler = hdl;
	v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
L
Linus Torvalds 已提交
4079

B
Bjørn Mork 已提交
4080
	btv->revision = dev->revision;
4081
	pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4082 4083 4084 4085
	pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
		bttv_num, btv->id, btv->revision, pci_name(dev),
		btv->c.pci->irq, lat,
		(unsigned long long)pci_resource_start(dev, 0));
L
Linus Torvalds 已提交
4086 4087
	schedule();

4088 4089
	btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
	if (NULL == btv->bt848_mmio) {
4090
		pr_err("%d: ioremap() failed\n", btv->c.nr);
L
Linus Torvalds 已提交
4091 4092 4093 4094
		result = -EIO;
		goto fail1;
	}

4095
	/* identify card */
L
Linus Torvalds 已提交
4096 4097
	bttv_idcard(btv);

4098
	/* disable irqs, register irq handler */
L
Linus Torvalds 已提交
4099
	btwrite(0, BT848_INT_MASK);
4100
	result = request_irq(btv->c.pci->irq, bttv_irq,
4101
	    IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
4102
	if (result < 0) {
4103 4104
		pr_err("%d: can't get IRQ %d\n",
		       bttv_num, btv->c.pci->irq);
L
Linus Torvalds 已提交
4105
		goto fail1;
4106
	}
L
Linus Torvalds 已提交
4107 4108 4109 4110

	if (0 != bttv_handle_chipset(btv)) {
		result = -EIO;
		goto fail2;
4111
	}
L
Linus Torvalds 已提交
4112 4113 4114

	/* init options from insmod args */
	btv->opt_combfilter = combfilter;
4115 4116
	bttv_ctrl_combfilter.def = combfilter;
	bttv_ctrl_lumafilter.def = lumafilter;
L
Linus Torvalds 已提交
4117
	btv->opt_automute   = automute;
4118 4119
	bttv_ctrl_automute.def = automute;
	bttv_ctrl_agc_crush.def = agc_crush;
L
Linus Torvalds 已提交
4120
	btv->opt_vcr_hack   = vcr_hack;
4121 4122 4123
	bttv_ctrl_vcr_hack.def = vcr_hack;
	bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
	bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
4124
	btv->opt_uv_ratio   = uv_ratio;
4125 4126 4127
	bttv_ctrl_uv_ratio.def = uv_ratio;
	bttv_ctrl_full_luma.def = full_luma_range;
	bttv_ctrl_coring.def = coring;
L
Linus Torvalds 已提交
4128 4129 4130 4131 4132

	/* fill struct bttv with some useful defaults */
	btv->init.btv         = btv;
	btv->init.ov.w.width  = 320;
	btv->init.ov.w.height = 240;
4133
	btv->init.fmt         = format_by_fourcc(V4L2_PIX_FMT_BGR24);
L
Linus Torvalds 已提交
4134 4135
	btv->init.width       = 320;
	btv->init.height      = 240;
4136 4137 4138
	btv->init.ov.w.width  = 320;
	btv->init.ov.w.height = 240;
	btv->init.ov.field    = V4L2_FIELD_INTERLACED;
L
Linus Torvalds 已提交
4139 4140
	btv->input = 0;

4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
	v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
		V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
	if (btv->volume_gpio)
		v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
			V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
	v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);

L
Linus Torvalds 已提交
4169
	/* initialize hardware */
4170 4171
	if (bttv_gpio)
		bttv_gpio_tracking(btv,"pre-init");
L
Linus Torvalds 已提交
4172 4173 4174 4175 4176

	bttv_risc_init_main(btv);
	init_bt848(btv);

	/* gpio */
4177 4178 4179 4180
	btwrite(0x00, BT848_GPIO_REG_INP);
	btwrite(0x00, BT848_GPIO_OUT_EN);
	if (bttv_verbose)
		bttv_gpio_tracking(btv,"init");
L
Linus Torvalds 已提交
4181

4182 4183
	/* needs to be done before i2c is registered */
	bttv_init_card1(btv);
L
Linus Torvalds 已提交
4184

4185 4186
	/* register i2c + gpio */
	init_bttv_i2c(btv);
L
Linus Torvalds 已提交
4187

4188 4189
	/* some card-specific stuff (needs working i2c) */
	bttv_init_card2(btv);
4190
	bttv_init_tuner(btv);
4191 4192 4193 4194
	if (btv->tuner_type != TUNER_ABSENT) {
		bttv_set_frequency(btv, &init_freq);
		btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
	}
4195
	btv->std = V4L2_STD_PAL;
L
Linus Torvalds 已提交
4196
	init_irqreg(btv);
4197 4198
	if (!bttv_tvcards[btv->c.type].no_video)
		v4l2_ctrl_handler_setup(hdl);
4199 4200 4201 4202
	if (hdl->error) {
		result = hdl->error;
		goto fail2;
	}
4203 4204 4205
	/* mute device */
	audio_mute(btv, 1);

4206
	/* register video4linux + input */
L
Linus Torvalds 已提交
4207
	if (!bttv_tvcards[btv->c.type].no_video) {
4208
		v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
4209
				v4l2_ctrl_radio_filter, false);
4210 4211 4212 4213
		if (btv->radio_ctrl_handler.error) {
			result = btv->radio_ctrl_handler.error;
			goto fail2;
		}
4214
		set_input(btv, 0, btv->tvnorm);
4215 4216 4217 4218
		bttv_crop_reset(&btv->crop[0], btv->tvnorm);
		btv->crop[1] = btv->crop[0]; /* current = default */
		disclaim_vbi_lines(btv);
		disclaim_video_lines(btv);
4219
		bttv_register_video(btv);
L
Linus Torvalds 已提交
4220 4221
	}

4222 4223
	/* add subdevices and autoload dvb-bt8xx if needed */
	if (bttv_tvcards[btv->c.type].has_dvb) {
L
Linus Torvalds 已提交
4224
		bttv_sub_add_device(&btv->c, "dvb");
4225 4226
		request_modules(btv);
	}
L
Linus Torvalds 已提交
4227

4228 4229 4230 4231
	if (!disable_ir) {
		init_bttv_i2c_ir(btv);
		bttv_input_init(btv);
	}
4232

L
Linus Torvalds 已提交
4233 4234
	/* everything is fine */
	bttv_num++;
4235
	return 0;
L
Linus Torvalds 已提交
4236

4237
fail2:
4238
	free_irq(btv->c.pci->irq,btv);
L
Linus Torvalds 已提交
4239

4240
fail1:
4241 4242
	v4l2_ctrl_handler_free(&btv->ctrl_handler);
	v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
4243 4244 4245
	v4l2_device_unregister(&btv->c.v4l2_dev);

fail0:
L
Linus Torvalds 已提交
4246 4247 4248 4249
	if (btv->bt848_mmio)
		iounmap(btv->bt848_mmio);
	release_mem_region(pci_resource_start(btv->c.pci,0),
			   pci_resource_len(btv->c.pci,0));
4250
	pci_disable_device(btv->c.pci);
L
Linus Torvalds 已提交
4251 4252 4253
	return result;
}

4254
static void bttv_remove(struct pci_dev *pci_dev)
L
Linus Torvalds 已提交
4255
{
4256 4257
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
	struct bttv *btv = to_bttv(v4l2_dev);
L
Linus Torvalds 已提交
4258 4259

	if (bttv_verbose)
4260
		pr_info("%d: unloading\n", btv->c.nr);
L
Linus Torvalds 已提交
4261

4262 4263 4264
	if (bttv_tvcards[btv->c.type].has_dvb)
		flush_request_modules(btv);

4265
	/* shutdown everything (DMA+IRQs) */
L
Linus Torvalds 已提交
4266 4267 4268 4269 4270 4271 4272 4273 4274
	btand(~15, BT848_GPIO_DMA_CTL);
	btwrite(0, BT848_INT_MASK);
	btwrite(~0x0, BT848_INT_STAT);
	btwrite(0x0, BT848_GPIO_OUT_EN);
	if (bttv_gpio)
		bttv_gpio_tracking(btv,"cleanup");

	/* tell gpio modules we are leaving ... */
	btv->shutdown=1;
4275
	bttv_input_fini(btv);
4276
	bttv_sub_del_devices(&btv->c);
L
Linus Torvalds 已提交
4277

4278
	/* unregister i2c_bus + input */
L
Linus Torvalds 已提交
4279 4280 4281 4282 4283 4284
	fini_bttv_i2c(btv);

	/* unregister video4linux */
	bttv_unregister_video(btv);

	/* free allocated memory */
4285 4286
	v4l2_ctrl_handler_free(&btv->ctrl_handler);
	v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
L
Linus Torvalds 已提交
4287 4288
	btcx_riscmem_free(btv->c.pci,&btv->main);

4289
	/* free resources */
4290
	free_irq(btv->c.pci->irq,btv);
L
Linus Torvalds 已提交
4291
	iounmap(btv->bt848_mmio);
4292 4293
	release_mem_region(pci_resource_start(btv->c.pci,0),
			   pci_resource_len(btv->c.pci,0));
4294
	pci_disable_device(btv->c.pci);
L
Linus Torvalds 已提交
4295

4296
	v4l2_device_unregister(&btv->c.v4l2_dev);
4297 4298 4299
	bttvs[btv->c.nr] = NULL;
	kfree(btv);

4300
	return;
L
Linus Torvalds 已提交
4301 4302
}

4303
#ifdef CONFIG_PM
L
Linus Torvalds 已提交
4304 4305
static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
{
4306 4307
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
	struct bttv *btv = to_bttv(v4l2_dev);
L
Linus Torvalds 已提交
4308 4309 4310
	struct bttv_buffer_set idle;
	unsigned long flags;

4311
	dprintk("%d: suspend %d\n", btv->c.nr, state.event);
L
Linus Torvalds 已提交
4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341

	/* stop dma + irqs */
	spin_lock_irqsave(&btv->s_lock,flags);
	memset(&idle, 0, sizeof(idle));
	btv->state.video = btv->curr;
	btv->state.vbi   = btv->cvbi;
	btv->state.loop_irq = btv->loop_irq;
	btv->curr = idle;
	btv->loop_irq = 0;
	bttv_buffer_activate_video(btv, &idle);
	bttv_buffer_activate_vbi(btv, NULL);
	bttv_set_dma(btv, 0);
	btwrite(0, BT848_INT_MASK);
	spin_unlock_irqrestore(&btv->s_lock,flags);

	/* save bt878 state */
	btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
	btv->state.gpio_data   = gpio_read();

	/* save pci state */
	pci_save_state(pci_dev);
	if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
		pci_disable_device(pci_dev);
		btv->state.disabled = 1;
	}
	return 0;
}

static int bttv_resume(struct pci_dev *pci_dev)
{
4342 4343
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
	struct bttv *btv = to_bttv(v4l2_dev);
L
Linus Torvalds 已提交
4344
	unsigned long flags;
4345
	int err;
L
Linus Torvalds 已提交
4346

4347
	dprintk("%d: resume\n", btv->c.nr);
L
Linus Torvalds 已提交
4348 4349 4350

	/* restore pci state */
	if (btv->state.disabled) {
4351 4352
		err=pci_enable_device(pci_dev);
		if (err) {
4353
			pr_warn("%d: Can't enable device\n", btv->c.nr);
4354 4355
			return err;
		}
L
Linus Torvalds 已提交
4356 4357
		btv->state.disabled = 0;
	}
4358 4359 4360
	err=pci_set_power_state(pci_dev, PCI_D0);
	if (err) {
		pci_disable_device(pci_dev);
4361
		pr_warn("%d: Can't enable device\n", btv->c.nr);
4362 4363 4364 4365
		btv->state.disabled = 1;
		return err;
	}

L
Linus Torvalds 已提交
4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383
	pci_restore_state(pci_dev);

	/* restore bt878 state */
	bttv_reinit_bt848(btv);
	gpio_inout(0xffffff, btv->state.gpio_enable);
	gpio_write(btv->state.gpio_data);

	/* restart dma */
	spin_lock_irqsave(&btv->s_lock,flags);
	btv->curr = btv->state.video;
	btv->cvbi = btv->state.vbi;
	btv->loop_irq = btv->state.loop_irq;
	bttv_buffer_activate_video(btv, &btv->curr);
	bttv_buffer_activate_vbi(btv, btv->cvbi);
	bttv_set_dma(btv, 0);
	spin_unlock_irqrestore(&btv->s_lock,flags);
	return 0;
}
4384
#endif
L
Linus Torvalds 已提交
4385

4386
static const struct pci_device_id bttv_pci_tbl[] = {
4387 4388 4389 4390
	{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
	{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
	{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
	{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4391
	{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
4392
	{0,}
L
Linus Torvalds 已提交
4393 4394 4395 4396 4397
};

MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);

static struct pci_driver bttv_pci_driver = {
4398 4399 4400
	.name     = "bttv",
	.id_table = bttv_pci_tbl,
	.probe    = bttv_probe,
4401
	.remove   = bttv_remove,
4402
#ifdef CONFIG_PM
L
Linus Torvalds 已提交
4403 4404
	.suspend  = bttv_suspend,
	.resume   = bttv_resume,
4405
#endif
L
Linus Torvalds 已提交
4406 4407
};

A
Adrian Bunk 已提交
4408
static int __init bttv_init_module(void)
L
Linus Torvalds 已提交
4409
{
4410 4411
	int ret;

L
Linus Torvalds 已提交
4412 4413
	bttv_num = 0;

4414
	pr_info("driver version %s loaded\n", BTTV_VERSION);
L
Linus Torvalds 已提交
4415 4416
	if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
		gbuffers = 2;
4417
	if (gbufsize > BTTV_MAX_FBUF)
L
Linus Torvalds 已提交
4418 4419 4420
		gbufsize = BTTV_MAX_FBUF;
	gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
	if (bttv_verbose)
4421 4422
		pr_info("using %d buffers with %dk (%d pages) each for capture\n",
			gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
L
Linus Torvalds 已提交
4423 4424 4425

	bttv_check_chipset();

4426 4427
	ret = bus_register(&bttv_sub_bus_type);
	if (ret < 0) {
4428
		pr_warn("bus_register error: %d\n", ret);
4429 4430
		return ret;
	}
4431 4432 4433 4434 4435
	ret = pci_register_driver(&bttv_pci_driver);
	if (ret < 0)
		bus_unregister(&bttv_sub_bus_type);

	return ret;
L
Linus Torvalds 已提交
4436 4437
}

A
Adrian Bunk 已提交
4438
static void __exit bttv_cleanup_module(void)
L
Linus Torvalds 已提交
4439 4440 4441 4442 4443 4444 4445
{
	pci_unregister_driver(&bttv_pci_driver);
	bus_unregister(&bttv_sub_bus_type);
}

module_init(bttv_init_module);
module_exit(bttv_cleanup_module);