cx18-driver.c 39.2 KB
Newer Older
1 2 3 4 5 6
/*
 *  cx18 driver initialization and card probing
 *
 *  Derived from ivtv-driver.c
 *
 *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7
 *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA
 */

#include "cx18-driver.h"
26
#include "cx18-io.h"
27 28 29 30 31 32
#include "cx18-version.h"
#include "cx18-cards.h"
#include "cx18-i2c.h"
#include "cx18-irq.h"
#include "cx18-gpio.h"
#include "cx18-firmware.h"
33
#include "cx18-queue.h"
34 35 36 37 38
#include "cx18-streams.h"
#include "cx18-av-core.h"
#include "cx18-scb.h"
#include "cx18-mailbox.h"
#include "cx18-ioctl.h"
39
#include "cx18-controls.h"
40
#include "tuner-xc2028.h"
41
#include <linux/dma-mapping.h>
42 43 44 45 46 47 48 49 50
#include <media/tveeprom.h>

/* If you have already X v4l cards, then set this to X. This way
   the device numbers stay matched. Example: you have a WinTV card
   without radio and a Compro H900 with. Normally this would give a
   video1 device together with a radio0 device for the Compro. By
   setting this to 1 you ensure that radio0 is now also radio1. */
int cx18_first_minor;

51 52 53 54
/* Callback for registering extensions */
int (*cx18_ext_init)(struct cx18 *);
EXPORT_SYMBOL(cx18_ext_init);

55
/* add your revision and whatnot here */
56
static struct pci_device_id cx18_pci_tbl[] = {
57 58 59 60 61 62 63
	{PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{0,}
};

MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);

64 65
static atomic_t cx18_instance = ATOMIC_INIT(0);

66 67 68 69 70 71 72 73 74 75
/* Parameter declarations */
static int cardtype[CX18_MAX_CARDS];
static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1 };
static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1,
				     -1, -1, -1, -1, -1, -1, -1, -1 };
76 77
static unsigned cardtype_c = 1;
static unsigned tuner_c = 1;
78
static unsigned radio_c = 1;
79 80 81 82 83 84
static char pal[] = "--";
static char secam[] = "--";
static char ntsc[] = "-";

/* Buffers */
static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
85 86
static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
87 88 89 90
static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;

91 92 93 94 95 96 97 98
static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;

static int enc_ts_bufs = -1;
static int enc_mpg_bufs = -1;
99
static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;
100 101 102 103 104
static int enc_yuv_bufs = -1;
static int enc_vbi_bufs = -1;
static int enc_pcm_bufs = -1;


105 106
static int cx18_pci_latency = 1;

107 108 109
static int mmio_ndelay;
static int retry_mmio = 1;

110 111 112
int cx18_debug;

module_param_array(tuner, int, &tuner_c, 0644);
113
module_param_array(radio, int, &radio_c, 0644);
114 115 116 117 118
module_param_array(cardtype, int, &cardtype_c, 0644);
module_param_string(pal, pal, sizeof(pal), 0644);
module_param_string(secam, secam, sizeof(secam), 0644);
module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
module_param_named(debug, cx18_debug, int, 0644);
119 120
module_param(mmio_ndelay, int, 0644);
module_param(retry_mmio, int, 0644);
121 122 123 124
module_param(cx18_pci_latency, int, 0644);
module_param(cx18_first_minor, int, 0644);

module_param(enc_ts_buffers, int, 0644);
125 126
module_param(enc_mpg_buffers, int, 0644);
module_param(enc_idx_buffers, int, 0644);
127 128 129 130
module_param(enc_yuv_buffers, int, 0644);
module_param(enc_vbi_buffers, int, 0644);
module_param(enc_pcm_buffers, int, 0644);

131 132 133 134 135 136 137 138 139 140 141 142 143
module_param(enc_ts_bufsize, int, 0644);
module_param(enc_mpg_bufsize, int, 0644);
module_param(enc_idx_bufsize, int, 0644);
module_param(enc_yuv_bufsize, int, 0644);
module_param(enc_pcm_bufsize, int, 0644);

module_param(enc_ts_bufs, int, 0644);
module_param(enc_mpg_bufs, int, 0644);
module_param(enc_idx_bufs, int, 0644);
module_param(enc_yuv_bufs, int, 0644);
module_param(enc_vbi_bufs, int, 0644);
module_param(enc_pcm_bufs, int, 0644);

144 145 146 147 148 149 150 151 152 153 154 155
MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
			"\t\t\tsee tuner.h for values");
MODULE_PARM_DESC(radio,
		 "Enable or disable the radio. Use only if autodetection\n"
		 "\t\t\tfails. 0 = disable, 1 = enable");
MODULE_PARM_DESC(cardtype,
		 "Only use this option if your card is not detected properly.\n"
		 "\t\tSpecify card type:\n"
		 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
		 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
		 "\t\t\t 3 = Compro VideoMate H900\n"
		 "\t\t\t 4 = Yuan MPC718\n"
156
		 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
157
		 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
158 159
		 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
		 "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
160
		 "\t\t\t 9 = GoTView PCI DVD3 Hybrid\n"
161
		 "\t\t\t 10 = Hauppauge HVR 1600 (S5H1411)\n"
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		 "\t\t\t 0 = Autodetect (default)\n"
		 "\t\t\t-1 = Ignore this card\n\t\t");
MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
MODULE_PARM_DESC(debug,
		 "Debug level (bitmask). Default: 0\n"
		 "\t\t\t  1/0x0001: warning\n"
		 "\t\t\t  2/0x0002: info\n"
		 "\t\t\t  4/0x0004: mailbox\n"
		 "\t\t\t  8/0x0008: dma\n"
		 "\t\t\t 16/0x0010: ioctl\n"
		 "\t\t\t 32/0x0020: file\n"
		 "\t\t\t 64/0x0040: i2c\n"
		 "\t\t\t128/0x0080: irq\n"
		 "\t\t\t256/0x0100: high volume\n");
