ivtv-driver.c 43.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
    ivtv driver initialization and card probing
    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
    Copyright (C) 2004  Chris Kennedy <c@groovy.org>
    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* Main Driver file for the ivtv project:
 * Driver for the Conexant CX23415/CX23416 chip.
 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
 * License: GPL
 * http://www.ivtvdriver.org
 *
 * -----
 * MPG600/MPG160 support by  T.Adachi <tadachi@tadachi-net.com>
 *                      and Takeru KOMORIYA<komoriya@paken.org>
 *
 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
 *                using information provided by Jiun-Kuei Jung @ AVerMedia.
 *
 * Kurouto Sikou CX23416GYC-STVLP tested by K.Ohta <alpha292@bremen.or.jp>
 *                using information from T.Adachi,Takeru KOMORIYA and others :-)
 *
 * Nagase TRANSGEAR 5000TV, Aopen VA2000MAX-STN6 and I/O data GV-MVP/RX
 *                version by T.Adachi. Special thanks  Mr.Suzuki
 */

#include "ivtv-driver.h"
#include "ivtv-version.h"
#include "ivtv-fileops.h"
#include "ivtv-i2c.h"
#include "ivtv-firmware.h"
#include "ivtv-queue.h"
#include "ivtv-udma.h"
#include "ivtv-irq.h"
#include "ivtv-mailbox.h"
#include "ivtv-streams.h"
#include "ivtv-ioctl.h"
#include "ivtv-cards.h"
#include "ivtv-vbi.h"
55
#include "ivtv-routing.h"
56 57 58
#include "ivtv-gpio.h"

#include <media/tveeprom.h>
59
#include <media/saa7115.h>
60
#include <media/v4l2-chip-ident.h>
61
#include "tuner-xc2028.h"
62 63 64 65 66 67

/* If you have already X v4l cards, then set this to X. This way
   the device numbers stay matched. Example: you have a WinTV card
   without radio and a PVR-350 with. Normally this would give a
   video1 device together with a radio0 device for the PVR. By
   setting this to 1 you ensure that radio0 is now also radio1. */
68
int ivtv_first_minor;
69 70 71 72 73 74 75 76 77 78 79 80

/* add your revision and whatnot here */
static struct pci_device_id ivtv_pci_tbl[] __devinitdata = {
	{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV15,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV16,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{0,}
};

MODULE_DEVICE_TABLE(pci,ivtv_pci_tbl);

81 82 83
/* ivtv instance counter */
static atomic_t ivtv_instance = ATOMIC_INIT(0);

84 85
/* Parameter declarations */
static int cardtype[IVTV_MAX_CARDS];
86 87 88 89 90 91 92 93
static int tuner[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1 };
static int radio[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1 };
94 95 96 97
static int i2c_clock_period[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
					       -1, -1, -1, -1, -1, -1, -1, -1,
					       -1, -1, -1, -1, -1, -1, -1, -1,
					       -1, -1, -1, -1, -1, -1, -1, -1 };
98

99 100 101
static unsigned int cardtype_c = 1;
static unsigned int tuner_c = 1;
static unsigned int radio_c = 1;
102
static unsigned int i2c_clock_period_c = 1;
103
static char pal[] = "---";
104 105 106 107
static char secam[] = "--";
static char ntsc[] = "-";

/* Buffers */
108 109 110 111 112 113 114 115 116 117 118 119

/* DMA Buffers, Default size in MB allocated */
#define IVTV_DEFAULT_ENC_MPG_BUFFERS 4
#define IVTV_DEFAULT_ENC_YUV_BUFFERS 2
#define IVTV_DEFAULT_ENC_VBI_BUFFERS 1
/* Exception: size in kB for this stream (MB is overkill) */
#define IVTV_DEFAULT_ENC_PCM_BUFFERS 320
#define IVTV_DEFAULT_DEC_MPG_BUFFERS 1
#define IVTV_DEFAULT_DEC_YUV_BUFFERS 1
/* Exception: size in kB for this stream (MB is way overkill) */
#define IVTV_DEFAULT_DEC_VBI_BUFFERS 64

120 121 122 123 124 125 126 127
static int enc_mpg_buffers = IVTV_DEFAULT_ENC_MPG_BUFFERS;
static int enc_yuv_buffers = IVTV_DEFAULT_ENC_YUV_BUFFERS;
static int enc_vbi_buffers = IVTV_DEFAULT_ENC_VBI_BUFFERS;
static int enc_pcm_buffers = IVTV_DEFAULT_ENC_PCM_BUFFERS;
static int dec_mpg_buffers = IVTV_DEFAULT_DEC_MPG_BUFFERS;
static int dec_yuv_buffers = IVTV_DEFAULT_DEC_YUV_BUFFERS;
static int dec_vbi_buffers = IVTV_DEFAULT_DEC_VBI_BUFFERS;

128 129
static int ivtv_yuv_mode;
static int ivtv_yuv_threshold = -1;
130 131
static int ivtv_pci_latency = 1;

132
int ivtv_debug;
133 134 135
#ifdef CONFIG_VIDEO_ADV_DEBUG
int ivtv_fw_debug;
#endif
136

137
static int tunertype = -1;
138
static int newi2c = -1;
139 140 141 142 143 144 145 146

module_param_array(tuner, int, &tuner_c, 0644);
module_param_array(radio, bool, &radio_c, 0644);
module_param_array(cardtype, int, &cardtype_c, 0644);
module_param_string(pal, pal, sizeof(pal), 0644);
module_param_string(secam, secam, sizeof(secam), 0644);
module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
module_param_named(debug,ivtv_debug, int, 0644);
147 148 149
#ifdef CONFIG_VIDEO_ADV_DEBUG
module_param_named(fw_debug, ivtv_fw_debug, int, 0644);
#endif
150 151 152 153 154 155 156 157 158 159 160 161 162
module_param(ivtv_pci_latency, int, 0644);
module_param(ivtv_yuv_mode, int, 0644);
module_param(ivtv_yuv_threshold, int, 0644);
module_param(ivtv_first_minor, int, 0644);

module_param(enc_mpg_buffers, int, 0644);
module_param(enc_yuv_buffers, int, 0644);
module_param(enc_vbi_buffers, int, 0644);
module_param(enc_pcm_buffers, int, 0644);
module_param(dec_mpg_buffers, int, 0644);
module_param(dec_yuv_buffers, int, 0644);
module_param(dec_vbi_buffers, int, 0644);

163
module_param(tunertype, int, 0644);
164
module_param(newi2c, int, 0644);
165
module_param_array(i2c_clock_period, int, &i2c_clock_period_c, 0644);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
			"\t\t\tsee tuner.h for values");
MODULE_PARM_DESC(radio,
		 "Enable or disable the radio. Use only if autodetection\n"
		 "\t\t\tfails. 0 = disable, 1 = enable");
