cx88-blackbird.c 40.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *
 *  Support for a cx23416 mpeg encoder via cx2388x host port.
 *  "blackbird" reference design.
 *
 *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
 *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
 *
 *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/firmware.h>
33 34
#include <media/v4l2-common.h>
#include <media/cx2341x.h>
L
Linus Torvalds 已提交
35 36 37 38

#include "cx88.h"

MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
39
MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
L
Linus Torvalds 已提交
40 41
MODULE_LICENSE("GPL");

42
static unsigned int mpegbufs = 32;
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
module_param(mpegbufs,int,0644);
MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");

static unsigned int debug = 0;
module_param(debug,int,0644);
MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");

#define dprintk(level,fmt, arg...)	if (debug >= level) \
	printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)


/* ------------------------------------------------------------------ */

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

struct cx88_tvnorm {
	char                   *name;
	v4l2_std_id            id;
	u32                    cxiformat;
	u32                    cxoformat;
};

static struct cx88_tvnorm tvnorms[] = {
	{
		.name      = "NTSC-M",
		.id        = V4L2_STD_NTSC_M,
		.cxiformat = VideoFormatNTSC,
		.cxoformat = 0x181f0008,
	},{
		.name      = "NTSC-JP",
		.id        = V4L2_STD_NTSC_M_JP,
		.cxiformat = VideoFormatNTSCJapan,
		.cxoformat = 0x181f0008,
	},{
		.name      = "PAL-BG",
		.id        = V4L2_STD_PAL_BG,
		.cxiformat = VideoFormatPAL,
		.cxoformat = 0x181f0008,
	},{
		.name      = "PAL-DK",
		.id        = V4L2_STD_PAL_DK,
		.cxiformat = VideoFormatPAL,
		.cxoformat = 0x181f0008,
	},{
		.name      = "PAL-I",
		.id        = V4L2_STD_PAL_I,
		.cxiformat = VideoFormatPAL,
		.cxoformat = 0x181f0008,
	},{
		.name      = "PAL-M",
		.id        = V4L2_STD_PAL_M,
		.cxiformat = VideoFormatPALM,
		.cxoformat = 0x1c1f0008,
	},{
		.name      = "PAL-N",
		.id        = V4L2_STD_PAL_N,
		.cxiformat = VideoFormatPALN,
		.cxoformat = 0x1c1f0008,
	},{
		.name      = "PAL-Nc",
		.id        = V4L2_STD_PAL_Nc,
		.cxiformat = VideoFormatPALNC,
		.cxoformat = 0x1c1f0008,
	},{
		.name      = "PAL-60",
		.id        = V4L2_STD_PAL_60,
		.cxiformat = VideoFormatPAL60,
		.cxoformat = 0x181f0008,
	},{
		.name      = "SECAM-L",
		.id        = V4L2_STD_SECAM_L,
		.cxiformat = VideoFormatSECAM,
		.cxoformat = 0x181f0008,
	},{
		.name      = "SECAM-DK",
		.id        = V4L2_STD_SECAM_DK,
		.cxiformat = VideoFormatSECAM,
		.cxoformat = 0x181f0008,
	}
};
int cx88_do_ioctl( struct inode *inode, struct file *file,
		   int radio, struct cx88_core *core, unsigned int cmd,
		   void *arg, v4l2_kioctl driver_ioctl );

L
Linus Torvalds 已提交
126 127 128 129 130 131
#define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024

/* defines below are from ivtv-driver.h */

#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
/* Firmware API commands */
#define IVTV_API_STD_TIMEOUT 500

enum blackbird_capture_type {
	BLACKBIRD_MPEG_CAPTURE,
	BLACKBIRD_RAW_CAPTURE,
	BLACKBIRD_RAW_PASSTHRU_CAPTURE
};
enum blackbird_capture_bits {
	BLACKBIRD_RAW_BITS_NONE             = 0x00,
	BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,
	BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,
	BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,
	BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
	BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
};
enum blackbird_capture_end {
	BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
	BLACKBIRD_END_NOW, /* stop immediately, no irq */
};
enum blackbird_framerate {
	BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
	BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
};
enum blackbird_stream_port {
	BLACKBIRD_OUTPUT_PORT_MEMORY,
	BLACKBIRD_OUTPUT_PORT_STREAMING,
	BLACKBIRD_OUTPUT_PORT_SERIAL
};
enum blackbird_data_xfer_status {
	BLACKBIRD_MORE_BUFFERS_FOLLOW,
	BLACKBIRD_LAST_BUFFER,
};
enum blackbird_picture_mask {
	BLACKBIRD_PICTURE_MASK_NONE,
	BLACKBIRD_PICTURE_MASK_I_FRAMES,
	BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
	BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
};
enum blackbird_vbi_mode_bits {
	BLACKBIRD_VBI_BITS_SLICED,
	BLACKBIRD_VBI_BITS_RAW,
};
enum blackbird_vbi_insertion_bits {
	BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
	BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
	BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
	BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
};
enum blackbird_dma_unit {
	BLACKBIRD_DMA_BYTES,
	BLACKBIRD_DMA_FRAMES,
};
enum blackbird_dma_transfer_status_bits {
	BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
	BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
	BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
};
enum blackbird_pause {
	BLACKBIRD_PAUSE_ENCODING,
	BLACKBIRD_RESUME_ENCODING,
};
enum blackbird_copyright {
	BLACKBIRD_COPYRIGHT_OFF,
	BLACKBIRD_COPYRIGHT_ON,
};
enum blackbird_notification_type {
	BLACKBIRD_NOTIFICATION_REFRESH,
};
enum blackbird_notification_status {
	BLACKBIRD_NOTIFICATION_OFF,
	BLACKBIRD_NOTIFICATION_ON,
};
enum blackbird_notification_mailbox {
	BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
};
enum blackbird_field1_lines {
	BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
	BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
	BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
};
enum blackbird_field2_lines {
	BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
	BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
	BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
};
enum blackbird_custom_data_type {
	BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
	BLACKBIRD_CUSTOM_PRIVATE_PACKET,
};
enum blackbird_mute {
	BLACKBIRD_UNMUTE,
	BLACKBIRD_MUTE,
};
enum blackbird_mute_video_mask {
	BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
	BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
	BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
};
enum blackbird_mute_video_shift {
	BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
	BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
	BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
};
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