MODULE_PARM_DESC(cx18_pci_latency,
		 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
		 "\t\t\tDefault: Yes");
181
MODULE_PARM_DESC(retry_mmio,
182 183
		 "(Deprecated) MMIO writes are now always checked and retried\n"
		 "\t\t\tEffectively: 1 [Yes]");
184
MODULE_PARM_DESC(mmio_ndelay,
185 186
		 "(Deprecated) MMIO accesses are now never purposely delayed\n"
		 "\t\t\tEffectively: 0 ns");
187
MODULE_PARM_DESC(enc_ts_buffers,
188
		 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
189
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
MODULE_PARM_DESC(enc_ts_bufsize,
		 "Size of an encoder TS buffer (kB)\n"
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
MODULE_PARM_DESC(enc_ts_bufs,
		 "Number of encoder TS buffers\n"
		 "\t\t\tDefault is computed from other enc_ts_* parameters");
MODULE_PARM_DESC(enc_mpg_buffers,
		 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
MODULE_PARM_DESC(enc_mpg_bufsize,
		 "Size of an encoder MPG buffer (kB)\n"
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
MODULE_PARM_DESC(enc_mpg_bufs,
		 "Number of encoder MPG buffers\n"
		 "\t\t\tDefault is computed from other enc_mpg_* parameters");
MODULE_PARM_DESC(enc_idx_buffers,
206 207 208
		 "(Deprecated) Encoder IDX buffer memory (MB)\n"
		 "\t\t\tIgnored, except 0 disables IDX buffer allocations\n"
		 "\t\t\tDefault: 1 [Enabled]");
209 210
MODULE_PARM_DESC(enc_idx_bufsize,
		 "Size of an encoder IDX buffer (kB)\n"
211 212 213
		 "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"
		 "\t\t\t(multiples of size required for 64 index entries)\n"
		 "\t\t\tDefault: 2");
214 215
MODULE_PARM_DESC(enc_idx_bufs,
		 "Number of encoder IDX buffers\n"
216
		 "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));
217
MODULE_PARM_DESC(enc_yuv_buffers,
218
		 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
219
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
220 221
MODULE_PARM_DESC(enc_yuv_bufsize,
		 "Size of an encoder YUV buffer (kB)\n"
222 223 224
		 "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
		 "\t\t\t(multiples of size required for 32 screen lines)\n"
		 "\t\t\tDefault: 102");
225 226 227
MODULE_PARM_DESC(enc_yuv_bufs,
		 "Number of encoder YUV buffers\n"
		 "\t\t\tDefault is computed from other enc_yuv_* parameters");
228
MODULE_PARM_DESC(enc_vbi_buffers,
229
		 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
230
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
231 232
MODULE_PARM_DESC(enc_vbi_bufs,
		 "Number of encoder VBI buffers\n"
233
		 "\t\t\tDefault is computed from enc_vbi_buffers");
234
MODULE_PARM_DESC(enc_pcm_buffers,
235
		 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
236
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
237 238 239 240 241 242
MODULE_PARM_DESC(enc_pcm_bufsize,
		 "Size of an encoder PCM buffer (kB)\n"
		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
MODULE_PARM_DESC(enc_pcm_bufs,
		 "Number of encoder PCM buffers\n"
		 "\t\t\tDefault is computed from other enc_pcm_* parameters");
243

244 245
MODULE_PARM_DESC(cx18_first_minor,
		 "Set device node number assigned to first card");
246 247 248 249 250 251 252 253

MODULE_AUTHOR("Hans Verkuil");
MODULE_DESCRIPTION("CX23418 driver");
MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
MODULE_LICENSE("GPL");

MODULE_VERSION(CX18_VERSION);

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
	struct cx18 *dev = container_of(work, struct cx18, request_module_wk);

	/* Make sure cx18-alsa module is loaded */
	request_module("cx18-alsa");

	/* Initialize cx18-alsa for this instance of the cx18 device */
	if (cx18_ext_init != NULL)
		cx18_ext_init(dev);
}

static void request_modules(struct cx18 *dev)
{
	INIT_WORK(&dev->request_module_wk, request_module_async);
	schedule_work(&dev->request_module_wk);
}
272 273 274

static void flush_request_modules(struct cx18 *dev)
{
275
	flush_work(&dev->request_module_wk);
276
}
277 278
#else
#define request_modules(dev)
279
#define flush_request_modules(dev)
280
#endif /* CONFIG_MODULES */
281

282 283 284
/* Generic utility functions */
int cx18_msleep_timeout(unsigned int msecs, int intr)
{
285
	long int timeout = msecs_to_jiffies(msecs);
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	int sig;

	do {
		set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
		timeout = schedule_timeout(timeout);
		sig = intr ? signal_pending(current) : 0;
	} while (!sig && timeout);
	return sig;
}

/* Release ioremapped memory */
static void cx18_iounmap(struct cx18 *cx)
{
	if (cx == NULL)
		return;

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

310 311 312 313 314 315 316 317 318 319 320 321 322 323
static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
{
	int i;

	CX18_INFO("eeprom dump:\n");
	for (i = 0; i < len; i++) {
		if (0 == (i % 16))
			CX18_INFO("eeprom %02x:", i);
		printk(KERN_CONT " %02x", eedata[i]);
		if (15 == (i % 16))
			printk(KERN_CONT "\n");
	}
}

324 325 326
/* Hauppauge card? get values from tveeprom */
void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
{
327
	struct i2c_client *c;
328 329
	u8 eedata[256];

330 331 332 333 334
	c = kzalloc(sizeof(*c), GFP_KERNEL);

	strlcpy(c->name, "cx18 tveeprom tmp", sizeof(c->name));
	c->adapter = &cx->i2c_adap[0];
	c->addr = 0xa0 >> 1;
335

336
	memset(tv, 0, sizeof(*tv));
337 338
	if (tveeprom_read(c, eedata, sizeof(eedata)))
		goto ret;
339 340 341 342

	switch (cx->card->type) {
	case CX18_CARD_HVR_1600_ESMT:
	case CX18_CARD_HVR_1600_SAMSUNG:
343
	case CX18_CARD_HVR_1600_S5H1411:
344
		tveeprom_hauppauge_analog(c, tv, eedata);
345 346
		break;
	case CX18_CARD_YUAN_MPC718:
347
	case CX18_CARD_GOTVIEW_PCI_DVD3:
348 349 350 351 352 353 354 355 356 357
		tv->model = 0x718;
		cx18_eeprom_dump(cx, eedata, sizeof(eedata));
		CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
			  eedata[2], eedata[1], eedata[4], eedata[3]);
		break;
	default:
		tv->model = 0xffffffff;
		cx18_eeprom_dump(cx, eedata, sizeof(eedata));
		break;
	}
358 359 360

ret:
	kfree(c);
361 362 363 364 365 366 367 368 369 370
}

static void cx18_process_eeprom(struct cx18 *cx)
{
	struct tveeprom tv;

	cx18_read_eeprom(cx, &tv);

	/* Many thanks to Steven Toth from Hauppauge for providing the
	   model numbers */
371 372 373
	/* Note: the Samsung memory models cannot be reliably determined
	   from the model number. Use the cardtype module option if you
	   have one of these preproduction models. */
374
	switch (tv.model) {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	case 74301: /* Retail models */
	case 74321:
	case 74351: /* OEM models */
	case 74361:
		/* Digital side is s5h1411/tda18271 */
		cx->card = cx18_get_card(CX18_CARD_HVR_1600_S5H1411);
		break;
	case 74021: /* Retail models */
	case 74031:
	case 74041:
	case 74141:
	case 74541: /* OEM models */
	case 74551:
	case 74591:
	case 74651:
	case 74691:
	case 74751:
	case 74891:
		/* Digital side is s5h1409/mxl5005s */
394 395
		cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
		break;
396 397 398 399 400
	case 0x718:
		return;
	case 0xffffffff:
		CX18_INFO("Unknown EEPROM encoding\n");
		return;
401 402 403 404
	case 0:
		CX18_ERR("Invalid EEPROM\n");
		return;
	default:
405 406
		CX18_ERR("Unknown model %d, defaulting to original HVR-1600 "
			 "(cardtype=1)\n", tv.model);
407 408 409 410 411 412 413 414 415 416 417
		cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
		break;
	}

	cx->v4l2_cap = cx->card->v4l2_capabilities;
	cx->card_name = cx->card->name;
	cx->card_i2c = cx->card->i2c;

	CX18_INFO("Autodetected %s\n", cx->card_name);

	if (tv.tuner_type == TUNER_ABSENT)
418
		CX18_ERR("tveeprom cannot autodetect tuner!\n");
419 420 421 422 423 424 425 426 427 428 429

	if (cx->options.tuner == -1)
		cx->options.tuner = tv.tuner_type;
	if (cx->options.radio == -1)
		cx->options.radio = (tv.has_radio != 0);

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

	/* autodetect tuner standard */
430 431 432 433 434 435 436 437 438 439
#define TVEEPROM_TUNER_FORMAT_ALL (V4L2_STD_B  | V4L2_STD_GH | \
				   V4L2_STD_MN | \
				   V4L2_STD_PAL_I | \
				   V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC | \
				   V4L2_STD_DK)
	if ((tv.tuner_formats & TVEEPROM_TUNER_FORMAT_ALL)
					== TVEEPROM_TUNER_FORMAT_ALL) {
		CX18_DEBUG_INFO("Worldwide tuner detected\n");
		cx->std = V4L2_STD_ALL;
	} else if (tv.tuner_formats & V4L2_STD_PAL) {
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
		CX18_DEBUG_INFO("PAL tuner detected\n");
		cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
	} else if (tv.tuner_formats & V4L2_STD_NTSC) {
		CX18_DEBUG_INFO("NTSC tuner detected\n");
		cx->std |= V4L2_STD_NTSC_M;
	} else if (tv.tuner_formats & V4L2_STD_SECAM) {
		CX18_DEBUG_INFO("SECAM tuner detected\n");
		cx->std |= V4L2_STD_SECAM_L;
	} else {
		CX18_INFO("No tuner detected, default to NTSC-M\n");
		cx->std |= V4L2_STD_NTSC_M;
	}
}

static v4l2_std_id cx18_parse_std(struct cx18 *cx)
{
	switch (pal[0]) {
	case '6':
		return V4L2_STD_PAL_60;
	case 'b':
	case 'B':
	case 'g':
	case 'G':
		return V4L2_STD_PAL_BG;
	case 'h':
	case 'H':
		return V4L2_STD_PAL_H;
	case 'n':
	case 'N':
		if (pal[1] == 'c' || pal[1] == 'C')
			return V4L2_STD_PAL_Nc;
		return V4L2_STD_PAL_N;
	case 'i':
	case 'I':
		return V4L2_STD_PAL_I;
	case 'd':
	case 'D':
	case 'k':
	case 'K':
		return V4L2_STD_PAL_DK;
	case 'M':
	case 'm':
		return V4L2_STD_PAL_M;
	case '-':
		break;
	default:
		CX18_WARN("pal= argument not recognised\n");
		return 0;
	}

	switch (secam[0]) {
	case 'b':
	case 'B':
	case 'g':
	case 'G':
	case 'h':
	case 'H':
		return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
	case 'd':
	case 'D':
	case 'k':
	case 'K':
		return V4L2_STD_SECAM_DK;
	case 'l':
	case 'L':
		if (secam[1] == 'C' || secam[1] == 'c')
			return V4L2_STD_SECAM_LC;
		return V4L2_STD_SECAM_L;
	case '-':
		break;
	default:
		CX18_WARN("secam= argument not recognised\n");
		return 0;
	}

	switch (ntsc[0]) {
	case 'm':
	case 'M':
		return V4L2_STD_NTSC_M;
	case 'j':
	case 'J':
		return V4L2_STD_NTSC_M_JP;
	case 'k':
	case 'K':
		return V4L2_STD_NTSC_M_KR;
	case '-':
		break;
	default:
		CX18_WARN("ntsc= argument not recognised\n");
		return 0;
	}

	/* no match found */
	return 0;
}

static void cx18_process_options(struct cx18 *cx)
{
	int i, j;

	cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
541 542
	cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
	cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
543 544 545
	cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
	cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
	cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
546 547 548 549 550 551 552 553 554 555 556 557 558 559
	cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */

	cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
	cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
	cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
	cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
	cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
	cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
	cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */

	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
560
	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;
561 562 563
	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */

564
	/* Ensure stream_buffers & stream_buf_size are valid */
565
	for (i = 0; i < CX18_MAX_STREAMS; i++) {
566 567 568
		if (cx->stream_buffers[i] == 0 ||     /* User said 0 buffers */
		    cx->options.megabytes[i] <= 0 ||  /* User said 0 MB total */
		    cx->stream_buf_size[i] <= 0) {    /* User said buf size 0 */
569 570 571 572 573
			cx->options.megabytes[i] = 0;
			cx->stream_buffers[i] = 0;
			cx->stream_buf_size[i] = 0;
			continue;
		}
574
		/*
575 576
		 * YUV is a special case where the stream_buf_size needs to be
		 * an integral multiple of 33.75 kB (storage for 32 screens
577 578 579 580 581 582
		 * lines to maintain alignment in case of lost buffers).
		 *
		 * IDX is a special case where the stream_buf_size should be
		 * an integral multiple of 1.5 kB (storage for 64 index entries
		 * to maintain alignment in case of lost buffers).
		 *
583 584 585 586 587 588 589 590 591
		 */
		if (i == CX18_ENC_STREAM_TYPE_YUV) {
			cx->stream_buf_size[i] *= 1024;
			cx->stream_buf_size[i] -=
			   (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);

			if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
				cx->stream_buf_size[i] =
						CX18_UNIT_ENC_YUV_BUFSIZE;
592 593 594 595 596 597 598 599
		} else if (i == CX18_ENC_STREAM_TYPE_IDX) {
			cx->stream_buf_size[i] *= 1024;
			cx->stream_buf_size[i] -=
			   (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);

			if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)
				cx->stream_buf_size[i] =
						CX18_UNIT_ENC_IDX_BUFSIZE;
600 601
		}
		/*
602
		 * YUV and IDX are special cases where the stream_buf_size is
603
		 * now in bytes.
604 605 606
		 * VBI is a special case where the stream_buf_size is fixed
		 * and already in bytes
		 */
607
		if (i == CX18_ENC_STREAM_TYPE_VBI ||
608 609
		    i == CX18_ENC_STREAM_TYPE_YUV ||
		    i == CX18_ENC_STREAM_TYPE_IDX) {
610 611 612 613 614 615 616 617 618
			if (cx->stream_buffers[i] < 0) {
				cx->stream_buffers[i] =
					cx->options.megabytes[i] * 1024 * 1024
					/ cx->stream_buf_size[i];
			} else {
				/* N.B. This might round down to 0 */
				cx->options.megabytes[i] =
					cx->stream_buffers[i]
					* cx->stream_buf_size[i]/(1024 * 1024);
619 620
			}
		} else {
621 622 623 624 625 626 627 628 629 630 631 632 633
			/* All other streams have stream_buf_size in kB here */
			if (cx->stream_buffers[i] < 0) {
				cx->stream_buffers[i] =
						cx->options.megabytes[i] * 1024
						/ cx->stream_buf_size[i];
			} else {
				/* N.B. This might round down to 0 */
				cx->options.megabytes[i] =
						cx->stream_buffers[i]
						* cx->stream_buf_size[i] / 1024;
			}
			/* convert from kB to bytes */
			cx->stream_buf_size[i] *= 1024;
634
		}
635 636 637
		CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
				"%d bytes\n", i, cx->options.megabytes[i],
				cx->stream_buffers[i], cx->stream_buf_size[i]);
638 639
	}

640 641 642
	cx->options.cardtype = cardtype[cx->instance];
	cx->options.tuner = tuner[cx->instance];
	cx->options.radio = radio[cx->instance];
643 644 645 646 647 648 649 650 651 652 653 654

	cx->std = cx18_parse_std(cx);
	if (cx->options.cardtype == -1) {
		CX18_INFO("Ignore card\n");
		return;
	}
	cx->card = cx18_get_card(cx->options.cardtype - 1);
	if (cx->card)
		CX18_INFO("User specified %s card\n", cx->card->name);
	else if (cx->options.cardtype != 0)
		CX18_ERR("Unknown user specified type, trying to autodetect card\n");
	if (cx->card == NULL) {
655
		if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
656 657 658 659 660 661 662 663 664
			cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
			CX18_INFO("Autodetected Hauppauge card\n");
		}
	}
	if (cx->card == NULL) {
		for (i = 0; (cx->card = cx18_get_card(i)); i++) {
			if (cx->card->pci_list == NULL)
				continue;
			for (j = 0; cx->card->pci_list[j].device; j++) {
665
				if (cx->pci_dev->device !=
666 667
				    cx->card->pci_list[j].device)
					continue;
668
				if (cx->pci_dev->subsystem_vendor !=
669 670
				    cx->card->pci_list[j].subsystem_vendor)
					continue;
671
				if (cx->pci_dev->subsystem_device !=
672 673 674 675 676 677 678 679 680 681 682
				    cx->card->pci_list[j].subsystem_device)
					continue;
				CX18_INFO("Autodetected %s card\n", cx->card->name);
				goto done;
			}
		}
	}
done:

	if (cx->card == NULL) {
		cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
683
		CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
684
			 cx->pci_dev->vendor, cx->pci_dev->device);
685
		CX18_ERR("              subsystem vendor/device: [%04x:%04x]\n",
686 687
			 cx->pci_dev->subsystem_vendor,
			 cx->pci_dev->subsystem_device);
688 689 690 691 692 693 694 695 696 697
		CX18_ERR("Defaulting to %s card\n", cx->card->name);
		CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
		CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
		CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
	}
	cx->v4l2_cap = cx->card->v4l2_capabilities;
	cx->card_name = cx->card->name;
	cx->card_i2c = cx->card->i2c;
}

698
static int cx18_create_in_workq(struct cx18 *cx)
699 700 701
{
	snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
		 cx->v4l2_dev.name);
702
	cx->in_work_queue = alloc_ordered_workqueue("%s", 0, cx->in_workq_name);
703 704 705 706 707 708 709
	if (cx->in_work_queue == NULL) {
		CX18_ERR("Unable to create incoming mailbox handler thread\n");
		return -ENOMEM;
	}
	return 0;
}

710
static void cx18_init_in_work_orders(struct cx18 *cx)
711 712 713 714 715 716 717 718 719
{
	int i;
	for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
		cx->in_work_order[i].cx = cx;
		cx->in_work_order[i].str = cx->epu_debug_str;
		INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
	}
}

720
/* Precondition: the cx18 structure has been memset to 0. Only
721
   the dev and instance fields have been filled in.
722 723 724
   No assumptions on the card type may be made here (see cx18_init_struct2
   for that).
 */
725
static int cx18_init_struct1(struct cx18 *cx)
726
{
727
	int ret;
728

729
	cx->base_addr = pci_resource_start(cx->pci_dev, 0);
730 731

	mutex_init(&cx->serialize_lock);
732
	mutex_init(&cx->gpio_lock);
733 734
	mutex_init(&cx->epu2apu_mb_lock);
	mutex_init(&cx->epu2cpu_mb_lock);
735

736
	ret = cx18_create_in_workq(cx);
737
	if (ret)
738
		return ret;
739

740 741
	cx18_init_in_work_orders(cx);

742 743 744 745
	/* start counting open_id at 1 */
	cx->open_id = 1;

	/* Initial settings */
746 747 748 749
	cx->cxhdl.port = CX2341X_PORT_MEMORY;
	cx->cxhdl.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
	cx->cxhdl.ops = &cx18_cxhdl_ops;
	cx->cxhdl.func = cx18_api_func;
750
	cx->cxhdl.priv = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
751 752 753 754 755 756 757 758 759 760 761
	ret = cx2341x_handler_init(&cx->cxhdl, 50);
	if (ret)
		return ret;
	cx->v4l2_dev.ctrl_handler = &cx->cxhdl.hdl;

	cx->temporal_strength = cx->cxhdl.video_temporal_filter->cur.val;
	cx->spatial_strength = cx->cxhdl.video_spatial_filter->cur.val;
	cx->filter_mode = cx->cxhdl.video_spatial_filter_mode->cur.val |
		(cx->cxhdl.video_temporal_filter_mode->cur.val << 1) |
		(cx->cxhdl.video_median_filter_type->cur.val << 2);

762 763 764 765 766 767
	init_waitqueue_head(&cx->cap_w);
	init_waitqueue_head(&cx->mb_apu_waitq);
	init_waitqueue_head(&cx->mb_cpu_waitq);
	init_waitqueue_head(&cx->dma_waitq);

	/* VBI */
768
	cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
769
	cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
770

771 772 773 774 775 776
	/* IVTV style VBI insertion into MPEG streams */
	INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
	INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
	INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
	list_add(&cx->vbi.sliced_mpeg_buf.list,
		 &cx->vbi.sliced_mpeg_mdl.buf_list);
777 778 779 780 781
	return 0;
}

/* Second initialization part. Here the card type has been
   autodetected. */
782
static void cx18_init_struct2(struct cx18 *cx)
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
{
	int i;

	for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
		if (cx->card->video_inputs[i].video_type == 0)
			break;
	cx->nof_inputs = i;
	for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
		if (cx->card->audio_inputs[i].audio_type == 0)
			break;
	cx->nof_audio_inputs = i;

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

807
static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
808 809 810 811 812 813 814
			  const struct pci_device_id *pci_id)
{
	u16 cmd;
	unsigned char pci_latency;

	CX18_DEBUG_INFO("Enabling pci device\n");

815
	if (pci_enable_device(pci_dev)) {
816
		CX18_ERR("Can't enable device %d!\n", cx->instance);
817 818
		return -EIO;
	}
819
	if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) {
820
		CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
821 822 823
		return -EIO;
	}
	if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
824 825
		CX18_ERR("Cannot request encoder memory region, card %d\n",
			 cx->instance);
826 827 828
		return -EIO;
	}