MODULE_PARM_DESC(cardtype,
		 "Only use this option if your card is not detected properly.\n"
		 "\t\tSpecify card type:\n"
		 "\t\t\t 1 = WinTV PVR 250\n"
		 "\t\t\t 2 = WinTV PVR 350\n"
		 "\t\t\t 3 = WinTV PVR-150 or PVR-500\n"
		 "\t\t\t 4 = AVerMedia M179\n"
		 "\t\t\t 5 = YUAN MPG600/Kuroutoshikou iTVC16-STVLP\n"
		 "\t\t\t 6 = YUAN MPG160/Kuroutoshikou iTVC15-STVLP\n"
		 "\t\t\t 7 = YUAN PG600/DIAMONDMM PVR-550 (CX Falcon 2)\n"
		 "\t\t\t 8 = Adaptec AVC-2410\n"
		 "\t\t\t 9 = Adaptec AVC-2010\n"
		 "\t\t\t10 = NAGASE TRANSGEAR 5000TV\n"
		 "\t\t\t11 = AOpen VA2000MAX-STN6\n"
		 "\t\t\t12 = YUAN MPG600GR/Kuroutoshikou CX23416GYC-STVLP\n"
		 "\t\t\t13 = I/O Data GV-MVP/RX\n"
		 "\t\t\t14 = I/O Data GV-MVP/RX2E\n"
		 "\t\t\t15 = GOTVIEW PCI DVD\n"
		 "\t\t\t16 = GOTVIEW PCI DVD2 Deluxe\n"
		 "\t\t\t17 = Yuan MPC622\n"
		 "\t\t\t18 = Digital Cowboy DCT-MTVP1\n"
193 194 195
		 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"
		 "\t\t\t20 = Club3D ZAP-TV1x01\n"
		 "\t\t\t21 = AverTV MCE 116 Plus\n"
196
		 "\t\t\t22 = ASUS Falcon2\n"
197
		 "\t\t\t23 = AverMedia PVR-150 Plus\n"
198
		 "\t\t\t24 = AverMedia EZMaker PCI Deluxe\n"
199
		 "\t\t\t25 = AverMedia M104 (not yet working)\n"
200
		 "\t\t\t26 = Buffalo PC-MV5L/PCI\n"
201
		 "\t\t\t27 = AVerMedia UltraTV 1500 MCE\n"
202
		 "\t\t\t28 = Sony VAIO Giga Pocket (ENX Kikyou)\n"
203 204
		 "\t\t\t 0 = Autodetect (default)\n"
		 "\t\t\t-1 = Ignore this card\n\t\t");
205 206 207
MODULE_PARM_DESC(pal, "Set PAL standard: BGH, DK, I, M, N, Nc, 60");
MODULE_PARM_DESC(secam, "Set SECAM standard: BGH, DK, L, LC");
MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J (Japan), K (South Korea)");
208
MODULE_PARM_DESC(tunertype,
209
		"Specify tuner type:\n"
210 211 212
		"\t\t\t 0 = tuner for PAL-B/G/H/D/K/I, SECAM-B/G/H/D/K/L/Lc\n"
		"\t\t\t 1 = tuner for NTSC-M/J/K, PAL-M/N/Nc\n"
		"\t\t\t-1 = Autodetect (default)\n");
213
MODULE_PARM_DESC(debug,
214 215 216 217 218 219 220 221 222 223 224 225
		 "Debug level (bitmask). Default: 0\n"
		 "\t\t\t   1/0x0001: warning\n"
		 "\t\t\t   2/0x0002: info\n"
		 "\t\t\t   4/0x0004: mailbox\n"
		 "\t\t\t   8/0x0008: ioctl\n"
		 "\t\t\t  16/0x0010: file\n"
		 "\t\t\t  32/0x0020: dma\n"
		 "\t\t\t  64/0x0040: irq\n"
		 "\t\t\t 128/0x0080: decoder\n"
		 "\t\t\t 256/0x0100: yuv\n"
		 "\t\t\t 512/0x0200: i2c\n"
		 "\t\t\t1024/0x0400: high volume\n");
226 227 228 229
#ifdef CONFIG_VIDEO_ADV_DEBUG
MODULE_PARM_DESC(fw_debug,
		 "Enable code for debugging firmware problems.  Default: 0\n");
#endif
230 231 232 233 234 235 236 237 238
MODULE_PARM_DESC(ivtv_pci_latency,
		 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
		 "\t\t\tDefault: Yes");
MODULE_PARM_DESC(ivtv_yuv_mode,
		 "Specify the yuv playback mode:\n"
		 "\t\t\t0 = interlaced\n\t\t\t1 = progressive\n\t\t\t2 = auto\n"
		 "\t\t\tDefault: 0 (interlaced)");
MODULE_PARM_DESC(ivtv_yuv_threshold,
		 "If ivtv_yuv_mode is 2 (auto) then playback content as\n\t\tprogressive if src height <= ivtv_yuvthreshold\n"
239
		 "\t\t\tDefault: 480");
240 241 242 243 244 245 246 247 248 249
MODULE_PARM_DESC(enc_mpg_buffers,
		 "Encoder MPG Buffers (in MB)\n"
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_MPG_BUFFERS));
MODULE_PARM_DESC(enc_yuv_buffers,
		 "Encoder YUV Buffers (in MB)\n"
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_YUV_BUFFERS));
MODULE_PARM_DESC(enc_vbi_buffers,
		 "Encoder VBI Buffers (in MB)\n"
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_VBI_BUFFERS));
MODULE_PARM_DESC(enc_pcm_buffers,
250
		 "Encoder PCM buffers (in kB)\n"
251 252 253 254 255 256 257 258
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_PCM_BUFFERS));
MODULE_PARM_DESC(dec_mpg_buffers,
		 "Decoder MPG buffers (in MB)\n"
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_MPG_BUFFERS));
MODULE_PARM_DESC(dec_yuv_buffers,
		 "Decoder YUV buffers (in MB)\n"
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_YUV_BUFFERS));
MODULE_PARM_DESC(dec_vbi_buffers,
259
		 "Decoder VBI buffers (in kB)\n"
260 261 262 263 264
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_VBI_BUFFERS));
MODULE_PARM_DESC(newi2c,
		 "Use new I2C implementation\n"
		 "\t\t\t-1 is autodetect, 0 is off, 1 is on\n"
		 "\t\t\tDefault is autodetect");
265 266 267 268
MODULE_PARM_DESC(i2c_clock_period,
		 "Period of SCL for the I2C bus controlled by the CX23415/6\n"
		 "\t\t\tMin: 10 usec (100 kHz), Max: 4500 usec (222 Hz)\n"
		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_I2C_CLOCK_PERIOD));
269

270
MODULE_PARM_DESC(ivtv_first_minor, "Set device node number assigned to first card");
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");
MODULE_DESCRIPTION("CX23415/CX23416 driver");
MODULE_SUPPORTED_DEVICE
    ("CX23415/CX23416 MPEG2 encoder (WinTV PVR-150/250/350/500,\n"
		"\t\t\tYuan MPG series and similar)");
MODULE_LICENSE("GPL");

MODULE_VERSION(IVTV_VERSION);

void ivtv_clear_irq_mask(struct ivtv *itv, u32 mask)
{
	itv->irqmask &= ~mask;
	write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
}

void ivtv_set_irq_mask(struct ivtv *itv, u32 mask)
{
	itv->irqmask |= mask;
	write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
}

int ivtv_set_output_mode(struct ivtv *itv, int mode)
{
    int old_mode;

    spin_lock(&itv->lock);
    old_mode = itv->output_mode;
    if (old_mode == 0)
	itv->output_mode = old_mode = mode;
    spin_unlock(&itv->lock);
    return old_mode;
}

struct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv)
{
	switch (itv->output_mode) {
	case OUT_MPG:
		return &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
	case OUT_YUV:
		return &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
	default:
		return NULL;
	}
}