/* Registers */
#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
#define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
#define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
#define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
#define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)

/* ------------------------------------------------------------------ */

static void host_setup(struct cx88_core *core)
{
	/* toggle reset of the host */
	cx_write(MO_GPHST_SOFT_RST, 1);
	udelay(100);
	cx_write(MO_GPHST_SOFT_RST, 0);
	udelay(100);

	/* host port setup */
	cx_write(MO_GPHST_WSC, 0x44444444U);
	cx_write(MO_GPHST_XFR, 0);
	cx_write(MO_GPHST_WDTH, 15);
	cx_write(MO_GPHST_HDSHK, 0);
	cx_write(MO_GPHST_MUX16, 0x44448888U);
	cx_write(MO_GPHST_MODE, 0);
}

/* ------------------------------------------------------------------ */

#define P1_MDATA0 0x390000
#define P1_MDATA1 0x390001
#define P1_MDATA2 0x390002
#define P1_MDATA3 0x390003
#define P1_MADDR2 0x390004
#define P1_MADDR1 0x390005
#define P1_MADDR0 0x390006
#define P1_RDATA0 0x390008
#define P1_RDATA1 0x390009
#define P1_RDATA2 0x39000A
#define P1_RDATA3 0x39000B
#define P1_RADDR0 0x39000C
#define P1_RADDR1 0x39000D
#define P1_RRDWR  0x39000E

static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
{
	unsigned long timeout = jiffies + msecs_to_jiffies(1);
	u32 gpio0,need;

	need = state ? 2 : 0;
	for (;;) {
		gpio0 = cx_read(MO_GP0_IO) & 2;
		if (need == gpio0)
			return 0;
		if (time_after(jiffies,timeout))
			return -1;
		udelay(1);
	}
}

static int memory_write(struct cx88_core *core, u32 address, u32 value)
{
	/* Warning: address is dword address (4 bytes) */
	cx_writeb(P1_MDATA0, (unsigned int)value);
	cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
	cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
	cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
	cx_writeb(P1_MADDR0, (unsigned int)address);
	cx_read(P1_MDATA0);
	cx_read(P1_MADDR0);

	return wait_ready_gpio0_bit1(core,1);
}

static int memory_read(struct cx88_core *core, u32 address, u32 *value)
{
316
	int retval;
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	u32 val;

	/* Warning: address is dword address (4 bytes) */
	cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
	cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
	cx_writeb(P1_MADDR0, (unsigned int)address);
	cx_read(P1_MADDR0);

	retval = wait_ready_gpio0_bit1(core,1);

	cx_writeb(P1_MDATA3, 0);
	val     = (unsigned char)cx_read(P1_MDATA3) << 24;
	cx_writeb(P1_MDATA2, 0);
	val    |= (unsigned char)cx_read(P1_MDATA2) << 16;
	cx_writeb(P1_MDATA1, 0);
	val    |= (unsigned char)cx_read(P1_MDATA1) << 8;
	cx_writeb(P1_MDATA0, 0);
	val    |= (unsigned char)cx_read(P1_MDATA0);

	*value  = val;
	return retval;
}

static int register_write(struct cx88_core *core, u32 address, u32 value)
{
	cx_writeb(P1_RDATA0, (unsigned int)value);
	cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
	cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
	cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
	cx_writeb(P1_RADDR0, (unsigned int)address);
	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
	cx_writeb(P1_RRDWR, 1);
	cx_read(P1_RDATA0);
	cx_read(P1_RADDR0);

	return wait_ready_gpio0_bit1(core,1);
}


static int register_read(struct cx88_core *core, u32 address, u32 *value)
{
	int retval;
	u32 val;

	cx_writeb(P1_RADDR0, (unsigned int)address);
	cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
	cx_writeb(P1_RRDWR, 0);
	cx_read(P1_RADDR0);

	retval  = wait_ready_gpio0_bit1(core,1);
	val     = (unsigned char)cx_read(P1_RDATA0);
	val    |= (unsigned char)cx_read(P1_RDATA1) << 8;
	val    |= (unsigned char)cx_read(P1_RDATA2) << 16;
	val    |= (unsigned char)cx_read(P1_RDATA3) << 24;

	*value  = val;
	return retval;
}

/* ------------------------------------------------------------------ */

378
static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
L
Linus Torvalds 已提交
379
{
380
	struct cx8802_dev *dev = priv;
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	unsigned long timeout;
	u32 value, flag, retval;
	int i;

	dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);

	/* this may not be 100% safe if we can't read any memory location
	   without side effects */
	memory_read(dev->core, dev->mailbox - 4, &value);
	if (value != 0x12345678) {
		dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
		return -1;
	}

	memory_read(dev->core, dev->mailbox, &flag);
	if (flag) {
		dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
		return -1;
	}

	flag |= 1; /* tell 'em we're working on it */
	memory_write(dev->core, dev->mailbox, flag);

	/* write command + args + fill remaining with zeros */
	memory_write(dev->core, dev->mailbox + 1, command); /* command code */
	memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
407 408 409
	for (i = 0; i < in; i++) {
		memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
		dprintk(1, "API Input %d = %d\n", i, data[i]);
L
Linus Torvalds 已提交
410
	}