829
	/* Enable bus mastering and memory mapped IO for the CX23418 */
830
	pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
831
	cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
832
	pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
833

B
Bjørn Mork 已提交
834
	cx->card_rev = pci_dev->revision;
835
	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
836 837 838 839

	if (pci_latency < 64 && cx18_pci_latency) {
		CX18_INFO("Unreasonably low latency timer, "
			       "setting to 64 (was %d)\n", pci_latency);
840 841
		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
		pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
842 843 844
	}

	CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
845
		   "irq: %d, latency: %d, memory: 0x%llx\n",
846 847
		   cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
		   PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
848
		   cx->pci_dev->irq, pci_latency, (u64)cx->base_addr);
849 850 851 852

	return 0;
}

853
static void cx18_init_subdevs(struct cx18 *cx)
854 855
{
	u32 hw = cx->card->hw_all;
856
	u32 device;
857 858
	int i;

859
	for (i = 0, device = 1; i < 32; i++, device <<= 1) {
860 861 862

		if (!(device & hw))
			continue;
863 864 865 866 867

		switch (device) {
		case CX18_HW_DVB:
		case CX18_HW_TVEEPROM:
			/* These subordinate devices do not use probing */
868
			cx->hw_flags |= device;
869 870 871 872
			break;
		case CX18_HW_418_AV:
			/* The A/V decoder gets probed earlier to set PLLs */
			/* Just note that the card uses it (i.e. has analog) */
873
			cx->hw_flags |= device;
874
			break;
875 876 877 878 879 880 881 882 883 884
		case CX18_HW_GPIO_RESET_CTRL:
			/*
			 * The Reset Controller gets probed and added to
			 * hw_flags earlier for i2c adapter/bus initialization
			 */
			break;
		case CX18_HW_GPIO_MUX:
			if (cx18_gpio_register(cx, device) == 0)
				cx->hw_flags |= device;
			break;
885 886 887 888 889
		default:
			if (cx18_i2c_register(cx, i) == 0)
				cx->hw_flags |= device;
			break;
		}
890 891
	}

892 893 894 895 896
	if (cx->hw_flags & CX18_HW_418_AV)
		cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);

	if (cx->card->hw_muxer != 0)
		cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