int ivtv_waitq(wait_queue_head_t *waitq)
{
	DEFINE_WAIT(wait);

	prepare_to_wait(waitq, &wait, TASK_INTERRUPTIBLE);
	schedule();
	finish_wait(waitq, &wait);
	return signal_pending(current) ? -EINTR : 0;
}

/* Generic utility functions */
328
int ivtv_msleep_timeout(unsigned int msecs, int intr)
329
{
330
	int timeout = msecs_to_jiffies(msecs);
331 332 333 334

	do {
		set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
		timeout = schedule_timeout(timeout);
335 336 337 338 339 340
		if (intr) {
			int ret = signal_pending(current);

			if (ret)
				return ret;
		}
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	} while (timeout);
	return 0;
}

/* Release ioremapped memory */
static void ivtv_iounmap(struct ivtv *itv)
{
	if (itv == NULL)
		return;

	/* Release registers memory */
	if (itv->reg_mem != NULL) {
		IVTV_DEBUG_INFO("releasing reg_mem\n");
		iounmap(itv->reg_mem);
		itv->reg_mem = NULL;
	}
	/* Release io memory */
	if (itv->has_cx23415 && itv->dec_mem != NULL) {
		IVTV_DEBUG_INFO("releasing dec_mem\n");
		iounmap(itv->dec_mem);
	}
	itv->dec_mem = NULL;

	/* Release io memory */
	if (itv->enc_mem != NULL) {
		IVTV_DEBUG_INFO("releasing enc_mem\n");
		iounmap(itv->enc_mem);
		itv->enc_mem = NULL;
	}
}

/* Hauppauge card? get values from tveeprom */
void ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv)
{
	u8 eedata[256];

	itv->i2c_client.addr = 0xA0 >> 1;
	tveeprom_read(&itv->i2c_client, eedata, sizeof(eedata));
	tveeprom_hauppauge_analog(&itv->i2c_client, tv, eedata);
}

static void ivtv_process_eeprom(struct ivtv *itv)
{
	struct tveeprom tv;
385
	int pci_slot = PCI_SLOT(itv->pdev->devfn);
386 387 388 389 390 391 392 393 394

	ivtv_read_eeprom(itv, &tv);

	/* Many thanks to Steven Toth from Hauppauge for providing the
	   model numbers */
	switch (tv.model) {
		/* In a few cases the PCI subsystem IDs do not correctly
		   identify the card. A better method is to check the
		   model number from the eeprom instead. */
395
		case 30012 ... 30039:  /* Low profile PVR250 */
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
		case 32000 ... 32999:
		case 48000 ... 48099:  /* 48??? range are PVR250s with a cx23415 */
		case 48400 ... 48599:
			itv->card = ivtv_get_card(IVTV_CARD_PVR_250);
			break;
		case 48100 ... 48399:
		case 48600 ... 48999:
			itv->card = ivtv_get_card(IVTV_CARD_PVR_350);
			break;
		case 23000 ... 23999:  /* PVR500 */
		case 25000 ... 25999:  /* Low profile PVR150 */
		case 26000 ... 26999:  /* Regular PVR150 */
			itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
			break;
		case 0:
			IVTV_ERR("Invalid EEPROM\n");
			return;
		default:
			IVTV_ERR("Unknown model %d, defaulting to PVR-150\n", tv.model);
			itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
			break;
	}

	switch (tv.model) {
		/* Old style PVR350 (with an saa7114) uses this input for
		   the tuner. */
		case 48254:
			itv->card = ivtv_get_card(IVTV_CARD_PVR_350_V1);
			break;
		default:
			break;
	}

	itv->v4l2_cap = itv->card->v4l2_capabilities;
	itv->card_name = itv->card->name;
431
	itv->card_i2c = itv->card->i2c;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

	/* If this is a PVR500 then it should be possible to detect whether it is the
	   first or second unit by looking at the subsystem device ID: is bit 4 is
	   set, then it is the second unit (according to info from Hauppauge).

	   However, while this works for most cards, I have seen a few PVR500 cards
	   where both units have the same subsystem ID.

	   So instead I look at the reported 'PCI slot' (which is the slot on the PVR500
	   PCI bridge) and if it is 8, then it is assumed to be the first unit, otherwise
	   it is the second unit. It is possible that it is a different slot when ivtv is
	   used in Xen, in that case I ignore this card here. The worst that can happen
	   is that the card presents itself with a non-working radio device.

	   This detection is needed since the eeprom reports incorrectly that a radio is
	   present on the second unit. */
	if (tv.model / 1000 == 23) {
449 450 451 452 453 454
		static const struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
			.radio = { 0x60, I2C_CLIENT_END },
			.demod = { 0x43, I2C_CLIENT_END },
			.tv = { 0x61, I2C_CLIENT_END },
		};

455
		itv->card_name = "WinTV PVR 500";
456
		itv->card_i2c = &ivtv_i2c_radio;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		if (pci_slot == 8 || pci_slot == 9) {
			int is_first = (pci_slot & 1) == 0;

			itv->card_name = is_first ? "WinTV PVR 500 (unit #1)" :
						    "WinTV PVR 500 (unit #2)";
			if (!is_first) {
				IVTV_INFO("Correcting tveeprom data: no radio present on second unit\n");
				tv.has_radio = 0;
			}
		}
	}
	IVTV_INFO("Autodetected %s\n", itv->card_name);

	switch (tv.tuner_hauppauge_model) {
		case 85:
		case 99:
		case 112:
			itv->pvr150_workaround = 1;
			break;
		default:
			break;
	}
	if (tv.tuner_type == TUNER_ABSENT)
480
		IVTV_ERR("tveeprom cannot autodetect tuner!\n");
481 482 483 484 485 486

	if (itv->options.tuner == -1)
		itv->options.tuner = tv.tuner_type;
	if (itv->options.radio == -1)
		itv->options.radio = (tv.has_radio != 0);
	/* only enable newi2c if an IR blaster is present */
487 488
	if (itv->options.newi2c == -1 && tv.has_ir) {
		itv->options.newi2c = (tv.has_ir & 4) ? 1 : 0;
489
		if (itv->options.newi2c) {
490
		    IVTV_INFO("Reopen i2c bus for IR-blaster support\n");
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		    exit_ivtv_i2c(itv);
		    init_ivtv_i2c(itv);
		}
	}

	if (itv->std != 0)
		/* user specified tuner standard */
		return;

	/* autodetect tuner standard */
	if (tv.tuner_formats & V4L2_STD_PAL) {
		IVTV_DEBUG_INFO("PAL tuner detected\n");
		itv->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
	} else if (tv.tuner_formats & V4L2_STD_NTSC) {
		IVTV_DEBUG_INFO("NTSC tuner detected\n");
		itv->std |= V4L2_STD_NTSC_M;
	} else if (tv.tuner_formats & V4L2_STD_SECAM) {
		IVTV_DEBUG_INFO("SECAM tuner detected\n");
		itv->std |= V4L2_STD_SECAM_L;
	} else {
		IVTV_INFO("No tuner detected, default to NTSC-M\n");
		itv->std |= V4L2_STD_NTSC_M;
	}
}