411
	for (; i < CX2341X_MBOX_MAX_DATA; i++)
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
		memory_write(dev->core, dev->mailbox + 4 + i, 0);

	flag |= 3; /* tell 'em we're done writing */
	memory_write(dev->core, dev->mailbox, flag);

	/* wait for firmware to handle the API command */
	timeout = jiffies + msecs_to_jiffies(10);
	for (;;) {
		memory_read(dev->core, dev->mailbox, &flag);
		if (0 != (flag & 4))
			break;
		if (time_after(jiffies,timeout)) {
			dprintk(0, "ERROR: API Mailbox timeout\n");
			return -1;
		}
		udelay(10);
	}

	/* read output values */
431 432 433
	for (i = 0; i < out; i++) {
		memory_read(dev->core, dev->mailbox + 4 + i, data + i);
		dprintk(1, "API Output %d = %d\n", i, data[i]);
L
Linus Torvalds 已提交
434 435 436 437 438 439 440 441 442
	}

	memory_read(dev->core, dev->mailbox + 2, &retval);
	dprintk(1, "API result = %d\n",retval);

	flag = 0;
	memory_write(dev->core, dev->mailbox, flag);
	return retval;
}
443
/* ------------------------------------------------------------------ */
L
Linus Torvalds 已提交
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
/* We don't need to call the API often, so using just one mailbox will probably suffice */
static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
			     u32 inputcnt, u32 outputcnt, ...)
{
	u32 data[CX2341X_MBOX_MAX_DATA];
	va_list vargs;
	int i, err;

	va_start(vargs, outputcnt);

	for (i = 0; i < inputcnt; i++) {
		data[i] = va_arg(vargs, int);
	}
	err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
	for (i = 0; i < outputcnt; i++) {
		int *vptr = va_arg(vargs, int *);
		*vptr = data[i];
	}
	va_end(vargs);
	return err;
}
L
Linus Torvalds 已提交
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

static int blackbird_find_mailbox(struct cx8802_dev *dev)
{
	u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
	int signaturecnt=0;
	u32 value;
	int i;

	for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
		memory_read(dev->core, i, &value);
		if (value == signature[signaturecnt])
			signaturecnt++;
		else
			signaturecnt = 0;
		if (4 == signaturecnt) {
			dprintk(1, "Mailbox signature found\n");
			return i+1;
		}
	}
	dprintk(0, "Mailbox signature values not found!\n");
	return -1;
}

static int blackbird_load_firmware(struct cx8802_dev *dev)
{
	static const unsigned char magic[8] = {
		0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
	};
	const struct firmware *firmware;
	int i, retval = 0;
	u32 value = 0;
	u32 checksum = 0;
	u32 *dataptr;

	retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
501 502 503
	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
	retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
L
Linus Torvalds 已提交
504
	msleep(1);
505
	retval |= register_write(dev->core, IVTV_REG_APU, 0);
L
Linus Torvalds 已提交
506 507 508 509

	if (retval < 0)
		dprintk(0, "Error with register_write\n");

510
	retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
L
Linus Torvalds 已提交
511
				  &dev->pci->dev);
512 513


L
Linus Torvalds 已提交
514 515
	if (retval != 0) {
		dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
516
			CX2341X_FIRM_ENC_FILENAME);
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524
		dprintk(0, "Please fix your hotplug setup, the board will "
			"not work without firmware loaded!\n");
		return -1;
	}

	if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
		dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
			firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
M
Magnus Damm 已提交
525
		release_firmware(firmware);
L
Linus Torvalds 已提交
526 527 528 529 530
		return -1;
	}

	if (0 != memcmp(firmware->data, magic, 8)) {
		dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
M
Magnus Damm 已提交
531
		release_firmware(firmware);
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
		return -1;
	}

	/* transfer to the chip */
	dprintk(1,"Loading firmware ...\n");
	dataptr = (u32*)firmware->data;
	for (i = 0; i < (firmware->size >> 2); i++) {
		value = *dataptr;
		checksum += ~value;
		memory_write(dev->core, i, value);
		dataptr++;
	}

	/* read back to verify with the checksum */
	for (i--; i >= 0; i--) {
		memory_read(dev->core, i, &value);
		checksum -= ~value;
	}
	if (checksum) {
		dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
M
Magnus Damm 已提交
552
		release_firmware(firmware);
L
Linus Torvalds 已提交
553 554 555 556 557
		return -1;
	}
	release_firmware(firmware);
	dprintk(0, "Firmware upload successful.\n");

558 559 560
	retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
	retval |= register_read(dev->core, IVTV_REG_SPU, &value);
	retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
L
Linus Torvalds 已提交
561 562 563
	msleep(1);

	retval |= register_read(dev->core, IVTV_REG_VPU, &value);
564
	retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
L
Linus Torvalds 已提交
565 566 567 568 569 570

	if (retval < 0)
		dprintk(0, "Error with register_write\n");
	return 0;
}

571 572 573 574 575 576 577 578 579 580 581 582 583
/**
 Settings used by the windows tv app for PVR2000:
=================================================================================================================
Profile | Codec | Resolution | CBR/VBR | Video Qlty   | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
-----------------------------------------------------------------------------------------------------------------
MPEG-1  | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 2000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
MPEG-2  | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 4000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
VCD     | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 1150 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
DVD     | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
=================================================================================================================
*DB: "DirectBurn"
*/
584

585 586 587 588 589 590 591 592
static void blackbird_codec_settings(struct cx8802_dev *dev)
{
	/* assign frame size */
	blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
				dev->height, dev->width);

	dev->params.width = dev->width;
	dev->params.height = dev->height;
593
	dev->params.is_50hz = (dev->core->tvnorm & V4L2_STD_625_50) != 0;
