162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci    ivtv driver initialization and card probing
362306a36Sopenharmony_ci    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
462306a36Sopenharmony_ci    Copyright (C) 2004  Chris Kennedy <c@groovy.org>
562306a36Sopenharmony_ci    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci    This program is free software; you can redistribute it and/or modify
862306a36Sopenharmony_ci    it under the terms of the GNU General Public License as published by
962306a36Sopenharmony_ci    the Free Software Foundation; either version 2 of the License, or
1062306a36Sopenharmony_ci    (at your option) any later version.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci    This program is distributed in the hope that it will be useful,
1362306a36Sopenharmony_ci    but WITHOUT ANY WARRANTY; without even the implied warranty of
1462306a36Sopenharmony_ci    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1562306a36Sopenharmony_ci    GNU General Public License for more details.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci    You should have received a copy of the GNU General Public License
1862306a36Sopenharmony_ci    along with this program; if not, write to the Free Software
1962306a36Sopenharmony_ci    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/* Main Driver file for the ivtv project:
2362306a36Sopenharmony_ci * Driver for the Conexant CX23415/CX23416 chip.
2462306a36Sopenharmony_ci * Author: Kevin Thayer (nufan_wfk at yahoo.com)
2562306a36Sopenharmony_ci * License: GPL
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * -----
2862306a36Sopenharmony_ci * MPG600/MPG160 support by  T.Adachi <tadachi@tadachi-net.com>
2962306a36Sopenharmony_ci *                      and Takeru KOMORIYA<komoriya@paken.org>
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
3262306a36Sopenharmony_ci *                using information provided by Jiun-Kuei Jung @ AVerMedia.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * Kurouto Sikou CX23416GYC-STVLP tested by K.Ohta <alpha292@bremen.or.jp>
3562306a36Sopenharmony_ci *                using information from T.Adachi,Takeru KOMORIYA and others :-)
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * Nagase TRANSGEAR 5000TV, Aopen VA2000MAX-STN6 and I/O data GV-MVP/RX
3862306a36Sopenharmony_ci *                version by T.Adachi. Special thanks  Mr.Suzuki
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#include "ivtv-driver.h"
4262306a36Sopenharmony_ci#include "ivtv-version.h"
4362306a36Sopenharmony_ci#include "ivtv-fileops.h"
4462306a36Sopenharmony_ci#include "ivtv-i2c.h"
4562306a36Sopenharmony_ci#include "ivtv-firmware.h"
4662306a36Sopenharmony_ci#include "ivtv-queue.h"
4762306a36Sopenharmony_ci#include "ivtv-udma.h"
4862306a36Sopenharmony_ci#include "ivtv-irq.h"
4962306a36Sopenharmony_ci#include "ivtv-mailbox.h"
5062306a36Sopenharmony_ci#include "ivtv-streams.h"
5162306a36Sopenharmony_ci#include "ivtv-ioctl.h"
5262306a36Sopenharmony_ci#include "ivtv-cards.h"
5362306a36Sopenharmony_ci#include "ivtv-vbi.h"
5462306a36Sopenharmony_ci#include "ivtv-routing.h"
5562306a36Sopenharmony_ci#include "ivtv-controls.h"
5662306a36Sopenharmony_ci#include "ivtv-gpio.h"
5762306a36Sopenharmony_ci#include <linux/dma-mapping.h>
5862306a36Sopenharmony_ci#include <media/tveeprom.h>
5962306a36Sopenharmony_ci#include <media/i2c/saa7115.h>
6062306a36Sopenharmony_ci#include "xc2028.h"
6162306a36Sopenharmony_ci#include <uapi/linux/sched/types.h>
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* If you have already X v4l cards, then set this to X. This way
6462306a36Sopenharmony_ci   the device numbers stay matched. Example: you have a WinTV card
6562306a36Sopenharmony_ci   without radio and a PVR-350 with. Normally this would give a
6662306a36Sopenharmony_ci   video1 device together with a radio0 device for the PVR. By
6762306a36Sopenharmony_ci   setting this to 1 you ensure that radio0 is now also radio1. */
6862306a36Sopenharmony_ciint ivtv_first_minor;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/* Callback for registering extensions */
7162306a36Sopenharmony_ciint (*ivtv_ext_init)(struct ivtv *);
7262306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_ext_init);
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/* add your revision and whatnot here */
7562306a36Sopenharmony_cistatic const struct pci_device_id ivtv_pci_tbl[] = {
7662306a36Sopenharmony_ci	{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV15,
7762306a36Sopenharmony_ci	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
7862306a36Sopenharmony_ci	{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV16,
7962306a36Sopenharmony_ci	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
8062306a36Sopenharmony_ci	{0,}
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(pci,ivtv_pci_tbl);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/* ivtv instance counter */
8662306a36Sopenharmony_cistatic atomic_t ivtv_instance = ATOMIC_INIT(0);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* Parameter declarations */
8962306a36Sopenharmony_cistatic int cardtype[IVTV_MAX_CARDS];
9062306a36Sopenharmony_cistatic int tuner[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
9162306a36Sopenharmony_ci				     -1, -1, -1, -1, -1, -1, -1, -1,
9262306a36Sopenharmony_ci				     -1, -1, -1, -1, -1, -1, -1, -1,
9362306a36Sopenharmony_ci				     -1, -1, -1, -1, -1, -1, -1, -1 };
9462306a36Sopenharmony_cistatic int radio[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
9562306a36Sopenharmony_ci				     -1, -1, -1, -1, -1, -1, -1, -1,
9662306a36Sopenharmony_ci				     -1, -1, -1, -1, -1, -1, -1, -1,
9762306a36Sopenharmony_ci				     -1, -1, -1, -1, -1, -1, -1, -1 };
9862306a36Sopenharmony_cistatic int i2c_clock_period[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
9962306a36Sopenharmony_ci					       -1, -1, -1, -1, -1, -1, -1, -1,
10062306a36Sopenharmony_ci					       -1, -1, -1, -1, -1, -1, -1, -1,
10162306a36Sopenharmony_ci					       -1, -1, -1, -1, -1, -1, -1, -1 };
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistatic unsigned int cardtype_c = 1;
10462306a36Sopenharmony_cistatic unsigned int tuner_c = 1;
10562306a36Sopenharmony_cistatic int radio_c = 1;
10662306a36Sopenharmony_cistatic unsigned int i2c_clock_period_c = 1;
10762306a36Sopenharmony_cistatic char pal[] = "---";
10862306a36Sopenharmony_cistatic char secam[] = "--";
10962306a36Sopenharmony_cistatic char ntsc[] = "-";
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/* Buffers */
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci/* DMA Buffers, Default size in MB allocated */
11462306a36Sopenharmony_ci#define IVTV_DEFAULT_ENC_MPG_BUFFERS 4
11562306a36Sopenharmony_ci#define IVTV_DEFAULT_ENC_YUV_BUFFERS 2
11662306a36Sopenharmony_ci#define IVTV_DEFAULT_ENC_VBI_BUFFERS 1
11762306a36Sopenharmony_ci/* Exception: size in kB for this stream (MB is overkill) */
11862306a36Sopenharmony_ci#define IVTV_DEFAULT_ENC_PCM_BUFFERS 320
11962306a36Sopenharmony_ci#define IVTV_DEFAULT_DEC_MPG_BUFFERS 1
12062306a36Sopenharmony_ci#define IVTV_DEFAULT_DEC_YUV_BUFFERS 1
12162306a36Sopenharmony_ci/* Exception: size in kB for this stream (MB is way overkill) */
12262306a36Sopenharmony_ci#define IVTV_DEFAULT_DEC_VBI_BUFFERS 64
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic int enc_mpg_buffers = IVTV_DEFAULT_ENC_MPG_BUFFERS;
12562306a36Sopenharmony_cistatic int enc_yuv_buffers = IVTV_DEFAULT_ENC_YUV_BUFFERS;
12662306a36Sopenharmony_cistatic int enc_vbi_buffers = IVTV_DEFAULT_ENC_VBI_BUFFERS;
12762306a36Sopenharmony_cistatic int enc_pcm_buffers = IVTV_DEFAULT_ENC_PCM_BUFFERS;
12862306a36Sopenharmony_cistatic int dec_mpg_buffers = IVTV_DEFAULT_DEC_MPG_BUFFERS;
12962306a36Sopenharmony_cistatic int dec_yuv_buffers = IVTV_DEFAULT_DEC_YUV_BUFFERS;
13062306a36Sopenharmony_cistatic int dec_vbi_buffers = IVTV_DEFAULT_DEC_VBI_BUFFERS;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic int ivtv_yuv_mode;
13362306a36Sopenharmony_cistatic int ivtv_yuv_threshold = -1;
13462306a36Sopenharmony_cistatic int ivtv_pci_latency = 1;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciint ivtv_debug;
13762306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
13862306a36Sopenharmony_ciint ivtv_fw_debug;
13962306a36Sopenharmony_ci#endif
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_cistatic int tunertype = -1;
14262306a36Sopenharmony_cistatic int newi2c = -1;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cimodule_param_array(tuner, int, &tuner_c, 0644);
14562306a36Sopenharmony_cimodule_param_array(radio, int, &radio_c, 0644);
14662306a36Sopenharmony_cimodule_param_array(cardtype, int, &cardtype_c, 0644);
14762306a36Sopenharmony_cimodule_param_string(pal, pal, sizeof(pal), 0644);
14862306a36Sopenharmony_cimodule_param_string(secam, secam, sizeof(secam), 0644);
14962306a36Sopenharmony_cimodule_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
15062306a36Sopenharmony_cimodule_param_named(debug,ivtv_debug, int, 0644);
15162306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
15262306a36Sopenharmony_cimodule_param_named(fw_debug, ivtv_fw_debug, int, 0644);
15362306a36Sopenharmony_ci#endif
15462306a36Sopenharmony_cimodule_param(ivtv_pci_latency, int, 0644);
15562306a36Sopenharmony_cimodule_param(ivtv_yuv_mode, int, 0644);
15662306a36Sopenharmony_cimodule_param(ivtv_yuv_threshold, int, 0644);
15762306a36Sopenharmony_cimodule_param(ivtv_first_minor, int, 0644);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_cimodule_param(enc_mpg_buffers, int, 0644);
16062306a36Sopenharmony_cimodule_param(enc_yuv_buffers, int, 0644);
16162306a36Sopenharmony_cimodule_param(enc_vbi_buffers, int, 0644);
16262306a36Sopenharmony_cimodule_param(enc_pcm_buffers, int, 0644);
16362306a36Sopenharmony_cimodule_param(dec_mpg_buffers, int, 0644);
16462306a36Sopenharmony_cimodule_param(dec_yuv_buffers, int, 0644);
16562306a36Sopenharmony_cimodule_param(dec_vbi_buffers, int, 0644);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cimodule_param(tunertype, int, 0644);
16862306a36Sopenharmony_cimodule_param(newi2c, int, 0644);
16962306a36Sopenharmony_cimodule_param_array(i2c_clock_period, int, &i2c_clock_period_c, 0644);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciMODULE_PARM_DESC(tuner, "Tuner type selection,\n"
17262306a36Sopenharmony_ci			"\t\t\tsee tuner.h for values");
17362306a36Sopenharmony_ciMODULE_PARM_DESC(radio,
17462306a36Sopenharmony_ci		 "Enable or disable the radio. Use only if autodetection\n"
17562306a36Sopenharmony_ci		 "\t\t\tfails. 0 = disable, 1 = enable");
17662306a36Sopenharmony_ciMODULE_PARM_DESC(cardtype,
17762306a36Sopenharmony_ci		 "Only use this option if your card is not detected properly.\n"
17862306a36Sopenharmony_ci		 "\t\tSpecify card type:\n"
17962306a36Sopenharmony_ci		 "\t\t\t 1 = WinTV PVR 250\n"
18062306a36Sopenharmony_ci		 "\t\t\t 2 = WinTV PVR 350\n"
18162306a36Sopenharmony_ci		 "\t\t\t 3 = WinTV PVR-150 or PVR-500\n"
18262306a36Sopenharmony_ci		 "\t\t\t 4 = AVerMedia M179\n"
18362306a36Sopenharmony_ci		 "\t\t\t 5 = YUAN MPG600/Kuroutoshikou iTVC16-STVLP\n"
18462306a36Sopenharmony_ci		 "\t\t\t 6 = YUAN MPG160/Kuroutoshikou iTVC15-STVLP\n"
18562306a36Sopenharmony_ci		 "\t\t\t 7 = YUAN PG600/DIAMONDMM PVR-550 (CX Falcon 2)\n"
18662306a36Sopenharmony_ci		 "\t\t\t 8 = Adaptec AVC-2410\n"
18762306a36Sopenharmony_ci		 "\t\t\t 9 = Adaptec AVC-2010\n"
18862306a36Sopenharmony_ci		 "\t\t\t10 = NAGASE TRANSGEAR 5000TV\n"
18962306a36Sopenharmony_ci		 "\t\t\t11 = AOpen VA2000MAX-STN6\n"
19062306a36Sopenharmony_ci		 "\t\t\t12 = YUAN MPG600GR/Kuroutoshikou CX23416GYC-STVLP\n"
19162306a36Sopenharmony_ci		 "\t\t\t13 = I/O Data GV-MVP/RX\n"
19262306a36Sopenharmony_ci		 "\t\t\t14 = I/O Data GV-MVP/RX2E\n"
19362306a36Sopenharmony_ci		 "\t\t\t15 = GOTVIEW PCI DVD\n"
19462306a36Sopenharmony_ci		 "\t\t\t16 = GOTVIEW PCI DVD2 Deluxe\n"
19562306a36Sopenharmony_ci		 "\t\t\t17 = Yuan MPC622\n"
19662306a36Sopenharmony_ci		 "\t\t\t18 = Digital Cowboy DCT-MTVP1\n"
19762306a36Sopenharmony_ci		 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"
19862306a36Sopenharmony_ci		 "\t\t\t20 = Club3D ZAP-TV1x01\n"
19962306a36Sopenharmony_ci		 "\t\t\t21 = AverTV MCE 116 Plus\n"
20062306a36Sopenharmony_ci		 "\t\t\t22 = ASUS Falcon2\n"
20162306a36Sopenharmony_ci		 "\t\t\t23 = AverMedia PVR-150 Plus\n"
20262306a36Sopenharmony_ci		 "\t\t\t24 = AverMedia EZMaker PCI Deluxe\n"
20362306a36Sopenharmony_ci		 "\t\t\t25 = AverMedia M104 (not yet working)\n"
20462306a36Sopenharmony_ci		 "\t\t\t26 = Buffalo PC-MV5L/PCI\n"
20562306a36Sopenharmony_ci		 "\t\t\t27 = AVerMedia UltraTV 1500 MCE\n"
20662306a36Sopenharmony_ci		 "\t\t\t28 = Sony VAIO Giga Pocket (ENX Kikyou)\n"
20762306a36Sopenharmony_ci		 "\t\t\t 0 = Autodetect (default)\n"
20862306a36Sopenharmony_ci		 "\t\t\t-1 = Ignore this card\n\t\t");
20962306a36Sopenharmony_ciMODULE_PARM_DESC(pal, "Set PAL standard: BGH, DK, I, M, N, Nc, 60");
21062306a36Sopenharmony_ciMODULE_PARM_DESC(secam, "Set SECAM standard: BGH, DK, L, LC");
21162306a36Sopenharmony_ciMODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J (Japan), K (South Korea)");
21262306a36Sopenharmony_ciMODULE_PARM_DESC(tunertype,
21362306a36Sopenharmony_ci		"Specify tuner type:\n"
21462306a36Sopenharmony_ci		"\t\t\t 0 = tuner for PAL-B/G/H/D/K/I, SECAM-B/G/H/D/K/L/Lc\n"
21562306a36Sopenharmony_ci		"\t\t\t 1 = tuner for NTSC-M/J/K, PAL-M/N/Nc\n"
21662306a36Sopenharmony_ci		"\t\t\t-1 = Autodetect (default)\n");
21762306a36Sopenharmony_ciMODULE_PARM_DESC(debug,
21862306a36Sopenharmony_ci		 "Debug level (bitmask). Default: 0\n"
21962306a36Sopenharmony_ci		 "\t\t\t   1/0x0001: warning\n"
22062306a36Sopenharmony_ci		 "\t\t\t   2/0x0002: info\n"
22162306a36Sopenharmony_ci		 "\t\t\t   4/0x0004: mailbox\n"
22262306a36Sopenharmony_ci		 "\t\t\t   8/0x0008: ioctl\n"
22362306a36Sopenharmony_ci		 "\t\t\t  16/0x0010: file\n"
22462306a36Sopenharmony_ci		 "\t\t\t  32/0x0020: dma\n"
22562306a36Sopenharmony_ci		 "\t\t\t  64/0x0040: irq\n"
22662306a36Sopenharmony_ci		 "\t\t\t 128/0x0080: decoder\n"
22762306a36Sopenharmony_ci		 "\t\t\t 256/0x0100: yuv\n"
22862306a36Sopenharmony_ci		 "\t\t\t 512/0x0200: i2c\n"
22962306a36Sopenharmony_ci		 "\t\t\t1024/0x0400: high volume\n");
23062306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
23162306a36Sopenharmony_ciMODULE_PARM_DESC(fw_debug,
23262306a36Sopenharmony_ci		 "Enable code for debugging firmware problems.  Default: 0\n");
23362306a36Sopenharmony_ci#endif
23462306a36Sopenharmony_ciMODULE_PARM_DESC(ivtv_pci_latency,
23562306a36Sopenharmony_ci		 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
23662306a36Sopenharmony_ci		 "\t\t\tDefault: Yes");
23762306a36Sopenharmony_ciMODULE_PARM_DESC(ivtv_yuv_mode,
23862306a36Sopenharmony_ci		 "Specify the yuv playback mode:\n"
23962306a36Sopenharmony_ci		 "\t\t\t0 = interlaced\n\t\t\t1 = progressive\n\t\t\t2 = auto\n"
24062306a36Sopenharmony_ci		 "\t\t\tDefault: 0 (interlaced)");
24162306a36Sopenharmony_ciMODULE_PARM_DESC(ivtv_yuv_threshold,
24262306a36Sopenharmony_ci		 "If ivtv_yuv_mode is 2 (auto) then playback content as\n\t\tprogressive if src height <= ivtv_yuvthreshold\n"
24362306a36Sopenharmony_ci		 "\t\t\tDefault: 480");
24462306a36Sopenharmony_ciMODULE_PARM_DESC(enc_mpg_buffers,
24562306a36Sopenharmony_ci		 "Encoder MPG Buffers (in MB)\n"
24662306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_MPG_BUFFERS));
24762306a36Sopenharmony_ciMODULE_PARM_DESC(enc_yuv_buffers,
24862306a36Sopenharmony_ci		 "Encoder YUV Buffers (in MB)\n"
24962306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_YUV_BUFFERS));
25062306a36Sopenharmony_ciMODULE_PARM_DESC(enc_vbi_buffers,
25162306a36Sopenharmony_ci		 "Encoder VBI Buffers (in MB)\n"
25262306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_VBI_BUFFERS));
25362306a36Sopenharmony_ciMODULE_PARM_DESC(enc_pcm_buffers,
25462306a36Sopenharmony_ci		 "Encoder PCM buffers (in kB)\n"
25562306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_PCM_BUFFERS));
25662306a36Sopenharmony_ciMODULE_PARM_DESC(dec_mpg_buffers,
25762306a36Sopenharmony_ci		 "Decoder MPG buffers (in MB)\n"
25862306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_MPG_BUFFERS));
25962306a36Sopenharmony_ciMODULE_PARM_DESC(dec_yuv_buffers,
26062306a36Sopenharmony_ci		 "Decoder YUV buffers (in MB)\n"
26162306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_YUV_BUFFERS));
26262306a36Sopenharmony_ciMODULE_PARM_DESC(dec_vbi_buffers,
26362306a36Sopenharmony_ci		 "Decoder VBI buffers (in kB)\n"
26462306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_VBI_BUFFERS));
26562306a36Sopenharmony_ciMODULE_PARM_DESC(newi2c,
26662306a36Sopenharmony_ci		 "Use new I2C implementation\n"
26762306a36Sopenharmony_ci		 "\t\t\t-1 is autodetect, 0 is off, 1 is on\n"
26862306a36Sopenharmony_ci		 "\t\t\tDefault is autodetect");
26962306a36Sopenharmony_ciMODULE_PARM_DESC(i2c_clock_period,
27062306a36Sopenharmony_ci		 "Period of SCL for the I2C bus controlled by the CX23415/6\n"
27162306a36Sopenharmony_ci		 "\t\t\tMin: 10 usec (100 kHz), Max: 4500 usec (222 Hz)\n"
27262306a36Sopenharmony_ci		 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_I2C_CLOCK_PERIOD));
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ciMODULE_PARM_DESC(ivtv_first_minor, "Set device node number assigned to first card");
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ciMODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");
27762306a36Sopenharmony_ciMODULE_DESCRIPTION("CX23415/CX23416 driver");
27862306a36Sopenharmony_ciMODULE_LICENSE("GPL");
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ciMODULE_VERSION(IVTV_VERSION);
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci#if defined(CONFIG_MODULES) && defined(MODULE)
28362306a36Sopenharmony_cistatic void request_module_async(struct work_struct *work)
28462306a36Sopenharmony_ci{
28562306a36Sopenharmony_ci	struct ivtv *dev = container_of(work, struct ivtv, request_module_wk);
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	/* Make sure ivtv-alsa module is loaded */
28862306a36Sopenharmony_ci	request_module("ivtv-alsa");
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	/* Initialize ivtv-alsa for this instance of the cx18 device */
29162306a36Sopenharmony_ci	if (ivtv_ext_init != NULL)
29262306a36Sopenharmony_ci		ivtv_ext_init(dev);
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic void request_modules(struct ivtv *dev)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	INIT_WORK(&dev->request_module_wk, request_module_async);
29862306a36Sopenharmony_ci	schedule_work(&dev->request_module_wk);
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic void flush_request_modules(struct ivtv *dev)
30262306a36Sopenharmony_ci{
30362306a36Sopenharmony_ci	flush_work(&dev->request_module_wk);
30462306a36Sopenharmony_ci}
30562306a36Sopenharmony_ci#else
30662306a36Sopenharmony_ci#define request_modules(dev)
30762306a36Sopenharmony_ci#define flush_request_modules(dev)
30862306a36Sopenharmony_ci#endif /* CONFIG_MODULES */
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_civoid ivtv_clear_irq_mask(struct ivtv *itv, u32 mask)
31162306a36Sopenharmony_ci{
31262306a36Sopenharmony_ci	itv->irqmask &= ~mask;
31362306a36Sopenharmony_ci	write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_civoid ivtv_set_irq_mask(struct ivtv *itv, u32 mask)
31762306a36Sopenharmony_ci{
31862306a36Sopenharmony_ci	itv->irqmask |= mask;
31962306a36Sopenharmony_ci	write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
32062306a36Sopenharmony_ci}
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ciint ivtv_set_output_mode(struct ivtv *itv, int mode)
32362306a36Sopenharmony_ci{
32462306a36Sopenharmony_ci    int old_mode;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci    spin_lock(&itv->lock);
32762306a36Sopenharmony_ci    old_mode = itv->output_mode;
32862306a36Sopenharmony_ci    if (old_mode == 0)
32962306a36Sopenharmony_ci	itv->output_mode = old_mode = mode;
33062306a36Sopenharmony_ci    spin_unlock(&itv->lock);
33162306a36Sopenharmony_ci    return old_mode;
33262306a36Sopenharmony_ci}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_cistruct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv)
33562306a36Sopenharmony_ci{
33662306a36Sopenharmony_ci	switch (itv->output_mode) {
33762306a36Sopenharmony_ci	case OUT_MPG:
33862306a36Sopenharmony_ci		return &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
33962306a36Sopenharmony_ci	case OUT_YUV:
34062306a36Sopenharmony_ci		return &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
34162306a36Sopenharmony_ci	default:
34262306a36Sopenharmony_ci		return NULL;
34362306a36Sopenharmony_ci	}
34462306a36Sopenharmony_ci}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ciint ivtv_waitq(wait_queue_head_t *waitq)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	DEFINE_WAIT(wait);
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	prepare_to_wait(waitq, &wait, TASK_INTERRUPTIBLE);
35162306a36Sopenharmony_ci	schedule();
35262306a36Sopenharmony_ci	finish_wait(waitq, &wait);
35362306a36Sopenharmony_ci	return signal_pending(current) ? -EINTR : 0;
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci/* Generic utility functions */
35762306a36Sopenharmony_ciint ivtv_msleep_timeout(unsigned int msecs, int intr)
35862306a36Sopenharmony_ci{
35962306a36Sopenharmony_ci	int timeout = msecs_to_jiffies(msecs);
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	do {
36262306a36Sopenharmony_ci		set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
36362306a36Sopenharmony_ci		timeout = schedule_timeout(timeout);
36462306a36Sopenharmony_ci		if (intr) {
36562306a36Sopenharmony_ci			int ret = signal_pending(current);
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci			if (ret)
36862306a36Sopenharmony_ci				return ret;
36962306a36Sopenharmony_ci		}
37062306a36Sopenharmony_ci	} while (timeout);
37162306a36Sopenharmony_ci	return 0;
37262306a36Sopenharmony_ci}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci/* Release ioremapped memory */
37562306a36Sopenharmony_cistatic void ivtv_iounmap(struct ivtv *itv)
37662306a36Sopenharmony_ci{
37762306a36Sopenharmony_ci	if (itv == NULL)
37862306a36Sopenharmony_ci		return;
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	/* Release registers memory */
38162306a36Sopenharmony_ci	if (itv->reg_mem != NULL) {
38262306a36Sopenharmony_ci		IVTV_DEBUG_INFO("releasing reg_mem\n");
38362306a36Sopenharmony_ci		iounmap(itv->reg_mem);
38462306a36Sopenharmony_ci		itv->reg_mem = NULL;
38562306a36Sopenharmony_ci	}
38662306a36Sopenharmony_ci	/* Release io memory */
38762306a36Sopenharmony_ci	if (itv->has_cx23415 && itv->dec_mem != NULL) {
38862306a36Sopenharmony_ci		IVTV_DEBUG_INFO("releasing dec_mem\n");
38962306a36Sopenharmony_ci		iounmap(itv->dec_mem);
39062306a36Sopenharmony_ci	}
39162306a36Sopenharmony_ci	itv->dec_mem = NULL;
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	/* Release io memory */
39462306a36Sopenharmony_ci	if (itv->enc_mem != NULL) {
39562306a36Sopenharmony_ci		IVTV_DEBUG_INFO("releasing enc_mem\n");
39662306a36Sopenharmony_ci		iounmap(itv->enc_mem);
39762306a36Sopenharmony_ci		itv->enc_mem = NULL;
39862306a36Sopenharmony_ci	}
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci/* Hauppauge card? get values from tveeprom */
40262306a36Sopenharmony_civoid ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	u8 eedata[256];
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	itv->i2c_client.addr = 0xA0 >> 1;
40762306a36Sopenharmony_ci	tveeprom_read(&itv->i2c_client, eedata, sizeof(eedata));
40862306a36Sopenharmony_ci	tveeprom_hauppauge_analog(tv, eedata);
40962306a36Sopenharmony_ci}
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_cistatic void ivtv_process_eeprom(struct ivtv *itv)
41262306a36Sopenharmony_ci{
41362306a36Sopenharmony_ci	struct tveeprom tv;
41462306a36Sopenharmony_ci	int pci_slot = PCI_SLOT(itv->pdev->devfn);
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	ivtv_read_eeprom(itv, &tv);
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	/* Many thanks to Steven Toth from Hauppauge for providing the
41962306a36Sopenharmony_ci	   model numbers */
42062306a36Sopenharmony_ci	switch (tv.model) {
42162306a36Sopenharmony_ci		/* In a few cases the PCI subsystem IDs do not correctly
42262306a36Sopenharmony_ci		   identify the card. A better method is to check the
42362306a36Sopenharmony_ci		   model number from the eeprom instead. */
42462306a36Sopenharmony_ci		case 30012 ... 30039:  /* Low profile PVR250 */
42562306a36Sopenharmony_ci		case 32000 ... 32999:
42662306a36Sopenharmony_ci		case 48000 ... 48099:  /* 48??? range are PVR250s with a cx23415 */
42762306a36Sopenharmony_ci		case 48400 ... 48599:
42862306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_PVR_250);
42962306a36Sopenharmony_ci			break;
43062306a36Sopenharmony_ci		case 48100 ... 48399:
43162306a36Sopenharmony_ci		case 48600 ... 48999:
43262306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_PVR_350);
43362306a36Sopenharmony_ci			break;
43462306a36Sopenharmony_ci		case 23000 ... 23999:  /* PVR500 */
43562306a36Sopenharmony_ci		case 25000 ... 25999:  /* Low profile PVR150 */
43662306a36Sopenharmony_ci		case 26000 ... 26999:  /* Regular PVR150 */
43762306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
43862306a36Sopenharmony_ci			break;
43962306a36Sopenharmony_ci		case 0:
44062306a36Sopenharmony_ci			IVTV_ERR("Invalid EEPROM\n");
44162306a36Sopenharmony_ci			return;
44262306a36Sopenharmony_ci		default:
44362306a36Sopenharmony_ci			IVTV_ERR("Unknown model %d, defaulting to PVR-150\n", tv.model);
44462306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
44562306a36Sopenharmony_ci			break;
44662306a36Sopenharmony_ci	}
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci	switch (tv.model) {
44962306a36Sopenharmony_ci		/* Old style PVR350 (with an saa7114) uses this input for
45062306a36Sopenharmony_ci		   the tuner. */
45162306a36Sopenharmony_ci		case 48254:
45262306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_PVR_350_V1);
45362306a36Sopenharmony_ci			break;
45462306a36Sopenharmony_ci		default:
45562306a36Sopenharmony_ci			break;
45662306a36Sopenharmony_ci	}
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci	itv->v4l2_cap = itv->card->v4l2_capabilities;
45962306a36Sopenharmony_ci	itv->card_name = itv->card->name;
46062306a36Sopenharmony_ci	itv->card_i2c = itv->card->i2c;
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	/* If this is a PVR500 then it should be possible to detect whether it is the
46362306a36Sopenharmony_ci	   first or second unit by looking at the subsystem device ID: is bit 4 is
46462306a36Sopenharmony_ci	   set, then it is the second unit (according to info from Hauppauge).
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	   However, while this works for most cards, I have seen a few PVR500 cards
46762306a36Sopenharmony_ci	   where both units have the same subsystem ID.
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	   So instead I look at the reported 'PCI slot' (which is the slot on the PVR500
47062306a36Sopenharmony_ci	   PCI bridge) and if it is 8, then it is assumed to be the first unit, otherwise
47162306a36Sopenharmony_ci	   it is the second unit. It is possible that it is a different slot when ivtv is
47262306a36Sopenharmony_ci	   used in Xen, in that case I ignore this card here. The worst that can happen
47362306a36Sopenharmony_ci	   is that the card presents itself with a non-working radio device.
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	   This detection is needed since the eeprom reports incorrectly that a radio is
47662306a36Sopenharmony_ci	   present on the second unit. */
47762306a36Sopenharmony_ci	if (tv.model / 1000 == 23) {
47862306a36Sopenharmony_ci		static const struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
47962306a36Sopenharmony_ci			.radio = { 0x60, I2C_CLIENT_END },
48062306a36Sopenharmony_ci			.demod = { 0x43, I2C_CLIENT_END },
48162306a36Sopenharmony_ci			.tv = { 0x61, I2C_CLIENT_END },
48262306a36Sopenharmony_ci		};
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci		itv->card_name = "WinTV PVR 500";
48562306a36Sopenharmony_ci		itv->card_i2c = &ivtv_i2c_radio;
48662306a36Sopenharmony_ci		if (pci_slot == 8 || pci_slot == 9) {
48762306a36Sopenharmony_ci			int is_first = (pci_slot & 1) == 0;
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci			itv->card_name = is_first ? "WinTV PVR 500 (unit #1)" :
49062306a36Sopenharmony_ci						    "WinTV PVR 500 (unit #2)";
49162306a36Sopenharmony_ci			if (!is_first) {
49262306a36Sopenharmony_ci				IVTV_INFO("Correcting tveeprom data: no radio present on second unit\n");
49362306a36Sopenharmony_ci				tv.has_radio = 0;
49462306a36Sopenharmony_ci			}
49562306a36Sopenharmony_ci		}
49662306a36Sopenharmony_ci	}
49762306a36Sopenharmony_ci	IVTV_INFO("Autodetected %s\n", itv->card_name);
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci	switch (tv.tuner_hauppauge_model) {
50062306a36Sopenharmony_ci		case 85:
50162306a36Sopenharmony_ci		case 99:
50262306a36Sopenharmony_ci		case 112:
50362306a36Sopenharmony_ci			itv->pvr150_workaround = 1;
50462306a36Sopenharmony_ci			break;
50562306a36Sopenharmony_ci		default:
50662306a36Sopenharmony_ci			break;
50762306a36Sopenharmony_ci	}
50862306a36Sopenharmony_ci	if (tv.tuner_type == TUNER_ABSENT)
50962306a36Sopenharmony_ci		IVTV_ERR("tveeprom cannot autodetect tuner!\n");
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	if (itv->options.tuner == -1)
51262306a36Sopenharmony_ci		itv->options.tuner = tv.tuner_type;
51362306a36Sopenharmony_ci	if (itv->options.radio == -1)
51462306a36Sopenharmony_ci		itv->options.radio = (tv.has_radio != 0);
51562306a36Sopenharmony_ci	/* only enable newi2c if an IR blaster is present */
51662306a36Sopenharmony_ci	if (itv->options.newi2c == -1 && tv.has_ir) {
51762306a36Sopenharmony_ci		itv->options.newi2c = (tv.has_ir & 4) ? 1 : 0;
51862306a36Sopenharmony_ci		if (itv->options.newi2c) {
51962306a36Sopenharmony_ci		    IVTV_INFO("Reopen i2c bus for IR-blaster support\n");
52062306a36Sopenharmony_ci		    exit_ivtv_i2c(itv);
52162306a36Sopenharmony_ci		    init_ivtv_i2c(itv);
52262306a36Sopenharmony_ci		}
52362306a36Sopenharmony_ci	}
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	if (itv->std != 0)
52662306a36Sopenharmony_ci		/* user specified tuner standard */
52762306a36Sopenharmony_ci		return;
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci	/* autodetect tuner standard */
53062306a36Sopenharmony_ci	if (tv.tuner_formats & V4L2_STD_PAL) {
53162306a36Sopenharmony_ci		IVTV_DEBUG_INFO("PAL tuner detected\n");
53262306a36Sopenharmony_ci		itv->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
53362306a36Sopenharmony_ci	} else if (tv.tuner_formats & V4L2_STD_NTSC) {
53462306a36Sopenharmony_ci		IVTV_DEBUG_INFO("NTSC tuner detected\n");
53562306a36Sopenharmony_ci		itv->std |= V4L2_STD_NTSC_M;
53662306a36Sopenharmony_ci	} else if (tv.tuner_formats & V4L2_STD_SECAM) {
53762306a36Sopenharmony_ci		IVTV_DEBUG_INFO("SECAM tuner detected\n");
53862306a36Sopenharmony_ci		itv->std |= V4L2_STD_SECAM_L;
53962306a36Sopenharmony_ci	} else {
54062306a36Sopenharmony_ci		IVTV_INFO("No tuner detected, default to NTSC-M\n");
54162306a36Sopenharmony_ci		itv->std |= V4L2_STD_NTSC_M;
54262306a36Sopenharmony_ci	}
54362306a36Sopenharmony_ci}
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_cistatic v4l2_std_id ivtv_parse_std(struct ivtv *itv)
54662306a36Sopenharmony_ci{
54762306a36Sopenharmony_ci	switch (pal[0]) {
54862306a36Sopenharmony_ci		case '6':
54962306a36Sopenharmony_ci			tunertype = 0;
55062306a36Sopenharmony_ci			return V4L2_STD_PAL_60;
55162306a36Sopenharmony_ci		case 'b':
55262306a36Sopenharmony_ci		case 'B':
55362306a36Sopenharmony_ci		case 'g':
55462306a36Sopenharmony_ci		case 'G':
55562306a36Sopenharmony_ci		case 'h':
55662306a36Sopenharmony_ci		case 'H':
55762306a36Sopenharmony_ci			tunertype = 0;
55862306a36Sopenharmony_ci			return V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
55962306a36Sopenharmony_ci		case 'n':
56062306a36Sopenharmony_ci		case 'N':
56162306a36Sopenharmony_ci			tunertype = 1;
56262306a36Sopenharmony_ci			if (pal[1] == 'c' || pal[1] == 'C')
56362306a36Sopenharmony_ci				return V4L2_STD_PAL_Nc;
56462306a36Sopenharmony_ci			return V4L2_STD_PAL_N;
56562306a36Sopenharmony_ci		case 'i':
56662306a36Sopenharmony_ci		case 'I':
56762306a36Sopenharmony_ci			tunertype = 0;
56862306a36Sopenharmony_ci			return V4L2_STD_PAL_I;
56962306a36Sopenharmony_ci		case 'd':
57062306a36Sopenharmony_ci		case 'D':
57162306a36Sopenharmony_ci		case 'k':
57262306a36Sopenharmony_ci		case 'K':
57362306a36Sopenharmony_ci			tunertype = 0;
57462306a36Sopenharmony_ci			return V4L2_STD_PAL_DK;
57562306a36Sopenharmony_ci		case 'M':
57662306a36Sopenharmony_ci		case 'm':
57762306a36Sopenharmony_ci			tunertype = 1;
57862306a36Sopenharmony_ci			return V4L2_STD_PAL_M;
57962306a36Sopenharmony_ci		case '-':
58062306a36Sopenharmony_ci			break;
58162306a36Sopenharmony_ci		default:
58262306a36Sopenharmony_ci			IVTV_WARN("pal= argument not recognised\n");
58362306a36Sopenharmony_ci			return 0;
58462306a36Sopenharmony_ci	}
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci	switch (secam[0]) {
58762306a36Sopenharmony_ci		case 'b':
58862306a36Sopenharmony_ci		case 'B':
58962306a36Sopenharmony_ci		case 'g':
59062306a36Sopenharmony_ci		case 'G':
59162306a36Sopenharmony_ci		case 'h':
59262306a36Sopenharmony_ci		case 'H':
59362306a36Sopenharmony_ci			tunertype = 0;
59462306a36Sopenharmony_ci			return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
59562306a36Sopenharmony_ci		case 'd':
59662306a36Sopenharmony_ci		case 'D':
59762306a36Sopenharmony_ci		case 'k':
59862306a36Sopenharmony_ci		case 'K':
59962306a36Sopenharmony_ci			tunertype = 0;
60062306a36Sopenharmony_ci			return V4L2_STD_SECAM_DK;
60162306a36Sopenharmony_ci		case 'l':
60262306a36Sopenharmony_ci		case 'L':
60362306a36Sopenharmony_ci			tunertype = 0;
60462306a36Sopenharmony_ci			if (secam[1] == 'C' || secam[1] == 'c')
60562306a36Sopenharmony_ci				return V4L2_STD_SECAM_LC;
60662306a36Sopenharmony_ci			return V4L2_STD_SECAM_L;
60762306a36Sopenharmony_ci		case '-':
60862306a36Sopenharmony_ci			break;
60962306a36Sopenharmony_ci		default:
61062306a36Sopenharmony_ci			IVTV_WARN("secam= argument not recognised\n");
61162306a36Sopenharmony_ci			return 0;
61262306a36Sopenharmony_ci	}
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	switch (ntsc[0]) {
61562306a36Sopenharmony_ci		case 'm':
61662306a36Sopenharmony_ci		case 'M':
61762306a36Sopenharmony_ci			tunertype = 1;
61862306a36Sopenharmony_ci			return V4L2_STD_NTSC_M;
61962306a36Sopenharmony_ci		case 'j':
62062306a36Sopenharmony_ci		case 'J':
62162306a36Sopenharmony_ci			tunertype = 1;
62262306a36Sopenharmony_ci			return V4L2_STD_NTSC_M_JP;
62362306a36Sopenharmony_ci		case 'k':
62462306a36Sopenharmony_ci		case 'K':
62562306a36Sopenharmony_ci			tunertype = 1;
62662306a36Sopenharmony_ci			return V4L2_STD_NTSC_M_KR;
62762306a36Sopenharmony_ci		case '-':
62862306a36Sopenharmony_ci			break;
62962306a36Sopenharmony_ci		default:
63062306a36Sopenharmony_ci			IVTV_WARN("ntsc= argument not recognised\n");
63162306a36Sopenharmony_ci			return 0;
63262306a36Sopenharmony_ci	}
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci	/* no match found */
63562306a36Sopenharmony_ci	return 0;
63662306a36Sopenharmony_ci}
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_cistatic void ivtv_process_options(struct ivtv *itv)
63962306a36Sopenharmony_ci{
64062306a36Sopenharmony_ci	const char *chipname;
64162306a36Sopenharmony_ci	int i, j;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers * 1024;
64462306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers * 1024;
64562306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers * 1024;
64662306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
64762306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_MPG] = dec_mpg_buffers * 1024;
64862306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_YUV] = dec_yuv_buffers * 1024;
64962306a36Sopenharmony_ci	itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_VBI] = dec_vbi_buffers;
65062306a36Sopenharmony_ci	itv->options.cardtype = cardtype[itv->instance];
65162306a36Sopenharmony_ci	itv->options.tuner = tuner[itv->instance];
65262306a36Sopenharmony_ci	itv->options.radio = radio[itv->instance];
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	itv->options.i2c_clock_period = i2c_clock_period[itv->instance];
65562306a36Sopenharmony_ci	if (itv->options.i2c_clock_period == -1)
65662306a36Sopenharmony_ci		itv->options.i2c_clock_period = IVTV_DEFAULT_I2C_CLOCK_PERIOD;
65762306a36Sopenharmony_ci	else if (itv->options.i2c_clock_period < 10)
65862306a36Sopenharmony_ci		itv->options.i2c_clock_period = 10;
65962306a36Sopenharmony_ci	else if (itv->options.i2c_clock_period > 4500)
66062306a36Sopenharmony_ci		itv->options.i2c_clock_period = 4500;
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	itv->options.newi2c = newi2c;
66362306a36Sopenharmony_ci	if (tunertype < -1 || tunertype > 1) {
66462306a36Sopenharmony_ci		IVTV_WARN("Invalid tunertype argument, will autodetect instead\n");
66562306a36Sopenharmony_ci		tunertype = -1;
66662306a36Sopenharmony_ci	}
66762306a36Sopenharmony_ci	itv->std = ivtv_parse_std(itv);
66862306a36Sopenharmony_ci	if (itv->std == 0 && tunertype >= 0)
66962306a36Sopenharmony_ci		itv->std = tunertype ? V4L2_STD_MN : (V4L2_STD_ALL & ~V4L2_STD_MN);
67062306a36Sopenharmony_ci	itv->has_cx23415 = (itv->pdev->device == PCI_DEVICE_ID_IVTV15);
67162306a36Sopenharmony_ci	chipname = itv->has_cx23415 ? "cx23415" : "cx23416";
67262306a36Sopenharmony_ci	if (itv->options.cardtype == -1) {
67362306a36Sopenharmony_ci		IVTV_INFO("Ignore card (detected %s based chip)\n", chipname);
67462306a36Sopenharmony_ci		return;
67562306a36Sopenharmony_ci	}
67662306a36Sopenharmony_ci	if ((itv->card = ivtv_get_card(itv->options.cardtype - 1))) {
67762306a36Sopenharmony_ci		IVTV_INFO("User specified %s card (detected %s based chip)\n",
67862306a36Sopenharmony_ci				itv->card->name, chipname);
67962306a36Sopenharmony_ci	} else if (itv->options.cardtype != 0) {
68062306a36Sopenharmony_ci		IVTV_ERR("Unknown user specified type, trying to autodetect card\n");
68162306a36Sopenharmony_ci	}
68262306a36Sopenharmony_ci	if (itv->card == NULL) {
68362306a36Sopenharmony_ci		if (itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE ||
68462306a36Sopenharmony_ci		    itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT1 ||
68562306a36Sopenharmony_ci		    itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT2) {
68662306a36Sopenharmony_ci			itv->card = ivtv_get_card(itv->has_cx23415 ? IVTV_CARD_PVR_350 : IVTV_CARD_PVR_150);
68762306a36Sopenharmony_ci			IVTV_INFO("Autodetected Hauppauge card (%s based)\n",
68862306a36Sopenharmony_ci					chipname);
68962306a36Sopenharmony_ci		}
69062306a36Sopenharmony_ci	}
69162306a36Sopenharmony_ci	if (itv->card == NULL) {
69262306a36Sopenharmony_ci		for (i = 0; (itv->card = ivtv_get_card(i)); i++) {
69362306a36Sopenharmony_ci			if (itv->card->pci_list == NULL)
69462306a36Sopenharmony_ci				continue;
69562306a36Sopenharmony_ci			for (j = 0; itv->card->pci_list[j].device; j++) {
69662306a36Sopenharmony_ci				if (itv->pdev->device !=
69762306a36Sopenharmony_ci				    itv->card->pci_list[j].device)
69862306a36Sopenharmony_ci					continue;
69962306a36Sopenharmony_ci				if (itv->pdev->subsystem_vendor !=
70062306a36Sopenharmony_ci				    itv->card->pci_list[j].subsystem_vendor)
70162306a36Sopenharmony_ci					continue;
70262306a36Sopenharmony_ci				if (itv->pdev->subsystem_device !=
70362306a36Sopenharmony_ci				    itv->card->pci_list[j].subsystem_device)
70462306a36Sopenharmony_ci					continue;
70562306a36Sopenharmony_ci				IVTV_INFO("Autodetected %s card (%s based)\n",
70662306a36Sopenharmony_ci						itv->card->name, chipname);
70762306a36Sopenharmony_ci				goto done;
70862306a36Sopenharmony_ci			}
70962306a36Sopenharmony_ci		}
71062306a36Sopenharmony_ci	}
71162306a36Sopenharmony_cidone:
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	if (itv->card == NULL) {
71462306a36Sopenharmony_ci		itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
71562306a36Sopenharmony_ci		IVTV_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
71662306a36Sopenharmony_ci		     itv->pdev->vendor, itv->pdev->device);
71762306a36Sopenharmony_ci		IVTV_ERR("              subsystem vendor/device: [%04x:%04x]\n",
71862306a36Sopenharmony_ci		     itv->pdev->subsystem_vendor, itv->pdev->subsystem_device);
71962306a36Sopenharmony_ci		IVTV_ERR("              %s based\n", chipname);
72062306a36Sopenharmony_ci		IVTV_ERR("Defaulting to %s card\n", itv->card->name);
72162306a36Sopenharmony_ci		IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
72262306a36Sopenharmony_ci		IVTV_ERR("card you have to the linux-media mailinglist (www.linuxtv.org)\n");
72362306a36Sopenharmony_ci		IVTV_ERR("Prefix your subject line with [UNKNOWN IVTV CARD].\n");
72462306a36Sopenharmony_ci	}
72562306a36Sopenharmony_ci	itv->v4l2_cap = itv->card->v4l2_capabilities;
72662306a36Sopenharmony_ci	itv->card_name = itv->card->name;
72762306a36Sopenharmony_ci	itv->card_i2c = itv->card->i2c;
72862306a36Sopenharmony_ci}
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci/* Precondition: the ivtv structure has been memset to 0. Only
73162306a36Sopenharmony_ci   the dev and num fields have been filled in.
73262306a36Sopenharmony_ci   No assumptions on the card type may be made here (see ivtv_init_struct2
73362306a36Sopenharmony_ci   for that).
73462306a36Sopenharmony_ci */
73562306a36Sopenharmony_cistatic int ivtv_init_struct1(struct ivtv *itv)
73662306a36Sopenharmony_ci{
73762306a36Sopenharmony_ci	itv->base_addr = pci_resource_start(itv->pdev, 0);
73862306a36Sopenharmony_ci	itv->enc_mbox.max_mbox = 2; /* the encoder has 3 mailboxes (0-2) */
73962306a36Sopenharmony_ci	itv->dec_mbox.max_mbox = 1; /* the decoder has 2 mailboxes (0-1) */
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci	mutex_init(&itv->serialize_lock);
74262306a36Sopenharmony_ci	mutex_init(&itv->i2c_bus_lock);
74362306a36Sopenharmony_ci	mutex_init(&itv->udma.lock);
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci	spin_lock_init(&itv->lock);
74662306a36Sopenharmony_ci	spin_lock_init(&itv->dma_reg_lock);
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	kthread_init_worker(&itv->irq_worker);
74962306a36Sopenharmony_ci	itv->irq_worker_task = kthread_run(kthread_worker_fn, &itv->irq_worker,
75062306a36Sopenharmony_ci					   "%s", itv->v4l2_dev.name);
75162306a36Sopenharmony_ci	if (IS_ERR(itv->irq_worker_task)) {
75262306a36Sopenharmony_ci		IVTV_ERR("Could not create ivtv task\n");
75362306a36Sopenharmony_ci		return -1;
75462306a36Sopenharmony_ci	}
75562306a36Sopenharmony_ci	/* must use the FIFO scheduler as it is realtime sensitive */
75662306a36Sopenharmony_ci	sched_set_fifo(itv->irq_worker_task);
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci	kthread_init_work(&itv->irq_work, ivtv_irq_work_handler);
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci	/* Initial settings */
76162306a36Sopenharmony_ci	itv->cxhdl.port = CX2341X_PORT_MEMORY;
76262306a36Sopenharmony_ci	itv->cxhdl.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
76362306a36Sopenharmony_ci	init_waitqueue_head(&itv->eos_waitq);
76462306a36Sopenharmony_ci	init_waitqueue_head(&itv->event_waitq);
76562306a36Sopenharmony_ci	init_waitqueue_head(&itv->vsync_waitq);
76662306a36Sopenharmony_ci	init_waitqueue_head(&itv->dma_waitq);
76762306a36Sopenharmony_ci	timer_setup(&itv->dma_timer, ivtv_unfinished_dma, 0);
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	itv->cur_dma_stream = -1;
77062306a36Sopenharmony_ci	itv->cur_pio_stream = -1;
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci	/* Ctrls */
77362306a36Sopenharmony_ci	itv->speed = 1000;
77462306a36Sopenharmony_ci
77562306a36Sopenharmony_ci	/* VBI */
77662306a36Sopenharmony_ci	itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
77762306a36Sopenharmony_ci	itv->vbi.sliced_in = &itv->vbi.in.fmt.sliced;
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci	/* Init the sg table for osd/yuv output */
78062306a36Sopenharmony_ci	sg_init_table(itv->udma.SGlist, IVTV_DMA_SG_OSD_ENT);
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci	/* OSD */
78362306a36Sopenharmony_ci	itv->osd_global_alpha_state = 1;
78462306a36Sopenharmony_ci	itv->osd_global_alpha = 255;
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci	/* YUV */
78762306a36Sopenharmony_ci	atomic_set(&itv->yuv_info.next_dma_frame, -1);
78862306a36Sopenharmony_ci	itv->yuv_info.lace_mode = ivtv_yuv_mode;
78962306a36Sopenharmony_ci	itv->yuv_info.lace_threshold = ivtv_yuv_threshold;
79062306a36Sopenharmony_ci	itv->yuv_info.max_frames_buffered = 3;
79162306a36Sopenharmony_ci	itv->yuv_info.track_osd = 1;
79262306a36Sopenharmony_ci	return 0;
79362306a36Sopenharmony_ci}
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci/* Second initialization part. Here the card type has been
79662306a36Sopenharmony_ci   autodetected. */
79762306a36Sopenharmony_cistatic void ivtv_init_struct2(struct ivtv *itv)
79862306a36Sopenharmony_ci{
79962306a36Sopenharmony_ci	int i;
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	for (i = 0; i < IVTV_CARD_MAX_VIDEO_INPUTS; i++)
80262306a36Sopenharmony_ci		if (itv->card->video_inputs[i].video_type == 0)
80362306a36Sopenharmony_ci			break;
80462306a36Sopenharmony_ci	itv->nof_inputs = i;
80562306a36Sopenharmony_ci	for (i = 0; i < IVTV_CARD_MAX_AUDIO_INPUTS; i++)
80662306a36Sopenharmony_ci		if (itv->card->audio_inputs[i].audio_type == 0)
80762306a36Sopenharmony_ci			break;
80862306a36Sopenharmony_ci	itv->nof_audio_inputs = i;
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_ci	if (itv->card->hw_all & IVTV_HW_CX25840) {
81162306a36Sopenharmony_ci		itv->vbi.sliced_size = 288;  /* multiple of 16, real size = 284 */
81262306a36Sopenharmony_ci	} else {
81362306a36Sopenharmony_ci		itv->vbi.sliced_size = 64;   /* multiple of 16, real size = 52 */
81462306a36Sopenharmony_ci	}
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	/* Find tuner input */
81762306a36Sopenharmony_ci	for (i = 0; i < itv->nof_inputs; i++) {
81862306a36Sopenharmony_ci		if (itv->card->video_inputs[i].video_type ==
81962306a36Sopenharmony_ci				IVTV_CARD_INPUT_VID_TUNER)
82062306a36Sopenharmony_ci			break;
82162306a36Sopenharmony_ci	}
82262306a36Sopenharmony_ci	if (i >= itv->nof_inputs)
82362306a36Sopenharmony_ci		i = 0;
82462306a36Sopenharmony_ci	itv->active_input = i;
82562306a36Sopenharmony_ci	itv->audio_input = itv->card->video_inputs[i].audio_index;
82662306a36Sopenharmony_ci}
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_cistatic int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *pdev,
82962306a36Sopenharmony_ci			  const struct pci_device_id *pci_id)
83062306a36Sopenharmony_ci{
83162306a36Sopenharmony_ci	u16 cmd;
83262306a36Sopenharmony_ci	unsigned char pci_latency;
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_ci	IVTV_DEBUG_INFO("Enabling pci device\n");
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci	if (pci_enable_device(pdev)) {
83762306a36Sopenharmony_ci		IVTV_ERR("Can't enable device!\n");
83862306a36Sopenharmony_ci		return -EIO;
83962306a36Sopenharmony_ci	}
84062306a36Sopenharmony_ci	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
84162306a36Sopenharmony_ci		IVTV_ERR("No suitable DMA available.\n");
84262306a36Sopenharmony_ci		return -EIO;
84362306a36Sopenharmony_ci	}
84462306a36Sopenharmony_ci	if (!request_mem_region(itv->base_addr, IVTV_ENCODER_SIZE, "ivtv encoder")) {
84562306a36Sopenharmony_ci		IVTV_ERR("Cannot request encoder memory region.\n");
84662306a36Sopenharmony_ci		return -EIO;
84762306a36Sopenharmony_ci	}
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci	if (!request_mem_region(itv->base_addr + IVTV_REG_OFFSET,
85062306a36Sopenharmony_ci				IVTV_REG_SIZE, "ivtv registers")) {
85162306a36Sopenharmony_ci		IVTV_ERR("Cannot request register memory region.\n");
85262306a36Sopenharmony_ci		release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
85362306a36Sopenharmony_ci		return -EIO;
85462306a36Sopenharmony_ci	}
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci	if (itv->has_cx23415 &&
85762306a36Sopenharmony_ci	    !request_mem_region(itv->base_addr + IVTV_DECODER_OFFSET,
85862306a36Sopenharmony_ci				IVTV_DECODER_SIZE, "ivtv decoder")) {
85962306a36Sopenharmony_ci		IVTV_ERR("Cannot request decoder memory region.\n");
86062306a36Sopenharmony_ci		release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
86162306a36Sopenharmony_ci		release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
86262306a36Sopenharmony_ci		return -EIO;
86362306a36Sopenharmony_ci	}
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci	/* Check for bus mastering */
86662306a36Sopenharmony_ci	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
86762306a36Sopenharmony_ci	if (!(cmd & PCI_COMMAND_MASTER)) {
86862306a36Sopenharmony_ci		IVTV_DEBUG_INFO("Attempting to enable Bus Mastering\n");
86962306a36Sopenharmony_ci		pci_set_master(pdev);
87062306a36Sopenharmony_ci		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
87162306a36Sopenharmony_ci		if (!(cmd & PCI_COMMAND_MASTER)) {
87262306a36Sopenharmony_ci			IVTV_ERR("Bus Mastering is not enabled\n");
87362306a36Sopenharmony_ci			if (itv->has_cx23415)
87462306a36Sopenharmony_ci				release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET,
87562306a36Sopenharmony_ci						   IVTV_DECODER_SIZE);
87662306a36Sopenharmony_ci			release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
87762306a36Sopenharmony_ci			release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
87862306a36Sopenharmony_ci			return -ENXIO;
87962306a36Sopenharmony_ci		}
88062306a36Sopenharmony_ci	}
88162306a36Sopenharmony_ci	IVTV_DEBUG_INFO("Bus Mastering Enabled.\n");
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_ci	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_ci	if (pci_latency < 64 && ivtv_pci_latency) {
88662306a36Sopenharmony_ci		IVTV_INFO("Unreasonably low latency timer, setting to 64 (was %d)\n",
88762306a36Sopenharmony_ci			  pci_latency);
88862306a36Sopenharmony_ci		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
88962306a36Sopenharmony_ci		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
89062306a36Sopenharmony_ci	}
89162306a36Sopenharmony_ci	/* This config space value relates to DMA latencies. The
89262306a36Sopenharmony_ci	   default value 0x8080 is too low however and will lead
89362306a36Sopenharmony_ci	   to DMA errors. 0xffff is the max value which solves
89462306a36Sopenharmony_ci	   these problems. */
89562306a36Sopenharmony_ci	pci_write_config_dword(pdev, 0x40, 0xffff);
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_ci	IVTV_DEBUG_INFO("%d (rev %d) at %02x:%02x.%x, irq: %d, latency: %d, memory: 0x%llx\n",
89862306a36Sopenharmony_ci		   pdev->device, pdev->revision, pdev->bus->number,
89962306a36Sopenharmony_ci		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
90062306a36Sopenharmony_ci		   pdev->irq, pci_latency, (u64)itv->base_addr);
90162306a36Sopenharmony_ci
90262306a36Sopenharmony_ci	return 0;
90362306a36Sopenharmony_ci}
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_cistatic void ivtv_load_and_init_modules(struct ivtv *itv)
90662306a36Sopenharmony_ci{
90762306a36Sopenharmony_ci	u32 hw = itv->card->hw_all;
90862306a36Sopenharmony_ci	unsigned i;
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci	/* check which i2c devices are actually found */
91162306a36Sopenharmony_ci	for (i = 0; i < 32; i++) {
91262306a36Sopenharmony_ci		u32 device = BIT(i);
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci		if (!(device & hw))
91562306a36Sopenharmony_ci			continue;
91662306a36Sopenharmony_ci		if (device == IVTV_HW_GPIO || device == IVTV_HW_TVEEPROM) {
91762306a36Sopenharmony_ci			/* GPIO and TVEEPROM do not use i2c probing */
91862306a36Sopenharmony_ci			itv->hw_flags |= device;
91962306a36Sopenharmony_ci			continue;
92062306a36Sopenharmony_ci		}
92162306a36Sopenharmony_ci		if (ivtv_i2c_register(itv, i) == 0)
92262306a36Sopenharmony_ci			itv->hw_flags |= device;
92362306a36Sopenharmony_ci	}
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci	/* probe for legacy IR controllers that aren't in card definitions */
92662306a36Sopenharmony_ci	if ((itv->hw_flags & IVTV_HW_IR_ANY) == 0)
92762306a36Sopenharmony_ci		ivtv_i2c_new_ir_legacy(itv);
92862306a36Sopenharmony_ci
92962306a36Sopenharmony_ci	if (itv->card->hw_all & IVTV_HW_CX25840)
93062306a36Sopenharmony_ci		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_CX25840);
93162306a36Sopenharmony_ci	else if (itv->card->hw_all & IVTV_HW_SAA717X)
93262306a36Sopenharmony_ci		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA717X);
93362306a36Sopenharmony_ci	else if (itv->card->hw_all & IVTV_HW_SAA7114)
93462306a36Sopenharmony_ci		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7114);
93562306a36Sopenharmony_ci	else
93662306a36Sopenharmony_ci		itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7115);
93762306a36Sopenharmony_ci	itv->sd_audio = ivtv_find_hw(itv, itv->card->hw_audio_ctrl);
93862306a36Sopenharmony_ci	itv->sd_muxer = ivtv_find_hw(itv, itv->card->hw_muxer);
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	hw = itv->hw_flags;
94162306a36Sopenharmony_ci
94262306a36Sopenharmony_ci	if (itv->card->type == IVTV_CARD_CX23416GYC) {
94362306a36Sopenharmony_ci		/* Several variations of this card exist, detect which card
94462306a36Sopenharmony_ci		   type should be used. */
94562306a36Sopenharmony_ci		if ((hw & (IVTV_HW_UPD64031A | IVTV_HW_UPD6408X)) == 0)
94662306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGRYCS);
94762306a36Sopenharmony_ci		else if ((hw & IVTV_HW_UPD64031A) == 0)
94862306a36Sopenharmony_ci			itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGR);
94962306a36Sopenharmony_ci	}
95062306a36Sopenharmony_ci	else if (itv->card->type == IVTV_CARD_GV_MVPRX ||
95162306a36Sopenharmony_ci		 itv->card->type == IVTV_CARD_GV_MVPRX2E) {
95262306a36Sopenharmony_ci		/* The crystal frequency of GVMVPRX is 24.576MHz */
95362306a36Sopenharmony_ci		v4l2_subdev_call(itv->sd_video, video, s_crystal_freq,
95462306a36Sopenharmony_ci			SAA7115_FREQ_24_576_MHZ, SAA7115_FREQ_FL_UCGC);
95562306a36Sopenharmony_ci	}
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci	if (hw & IVTV_HW_CX25840) {
95862306a36Sopenharmony_ci		itv->vbi.raw_decoder_line_size = 1444;
95962306a36Sopenharmony_ci		itv->vbi.raw_decoder_sav_odd_field = 0x20;
96062306a36Sopenharmony_ci		itv->vbi.raw_decoder_sav_even_field = 0x60;
96162306a36Sopenharmony_ci		itv->vbi.sliced_decoder_line_size = 272;
96262306a36Sopenharmony_ci		itv->vbi.sliced_decoder_sav_odd_field = 0xB0;
96362306a36Sopenharmony_ci		itv->vbi.sliced_decoder_sav_even_field = 0xF0;
96462306a36Sopenharmony_ci	}
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_ci	if (hw & IVTV_HW_SAA711X) {
96762306a36Sopenharmony_ci		/* determine the exact saa711x model */
96862306a36Sopenharmony_ci		itv->hw_flags &= ~IVTV_HW_SAA711X;
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci		if (strstr(itv->sd_video->name, "saa7114")) {
97162306a36Sopenharmony_ci			itv->hw_flags |= IVTV_HW_SAA7114;
97262306a36Sopenharmony_ci			/* VBI is not yet supported by the saa7114 driver. */
97362306a36Sopenharmony_ci			itv->v4l2_cap &= ~(V4L2_CAP_SLICED_VBI_CAPTURE|V4L2_CAP_VBI_CAPTURE);
97462306a36Sopenharmony_ci		} else {
97562306a36Sopenharmony_ci			itv->hw_flags |= IVTV_HW_SAA7115;
97662306a36Sopenharmony_ci		}
97762306a36Sopenharmony_ci		itv->vbi.raw_decoder_line_size = 1443;
97862306a36Sopenharmony_ci		itv->vbi.raw_decoder_sav_odd_field = 0x25;
97962306a36Sopenharmony_ci		itv->vbi.raw_decoder_sav_even_field = 0x62;
98062306a36Sopenharmony_ci		itv->vbi.sliced_decoder_line_size = 51;
98162306a36Sopenharmony_ci		itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
98262306a36Sopenharmony_ci		itv->vbi.sliced_decoder_sav_even_field = 0xEC;
98362306a36Sopenharmony_ci	}
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_ci	if (hw & IVTV_HW_SAA717X) {
98662306a36Sopenharmony_ci		itv->vbi.raw_decoder_line_size = 1443;
98762306a36Sopenharmony_ci		itv->vbi.raw_decoder_sav_odd_field = 0x25;
98862306a36Sopenharmony_ci		itv->vbi.raw_decoder_sav_even_field = 0x62;
98962306a36Sopenharmony_ci		itv->vbi.sliced_decoder_line_size = 51;
99062306a36Sopenharmony_ci		itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
99162306a36Sopenharmony_ci		itv->vbi.sliced_decoder_sav_even_field = 0xEC;
99262306a36Sopenharmony_ci	}
99362306a36Sopenharmony_ci}
99462306a36Sopenharmony_ci
99562306a36Sopenharmony_cistatic int ivtv_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
99662306a36Sopenharmony_ci{
99762306a36Sopenharmony_ci	int retval = 0;
99862306a36Sopenharmony_ci	int vbi_buf_size;
99962306a36Sopenharmony_ci	struct ivtv *itv;
100062306a36Sopenharmony_ci
100162306a36Sopenharmony_ci	itv = kzalloc(sizeof(struct ivtv), GFP_KERNEL);
100262306a36Sopenharmony_ci	if (itv == NULL)
100362306a36Sopenharmony_ci		return -ENOMEM;
100462306a36Sopenharmony_ci	itv->pdev = pdev;
100562306a36Sopenharmony_ci	itv->instance = v4l2_device_set_name(&itv->v4l2_dev, "ivtv",
100662306a36Sopenharmony_ci						&ivtv_instance);
100762306a36Sopenharmony_ci
100862306a36Sopenharmony_ci	retval = v4l2_device_register(&pdev->dev, &itv->v4l2_dev);
100962306a36Sopenharmony_ci	if (retval) {
101062306a36Sopenharmony_ci		kfree(itv);
101162306a36Sopenharmony_ci		return retval;
101262306a36Sopenharmony_ci	}
101362306a36Sopenharmony_ci	IVTV_INFO("Initializing card %d\n", itv->instance);
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci	ivtv_process_options(itv);
101662306a36Sopenharmony_ci	if (itv->options.cardtype == -1) {
101762306a36Sopenharmony_ci		retval = -ENODEV;
101862306a36Sopenharmony_ci		goto err;
101962306a36Sopenharmony_ci	}
102062306a36Sopenharmony_ci	if (ivtv_init_struct1(itv)) {
102162306a36Sopenharmony_ci		retval = -ENOMEM;
102262306a36Sopenharmony_ci		goto err;
102362306a36Sopenharmony_ci	}
102462306a36Sopenharmony_ci	retval = cx2341x_handler_init(&itv->cxhdl, 50);
102562306a36Sopenharmony_ci	if (retval)
102662306a36Sopenharmony_ci		goto err;
102762306a36Sopenharmony_ci	itv->v4l2_dev.ctrl_handler = &itv->cxhdl.hdl;
102862306a36Sopenharmony_ci	itv->cxhdl.ops = &ivtv_cxhdl_ops;
102962306a36Sopenharmony_ci	itv->cxhdl.priv = itv;
103062306a36Sopenharmony_ci	itv->cxhdl.func = ivtv_api_func;
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci	IVTV_DEBUG_INFO("base addr: 0x%llx\n", (u64)itv->base_addr);
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	/* PCI Device Setup */
103562306a36Sopenharmony_ci	retval = ivtv_setup_pci(itv, pdev, pci_id);
103662306a36Sopenharmony_ci	if (retval == -EIO)
103762306a36Sopenharmony_ci		goto free_worker;
103862306a36Sopenharmony_ci	if (retval == -ENXIO)
103962306a36Sopenharmony_ci		goto free_mem;
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	/* map io memory */
104262306a36Sopenharmony_ci	IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
104362306a36Sopenharmony_ci		   (u64)itv->base_addr + IVTV_ENCODER_OFFSET, IVTV_ENCODER_SIZE);
104462306a36Sopenharmony_ci	itv->enc_mem = ioremap(itv->base_addr + IVTV_ENCODER_OFFSET,
104562306a36Sopenharmony_ci				       IVTV_ENCODER_SIZE);
104662306a36Sopenharmony_ci	if (!itv->enc_mem) {
104762306a36Sopenharmony_ci		IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 encoder memory\n");
104862306a36Sopenharmony_ci		IVTV_ERR("Each capture card with a CX23415/6 needs 8 MB of vmalloc address space for this window\n");
104962306a36Sopenharmony_ci		IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
105062306a36Sopenharmony_ci		IVTV_ERR("Use the vmalloc= kernel command line option to set VmallocTotal to a larger value\n");
105162306a36Sopenharmony_ci		retval = -ENOMEM;
105262306a36Sopenharmony_ci		goto free_mem;
105362306a36Sopenharmony_ci	}
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci	if (itv->has_cx23415) {
105662306a36Sopenharmony_ci		IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
105762306a36Sopenharmony_ci				(u64)itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
105862306a36Sopenharmony_ci		itv->dec_mem = ioremap(itv->base_addr + IVTV_DECODER_OFFSET,
105962306a36Sopenharmony_ci				IVTV_DECODER_SIZE);
106062306a36Sopenharmony_ci		if (!itv->dec_mem) {
106162306a36Sopenharmony_ci			IVTV_ERR("ioremap failed. Can't get a window into CX23415 decoder memory\n");
106262306a36Sopenharmony_ci			IVTV_ERR("Each capture card with a CX23415 needs 8 MB of vmalloc address space for this window\n");
106362306a36Sopenharmony_ci			IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
106462306a36Sopenharmony_ci			IVTV_ERR("Use the vmalloc= kernel command line option to set VmallocTotal to a larger value\n");
106562306a36Sopenharmony_ci			retval = -ENOMEM;
106662306a36Sopenharmony_ci			goto free_mem;
106762306a36Sopenharmony_ci		}
106862306a36Sopenharmony_ci	}
106962306a36Sopenharmony_ci	else {
107062306a36Sopenharmony_ci		itv->dec_mem = itv->enc_mem;
107162306a36Sopenharmony_ci	}
107262306a36Sopenharmony_ci
107362306a36Sopenharmony_ci	/* map registers memory */
107462306a36Sopenharmony_ci	IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
107562306a36Sopenharmony_ci		   (u64)itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
107662306a36Sopenharmony_ci	itv->reg_mem =
107762306a36Sopenharmony_ci	    ioremap(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
107862306a36Sopenharmony_ci	if (!itv->reg_mem) {
107962306a36Sopenharmony_ci		IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 register space\n");
108062306a36Sopenharmony_ci		IVTV_ERR("Each capture card with a CX23415/6 needs 64 kB of vmalloc address space for this window\n");
108162306a36Sopenharmony_ci		IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
108262306a36Sopenharmony_ci		IVTV_ERR("Use the vmalloc= kernel command line option to set VmallocTotal to a larger value\n");
108362306a36Sopenharmony_ci		retval = -ENOMEM;
108462306a36Sopenharmony_ci		goto free_io;
108562306a36Sopenharmony_ci	}
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_ci	retval = ivtv_gpio_init(itv);
108862306a36Sopenharmony_ci	if (retval)
108962306a36Sopenharmony_ci		goto free_io;
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ci	/* active i2c  */
109262306a36Sopenharmony_ci	IVTV_DEBUG_INFO("activating i2c...\n");
109362306a36Sopenharmony_ci	if (init_ivtv_i2c(itv)) {
109462306a36Sopenharmony_ci		IVTV_ERR("Could not initialize i2c\n");
109562306a36Sopenharmony_ci		goto free_io;
109662306a36Sopenharmony_ci	}
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_ci	if (itv->card->hw_all & IVTV_HW_TVEEPROM) {
109962306a36Sopenharmony_ci		/* Based on the model number the cardtype may be changed.
110062306a36Sopenharmony_ci		   The PCI IDs are not always reliable. */
110162306a36Sopenharmony_ci		ivtv_process_eeprom(itv);
110262306a36Sopenharmony_ci	}
110362306a36Sopenharmony_ci	if (itv->card->comment)
110462306a36Sopenharmony_ci		IVTV_INFO("%s", itv->card->comment);
110562306a36Sopenharmony_ci	if (itv->card->v4l2_capabilities == 0) {
110662306a36Sopenharmony_ci		/* card was detected but is not supported */
110762306a36Sopenharmony_ci		retval = -ENODEV;
110862306a36Sopenharmony_ci		goto free_i2c;
110962306a36Sopenharmony_ci	}
111062306a36Sopenharmony_ci
111162306a36Sopenharmony_ci	if (itv->std == 0) {
111262306a36Sopenharmony_ci		itv->std = V4L2_STD_NTSC_M;
111362306a36Sopenharmony_ci	}
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci	if (itv->options.tuner == -1) {
111662306a36Sopenharmony_ci		int i;
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci		for (i = 0; i < IVTV_CARD_MAX_TUNERS; i++) {
111962306a36Sopenharmony_ci			if ((itv->std & itv->card->tuners[i].std) == 0)
112062306a36Sopenharmony_ci				continue;
112162306a36Sopenharmony_ci			itv->options.tuner = itv->card->tuners[i].tuner;
112262306a36Sopenharmony_ci			break;
112362306a36Sopenharmony_ci		}
112462306a36Sopenharmony_ci	}
112562306a36Sopenharmony_ci	/* if no tuner was found, then pick the first tuner in the card list */
112662306a36Sopenharmony_ci	if (itv->options.tuner == -1 && itv->card->tuners[0].std) {
112762306a36Sopenharmony_ci		itv->std = itv->card->tuners[0].std;
112862306a36Sopenharmony_ci		if (itv->std & V4L2_STD_PAL)
112962306a36Sopenharmony_ci			itv->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
113062306a36Sopenharmony_ci		else if (itv->std & V4L2_STD_NTSC)
113162306a36Sopenharmony_ci			itv->std = V4L2_STD_NTSC_M;
113262306a36Sopenharmony_ci		else if (itv->std & V4L2_STD_SECAM)
113362306a36Sopenharmony_ci			itv->std = V4L2_STD_SECAM_L;
113462306a36Sopenharmony_ci		itv->options.tuner = itv->card->tuners[0].tuner;
113562306a36Sopenharmony_ci	}
113662306a36Sopenharmony_ci	if (itv->options.radio == -1)
113762306a36Sopenharmony_ci		itv->options.radio = (itv->card->radio_input.audio_type != 0);
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci	/* The card is now fully identified, continue with card-specific
114062306a36Sopenharmony_ci	   initialization. */
114162306a36Sopenharmony_ci	ivtv_init_struct2(itv);
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci	ivtv_load_and_init_modules(itv);
114462306a36Sopenharmony_ci
114562306a36Sopenharmony_ci	if (itv->std & V4L2_STD_525_60) {
114662306a36Sopenharmony_ci		itv->is_60hz = 1;
114762306a36Sopenharmony_ci		itv->is_out_60hz = 1;
114862306a36Sopenharmony_ci	} else {
114962306a36Sopenharmony_ci		itv->is_50hz = 1;
115062306a36Sopenharmony_ci		itv->is_out_50hz = 1;
115162306a36Sopenharmony_ci	}
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci	itv->yuv_info.osd_full_w = 720;
115462306a36Sopenharmony_ci	itv->yuv_info.osd_full_h = itv->is_out_50hz ? 576 : 480;
115562306a36Sopenharmony_ci	itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;
115662306a36Sopenharmony_ci	itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;
115762306a36Sopenharmony_ci
115862306a36Sopenharmony_ci	cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
115962306a36Sopenharmony_ci
116062306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
116162306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
116262306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;
116362306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = 0x10000;
116462306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = 0x08000;
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci	/* Setup VBI Raw Size. Should be big enough to hold PAL.
116762306a36Sopenharmony_ci	   It is possible to switch between PAL and NTSC, so we need to
116862306a36Sopenharmony_ci	   take the largest size here. */
116962306a36Sopenharmony_ci	/* 1456 is multiple of 16, real size = 1444 */
117062306a36Sopenharmony_ci	itv->vbi.raw_size = 1456;
117162306a36Sopenharmony_ci	/* We use a buffer size of 1/2 of the total size needed for a
117262306a36Sopenharmony_ci	   frame. This is actually very useful, since we now receive
117362306a36Sopenharmony_ci	   a field at a time and that makes 'compressing' the raw data
117462306a36Sopenharmony_ci	   down to size by stripping off the SAV codes a lot easier.
117562306a36Sopenharmony_ci	   Note: having two different buffer sizes prevents standard
117662306a36Sopenharmony_ci	   switching on the fly. We need to find a better solution... */
117762306a36Sopenharmony_ci	vbi_buf_size = itv->vbi.raw_size * (itv->is_60hz ? 24 : 36) / 2;
117862306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
117962306a36Sopenharmony_ci	itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_VBI] = sizeof(struct v4l2_sliced_vbi_data) * 36;
118062306a36Sopenharmony_ci
118162306a36Sopenharmony_ci	if (itv->options.radio > 0)
118262306a36Sopenharmony_ci		itv->v4l2_cap |= V4L2_CAP_RADIO;
118362306a36Sopenharmony_ci
118462306a36Sopenharmony_ci	if (itv->options.tuner > -1) {
118562306a36Sopenharmony_ci		struct tuner_setup setup;
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_ci		setup.addr = ADDR_UNSET;
118862306a36Sopenharmony_ci		setup.type = itv->options.tuner;
118962306a36Sopenharmony_ci		setup.mode_mask = T_ANALOG_TV;  /* matches TV tuners */
119062306a36Sopenharmony_ci		if (itv->options.radio > 0)
119162306a36Sopenharmony_ci			setup.mode_mask |= T_RADIO;
119262306a36Sopenharmony_ci		setup.tuner_callback = (setup.type == TUNER_XC2028) ?
119362306a36Sopenharmony_ci			ivtv_reset_tuner_gpio : NULL;
119462306a36Sopenharmony_ci		ivtv_call_all(itv, tuner, s_type_addr, &setup);
119562306a36Sopenharmony_ci		if (setup.type == TUNER_XC2028) {
119662306a36Sopenharmony_ci			static struct xc2028_ctrl ctrl = {
119762306a36Sopenharmony_ci				.fname = XC2028_DEFAULT_FIRMWARE,
119862306a36Sopenharmony_ci				.max_len = 64,
119962306a36Sopenharmony_ci			};
120062306a36Sopenharmony_ci			struct v4l2_priv_tun_config cfg = {
120162306a36Sopenharmony_ci				.tuner = itv->options.tuner,
120262306a36Sopenharmony_ci				.priv = &ctrl,
120362306a36Sopenharmony_ci			};
120462306a36Sopenharmony_ci			ivtv_call_all(itv, tuner, s_config, &cfg);
120562306a36Sopenharmony_ci		}
120662306a36Sopenharmony_ci	}
120762306a36Sopenharmony_ci
120862306a36Sopenharmony_ci	/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
120962306a36Sopenharmony_ci	   are not. */
121062306a36Sopenharmony_ci	itv->tuner_std = itv->std;
121162306a36Sopenharmony_ci
121262306a36Sopenharmony_ci	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
121362306a36Sopenharmony_ci		struct v4l2_ctrl_handler *hdl = itv->v4l2_dev.ctrl_handler;
121462306a36Sopenharmony_ci
121562306a36Sopenharmony_ci		itv->ctrl_pts = v4l2_ctrl_new_std(hdl, &ivtv_hdl_out_ops,
121662306a36Sopenharmony_ci				V4L2_CID_MPEG_VIDEO_DEC_PTS, 0, 0, 0, 0);
121762306a36Sopenharmony_ci		itv->ctrl_frame = v4l2_ctrl_new_std(hdl, &ivtv_hdl_out_ops,
121862306a36Sopenharmony_ci				V4L2_CID_MPEG_VIDEO_DEC_FRAME, 0, 0, 0, 0);
121962306a36Sopenharmony_ci		/* Note: V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO is not supported,
122062306a36Sopenharmony_ci		   mask that menu item. */
122162306a36Sopenharmony_ci		itv->ctrl_audio_playback =
122262306a36Sopenharmony_ci			v4l2_ctrl_new_std_menu(hdl, &ivtv_hdl_out_ops,
122362306a36Sopenharmony_ci				V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK,
122462306a36Sopenharmony_ci				V4L2_MPEG_AUDIO_DEC_PLAYBACK_SWAPPED_STEREO,
122562306a36Sopenharmony_ci				1 << V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO,
122662306a36Sopenharmony_ci				V4L2_MPEG_AUDIO_DEC_PLAYBACK_STEREO);
122762306a36Sopenharmony_ci		itv->ctrl_audio_multilingual_playback =
122862306a36Sopenharmony_ci			v4l2_ctrl_new_std_menu(hdl, &ivtv_hdl_out_ops,
122962306a36Sopenharmony_ci				V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK,
123062306a36Sopenharmony_ci				V4L2_MPEG_AUDIO_DEC_PLAYBACK_SWAPPED_STEREO,
123162306a36Sopenharmony_ci				1 << V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO,
123262306a36Sopenharmony_ci				V4L2_MPEG_AUDIO_DEC_PLAYBACK_LEFT);
123362306a36Sopenharmony_ci		if (hdl->error) {
123462306a36Sopenharmony_ci			retval = hdl->error;
123562306a36Sopenharmony_ci			goto free_i2c;
123662306a36Sopenharmony_ci		}
123762306a36Sopenharmony_ci		v4l2_ctrl_cluster(2, &itv->ctrl_pts);
123862306a36Sopenharmony_ci		v4l2_ctrl_cluster(2, &itv->ctrl_audio_playback);
123962306a36Sopenharmony_ci		ivtv_call_all(itv, video, s_std_output, itv->std);
124062306a36Sopenharmony_ci		/* Turn off the output signal. The mpeg decoder is not yet
124162306a36Sopenharmony_ci		   active so without this you would get a green image until the
124262306a36Sopenharmony_ci		   mpeg decoder becomes active. */
124362306a36Sopenharmony_ci		ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
124462306a36Sopenharmony_ci	}
124562306a36Sopenharmony_ci
124662306a36Sopenharmony_ci	/* clear interrupt mask, effectively disabling interrupts */
124762306a36Sopenharmony_ci	ivtv_set_irq_mask(itv, 0xffffffff);
124862306a36Sopenharmony_ci
124962306a36Sopenharmony_ci	/* Register IRQ */
125062306a36Sopenharmony_ci	retval = request_irq(itv->pdev->irq, ivtv_irq_handler,
125162306a36Sopenharmony_ci	     IRQF_SHARED, itv->v4l2_dev.name, (void *)itv);
125262306a36Sopenharmony_ci	if (retval) {
125362306a36Sopenharmony_ci		IVTV_ERR("Failed to register irq %d\n", retval);
125462306a36Sopenharmony_ci		goto free_i2c;
125562306a36Sopenharmony_ci	}
125662306a36Sopenharmony_ci
125762306a36Sopenharmony_ci	retval = ivtv_streams_setup(itv);
125862306a36Sopenharmony_ci	if (retval) {
125962306a36Sopenharmony_ci		IVTV_ERR("Error %d setting up streams\n", retval);
126062306a36Sopenharmony_ci		goto free_irq;
126162306a36Sopenharmony_ci	}
126262306a36Sopenharmony_ci	retval = ivtv_streams_register(itv);
126362306a36Sopenharmony_ci	if (retval) {
126462306a36Sopenharmony_ci		IVTV_ERR("Error %d registering devices\n", retval);
126562306a36Sopenharmony_ci		goto free_streams;
126662306a36Sopenharmony_ci	}
126762306a36Sopenharmony_ci	IVTV_INFO("Initialized card: %s\n", itv->card_name);
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_ci	/* Load ivtv submodules (ivtv-alsa) */
127062306a36Sopenharmony_ci	request_modules(itv);
127162306a36Sopenharmony_ci	return 0;
127262306a36Sopenharmony_ci
127362306a36Sopenharmony_cifree_streams:
127462306a36Sopenharmony_ci	ivtv_streams_cleanup(itv);
127562306a36Sopenharmony_cifree_irq:
127662306a36Sopenharmony_ci	free_irq(itv->pdev->irq, (void *)itv);
127762306a36Sopenharmony_cifree_i2c:
127862306a36Sopenharmony_ci	v4l2_ctrl_handler_free(&itv->cxhdl.hdl);
127962306a36Sopenharmony_ci	exit_ivtv_i2c(itv);
128062306a36Sopenharmony_cifree_io:
128162306a36Sopenharmony_ci	ivtv_iounmap(itv);
128262306a36Sopenharmony_cifree_mem:
128362306a36Sopenharmony_ci	release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
128462306a36Sopenharmony_ci	release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
128562306a36Sopenharmony_ci	if (itv->has_cx23415)
128662306a36Sopenharmony_ci		release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
128762306a36Sopenharmony_cifree_worker:
128862306a36Sopenharmony_ci	kthread_stop(itv->irq_worker_task);
128962306a36Sopenharmony_cierr:
129062306a36Sopenharmony_ci	if (retval == 0)
129162306a36Sopenharmony_ci		retval = -ENODEV;
129262306a36Sopenharmony_ci	IVTV_ERR("Error %d on initialization\n", retval);
129362306a36Sopenharmony_ci
129462306a36Sopenharmony_ci	v4l2_device_unregister(&itv->v4l2_dev);
129562306a36Sopenharmony_ci	kfree(itv);
129662306a36Sopenharmony_ci	return retval;
129762306a36Sopenharmony_ci}
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_ciint ivtv_init_on_first_open(struct ivtv *itv)
130062306a36Sopenharmony_ci{
130162306a36Sopenharmony_ci	struct v4l2_frequency vf;
130262306a36Sopenharmony_ci	/* Needed to call ioctls later */
130362306a36Sopenharmony_ci	struct ivtv_open_id fh;
130462306a36Sopenharmony_ci	int fw_retry_count = 3;
130562306a36Sopenharmony_ci	int video_input;
130662306a36Sopenharmony_ci
130762306a36Sopenharmony_ci	fh.itv = itv;
130862306a36Sopenharmony_ci	fh.type = IVTV_ENC_STREAM_TYPE_MPG;
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci	if (test_bit(IVTV_F_I_FAILED, &itv->i_flags))
131162306a36Sopenharmony_ci		return -ENXIO;
131262306a36Sopenharmony_ci
131362306a36Sopenharmony_ci	if (test_and_set_bit(IVTV_F_I_INITED, &itv->i_flags))
131462306a36Sopenharmony_ci		return 0;
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci	while (--fw_retry_count > 0) {
131762306a36Sopenharmony_ci		/* load firmware */
131862306a36Sopenharmony_ci		if (ivtv_firmware_init(itv) == 0)
131962306a36Sopenharmony_ci			break;
132062306a36Sopenharmony_ci		if (fw_retry_count > 1)
132162306a36Sopenharmony_ci			IVTV_WARN("Retry loading firmware\n");
132262306a36Sopenharmony_ci	}
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_ci	if (fw_retry_count == 0) {
132562306a36Sopenharmony_ci		set_bit(IVTV_F_I_FAILED, &itv->i_flags);
132662306a36Sopenharmony_ci		return -ENXIO;
132762306a36Sopenharmony_ci	}
132862306a36Sopenharmony_ci
132962306a36Sopenharmony_ci	/* Try and get firmware versions */
133062306a36Sopenharmony_ci	IVTV_DEBUG_INFO("Getting firmware version..\n");
133162306a36Sopenharmony_ci	ivtv_firmware_versions(itv);
133262306a36Sopenharmony_ci
133362306a36Sopenharmony_ci	if (itv->card->hw_all & IVTV_HW_CX25840)
133462306a36Sopenharmony_ci		v4l2_subdev_call(itv->sd_video, core, load_fw);
133562306a36Sopenharmony_ci
133662306a36Sopenharmony_ci	vf.tuner = 0;
133762306a36Sopenharmony_ci	vf.type = V4L2_TUNER_ANALOG_TV;
133862306a36Sopenharmony_ci	vf.frequency = 6400; /* the tuner 'baseline' frequency */
133962306a36Sopenharmony_ci
134062306a36Sopenharmony_ci	/* Set initial frequency. For PAL/SECAM broadcasts no
134162306a36Sopenharmony_ci	   'default' channel exists AFAIK. */
134262306a36Sopenharmony_ci	if (itv->std == V4L2_STD_NTSC_M_JP) {
134362306a36Sopenharmony_ci		vf.frequency = 1460;	/* ch. 1 91250*16/1000 */
134462306a36Sopenharmony_ci	}
134562306a36Sopenharmony_ci	else if (itv->std & V4L2_STD_NTSC_M) {
134662306a36Sopenharmony_ci		vf.frequency = 1076;	/* ch. 4 67250*16/1000 */
134762306a36Sopenharmony_ci	}
134862306a36Sopenharmony_ci
134962306a36Sopenharmony_ci	video_input = itv->active_input;
135062306a36Sopenharmony_ci	itv->active_input++;	/* Force update of input */
135162306a36Sopenharmony_ci	ivtv_s_input(NULL, &fh, video_input);
135262306a36Sopenharmony_ci
135362306a36Sopenharmony_ci	/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
135462306a36Sopenharmony_ci	   in one place. */
135562306a36Sopenharmony_ci	itv->std++;		/* Force full standard initialization */
135662306a36Sopenharmony_ci	itv->std_out = itv->std;
135762306a36Sopenharmony_ci	ivtv_s_frequency(NULL, &fh, &vf);
135862306a36Sopenharmony_ci
135962306a36Sopenharmony_ci	if (itv->card->v4l2_capabilities & V4L2_CAP_VIDEO_OUTPUT) {
136062306a36Sopenharmony_ci		/* Turn on the TV-out: ivtv_init_mpeg_decoder() initializes
136162306a36Sopenharmony_ci		   the mpeg decoder so now the saa7127 receives a proper
136262306a36Sopenharmony_ci		   signal. */
136362306a36Sopenharmony_ci		ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
136462306a36Sopenharmony_ci		ivtv_init_mpeg_decoder(itv);
136562306a36Sopenharmony_ci	}
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci	/* On a cx23416 this seems to be able to enable DMA to the chip? */
136862306a36Sopenharmony_ci	if (!itv->has_cx23415)
136962306a36Sopenharmony_ci		write_reg_sync(0x03, IVTV_REG_DMACONTROL);
137062306a36Sopenharmony_ci
137162306a36Sopenharmony_ci	ivtv_s_std_enc(itv, itv->tuner_std);
137262306a36Sopenharmony_ci
137362306a36Sopenharmony_ci	/* Default interrupts enabled. For the PVR350 this includes the
137462306a36Sopenharmony_ci	   decoder VSYNC interrupt, which is always on. It is not only used
137562306a36Sopenharmony_ci	   during decoding but also by the OSD.
137662306a36Sopenharmony_ci	   Some old PVR250 cards had a cx23415, so testing for that is too
137762306a36Sopenharmony_ci	   general. Instead test if the card has video output capability. */
137862306a36Sopenharmony_ci	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
137962306a36Sopenharmony_ci		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT | IVTV_IRQ_DEC_VSYNC);
138062306a36Sopenharmony_ci		ivtv_set_osd_alpha(itv);
138162306a36Sopenharmony_ci		ivtv_s_std_dec(itv, itv->tuner_std);
138262306a36Sopenharmony_ci	} else {
138362306a36Sopenharmony_ci		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT);
138462306a36Sopenharmony_ci	}
138562306a36Sopenharmony_ci
138662306a36Sopenharmony_ci	/* Setup initial controls */
138762306a36Sopenharmony_ci	cx2341x_handler_setup(&itv->cxhdl);
138862306a36Sopenharmony_ci	return 0;
138962306a36Sopenharmony_ci}
139062306a36Sopenharmony_ci
139162306a36Sopenharmony_cistatic void ivtv_remove(struct pci_dev *pdev)
139262306a36Sopenharmony_ci{
139362306a36Sopenharmony_ci	struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
139462306a36Sopenharmony_ci	struct ivtv *itv = to_ivtv(v4l2_dev);
139562306a36Sopenharmony_ci	int i;
139662306a36Sopenharmony_ci
139762306a36Sopenharmony_ci	IVTV_DEBUG_INFO("Removing card\n");
139862306a36Sopenharmony_ci
139962306a36Sopenharmony_ci	flush_request_modules(itv);
140062306a36Sopenharmony_ci
140162306a36Sopenharmony_ci	if (test_bit(IVTV_F_I_INITED, &itv->i_flags)) {
140262306a36Sopenharmony_ci		/* Stop all captures */
140362306a36Sopenharmony_ci		IVTV_DEBUG_INFO("Stopping all streams\n");
140462306a36Sopenharmony_ci		if (atomic_read(&itv->capturing) > 0)
140562306a36Sopenharmony_ci			ivtv_stop_all_captures(itv);
140662306a36Sopenharmony_ci
140762306a36Sopenharmony_ci		/* Stop all decoding */
140862306a36Sopenharmony_ci		IVTV_DEBUG_INFO("Stopping decoding\n");
140962306a36Sopenharmony_ci
141062306a36Sopenharmony_ci		/* Turn off the TV-out */
141162306a36Sopenharmony_ci		if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
141262306a36Sopenharmony_ci			ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
141362306a36Sopenharmony_ci		if (atomic_read(&itv->decoding) > 0) {
141462306a36Sopenharmony_ci			int type;
141562306a36Sopenharmony_ci
141662306a36Sopenharmony_ci			if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
141762306a36Sopenharmony_ci				type = IVTV_DEC_STREAM_TYPE_YUV;
141862306a36Sopenharmony_ci			else
141962306a36Sopenharmony_ci				type = IVTV_DEC_STREAM_TYPE_MPG;
142062306a36Sopenharmony_ci			ivtv_stop_v4l2_decode_stream(&itv->streams[type],
142162306a36Sopenharmony_ci				V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
142262306a36Sopenharmony_ci		}
142362306a36Sopenharmony_ci		ivtv_halt_firmware(itv);
142462306a36Sopenharmony_ci	}
142562306a36Sopenharmony_ci
142662306a36Sopenharmony_ci	/* Interrupts */
142762306a36Sopenharmony_ci	ivtv_set_irq_mask(itv, 0xffffffff);
142862306a36Sopenharmony_ci	timer_shutdown_sync(&itv->dma_timer);
142962306a36Sopenharmony_ci
143062306a36Sopenharmony_ci	/* Kill irq worker */
143162306a36Sopenharmony_ci	kthread_flush_worker(&itv->irq_worker);
143262306a36Sopenharmony_ci	kthread_stop(itv->irq_worker_task);
143362306a36Sopenharmony_ci
143462306a36Sopenharmony_ci	ivtv_streams_cleanup(itv);
143562306a36Sopenharmony_ci	ivtv_udma_free(itv);
143662306a36Sopenharmony_ci
143762306a36Sopenharmony_ci	v4l2_ctrl_handler_free(&itv->cxhdl.hdl);
143862306a36Sopenharmony_ci
143962306a36Sopenharmony_ci	exit_ivtv_i2c(itv);
144062306a36Sopenharmony_ci
144162306a36Sopenharmony_ci	free_irq(itv->pdev->irq, (void *)itv);
144262306a36Sopenharmony_ci	ivtv_iounmap(itv);
144362306a36Sopenharmony_ci
144462306a36Sopenharmony_ci	release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
144562306a36Sopenharmony_ci	release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
144662306a36Sopenharmony_ci	if (itv->has_cx23415)
144762306a36Sopenharmony_ci		release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
144862306a36Sopenharmony_ci
144962306a36Sopenharmony_ci	pci_disable_device(itv->pdev);
145062306a36Sopenharmony_ci	for (i = 0; i < IVTV_VBI_FRAMES; i++)
145162306a36Sopenharmony_ci		kfree(itv->vbi.sliced_mpeg_data[i]);
145262306a36Sopenharmony_ci
145362306a36Sopenharmony_ci	pr_info("Removed %s\n", itv->card_name);
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci	v4l2_device_unregister(&itv->v4l2_dev);
145662306a36Sopenharmony_ci	kfree(itv);
145762306a36Sopenharmony_ci}
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci/* define a pci_driver for card detection */
146062306a36Sopenharmony_cistatic struct pci_driver ivtv_pci_driver = {
146162306a36Sopenharmony_ci      .name =     "ivtv",
146262306a36Sopenharmony_ci      .id_table = ivtv_pci_tbl,
146362306a36Sopenharmony_ci      .probe =    ivtv_probe,
146462306a36Sopenharmony_ci      .remove =   ivtv_remove,
146562306a36Sopenharmony_ci};
146662306a36Sopenharmony_ci
146762306a36Sopenharmony_cistatic int __init module_start(void)
146862306a36Sopenharmony_ci{
146962306a36Sopenharmony_ci	pr_info("Start initialization, version %s\n", IVTV_VERSION);
147062306a36Sopenharmony_ci
147162306a36Sopenharmony_ci	/* Validate parameters */
147262306a36Sopenharmony_ci	if (ivtv_first_minor < 0 || ivtv_first_minor >= IVTV_MAX_CARDS) {
147362306a36Sopenharmony_ci		pr_err("Exiting, ivtv_first_minor must be between 0 and %d\n",
147462306a36Sopenharmony_ci		     IVTV_MAX_CARDS - 1);
147562306a36Sopenharmony_ci		return -1;
147662306a36Sopenharmony_ci	}
147762306a36Sopenharmony_ci
147862306a36Sopenharmony_ci	if (ivtv_debug < 0 || ivtv_debug > 2047) {
147962306a36Sopenharmony_ci		ivtv_debug = 0;
148062306a36Sopenharmony_ci		pr_info("Debug value must be >= 0 and <= 2047\n");
148162306a36Sopenharmony_ci	}
148262306a36Sopenharmony_ci
148362306a36Sopenharmony_ci	if (pci_register_driver(&ivtv_pci_driver)) {
148462306a36Sopenharmony_ci		pr_err("Error detecting PCI card\n");
148562306a36Sopenharmony_ci		return -ENODEV;
148662306a36Sopenharmony_ci	}
148762306a36Sopenharmony_ci	pr_info("End initialization\n");
148862306a36Sopenharmony_ci	return 0;
148962306a36Sopenharmony_ci}
149062306a36Sopenharmony_ci
149162306a36Sopenharmony_cistatic void __exit module_cleanup(void)
149262306a36Sopenharmony_ci{
149362306a36Sopenharmony_ci	pci_unregister_driver(&ivtv_pci_driver);
149462306a36Sopenharmony_ci}
149562306a36Sopenharmony_ci
149662306a36Sopenharmony_ci/* Note: These symbols are exported because they are used by the ivtvfb
149762306a36Sopenharmony_ci   framebuffer module and an infrared module for the IR-blaster. */
149862306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_set_irq_mask);
149962306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_api);
150062306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_vapi);
150162306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_vapi_result);
150262306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_clear_irq_mask);
150362306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_debug);
150462306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
150562306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_fw_debug);
150662306a36Sopenharmony_ci#endif
150762306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_reset_ir_gpio);
150862306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_udma_setup);
150962306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_udma_unmap);
151062306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_udma_alloc);
151162306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_udma_prepare);
151262306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_init_on_first_open);
151362306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_firmware_check);
151462306a36Sopenharmony_ci
151562306a36Sopenharmony_cimodule_init(module_start);
151662306a36Sopenharmony_cimodule_exit(module_cleanup);
1517