static v4l2_std_id ivtv_parse_std(struct ivtv *itv)
{
	switch (pal[0]) {
		case '6':
520
			tunertype = 0;
521 522 523 524 525 526 527
			return V4L2_STD_PAL_60;
		case 'b':
		case 'B':
		case 'g':
		case 'G':
		case 'h':
		case 'H':
528
			tunertype = 0;
529
			return V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
530 531
		case 'n':
		case 'N':
532
			tunertype = 1;
533 534 535 536 537
			if (pal[1] == 'c' || pal[1] == 'C')
				return V4L2_STD_PAL_Nc;
			return V4L2_STD_PAL_N;
		case 'i':
		case 'I':
538
			tunertype = 0;
539 540 541 542 543
			return V4L2_STD_PAL_I;
		case 'd':
		case 'D':
		case 'k':
		case 'K':
544
			tunertype = 0;
545 546 547
			return V4L2_STD_PAL_DK;
		case 'M':
		case 'm':
548
			tunertype = 1;
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
			return V4L2_STD_PAL_M;
		case '-':
			break;
		default:
			IVTV_WARN("pal= argument not recognised\n");
			return 0;
	}

	switch (secam[0]) {
		case 'b':
		case 'B':
		case 'g':
		case 'G':
		case 'h':
		case 'H':
564
			tunertype = 0;
565 566 567 568 569
			return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
		case 'd':
		case 'D':
		case 'k':
		case 'K':
570
			tunertype = 0;
571 572 573
			return V4L2_STD_SECAM_DK;
		case 'l':
		case 'L':
574
			tunertype = 0;
575 576 577 578 579 580 581 582 583 584 585 586 587
			if (secam[1] == 'C' || secam[1] == 'c')
				return V4L2_STD_SECAM_LC;
			return V4L2_STD_SECAM_L;
		case '-':
			break;
		default:
			IVTV_WARN("secam= argument not recognised\n");
			return 0;
	}

	switch (ntsc[0]) {
		case 'm':
		case 'M':
588
			tunertype = 1;
589 590 591
			return V4L2_STD_NTSC_M;
		case 'j':
		case 'J':
592
			tunertype = 1;
593 594 595
			return V4L2_STD_NTSC_M_JP;
		case 'k':
		case 'K':
596
			tunertype = 1;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
			return V4L2_STD_NTSC_M_KR;
		case '-':
			break;
		default:
			IVTV_WARN("ntsc= argument not recognised\n");
			return 0;
	}

	/* no match found */
	return 0;
}

static void ivtv_process_options(struct ivtv *itv)
{
	const char *chipname;
	int i, j;

614 615 616 617 618 619 620
	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers * 1024;
	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers * 1024;
	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers * 1024;
	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_MPG] = dec_mpg_buffers * 1024;
	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_YUV] = dec_yuv_buffers * 1024;
	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_VBI] = dec_vbi_buffers;
621 622 623
	itv->options.cardtype = cardtype[itv->instance];
	itv->options.tuner = tuner[itv->instance];
	itv->options.radio = radio[itv->instance];
624 625 626 627 628 629 630 631 632

	itv->options.i2c_clock_period = i2c_clock_period[itv->instance];
	if (itv->options.i2c_clock_period == -1)
		itv->options.i2c_clock_period = IVTV_DEFAULT_I2C_CLOCK_PERIOD;
	else if (itv->options.i2c_clock_period < 10)
		itv->options.i2c_clock_period = 10;
	else if (itv->options.i2c_clock_period > 4500)
		itv->options.i2c_clock_period = 4500;

633
	itv->options.newi2c = newi2c;
634 635 636
	if (tunertype < -1 || tunertype > 1) {
		IVTV_WARN("Invalid tunertype argument, will autodetect instead\n");
		tunertype = -1;
637
	}
638
	itv->std = ivtv_parse_std(itv);
639 640
	if (itv->std == 0 && tunertype >= 0)
		itv->std = tunertype ? V4L2_STD_MN : (V4L2_STD_ALL & ~V4L2_STD_MN);
641
	itv->has_cx23415 = (itv->pdev->device == PCI_DEVICE_ID_IVTV15);
642 643 644 645 646 647 648 649 650 651 652 653
	chipname = itv->has_cx23415 ? "cx23415" : "cx23416";
	if (itv->options.cardtype == -1) {
		IVTV_INFO("Ignore card (detected %s based chip)\n", chipname);
		return;
	}
	if ((itv->card = ivtv_get_card(itv->options.cardtype - 1))) {
		IVTV_INFO("User specified %s card (detected %s based chip)\n",
				itv->card->name, chipname);
	} else if (itv->options.cardtype != 0) {
		IVTV_ERR("Unknown user specified type, trying to autodetect card\n");
	}
	if (itv->card == NULL) {
654 655 656
		if (itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE ||
		    itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT1 ||
		    itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT2) {
657 658 659 660 661 662 663 664 665 666
			itv->card = ivtv_get_card(itv->has_cx23415 ? IVTV_CARD_PVR_350 : IVTV_CARD_PVR_150);
			IVTV_INFO("Autodetected Hauppauge card (%s based)\n",
					chipname);
		}
	}
	if (itv->card == NULL) {
		for (i = 0; (itv->card = ivtv_get_card(i)); i++) {
			if (itv->card->pci_list == NULL)
				continue;
			for (j = 0; itv->card->pci_list[j].device; j++) {
667
				if (itv->pdev->device !=
668 669
				    itv->card->pci_list[j].device)
					continue;
670
				if (itv->pdev->subsystem_vendor !=
671 672
				    itv->card->pci_list[j].subsystem_vendor)
					continue;
673
				if (itv->pdev->subsystem_device !=
674 675 676 677 678 679 680 681 682 683 684 685
				    itv->card->pci_list[j].subsystem_device)
					continue;
				IVTV_INFO("Autodetected %s card (%s based)\n",
						itv->card->name, chipname);
				goto done;
			}
		}
	}
done:

	if (itv->card == NULL) {
		itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
686
		IVTV_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
687
		     itv->pdev->vendor, itv->pdev->device);
688
		IVTV_ERR("              subsystem vendor/device: [%04x:%04x]\n",
689
		     itv->pdev->subsystem_vendor, itv->pdev->subsystem_device);
690 691 692 693
		IVTV_ERR("              %s based\n", chipname);
		IVTV_ERR("Defaulting to %s card\n", itv->card->name);
		IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
		IVTV_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
694
		IVTV_ERR("Prefix your subject line with [UNKNOWN IVTV CARD].\n");
695 696 697
	}
	itv->v4l2_cap = itv->card->v4l2_capabilities;
	itv->card_name = itv->card->name;
698
	itv->card_i2c = itv->card->i2c;
699 700 701 702 703 704 705 706 707
}

/* Precondition: the ivtv structure has been memset to 0. Only
   the dev and num fields have been filled in.
   No assumptions on the card type may be made here (see ivtv_init_struct2
   for that).
 */