594 595 596 597

	cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
static struct v4l2_mpeg_compression default_mpeg_params = {
	.st_type          = V4L2_MPEG_PS_2,
	.st_bitrate       = {
		.mode     = V4L2_BITRATE_CBR,
		.min      = 0,
		.target   = 0,
		.max      = 0
	},
	.ts_pid_pmt       = 16,
	.ts_pid_audio     = 260,
	.ts_pid_video     = 256,
	.ts_pid_pcr       = 259,
	.ps_size          = 0,
	.au_type          = V4L2_MPEG_AU_2_II,
	.au_bitrate       = {
		.mode     = V4L2_BITRATE_CBR,
		.min      = 224,
		.target   = 224,
		.max      = 224
	},
618
	.au_sample_rate    = 48000,
619 620 621 622 623 624 625 626 627 628
	.au_pesid          = 0,
	.vi_type           = V4L2_MPEG_VI_2,
	.vi_aspect_ratio   = V4L2_MPEG_ASPECT_4_3,
	.vi_bitrate        = {
		.mode      = V4L2_BITRATE_CBR,
		.min       = 4000,
		.target    = 4500,
		.max       = 6000
	},
	.vi_frame_rate     = 25,
629
	.vi_frames_per_gop = 12,
630 631
	.vi_bframes_count  = 2,
	.vi_pesid          = 0,
632
	.closed_gops       = 1,
633 634 635
	.pulldown          = 0
};

L
Linus Torvalds 已提交
636 637 638 639 640 641 642
static int blackbird_initialize_codec(struct cx8802_dev *dev)
{
	struct cx88_core *core = dev->core;
	int version;
	int retval;

	dprintk(1,"Initialize codec\n");
643
	retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657
	if (retval < 0) {
		/* ping was not successful, reset and upload firmware */
		cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
		msleep(1);
		cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
		msleep(1);
		retval = blackbird_load_firmware(dev);
		if (retval < 0)
			return retval;

		dev->mailbox = blackbird_find_mailbox(dev);
		if (dev->mailbox < 0)
			return -1;

658
		retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
L
Linus Torvalds 已提交
659 660 661 662 663
		if (retval < 0) {
			dprintk(0, "ERROR: Firmware ping failed!\n");
			return -1;
		}

664
		retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
		if (retval < 0) {
			dprintk(0, "ERROR: Firmware get encoder version failed!\n");
			return -1;
		}
		dprintk(0, "Firmware version is 0x%08x\n", version);
	}
	msleep(1);

	cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
	cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
	cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
	cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */

	blackbird_codec_settings(dev);
	msleep(1);

681 682 683
	/* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
	   blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
	   blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
684
	blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
685
			BLACKBIRD_FIELD1_SAA7115,
686
			BLACKBIRD_FIELD2_SAA7115
687 688 689
		);

	/* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
690
	blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
691 692
			BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
L
Linus Torvalds 已提交
693

694
	/* initialize the video input */
695
	blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
L
Linus Torvalds 已提交
696 697 698

	msleep(1);

699
	blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
L
Linus Torvalds 已提交
700
	msleep(1);
701
	blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
L
Linus Torvalds 已提交
702 703
	msleep(1);

704
	/* start capturing to the host interface */
705 706
	/* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
	blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
707 708
			BLACKBIRD_MPEG_CAPTURE,
			BLACKBIRD_RAW_BITS_NONE
709
		);
710
	msleep(10);
L
Linus Torvalds 已提交
711

712
	blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720 721 722
	return 0;
}

/* ------------------------------------------------------------------ */

static int bb_buf_setup(struct videobuf_queue *q,
			unsigned int *count, unsigned int *size)
{
	struct cx8802_fh *fh = q->priv_data;

723
	fh->dev->ts_packet_size  = 188 * 4; /* was: 512 */
724
	fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
L
Linus Torvalds 已提交
725 726

	*size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
727
	*count = fh->dev->ts_packet_count;
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735
	return 0;
}

static int
bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
	       enum v4l2_field field)
{
	struct cx8802_fh *fh = q->priv_data;
736
	return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747 748
}

static void
bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
	struct cx8802_fh *fh = q->priv_data;
	cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
}

static void
bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
749
	cx88_free_buffer(q, (struct cx88_buffer*)vb);
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759 760
}

static struct videobuf_queue_ops blackbird_qops = {
	.buf_setup    = bb_buf_setup,
	.buf_prepare  = bb_buf_prepare,
	.buf_queue    = bb_buf_queue,
	.buf_release  = bb_buf_release,
};

/* ------------------------------------------------------------------ */

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
static const u32 *ctrl_classes[] = {
	cx88_user_ctrls,
	cx2341x_mpeg_ctrls,
	NULL
};

static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
{
	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
	if (qctrl->id == 0)
		return -EINVAL;

	/* Standard V4L2 controls */
	if (cx8800_ctrl_query(qctrl) == 0)
		return 0;

	/* MPEG V4L2 controls */
	if (cx2341x_ctrl_query(&dev->params, qctrl))
		qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
	return 0;
}

static int blackbird_querymenu(struct cx8802_dev *dev, struct v4l2_querymenu *qmenu)
{
	struct v4l2_queryctrl qctrl;

	qctrl.id = qmenu->id;
	blackbird_queryctrl(dev, &qctrl);
	return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
}

/* ------------------------------------------------------------------ */