897 898
}

899 900
static int cx18_probe(struct pci_dev *pci_dev,
		      const struct pci_device_id *pci_id)
901 902
{
	int retval = 0;
903
	int i;
904 905 906
	u32 devtype;
	struct cx18 *cx;

907 908 909 910 911
	/* FIXME - module parameter arrays constrain max instances */
	i = atomic_inc_return(&cx18_instance) - 1;
	if (i >= CX18_MAX_CARDS) {
		printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
		       "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
912 913 914 915
		return -ENOMEM;
	}

	cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
916 917 918
	if (cx == NULL) {
		printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
		       i);
919 920
		return -ENOMEM;
	}
921
	cx->pci_dev = pci_dev;
922 923
	cx->instance = i;

924 925
	retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
	if (retval) {
926 927 928 929
		printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
		       "\n", cx->instance);
		kfree(cx);
		return retval;
930
	}
931 932 933
	snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
		 cx->instance);
	CX18_INFO("Initializing card %d\n", cx->instance);
934

935 936 937
	cx18_process_options(cx);
	if (cx->options.cardtype == -1) {
		retval = -ENODEV;
938
		goto err;
939
	}
940 941 942

	retval = cx18_init_struct1(cx);
	if (retval)
943
		goto err;
944

945
	CX18_DEBUG_INFO("base addr: 0x%llx\n", (u64)cx->base_addr);