static int __devinit ivtv_init_struct1(struct ivtv *itv)
{
708 709
	struct sched_param param = { .sched_priority = 99 };

710
	itv->base_addr = pci_resource_start(itv->pdev, 0);
711 712 713
	itv->enc_mbox.max_mbox = 2; /* the encoder has 3 mailboxes (0-2) */
	itv->dec_mbox.max_mbox = 1; /* the decoder has 2 mailboxes (0-1) */

714
	mutex_init(&itv->serialize_lock);
715 716 717
	mutex_init(&itv->i2c_bus_lock);
	mutex_init(&itv->udma.lock);

718 719
	spin_lock_init(&itv->lock);
	spin_lock_init(&itv->dma_reg_lock);
720

721 722 723 724 725
	init_kthread_worker(&itv->irq_worker);
	itv->irq_worker_task = kthread_run(kthread_worker_fn, &itv->irq_worker,
					   itv->v4l2_dev.name);
	if (IS_ERR(itv->irq_worker_task)) {
		IVTV_ERR("Could not create ivtv task\n");
726 727
		return -1;
	}
728 729
	/* must use the FIFO scheduler as it is realtime sensitive */
	sched_setscheduler(itv->irq_worker_task, SCHED_FIFO, &param);
730

731
	init_kthread_work(&itv->irq_work, ivtv_irq_work_handler);
732 733 734 735 736 737 738 739

	/* start counting open_id at 1 */
	itv->open_id = 1;

	/* Initial settings */
	cx2341x_fill_defaults(&itv->params);
	itv->params.port = CX2341X_PORT_MEMORY;
	itv->params.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
740
	init_waitqueue_head(&itv->eos_waitq);
741 742 743 744 745 746 747 748
	init_waitqueue_head(&itv->event_waitq);
	init_waitqueue_head(&itv->vsync_waitq);
	init_waitqueue_head(&itv->dma_waitq);
	init_timer(&itv->dma_timer);
	itv->dma_timer.function = ivtv_unfinished_dma;
	itv->dma_timer.data = (unsigned long)itv;

	itv->cur_dma_stream = -1;
749
	itv->cur_pio_stream = -1;
750 751 752 753 754 755 756
	itv->audio_stereo_mode = AUDIO_STEREO;
	itv->audio_bilingual_mode = AUDIO_MONO_LEFT;

	/* Ctrls */
	itv->speed = 1000;

	/* VBI */
757
	itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
758 759
	itv->vbi.sliced_in = &itv->vbi.in.fmt.sliced;

760 761 762
	/* Init the sg table for osd/yuv output */
	sg_init_table(itv->udma.SGlist, IVTV_DMA_SG_OSD_ENT);

763 764 765 766 767 768 769 770
	/* OSD */
	itv->osd_global_alpha_state = 1;
	itv->osd_global_alpha = 255;

	/* YUV */
	atomic_set(&itv->yuv_info.next_dma_frame, -1);
	itv->yuv_info.lace_mode = ivtv_yuv_mode;
	itv->yuv_info.lace_threshold = ivtv_yuv_threshold;
771
	itv->yuv_info.max_frames_buffered = 3;
772
	itv->yuv_info.track_osd = 1;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	return 0;
}

/* Second initialization part. Here the card type has been
   autodetected. */
static void __devinit ivtv_init_struct2(struct ivtv *itv)
{
	int i;

	for (i = 0; i < IVTV_CARD_MAX_VIDEO_INPUTS; i++)
		if (itv->card->video_inputs[i].video_type == 0)
			break;
	itv->nof_inputs = i;
	for (i = 0; i < IVTV_CARD_MAX_AUDIO_INPUTS; i++)
		if (itv->card->audio_inputs[i].audio_type == 0)
			break;
	itv->nof_audio_inputs = i;

	if (itv->card->hw_all & IVTV_HW_CX25840) {
		itv->vbi.sliced_size = 288;  /* multiple of 16, real size = 284 */
	} else {
		itv->vbi.sliced_size = 64;   /* multiple of 16, real size = 52 */
	}

	/* Find tuner input */
	for (i = 0; i < itv->nof_inputs; i++) {
		if (itv->card->video_inputs[i].video_type ==
				IVTV_CARD_INPUT_VID_TUNER)
			break;
	}
	if (i == itv->nof_inputs)
		i = 0;
	itv->active_input = i;
	itv->audio_input = itv->card->video_inputs[i].audio_index;
}

809
static int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *pdev,
810 811 812
			  const struct pci_device_id *pci_id)
{
	u16 cmd;
813
	u8 card_rev;
814 815 816 817
	unsigned char pci_latency;

	IVTV_DEBUG_INFO("Enabling pci device\n");

818
	if (pci_enable_device(pdev)) {
819
		IVTV_ERR("Can't enable device!\n");
820 821
		return -EIO;
	}
822
	if (pci_set_dma_mask(pdev, 0xffffffff)) {
823
		IVTV_ERR("No suitable DMA available.\n");
824 825 826
		return -EIO;
	}
	if (!request_mem_region(itv->base_addr, IVTV_ENCODER_SIZE, "ivtv encoder")) {
827
		IVTV_ERR("Cannot request encoder memory region.\n");
828 829 830 831 832
		return -EIO;
	}

	if (!request_mem_region(itv->base_addr + IVTV_REG_OFFSET,
				IVTV_REG_SIZE, "ivtv registers")) {
833
		IVTV_ERR("Cannot request register memory region.\n");
834 835 836 837 838 839 840
		release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
		return -EIO;
	}

	if (itv->has_cx23415 &&
	    !request_mem_region(itv->base_addr + IVTV_DECODER_OFFSET,
				IVTV_DECODER_SIZE, "ivtv decoder")) {
841
		IVTV_ERR("Cannot request decoder memory region.\n");
842 843 844 845 846 847
		release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
		release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
		return -EIO;
	}

	/* Check for bus mastering */
848
	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
849 850
	if (!(cmd & PCI_COMMAND_MASTER)) {
		IVTV_DEBUG_INFO("Attempting to enable Bus Mastering\n");
851 852
		pci_set_master(pdev);
		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
853 854 855 856 857 858 859
		if (!(cmd & PCI_COMMAND_MASTER)) {
			IVTV_ERR("Bus Mastering is not enabled\n");
			return -ENXIO;
		}
	}
	IVTV_DEBUG_INFO("Bus Mastering Enabled.\n");

860 861
	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &card_rev);
	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
862 863 864 865

	if (pci_latency < 64 && ivtv_pci_latency) {
		IVTV_INFO("Unreasonably low latency timer, "
			       "setting to 64 (was %d)\n", pci_latency);
866 867
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
868 869 870 871 872
	}
	/* This config space value relates to DMA latencies. The
	   default value 0x8080 is too low however and will lead
	   to DMA errors. 0xffff is the max value which solves
	   these problems. */
873
	pci_write_config_dword(pdev, 0x40, 0xffff);
874 875 876

	IVTV_DEBUG_INFO("%d (rev %d) at %02x:%02x.%x, "
		   "irq: %d, latency: %d, memory: 0x%lx\n",
877 878 879
		   pdev->device, card_rev, pdev->bus->number,
		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
		   pdev->irq, pci_latency, (unsigned long)itv->base_addr);
880 881 882 883 884 885 886

	return 0;
}

