Path: blob/master/drivers/media/video/ivtv/ivtv-driver.c
17736 views
/*1ivtv driver initialization and card probing2Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>3Copyright (C) 2004 Chris Kennedy <[email protected]>4Copyright (C) 2005-2007 Hans Verkuil <[email protected]>56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021/* Main Driver file for the ivtv project:22* Driver for the Conexant CX23415/CX23416 chip.23* Author: Kevin Thayer (nufan_wfk at yahoo.com)24* License: GPL25* http://www.ivtvdriver.org26*27* -----28* MPG600/MPG160 support by T.Adachi <[email protected]>29* and Takeru KOMORIYA<[email protected]>30*31* AVerMedia M179 GPIO info by Chris Pinkham <[email protected]>32* using information provided by Jiun-Kuei Jung @ AVerMedia.33*34* Kurouto Sikou CX23416GYC-STVLP tested by K.Ohta <[email protected]>35* using information from T.Adachi,Takeru KOMORIYA and others :-)36*37* Nagase TRANSGEAR 5000TV, Aopen VA2000MAX-STN6 and I/O data GV-MVP/RX38* version by T.Adachi. Special thanks Mr.Suzuki39*/4041#include "ivtv-driver.h"42#include "ivtv-version.h"43#include "ivtv-fileops.h"44#include "ivtv-i2c.h"45#include "ivtv-firmware.h"46#include "ivtv-queue.h"47#include "ivtv-udma.h"48#include "ivtv-irq.h"49#include "ivtv-mailbox.h"50#include "ivtv-streams.h"51#include "ivtv-ioctl.h"52#include "ivtv-cards.h"53#include "ivtv-vbi.h"54#include "ivtv-routing.h"55#include "ivtv-controls.h"56#include "ivtv-gpio.h"5758#include <media/tveeprom.h>59#include <media/saa7115.h>60#include <media/v4l2-chip-ident.h>61#include "tuner-xc2028.h"6263/* If you have already X v4l cards, then set this to X. This way64the device numbers stay matched. Example: you have a WinTV card65without radio and a PVR-350 with. Normally this would give a66video1 device together with a radio0 device for the PVR. By67setting this to 1 you ensure that radio0 is now also radio1. */68int ivtv_first_minor;6970/* add your revision and whatnot here */71static struct pci_device_id ivtv_pci_tbl[] __devinitdata = {72{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV15,73PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},74{PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV16,75PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},76{0,}77};7879MODULE_DEVICE_TABLE(pci,ivtv_pci_tbl);8081/* ivtv instance counter */82static atomic_t ivtv_instance = ATOMIC_INIT(0);8384/* Parameter declarations */85static int cardtype[IVTV_MAX_CARDS];86static int tuner[IVTV_MAX_CARDS] = { -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-1, -1, -1, -1, -1, -1, -1, -1 };90static int radio[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,91-1, -1, -1, -1, -1, -1, -1, -1,92-1, -1, -1, -1, -1, -1, -1, -1,93-1, -1, -1, -1, -1, -1, -1, -1 };94static int i2c_clock_period[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,95-1, -1, -1, -1, -1, -1, -1, -1,96-1, -1, -1, -1, -1, -1, -1, -1,97-1, -1, -1, -1, -1, -1, -1, -1 };9899static unsigned int cardtype_c = 1;100static unsigned int tuner_c = 1;101static unsigned int radio_c = 1;102static unsigned int i2c_clock_period_c = 1;103static char pal[] = "---";104static char secam[] = "--";105static char ntsc[] = "-";106107/* Buffers */108109/* DMA Buffers, Default size in MB allocated */110#define IVTV_DEFAULT_ENC_MPG_BUFFERS 4111#define IVTV_DEFAULT_ENC_YUV_BUFFERS 2112#define IVTV_DEFAULT_ENC_VBI_BUFFERS 1113/* Exception: size in kB for this stream (MB is overkill) */114#define IVTV_DEFAULT_ENC_PCM_BUFFERS 320115#define IVTV_DEFAULT_DEC_MPG_BUFFERS 1116#define IVTV_DEFAULT_DEC_YUV_BUFFERS 1117/* Exception: size in kB for this stream (MB is way overkill) */118#define IVTV_DEFAULT_DEC_VBI_BUFFERS 64119120static int enc_mpg_buffers = IVTV_DEFAULT_ENC_MPG_BUFFERS;121static int enc_yuv_buffers = IVTV_DEFAULT_ENC_YUV_BUFFERS;122static int enc_vbi_buffers = IVTV_DEFAULT_ENC_VBI_BUFFERS;123static int enc_pcm_buffers = IVTV_DEFAULT_ENC_PCM_BUFFERS;124static int dec_mpg_buffers = IVTV_DEFAULT_DEC_MPG_BUFFERS;125static int dec_yuv_buffers = IVTV_DEFAULT_DEC_YUV_BUFFERS;126static int dec_vbi_buffers = IVTV_DEFAULT_DEC_VBI_BUFFERS;127128static int ivtv_yuv_mode;129static int ivtv_yuv_threshold = -1;130static int ivtv_pci_latency = 1;131132int ivtv_debug;133#ifdef CONFIG_VIDEO_ADV_DEBUG134int ivtv_fw_debug;135#endif136137static int tunertype = -1;138static int newi2c = -1;139140module_param_array(tuner, int, &tuner_c, 0644);141module_param_array(radio, bool, &radio_c, 0644);142module_param_array(cardtype, int, &cardtype_c, 0644);143module_param_string(pal, pal, sizeof(pal), 0644);144module_param_string(secam, secam, sizeof(secam), 0644);145module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);146module_param_named(debug,ivtv_debug, int, 0644);147#ifdef CONFIG_VIDEO_ADV_DEBUG148module_param_named(fw_debug, ivtv_fw_debug, int, 0644);149#endif150module_param(ivtv_pci_latency, int, 0644);151module_param(ivtv_yuv_mode, int, 0644);152module_param(ivtv_yuv_threshold, int, 0644);153module_param(ivtv_first_minor, int, 0644);154155module_param(enc_mpg_buffers, int, 0644);156module_param(enc_yuv_buffers, int, 0644);157module_param(enc_vbi_buffers, int, 0644);158module_param(enc_pcm_buffers, int, 0644);159module_param(dec_mpg_buffers, int, 0644);160module_param(dec_yuv_buffers, int, 0644);161module_param(dec_vbi_buffers, int, 0644);162163module_param(tunertype, int, 0644);164module_param(newi2c, int, 0644);165module_param_array(i2c_clock_period, int, &i2c_clock_period_c, 0644);166167MODULE_PARM_DESC(tuner, "Tuner type selection,\n"168"\t\t\tsee tuner.h for values");169MODULE_PARM_DESC(radio,170"Enable or disable the radio. Use only if autodetection\n"171"\t\t\tfails. 0 = disable, 1 = enable");172MODULE_PARM_DESC(cardtype,173"Only use this option if your card is not detected properly.\n"174"\t\tSpecify card type:\n"175"\t\t\t 1 = WinTV PVR 250\n"176"\t\t\t 2 = WinTV PVR 350\n"177"\t\t\t 3 = WinTV PVR-150 or PVR-500\n"178"\t\t\t 4 = AVerMedia M179\n"179"\t\t\t 5 = YUAN MPG600/Kuroutoshikou iTVC16-STVLP\n"180"\t\t\t 6 = YUAN MPG160/Kuroutoshikou iTVC15-STVLP\n"181"\t\t\t 7 = YUAN PG600/DIAMONDMM PVR-550 (CX Falcon 2)\n"182"\t\t\t 8 = Adaptec AVC-2410\n"183"\t\t\t 9 = Adaptec AVC-2010\n"184"\t\t\t10 = NAGASE TRANSGEAR 5000TV\n"185"\t\t\t11 = AOpen VA2000MAX-STN6\n"186"\t\t\t12 = YUAN MPG600GR/Kuroutoshikou CX23416GYC-STVLP\n"187"\t\t\t13 = I/O Data GV-MVP/RX\n"188"\t\t\t14 = I/O Data GV-MVP/RX2E\n"189"\t\t\t15 = GOTVIEW PCI DVD\n"190"\t\t\t16 = GOTVIEW PCI DVD2 Deluxe\n"191"\t\t\t17 = Yuan MPC622\n"192"\t\t\t18 = Digital Cowboy DCT-MTVP1\n"193"\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"194"\t\t\t20 = Club3D ZAP-TV1x01\n"195"\t\t\t21 = AverTV MCE 116 Plus\n"196"\t\t\t22 = ASUS Falcon2\n"197"\t\t\t23 = AverMedia PVR-150 Plus\n"198"\t\t\t24 = AverMedia EZMaker PCI Deluxe\n"199"\t\t\t25 = AverMedia M104 (not yet working)\n"200"\t\t\t26 = Buffalo PC-MV5L/PCI\n"201"\t\t\t27 = AVerMedia UltraTV 1500 MCE\n"202"\t\t\t28 = Sony VAIO Giga Pocket (ENX Kikyou)\n"203"\t\t\t 0 = Autodetect (default)\n"204"\t\t\t-1 = Ignore this card\n\t\t");205MODULE_PARM_DESC(pal, "Set PAL standard: BGH, DK, I, M, N, Nc, 60");206MODULE_PARM_DESC(secam, "Set SECAM standard: BGH, DK, L, LC");207MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J (Japan), K (South Korea)");208MODULE_PARM_DESC(tunertype,209"Specify tuner type:\n"210"\t\t\t 0 = tuner for PAL-B/G/H/D/K/I, SECAM-B/G/H/D/K/L/Lc\n"211"\t\t\t 1 = tuner for NTSC-M/J/K, PAL-M/N/Nc\n"212"\t\t\t-1 = Autodetect (default)\n");213MODULE_PARM_DESC(debug,214"Debug level (bitmask). Default: 0\n"215"\t\t\t 1/0x0001: warning\n"216"\t\t\t 2/0x0002: info\n"217"\t\t\t 4/0x0004: mailbox\n"218"\t\t\t 8/0x0008: ioctl\n"219"\t\t\t 16/0x0010: file\n"220"\t\t\t 32/0x0020: dma\n"221"\t\t\t 64/0x0040: irq\n"222"\t\t\t 128/0x0080: decoder\n"223"\t\t\t 256/0x0100: yuv\n"224"\t\t\t 512/0x0200: i2c\n"225"\t\t\t1024/0x0400: high volume\n");226#ifdef CONFIG_VIDEO_ADV_DEBUG227MODULE_PARM_DESC(fw_debug,228"Enable code for debugging firmware problems. Default: 0\n");229#endif230MODULE_PARM_DESC(ivtv_pci_latency,231"Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"232"\t\t\tDefault: Yes");233MODULE_PARM_DESC(ivtv_yuv_mode,234"Specify the yuv playback mode:\n"235"\t\t\t0 = interlaced\n\t\t\t1 = progressive\n\t\t\t2 = auto\n"236"\t\t\tDefault: 0 (interlaced)");237MODULE_PARM_DESC(ivtv_yuv_threshold,238"If ivtv_yuv_mode is 2 (auto) then playback content as\n\t\tprogressive if src height <= ivtv_yuvthreshold\n"239"\t\t\tDefault: 480");240MODULE_PARM_DESC(enc_mpg_buffers,241"Encoder MPG Buffers (in MB)\n"242"\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_MPG_BUFFERS));243MODULE_PARM_DESC(enc_yuv_buffers,244"Encoder YUV Buffers (in MB)\n"245"\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_YUV_BUFFERS));246MODULE_PARM_DESC(enc_vbi_buffers,247"Encoder VBI Buffers (in MB)\n"248"\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_VBI_BUFFERS));249MODULE_PARM_DESC(enc_pcm_buffers,250"Encoder PCM buffers (in kB)\n"251"\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_PCM_BUFFERS));252MODULE_PARM_DESC(dec_mpg_buffers,253"Decoder MPG buffers (in MB)\n"254"\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_MPG_BUFFERS));255MODULE_PARM_DESC(dec_yuv_buffers,256"Decoder YUV buffers (in MB)\n"257"\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_YUV_BUFFERS));258MODULE_PARM_DESC(dec_vbi_buffers,259"Decoder VBI buffers (in kB)\n"260"\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_VBI_BUFFERS));261MODULE_PARM_DESC(newi2c,262"Use new I2C implementation\n"263"\t\t\t-1 is autodetect, 0 is off, 1 is on\n"264"\t\t\tDefault is autodetect");265MODULE_PARM_DESC(i2c_clock_period,266"Period of SCL for the I2C bus controlled by the CX23415/6\n"267"\t\t\tMin: 10 usec (100 kHz), Max: 4500 usec (222 Hz)\n"268"\t\t\tDefault: " __stringify(IVTV_DEFAULT_I2C_CLOCK_PERIOD));269270MODULE_PARM_DESC(ivtv_first_minor, "Set device node number assigned to first card");271272MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");273MODULE_DESCRIPTION("CX23415/CX23416 driver");274MODULE_SUPPORTED_DEVICE275("CX23415/CX23416 MPEG2 encoder (WinTV PVR-150/250/350/500,\n"276"\t\t\tYuan MPG series and similar)");277MODULE_LICENSE("GPL");278279MODULE_VERSION(IVTV_VERSION);280281void ivtv_clear_irq_mask(struct ivtv *itv, u32 mask)282{283itv->irqmask &= ~mask;284write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);285}286287void ivtv_set_irq_mask(struct ivtv *itv, u32 mask)288{289itv->irqmask |= mask;290write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);291}292293int ivtv_set_output_mode(struct ivtv *itv, int mode)294{295int old_mode;296297spin_lock(&itv->lock);298old_mode = itv->output_mode;299if (old_mode == 0)300itv->output_mode = old_mode = mode;301spin_unlock(&itv->lock);302return old_mode;303}304305struct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv)306{307switch (itv->output_mode) {308case OUT_MPG:309return &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];310case OUT_YUV:311return &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];312default:313return NULL;314}315}316317int ivtv_waitq(wait_queue_head_t *waitq)318{319DEFINE_WAIT(wait);320321prepare_to_wait(waitq, &wait, TASK_INTERRUPTIBLE);322schedule();323finish_wait(waitq, &wait);324return signal_pending(current) ? -EINTR : 0;325}326327/* Generic utility functions */328int ivtv_msleep_timeout(unsigned int msecs, int intr)329{330int timeout = msecs_to_jiffies(msecs);331332do {333set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);334timeout = schedule_timeout(timeout);335if (intr) {336int ret = signal_pending(current);337338if (ret)339return ret;340}341} while (timeout);342return 0;343}344345/* Release ioremapped memory */346static void ivtv_iounmap(struct ivtv *itv)347{348if (itv == NULL)349return;350351/* Release registers memory */352if (itv->reg_mem != NULL) {353IVTV_DEBUG_INFO("releasing reg_mem\n");354iounmap(itv->reg_mem);355itv->reg_mem = NULL;356}357/* Release io memory */358if (itv->has_cx23415 && itv->dec_mem != NULL) {359IVTV_DEBUG_INFO("releasing dec_mem\n");360iounmap(itv->dec_mem);361}362itv->dec_mem = NULL;363364/* Release io memory */365if (itv->enc_mem != NULL) {366IVTV_DEBUG_INFO("releasing enc_mem\n");367iounmap(itv->enc_mem);368itv->enc_mem = NULL;369}370}371372/* Hauppauge card? get values from tveeprom */373void ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv)374{375u8 eedata[256];376377itv->i2c_client.addr = 0xA0 >> 1;378tveeprom_read(&itv->i2c_client, eedata, sizeof(eedata));379tveeprom_hauppauge_analog(&itv->i2c_client, tv, eedata);380}381382static void ivtv_process_eeprom(struct ivtv *itv)383{384struct tveeprom tv;385int pci_slot = PCI_SLOT(itv->pdev->devfn);386387ivtv_read_eeprom(itv, &tv);388389/* Many thanks to Steven Toth from Hauppauge for providing the390model numbers */391switch (tv.model) {392/* In a few cases the PCI subsystem IDs do not correctly393identify the card. A better method is to check the394model number from the eeprom instead. */395case 30012 ... 30039: /* Low profile PVR250 */396case 32000 ... 32999:397case 48000 ... 48099: /* 48??? range are PVR250s with a cx23415 */398case 48400 ... 48599:399itv->card = ivtv_get_card(IVTV_CARD_PVR_250);400break;401case 48100 ... 48399:402case 48600 ... 48999:403itv->card = ivtv_get_card(IVTV_CARD_PVR_350);404break;405case 23000 ... 23999: /* PVR500 */406case 25000 ... 25999: /* Low profile PVR150 */407case 26000 ... 26999: /* Regular PVR150 */408itv->card = ivtv_get_card(IVTV_CARD_PVR_150);409break;410case 0:411IVTV_ERR("Invalid EEPROM\n");412return;413default:414IVTV_ERR("Unknown model %d, defaulting to PVR-150\n", tv.model);415itv->card = ivtv_get_card(IVTV_CARD_PVR_150);416break;417}418419switch (tv.model) {420/* Old style PVR350 (with an saa7114) uses this input for421the tuner. */422case 48254:423itv->card = ivtv_get_card(IVTV_CARD_PVR_350_V1);424break;425default:426break;427}428429itv->v4l2_cap = itv->card->v4l2_capabilities;430itv->card_name = itv->card->name;431itv->card_i2c = itv->card->i2c;432433/* If this is a PVR500 then it should be possible to detect whether it is the434first or second unit by looking at the subsystem device ID: is bit 4 is435set, then it is the second unit (according to info from Hauppauge).436437However, while this works for most cards, I have seen a few PVR500 cards438where both units have the same subsystem ID.439440So instead I look at the reported 'PCI slot' (which is the slot on the PVR500441PCI bridge) and if it is 8, then it is assumed to be the first unit, otherwise442it is the second unit. It is possible that it is a different slot when ivtv is443used in Xen, in that case I ignore this card here. The worst that can happen444is that the card presents itself with a non-working radio device.445446This detection is needed since the eeprom reports incorrectly that a radio is447present on the second unit. */448if (tv.model / 1000 == 23) {449static const struct ivtv_card_tuner_i2c ivtv_i2c_radio = {450.radio = { 0x60, I2C_CLIENT_END },451.demod = { 0x43, I2C_CLIENT_END },452.tv = { 0x61, I2C_CLIENT_END },453};454455itv->card_name = "WinTV PVR 500";456itv->card_i2c = &ivtv_i2c_radio;457if (pci_slot == 8 || pci_slot == 9) {458int is_first = (pci_slot & 1) == 0;459460itv->card_name = is_first ? "WinTV PVR 500 (unit #1)" :461"WinTV PVR 500 (unit #2)";462if (!is_first) {463IVTV_INFO("Correcting tveeprom data: no radio present on second unit\n");464tv.has_radio = 0;465}466}467}468IVTV_INFO("Autodetected %s\n", itv->card_name);469470switch (tv.tuner_hauppauge_model) {471case 85:472case 99:473case 112:474itv->pvr150_workaround = 1;475break;476default:477break;478}479if (tv.tuner_type == TUNER_ABSENT)480IVTV_ERR("tveeprom cannot autodetect tuner!\n");481482if (itv->options.tuner == -1)483itv->options.tuner = tv.tuner_type;484if (itv->options.radio == -1)485itv->options.radio = (tv.has_radio != 0);486/* only enable newi2c if an IR blaster is present */487if (itv->options.newi2c == -1 && tv.has_ir) {488itv->options.newi2c = (tv.has_ir & 4) ? 1 : 0;489if (itv->options.newi2c) {490IVTV_INFO("Reopen i2c bus for IR-blaster support\n");491exit_ivtv_i2c(itv);492init_ivtv_i2c(itv);493}494}495496if (itv->std != 0)497/* user specified tuner standard */498return;499500/* autodetect tuner standard */501if (tv.tuner_formats & V4L2_STD_PAL) {502IVTV_DEBUG_INFO("PAL tuner detected\n");503itv->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;504} else if (tv.tuner_formats & V4L2_STD_NTSC) {505IVTV_DEBUG_INFO("NTSC tuner detected\n");506itv->std |= V4L2_STD_NTSC_M;507} else if (tv.tuner_formats & V4L2_STD_SECAM) {508IVTV_DEBUG_INFO("SECAM tuner detected\n");509itv->std |= V4L2_STD_SECAM_L;510} else {511IVTV_INFO("No tuner detected, default to NTSC-M\n");512itv->std |= V4L2_STD_NTSC_M;513}514}515516static v4l2_std_id ivtv_parse_std(struct ivtv *itv)517{518switch (pal[0]) {519case '6':520tunertype = 0;521return V4L2_STD_PAL_60;522case 'b':523case 'B':524case 'g':525case 'G':526case 'h':527case 'H':528tunertype = 0;529return V4L2_STD_PAL_BG | V4L2_STD_PAL_H;530case 'n':531case 'N':532tunertype = 1;533if (pal[1] == 'c' || pal[1] == 'C')534return V4L2_STD_PAL_Nc;535return V4L2_STD_PAL_N;536case 'i':537case 'I':538tunertype = 0;539return V4L2_STD_PAL_I;540case 'd':541case 'D':542case 'k':543case 'K':544tunertype = 0;545return V4L2_STD_PAL_DK;546case 'M':547case 'm':548tunertype = 1;549return V4L2_STD_PAL_M;550case '-':551break;552default:553IVTV_WARN("pal= argument not recognised\n");554return 0;555}556557switch (secam[0]) {558case 'b':559case 'B':560case 'g':561case 'G':562case 'h':563case 'H':564tunertype = 0;565return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;566case 'd':567case 'D':568case 'k':569case 'K':570tunertype = 0;571return V4L2_STD_SECAM_DK;572case 'l':573case 'L':574tunertype = 0;575if (secam[1] == 'C' || secam[1] == 'c')576return V4L2_STD_SECAM_LC;577return V4L2_STD_SECAM_L;578case '-':579break;580default:581IVTV_WARN("secam= argument not recognised\n");582return 0;583}584585switch (ntsc[0]) {586case 'm':587case 'M':588tunertype = 1;589return V4L2_STD_NTSC_M;590case 'j':591case 'J':592tunertype = 1;593return V4L2_STD_NTSC_M_JP;594case 'k':595case 'K':596tunertype = 1;597return V4L2_STD_NTSC_M_KR;598case '-':599break;600default:601IVTV_WARN("ntsc= argument not recognised\n");602return 0;603}604605/* no match found */606return 0;607}608609static void ivtv_process_options(struct ivtv *itv)610{611const char *chipname;612int i, j;613614itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers * 1024;615itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers * 1024;616itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers * 1024;617itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;618itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_MPG] = dec_mpg_buffers * 1024;619itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_YUV] = dec_yuv_buffers * 1024;620itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_VBI] = dec_vbi_buffers;621itv->options.cardtype = cardtype[itv->instance];622itv->options.tuner = tuner[itv->instance];623itv->options.radio = radio[itv->instance];624625itv->options.i2c_clock_period = i2c_clock_period[itv->instance];626if (itv->options.i2c_clock_period == -1)627itv->options.i2c_clock_period = IVTV_DEFAULT_I2C_CLOCK_PERIOD;628else if (itv->options.i2c_clock_period < 10)629itv->options.i2c_clock_period = 10;630else if (itv->options.i2c_clock_period > 4500)631itv->options.i2c_clock_period = 4500;632633itv->options.newi2c = newi2c;634if (tunertype < -1 || tunertype > 1) {635IVTV_WARN("Invalid tunertype argument, will autodetect instead\n");636tunertype = -1;637}638itv->std = ivtv_parse_std(itv);639if (itv->std == 0 && tunertype >= 0)640itv->std = tunertype ? V4L2_STD_MN : (V4L2_STD_ALL & ~V4L2_STD_MN);641itv->has_cx23415 = (itv->pdev->device == PCI_DEVICE_ID_IVTV15);642chipname = itv->has_cx23415 ? "cx23415" : "cx23416";643if (itv->options.cardtype == -1) {644IVTV_INFO("Ignore card (detected %s based chip)\n", chipname);645return;646}647if ((itv->card = ivtv_get_card(itv->options.cardtype - 1))) {648IVTV_INFO("User specified %s card (detected %s based chip)\n",649itv->card->name, chipname);650} else if (itv->options.cardtype != 0) {651IVTV_ERR("Unknown user specified type, trying to autodetect card\n");652}653if (itv->card == NULL) {654if (itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE ||655itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT1 ||656itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT2) {657itv->card = ivtv_get_card(itv->has_cx23415 ? IVTV_CARD_PVR_350 : IVTV_CARD_PVR_150);658IVTV_INFO("Autodetected Hauppauge card (%s based)\n",659chipname);660}661}662if (itv->card == NULL) {663for (i = 0; (itv->card = ivtv_get_card(i)); i++) {664if (itv->card->pci_list == NULL)665continue;666for (j = 0; itv->card->pci_list[j].device; j++) {667if (itv->pdev->device !=668itv->card->pci_list[j].device)669continue;670if (itv->pdev->subsystem_vendor !=671itv->card->pci_list[j].subsystem_vendor)672continue;673if (itv->pdev->subsystem_device !=674itv->card->pci_list[j].subsystem_device)675continue;676IVTV_INFO("Autodetected %s card (%s based)\n",677itv->card->name, chipname);678goto done;679}680}681}682done:683684if (itv->card == NULL) {685itv->card = ivtv_get_card(IVTV_CARD_PVR_150);686IVTV_ERR("Unknown card: vendor/device: [%04x:%04x]\n",687itv->pdev->vendor, itv->pdev->device);688IVTV_ERR(" subsystem vendor/device: [%04x:%04x]\n",689itv->pdev->subsystem_vendor, itv->pdev->subsystem_device);690IVTV_ERR(" %s based\n", chipname);691IVTV_ERR("Defaulting to %s card\n", itv->card->name);692IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");693IVTV_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");694IVTV_ERR("Prefix your subject line with [UNKNOWN IVTV CARD].\n");695}696itv->v4l2_cap = itv->card->v4l2_capabilities;697itv->card_name = itv->card->name;698itv->card_i2c = itv->card->i2c;699}700701/* Precondition: the ivtv structure has been memset to 0. Only702the dev and num fields have been filled in.703No assumptions on the card type may be made here (see ivtv_init_struct2704for that).705*/706static int __devinit ivtv_init_struct1(struct ivtv *itv)707{708struct sched_param param = { .sched_priority = 99 };709710itv->base_addr = pci_resource_start(itv->pdev, 0);711itv->enc_mbox.max_mbox = 2; /* the encoder has 3 mailboxes (0-2) */712itv->dec_mbox.max_mbox = 1; /* the decoder has 2 mailboxes (0-1) */713714mutex_init(&itv->serialize_lock);715mutex_init(&itv->i2c_bus_lock);716mutex_init(&itv->udma.lock);717718spin_lock_init(&itv->lock);719spin_lock_init(&itv->dma_reg_lock);720721init_kthread_worker(&itv->irq_worker);722itv->irq_worker_task = kthread_run(kthread_worker_fn, &itv->irq_worker,723itv->v4l2_dev.name);724if (IS_ERR(itv->irq_worker_task)) {725IVTV_ERR("Could not create ivtv task\n");726return -1;727}728/* must use the FIFO scheduler as it is realtime sensitive */729sched_setscheduler(itv->irq_worker_task, SCHED_FIFO, ¶m);730731init_kthread_work(&itv->irq_work, ivtv_irq_work_handler);732733/* start counting open_id at 1 */734itv->open_id = 1;735736/* Initial settings */737itv->cxhdl.port = CX2341X_PORT_MEMORY;738itv->cxhdl.capabilities = CX2341X_CAP_HAS_SLICED_VBI;739init_waitqueue_head(&itv->eos_waitq);740init_waitqueue_head(&itv->event_waitq);741init_waitqueue_head(&itv->vsync_waitq);742init_waitqueue_head(&itv->dma_waitq);743init_timer(&itv->dma_timer);744itv->dma_timer.function = ivtv_unfinished_dma;745itv->dma_timer.data = (unsigned long)itv;746747itv->cur_dma_stream = -1;748itv->cur_pio_stream = -1;749itv->audio_stereo_mode = AUDIO_STEREO;750itv->audio_bilingual_mode = AUDIO_MONO_LEFT;751752/* Ctrls */753itv->speed = 1000;754755/* VBI */756itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;757itv->vbi.sliced_in = &itv->vbi.in.fmt.sliced;758759/* Init the sg table for osd/yuv output */760sg_init_table(itv->udma.SGlist, IVTV_DMA_SG_OSD_ENT);761762/* OSD */763itv->osd_global_alpha_state = 1;764itv->osd_global_alpha = 255;765766/* YUV */767atomic_set(&itv->yuv_info.next_dma_frame, -1);768itv->yuv_info.lace_mode = ivtv_yuv_mode;769itv->yuv_info.lace_threshold = ivtv_yuv_threshold;770itv->yuv_info.max_frames_buffered = 3;771itv->yuv_info.track_osd = 1;772return 0;773}774775/* Second initialization part. Here the card type has been776autodetected. */777static void __devinit ivtv_init_struct2(struct ivtv *itv)778{779int i;780781for (i = 0; i < IVTV_CARD_MAX_VIDEO_INPUTS; i++)782if (itv->card->video_inputs[i].video_type == 0)783break;784itv->nof_inputs = i;785for (i = 0; i < IVTV_CARD_MAX_AUDIO_INPUTS; i++)786if (itv->card->audio_inputs[i].audio_type == 0)787break;788itv->nof_audio_inputs = i;789790if (itv->card->hw_all & IVTV_HW_CX25840) {791itv->vbi.sliced_size = 288; /* multiple of 16, real size = 284 */792} else {793itv->vbi.sliced_size = 64; /* multiple of 16, real size = 52 */794}795796/* Find tuner input */797for (i = 0; i < itv->nof_inputs; i++) {798if (itv->card->video_inputs[i].video_type ==799IVTV_CARD_INPUT_VID_TUNER)800break;801}802if (i == itv->nof_inputs)803i = 0;804itv->active_input = i;805itv->audio_input = itv->card->video_inputs[i].audio_index;806}807808static int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *pdev,809const struct pci_device_id *pci_id)810{811u16 cmd;812unsigned char pci_latency;813814IVTV_DEBUG_INFO("Enabling pci device\n");815816if (pci_enable_device(pdev)) {817IVTV_ERR("Can't enable device!\n");818return -EIO;819}820if (pci_set_dma_mask(pdev, 0xffffffff)) {821IVTV_ERR("No suitable DMA available.\n");822return -EIO;823}824if (!request_mem_region(itv->base_addr, IVTV_ENCODER_SIZE, "ivtv encoder")) {825IVTV_ERR("Cannot request encoder memory region.\n");826return -EIO;827}828829if (!request_mem_region(itv->base_addr + IVTV_REG_OFFSET,830IVTV_REG_SIZE, "ivtv registers")) {831IVTV_ERR("Cannot request register memory region.\n");832release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);833return -EIO;834}835836if (itv->has_cx23415 &&837!request_mem_region(itv->base_addr + IVTV_DECODER_OFFSET,838IVTV_DECODER_SIZE, "ivtv decoder")) {839IVTV_ERR("Cannot request decoder memory region.\n");840release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);841release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);842return -EIO;843}844845/* Check for bus mastering */846pci_read_config_word(pdev, PCI_COMMAND, &cmd);847if (!(cmd & PCI_COMMAND_MASTER)) {848IVTV_DEBUG_INFO("Attempting to enable Bus Mastering\n");849pci_set_master(pdev);850pci_read_config_word(pdev, PCI_COMMAND, &cmd);851if (!(cmd & PCI_COMMAND_MASTER)) {852IVTV_ERR("Bus Mastering is not enabled\n");853return -ENXIO;854}855}856IVTV_DEBUG_INFO("Bus Mastering Enabled.\n");857858pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);859860if (pci_latency < 64 && ivtv_pci_latency) {861IVTV_INFO("Unreasonably low latency timer, "862"setting to 64 (was %d)\n", pci_latency);863pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);864pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);865}866/* This config space value relates to DMA latencies. The867default value 0x8080 is too low however and will lead868to DMA errors. 0xffff is the max value which solves869these problems. */870pci_write_config_dword(pdev, 0x40, 0xffff);871872IVTV_DEBUG_INFO("%d (rev %d) at %02x:%02x.%x, "873"irq: %d, latency: %d, memory: 0x%lx\n",874pdev->device, pdev->revision, pdev->bus->number,875PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),876pdev->irq, pci_latency, (unsigned long)itv->base_addr);877878return 0;879}880881static void ivtv_load_and_init_modules(struct ivtv *itv)882{883u32 hw = itv->card->hw_all;884unsigned i;885886/* check which i2c devices are actually found */887for (i = 0; i < 32; i++) {888u32 device = 1 << i;889890if (!(device & hw))891continue;892if (device == IVTV_HW_GPIO || device == IVTV_HW_TVEEPROM) {893/* GPIO and TVEEPROM do not use i2c probing */894itv->hw_flags |= device;895continue;896}897if (ivtv_i2c_register(itv, i) == 0)898itv->hw_flags |= device;899}900901/* probe for legacy IR controllers that aren't in card definitions */902if ((itv->hw_flags & IVTV_HW_IR_ANY) == 0)903ivtv_i2c_new_ir_legacy(itv);904905if (itv->card->hw_all & IVTV_HW_CX25840)906itv->sd_video = ivtv_find_hw(itv, IVTV_HW_CX25840);907else if (itv->card->hw_all & IVTV_HW_SAA717X)908itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA717X);909else if (itv->card->hw_all & IVTV_HW_SAA7114)910itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7114);911else912itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7115);913itv->sd_audio = ivtv_find_hw(itv, itv->card->hw_audio_ctrl);914itv->sd_muxer = ivtv_find_hw(itv, itv->card->hw_muxer);915916hw = itv->hw_flags;917918if (itv->card->type == IVTV_CARD_CX23416GYC) {919/* Several variations of this card exist, detect which card920type should be used. */921if ((hw & (IVTV_HW_UPD64031A | IVTV_HW_UPD6408X)) == 0)922itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGRYCS);923else if ((hw & IVTV_HW_UPD64031A) == 0)924itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGR);925}926else if (itv->card->type == IVTV_CARD_GV_MVPRX ||927itv->card->type == IVTV_CARD_GV_MVPRX2E) {928/* The crystal frequency of GVMVPRX is 24.576MHz */929v4l2_subdev_call(itv->sd_video, video, s_crystal_freq,930SAA7115_FREQ_24_576_MHZ, SAA7115_FREQ_FL_UCGC);931}932933if (hw & IVTV_HW_CX25840) {934itv->vbi.raw_decoder_line_size = 1444;935itv->vbi.raw_decoder_sav_odd_field = 0x20;936itv->vbi.raw_decoder_sav_even_field = 0x60;937itv->vbi.sliced_decoder_line_size = 272;938itv->vbi.sliced_decoder_sav_odd_field = 0xB0;939itv->vbi.sliced_decoder_sav_even_field = 0xF0;940}941942if (hw & IVTV_HW_SAA711X) {943struct v4l2_dbg_chip_ident v;944945/* determine the exact saa711x model */946itv->hw_flags &= ~IVTV_HW_SAA711X;947948v.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;949strlcpy(v.match.name, "saa7115", sizeof(v.match.name));950ivtv_call_hw(itv, IVTV_HW_SAA711X, core, g_chip_ident, &v);951if (v.ident == V4L2_IDENT_SAA7114) {952itv->hw_flags |= IVTV_HW_SAA7114;953/* VBI is not yet supported by the saa7114 driver. */954itv->v4l2_cap &= ~(V4L2_CAP_SLICED_VBI_CAPTURE|V4L2_CAP_VBI_CAPTURE);955} else {956itv->hw_flags |= IVTV_HW_SAA7115;957}958itv->vbi.raw_decoder_line_size = 1443;959itv->vbi.raw_decoder_sav_odd_field = 0x25;960itv->vbi.raw_decoder_sav_even_field = 0x62;961itv->vbi.sliced_decoder_line_size = 51;962itv->vbi.sliced_decoder_sav_odd_field = 0xAB;963itv->vbi.sliced_decoder_sav_even_field = 0xEC;964}965966if (hw & IVTV_HW_SAA717X) {967itv->vbi.raw_decoder_line_size = 1443;968itv->vbi.raw_decoder_sav_odd_field = 0x25;969itv->vbi.raw_decoder_sav_even_field = 0x62;970itv->vbi.sliced_decoder_line_size = 51;971itv->vbi.sliced_decoder_sav_odd_field = 0xAB;972itv->vbi.sliced_decoder_sav_even_field = 0xEC;973}974}975976static int __devinit ivtv_probe(struct pci_dev *pdev,977const struct pci_device_id *pci_id)978{979int retval = 0;980int vbi_buf_size;981struct ivtv *itv;982983itv = kzalloc(sizeof(struct ivtv), GFP_ATOMIC);984if (itv == NULL)985return -ENOMEM;986itv->pdev = pdev;987itv->instance = v4l2_device_set_name(&itv->v4l2_dev, "ivtv",988&ivtv_instance);989990retval = v4l2_device_register(&pdev->dev, &itv->v4l2_dev);991if (retval) {992kfree(itv);993return retval;994}995IVTV_INFO("Initializing card %d\n", itv->instance);996997ivtv_process_options(itv);998if (itv->options.cardtype == -1) {999retval = -ENODEV;1000goto err;1001}1002if (ivtv_init_struct1(itv)) {1003retval = -ENOMEM;1004goto err;1005}1006retval = cx2341x_handler_init(&itv->cxhdl, 50);1007if (retval)1008goto err;1009itv->v4l2_dev.ctrl_handler = &itv->cxhdl.hdl;1010itv->cxhdl.ops = &ivtv_cxhdl_ops;1011itv->cxhdl.priv = itv;1012itv->cxhdl.func = ivtv_api_func;10131014IVTV_DEBUG_INFO("base addr: 0x%08x\n", itv->base_addr);10151016/* PCI Device Setup */1017retval = ivtv_setup_pci(itv, pdev, pci_id);1018if (retval == -EIO)1019goto free_worker;1020if (retval == -ENXIO)1021goto free_mem;10221023/* map io memory */1024IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",1025itv->base_addr + IVTV_ENCODER_OFFSET, IVTV_ENCODER_SIZE);1026itv->enc_mem = ioremap_nocache(itv->base_addr + IVTV_ENCODER_OFFSET,1027IVTV_ENCODER_SIZE);1028if (!itv->enc_mem) {1029IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 "1030"encoder memory\n");1031IVTV_ERR("Each capture card with a CX23415/6 needs 8 MB of "1032"vmalloc address space for this window\n");1033IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");1034IVTV_ERR("Use the vmalloc= kernel command line option to set "1035"VmallocTotal to a larger value\n");1036retval = -ENOMEM;1037goto free_mem;1038}10391040if (itv->has_cx23415) {1041IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",1042itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);1043itv->dec_mem = ioremap_nocache(itv->base_addr + IVTV_DECODER_OFFSET,1044IVTV_DECODER_SIZE);1045if (!itv->dec_mem) {1046IVTV_ERR("ioremap failed. Can't get a window into "1047"CX23415 decoder memory\n");1048IVTV_ERR("Each capture card with a CX23415 needs 8 MB "1049"of vmalloc address space for this window\n");1050IVTV_ERR("Check the output of 'grep Vmalloc "1051"/proc/meminfo'\n");1052IVTV_ERR("Use the vmalloc= kernel command line option "1053"to set VmallocTotal to a larger value\n");1054retval = -ENOMEM;1055goto free_mem;1056}1057}1058else {1059itv->dec_mem = itv->enc_mem;1060}10611062/* map registers memory */1063IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",1064itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);1065itv->reg_mem =1066ioremap_nocache(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);1067if (!itv->reg_mem) {1068IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 "1069"register space\n");1070IVTV_ERR("Each capture card with a CX23415/6 needs 64 kB of "1071"vmalloc address space for this window\n");1072IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");1073IVTV_ERR("Use the vmalloc= kernel command line option to set "1074"VmallocTotal to a larger value\n");1075retval = -ENOMEM;1076goto free_io;1077}10781079retval = ivtv_gpio_init(itv);1080if (retval)1081goto free_io;10821083/* active i2c */1084IVTV_DEBUG_INFO("activating i2c...\n");1085if (init_ivtv_i2c(itv)) {1086IVTV_ERR("Could not initialize i2c\n");1087goto free_io;1088}10891090if (itv->card->hw_all & IVTV_HW_TVEEPROM) {1091/* Based on the model number the cardtype may be changed.1092The PCI IDs are not always reliable. */1093ivtv_process_eeprom(itv);1094}1095if (itv->card->comment)1096IVTV_INFO("%s", itv->card->comment);1097if (itv->card->v4l2_capabilities == 0) {1098/* card was detected but is not supported */1099retval = -ENODEV;1100goto free_i2c;1101}11021103if (itv->std == 0) {1104itv->std = V4L2_STD_NTSC_M;1105}11061107if (itv->options.tuner == -1) {1108int i;11091110for (i = 0; i < IVTV_CARD_MAX_TUNERS; i++) {1111if ((itv->std & itv->card->tuners[i].std) == 0)1112continue;1113itv->options.tuner = itv->card->tuners[i].tuner;1114break;1115}1116}1117/* if no tuner was found, then pick the first tuner in the card list */1118if (itv->options.tuner == -1 && itv->card->tuners[0].std) {1119itv->std = itv->card->tuners[0].std;1120if (itv->std & V4L2_STD_PAL)1121itv->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;1122else if (itv->std & V4L2_STD_NTSC)1123itv->std = V4L2_STD_NTSC_M;1124else if (itv->std & V4L2_STD_SECAM)1125itv->std = V4L2_STD_SECAM_L;1126itv->options.tuner = itv->card->tuners[0].tuner;1127}1128if (itv->options.radio == -1)1129itv->options.radio = (itv->card->radio_input.audio_type != 0);11301131/* The card is now fully identified, continue with card-specific1132initialization. */1133ivtv_init_struct2(itv);11341135ivtv_load_and_init_modules(itv);11361137if (itv->std & V4L2_STD_525_60) {1138itv->is_60hz = 1;1139itv->is_out_60hz = 1;1140} else {1141itv->is_50hz = 1;1142itv->is_out_50hz = 1;1143}11441145itv->yuv_info.osd_full_w = 720;1146itv->yuv_info.osd_full_h = itv->is_out_50hz ? 576 : 480;1147itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;1148itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;11491150cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);11511152itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;1153itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;1154itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;1155itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = 0x10000;1156itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = 0x08000;11571158/* Setup VBI Raw Size. Should be big enough to hold PAL.1159It is possible to switch between PAL and NTSC, so we need to1160take the largest size here. */1161/* 1456 is multiple of 16, real size = 1444 */1162itv->vbi.raw_size = 1456;1163/* We use a buffer size of 1/2 of the total size needed for a1164frame. This is actually very useful, since we now receive1165a field at a time and that makes 'compressing' the raw data1166down to size by stripping off the SAV codes a lot easier.1167Note: having two different buffer sizes prevents standard1168switching on the fly. We need to find a better solution... */1169vbi_buf_size = itv->vbi.raw_size * (itv->is_60hz ? 24 : 36) / 2;1170itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_VBI] = vbi_buf_size;1171itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_VBI] = sizeof(struct v4l2_sliced_vbi_data) * 36;11721173if (itv->options.radio > 0)1174itv->v4l2_cap |= V4L2_CAP_RADIO;11751176if (itv->options.tuner > -1) {1177struct tuner_setup setup;11781179setup.addr = ADDR_UNSET;1180setup.type = itv->options.tuner;1181setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */1182setup.tuner_callback = (setup.type == TUNER_XC2028) ?1183ivtv_reset_tuner_gpio : NULL;1184ivtv_call_all(itv, tuner, s_type_addr, &setup);1185if (setup.type == TUNER_XC2028) {1186static struct xc2028_ctrl ctrl = {1187.fname = XC2028_DEFAULT_FIRMWARE,1188.max_len = 64,1189};1190struct v4l2_priv_tun_config cfg = {1191.tuner = itv->options.tuner,1192.priv = &ctrl,1193};1194ivtv_call_all(itv, tuner, s_config, &cfg);1195}1196}11971198/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)1199are not. */1200itv->tuner_std = itv->std;12011202if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {1203ivtv_call_all(itv, video, s_std_output, itv->std);1204/* Turn off the output signal. The mpeg decoder is not yet1205active so without this you would get a green image until the1206mpeg decoder becomes active. */1207ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);1208}12091210/* clear interrupt mask, effectively disabling interrupts */1211ivtv_set_irq_mask(itv, 0xffffffff);12121213/* Register IRQ */1214retval = request_irq(itv->pdev->irq, ivtv_irq_handler,1215IRQF_SHARED | IRQF_DISABLED, itv->v4l2_dev.name, (void *)itv);1216if (retval) {1217IVTV_ERR("Failed to register irq %d\n", retval);1218goto free_i2c;1219}12201221retval = ivtv_streams_setup(itv);1222if (retval) {1223IVTV_ERR("Error %d setting up streams\n", retval);1224goto free_irq;1225}1226retval = ivtv_streams_register(itv);1227if (retval) {1228IVTV_ERR("Error %d registering devices\n", retval);1229goto free_streams;1230}1231IVTV_INFO("Initialized card: %s\n", itv->card_name);1232return 0;12331234free_streams:1235ivtv_streams_cleanup(itv, 1);1236free_irq:1237free_irq(itv->pdev->irq, (void *)itv);1238free_i2c:1239exit_ivtv_i2c(itv);1240free_io:1241ivtv_iounmap(itv);1242free_mem:1243release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);1244release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);1245if (itv->has_cx23415)1246release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);1247free_worker:1248kthread_stop(itv->irq_worker_task);1249err:1250if (retval == 0)1251retval = -ENODEV;1252IVTV_ERR("Error %d on initialization\n", retval);12531254v4l2_device_unregister(&itv->v4l2_dev);1255kfree(itv);1256return retval;1257}12581259int ivtv_init_on_first_open(struct ivtv *itv)1260{1261struct v4l2_frequency vf;1262/* Needed to call ioctls later */1263struct ivtv_open_id fh;1264int fw_retry_count = 3;1265int video_input;12661267fh.itv = itv;12681269if (test_bit(IVTV_F_I_FAILED, &itv->i_flags))1270return -ENXIO;12711272if (test_and_set_bit(IVTV_F_I_INITED, &itv->i_flags))1273return 0;12741275while (--fw_retry_count > 0) {1276/* load firmware */1277if (ivtv_firmware_init(itv) == 0)1278break;1279if (fw_retry_count > 1)1280IVTV_WARN("Retry loading firmware\n");1281}12821283if (fw_retry_count == 0) {1284set_bit(IVTV_F_I_FAILED, &itv->i_flags);1285return -ENXIO;1286}12871288/* Try and get firmware versions */1289IVTV_DEBUG_INFO("Getting firmware version..\n");1290ivtv_firmware_versions(itv);12911292if (itv->card->hw_all & IVTV_HW_CX25840)1293v4l2_subdev_call(itv->sd_video, core, load_fw);12941295vf.tuner = 0;1296vf.type = V4L2_TUNER_ANALOG_TV;1297vf.frequency = 6400; /* the tuner 'baseline' frequency */12981299/* Set initial frequency. For PAL/SECAM broadcasts no1300'default' channel exists AFAIK. */1301if (itv->std == V4L2_STD_NTSC_M_JP) {1302vf.frequency = 1460; /* ch. 1 91250*16/1000 */1303}1304else if (itv->std & V4L2_STD_NTSC_M) {1305vf.frequency = 1076; /* ch. 4 67250*16/1000 */1306}13071308video_input = itv->active_input;1309itv->active_input++; /* Force update of input */1310ivtv_s_input(NULL, &fh, video_input);13111312/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code1313in one place. */1314itv->std++; /* Force full standard initialization */1315itv->std_out = itv->std;1316ivtv_s_frequency(NULL, &fh, &vf);13171318if (itv->card->v4l2_capabilities & V4L2_CAP_VIDEO_OUTPUT) {1319/* Turn on the TV-out: ivtv_init_mpeg_decoder() initializes1320the mpeg decoder so now the saa7127 receives a proper1321signal. */1322ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);1323ivtv_init_mpeg_decoder(itv);1324}13251326/* On a cx23416 this seems to be able to enable DMA to the chip? */1327if (!itv->has_cx23415)1328write_reg_sync(0x03, IVTV_REG_DMACONTROL);13291330ivtv_s_std_enc(itv, &itv->tuner_std);13311332/* Default interrupts enabled. For the PVR350 this includes the1333decoder VSYNC interrupt, which is always on. It is not only used1334during decoding but also by the OSD.1335Some old PVR250 cards had a cx23415, so testing for that is too1336general. Instead test if the card has video output capability. */1337if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {1338ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT | IVTV_IRQ_DEC_VSYNC);1339ivtv_set_osd_alpha(itv);1340ivtv_s_std_dec(itv, &itv->tuner_std);1341} else {1342ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT);1343}13441345/* Setup initial controls */1346cx2341x_handler_setup(&itv->cxhdl);1347return 0;1348}13491350static void ivtv_remove(struct pci_dev *pdev)1351{1352struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);1353struct ivtv *itv = to_ivtv(v4l2_dev);1354int i;13551356IVTV_DEBUG_INFO("Removing card\n");13571358if (test_bit(IVTV_F_I_INITED, &itv->i_flags)) {1359/* Stop all captures */1360IVTV_DEBUG_INFO("Stopping all streams\n");1361if (atomic_read(&itv->capturing) > 0)1362ivtv_stop_all_captures(itv);13631364/* Stop all decoding */1365IVTV_DEBUG_INFO("Stopping decoding\n");13661367/* Turn off the TV-out */1368if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)1369ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);1370if (atomic_read(&itv->decoding) > 0) {1371int type;13721373if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))1374type = IVTV_DEC_STREAM_TYPE_YUV;1375else1376type = IVTV_DEC_STREAM_TYPE_MPG;1377ivtv_stop_v4l2_decode_stream(&itv->streams[type],1378VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);1379}1380ivtv_halt_firmware(itv);1381}13821383/* Interrupts */1384ivtv_set_irq_mask(itv, 0xffffffff);1385del_timer_sync(&itv->dma_timer);13861387/* Kill irq worker */1388flush_kthread_worker(&itv->irq_worker);1389kthread_stop(itv->irq_worker_task);13901391ivtv_streams_cleanup(itv, 1);1392ivtv_udma_free(itv);13931394exit_ivtv_i2c(itv);13951396free_irq(itv->pdev->irq, (void *)itv);1397ivtv_iounmap(itv);13981399release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);1400release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);1401if (itv->has_cx23415)1402release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);14031404pci_disable_device(itv->pdev);1405for (i = 0; i < IVTV_VBI_FRAMES; i++)1406kfree(itv->vbi.sliced_mpeg_data[i]);14071408printk(KERN_INFO "ivtv: Removed %s\n", itv->card_name);14091410v4l2_device_unregister(&itv->v4l2_dev);1411kfree(itv);1412}14131414/* define a pci_driver for card detection */1415static struct pci_driver ivtv_pci_driver = {1416.name = "ivtv",1417.id_table = ivtv_pci_tbl,1418.probe = ivtv_probe,1419.remove = ivtv_remove,1420};14211422static int __init module_start(void)1423{1424printk(KERN_INFO "ivtv: Start initialization, version %s\n", IVTV_VERSION);14251426/* Validate parameters */1427if (ivtv_first_minor < 0 || ivtv_first_minor >= IVTV_MAX_CARDS) {1428printk(KERN_ERR "ivtv: Exiting, ivtv_first_minor must be between 0 and %d\n",1429IVTV_MAX_CARDS - 1);1430return -1;1431}14321433if (ivtv_debug < 0 || ivtv_debug > 2047) {1434ivtv_debug = 0;1435printk(KERN_INFO "ivtv: Debug value must be >= 0 and <= 2047\n");1436}14371438if (pci_register_driver(&ivtv_pci_driver)) {1439printk(KERN_ERR "ivtv: Error detecting PCI card\n");1440return -ENODEV;1441}1442printk(KERN_INFO "ivtv: End initialization\n");1443return 0;1444}14451446static void __exit module_cleanup(void)1447{1448pci_unregister_driver(&ivtv_pci_driver);1449}14501451/* Note: These symbols are exported because they are used by the ivtvfb1452framebuffer module and an infrared module for the IR-blaster. */1453EXPORT_SYMBOL(ivtv_set_irq_mask);1454EXPORT_SYMBOL(ivtv_api);1455EXPORT_SYMBOL(ivtv_vapi);1456EXPORT_SYMBOL(ivtv_vapi_result);1457EXPORT_SYMBOL(ivtv_clear_irq_mask);1458EXPORT_SYMBOL(ivtv_debug);1459#ifdef CONFIG_VIDEO_ADV_DEBUG1460EXPORT_SYMBOL(ivtv_fw_debug);1461#endif1462EXPORT_SYMBOL(ivtv_reset_ir_gpio);1463EXPORT_SYMBOL(ivtv_udma_setup);1464EXPORT_SYMBOL(ivtv_udma_unmap);1465EXPORT_SYMBOL(ivtv_udma_alloc);1466EXPORT_SYMBOL(ivtv_udma_prepare);1467EXPORT_SYMBOL(ivtv_init_on_first_open);1468EXPORT_SYMBOL(ivtv_firmware_check);14691470module_init(module_start);1471module_exit(module_cleanup);147214731474