946 947

	/* PCI Device Setup */
948
	retval = cx18_setup_pci(cx, pci_dev, pci_id);
949
	if (retval != 0)
950
		goto free_workqueues;
951

952
	/* map io memory */
953 954
	CX18_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
		   (u64)cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
955 956 957
	cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
				       CX18_MEM_SIZE);
	if (!cx->enc_mem) {
958 959 960 961 962 963 964
		CX18_ERR("ioremap failed. Can't get a window into CX23418 "
			 "memory and register space\n");
		CX18_ERR("Each capture card with a CX23418 needs 64 MB of "
			 "vmalloc address space for the window\n");
		CX18_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
		CX18_ERR("Use the vmalloc= kernel command line option to set "
			 "VmallocTotal to a larger value\n");
965 966 967 968
		retval = -ENOMEM;
		goto free_mem;
	}
	cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
969
	devtype = cx18_read_reg(cx, 0xC72028);
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	switch (devtype & 0xff000000) {
	case 0xff000000:
		CX18_INFO("cx23418 revision %08x (A)\n", devtype);
		break;
	case 0x01000000:
		CX18_INFO("cx23418 revision %08x (B)\n", devtype);
		break;
	default:
		CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
		break;
	}

	cx18_init_power(cx, 1);
	cx18_init_memory(cx);