L
Linus Torvalds 已提交
794 795 796 797 798
static int mpeg_do_ioctl(struct inode *inode, struct file *file,
			 unsigned int cmd, void *arg)
{
	struct cx8802_fh  *fh  = file->private_data;
	struct cx8802_dev *dev = fh->dev;
799
	struct cx88_core  *core = dev->core;
L
Linus Torvalds 已提交
800 801

	if (debug > 1)
802
		v4l_print_ioctl(core->name,cmd);
L
Linus Torvalds 已提交
803 804 805

	switch (cmd) {

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	/* --- capabilities ------------------------------------------ */
	case VIDIOC_QUERYCAP:
	{
		struct v4l2_capability *cap = arg;

		memset(cap,0,sizeof(*cap));
		strcpy(cap->driver, "cx88_blackbird");
		strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
		sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
		cap->version = CX88_VERSION_CODE;
		cap->capabilities =
			V4L2_CAP_VIDEO_CAPTURE |
			V4L2_CAP_READWRITE     |
			V4L2_CAP_STREAMING     |
			0;
		if (UNSET != core->tuner_type)
			cap->capabilities |= V4L2_CAP_TUNER;

		return 0;
	}

L
Linus Torvalds 已提交
827 828 829 830 831 832 833 834 835 836 837 838
	/* --- capture ioctls ---------------------------------------- */
	case VIDIOC_ENUM_FMT:
	{
		struct v4l2_fmtdesc *f = arg;
		int index;

		index = f->index;
		if (index != 0)
			return -EINVAL;

		memset(f,0,sizeof(*f));
		f->index = index;
839
		strlcpy(f->description, "MPEG", sizeof(f->description));
L
Linus Torvalds 已提交
840 841 842 843 844 845 846 847 848 849
		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		f->pixelformat = V4L2_PIX_FMT_MPEG;
		return 0;
	}
	case VIDIOC_G_FMT:
	{
		struct v4l2_format *f = arg;

		memset(f,0,sizeof(*f));
		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
850 851 852 853
		f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
		f->fmt.pix.bytesperline = 0;
		f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
		f->fmt.pix.colorspace   = 0;
L
Linus Torvalds 已提交
854 855
		f->fmt.pix.width        = dev->width;
		f->fmt.pix.height       = dev->height;
856 857 858 859 860 861 862 863 864 865
		f->fmt.pix.field        = fh->mpegq.field;
		dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
			dev->width, dev->height, fh->mpegq.field );
		return 0;
	}
	case VIDIOC_TRY_FMT:
	{
		struct v4l2_format *f = arg;

		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
L
Linus Torvalds 已提交
866
		f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
867
		f->fmt.pix.bytesperline = 0;
868
		f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
869
		f->fmt.pix.colorspace   = 0;
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
		dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
			dev->width, dev->height, fh->mpegq.field );
		return 0;
	}
	case VIDIOC_S_FMT:
	{
		struct v4l2_format *f = arg;

		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
		f->fmt.pix.bytesperline = 0;
		f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
		f->fmt.pix.colorspace   = 0;
		dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
			f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
885
		return 0;
L
Linus Torvalds 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
	}

	/* --- streaming capture ------------------------------------- */
	case VIDIOC_REQBUFS:
		return videobuf_reqbufs(&fh->mpegq, arg);

	case VIDIOC_QUERYBUF:
		return videobuf_querybuf(&fh->mpegq, arg);

	case VIDIOC_QBUF:
		return videobuf_qbuf(&fh->mpegq, arg);

	case VIDIOC_DQBUF:
		return videobuf_dqbuf(&fh->mpegq, arg,
				      file->f_flags & O_NONBLOCK);

	case VIDIOC_STREAMON:
		return videobuf_streamon(&fh->mpegq);

	case VIDIOC_STREAMOFF:
		return videobuf_streamoff(&fh->mpegq);

908 909 910 911 912
	/* --- mpeg compression -------------------------------------- */
	case VIDIOC_G_MPEGCOMP:
	{
		struct v4l2_mpeg_compression *f = arg;

913 914
		printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
				    "Replace with VIDIOC_G_EXT_CTRLS!");
915
		memcpy(f,&default_mpeg_params,sizeof(*f));
916 917 918
		return 0;
	}
	case VIDIOC_S_MPEGCOMP:
919 920
		printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
				    "Replace with VIDIOC_S_EXT_CTRLS!");
921 922
		return 0;
	case VIDIOC_G_EXT_CTRLS:
923
	{
924
		struct v4l2_ext_controls *f = arg;
925

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
		if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
			return -EINVAL;
		return cx2341x_ext_ctrls(&dev->params, f, cmd);
	}
	case VIDIOC_S_EXT_CTRLS:
	case VIDIOC_TRY_EXT_CTRLS:
	{
		struct v4l2_ext_controls *f = arg;
		struct cx2341x_mpeg_params p;
		int err;

		if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
			return -EINVAL;
		p = dev->params;
		err = cx2341x_ext_ctrls(&p, f, cmd);
		if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) {
			err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
			dev->params = p;
		}
		return err;
946
	}
947 948 949 950 951 952 953
	case VIDIOC_S_FREQUENCY:
	{
		blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
				  BLACKBIRD_END_NOW,
				  BLACKBIRD_MPEG_CAPTURE,
				  BLACKBIRD_RAW_BITS_NONE);

954
		cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
955 956 957 958 959 960

		blackbird_initialize_codec(dev);
		cx88_set_scale(dev->core, dev->width, dev->height,
			       fh->mpegq.field);
		return 0;
	}
961
	case VIDIOC_LOG_STATUS:
962 963 964 965
	{
		char name[32 + 2];

		snprintf(name, sizeof(name), "%s/2", core->name);
966 967
		printk("%s/2: ============  START LOG STATUS  ============\n",
		       core->name);
968
		cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
969
		cx2341x_log_status(&dev->params, name);
970 971 972
		printk("%s/2: =============  END LOG STATUS  =============\n",
		       core->name);
		return 0;
973
	}
