File Index
Files and directories
Sign in
FILE

ivtv-driver.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/pci/ivtv/ivtv-driver.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3    ivtv driver initialization and card probing
   4    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
   5    Copyright (C) 2004  Chris Kennedy <c@groovy.org>
   6    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@kernel.org>
   7 */
   8
   9/* Main Driver file for the ivtv project:
  10 * Driver for the Conexant CX23415/CX23416 chip.
  11 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
  12 * License: GPL
  13 *
  14 * -----
  15 * MPG600/MPG160 support by  T.Adachi <tadachi@tadachi-net.com>
  16 *                      and Takeru KOMORIYA<komoriya@paken.org>
  17 *
  18 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
  19 *                using information provided by Jiun-Kuei Jung @ AVerMedia.
  20 *
  21 * Kurouto Sikou CX23416GYC-STVLP tested by K.Ohta <alpha292@bremen.or.jp>
  22 *                using information from T.Adachi,Takeru KOMORIYA and others :-)
  23 *
  24 * Nagase TRANSGEAR 5000TV, Aopen VA2000MAX-STN6 and I/O data GV-MVP/RX
  25 *                version by T.Adachi. Special thanks  Mr.Suzuki
  26 */
  27
  28#include "ivtv-driver.h"
  29#include "ivtv-version.h"
  30#include "ivtv-fileops.h"
  31#include "ivtv-i2c.h"
  32#include "ivtv-firmware.h"
  33#include "ivtv-queue.h"
  34#include "ivtv-udma.h"
  35#include "ivtv-irq.h"
  36#include "ivtv-mailbox.h"
  37#include "ivtv-streams.h"
  38#include "ivtv-ioctl.h"
  39#include "ivtv-cards.h"
  40#include "ivtv-vbi.h"
  41#include "ivtv-routing.h"
  42#include "ivtv-controls.h"
  43#include "ivtv-gpio.h"
  44#include <linux/dma-mapping.h>
  45#include <media/tveeprom.h>
  46#include <media/i2c/saa7115.h>
  47#include "xc2028.h"
  48#include <uapi/linux/sched/types.h>
  49
  50/* If you have already X v4l cards, then set this to X. This way
  51   the device numbers stay matched. Example: you have a WinTV card
  52   without radio and a PVR-350 with. Normally this would give a
  53   video1 device together with a radio0 device for the PVR. By
  54   setting this to 1 you ensure that radio0 is now also radio1. */
  55int ivtv_first_minor;
  56
  57/* Callback for registering extensions */
  58int (*ivtv_ext_init)(struct ivtv *);
  59EXPORT_SYMBOL(ivtv_ext_init);
  60
  61/* add your revision and whatnot here */
  62static const struct pci_device_id ivtv_pci_tbl[] = {
  63	{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV15,
  64	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  65	{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV16,
  66	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  67	{0,}
  68};
  69
  70MODULE_DEVICE_TABLE(pci,ivtv_pci_tbl);
  71
  72/* ivtv instance counter */
  73static atomic_t ivtv_instance = ATOMIC_INIT(0);
  74
  75/* Parameter declarations */
  76static int cardtype[IVTV_MAX_CARDS];
  77static int tuner[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
  78				     -1, -1, -1, -1, -1, -1, -1, -1,
  79				     -1, -1, -1, -1, -1, -1, -1, -1,
  80				     -1, -1, -1, -1, -1, -1, -1, -1 };
  81static int radio[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
  82				     -1, -1, -1, -1, -1, -1, -1, -1,
  83				     -1, -1, -1, -1, -1, -1, -1, -1,
  84				     -1, -1, -1, -1, -1, -1, -1, -1 };
  85static int i2c_clock_period[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
  86					       -1, -1, -1, -1, -1, -1, -1, -1,
  87					       -1, -1, -1, -1, -1, -1, -1, -1,
  88					       -1, -1, -1, -1, -1, -1, -1, -1 };
  89
  90static unsigned int cardtype_c = 1;
  91static unsigned int tuner_c = 1;
  92static int radio_c = 1;
  93static unsigned int i2c_clock_period_c = 1;
  94static char pal[] = "---";
  95static char secam[] = "--";
  96static char ntsc[] = "-";
  97
  98/* Buffers */
  99
 100/* DMA Buffers, Default size in MB allocated */
 101#define IVTV_DEFAULT_ENC_MPG_BUFFERS 4
 102#define IVTV_DEFAULT_ENC_YUV_BUFFERS 2
 103#define IVTV_DEFAULT_ENC_VBI_BUFFERS 1
 104/* Exception: size in kB for this stream (MB is overkill) */
 105#define IVTV_DEFAULT_ENC_PCM_BUFFERS 320
 106#define IVTV_DEFAULT_DEC_MPG_BUFFERS 1
 107#define IVTV_DEFAULT_DEC_YUV_BUFFERS 1
 108/* Exception: size in kB for this stream (MB is way overkill) */
 109#define IVTV_DEFAULT_DEC_VBI_BUFFERS 64
 110
 111static int enc_mpg_buffers = IVTV_DEFAULT_ENC_MPG_BUFFERS;
 112static int enc_yuv_buffers = IVTV_DEFAULT_ENC_YUV_BUFFERS;
 113static int enc_vbi_buffers = IVTV_DEFAULT_ENC_VBI_BUFFERS;
 114static int enc_pcm_buffers = IVTV_DEFAULT_ENC_PCM_BUFFERS;
 115static int dec_mpg_buffers = IVTV_DEFAULT_DEC_MPG_BUFFERS;
 116static int dec_yuv_buffers = IVTV_DEFAULT_DEC_YUV_BUFFERS;
 117static int dec_vbi_buffers = IVTV_DEFAULT_DEC_VBI_BUFFERS;
 118
 119static int ivtv_yuv_mode;
 120static int ivtv_yuv_threshold = -1;
 121static int ivtv_pci_latency = 1;
 122
 123int ivtv_debug;
 124#ifdef CONFIG_VIDEO_ADV_DEBUG
 125int ivtv_fw_debug;
 126#endif
 127
 128static int tunertype = -1;
 129static int newi2c = -1;
 130
 131module_param_array(tuner, int, &tuner_c, 0644);
 132module_param_array(radio, int, &radio_c, 0644);
 133module_param_array(cardtype, int, &cardtype_c, 0644);
 134module_param_string(pal, pal, sizeof(pal), 0644);
 135module_param_string(secam, secam, sizeof(secam), 0644);
 136module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
 137module_param_named(debug,ivtv_debug, int, 0644);
 138#ifdef CONFIG_VIDEO_ADV_DEBUG
 139module_param_named(fw_debug, ivtv_fw_debug, int, 0644);
 140#endif
 141module_param(ivtv_pci_latency, int, 0644);
 142module_param(ivtv_yuv_mode, int, 0644);
 143module_param(ivtv_yuv_threshold, int, 0644);
 144module_param(ivtv_first_minor, int, 0644);
 145
 146module_param(enc_mpg_buffers, int, 0644);
 147module_param(enc_yuv_buffers, int, 0644);
 148module_param(enc_vbi_buffers, int, 0644);
 149module_param(enc_pcm_buffers, int, 0644);
 150module_param(dec_mpg_buffers, int, 0644);
 151module_param(dec_yuv_buffers, int, 0644);
 152module_param(dec_vbi_buffers, int, 0644);
 153
 154module_param(tunertype, int, 0644);
 155module_param(newi2c, int, 0644);
 156module_param_array(i2c_clock_period, int, &i2c_clock_period_c, 0644);
 157
 158MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
 159			"\t\t\tsee tuner.h for values");
 160MODULE_PARM_DESC(radio,
 161		 "Enable or disable the radio. Use only if autodetection\n"
 162		 "\t\t\tfails. 0 = disable, 1 = enable");
 163MODULE_PARM_DESC(cardtype,
 164		 "Only use this option if your card is not detected properly.\n"
 165		 "\t\tSpecify card type:\n"
 166		 "\t\t\t 1 = WinTV PVR 250\n"
 167		 "\t\t\t 2 = WinTV PVR 350\n"
 168		 "\t\t\t 3 = WinTV PVR-150 or PVR-500\n"
 169		 "\t\t\t 4 = AVerMedia M179\n"
 170		 "\t\t\t 5 = YUAN MPG600/Kuroutoshikou iTVC16-STVLP\n"
 171		 "\t\t\t 6 = YUAN MPG160/Kuroutoshikou iTVC15-STVLP\n"
 172		 "\t\t\t 7 = YUAN PG600/DIAMONDMM PVR-550 (CX Falcon 2)\n"
 173		 "\t\t\t 8 = Adaptec AVC-2410\n"
 174		 "\t\t\t 9 = Adaptec AVC-2010\n"
 175		 "\t\t\t10 = NAGASE TRANSGEAR 5000TV\n"
 176		 "\t\t\t11 = AOpen VA2000MAX-STN6\n"
 177		 "\t\t\t12 = YUAN MPG600GR/Kuroutoshikou CX23416GYC-STVLP\n"
 178		 "\t\t\t13 = I/O Data GV-MVP/RX\n"
 179		 "\t\t\t14 = I/O Data GV-MVP/RX2E\n"
 180		 "\t\t\t15 = GOTVIEW PCI DVD\n"
 181		 "\t\t\t16 = GOTVIEW PCI DVD2 Deluxe\n"
 182		 "\t\t\t17 = Yuan MPC622\n"
 183		 "\t\t\t18 = Digital Cowboy DCT-MTVP1\n"
 184		 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"
 185		 "\t\t\t20 = Club3D ZAP-TV1x01\n"
 186		 "\t\t\t21 = AverTV MCE 116 Plus\n"
 187		 "\t\t\t22 = ASUS Falcon2\n"
 188		 "\t\t\t23 = AverMedia PVR-150 Plus\n"
 189		 "\t\t\t24 = AverMedia EZMaker PCI Deluxe\n"
 190		 "\t\t\t25 = AverMedia M104 (not yet working)\n"
 191		 "\t\t\t26 = Buffalo PC-MV5L/PCI\n"
 192		 "\t\t\t27 = AVerMedia UltraTV 1500 MCE\n"
 193		 "\t\t\t28 = Sony VAIO Giga Pocket (ENX Kikyou)\n"
 194		 "\t\t\t 0 = Autodetect (default)\n"
 195		 "\t\t\t-1 = Ignore this card\n\t\t");
 196MODULE_PARM_DESC(pal, "Set PAL standard: BGH, DK, I, M, N, Nc, 60");
 197MODULE_PARM_DESC(secam, "Set SECAM standard: BGH, DK, L, LC");
 198MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J (Japan), K (South Korea)");
 199MODULE_PARM_DESC(tunertype,
 200		"Specify tuner type:\n"
 201		"\t\t\t 0 = tuner for PAL-B/G/H/D/K/I, SECAM-B/G/H/D/K/L/Lc\n"
 202		"\t\t\t 1 = tuner for NTSC-M/J/K, PAL-M/N/Nc\n"
 203		"\t\t\t-1 = Autodetect (default)\n");
 204MODULE_PARM_DESC(debug,
 205		 "Debug level (bitmask). Default: 0\n"
 206		 "\t\t\t   1/0x0001: warning\n"
 207		 "\t\t\t   2/0x0002: info\n"
 208		 "\t\t\t   4/0x0004: mailbox\n"
 209		 "\t\t\t   8/0x0008: ioctl\n"
 210		 "\t\t\t  16/0x0010: file\n"
 211		 "\t\t\t  32/0x0020: dma\n"
 212		 "\t\t\t  64/0x0040: irq\n"
 213		 "\t\t\t 128/0x0080: decoder\n"
 214		 "\t\t\t 256/0x0100: yuv\n"
 215		 "\t\t\t 512/0x0200: i2c\n"
 216		 "\t\t\t1024/0x0400: high volume\n");
 217#ifdef CONFIG_VIDEO_ADV_DEBUG
 218MODULE_PARM_DESC(fw_debug,
 219		 "Enable code for debugging firmware problems.  Default: 0\n");
 220#endif
 221MODULE_PARM_DESC(ivtv_pci_latency,
 222		 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
 223		 "\t\t\tDefault: Yes");
 224MODULE_PARM_DESC(ivtv_yuv_mode,
 225		 "Specify the yuv playback mode:\n"
 226		 "\t\t\t0 = interlaced\n\t\t\t1 = progressive\n\t\t\t2 = auto\n"
 227		 "\t\t\tDefault: 0 (interlaced)");
 228MODULE_PARM_DESC(ivtv_yuv_threshold,
 229		 "If ivtv_yuv_mode is 2 (auto) then playback content as\n\t\tprogressive if src height <= ivtv_yuvthreshold\n"
 230		 "\t\t\tDefault: 480");
 231MODULE_PARM_DESC(enc_mpg_buffers,
 232		 "Encoder MPG Buffers (in MB)\n"
 233		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_MPG_BUFFERS));
 234MODULE_PARM_DESC(enc_yuv_buffers,
 235		 "Encoder YUV Buffers (in MB)\n"
 236		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_YUV_BUFFERS));
 237MODULE_PARM_DESC(enc_vbi_buffers,
 238		 "Encoder VBI Buffers (in MB)\n"
 239		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_VBI_BUFFERS));
 240MODULE_PARM_DESC(enc_pcm_buffers,
 241		 "Encoder PCM buffers (in kB)\n"
 242		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_PCM_BUFFERS));
 243MODULE_PARM_DESC(dec_mpg_buffers,
 244		 "Decoder MPG buffers (in MB)\n"
 245		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_MPG_BUFFERS));
 246MODULE_PARM_DESC(dec_yuv_buffers,
 247		 "Decoder YUV buffers (in MB)\n"
 248		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_YUV_BUFFERS));
 249MODULE_PARM_DESC(dec_vbi_buffers,
 250		 "Decoder VBI buffers (in kB)\n"
 251		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_VBI_BUFFERS));
 252MODULE_PARM_DESC(newi2c,
 253		 "Use new I2C implementation\n"
 254		 "\t\t\t-1 is autodetect, 0 is off, 1 is on\n"
 255		 "\t\t\tDefault is autodetect");
 256MODULE_PARM_DESC(i2c_clock_period,
 257		 "Period of SCL for the I2C bus controlled by the CX23415/6\n"
 258		 "\t\t\tMin: 10 usec (100 kHz), Max: 4500 usec (222 Hz)\n"
 259		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_I2C_CLOCK_PERIOD));
 260
 261MODULE_PARM_DESC(ivtv_first_minor, "Set device node number assigned to first card");
 262
 263MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");
 264MODULE_DESCRIPTION("CX23415/CX23416 driver");
 265MODULE_LICENSE("GPL");
 266
 267MODULE_VERSION(IVTV_VERSION);
 268
 269#if defined(CONFIG_MODULES) && defined(MODULE)
 270static void request_module_async(struct work_struct *work)
 271{
 272	struct ivtv *dev = container_of(work, struct ivtv, request_module_wk);
 273
 274	/* Make sure ivtv-alsa module is loaded */
 275	request_module("ivtv-alsa");
 276
 277	/* Initialize ivtv-alsa for this instance of the cx18 device */
 278	if (ivtv_ext_init != NULL)
 279		ivtv_ext_init(dev);
 280}
 281
 282static void request_modules(struct ivtv *dev)
 283{
 284	INIT_WORK(&dev->request_module_wk, request_module_async);
 285	schedule_work(&dev->request_module_wk);
 286}
 287
 288static void flush_request_modules(struct ivtv *dev)
 289{
 290	flush_work(&dev->request_module_wk);
 291}
 292#else
 293#define request_modules(dev)
 294#define flush_request_modules(dev)
 295#endif /* CONFIG_MODULES */
 296
 297void ivtv_clear_irq_mask(struct ivtv *itv, u32 mask)
 298{
 299	itv->irqmask &= ~mask;
 300	write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
 301}
 302
 303void ivtv_set_irq_mask(struct ivtv *itv, u32 mask)
 304{
 305	itv->irqmask |= mask;
 306	write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
 307}
 308
 309int ivtv_set_output_mode(struct ivtv *itv, int mode)
 310{
 311    int old_mode;
 312
 313    spin_lock(&itv->lock);
 314    old_mode = itv->output_mode;
 315    if (old_mode == 0)
 316	itv->output_mode = old_mode = mode;
 317    spin_unlock(&itv->lock);
 318    return old_mode;
 319}
 320
 321struct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv)
 322{
 323	switch (itv->output_mode) {
 324	case OUT_MPG:
 325		return &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
 326	case OUT_YUV:
 327		return &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
 328	default:
 329		return NULL;
 330	}
 331}
 332
 333int ivtv_waitq(wait_queue_head_t *waitq)
 334{
 335	DEFINE_WAIT(wait);
 336
 337	prepare_to_wait(waitq, &wait, TASK_INTERRUPTIBLE);
 338	schedule();
 339	finish_wait(waitq, &wait);
 340	return signal_pending(current) ? -EINTR : 0;
 341}
 342
 343/* Generic utility functions */
 344int ivtv_msleep_timeout(unsigned int msecs, int intr)
 345{
 346	int timeout = msecs_to_jiffies(msecs);
 347
 348	do {
 349		set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
 350		timeout = schedule_timeout(timeout);
 351		if (intr) {
 352			int ret = signal_pending(current);
 353
 354			if (ret)
 355				return ret;
 356		}
 357	} while (timeout);
 358	return 0;
 359}
 360
 361/* Hauppauge card? get values from tveeprom */
 362void ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv)
 363{
 364	u8 eedata[256];
 365
 366	itv->i2c_client.addr = 0xA0 >> 1;
 367	tveeprom_read(&itv->i2c_client, eedata, sizeof(eedata));
 368	tveeprom_hauppauge_analog(tv, eedata);
 369}
 370
 371static void ivtv_process_eeprom(struct ivtv *itv)
 372{
 373	struct tveeprom tv;
 374	int pci_slot = PCI_SLOT(itv->pdev->devfn);
 375
 376	ivtv_read_eeprom(itv, &tv);
 377
 378	/* Many thanks to Steven Toth from Hauppauge for providing the
 379	   model numbers */
 380	switch (tv.model) {
 381		/* In a few cases the PCI subsystem IDs do not correctly
 382		   identify the card. A better method is to check the
 383		   model number from the eeprom instead. */
 384		case 30012 ... 30039:  /* Low profile PVR250 */
 385		case 32000 ... 32999:
 386		case 48000 ... 48099:  /* 48??? range are PVR250s with a cx23415 */
 387		case 48400 ... 48599:
 388			itv->card = ivtv_get_card(IVTV_CARD_PVR_250);
 389			break;
 390		case 48100 ... 48399:
 391		case 48600 ... 48999:
 392			itv->card = ivtv_get_card(IVTV_CARD_PVR_350);
 393			break;
 394		case 23000 ... 23999:  /* PVR500 */
 395		case 25000 ... 25999:  /* Low profile PVR150 */
 396		case 26000 ... 26999:  /* Regular PVR150 */
 397			itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
 398			break;
 399		case 0:
 400			IVTV_ERR("Invalid EEPROM\n");
 401			return;
 402		default:
 403			IVTV_ERR("Unknown model %d, defaulting to PVR-150\n", tv.model);
 404			itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
 405			break;
 406	}
 407
 408	switch (tv.model) {
 409		/* Old style PVR350 (with an saa7114) uses this input for
 410		   the tuner. */
 411		case 48254:
 412			itv->card = ivtv_get_card(IVTV_CARD_PVR_350_V1);
 413			break;
 414		default:
 415			break;
 416	}
 417
 418	itv->v4l2_cap = itv->card->v4l2_capabilities;
 419	itv->card_name = itv->card->name;
 420	itv->card_i2c = itv->card->i2c;
 421
 422	/* If this is a PVR500 then it should be possible to detect whether it is the
 423	   first or second unit by looking at the subsystem device ID: is bit 4 is
 424	   set, then it is the second unit (according to info from Hauppauge).
 425
 426	   However, while this works for most cards, I have seen a few PVR500 cards
 427	   where both units have the same subsystem ID.
 428
 429	   So instead I look at the reported 'PCI slot' (which is the slot on the PVR500
 430	   PCI bridge) and if it is 8, then it is assumed to be the first unit, otherwise
 431	   it is the second unit. It is possible that it is a different slot when ivtv is
 432	   used in Xen, in that case I ignore this card here. The worst that can happen
 433	   is that the card presents itself with a non-working radio device.
 434
 435	   This detection is needed since the eeprom reports incorrectly that a radio is
 436	   present on the second unit. */
 437	if (tv.model / 1000 == 23) {
 438		static const struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
 439			.radio = { 0x60, I2C_CLIENT_END },
 440			.demod = { 0x43, I2C_CLIENT_END },
 441			.tv = { 0x61, I2C_CLIENT_END },
 442		};
 443
 444		itv->card_name = "WinTV PVR 500";
 445		itv->card_i2c = &ivtv_i2c_radio;
 446		if (pci_slot == 8 || pci_slot == 9) {
 447			int is_first = (pci_slot & 1) == 0;
 448
 449			itv->card_name = is_first ? "WinTV PVR 500 (unit #1)" :
 450						    "WinTV PVR 500 (unit #2)";
 451			if (!is_first) {
 452				IVTV_INFO("Correcting tveeprom data: no radio present on second unit\n");
 453				tv.has_radio = 0;
 454			}
 455		}
 456	}
 457	IVTV_INFO("Autodetected %s\n", itv->card_name);
 458
 459	switch (tv.tuner_hauppauge_model) {
 460		case 85:
 461		case 99:
 462		case 112:
 463			itv->pvr150_workaround = 1;
 464			break;
 465		default:
 466			break;
 467	}
 468	if (tv.tuner_type == TUNER_ABSENT)
 469		IVTV_ERR("tveeprom cannot autodetect tuner!\n");
 470
 471	if (itv->options.tuner == -1)
 472		itv->options.tuner = tv.tuner_type;
 473	if (itv->options.radio == -1)
 474		itv->options.radio = (tv.has_radio != 0);
 475	/* only enable newi2c if an IR blaster is present */
 476	if (itv->options.newi2c == -1 && tv.has_ir) {
 477		itv->options.newi2c = (tv.has_ir & 4) ? 1 : 0;
 478		if (itv->options.newi2c) {
 479		    IVTV_INFO("Reopen i2c bus for IR-blaster support\n");
 480		    exit_ivtv_i2c(itv);
 481		    init_ivtv_i2c(itv);
 482		}
 483	}
 484
 485	if (itv->std != 0)
 486		/* user specified tuner standard */
 487		return;
 488
 489	/* autodetect tuner standard */
 490	if (tv.tuner_formats & V4L2_STD_PAL) {
 491		IVTV_DEBUG_INFO("PAL tuner detected\n");
 492		itv->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
 493	} else if (tv.tuner_formats & V4L2_STD_NTSC) {
 494		IVTV_DEBUG_INFO("NTSC tuner detected\n");
 495		itv->std |= V4L2_STD_NTSC_M;
 496	} else if (tv.tuner_formats & V4L2_STD_SECAM) {
 497		IVTV_DEBUG_INFO("SECAM tuner detected\n");
 498		itv->std |= V4L2_STD_SECAM_L;
 499	} else {
 500		IVTV_INFO("No tuner detected, default to NTSC-M\n");
 501		itv->std |= V4L2_STD_NTSC_M;
 502	}
 503}
 504
 505static v4l2_std_id ivtv_parse_std(struct ivtv *itv)
 506{
 507	switch (pal[0]) {
 508		case '6':
 509			tunertype = 0;
 510			return V4L2_STD_PAL_60;
 511		case 'b':
 512		case 'B':
 513		case 'g':
 514		case 'G':
 515		case 'h':
 516		case 'H':
 517			tunertype = 0;
 518			return V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
 519		case 'n':
 520		case 'N':
 521			tunertype = 1;
 522			if (pal[1] == 'c' || pal[1] == 'C')
 523				return V4L2_STD_PAL_Nc;
 524			return V4L2_STD_PAL_N;
 525		case 'i':
 526		case 'I':
 527			tunertype = 0;
 528			return V4L2_STD_PAL_I;
 529		case 'd':
 530		case 'D':
 531		case 'k':
 532		case 'K':
 533			tunertype = 0;
 534			return V4L2_STD_PAL_DK;
 535		case 'M':
 536		case 'm':
 537			tunertype = 1;
 538			return V4L2_STD_PAL_M;
 539		case '-':
 540			break;
 541		default:
 542			IVTV_WARN("pal= argument not recognised\n");
 543			return 0;
 544	}
 545
 546	switch (secam[0]) {
 547		case 'b':
 548		case 'B':
 549		case 'g':
 550		case 'G':
 551		case 'h':
 552		case 'H':
 553			tunertype = 0;
 554			return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
 555		case 'd':
 556		case 'D':
 557		case 'k':
 558		case 'K':
 559			tunertype = 0;
 560			return V4L2_STD_SECAM_DK;
 561		case 'l':
 562		case 'L':
 563			tunertype = 0;
 564			if (secam[1] == 'C' || secam[1] == 'c')
 565				return V4L2_STD_SECAM_LC;
 566			return V4L2_STD_SECAM_L;
 567		case '-':
 568			break;
 569		default:
 570			IVTV_WARN("secam= argument not recognised\n");
 571			return 0;
 572	}
 573
 574	switch (ntsc[0]) {
 575		case 'm':
 576		case 'M':
 577			tunertype = 1;
 578			return V4L2_STD_NTSC_M;
 579		case 'j':
 580		case 'J':
 581			tunertype = 1;
 582			return V4L2_STD_NTSC_M_JP;
 583		case 'k':
 584		case 'K':
 585			tunertype = 1;
 586			return V4L2_STD_NTSC_M_KR;
 587		case '-':
 588			break;
 589		default:
 590			IVTV_WARN("ntsc= argument not recognised\n");
 591			return 0;
 592	}
 593
 594	/* no match found */
 595	return 0;
 596}
 597
 598static void ivtv_process_options(struct ivtv *itv)
 599{
 600	const char *chipname;
 601	int i, j;
 602
 603	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers * 1024;
 604	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers * 1024;
 605	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers * 1024;
 606	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
 607	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_MPG] = dec_mpg_buffers * 1024;
 608	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_YUV] = dec_yuv_buffers * 1024;
 609	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_VBI] = dec_vbi_buffers;
 610	itv->options.cardtype = cardtype[itv->instance];
 611	itv->options.tuner = tuner[itv->instance];
 612	itv->options.radio = radio[itv->instance];
 613
 614	itv->options.i2c_clock_period = i2c_clock_period[itv->instance];
 615	if (itv->options.i2c_clock_period == -1)
 616		itv->options.i2c_clock_period = IVTV_DEFAULT_I2C_CLOCK_PERIOD;
 617	else if (itv->options.i2c_clock_period < 10)
 618		itv->options.i2c_clock_period = 10;
 619	else if (itv->options.i2c_clock_period > 4500)
 620		itv->options.i2c_clock_period = 4500;
 621
 622	itv->options.newi2c = newi2c;
 623	if (tunertype < -1 || tunertype > 1) {
 624		IVTV_WARN("Invalid tunertype argument, will autodetect instead\n");
 625		tunertype = -1;
 626	}
 627	itv->std = ivtv_parse_std(itv);
 628	if (itv->std == 0 && tunertype >= 0)
 629		itv->std = tunertype ? V4L2_STD_MN : (V4L2_STD_ALL & ~V4L2_STD_MN);
 630	itv->has_cx23415 = (itv->pdev->device == PCI_DEVICE_ID_IVTV15);
 631	chipname = itv->has_cx23415 ? "cx23415" : "cx23416";
 632	if (itv->options.cardtype == -1) {
 633		IVTV_INFO("Ignore card (detected %s based chip)\n", chipname);
 634		return;
 635	}
 636	if ((itv->card = ivtv_get_card(itv->options.cardtype - 1))) {
 637		IVTV_INFO("User specified %s card (detected %s based chip)\n",
 638				itv->card->name, chipname);
 639	} else if (itv->options.cardtype != 0) {
 640		IVTV_ERR("Unknown user specified type, trying to autodetect card\n");
 641	}
 642	if (itv->card == NULL) {
 643		if (itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE ||
 644		    itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT1 ||
 645		    itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT2) {
 646			itv->card = ivtv_get_card(itv->has_cx23415 ? IVTV_CARD_PVR_350 : IVTV_CARD_PVR_150);
 647			IVTV_INFO("Autodetected Hauppauge card (%s based)\n",
 648					chipname);
 649		}
 650	}
 651	if (itv->card == NULL) {
 652		for (i = 0; (itv->card = ivtv_get_card(i)); i++) {
 653			if (itv->card->pci_list == NULL)
 654				continue;
 655			for (j = 0; itv->card->pci_list[j].device; j++) {
 656				if (itv->pdev->device !=
 657				    itv->card->pci_list[j].device)
 658					continue;
 659				if (itv->pdev->subsystem_vendor !=
 660				    itv->card->pci_list[j].subsystem_vendor)
 661					continue;
 662				if (itv->pdev->subsystem_device !=
 663				    itv->card->pci_list[j].subsystem_device)
 664					continue;
 665				IVTV_INFO("Autodetected %s card (%s based)\n",
 666						itv->card->name, chipname);
 667				goto done;
 668			}
 669		}
 670	}
 671done:
 672
 673	if (itv->card == NULL) {
 674		itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
 675		IVTV_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
 676		     itv->pdev->vendor, itv->pdev->device);
 677		IVTV_ERR("              subsystem vendor/device: [%04x:%04x]\n",
 678		     itv->pdev->subsystem_vendor, itv->pdev->subsystem_device);
 679		IVTV_ERR("              %s based\n", chipname);
 680		IVTV_ERR("Defaulting to %s card\n", itv->card->name);
 681		IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
 682		IVTV_ERR("card you have to the linux-media mailinglist (www.linuxtv.org)\n");
 683		IVTV_ERR("Prefix your subject line with [UNKNOWN IVTV CARD].\n");
 684	}
 685	itv->v4l2_cap = itv->card->v4l2_capabilities;
 686	itv->card_name = itv->card->name;
 687	itv->card_i2c = itv->card->i2c;
 688}
 689
 690/* Precondition: the ivtv structure has been memset to 0. Only
 691   the dev and num fields have been filled in.
 692   No assumptions on the card type may be made here (see ivtv_init_struct2
 693   for that).
 694 */
 695static int ivtv_init_struct1(struct ivtv *itv)
 696{
 697	itv->base_addr = pci_resource_start(itv->pdev, 0);
 698	itv->enc_mbox.max_mbox = 2; /* the encoder has 3 mailboxes (0-2) */
 699	itv->dec_mbox.max_mbox = 1; /* the decoder has 2 mailboxes (0-1) */
 700
 701	mutex_init(&itv->serialize_lock);
 702	mutex_init(&itv->i2c_bus_lock);
 703	mutex_init(&itv->udma.lock);
 704
 705	spin_lock_init(&itv->lock);
 706	spin_lock_init(&itv->dma_reg_lock);
 707
 708	kthread_init_worker(&itv->irq_worker);
 709	itv->irq_worker_task = kthread_run(kthread_worker_fn, &itv->irq_worker,
 710					   "%s", itv->v4l2_dev.name);
 711	if (IS_ERR(itv->irq_worker_task)) {
 712		IVTV_ERR("Could not create ivtv task\n");
 713		return -1;
 714	}
 715	/* must use the FIFO scheduler as it is realtime sensitive */
 716	sched_set_fifo(itv->irq_worker_task);
 717
 718	kthread_init_work(&itv->irq_work, ivtv_irq_work_handler);
 719
 720	/* Initial settings */
 721	itv->cxhdl.port = CX2341X_PORT_MEMORY;
 722	itv->cxhdl.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
 723	init_waitqueue_head(&itv->eos_waitq);
 724	init_waitqueue_head(&itv->event_waitq);
 725	init_waitqueue_head(&itv->vsync_waitq);
 726	init_waitqueue_head(&itv->dma_waitq);
 727	timer_setup(&itv->dma_timer, ivtv_unfinished_dma, 0);
 728
 729	itv->cur_dma_stream = -1;
 730	itv->cur_pio_stream = -1;
 731
 732	/* Ctrls */
 733	itv->speed = 1000;
 734
 735	/* VBI */
 736	itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
 737	itv->vbi.sliced_in = &itv->vbi.in.fmt.sliced;
 738
 739	/* Init the sg table for osd/yuv output */
 740	sg_init_table(itv->udma.SGlist, IVTV_DMA_SG_OSD_ENT);
 741
 742	/* OSD */
 743	itv->osd_global_alpha_state = 1;
 744	itv->osd_global_alpha = 255;
 745
 746	/* YUV */
 747	atomic_set(&itv->yuv_info.next_dma_frame, -1);
 748	itv->yuv_info.lace_mode = ivtv_yuv_mode;
 749	itv->yuv_info.lace_threshold = ivtv_yuv_threshold;
 750	itv->yuv_info.max_frames_buffered = 3;
 751	itv->yuv_info.track_osd = 1;
 752	return 0;
 753}
 754
 755/* Second initialization part. Here the card type has been
 756   autodetected. */
 757static void ivtv_init_struct2(struct ivtv *itv)
 758{
 759	int i;
 760
 761	for (i = 0; i < IVTV_CARD_MAX_VIDEO_INPUTS; i++)
 762		if (itv->card->video_inputs[i].video_type == 0)
 763			break;
 764	itv->nof_inputs = i;
 765	for (i = 0; i < IVTV_CARD_MAX_AUDIO_INPUTS; i++)
 766		if (itv->card->audio_inputs[i].audio_type == 0)
 767			break;
 768	itv->nof_audio_inputs = i;
 769
 770	if (itv->card->hw_all & IVTV_HW_CX25840) {
 771		itv->vbi.sliced_size = 288;  /* multiple of 16, real size = 284 */
 772	} else {
 773		itv->vbi.sliced_size = 64;   /* multiple of 16, real size = 52 */
 774	}
 775
 776	/* Find tuner input */
 777	for (i = 0; i < itv->nof_inputs; i++) {
 778		if (itv->card->video_inputs[i].video_type ==
 779				IVTV_CARD_INPUT_VID_TUNER)
 780			break;
 781	}
 782	if (i >= itv->nof_inputs)
 783		i = 0;
 784	itv->active_input = i;
 785	itv->audio_input = itv->card->video_inputs[i].audio_index;
 786}
 787
 788static int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *pdev,
 789			  const struct pci_device_id *pci_id)
 790{
 791	u16 cmd;
 792	unsigned char pci_latency;
 793
 794	IVTV_DEBUG_INFO("Enabling pci device\n");
 795
 796	if (pcim_enable_device(pdev)) {
 797		IVTV_ERR("Can't enable device!\n");
 798		return -EIO;
 799	}
 800	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
 801		IVTV_ERR("No suitable DMA available.\n");
 802		return -EIO;
 803	}
 804	if (!devm_request_mem_region(&pdev->dev, itv->base_addr,
 805				     IVTV_ENCODER_SIZE, "ivtv encoder")) {
 806		IVTV_ERR("Cannot request encoder memory region.\n");
 807		return -EIO;
 808	}
 809
 810	if (!devm_request_mem_region(&pdev->dev,
 811				     itv->base_addr + IVTV_REG_OFFSET,
 812				     IVTV_REG_SIZE, "ivtv registers")) {
 813		IVTV_ERR("Cannot request register memory region.\n");
 814		return -EIO;
 815	}
 816
 817	if (itv->has_cx23415 &&
 818	    !devm_request_mem_region(&pdev->dev,
 819				     itv->base_addr + IVTV_DECODER_OFFSET,
 820				     IVTV_DECODER_SIZE, "ivtv decoder")) {
 821		IVTV_ERR("Cannot request decoder memory region.\n");
 822		return -EIO;
 823	}
 824
 825	/* Check for bus mastering */
 826	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
 827	if (!(cmd & PCI_COMMAND_MASTER)) {
 828		IVTV_DEBUG_INFO("Attempting to enable Bus Mastering\n");
 829		pci_set_master(pdev);
 830		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
 831		if (!(cmd & PCI_COMMAND_MASTER)) {
 832			IVTV_ERR("Bus Mastering is not enabled\n");
 833			return -ENXIO;
 834		}
 835	}
 836	IVTV_DEBUG_INFO("Bus Mastering Enabled.\n");
 837
 838	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
 839
 840	if (pci_latency < 64 && ivtv_pci_latency) {
 841		IVTV_INFO("Unreasonably low latency timer, setting to 64 (was %d)\n",
 842			  pci_latency);
 843		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
 844		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
 845	}
 846	/* This config space value relates to DMA latencies. The
 847	   default value 0x8080 is too low however and will lead
 848	   to DMA errors. 0xffff is the max value which solves
 849	   these problems. */
 850	pci_write_config_dword(pdev, 0x40, 0xffff);
 851
 852	IVTV_DEBUG_INFO("%d (rev %d) at %02x:%02x.%x, irq: %d, latency: %d, memory: 0x%llx\n",
 853		   pdev->device, pdev->revision, pdev->bus->number,
 854		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
 855		   pdev->irq, pci_latency, (u64)itv->base_addr);
 856
 857	return 0;
 858}
 859
 860static void ivtv_load_and_init_modules(struct ivtv *itv)
 861{
 862	u32 hw = itv->card->hw_all;
 863	unsigned i;
 864
 865	/* check which i2c devices are actually found */
 866	for (i = 0; i < 32; i++) {
 867		u32 device = BIT(i);
 868
 869		if (!(device & hw))
 870			continue;
 871		if (device == IVTV_HW_GPIO || device == IVTV_HW_TVEEPROM) {
 872			/* GPIO and TVEEPROM do not use i2c probing */
 873			itv->hw_flags |= device;
 874			continue;
 875		}
 876		if (ivtv_i2c_register(itv, i) == 0)
 877			itv->hw_flags |= device;
 878	}
 879
 880	/* probe for legacy IR controllers that aren't in card definitions */
 881	if ((itv->hw_flags & IVTV_HW_IR_ANY) == 0)
 882		ivtv_i2c_new_ir_legacy(itv);
 883
 884	if (itv->card->hw_all & IVTV_HW_CX25840)
 885		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_CX25840);
 886	else if (itv->card->hw_all & IVTV_HW_SAA717X)
 887		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA717X);
 888	else if (itv->card->hw_all & IVTV_HW_SAA7114)
 889		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7114);
 890	else
 891		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7115);
 892	itv->sd_audio = ivtv_find_hw(itv, itv->card->hw_audio_ctrl);
 893	itv->sd_muxer = ivtv_find_hw(itv, itv->card->hw_muxer);
 894
 895	hw = itv->hw_flags;
 896
 897	if (itv->card->type == IVTV_CARD_CX23416GYC) {
 898		/* Several variations of this card exist, detect which card
 899		   type should be used. */
 900		if ((hw & (IVTV_HW_UPD64031A | IVTV_HW_UPD6408X)) == 0)
 901			itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGRYCS);
 902		else if ((hw & IVTV_HW_UPD64031A) == 0)
 903			itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGR);
 904	}
 905	else if (itv->card->type == IVTV_CARD_GV_MVPRX ||
 906		 itv->card->type == IVTV_CARD_GV_MVPRX2E) {
 907		/* The crystal frequency of GVMVPRX is 24.576MHz */
 908		v4l2_subdev_call(itv->sd_video, video, s_crystal_freq,
 909			SAA7115_FREQ_24_576_MHZ, SAA7115_FREQ_FL_UCGC);
 910	}
 911
 912	if (hw & IVTV_HW_CX25840) {
 913		itv->vbi.raw_decoder_line_size = 1444;
 914		itv->vbi.raw_decoder_sav_odd_field = 0x20;
 915		itv->vbi.raw_decoder_sav_even_field = 0x60;
 916		itv->vbi.sliced_decoder_line_size = 272;
 917		itv->vbi.sliced_decoder_sav_odd_field = 0xB0;
 918		itv->vbi.sliced_decoder_sav_even_field = 0xF0;
 919	}
 920
 921	if (hw & IVTV_HW_SAA711X) {
 922		/* determine the exact saa711x model */
 923		itv->hw_flags &= ~IVTV_HW_SAA711X;
 924
 925		if (strstr(itv->sd_video->name, "saa7114")) {
 926			itv->hw_flags |= IVTV_HW_SAA7114;
 927			/* VBI is not yet supported by the saa7114 driver. */
 928			itv->v4l2_cap &= ~(V4L2_CAP_SLICED_VBI_CAPTURE|V4L2_CAP_VBI_CAPTURE);
 929		} else {
 930			itv->hw_flags |= IVTV_HW_SAA7115;
 931		}
 932		itv->vbi.raw_decoder_line_size = 1443;
 933		itv->vbi.raw_decoder_sav_odd_field = 0x25;
 934		itv->vbi.raw_decoder_sav_even_field = 0x62;
 935		itv->vbi.sliced_decoder_line_size = 51;
 936		itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
 937		itv->vbi.sliced_decoder_sav_even_field = 0xEC;
 938	}
 939
 940	if (hw & IVTV_HW_SAA717X) {
 941		itv->vbi.raw_decoder_line_size = 1443;
 942		itv->vbi.raw_decoder_sav_odd_field = 0x25;
 943		itv->vbi.raw_decoder_sav_even_field = 0x62;
 944		itv->vbi.sliced_decoder_line_size = 51;
 945		itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
 946		itv->vbi.sliced_decoder_sav_even_field = 0xEC;
 947	}
 948}
 949
 950static int ivtv_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
 951{
 952	int retval = 0;
 953	int vbi_buf_size;
 954	struct ivtv *itv;
 955
 956	itv = kzalloc(sizeof(struct ivtv), GFP_KERNEL);
 957	if (itv == NULL)
 958		return -ENOMEM;
 959	itv->pdev = pdev;
 960	itv->instance = v4l2_device_set_name(&itv->v4l2_dev, "ivtv",
 961						&ivtv_instance);
 962
 963	retval = v4l2_device_register(&pdev->dev, &itv->v4l2_dev);
 964	if (retval) {
 965		kfree(itv);
 966		return retval;
 967	}
 968	IVTV_INFO("Initializing card %d\n", itv->instance);
 969
 970	ivtv_process_options(itv);
 971	if (itv->options.cardtype == -1) {
 972		retval = -ENODEV;
 973		goto err;
 974	}
 975	if (ivtv_init_struct1(itv)) {
 976		retval = -ENOMEM;
 977		goto err;
 978	}
 979	retval = cx2341x_handler_init(&itv->cxhdl, 50);
 980	if (retval)
 981		goto err;
 982	itv->v4l2_dev.ctrl_handler = &itv->cxhdl.hdl;
 983	itv->cxhdl.ops = &ivtv_cxhdl_ops;
 984	itv->cxhdl.priv = itv;
 985	itv->cxhdl.func = ivtv_api_func;
 986
 987	IVTV_DEBUG_INFO("base addr: 0x%llx\n", (u64)itv->base_addr);
 988
 989	/* PCI Device Setup */
 990	retval = ivtv_setup_pci(itv, pdev, pci_id);
 991	if (retval == -EIO || retval == -ENXIO)
 992		goto free_worker;
 993
 994	/* map io memory */
 995	IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
 996		   (u64)itv->base_addr + IVTV_ENCODER_OFFSET, IVTV_ENCODER_SIZE);
 997	itv->enc_mem = devm_ioremap(&pdev->dev,
 998				    itv->base_addr + IVTV_ENCODER_OFFSET,
 999				    IVTV_ENCODER_SIZE);