static void ivtv_load_and_init_modules(struct ivtv *itv)
{
	u32 hw = itv->card->hw_all;
887
	unsigned i;
888 889 890 891 892 893 894

	/* check which i2c devices are actually found */
	for (i = 0; i < 32; i++) {
		u32 device = 1 << i;

		if (!(device & hw))
			continue;
895 896 897
		if (device == IVTV_HW_GPIO || device == IVTV_HW_TVEEPROM) {
			/* GPIO and TVEEPROM do not use i2c probing */
			itv->hw_flags |= device;
898 899
			continue;
		}
900
		if (ivtv_i2c_register(itv, i) == 0)
901 902 903
			itv->hw_flags |= device;
	}

904
	/* probe for legacy IR controllers that aren't in card definitions */
905 906
	if ((itv->hw_flags & IVTV_HW_IR_ANY) == 0)
		ivtv_i2c_new_ir_legacy(itv);
907

908 909 910 911 912 913 914 915 916 917 918
	if (itv->card->hw_all & IVTV_HW_CX25840)
		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_CX25840);
	else if (itv->card->hw_all & IVTV_HW_SAA717X)
		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA717X);
	else if (itv->card->hw_all & IVTV_HW_SAA7114)
		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7114);
	else
		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7115);
	itv->sd_audio = ivtv_find_hw(itv, itv->card->hw_audio_ctrl);
	itv->sd_muxer = ivtv_find_hw(itv, itv->card->hw_muxer);

919 920 921 922 923 924 925 926 927 928
	hw = itv->hw_flags;

	if (itv->card->type == IVTV_CARD_CX23416GYC) {
		/* Several variations of this card exist, detect which card
		   type should be used. */
		if ((hw & (IVTV_HW_UPD64031A | IVTV_HW_UPD6408X)) == 0)
			itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGRYCS);
		else if ((hw & IVTV_HW_UPD64031A) == 0)
			itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGR);
	}
929 930 931
	else if (itv->card->type == IVTV_CARD_GV_MVPRX ||
		 itv->card->type == IVTV_CARD_GV_MVPRX2E) {
		/* The crystal frequency of GVMVPRX is 24.576MHz */
932 933
		v4l2_subdev_call(itv->sd_video, video, s_crystal_freq,
			SAA7115_FREQ_24_576_MHZ, SAA7115_FREQ_FL_UCGC);
934
	}
935 936 937 938 939 940 941 942 943 944 945

	if (hw & IVTV_HW_CX25840) {
		itv->vbi.raw_decoder_line_size = 1444;
		itv->vbi.raw_decoder_sav_odd_field = 0x20;
		itv->vbi.raw_decoder_sav_even_field = 0x60;
		itv->vbi.sliced_decoder_line_size = 272;
		itv->vbi.sliced_decoder_sav_odd_field = 0xB0;
		itv->vbi.sliced_decoder_sav_even_field = 0xF0;
	}

	if (hw & IVTV_HW_SAA711X) {
946
		struct v4l2_dbg_chip_ident v;
947 948 949 950

		/* determine the exact saa711x model */
		itv->hw_flags &= ~IVTV_HW_SAA711X;

951 952
		v.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;
		strlcpy(v.match.name, "saa7115", sizeof(v.match.name));
953
		ivtv_call_hw(itv, IVTV_HW_SAA711X, core, g_chip_ident, &v);
954 955 956 957
		if (v.ident == V4L2_IDENT_SAA7114) {
			itv->hw_flags |= IVTV_HW_SAA7114;
			/* VBI is not yet supported by the saa7114 driver. */
			itv->v4l2_cap &= ~(V4L2_CAP_SLICED_VBI_CAPTURE|V4L2_CAP_VBI_CAPTURE);
958
		} else {
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
			itv->hw_flags |= IVTV_HW_SAA7115;
		}
		itv->vbi.raw_decoder_line_size = 1443;
		itv->vbi.raw_decoder_sav_odd_field = 0x25;
		itv->vbi.raw_decoder_sav_even_field = 0x62;
		itv->vbi.sliced_decoder_line_size = 51;
		itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
		itv->vbi.sliced_decoder_sav_even_field = 0xEC;
	}

	if (hw & IVTV_HW_SAA717X) {
		itv->vbi.raw_decoder_line_size = 1443;
		itv->vbi.raw_decoder_sav_odd_field = 0x25;
		itv->vbi.raw_decoder_sav_even_field = 0x62;
		itv->vbi.sliced_decoder_line_size = 51;
		itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
		itv->vbi.sliced_decoder_sav_even_field = 0xEC;
	}
}

979
static int __devinit ivtv_probe(struct pci_dev *pdev,
980 981 982 983 984 985 986
				const struct pci_device_id *pci_id)
{
	int retval = 0;
	int vbi_buf_size;
	struct ivtv *itv;

	itv = kzalloc(sizeof(struct ivtv), GFP_ATOMIC);
987
	if (itv == NULL)
988
		return -ENOMEM;
989
	itv->pdev = pdev;
990 991
	itv->instance = v4l2_device_set_name(&itv->v4l2_dev, "ivtv",
						&ivtv_instance);
992

993
	retval = v4l2_device_register(&pdev->dev, &itv->v4l2_dev);
994 995
	if (retval) {
		kfree(itv);
996
		return retval;
997
	}
998
	IVTV_INFO("Initializing card %d\n", itv->instance);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

	ivtv_process_options(itv);
	if (itv->options.cardtype == -1) {
		retval = -ENODEV;
		goto err;
	}
	if (ivtv_init_struct1(itv)) {
		retval = -ENOMEM;
		goto err;
	}

	IVTV_DEBUG_INFO("base addr: 0x%08x\n", itv->base_addr);

	/* PCI Device Setup */
1013 1014
	retval = ivtv_setup_pci(itv, pdev, pci_id);
	if (retval == -EIO)
1015
		goto free_worker;
1016 1017
	if (retval == -ENXIO)
		goto free_mem;
1018 1019 1020 1021 1022 1023 1024 1025

	/* map io memory */
	IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
		   itv->base_addr + IVTV_ENCODER_OFFSET, IVTV_ENCODER_SIZE);
	itv->enc_mem = ioremap_nocache(itv->base_addr + IVTV_ENCODER_OFFSET,
				       IVTV_ENCODER_SIZE);
	if (!itv->enc_mem) {
		IVTV_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
1026
		IVTV_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
		retval = -ENOMEM;
		goto free_mem;
	}

	if (itv->has_cx23415) {
		IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
				itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
		itv->dec_mem = ioremap_nocache(itv->base_addr + IVTV_DECODER_OFFSET,
				IVTV_DECODER_SIZE);
		if (!itv->dec_mem) {
			IVTV_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
1038
			IVTV_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
			retval = -ENOMEM;
			goto free_mem;
		}
	}
	else {
		itv->dec_mem = itv->enc_mem;
	}

	/* map registers memory */
	IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
		   itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
	itv->reg_mem =
	    ioremap_nocache(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
	if (!itv->reg_mem) {
		IVTV_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
1054
		IVTV_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
1055 1056 1057 1058
		retval = -ENOMEM;
		goto free_io;
	}

1059 1060 1061
	retval = ivtv_gpio_init(itv);
	if (retval)
		goto free_io;
1062 1063 1064 1065 1066

	/* active i2c  */
	IVTV_DEBUG_INFO("activating i2c...\n");
	if (init_ivtv_i2c(itv)) {
		IVTV_ERR("Could not initialize i2c\n");
1067
		goto free_io;
1068 1069 1070 1071 1072 1073 1074
	}

	if (itv->card->hw_all & IVTV_HW_TVEEPROM) {
		/* Based on the model number the cardtype may be changed.
		   The PCI IDs are not always reliable. */
		ivtv_process_eeprom(itv);
	}
1075 1076 1077 1078 1079 1080 1081
	if (itv->card->comment)
		IVTV_INFO("%s", itv->card->comment);
	if (itv->card->v4l2_capabilities == 0) {
		/* card was detected but is not supported */
		retval = -ENODEV;
		goto free_i2c;
	}
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

	if (itv->std == 0) {
		itv->std = V4L2_STD_NTSC_M;
	}

	if (itv->options.tuner == -1) {
		int i;

		for (i = 0; i < IVTV_CARD_MAX_TUNERS; i++) {
			if ((itv->std & itv->card->tuners[i].std) == 0)
				continue;
			itv->options.tuner = itv->card->tuners[i].tuner;
			break;
		}
	}
	/* if no tuner was found, then pick the first tuner in the card list */
	if (itv->options.tuner == -1 && itv->card->tuners[0].std) {
		itv->std = itv->card->tuners[0].std;
1100 1101 1102 1103 1104 1105
		if (itv->std & V4L2_STD_PAL)
			itv->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
		else if (itv->std & V4L2_STD_NTSC)
			itv->std = V4L2_STD_NTSC_M;
		else if (itv->std & V4L2_STD_SECAM)
			itv->std = V4L2_STD_SECAM_L;
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
		itv->options.tuner = itv->card->tuners[0].tuner;
	}
	if (itv->options.radio == -1)
		itv->options.radio = (itv->card->radio_input.audio_type != 0);

	/* The card is now fully identified, continue with card-specific
	   initialization. */
	ivtv_init_struct2(itv);

	ivtv_load_and_init_modules(itv);

	if (itv->std & V4L2_STD_525_60) {
		itv->is_60hz = 1;
		itv->is_out_60hz = 1;
	} else {
		itv->is_50hz = 1;
		itv->is_out_50hz = 1;
	}
1124 1125 1126 1127 1128 1129

	itv->yuv_info.osd_full_w = 720;
	itv->yuv_info.osd_full_h = itv->is_out_50hz ? 576 : 480;
	itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;
	itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;

1130 1131 1132 1133 1134
	itv->params.video_gop_size = itv->is_60hz ? 15 : 12;

	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;
1135 1136
	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = 0x10000;
	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = 0x08000;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

	/* Setup VBI Raw Size. Should be big enough to hold PAL.
	   It is possible to switch between PAL and NTSC, so we need to
	   take the largest size here. */
	/* 1456 is multiple of 16, real size = 1444 */
	itv->vbi.raw_size = 1456;
	/* We use a buffer size of 1/2 of the total size needed for a
	   frame. This is actually very useful, since we now receive
	   a field at a time and that makes 'compressing' the raw data
	   down to size by stripping off the SAV codes a lot easier.
	   Note: having two different buffer sizes prevents standard
	   switching on the fly. We need to find a better solution... */
	vbi_buf_size = itv->vbi.raw_size * (itv->is_60hz ? 24 : 36) / 2;
	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_VBI] = sizeof(struct v4l2_sliced_vbi_data) * 36;

	if (itv->options.radio > 0)
		itv->v4l2_cap |= V4L2_CAP_RADIO;