974 975 976 977 978 979 980 981 982 983
	case VIDIOC_QUERYMENU:
		return blackbird_querymenu(dev, arg);
	case VIDIOC_QUERYCTRL:
	{
		struct v4l2_queryctrl *c = arg;

		if (blackbird_queryctrl(dev, c) == 0)
			return 0;
		return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
	}
984

L
Linus Torvalds 已提交
985
	default:
986
		return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
L
Linus Torvalds 已提交
987 988 989 990
	}
	return 0;
}

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
		  struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
{
	int err;

       if (debug) {
	       if (debug > 1) {
		       if (_IOC_DIR(cmd) & _IOC_WRITE)
			       v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
		       else if (!_IOC_DIR(cmd) & _IOC_READ) {
			       v4l_print_ioctl("cx88", cmd);
		       }
	       } else
		       v4l_print_ioctl(core->name,cmd);

       }

	switch (cmd) {
	/* ---------- tv norms ---------- */
	case VIDIOC_ENUMSTD:
	{
		struct v4l2_standard *e = arg;
		unsigned int i;

		i = e->index;
		if (i >= ARRAY_SIZE(tvnorms))
			return -EINVAL;
		err = v4l2_video_std_construct(e, tvnorms[e->index].id,
					       tvnorms[e->index].name);
		e->index = i;
		if (err < 0)
			return err;
		return 0;
	}
	case VIDIOC_G_STD:
	{
		v4l2_std_id *id = arg;

		*id = core->tvnorm;
		return 0;
	}
	case VIDIOC_S_STD:
	{
		v4l2_std_id *id = arg;
		unsigned int i;

		for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
			if (*id & tvnorms[i].id)
				break;
		if (i == ARRAY_SIZE(tvnorms))
			return -EINVAL;

		mutex_lock(&core->lock);
		cx88_set_tvnorm(core,tvnorms[i].id);
		mutex_unlock(&core->lock);
		return 0;
	}

	/* ------ input switching ---------- */
	case VIDIOC_ENUMINPUT:
	{
		static const char *iname[] = {
			[ CX88_VMUX_COMPOSITE1 ] = "Composite1",
			[ CX88_VMUX_COMPOSITE2 ] = "Composite2",
			[ CX88_VMUX_COMPOSITE3 ] = "Composite3",
			[ CX88_VMUX_COMPOSITE4 ] = "Composite4",
			[ CX88_VMUX_SVIDEO     ] = "S-Video",
			[ CX88_VMUX_TELEVISION ] = "Television",
			[ CX88_VMUX_CABLE      ] = "Cable TV",
			[ CX88_VMUX_DVB        ] = "DVB",
			[ CX88_VMUX_DEBUG      ] = "for debug only",
		};
		struct v4l2_input *i = arg;
		unsigned int n;

		n = i->index;
		if (n >= 4)
			return -EINVAL;
		if (0 == INPUT(n)->type)
			return -EINVAL;
		memset(i,0,sizeof(*i));
		i->index = n;
		i->type  = V4L2_INPUT_TYPE_CAMERA;
		strcpy(i->name,iname[INPUT(n)->type]);
		if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
		    (CX88_VMUX_CABLE      == INPUT(n)->type))
			i->type = V4L2_INPUT_TYPE_TUNER;
		for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
			i->std |= tvnorms[n].id;
		return 0;
	}
	case VIDIOC_G_INPUT:
	{
		unsigned int *i = arg;

		*i = core->input;
		return 0;
	}
	case VIDIOC_S_INPUT:
	{
		unsigned int *i = arg;

		if (*i >= 4)
			return -EINVAL;
		mutex_lock(&core->lock);
		cx88_newstation(core);
		cx88_video_mux(core,*i);
		mutex_unlock(&core->lock);
		return 0;
	}



	/* --- controls ---------------------------------------------- */
	case VIDIOC_QUERYCTRL:
	{
		struct v4l2_queryctrl *qctrl = arg;

		qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
			if (unlikely(qctrl->id == 0))
				return -EINVAL;
		return cx8800_ctrl_query(qctrl);
	}
	case VIDIOC_G_CTRL:
		return cx88_get_control(core,arg);
	case VIDIOC_S_CTRL:
		return cx88_set_control(core,arg);

	/* --- tuner ioctls ------------------------------------------ */
	case VIDIOC_G_TUNER:
	{
		struct v4l2_tuner *t = arg;
		u32 reg;

		if (UNSET == core->tuner_type)
			return -EINVAL;
		if (0 != t->index)
			return -EINVAL;

		memset(t,0,sizeof(*t));
		strcpy(t->name, "Television");
		t->type       = V4L2_TUNER_ANALOG_TV;
		t->capability = V4L2_TUNER_CAP_NORM;
		t->rangehigh  = 0xffffffffUL;

		cx88_get_stereo(core ,t);
		reg = cx_read(MO_DEVICE_STATUS);
		t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
		return 0;
	}
	case VIDIOC_S_TUNER:
	{
		struct v4l2_tuner *t = arg;

		if (UNSET == core->tuner_type)
			return -EINVAL;
		if (0 != t->index)
			return -EINVAL;
		cx88_set_stereo(core, t->audmode, 1);
		return 0;
	}
	case VIDIOC_G_FREQUENCY:
	{
		struct v4l2_frequency *f = arg;

		memset(f,0,sizeof(*f));

		if (UNSET == core->tuner_type)
			return -EINVAL;

		/* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
		f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
		f->frequency = core->freq;

		cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);

		return 0;
	}
	case VIDIOC_S_FREQUENCY:
	{
		struct v4l2_frequency *f = arg;

		if (UNSET == core->tuner_type)
			return -EINVAL;
		if (f->tuner != 0)
			return -EINVAL;
		if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
			return -EINVAL;
		if (1 == radio && f->type != V4L2_TUNER_RADIO)
			return -EINVAL;
		mutex_lock(&core->lock);
		core->freq = f->frequency;
		cx88_newstation(core);
		cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);

		/* When changing channels it is required to reset TVAUDIO */
		msleep (10);
		cx88_set_tvaudio(core);

		mutex_unlock(&core->lock);
		return 0;
	}
#ifdef CONFIG_VIDEO_ADV_DEBUG
	/* ioctls to allow direct acces to the cx2388x registers */
	case VIDIOC_INT_G_REGISTER:
	{
		struct v4l2_register *reg = arg;

		if (reg->i2c_id != 0)
			return -EINVAL;
		/* cx2388x has a 24-bit register space */
		reg->val = cx_read(reg->reg&0xffffff);
		return 0;
	}
	case VIDIOC_INT_S_REGISTER:
	{
		struct v4l2_register *reg = arg;

		if (reg->i2c_id != 0)
			return -EINVAL;
		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		cx_write(reg->reg&0xffffff, reg->val);
		return 0;
	}
#endif

	default:
		return v4l_compat_translate_ioctl(inode,file,cmd,arg,
						  driver_ioctl);
	}
	return 0;
}

