Path: blob/master/drivers/media/video/cx18/cx18-driver.c
17681 views
/*1* cx18 driver initialization and card probing2*3* Derived from ivtv-driver.c4*5* Copyright (C) 2007 Hans Verkuil <[email protected]>6* Copyright (C) 2008 Andy Walls <[email protected]>7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA21* 02111-1307 USA22*/2324#include "cx18-driver.h"25#include "cx18-io.h"26#include "cx18-version.h"27#include "cx18-cards.h"28#include "cx18-i2c.h"29#include "cx18-irq.h"30#include "cx18-gpio.h"31#include "cx18-firmware.h"32#include "cx18-queue.h"33#include "cx18-streams.h"34#include "cx18-av-core.h"35#include "cx18-scb.h"36#include "cx18-mailbox.h"37#include "cx18-ioctl.h"38#include "cx18-controls.h"39#include "tuner-xc2028.h"4041#include <media/tveeprom.h>4243/* If you have already X v4l cards, then set this to X. This way44the device numbers stay matched. Example: you have a WinTV card45without radio and a Compro H900 with. Normally this would give a46video1 device together with a radio0 device for the Compro. By47setting this to 1 you ensure that radio0 is now also radio1. */48int cx18_first_minor;4950/* Callback for registering extensions */51int (*cx18_ext_init)(struct cx18 *);52EXPORT_SYMBOL(cx18_ext_init);5354/* add your revision and whatnot here */55static struct pci_device_id cx18_pci_tbl[] __devinitdata = {56{PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,57PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},58{0,}59};6061MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);6263static atomic_t cx18_instance = ATOMIC_INIT(0);6465/* Parameter declarations */66static int cardtype[CX18_MAX_CARDS];67static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,68-1, -1, -1, -1, -1, -1, -1, -1,69-1, -1, -1, -1, -1, -1, -1, -1,70-1, -1, -1, -1, -1, -1, -1, -1 };71static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,72-1, -1, -1, -1, -1, -1, -1, -1,73-1, -1, -1, -1, -1, -1, -1, -1,74-1, -1, -1, -1, -1, -1, -1, -1 };75static unsigned cardtype_c = 1;76static unsigned tuner_c = 1;77static unsigned radio_c = 1;78static char pal[] = "--";79static char secam[] = "--";80static char ntsc[] = "-";8182/* Buffers */83static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;84static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;85static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;86static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;87static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;88static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;8990static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;91static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;92static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;93static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;94static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;9596static int enc_ts_bufs = -1;97static int enc_mpg_bufs = -1;98static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;99static int enc_yuv_bufs = -1;100static int enc_vbi_bufs = -1;101static int enc_pcm_bufs = -1;102103104static int cx18_pci_latency = 1;105106static int mmio_ndelay;107static int retry_mmio = 1;108109int cx18_debug;110111module_param_array(tuner, int, &tuner_c, 0644);112module_param_array(radio, bool, &radio_c, 0644);113module_param_array(cardtype, int, &cardtype_c, 0644);114module_param_string(pal, pal, sizeof(pal), 0644);115module_param_string(secam, secam, sizeof(secam), 0644);116module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);117module_param_named(debug, cx18_debug, int, 0644);118module_param(mmio_ndelay, int, 0644);119module_param(retry_mmio, int, 0644);120module_param(cx18_pci_latency, int, 0644);121module_param(cx18_first_minor, int, 0644);122123module_param(enc_ts_buffers, int, 0644);124module_param(enc_mpg_buffers, int, 0644);125module_param(enc_idx_buffers, int, 0644);126module_param(enc_yuv_buffers, int, 0644);127module_param(enc_vbi_buffers, int, 0644);128module_param(enc_pcm_buffers, int, 0644);129130module_param(enc_ts_bufsize, int, 0644);131module_param(enc_mpg_bufsize, int, 0644);132module_param(enc_idx_bufsize, int, 0644);133module_param(enc_yuv_bufsize, int, 0644);134module_param(enc_pcm_bufsize, int, 0644);135136module_param(enc_ts_bufs, int, 0644);137module_param(enc_mpg_bufs, int, 0644);138module_param(enc_idx_bufs, int, 0644);139module_param(enc_yuv_bufs, int, 0644);140module_param(enc_vbi_bufs, int, 0644);141module_param(enc_pcm_bufs, int, 0644);142143MODULE_PARM_DESC(tuner, "Tuner type selection,\n"144"\t\t\tsee tuner.h for values");145MODULE_PARM_DESC(radio,146"Enable or disable the radio. Use only if autodetection\n"147"\t\t\tfails. 0 = disable, 1 = enable");148MODULE_PARM_DESC(cardtype,149"Only use this option if your card is not detected properly.\n"150"\t\tSpecify card type:\n"151"\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"152"\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"153"\t\t\t 3 = Compro VideoMate H900\n"154"\t\t\t 4 = Yuan MPC718\n"155"\t\t\t 5 = Conexant Raptor PAL/SECAM\n"156"\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"157"\t\t\t 7 = Leadtek WinFast PVR2100\n"158"\t\t\t 8 = Leadtek WinFast DVR3100 H\n"159"\t\t\t 9 = GoTView PCI DVD3 Hybrid\n"160"\t\t\t 10 = Hauppauge HVR 1600 (S5H1411)\n"161"\t\t\t 0 = Autodetect (default)\n"162"\t\t\t-1 = Ignore this card\n\t\t");163MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");164MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");165MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");166MODULE_PARM_DESC(debug,167"Debug level (bitmask). Default: 0\n"168"\t\t\t 1/0x0001: warning\n"169"\t\t\t 2/0x0002: info\n"170"\t\t\t 4/0x0004: mailbox\n"171"\t\t\t 8/0x0008: dma\n"172"\t\t\t 16/0x0010: ioctl\n"173"\t\t\t 32/0x0020: file\n"174"\t\t\t 64/0x0040: i2c\n"175"\t\t\t128/0x0080: irq\n"176"\t\t\t256/0x0100: high volume\n");177MODULE_PARM_DESC(cx18_pci_latency,178"Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"179"\t\t\tDefault: Yes");180MODULE_PARM_DESC(retry_mmio,181"(Deprecated) MMIO writes are now always checked and retried\n"182"\t\t\tEffectively: 1 [Yes]");183MODULE_PARM_DESC(mmio_ndelay,184"(Deprecated) MMIO accesses are now never purposely delayed\n"185"\t\t\tEffectively: 0 ns");186MODULE_PARM_DESC(enc_ts_buffers,187"Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"188"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));189MODULE_PARM_DESC(enc_ts_bufsize,190"Size of an encoder TS buffer (kB)\n"191"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));192MODULE_PARM_DESC(enc_ts_bufs,193"Number of encoder TS buffers\n"194"\t\t\tDefault is computed from other enc_ts_* parameters");195MODULE_PARM_DESC(enc_mpg_buffers,196"Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"197"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));198MODULE_PARM_DESC(enc_mpg_bufsize,199"Size of an encoder MPG buffer (kB)\n"200"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));201MODULE_PARM_DESC(enc_mpg_bufs,202"Number of encoder MPG buffers\n"203"\t\t\tDefault is computed from other enc_mpg_* parameters");204MODULE_PARM_DESC(enc_idx_buffers,205"(Deprecated) Encoder IDX buffer memory (MB)\n"206"\t\t\tIgnored, except 0 disables IDX buffer allocations\n"207"\t\t\tDefault: 1 [Enabled]");208MODULE_PARM_DESC(enc_idx_bufsize,209"Size of an encoder IDX buffer (kB)\n"210"\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"211"\t\t\t(multiples of size required for 64 index entries)\n"212"\t\t\tDefault: 2");213MODULE_PARM_DESC(enc_idx_bufs,214"Number of encoder IDX buffers\n"215"\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));216MODULE_PARM_DESC(enc_yuv_buffers,217"Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"218"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));219MODULE_PARM_DESC(enc_yuv_bufsize,220"Size of an encoder YUV buffer (kB)\n"221"\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"222"\t\t\t(multiples of size required for 32 screen lines)\n"223"\t\t\tDefault: 102");224MODULE_PARM_DESC(enc_yuv_bufs,225"Number of encoder YUV buffers\n"226"\t\t\tDefault is computed from other enc_yuv_* parameters");227MODULE_PARM_DESC(enc_vbi_buffers,228"Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"229"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));230MODULE_PARM_DESC(enc_vbi_bufs,231"Number of encoder VBI buffers\n"232"\t\t\tDefault is computed from enc_vbi_buffers");233MODULE_PARM_DESC(enc_pcm_buffers,234"Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"235"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));236MODULE_PARM_DESC(enc_pcm_bufsize,237"Size of an encoder PCM buffer (kB)\n"238"\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));239MODULE_PARM_DESC(enc_pcm_bufs,240"Number of encoder PCM buffers\n"241"\t\t\tDefault is computed from other enc_pcm_* parameters");242243MODULE_PARM_DESC(cx18_first_minor,244"Set device node number assigned to first card");245246MODULE_AUTHOR("Hans Verkuil");247MODULE_DESCRIPTION("CX23418 driver");248MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");249MODULE_LICENSE("GPL");250251MODULE_VERSION(CX18_VERSION);252253#if defined(CONFIG_MODULES) && defined(MODULE)254static void request_module_async(struct work_struct *work)255{256struct cx18 *dev = container_of(work, struct cx18, request_module_wk);257258/* Make sure cx18-alsa module is loaded */259request_module("cx18-alsa");260261/* Initialize cx18-alsa for this instance of the cx18 device */262if (cx18_ext_init != NULL)263cx18_ext_init(dev);264}265266static void request_modules(struct cx18 *dev)267{268INIT_WORK(&dev->request_module_wk, request_module_async);269schedule_work(&dev->request_module_wk);270}271272static void flush_request_modules(struct cx18 *dev)273{274flush_work_sync(&dev->request_module_wk);275}276#else277#define request_modules(dev)278#define flush_request_modules(dev)279#endif /* CONFIG_MODULES */280281/* Generic utility functions */282int cx18_msleep_timeout(unsigned int msecs, int intr)283{284long int timeout = msecs_to_jiffies(msecs);285int sig;286287do {288set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);289timeout = schedule_timeout(timeout);290sig = intr ? signal_pending(current) : 0;291} while (!sig && timeout);292return sig;293}294295/* Release ioremapped memory */296static void cx18_iounmap(struct cx18 *cx)297{298if (cx == NULL)299return;300301/* Release io memory */302if (cx->enc_mem != NULL) {303CX18_DEBUG_INFO("releasing enc_mem\n");304iounmap(cx->enc_mem);305cx->enc_mem = NULL;306}307}308309static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)310{311int i;312313CX18_INFO("eeprom dump:\n");314for (i = 0; i < len; i++) {315if (0 == (i % 16))316CX18_INFO("eeprom %02x:", i);317printk(KERN_CONT " %02x", eedata[i]);318if (15 == (i % 16))319printk(KERN_CONT "\n");320}321}322323/* Hauppauge card? get values from tveeprom */324void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)325{326struct i2c_client c;327u8 eedata[256];328329memset(&c, 0, sizeof(c));330strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name));331c.adapter = &cx->i2c_adap[0];332c.addr = 0xA0 >> 1;333334memset(tv, 0, sizeof(*tv));335if (tveeprom_read(&c, eedata, sizeof(eedata)))336return;337338switch (cx->card->type) {339case CX18_CARD_HVR_1600_ESMT:340case CX18_CARD_HVR_1600_SAMSUNG:341case CX18_CARD_HVR_1600_S5H1411:342tveeprom_hauppauge_analog(&c, tv, eedata);343break;344case CX18_CARD_YUAN_MPC718:345case CX18_CARD_GOTVIEW_PCI_DVD3:346tv->model = 0x718;347cx18_eeprom_dump(cx, eedata, sizeof(eedata));348CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",349eedata[2], eedata[1], eedata[4], eedata[3]);350break;351default:352tv->model = 0xffffffff;353cx18_eeprom_dump(cx, eedata, sizeof(eedata));354break;355}356}357358static void cx18_process_eeprom(struct cx18 *cx)359{360struct tveeprom tv;361362cx18_read_eeprom(cx, &tv);363364/* Many thanks to Steven Toth from Hauppauge for providing the365model numbers */366/* Note: the Samsung memory models cannot be reliably determined367from the model number. Use the cardtype module option if you368have one of these preproduction models. */369switch (tv.model) {370case 74301: /* Retail models */371case 74321:372case 74351: /* OEM models */373case 74361:374/* Digital side is s5h1411/tda18271 */375cx->card = cx18_get_card(CX18_CARD_HVR_1600_S5H1411);376break;377case 74021: /* Retail models */378case 74031:379case 74041:380case 74141:381case 74541: /* OEM models */382case 74551:383case 74591:384case 74651:385case 74691:386case 74751:387case 74891:388/* Digital side is s5h1409/mxl5005s */389cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);390break;391case 0x718:392return;393case 0xffffffff:394CX18_INFO("Unknown EEPROM encoding\n");395return;396case 0:397CX18_ERR("Invalid EEPROM\n");398return;399default:400CX18_ERR("Unknown model %d, defaulting to original HVR-1600 "401"(cardtype=1)\n", tv.model);402cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);403break;404}405406cx->v4l2_cap = cx->card->v4l2_capabilities;407cx->card_name = cx->card->name;408cx->card_i2c = cx->card->i2c;409410CX18_INFO("Autodetected %s\n", cx->card_name);411412if (tv.tuner_type == TUNER_ABSENT)413CX18_ERR("tveeprom cannot autodetect tuner!\n");414415if (cx->options.tuner == -1)416cx->options.tuner = tv.tuner_type;417if (cx->options.radio == -1)418cx->options.radio = (tv.has_radio != 0);419420if (cx->std != 0)421/* user specified tuner standard */422return;423424/* autodetect tuner standard */425#define TVEEPROM_TUNER_FORMAT_ALL (V4L2_STD_B | V4L2_STD_GH | \426V4L2_STD_MN | \427V4L2_STD_PAL_I | \428V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC | \429V4L2_STD_DK)430if ((tv.tuner_formats & TVEEPROM_TUNER_FORMAT_ALL)431== TVEEPROM_TUNER_FORMAT_ALL) {432CX18_DEBUG_INFO("Worldwide tuner detected\n");433cx->std = V4L2_STD_ALL;434} else if (tv.tuner_formats & V4L2_STD_PAL) {435CX18_DEBUG_INFO("PAL tuner detected\n");436cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;437} else if (tv.tuner_formats & V4L2_STD_NTSC) {438CX18_DEBUG_INFO("NTSC tuner detected\n");439cx->std |= V4L2_STD_NTSC_M;440} else if (tv.tuner_formats & V4L2_STD_SECAM) {441CX18_DEBUG_INFO("SECAM tuner detected\n");442cx->std |= V4L2_STD_SECAM_L;443} else {444CX18_INFO("No tuner detected, default to NTSC-M\n");445cx->std |= V4L2_STD_NTSC_M;446}447}448449static v4l2_std_id cx18_parse_std(struct cx18 *cx)450{451switch (pal[0]) {452case '6':453return V4L2_STD_PAL_60;454case 'b':455case 'B':456case 'g':457case 'G':458return V4L2_STD_PAL_BG;459case 'h':460case 'H':461return V4L2_STD_PAL_H;462case 'n':463case 'N':464if (pal[1] == 'c' || pal[1] == 'C')465return V4L2_STD_PAL_Nc;466return V4L2_STD_PAL_N;467case 'i':468case 'I':469return V4L2_STD_PAL_I;470case 'd':471case 'D':472case 'k':473case 'K':474return V4L2_STD_PAL_DK;475case 'M':476case 'm':477return V4L2_STD_PAL_M;478case '-':479break;480default:481CX18_WARN("pal= argument not recognised\n");482return 0;483}484485switch (secam[0]) {486case 'b':487case 'B':488case 'g':489case 'G':490case 'h':491case 'H':492return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;493case 'd':494case 'D':495case 'k':496case 'K':497return V4L2_STD_SECAM_DK;498case 'l':499case 'L':500if (secam[1] == 'C' || secam[1] == 'c')501return V4L2_STD_SECAM_LC;502return V4L2_STD_SECAM_L;503case '-':504break;505default:506CX18_WARN("secam= argument not recognised\n");507return 0;508}509510switch (ntsc[0]) {511case 'm':512case 'M':513return V4L2_STD_NTSC_M;514case 'j':515case 'J':516return V4L2_STD_NTSC_M_JP;517case 'k':518case 'K':519return V4L2_STD_NTSC_M_KR;520case '-':521break;522default:523CX18_WARN("ntsc= argument not recognised\n");524return 0;525}526527/* no match found */528return 0;529}530531static void cx18_process_options(struct cx18 *cx)532{533int i, j;534535cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;536cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;537cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;538cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;539cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;540cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;541cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */542543cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;544cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;545cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;546cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;547cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;548cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;549cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */550551cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;552cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;553cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;554cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;555cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;556cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;557cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */558559/* Ensure stream_buffers & stream_buf_size are valid */560for (i = 0; i < CX18_MAX_STREAMS; i++) {561if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */562cx->options.megabytes[i] <= 0 || /* User said 0 MB total */563cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */564cx->options.megabytes[i] = 0;565cx->stream_buffers[i] = 0;566cx->stream_buf_size[i] = 0;567continue;568}569/*570* YUV is a special case where the stream_buf_size needs to be571* an integral multiple of 33.75 kB (storage for 32 screens572* lines to maintain alignment in case of lost buffers).573*574* IDX is a special case where the stream_buf_size should be575* an integral multiple of 1.5 kB (storage for 64 index entries576* to maintain alignment in case of lost buffers).577*578*/579if (i == CX18_ENC_STREAM_TYPE_YUV) {580cx->stream_buf_size[i] *= 1024;581cx->stream_buf_size[i] -=582(cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);583584if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)585cx->stream_buf_size[i] =586CX18_UNIT_ENC_YUV_BUFSIZE;587} else if (i == CX18_ENC_STREAM_TYPE_IDX) {588cx->stream_buf_size[i] *= 1024;589cx->stream_buf_size[i] -=590(cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);591592if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)593cx->stream_buf_size[i] =594CX18_UNIT_ENC_IDX_BUFSIZE;595}596/*597* YUV and IDX are special cases where the stream_buf_size is598* now in bytes.599* VBI is a special case where the stream_buf_size is fixed600* and already in bytes601*/602if (i == CX18_ENC_STREAM_TYPE_VBI ||603i == CX18_ENC_STREAM_TYPE_YUV ||604i == CX18_ENC_STREAM_TYPE_IDX) {605if (cx->stream_buffers[i] < 0) {606cx->stream_buffers[i] =607cx->options.megabytes[i] * 1024 * 1024608/ cx->stream_buf_size[i];609} else {610/* N.B. This might round down to 0 */611cx->options.megabytes[i] =612cx->stream_buffers[i]613* cx->stream_buf_size[i]/(1024 * 1024);614}615} else {616/* All other streams have stream_buf_size in kB here */617if (cx->stream_buffers[i] < 0) {618cx->stream_buffers[i] =619cx->options.megabytes[i] * 1024620/ cx->stream_buf_size[i];621} else {622/* N.B. This might round down to 0 */623cx->options.megabytes[i] =624cx->stream_buffers[i]625* cx->stream_buf_size[i] / 1024;626}627/* convert from kB to bytes */628cx->stream_buf_size[i] *= 1024;629}630CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "631"%d bytes\n", i, cx->options.megabytes[i],632cx->stream_buffers[i], cx->stream_buf_size[i]);633}634635cx->options.cardtype = cardtype[cx->instance];636cx->options.tuner = tuner[cx->instance];637cx->options.radio = radio[cx->instance];638639cx->std = cx18_parse_std(cx);640if (cx->options.cardtype == -1) {641CX18_INFO("Ignore card\n");642return;643}644cx->card = cx18_get_card(cx->options.cardtype - 1);645if (cx->card)646CX18_INFO("User specified %s card\n", cx->card->name);647else if (cx->options.cardtype != 0)648CX18_ERR("Unknown user specified type, trying to autodetect card\n");649if (cx->card == NULL) {650if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {651cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);652CX18_INFO("Autodetected Hauppauge card\n");653}654}655if (cx->card == NULL) {656for (i = 0; (cx->card = cx18_get_card(i)); i++) {657if (cx->card->pci_list == NULL)658continue;659for (j = 0; cx->card->pci_list[j].device; j++) {660if (cx->pci_dev->device !=661cx->card->pci_list[j].device)662continue;663if (cx->pci_dev->subsystem_vendor !=664cx->card->pci_list[j].subsystem_vendor)665continue;666if (cx->pci_dev->subsystem_device !=667cx->card->pci_list[j].subsystem_device)668continue;669CX18_INFO("Autodetected %s card\n", cx->card->name);670goto done;671}672}673}674done:675676if (cx->card == NULL) {677cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);678CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",679cx->pci_dev->vendor, cx->pci_dev->device);680CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",681cx->pci_dev->subsystem_vendor,682cx->pci_dev->subsystem_device);683CX18_ERR("Defaulting to %s card\n", cx->card->name);684CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");685CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");686CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");687}688cx->v4l2_cap = cx->card->v4l2_capabilities;689cx->card_name = cx->card->name;690cx->card_i2c = cx->card->i2c;691}692693static int __devinit cx18_create_in_workq(struct cx18 *cx)694{695snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",696cx->v4l2_dev.name);697cx->in_work_queue = alloc_ordered_workqueue(cx->in_workq_name, 0);698if (cx->in_work_queue == NULL) {699CX18_ERR("Unable to create incoming mailbox handler thread\n");700return -ENOMEM;701}702return 0;703}704705static void __devinit cx18_init_in_work_orders(struct cx18 *cx)706{707int i;708for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {709cx->in_work_order[i].cx = cx;710cx->in_work_order[i].str = cx->epu_debug_str;711INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);712}713}714715/* Precondition: the cx18 structure has been memset to 0. Only716the dev and instance fields have been filled in.717No assumptions on the card type may be made here (see cx18_init_struct2718for that).719*/720static int __devinit cx18_init_struct1(struct cx18 *cx)721{722int ret;723724cx->base_addr = pci_resource_start(cx->pci_dev, 0);725726mutex_init(&cx->serialize_lock);727mutex_init(&cx->gpio_lock);728mutex_init(&cx->epu2apu_mb_lock);729mutex_init(&cx->epu2cpu_mb_lock);730731ret = cx18_create_in_workq(cx);732if (ret)733return ret;734735cx18_init_in_work_orders(cx);736737/* start counting open_id at 1 */738cx->open_id = 1;739740/* Initial settings */741cx->cxhdl.port = CX2341X_PORT_MEMORY;742cx->cxhdl.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;743cx->cxhdl.ops = &cx18_cxhdl_ops;744cx->cxhdl.func = cx18_api_func;745cx->cxhdl.priv = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];746ret = cx2341x_handler_init(&cx->cxhdl, 50);747if (ret)748return ret;749cx->v4l2_dev.ctrl_handler = &cx->cxhdl.hdl;750751cx->temporal_strength = cx->cxhdl.video_temporal_filter->cur.val;752cx->spatial_strength = cx->cxhdl.video_spatial_filter->cur.val;753cx->filter_mode = cx->cxhdl.video_spatial_filter_mode->cur.val |754(cx->cxhdl.video_temporal_filter_mode->cur.val << 1) |755(cx->cxhdl.video_median_filter_type->cur.val << 2);756757init_waitqueue_head(&cx->cap_w);758init_waitqueue_head(&cx->mb_apu_waitq);759init_waitqueue_head(&cx->mb_cpu_waitq);760init_waitqueue_head(&cx->dma_waitq);761762/* VBI */763cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;764cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;765766/* IVTV style VBI insertion into MPEG streams */767INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);768INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);769INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);770list_add(&cx->vbi.sliced_mpeg_buf.list,771&cx->vbi.sliced_mpeg_mdl.buf_list);772return 0;773}774775/* Second initialization part. Here the card type has been776autodetected. */777static void __devinit cx18_init_struct2(struct cx18 *cx)778{779int i;780781for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)782if (cx->card->video_inputs[i].video_type == 0)783break;784cx->nof_inputs = i;785for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)786if (cx->card->audio_inputs[i].audio_type == 0)787break;788cx->nof_audio_inputs = i;789790/* Find tuner input */791for (i = 0; i < cx->nof_inputs; i++) {792if (cx->card->video_inputs[i].video_type ==793CX18_CARD_INPUT_VID_TUNER)794break;795}796if (i == cx->nof_inputs)797i = 0;798cx->active_input = i;799cx->audio_input = cx->card->video_inputs[i].audio_index;800}801802static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,803const struct pci_device_id *pci_id)804{805u16 cmd;806unsigned char pci_latency;807808CX18_DEBUG_INFO("Enabling pci device\n");809810if (pci_enable_device(pci_dev)) {811CX18_ERR("Can't enable device %d!\n", cx->instance);812return -EIO;813}814if (pci_set_dma_mask(pci_dev, 0xffffffff)) {815CX18_ERR("No suitable DMA available, card %d\n", cx->instance);816return -EIO;817}818if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {819CX18_ERR("Cannot request encoder memory region, card %d\n",820cx->instance);821return -EIO;822}823824/* Enable bus mastering and memory mapped IO for the CX23418 */825pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);826cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;827pci_write_config_word(pci_dev, PCI_COMMAND, cmd);828829cx->card_rev = pci_dev->revision;830pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);831832if (pci_latency < 64 && cx18_pci_latency) {833CX18_INFO("Unreasonably low latency timer, "834"setting to 64 (was %d)\n", pci_latency);835pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);836pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);837}838839CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "840"irq: %d, latency: %d, memory: 0x%lx\n",841cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,842PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),843cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr);844845return 0;846}847848static void cx18_init_subdevs(struct cx18 *cx)849{850u32 hw = cx->card->hw_all;851u32 device;852int i;853854for (i = 0, device = 1; i < 32; i++, device <<= 1) {855856if (!(device & hw))857continue;858859switch (device) {860case CX18_HW_DVB:861case CX18_HW_TVEEPROM:862/* These subordinate devices do not use probing */863cx->hw_flags |= device;864break;865case CX18_HW_418_AV:866/* The A/V decoder gets probed earlier to set PLLs */867/* Just note that the card uses it (i.e. has analog) */868cx->hw_flags |= device;869break;870case CX18_HW_GPIO_RESET_CTRL:871/*872* The Reset Controller gets probed and added to873* hw_flags earlier for i2c adapter/bus initialization874*/875break;876case CX18_HW_GPIO_MUX:877if (cx18_gpio_register(cx, device) == 0)878cx->hw_flags |= device;879break;880default:881if (cx18_i2c_register(cx, i) == 0)882cx->hw_flags |= device;883break;884}885}886887if (cx->hw_flags & CX18_HW_418_AV)888cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);889890if (cx->card->hw_muxer != 0)891cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);892}893894static int __devinit cx18_probe(struct pci_dev *pci_dev,895const struct pci_device_id *pci_id)896{897int retval = 0;898int i;899u32 devtype;900struct cx18 *cx;901902/* FIXME - module parameter arrays constrain max instances */903i = atomic_inc_return(&cx18_instance) - 1;904if (i >= CX18_MAX_CARDS) {905printk(KERN_ERR "cx18: cannot manage card %d, driver has a "906"limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);907return -ENOMEM;908}909910cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);911if (cx == NULL) {912printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",913i);914return -ENOMEM;915}916cx->pci_dev = pci_dev;917cx->instance = i;918919retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);920if (retval) {921printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"922"\n", cx->instance);923kfree(cx);924return retval;925}926snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",927cx->instance);928CX18_INFO("Initializing card %d\n", cx->instance);929930cx18_process_options(cx);931if (cx->options.cardtype == -1) {932retval = -ENODEV;933goto err;934}935936retval = cx18_init_struct1(cx);937if (retval)938goto err;939940CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);941942/* PCI Device Setup */943retval = cx18_setup_pci(cx, pci_dev, pci_id);944if (retval != 0)945goto free_workqueues;946947/* map io memory */948CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",949cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);950cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,951CX18_MEM_SIZE);952if (!cx->enc_mem) {953CX18_ERR("ioremap failed. Can't get a window into CX23418 "954"memory and register space\n");955CX18_ERR("Each capture card with a CX23418 needs 64 MB of "956"vmalloc address space for the window\n");957CX18_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");958CX18_ERR("Use the vmalloc= kernel command line option to set "959"VmallocTotal to a larger value\n");960retval = -ENOMEM;961goto free_mem;962}963cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;964devtype = cx18_read_reg(cx, 0xC72028);965switch (devtype & 0xff000000) {966case 0xff000000:967CX18_INFO("cx23418 revision %08x (A)\n", devtype);968break;969case 0x01000000:970CX18_INFO("cx23418 revision %08x (B)\n", devtype);971break;972default:973CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);974break;975}976977cx18_init_power(cx, 1);978cx18_init_memory(cx);979980cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);981cx18_init_scb(cx);982983cx18_gpio_init(cx);984985/* Initialize integrated A/V decoder early to set PLLs, just in case */986retval = cx18_av_probe(cx);987if (retval) {988CX18_ERR("Could not register A/V decoder subdevice\n");989goto free_map;990}991992/* Initialize GPIO Reset Controller to do chip resets during i2c init */993if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {994if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)995CX18_WARN("Could not register GPIO reset controller"996"subdevice; proceeding anyway.\n");997else998cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;999}10001001/* active i2c */1002CX18_DEBUG_INFO("activating i2c...\n");1003retval = init_cx18_i2c(cx);1004if (retval) {1005CX18_ERR("Could not initialize i2c\n");1006goto free_map;1007}10081009if (cx->card->hw_all & CX18_HW_TVEEPROM) {1010/* Based on the model number the cardtype may be changed.1011The PCI IDs are not always reliable. */1012const struct cx18_card *orig_card = cx->card;1013cx18_process_eeprom(cx);10141015if (cx->card != orig_card) {1016/* Changed the cardtype; re-reset the I2C chips */1017cx18_gpio_init(cx);1018cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,1019core, reset, (u32) CX18_GPIO_RESET_I2C);1020}1021}1022if (cx->card->comment)1023CX18_INFO("%s", cx->card->comment);1024if (cx->card->v4l2_capabilities == 0) {1025retval = -ENODEV;1026goto free_i2c;1027}1028cx18_init_memory(cx);1029cx18_init_scb(cx);10301031/* Register IRQ */1032retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,1033IRQF_SHARED | IRQF_DISABLED,1034cx->v4l2_dev.name, (void *)cx);1035if (retval) {1036CX18_ERR("Failed to register irq %d\n", retval);1037goto free_i2c;1038}10391040if (cx->std == 0)1041cx->std = V4L2_STD_NTSC_M;10421043if (cx->options.tuner == -1) {1044for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {1045if ((cx->std & cx->card->tuners[i].std) == 0)1046continue;1047cx->options.tuner = cx->card->tuners[i].tuner;1048break;1049}1050}1051/* if no tuner was found, then pick the first tuner in the card list */1052if (cx->options.tuner == -1 && cx->card->tuners[0].std) {1053cx->std = cx->card->tuners[0].std;1054if (cx->std & V4L2_STD_PAL)1055cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;1056else if (cx->std & V4L2_STD_NTSC)1057cx->std = V4L2_STD_NTSC_M;1058else if (cx->std & V4L2_STD_SECAM)1059cx->std = V4L2_STD_SECAM_L;1060cx->options.tuner = cx->card->tuners[0].tuner;1061}1062if (cx->options.radio == -1)1063cx->options.radio = (cx->card->radio_input.audio_type != 0);10641065/* The card is now fully identified, continue with card-specific1066initialization. */1067cx18_init_struct2(cx);10681069cx18_init_subdevs(cx);10701071if (cx->std & V4L2_STD_525_60)1072cx->is_60hz = 1;1073else1074cx->is_50hz = 1;10751076cx2341x_handler_set_50hz(&cx->cxhdl, !cx->is_60hz);10771078if (cx->options.radio > 0)1079cx->v4l2_cap |= V4L2_CAP_RADIO;10801081if (cx->options.tuner > -1) {1082struct tuner_setup setup;10831084setup.addr = ADDR_UNSET;1085setup.type = cx->options.tuner;1086setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */1087setup.tuner_callback = (setup.type == TUNER_XC2028) ?1088cx18_reset_tuner_gpio : NULL;1089cx18_call_all(cx, tuner, s_type_addr, &setup);1090if (setup.type == TUNER_XC2028) {1091static struct xc2028_ctrl ctrl = {1092.fname = XC2028_DEFAULT_FIRMWARE,1093.max_len = 64,1094};1095struct v4l2_priv_tun_config cfg = {1096.tuner = cx->options.tuner,1097.priv = &ctrl,1098};1099cx18_call_all(cx, tuner, s_config, &cfg);1100}1101}11021103/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)1104are not. */1105cx->tuner_std = cx->std;1106if (cx->std == V4L2_STD_ALL)1107cx->std = V4L2_STD_NTSC_M;11081109retval = cx18_streams_setup(cx);1110if (retval) {1111CX18_ERR("Error %d setting up streams\n", retval);1112goto free_irq;1113}1114retval = cx18_streams_register(cx);1115if (retval) {1116CX18_ERR("Error %d registering devices\n", retval);1117goto free_streams;1118}11191120CX18_INFO("Initialized card: %s\n", cx->card_name);11211122/* Load cx18 submodules (cx18-alsa) */1123request_modules(cx);1124return 0;11251126free_streams:1127cx18_streams_cleanup(cx, 1);1128free_irq:1129free_irq(cx->pci_dev->irq, (void *)cx);1130free_i2c:1131exit_cx18_i2c(cx);1132free_map:1133cx18_iounmap(cx);1134free_mem:1135release_mem_region(cx->base_addr, CX18_MEM_SIZE);1136free_workqueues:1137destroy_workqueue(cx->in_work_queue);1138err:1139if (retval == 0)1140retval = -ENODEV;1141CX18_ERR("Error %d on initialization\n", retval);11421143v4l2_device_unregister(&cx->v4l2_dev);1144kfree(cx);1145return retval;1146}11471148int cx18_init_on_first_open(struct cx18 *cx)1149{1150int video_input;1151int fw_retry_count = 3;1152struct v4l2_frequency vf;1153struct cx18_open_id fh;1154v4l2_std_id std;11551156fh.cx = cx;11571158if (test_bit(CX18_F_I_FAILED, &cx->i_flags))1159return -ENXIO;11601161if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))1162return 0;11631164while (--fw_retry_count > 0) {1165/* load firmware */1166if (cx18_firmware_init(cx) == 0)1167break;1168if (fw_retry_count > 1)1169CX18_WARN("Retry loading firmware\n");1170}11711172if (fw_retry_count == 0) {1173set_bit(CX18_F_I_FAILED, &cx->i_flags);1174return -ENXIO;1175}1176set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);11771178/*1179* Init the firmware twice to work around a silicon bug1180* with the digital TS.1181*1182* The second firmware load requires us to normalize the APU state,1183* or the audio for the first analog capture will be badly incorrect.1184*1185* I can't seem to call APU_RESETAI and have it succeed without the1186* APU capturing audio, so we start and stop it here to do the reset1187*/11881189/* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */1190cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);1191cx18_vapi(cx, CX18_APU_RESETAI, 0);1192cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);11931194fw_retry_count = 3;1195while (--fw_retry_count > 0) {1196/* load firmware */1197if (cx18_firmware_init(cx) == 0)1198break;1199if (fw_retry_count > 1)1200CX18_WARN("Retry loading firmware\n");1201}12021203if (fw_retry_count == 0) {1204set_bit(CX18_F_I_FAILED, &cx->i_flags);1205return -ENXIO;1206}12071208/*1209* The second firmware load requires us to normalize the APU state,1210* or the audio for the first analog capture will be badly incorrect.1211*1212* I can't seem to call APU_RESETAI and have it succeed without the1213* APU capturing audio, so we start and stop it here to do the reset1214*/12151216/* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */1217cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);1218cx18_vapi(cx, CX18_APU_RESETAI, 0);1219cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);12201221/* Init the A/V decoder, if it hasn't been already */1222v4l2_subdev_call(cx->sd_av, core, load_fw);12231224vf.tuner = 0;1225vf.type = V4L2_TUNER_ANALOG_TV;1226vf.frequency = 6400; /* the tuner 'baseline' frequency */12271228/* Set initial frequency. For PAL/SECAM broadcasts no1229'default' channel exists AFAIK. */1230if (cx->std == V4L2_STD_NTSC_M_JP)1231vf.frequency = 1460; /* ch. 1 91250*16/1000 */1232else if (cx->std & V4L2_STD_NTSC_M)1233vf.frequency = 1076; /* ch. 4 67250*16/1000 */12341235video_input = cx->active_input;1236cx->active_input++; /* Force update of input */1237cx18_s_input(NULL, &fh, video_input);12381239/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code1240in one place. */1241cx->std++; /* Force full standard initialization */1242std = (cx->tuner_std == V4L2_STD_ALL) ? V4L2_STD_NTSC_M : cx->tuner_std;1243cx18_s_std(NULL, &fh, &std);1244cx18_s_frequency(NULL, &fh, &vf);1245return 0;1246}12471248static void cx18_cancel_in_work_orders(struct cx18 *cx)1249{1250int i;1251for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)1252cancel_work_sync(&cx->in_work_order[i].work);1253}12541255static void cx18_cancel_out_work_orders(struct cx18 *cx)1256{1257int i;1258for (i = 0; i < CX18_MAX_STREAMS; i++)1259if (&cx->streams[i].video_dev != NULL)1260cancel_work_sync(&cx->streams[i].out_work_order);1261}12621263static void cx18_remove(struct pci_dev *pci_dev)1264{1265struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);1266struct cx18 *cx = to_cx18(v4l2_dev);1267int i;12681269CX18_DEBUG_INFO("Removing Card\n");12701271flush_request_modules(cx);12721273/* Stop all captures */1274CX18_DEBUG_INFO("Stopping all streams\n");1275if (atomic_read(&cx->tot_capturing) > 0)1276cx18_stop_all_captures(cx);12771278/* Stop interrupts that cause incoming work to be queued */1279cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);12801281/* Incoming work can cause outgoing work, so clean up incoming first */1282cx18_cancel_in_work_orders(cx);1283cx18_cancel_out_work_orders(cx);12841285/* Stop ack interrupts that may have been needed for work to finish */1286cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);12871288cx18_halt_firmware(cx);12891290destroy_workqueue(cx->in_work_queue);12911292cx18_streams_cleanup(cx, 1);12931294exit_cx18_i2c(cx);12951296free_irq(cx->pci_dev->irq, (void *)cx);12971298cx18_iounmap(cx);12991300release_mem_region(cx->base_addr, CX18_MEM_SIZE);13011302pci_disable_device(cx->pci_dev);13031304if (cx->vbi.sliced_mpeg_data[0] != NULL)1305for (i = 0; i < CX18_VBI_FRAMES; i++)1306kfree(cx->vbi.sliced_mpeg_data[i]);13071308v4l2_ctrl_handler_free(&cx->av_state.hdl);13091310CX18_INFO("Removed %s\n", cx->card_name);13111312v4l2_device_unregister(v4l2_dev);1313kfree(cx);1314}131513161317/* define a pci_driver for card detection */1318static struct pci_driver cx18_pci_driver = {1319.name = "cx18",1320.id_table = cx18_pci_tbl,1321.probe = cx18_probe,1322.remove = cx18_remove,1323};13241325static int __init module_start(void)1326{1327printk(KERN_INFO "cx18: Start initialization, version %s\n",1328CX18_VERSION);13291330/* Validate parameters */1331if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {1332printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",1333CX18_MAX_CARDS - 1);1334return -1;1335}13361337if (cx18_debug < 0 || cx18_debug > 511) {1338cx18_debug = 0;1339printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");1340}13411342if (pci_register_driver(&cx18_pci_driver)) {1343printk(KERN_ERR "cx18: Error detecting PCI card\n");1344return -ENODEV;1345}1346printk(KERN_INFO "cx18: End initialization\n");1347return 0;1348}13491350static void __exit module_cleanup(void)1351{1352pci_unregister_driver(&cx18_pci_driver);1353}13541355module_init(module_start);1356module_exit(module_cleanup);135713581359