1000	if (!itv->enc_mem) {
1001		IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 encoder memory\n");
1002		IVTV_ERR("Each capture card with a CX23415/6 needs 8 MB of vmalloc address space for this window\n");
1003		IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
1004		IVTV_ERR("Use the vmalloc= kernel command line option to set VmallocTotal to a larger value\n");
1005		retval = -ENOMEM;
1006		goto free_worker;
1007	}
1008
1009	if (itv->has_cx23415) {
1010		IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
1011				(u64)itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1012		itv->dec_mem = devm_ioremap(&pdev->dev,
1013					    itv->base_addr + IVTV_DECODER_OFFSET,
1014					    IVTV_DECODER_SIZE);
1015		if (!itv->dec_mem) {
1016			IVTV_ERR("ioremap failed. Can't get a window into CX23415 decoder memory\n");
1017			IVTV_ERR("Each capture card with a CX23415 needs 8 MB of vmalloc address space for this window\n");
1018			IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
1019			IVTV_ERR("Use the vmalloc= kernel command line option to set VmallocTotal to a larger value\n");
1020			retval = -ENOMEM;
1021			goto free_worker;
1022		}
1023	}
1024	else {
1025		itv->dec_mem = itv->enc_mem;
1026	}
1027
1028	/* map registers memory */
1029	IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
1030		   (u64)itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1031	itv->reg_mem = devm_ioremap(&pdev->dev,
1032				    itv->base_addr + IVTV_REG_OFFSET,
1033				    IVTV_REG_SIZE);
1034	if (!itv->reg_mem) {
1035		IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 register space\n");
1036		IVTV_ERR("Each capture card with a CX23415/6 needs 64 kB of vmalloc address space for this window\n");
1037		IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
1038		IVTV_ERR("Use the vmalloc= kernel command line option to set VmallocTotal to a larger value\n");
1039		retval = -ENOMEM;
1040		goto free_worker;
1041	}
1042
1043	retval = ivtv_gpio_init(itv);
1044	if (retval)
1045		goto free_worker;
1046
1047	/* active i2c  */
1048	IVTV_DEBUG_INFO("activating i2c...\n");
1049	if (init_ivtv_i2c(itv)) {
1050		IVTV_ERR("Could not initialize i2c\n");
1051		goto free_worker;
1052	}
1053
1054	if (itv->card->hw_all & IVTV_HW_TVEEPROM) {
1055		/* Based on the model number the cardtype may be changed.
1056		   The PCI IDs are not always reliable. */
1057		ivtv_process_eeprom(itv);
1058	}
1059	if (itv->card->comment)
1060		IVTV_INFO("%s", itv->card->comment);
1061	if (itv->card->v4l2_capabilities == 0) {
1062		/* card was detected but is not supported */
1063		retval = -ENODEV;
1064		goto free_i2c;
1065	}
1066
1067	if (itv->std == 0) {
1068		itv->std = V4L2_STD_NTSC_M;
1069	}
1070
1071	if (itv->options.tuner == -1) {
1072		int i;
1073
1074		for (i = 0; i < IVTV_CARD_MAX_TUNERS; i++) {
1075			if ((itv->std & itv->card->tuners[i].std) == 0)
1076				continue;
1077			itv->options.tuner = itv->card->tuners[i].tuner;
1078			break;
1079		}
1080	}
1081	/* if no tuner was found, then pick the first tuner in the card list */
1082	if (itv->options.tuner == -1 && itv->card->tuners[0].std) {
1083		itv->std = itv->card->tuners[0].std;
1084		if (itv->std & V4L2_STD_PAL)
1085			itv->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
1086		else if (itv->std & V4L2_STD_NTSC)
1087			itv->std = V4L2_STD_NTSC_M;
1088		else if (itv->std & V4L2_STD_SECAM)
1089			itv->std = V4L2_STD_SECAM_L;
1090		itv->options.tuner = itv->card->tuners[0].tuner;
1091	}
1092	if (itv->options.radio == -1)
1093		itv->options.radio = (itv->card->radio_input.audio_type != 0);
1094
1095	/* The card is now fully identified, continue with card-specific
1096	   initialization. */
1097	ivtv_init_struct2(itv);
1098
1099	ivtv_load_and_init_modules(itv);
1100
1101	if (itv->std & V4L2_STD_525_60) {
1102		itv->is_60hz = 1;
1103		itv->is_out_60hz = 1;
1104	} else {
1105		itv->is_50hz = 1;
1106		itv->is_out_50hz = 1;
1107	}
1108
1109	itv->yuv_info.osd_full_w = 720;
1110	itv->yuv_info.osd_full_h = itv->is_out_50hz ? 576 : 480;
1111	itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;
1112	itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;
1113
1114	cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1115
1116	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
1117	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
1118	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;
1119	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = 0x10000;
1120	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = 0x08000;
1121
1122	/* Setup VBI Raw Size. Should be big enough to hold PAL.
1123	   It is possible to switch between PAL and NTSC, so we need to
1124	   take the largest size here. */
1125	/* 1456 is multiple of 16, real size = 1444 */
1126	itv->vbi.raw_size = 1456;
1127	/* We use a buffer size of 1/2 of the total size needed for a
1128	   frame. This is actually very useful, since we now receive
1129	   a field at a time and that makes 'compressing' the raw data
1130	   down to size by stripping off the SAV codes a lot easier.
1131	   Note: having two different buffer sizes prevents standard
1132	   switching on the fly. We need to find a better solution... */
1133	vbi_buf_size = itv->vbi.raw_size * (itv->is_60hz ? 24 : 36) / 2;
1134	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
1135	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_VBI] = sizeof(struct v4l2_sliced_vbi_data) * 36;
1136
1137	if (itv->options.radio > 0)
1138		itv->v4l2_cap |= V4L2_CAP_RADIO;
1139
1140	if (itv->options.tuner > -1) {
1141		struct tuner_setup setup;
1142
1143		setup.addr = ADDR_UNSET;
1144		setup.type = itv->options.tuner;
1145		setup.mode_mask = T_ANALOG_TV;  /* matches TV tuners */
1146		if (itv->options.radio > 0)
1147			setup.mode_mask |= T_RADIO;
1148		setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1149			ivtv_reset_tuner_gpio : NULL;
1150		ivtv_call_all(itv, tuner, s_type_addr, &setup);
1151		if (setup.type == TUNER_XC2028) {
1152			static struct xc2028_ctrl ctrl = {
1153				.fname = XC2028_DEFAULT_FIRMWARE,
1154				.max_len = 64,
1155			};
1156			struct v4l2_priv_tun_config cfg = {
1157				.tuner = itv->options.tuner,
1158				.priv = &ctrl,
1159			};
1160			ivtv_call_all(itv, tuner, s_config, &cfg);
1161		}
1162	}
1163
1164	/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1165	   are not. */
1166	itv->tuner_std = itv->std;
1167
1168	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1169		struct v4l2_ctrl_handler *hdl = itv->v4l2_dev.ctrl_handler;
1170
1171		itv->ctrl_pts = v4l2_ctrl_new_std(hdl, &ivtv_hdl_out_ops,
1172				V4L2_CID_MPEG_VIDEO_DEC_PTS, 0, 0, 0, 0);
1173		itv->ctrl_frame = v4l2_ctrl_new_std(hdl, &ivtv_hdl_out_ops,
1174				V4L2_CID_MPEG_VIDEO_DEC_FRAME, 0, 0, 0, 0);
1175		/* Note: V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO is not supported,
1176		   mask that menu item. */
1177		itv->ctrl_audio_playback =
1178			v4l2_ctrl_new_std_menu(hdl, &ivtv_hdl_out_ops,
1179				V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK,
1180				V4L2_MPEG_AUDIO_DEC_PLAYBACK_SWAPPED_STEREO,
1181				1 << V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO,
1182				V4L2_MPEG_AUDIO_DEC_PLAYBACK_STEREO);
1183		itv->ctrl_audio_multilingual_playback =
1184			v4l2_ctrl_new_std_menu(hdl, &ivtv_hdl_out_ops,
1185				V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK,
1186				V4L2_MPEG_AUDIO_DEC_PLAYBACK_SWAPPED_STEREO,
1187				1 << V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO,
1188				V4L2_MPEG_AUDIO_DEC_PLAYBACK_LEFT);
1189		if (hdl->error) {
1190			retval = hdl->error;
1191			goto free_i2c;
1192		}
1193		v4l2_ctrl_cluster(2, &itv->ctrl_pts);
1194		v4l2_ctrl_cluster(2, &itv->ctrl_audio_playback);
1195		ivtv_call_all(itv, video, s_std_output, itv->std);
1196		/* Turn off the output signal. The mpeg decoder is not yet
1197		   active so without this you would get a green image until the
1198		   mpeg decoder becomes active. */
1199		ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
1200	}
1201
1202	/* clear interrupt mask, effectively disabling interrupts */
1203	ivtv_set_irq_mask(itv, 0xffffffff);
1204
1205	/* Register IRQ */
1206	retval = request_irq(itv->pdev->irq, ivtv_irq_handler,
1207	     IRQF_SHARED, itv->v4l2_dev.name, (void *)itv);
1208	if (retval) {
1209		IVTV_ERR("Failed to register irq %d\n", retval);
1210		goto free_i2c;
1211	}
1212
1213	retval = ivtv_streams_setup(itv);
1214	if (retval) {
1215		IVTV_ERR("Error %d setting up streams\n", retval);
1216		goto free_irq;
1217	}
1218	retval = ivtv_streams_register(itv);
1219	if (retval) {
1220		IVTV_ERR("Error %d registering devices\n", retval);
1221		goto free_streams;
1222	}
1223	IVTV_INFO("Initialized card: %s\n", itv->card_name);
1224
1225	/* Load ivtv submodules (ivtv-alsa) */
1226	request_modules(itv);
1227	return 0;
1228
1229free_streams:
1230	ivtv_streams_cleanup(itv);
1231free_irq:
1232	free_irq(itv->pdev->irq, (void *)itv);
1233free_i2c:
1234	v4l2_ctrl_handler_free(&itv->cxhdl.hdl);
1235	exit_ivtv_i2c(itv);
1236free_worker:
1237	kthread_stop(itv->irq_worker_task);
1238err:
1239	if (retval == 0)
1240		retval = -ENODEV;
1241	IVTV_ERR("Error %d on initialization\n", retval);
1242
1243	v4l2_device_unregister(&itv->v4l2_dev);
1244	kfree(itv);
1245	return retval;
1246}
1247
1248int ivtv_init_on_first_open(struct ivtv *itv)
1249{
1250	/* Needed to call ioctls later */
1251	struct ivtv_stream *s = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
1252	struct v4l2_frequency vf;
1253	int fw_retry_count = 3;
1254	int video_input;
1255
1256	if (test_bit(IVTV_F_I_FAILED, &itv->i_flags))
1257		return -ENXIO;
1258
1259	if (test_and_set_bit(IVTV_F_I_INITED, &itv->i_flags))
1260		return 0;
1261
1262	while (--fw_retry_count > 0) {
1263		/* load firmware */
1264		if (ivtv_firmware_init(itv) == 0)
1265			break;
1266		if (fw_retry_count > 1)
1267			IVTV_WARN("Retry loading firmware\n");
1268	}
1269
1270	if (fw_retry_count == 0) {
1271		set_bit(IVTV_F_I_FAILED, &itv->i_flags);
1272		return -ENXIO;
1273	}
1274
1275	/* Try and get firmware versions */
1276	IVTV_DEBUG_INFO("Getting firmware version..\n");
1277	ivtv_firmware_versions(itv);
1278
1279	if (itv->card->hw_all & IVTV_HW_CX25840)
1280		v4l2_subdev_call(itv->sd_video, core, load_fw);
1281
1282	vf.tuner = 0;
1283	vf.type = V4L2_TUNER_ANALOG_TV;
1284	vf.frequency = 6400; /* the tuner 'baseline' frequency */
1285
1286	/* Set initial frequency. For PAL/SECAM broadcasts no
1287	   'default' channel exists AFAIK. */
1288	if (itv->std == V4L2_STD_NTSC_M_JP) {
1289		vf.frequency = 1460;	/* ch. 1 91250*16/1000 */
1290	}
1291	else if (itv->std & V4L2_STD_NTSC_M) {
1292		vf.frequency = 1076;	/* ch. 4 67250*16/1000 */
1293	}
1294
1295	video_input = itv->active_input;
1296	itv->active_input++;	/* Force update of input */
1297	ivtv_do_s_input(itv, video_input);
1298
1299	/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1300	   in one place. */
1301	itv->std++;		/* Force full standard initialization */
1302	itv->std_out = itv->std;
1303	ivtv_do_s_frequency(s, &vf);
1304
1305	if (itv->card->v4l2_capabilities & V4L2_CAP_VIDEO_OUTPUT) {
1306		/* Turn on the TV-out: ivtv_init_mpeg_decoder() initializes
1307		   the mpeg decoder so now the saa7127 receives a proper
1308		   signal. */
1309		ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
1310		ivtv_init_mpeg_decoder(itv);
1311	}
1312
1313	/* On a cx23416 this seems to be able to enable DMA to the chip? */
1314	if (!itv->has_cx23415)
1315		write_reg_sync(0x03, IVTV_REG_DMACONTROL);
1316
1317	ivtv_s_std_enc(itv, itv->tuner_std);
1318
1319	/* Default interrupts enabled. For the PVR350 this includes the
1320	   decoder VSYNC interrupt, which is always on. It is not only used
1321	   during decoding but also by the OSD.
1322	   Some old PVR250 cards had a cx23415, so testing for that is too
1323	   general. Instead test if the card has video output capability. */
1324	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1325		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT | IVTV_IRQ_DEC_VSYNC);
1326		ivtv_set_osd_alpha(itv);
1327		ivtv_s_std_dec(itv, itv->tuner_std);
1328	} else {
1329		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT);
1330	}
1331
1332	/* Setup initial controls */
1333	cx2341x_handler_setup(&itv->cxhdl);
1334	return 0;
1335}
1336
1337static void ivtv_remove(struct pci_dev *pdev)
1338{
1339	struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
1340	struct ivtv *itv = to_ivtv(v4l2_dev);
1341	int i;
1342
1343	IVTV_DEBUG_INFO("Removing card\n");
1344
1345	flush_request_modules(itv);
1346
1347	if (test_bit(IVTV_F_I_INITED, &itv->i_flags)) {
1348		/* Stop all captures */
1349		IVTV_DEBUG_INFO("Stopping all streams\n");
1350		if (atomic_read(&itv->capturing) > 0)
1351			ivtv_stop_all_captures(itv);
1352
1353		/* Stop all decoding */
1354		IVTV_DEBUG_INFO("Stopping decoding\n");
1355
1356		/* Turn off the TV-out */
1357		if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1358			ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
1359		if (atomic_read(&itv->decoding) > 0) {
1360			int type;
1361
1362			if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
1363				type = IVTV_DEC_STREAM_TYPE_YUV;
1364			else
1365				type = IVTV_DEC_STREAM_TYPE_MPG;
1366			ivtv_stop_v4l2_decode_stream(&itv->streams[type],
1367				V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
1368		}
1369		ivtv_halt_firmware(itv);
1370	}
1371
1372	/* Interrupts */
1373	ivtv_set_irq_mask(itv, 0xffffffff);
1374	timer_shutdown_sync(&itv->dma_timer);
1375
1376	/* Kill irq worker */
1377	kthread_flush_worker(&itv->irq_worker);
1378	kthread_stop(itv->irq_worker_task);
1379
1380	ivtv_streams_cleanup(itv);
1381	ivtv_udma_free(itv);
1382
1383	v4l2_ctrl_handler_free(&itv->cxhdl.hdl);
1384
1385	exit_ivtv_i2c(itv);
1386
1387	free_irq(itv->pdev->irq, (void *)itv);
1388
1389	for (i = 0; i < IVTV_VBI_FRAMES; i++)
1390		kfree(itv->vbi.sliced_mpeg_data[i]);
1391
1392	pr_info("Removed %s\n", itv->card_name);
1393
1394	v4l2_device_unregister(&itv->v4l2_dev);
1395	kfree(itv);
1396}
1397
1398/* define a pci_driver for card detection */
1399static struct pci_driver ivtv_pci_driver = {
1400      .name =     "ivtv",
1401      .id_table = ivtv_pci_tbl,
1402      .probe =    ivtv_probe,
1403      .remove =   ivtv_remove,
1404};
1405
1406static int __init module_start(void)
1407{
1408	pr_info("Start initialization, version %s\n", IVTV_VERSION);
1409
1410	/* Validate parameters */
1411	if (ivtv_first_minor < 0 || ivtv_first_minor >= IVTV_MAX_CARDS) {
1412		pr_err("Exiting, ivtv_first_minor must be between 0 and %d\n",
1413		     IVTV_MAX_CARDS - 1);
1414		return -1;
1415	}
1416
1417	if (ivtv_debug < 0 || ivtv_debug > 2047) {
1418		ivtv_debug = 0;
1419		pr_info("Debug value must be >= 0 and <= 2047\n");
1420	}
1421
1422	if (pci_register_driver(&ivtv_pci_driver)) {
1423		pr_err("Error detecting PCI card\n");
1424		return -ENODEV;
1425	}
1426	pr_info("End initialization\n");
1427	return 0;
1428}
1429
1430static void __exit module_cleanup(void)
1431{
1432	pci_unregister_driver(&ivtv_pci_driver);
1433}
1434
1435/* Note: These symbols are exported because they are used by the ivtvfb
1436   framebuffer module and an infrared module for the IR-blaster. */
1437EXPORT_SYMBOL(ivtv_set_irq_mask);
1438EXPORT_SYMBOL(ivtv_api);
1439EXPORT_SYMBOL(ivtv_vapi);
1440EXPORT_SYMBOL(ivtv_vapi_result);
1441EXPORT_SYMBOL(ivtv_clear_irq_mask);
1442EXPORT_SYMBOL(ivtv_debug);
1443#ifdef CONFIG_VIDEO_ADV_DEBUG
1444EXPORT_SYMBOL(ivtv_fw_debug);
1445#endif
1446EXPORT_SYMBOL(ivtv_reset_ir_gpio);
1447EXPORT_SYMBOL(ivtv_udma_setup);
1448EXPORT_SYMBOL(ivtv_udma_unmap);
1449EXPORT_SYMBOL(ivtv_udma_alloc);
1450EXPORT_SYMBOL(ivtv_udma_prepare);
1451EXPORT_SYMBOL(ivtv_init_on_first_open);
1452EXPORT_SYMBOL(ivtv_firmware_check);
1453
1454module_init(module_start);
1455module_exit(module_cleanup);
~/ Read-only file preview.