1225 1226 1227 1228
int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
			unsigned int cmd, void *arg);
unsigned int (*cx88_ioctl_translator)(unsigned int cmd);

1229 1230 1231 1232 1233
static unsigned int mpeg_translate_ioctl(unsigned int cmd)
{
	return cmd;
}

L
Linus Torvalds 已提交
1234
static int mpeg_ioctl(struct inode *inode, struct file *file,
1235
			unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1236
{
1237 1238
	cmd = cx88_ioctl_translator( cmd );
	return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
L
Linus Torvalds 已提交
1239 1240 1241 1242 1243
}

static int mpeg_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
1244
	struct cx8802_dev *dev = NULL;
L
Linus Torvalds 已提交
1245
	struct cx8802_fh *fh;
1246 1247
	struct cx8802_driver *drv = NULL;
	int err;
L
Linus Torvalds 已提交
1248

1249 1250
       dev = cx8802_get_device(inode);

1251 1252 1253
	dprintk( 1, "%s\n", __FUNCTION__);

	if (dev == NULL)
L
Linus Torvalds 已提交
1254 1255
		return -ENODEV;

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	/* Make sure we can acquire the hardware */
	drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
	if (drv) {
		err = drv->request_acquire(drv);
		if(err != 0) {
			dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err);
			return err;
		}
	}

	if (blackbird_initialize_codec(dev) < 0) {
		if (drv)
			drv->request_release(drv);
L
Linus Torvalds 已提交
1269
		return -EINVAL;
1270
	}
L
Linus Torvalds 已提交
1271 1272 1273
	dprintk(1,"open minor=%d\n",minor);

	/* allocate + initialize per filehandle data */
P
 
Panagiotis Issaris 已提交
1274
	fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1275 1276 1277
	if (NULL == fh) {
		if (drv)
			drv->request_release(drv);
L
Linus Torvalds 已提交
1278
		return -ENOMEM;
1279
	}
L
Linus Torvalds 已提交
1280 1281 1282 1283 1284 1285
	file->private_data = fh;
	fh->dev      = dev;

	videobuf_queue_init(&fh->mpegq, &blackbird_qops,
			    dev->pci, &dev->slock,
			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
1286
			    V4L2_FIELD_INTERLACED,
L
Linus Torvalds 已提交
1287 1288
			    sizeof(struct cx88_buffer),
			    fh);
1289 1290 1291 1292 1293

	/* FIXME: locking against other video device */
	cx88_set_scale(dev->core, dev->width, dev->height,
			fh->mpegq.field);

L
Linus Torvalds 已提交
1294 1295 1296 1297 1298 1299
	return 0;
}

static int mpeg_release(struct inode *inode, struct file *file)
{
	struct cx8802_fh  *fh  = file->private_data;
1300 1301
	struct cx8802_dev *dev = NULL;
	struct cx8802_driver *drv = NULL;
L
Linus Torvalds 已提交
1302

1303 1304
	/* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
	blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1305 1306 1307 1308
			BLACKBIRD_END_NOW,
			BLACKBIRD_MPEG_CAPTURE,
			BLACKBIRD_RAW_BITS_NONE
		);
L
Linus Torvalds 已提交
1309

1310
	cx8802_cancel_buffers(fh->dev);
L
Linus Torvalds 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319
	/* stop mpeg capture */
	if (fh->mpegq.streaming)
		videobuf_streamoff(&fh->mpegq);
	if (fh->mpegq.reading)
		videobuf_read_stop(&fh->mpegq);

	videobuf_mmap_free(&fh->mpegq);
	file->private_data = NULL;
	kfree(fh);
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

	/* Make sure we release the hardware */
	dev = cx8802_get_device(inode);
	if (dev == NULL)
		return -ENODEV;

	drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
	if (drv)
		drv->request_release(drv);

L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	return 0;
}

static ssize_t
mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
	struct cx8802_fh *fh = file->private_data;

	return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
				    file->f_flags & O_NONBLOCK);
}

static unsigned int
mpeg_poll(struct file *file, struct poll_table_struct *wait)
{
	struct cx8802_fh *fh = file->private_data;

	return videobuf_poll_stream(file, &fh->mpegq, wait);
}

static int
mpeg_mmap(struct file *file, struct vm_area_struct * vma)
{
	struct cx8802_fh *fh = file->private_data;

	return videobuf_mmap_mapper(&fh->mpegq, vma);
}

1358
static const struct file_operations mpeg_fops =
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
{
	.owner	       = THIS_MODULE,
	.open	       = mpeg_open,
	.release       = mpeg_release,
	.read	       = mpeg_read,
	.poll          = mpeg_poll,
	.mmap	       = mpeg_mmap,
	.ioctl	       = mpeg_ioctl,
	.llseek        = no_llseek,
};