A
Al Viro 已提交
985
	cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
986 987 988 989
	cx18_init_scb(cx);

	cx18_gpio_init(cx);

990 991
	/* Initialize integrated A/V decoder early to set PLLs, just in case */
	retval = cx18_av_probe(cx);
992 993 994 995 996
	if (retval) {
		CX18_ERR("Could not register A/V decoder subdevice\n");
		goto free_map;
	}

997 998 999 1000 1001 1002 1003 1004 1005
	/* Initialize GPIO Reset Controller to do chip resets during i2c init */
	if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
		if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
			CX18_WARN("Could not register GPIO reset controller"
				  "subdevice; proceeding anyway.\n");
		else
			cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
	}

1006 1007
	/* active i2c  */
	CX18_DEBUG_INFO("activating i2c...\n");
1008 1009
	retval = init_cx18_i2c(cx);
	if (retval) {
1010 1011 1012 1013 1014 1015 1016
		CX18_ERR("Could not initialize i2c\n");
		goto free_map;
	}

	if (cx->card->hw_all & CX18_HW_TVEEPROM) {
		/* Based on the model number the cardtype may be changed.
		   The PCI IDs are not always reliable. */
1017
		const struct cx18_card *orig_card = cx->card;
1018
		cx18_process_eeprom(cx);
1019 1020 1021 1022 1023 1024 1025

		if (cx->card != orig_card) {
			/* Changed the cardtype; re-reset the I2C chips */
			cx18_gpio_init(cx);
			cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,
					core, reset, (u32) CX18_GPIO_RESET_I2C);
		}