1156
	if (itv->options.tuner > -1) {
1157 1158 1159 1160 1161
		struct tuner_setup setup;

		setup.addr = ADDR_UNSET;
		setup.type = itv->options.tuner;
		setup.mode_mask = T_ANALOG_TV;  /* matches TV tuners */
1162 1163
		setup.tuner_callback = (setup.type == TUNER_XC2028) ?
			ivtv_reset_tuner_gpio : NULL;
1164
		ivtv_call_all(itv, tuner, s_type_addr, &setup);
1165 1166 1167 1168 1169 1170 1171 1172 1173
		if (setup.type == TUNER_XC2028) {
			static struct xc2028_ctrl ctrl = {
				.fname = XC2028_DEFAULT_FIRMWARE,
				.max_len = 64,
			};
			struct v4l2_priv_tun_config cfg = {
				.tuner = itv->options.tuner,
				.priv = &ctrl,
			};
1174
			ivtv_call_all(itv, tuner, s_config, &cfg);
1175
		}
1176 1177 1178 1179 1180 1181
	}

	/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
	   are not. */
	itv->tuner_std = itv->std;

1182
	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1183
		ivtv_call_all(itv, video, s_std_output, itv->std);
1184 1185 1186
		/* Turn off the output signal. The mpeg decoder is not yet
		   active so without this you would get a green image until the
		   mpeg decoder becomes active. */
1187
		ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
1188 1189
	}

1190 1191 1192 1193
	/* clear interrupt mask, effectively disabling interrupts */
	ivtv_set_irq_mask(itv, 0xffffffff);

	/* Register IRQ */
1194 1195
	retval = request_irq(itv->pdev->irq, ivtv_irq_handler,
	     IRQF_SHARED | IRQF_DISABLED, itv->v4l2_dev.name, (void *)itv);
1196 1197
	if (retval) {
		IVTV_ERR("Failed to register irq %d\n", retval);
1198 1199 1200 1201 1202 1203 1204
		goto free_i2c;
	}

	retval = ivtv_streams_setup(itv);
	if (retval) {
		IVTV_ERR("Error %d setting up streams\n", retval);
		goto free_irq;
1205
	}
1206 1207 1208
	retval = ivtv_streams_register(itv);
	if (retval) {
		IVTV_ERR("Error %d registering devices\n", retval);
1209
		goto free_streams;
1210
	}
1211
	IVTV_INFO("Initialized card: %s\n", itv->card_name);
1212 1213
	return 0;

1214
free_streams:
1215
	ivtv_streams_cleanup(itv, 1);
1216
free_irq:
1217
	free_irq(itv->pdev->irq, (void *)itv);
1218
free_i2c:
1219
	exit_ivtv_i2c(itv);
1220
free_io:
1221
	ivtv_iounmap(itv);
1222
free_mem:
1223 1224 1225 1226
	release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
	release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
	if (itv->has_cx23415)
		release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1227 1228
free_worker:
	kthread_stop(itv->irq_worker_task);
1229
err:
1230 1231 1232 1233
	if (retval == 0)
		retval = -ENODEV;
	IVTV_ERR("Error %d on initialization\n", retval);

1234
	v4l2_device_unregister(&itv->v4l2_dev);
1235
	kfree(itv);
1236 1237 1238
	return retval;
}