static struct video_device cx8802_mpeg_template =
{
	.name          = "cx8802",
	.type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
	.hardware      = 0,
	.fops          = &mpeg_fops,
	.minor         = -1,
};

/* ------------------------------------------------------------------ */

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
/* The CX8802 MPEG API will call this when we can use the hardware */
static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
{
	struct cx88_core *core = drv->core;
	int err = 0;

	switch (core->board) {
	case CX88_BOARD_HAUPPAUGE_HVR1300:
		/* By default, core setup will leave the cx22702 out of reset, on the bus.
		 * We left the hardware on power up with the cx22702 active.
		 * We're being given access to re-arrange the GPIOs.
		 * Take the bus off the cx22702 and put the cx23416 on it.
		 */
		cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
		cx_set(MO_GP0_IO,   0x00000004); /* Disable the cx22702 */
		break;
	default:
		err = -ENODEV;
	}
	return err;
}

/* The CX8802 MPEG API will call this when we need to release the hardware */
static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
{
	struct cx88_core *core = drv->core;
	int err = 0;

	switch (core->board) {
	case CX88_BOARD_HAUPPAUGE_HVR1300:
		/* Exit leaving the cx23416 on the bus */
		break;
	default:
		err = -ENODEV;
	}
	return err;
}

L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
static void blackbird_unregister_video(struct cx8802_dev *dev)
{
	if (dev->mpeg_dev) {
		if (-1 != dev->mpeg_dev->minor)
			video_unregister_device(dev->mpeg_dev);
		else
			video_device_release(dev->mpeg_dev);
		dev->mpeg_dev = NULL;
	}
}

static int blackbird_register_video(struct cx8802_dev *dev)
{
	int err;

	dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
				       &cx8802_mpeg_template,"mpeg");
	err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
	if (err < 0) {
		printk(KERN_INFO "%s/2: can't register mpeg device\n",
		       dev->core->name);
		return err;
	}
	printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
	       dev->core->name,dev->mpeg_dev->minor & 0x1f);
	return 0;
}

/* ----------------------------------------------------------- */

1449
static int cx8802_blackbird_probe(struct cx8802_driver *drv)
L
Linus Torvalds 已提交
1450
{
1451 1452
	struct cx88_core *core = drv->core;
	struct cx8802_dev *dev = core->dvbdev;
L
Linus Torvalds 已提交
1453 1454
	int err;

1455 1456 1457 1458 1459 1460
	dprintk( 1, "%s\n", __FUNCTION__);
	dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
		core->board,
		core->name,
		core->pci_bus,
		core->pci_slot);
L
Linus Torvalds 已提交
1461 1462

	err = -ENODEV;
1463
	if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD))
L
Linus Torvalds 已提交
1464 1465 1466
		goto fail_core;

	dev->width = 720;
1467
	dev->height = 576;
1468
	cx2341x_fill_defaults(&dev->params);
1469
	dev->params.port = CX2341X_PORT_STREAMING;
L
Linus Torvalds 已提交
1470

1471
	if (core->tvnorm & V4L2_STD_525_60) {
1472 1473 1474
		dev->height = 480;
	} else {
		dev->height = 576;
1475 1476
	}

L
Linus Torvalds 已提交
1477 1478 1479 1480 1481 1482
	/* blackbird stuff */
	printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
	       core->name);
	host_setup(dev->core);

	blackbird_register_video(dev);
1483 1484 1485

	/* initial device configuration: needed ? */

L
Linus Torvalds 已提交
1486 1487 1488 1489 1490 1491
	return 0;

 fail_core:
	return err;
}

1492
static int cx8802_blackbird_remove(struct cx8802_driver *drv)
L
Linus Torvalds 已提交
1493 1494
{
	/* blackbird */
1495
	blackbird_unregister_video(drv->core->dvbdev);
L
Linus Torvalds 已提交
1496

1497
	return 0;
L
Linus Torvalds 已提交
1498 1499
}

1500 1501 1502 1503 1504 1505 1506
static struct cx8802_driver cx8802_blackbird_driver = {
	.type_id	= CX88_MPEG_BLACKBIRD,
	.hw_access	= CX8802_DRVCTL_SHARED,
	.probe		= cx8802_blackbird_probe,
	.remove		= cx8802_blackbird_remove,
	.advise_acquire	= cx8802_blackbird_advise_acquire,
	.advise_release	= cx8802_blackbird_advise_release,
L
Linus Torvalds 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
};

static int blackbird_init(void)
{
	printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
	       (CX88_VERSION_CODE >> 16) & 0xff,
	       (CX88_VERSION_CODE >>  8) & 0xff,
	       CX88_VERSION_CODE & 0xff);
#ifdef SNAPSHOT
	printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
	       SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
#endif
1519 1520 1521
	cx88_ioctl_hook = mpeg_do_ioctl;
	cx88_ioctl_translator = mpeg_translate_ioctl;
	return cx8802_register_driver(&cx8802_blackbird_driver);
L
Linus Torvalds 已提交
1522 1523 1524 1525
}

static void blackbird_fini(void)
{
1526
	cx8802_unregister_driver(&cx8802_blackbird_driver);
L
Linus Torvalds 已提交
1527 1528 1529 1530 1531
}

module_init(blackbird_init);
module_exit(blackbird_fini);

1532 1533 1534
EXPORT_SYMBOL(cx88_ioctl_hook);
EXPORT_SYMBOL(cx88_ioctl_translator);

L
Linus Torvalds 已提交
1535 1536 1537 1538 1539
/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
1540
 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
L
Linus Torvalds 已提交
1541
 */