1026 1027 1028 1029 1030 1031 1032 1033
	}
	if (cx->card->comment)
		CX18_INFO("%s", cx->card->comment);
	if (cx->card->v4l2_capabilities == 0) {
		retval = -ENODEV;
		goto free_i2c;
	}
	cx18_init_memory(cx);
1034
	cx18_init_scb(cx);
1035 1036

	/* Register IRQ */
1037
	retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
1038
			     IRQF_SHARED, cx->v4l2_dev.name, (void *)cx);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	if (retval) {
		CX18_ERR("Failed to register irq %d\n", retval);
		goto free_i2c;
	}

	if (cx->std == 0)
		cx->std = V4L2_STD_NTSC_M;

	if (cx->options.tuner == -1) {
		for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
			if ((cx->std & cx->card->tuners[i].std) == 0)
				continue;
			cx->options.tuner = cx->card->tuners[i].tuner;
			break;
		}
	}
	/* if no tuner was found, then pick the first tuner in the card list */
	if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
		cx->std = cx->card->tuners[0].std;
1058 1059 1060 1061 1062 1063
		if (cx->std & V4L2_STD_PAL)
			cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
		else if (cx->std & V4L2_STD_NTSC)
			cx->std = V4L2_STD_NTSC_M;
		else if (cx->std & V4L2_STD_SECAM)
			cx->std = V4L2_STD_SECAM_L;
1064 1065 1066 1067 1068 1069 1070 1071 1072
		cx->options.tuner = cx->card->tuners[0].tuner;
	}
	if (cx->options.radio == -1)
		cx->options.radio = (cx->card->radio_input.audio_type != 0);

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

1073
	cx18_init_subdevs(cx);
1074

1075
	if (cx->std & V4L2_STD_525_60)
1076
		cx->is_60hz = 1;
1077
	else
1078
		cx->is_50hz = 1;
1079

1080
	cx2341x_handler_set_50hz(&cx->cxhdl, !cx->is_60hz);
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

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

	if (cx->options.tuner > -1) {
		struct tuner_setup setup;

		setup.addr = ADDR_UNSET;
		setup.type = cx->options.tuner;
		setup.mode_mask = T_ANALOG_TV;  /* matches TV tuners */
A
Andy Walls 已提交
1091 1092
		if (cx->options.radio > 0)
			setup.mode_mask |= T_RADIO;
1093 1094
		setup.tuner_callback = (setup.type == TUNER_XC2028) ?
			cx18_reset_tuner_gpio : NULL;
1095
		cx18_call_all(cx, tuner, s_type_addr, &setup);
1096 1097 1098 1099 1100 1101 1102 1103 1104
		if (setup.type == TUNER_XC2028) {
			static struct xc2028_ctrl ctrl = {
				.fname = XC2028_DEFAULT_FIRMWARE,
				.max_len = 64,
			};
			struct v4l2_priv_tun_config cfg = {
				.tuner = cx->options.tuner,
				.priv = &ctrl,
			};
1105
			cx18_call_all(cx, tuner, s_config, &cfg);
1106 1107 1108 1109 1110 1111
		}
	}

	/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
	   are not. */
	cx->tuner_std = cx->std;
1112 1113
	if (cx->std == V4L2_STD_ALL)
		cx->std = V4L2_STD_NTSC_M;
1114

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	retval = cx18_streams_setup(cx);
	if (retval) {
		CX18_ERR("Error %d setting up streams\n", retval);
		goto free_irq;
	}
	retval = cx18_streams_register(cx);
	if (retval) {
		CX18_ERR("Error %d registering devices\n", retval);
		goto free_streams;
	}
1125

1126
	CX18_INFO("Initialized card: %s\n", cx->card_name);
1127 1128 1129

	/* Load cx18 submodules (cx18-alsa) */
	request_modules(cx);
1130 1131 1132
	return 0;

free_streams:
1133
	cx18_streams_cleanup(cx, 1);
1134
free_irq:
1135
	free_irq(cx->pci_dev->irq, (void *)cx);
1136 1137 1138 1139 1140 1141
free_i2c:
	exit_cx18_i2c(cx);
free_map:
	cx18_iounmap(cx);
free_mem:
	release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1142
free_workqueues:
1143
	destroy_workqueue(cx->in_work_queue);
1144 1145 1146 1147 1148
err:
	if (retval == 0)
		retval = -ENODEV;
	CX18_ERR("Error %d on initialization\n", retval);

1149 1150
	v4l2_device_unregister(&cx->v4l2_dev);
	kfree(cx);
1151 1152 1153 1154 1155 1156 1157 1158
	return retval;
}