1239 1240 1241
int ivtv_init_on_first_open(struct ivtv *itv)
{
	struct v4l2_frequency vf;
1242 1243
	/* Needed to call ioctls later */
	struct ivtv_open_id fh;
1244 1245 1246
	int fw_retry_count = 3;
	int video_input;

1247 1248
	fh.itv = itv;

1249 1250 1251 1252 1253 1254
	if (test_bit(IVTV_F_I_FAILED, &itv->i_flags))
		return -ENXIO;

	if (test_and_set_bit(IVTV_F_I_INITED, &itv->i_flags))
		return 0;

1255 1256 1257 1258 1259 1260 1261
	while (--fw_retry_count > 0) {
		/* load firmware */
		if (ivtv_firmware_init(itv) == 0)
			break;
		if (fw_retry_count > 1)
			IVTV_WARN("Retry loading firmware\n");
	}
1262

1263
	if (fw_retry_count == 0) {
1264 1265
		set_bit(IVTV_F_I_FAILED, &itv->i_flags);
		return -ENXIO;
1266 1267 1268 1269 1270 1271
	}

	/* Try and get firmware versions */
	IVTV_DEBUG_INFO("Getting firmware version..\n");
	ivtv_firmware_versions(itv);

1272
	if (itv->card->hw_all & IVTV_HW_CX25840)
1273
		v4l2_subdev_call(itv->sd_video, core, load_fw);
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

	vf.tuner = 0;
	vf.type = V4L2_TUNER_ANALOG_TV;
	vf.frequency = 6400; /* the tuner 'baseline' frequency */

	/* Set initial frequency. For PAL/SECAM broadcasts no
	   'default' channel exists AFAIK. */
	if (itv->std == V4L2_STD_NTSC_M_JP) {
		vf.frequency = 1460;	/* ch. 1 91250*16/1000 */
	}
	else if (itv->std & V4L2_STD_NTSC_M) {
		vf.frequency = 1076;	/* ch. 4 67250*16/1000 */
	}

	video_input = itv->active_input;
	itv->active_input++;	/* Force update of input */
1290
	ivtv_s_input(NULL, &fh, video_input);
1291 1292 1293 1294 1295

	/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
	   in one place. */
	itv->std++;		/* Force full standard initialization */
	itv->std_out = itv->std;
1296
	ivtv_s_frequency(NULL, &fh, &vf);
1297 1298

	if (itv->card->v4l2_capabilities & V4L2_CAP_VIDEO_OUTPUT) {
1299 1300 1301
		/* Turn on the TV-out: ivtv_init_mpeg_decoder() initializes
		   the mpeg decoder so now the saa7127 receives a proper
		   signal. */
1302
		ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
		ivtv_init_mpeg_decoder(itv);
	}

	/* On a cx23416 this seems to be able to enable DMA to the chip? */
	if (!itv->has_cx23415)
		write_reg_sync(0x03, IVTV_REG_DMACONTROL);

	/* Default interrupts enabled. For the PVR350 this includes the
	   decoder VSYNC interrupt, which is always on. It is not only used
	   during decoding but also by the OSD.
	   Some old PVR250 cards had a cx23415, so testing for that is too
	   general. Instead test if the card has video output capability. */
	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT | IVTV_IRQ_DEC_VSYNC);
		ivtv_set_osd_alpha(itv);
	}
	else
		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT);
1321 1322 1323 1324

	/* For cards with video out, this call needs interrupts enabled */
	ivtv_s_std(NULL, &fh, &itv->tuner_std);

1325 1326 1327
	return 0;
}

1328
static void ivtv_remove(struct pci_dev *pdev)
1329
{
1330 1331
	struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
	struct ivtv *itv = to_ivtv(v4l2_dev);
1332
	int i;
1333

1334
	IVTV_DEBUG_INFO("Removing card\n");
1335

1336 1337 1338 1339 1340 1341 1342 1343
	if (test_bit(IVTV_F_I_INITED, &itv->i_flags)) {
		/* Stop all captures */
		IVTV_DEBUG_INFO("Stopping all streams\n");
		if (atomic_read(&itv->capturing) > 0)
			ivtv_stop_all_captures(itv);

		/* Stop all decoding */
		IVTV_DEBUG_INFO("Stopping decoding\n");
1344 1345 1346

		/* Turn off the TV-out */
		if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1347
			ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		if (atomic_read(&itv->decoding) > 0) {
			int type;

			if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
				type = IVTV_DEC_STREAM_TYPE_YUV;
			else
				type = IVTV_DEC_STREAM_TYPE_MPG;
			ivtv_stop_v4l2_decode_stream(&itv->streams[type],
				VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
		}
		ivtv_halt_firmware(itv);
1359 1360 1361 1362 1363 1364
	}

	/* Interrupts */
	ivtv_set_irq_mask(itv, 0xffffffff);
	del_timer_sync(&itv->dma_timer);

1365 1366 1367
	/* Kill irq worker */
	flush_kthread_worker(&itv->irq_worker);
	kthread_stop(itv->irq_worker_task);
1368

1369
	ivtv_streams_cleanup(itv, 1);
1370 1371 1372 1373
	ivtv_udma_free(itv);

	exit_ivtv_i2c(itv);

1374
	free_irq(itv->pdev->irq, (void *)itv);
1375
	ivtv_iounmap(itv);
1376 1377 1378 1379 1380 1381

	release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
	release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
	if (itv->has_cx23415)
		release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);

1382
	pci_disable_device(itv->pdev);
1383 1384
	for (i = 0; i < IVTV_VBI_FRAMES; i++)
		kfree(itv->vbi.sliced_mpeg_data[i]);
1385

1386
	printk(KERN_INFO "ivtv: Removed %s\n", itv->card_name);
1387 1388

	v4l2_device_unregister(&itv->v4l2_dev);
1389
	kfree(itv);
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
}

/* define a pci_driver for card detection */
static struct pci_driver ivtv_pci_driver = {
      .name =     "ivtv",
      .id_table = ivtv_pci_tbl,
      .probe =    ivtv_probe,
      .remove =   ivtv_remove,
};

1400
static int __init module_start(void)
1401
{
1402
	printk(KERN_INFO "ivtv: Start initialization, version %s\n", IVTV_VERSION);
1403 1404 1405

	/* Validate parameters */
	if (ivtv_first_minor < 0 || ivtv_first_minor >= IVTV_MAX_CARDS) {
1406
		printk(KERN_ERR "ivtv: Exiting, ivtv_first_minor must be between 0 and %d\n",
1407 1408 1409 1410
		     IVTV_MAX_CARDS - 1);
		return -1;
	}

1411
	if (ivtv_debug < 0 || ivtv_debug > 2047) {
1412
		ivtv_debug = 0;
1413
		printk(KERN_INFO "ivtv: Debug value must be >= 0 and <= 2047\n");
1414 1415
	}

1416
	if (pci_register_driver(&ivtv_pci_driver)) {
1417
		printk(KERN_ERR "ivtv: Error detecting PCI card\n");
1418 1419
		return -ENODEV;
	}
1420
	printk(KERN_INFO "ivtv: End initialization\n");
1421 1422 1423
	return 0;
}

1424
static void __exit module_cleanup(void)
1425
{
1426
	pci_unregister_driver(&ivtv_pci_driver);
1427 1428
}

1429
/* Note: These symbols are exported because they are used by the ivtvfb
1430
   framebuffer module and an infrared module for the IR-blaster. */
1431 1432 1433 1434 1435 1436
EXPORT_SYMBOL(ivtv_set_irq_mask);
EXPORT_SYMBOL(ivtv_api);
EXPORT_SYMBOL(ivtv_vapi);
EXPORT_SYMBOL(ivtv_vapi_result);
EXPORT_SYMBOL(ivtv_clear_irq_mask);
EXPORT_SYMBOL(ivtv_debug);
1437 1438 1439
#ifdef CONFIG_VIDEO_ADV_DEBUG
EXPORT_SYMBOL(ivtv_fw_debug);
#endif
1440 1441 1442 1443 1444
EXPORT_SYMBOL(ivtv_reset_ir_gpio);
EXPORT_SYMBOL(ivtv_udma_setup);
EXPORT_SYMBOL(ivtv_udma_unmap);
EXPORT_SYMBOL(ivtv_udma_alloc);
EXPORT_SYMBOL(ivtv_udma_prepare);
1445
EXPORT_SYMBOL(ivtv_init_on_first_open);
1446
EXPORT_SYMBOL(ivtv_firmware_check);
1447 1448 1449

module_init(module_start);
module_exit(module_cleanup);