int cx18_init_on_first_open(struct cx18 *cx)
{
	int video_input;
	int fw_retry_count = 3;
	struct v4l2_frequency vf;
1159
	struct cx18_open_id fh;
1160
	v4l2_std_id std;
1161 1162

	fh.cx = cx;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
		return -ENXIO;

	if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
		return 0;

	while (--fw_retry_count > 0) {
		/* load firmware */
		if (cx18_firmware_init(cx) == 0)
			break;
		if (fw_retry_count > 1)
			CX18_WARN("Retry loading firmware\n");
	}

	if (fw_retry_count == 0) {
		set_bit(CX18_F_I_FAILED, &cx->i_flags);
		return -ENXIO;
	}
	set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	/*
	 * Init the firmware twice to work around a silicon bug
	 * with the digital TS.
	 *
	 * The second firmware load requires us to normalize the APU state,
	 * or the audio for the first analog capture will be badly incorrect.
	 *
	 * I can't seem to call APU_RESETAI and have it succeed without the
	 * APU capturing audio, so we start and stop it here to do the reset
	 */

	/* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
	cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
	cx18_vapi(cx, CX18_APU_RESETAI, 0);
	cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213

	fw_retry_count = 3;
	while (--fw_retry_count > 0) {
		/* load firmware */
		if (cx18_firmware_init(cx) == 0)
			break;
		if (fw_retry_count > 1)
			CX18_WARN("Retry loading firmware\n");
	}

	if (fw_retry_count == 0) {
		set_bit(CX18_F_I_FAILED, &cx->i_flags);
		return -ENXIO;
	}

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	/*
	 * The second firmware load requires us to normalize the APU state,
	 * or the audio for the first analog capture will be badly incorrect.
	 *
	 * I can't seem to call APU_RESETAI and have it succeed without the
	 * APU capturing audio, so we start and stop it here to do the reset
	 */

	/* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
	cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
	cx18_vapi(cx, CX18_APU_RESETAI, 0);
	cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);

1227
	/* Init the A/V decoder, if it hasn't been already */
1228
	v4l2_subdev_call(cx->sd_av, core, load_fw);
1229

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
	vf.tuner = 0;
	vf.type = V4L2_TUNER_ANALOG_TV;
	vf.frequency = 6400; /* the tuner 'baseline' frequency */

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

	video_input = cx->active_input;
	cx->active_input++;	/* Force update of input */
1243
	cx18_s_input(NULL, &fh, video_input);
1244 1245 1246 1247

	/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
	   in one place. */
	cx->std++;		/* Force full standard initialization */
1248
	std = (cx->tuner_std == V4L2_STD_ALL) ? V4L2_STD_NTSC_M : cx->tuner_std;
1249
	cx18_s_std(NULL, &fh, std);
1250
	cx18_s_frequency(NULL, &fh, &vf);
1251 1252 1253
	return 0;
}

1254
static void cx18_cancel_in_work_orders(struct cx18 *cx)
1255 1256
{
	int i;
1257 1258
	for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
		cancel_work_sync(&cx->in_work_order[i].work);
1259 1260
}

1261 1262 1263 1264 1265 1266 1267 1268
static void cx18_cancel_out_work_orders(struct cx18 *cx)
{
	int i;
	for (i = 0; i < CX18_MAX_STREAMS; i++)
		if (&cx->streams[i].video_dev != NULL)
			cancel_work_sync(&cx->streams[i].out_work_order);
}

1269 1270
static void cx18_remove(struct pci_dev *pci_dev)
{
1271
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1272
	struct cx18 *cx = to_cx18(v4l2_dev);
1273
	int i;
1274

1275
	CX18_DEBUG_INFO("Removing Card\n");
1276

1277 1278
	flush_request_modules(cx);

1279 1280
	/* Stop all captures */
	CX18_DEBUG_INFO("Stopping all streams\n");
1281
	if (atomic_read(&cx->tot_capturing) > 0)
1282 1283
		cx18_stop_all_captures(cx);

1284
	/* Stop interrupts that cause incoming work to be queued */
1285
	cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
1286 1287 1288

	/* Incoming work can cause outgoing work, so clean up incoming first */
	cx18_cancel_in_work_orders(cx);
1289
	cx18_cancel_out_work_orders(cx);
1290 1291

	/* Stop ack interrupts that may have been needed for work to finish */
1292
	cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
1293 1294 1295

	cx18_halt_firmware(cx);

1296
	destroy_workqueue(cx->in_work_queue);
1297

1298
	cx18_streams_cleanup(cx, 1);
1299 1300 1301

	exit_cx18_i2c(cx);

1302
	free_irq(cx->pci_dev->irq, (void *)cx);
1303

1304
	cx18_iounmap(cx);
1305 1306 1307

	release_mem_region(cx->base_addr, CX18_MEM_SIZE);

1308
	pci_disable_device(cx->pci_dev);
1309 1310 1311 1312

	if (cx->vbi.sliced_mpeg_data[0] != NULL)
		for (i = 0; i < CX18_VBI_FRAMES; i++)
			kfree(cx->vbi.sliced_mpeg_data[i]);
1313

1314 1315
	v4l2_ctrl_handler_free(&cx->av_state.hdl);

1316
	CX18_INFO("Removed %s\n", cx->card_name);
1317

1318 1319
	v4l2_device_unregister(v4l2_dev);
	kfree(cx);
1320 1321
}

1322

1323 1324 1325 1326 1327 1328 1329 1330
/* define a pci_driver for card detection */
static struct pci_driver cx18_pci_driver = {
      .name =     "cx18",
      .id_table = cx18_pci_tbl,
      .probe =    cx18_probe,
      .remove =   cx18_remove,
};

1331
static int __init module_start(void)
1332
{
1333 1334
	printk(KERN_INFO "cx18:  Start initialization, version %s\n",
	       CX18_VERSION);
1335 1336 1337

	/* Validate parameters */
	if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
1338
		printk(KERN_ERR "cx18:  Exiting, cx18_first_minor must be between 0 and %d\n",
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
		     CX18_MAX_CARDS - 1);
		return -1;
	}

	if (cx18_debug < 0 || cx18_debug > 511) {
		cx18_debug = 0;
		printk(KERN_INFO "cx18:   Debug value must be >= 0 and <= 511!\n");
	}

	if (pci_register_driver(&cx18_pci_driver)) {
		printk(KERN_ERR "cx18:   Error detecting PCI card\n");
		return -ENODEV;
	}
	printk(KERN_INFO "cx18:  End initialization\n");
	return 0;
}

1356
static void __exit module_cleanup(void)
1357 1358 1359 1360 1361 1362
{
	pci_unregister_driver(&cx18_pci_driver);
}

module_init(module_start);
module_exit(module_cleanup);
1363
MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE);