stradis.c 64.4 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
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 33 34 35 36 37 38 39 40 41 42 43 44
 * stradis.c - stradis 4:2:2 mpeg decoder driver
 *
 * Stradis 4:2:2 MPEG-2 Decoder Driver
 * Copyright (C) 1999 Nathan Laredo <laredo@gnu.org>
 *
 * 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/delay.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/pci.h>
#include <linux/signal.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/sched.h>
#include <asm/types.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <linux/vmalloc.h>
#include <linux/videodev.h>
45
#include <media/v4l2-common.h>
46
#include <media/v4l2-ioctl.h>
L
Linus Torvalds 已提交
47 48 49 50 51 52 53

#include "saa7146.h"
#include "saa7146reg.h"
#include "ibmmpeg2.h"
#include "saa7121.h"
#include "cs8420.h"

J
 
Jiri Slaby 已提交
54 55 56
#define DEBUG(x)		/* debug driver */
#undef  IDEBUG			/* debug irq handler */
#undef  MDEBUG			/* debug memory management */
L
Linus Torvalds 已提交
57 58 59 60 61

#define SAA7146_MAX 6

static struct saa7146 saa7146s[SAA7146_MAX];

62
static int saa_num;		/* number of SAA7146s in use */
L
Linus Torvalds 已提交
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

static int video_nr = -1;
module_param(video_nr, int, 0);
MODULE_LICENSE("GPL");

#define nDebNormal	0x00480000
#define nDebNoInc	0x00480000
#define nDebVideo	0xd0480000
#define nDebAudio	0xd0400000
#define nDebDMA		0x02c80000

#define oDebNormal	0x13c80000
#define oDebNoInc	0x13c80000
#define oDebVideo	0xd1080000
#define oDebAudio	0xd1080000
#define oDebDMA		0x03080000

#define NewCard		(saa->boardcfg[3])
#define ChipControl	(saa->boardcfg[1])
#define NTSCFirstActive	(saa->boardcfg[4])
#define PALFirstActive	(saa->boardcfg[5])
#define NTSCLastActive	(saa->boardcfg[54])
#define PALLastActive	(saa->boardcfg[55])
#define Have2MB		(saa->boardcfg[18] & 0x40)
#define HaveCS8420	(saa->boardcfg[18] & 0x04)
#define IBMMPEGCD20	(saa->boardcfg[18] & 0x20)
#define HaveCS3310	(saa->boardcfg[18] & 0x01)
#define CS3310MaxLvl	((saa->boardcfg[30] << 8) | saa->boardcfg[31])
#define HaveCS4341	(saa->boardcfg[40] == 2)
#define SDIType		(saa->boardcfg[27])
#define CurrentMode	(saa->boardcfg[2])

#define debNormal	(NewCard ? nDebNormal : oDebNormal)
#define debNoInc	(NewCard ? nDebNoInc : oDebNoInc)
#define debVideo	(NewCard ? nDebVideo : oDebVideo)
#define debAudio	(NewCard ? nDebAudio : oDebAudio)
#define debDMA		(NewCard ? nDebDMA : oDebDMA)

#ifdef USE_RESCUE_EEPROM_SDM275
static unsigned char rescue_eeprom[64] = {
J
 
Jiri Slaby 已提交
103 104 105 106 107 108
	0x00, 0x01, 0x04, 0x13, 0x26, 0x0f, 0x10, 0x00, 0x00, 0x00, 0x43, 0x63,
	0x22, 0x01, 0x29, 0x15, 0x73, 0x00, 0x1f,  'd',  'e',  'c',  'x',  'l',
	 'd',  'v',  'a', 0x02, 0x00, 0x01, 0x00, 0xcc, 0xa4, 0x63, 0x09, 0xe2,
	0x10, 0x00, 0x0a, 0x00, 0x02, 0x02,  'd',  'e',  'c',  'x',  'l',  'a',
	0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00,
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
};
#endif

/* ----------------------------------------------------------------------- */
/* Hardware I2C functions */
static void I2CWipe(struct saa7146 *saa)
{
	int i;
	/* set i2c to ~=100kHz, abort transfer, clear busy */
	saawrite(0x600 | SAA7146_I2C_ABORT, SAA7146_I2C_STATUS);
	saawrite((SAA7146_MC2_UPLD_I2C << 16) |
		 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
	/* wait for i2c registers to be programmed */
	for (i = 0; i < 1000 &&
	     !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
		schedule();
	saawrite(0x600, SAA7146_I2C_STATUS);
	saawrite((SAA7146_MC2_UPLD_I2C << 16) |
		 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
	/* wait for i2c registers to be programmed */
	for (i = 0; i < 1000 &&
	     !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
		schedule();
	saawrite(0x600, SAA7146_I2C_STATUS);
	saawrite((SAA7146_MC2_UPLD_I2C << 16) |
		 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
	/* wait for i2c registers to be programmed */
	for (i = 0; i < 1000 &&
	     !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
		schedule();
}

/* read I2C */
static int I2CRead(struct saa7146 *saa, unsigned char addr,
		   unsigned char subaddr, int dosub)
{
	int i;

	if (saaread(SAA7146_I2C_STATUS) & 0x3c)
		I2CWipe(saa);
J
 
Jiri Slaby 已提交
149 150 151
	for (i = 0;
		i < 1000 && (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY);
		i++)
L
Linus Torvalds 已提交
152 153 154 155 156
		schedule();
	if (i == 1000)
		I2CWipe(saa);
	if (dosub)
		saawrite(((addr & 0xfe) << 24) | (((addr | 1) & 0xff) << 8) |
J
 
Jiri Slaby 已提交
157
			((subaddr & 0xff) << 16) | 0xed, SAA7146_I2C_TRANSFER);
L
Linus Torvalds 已提交
158 159
	else
		saawrite(((addr & 0xfe) << 24) | (((addr | 1) & 0xff) << 16) |
J
 
Jiri Slaby 已提交
160
			0xf1, SAA7146_I2C_TRANSFER);
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172
	saawrite((SAA7146_MC2_UPLD_I2C << 16) |
		 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
	/* wait for i2c registers to be programmed */
	for (i = 0; i < 1000 &&
	     !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
		schedule();
	/* wait for valid data */
	for (i = 0; i < 1000 &&
	     (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
		schedule();
	if (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_ERR)
		return -1;
J
 
Jiri Slaby 已提交
173
	if (i == 1000)
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187
		printk("i2c setup read timeout\n");
	saawrite(0x41, SAA7146_I2C_TRANSFER);
	saawrite((SAA7146_MC2_UPLD_I2C << 16) |
		 SAA7146_MC2_UPLD_I2C, SAA7146_MC2);
	/* wait for i2c registers to be programmed */
	for (i = 0; i < 1000 &&
	     !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_I2C); i++)
		schedule();
	/* wait for valid data */
	for (i = 0; i < 1000 &&
	     (saaread(SAA7146_I2C_TRANSFER) & SAA7146_I2C_BUSY); i++)
		schedule();
	if (saaread(SAA7146_I2C_TRANSFER) & SAA7146_I2C_ERR)
		return -1;
J
 
Jiri Slaby 已提交
188
	if (i == 1000)
L
Linus Torvalds 已提交
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
		printk("i2c read timeout\n");
	return ((saaread(SAA7146_I2C_TRANSFER) >> 24) & 0xff);
}

/* set both to write both bytes, reset it to write only b1 */

static int I2CWrite(struct saa7146 *saa, unsigned char addr, unsigned char b1,
		    unsigned char b2, int both)
{
	int i;
	u32 data;

	if (saaread(SAA7146_I2C_STATUS) & 0x3c)
		I2CWipe(saa);
	for (i = 0; i < 1000 &&
	     (saaread(SAA7146_I2C_STATUS) & SAA7146_I2C_BUSY); i++)
		schedule();
	if (i == 1000)
		I2CWipe(saa);
	data = ((addr & 0xfe) << 24) | ((b1 & 0xff) << 16);
	if (both)
		data |= ((b2 & 0xff) << 8) | 0xe5;
	else
		data |= 0xd1;
	saawrite(data, SAA7146_I2C_TRANSFER);
	saawrite((SAA7146_MC2_UPLD_I2C << 16) | SAA7146_MC2_UPLD_I2C,
		 SAA7146_MC2);
	return 0;
}

static void attach_inform(struct saa7146 *saa, int id)
{
	int i;

J
 
Jiri Slaby 已提交
223 224 225
	DEBUG(printk(KERN_DEBUG "stradis%d: i2c: device found=%02x\n", saa->nr,
		id));
	if (id == 0xa0) {	/* we have rev2 or later board, fill in info */
L
Linus Torvalds 已提交
226 227 228 229
		for (i = 0; i < 64; i++)
			saa->boardcfg[i] = I2CRead(saa, 0xa0, i, 1);
#ifdef USE_RESCUE_EEPROM_SDM275
		if (saa->boardcfg[0] != 0) {
J
 
Jiri Slaby 已提交
230 231
			printk("stradis%d: WARNING: EEPROM STORED VALUES HAVE "
				"BEEN IGNORED\n", saa->nr);
L
Linus Torvalds 已提交
232 233 234 235 236 237
			for (i = 0; i < 64; i++)
				saa->boardcfg[i] = rescue_eeprom[i];
		}
#endif
		printk("stradis%d: config =", saa->nr);
		for (i = 0; i < 51; i++) {
J
 
Jiri Slaby 已提交
238
			printk(" %02x", saa->boardcfg[i]);
L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
		}
		printk("\n");
	}
}

static void I2CBusScan(struct saa7146 *saa)
{
	int i;
	for (i = 0; i < 0xff; i += 2)
		if ((I2CRead(saa, i, 0, 0)) >= 0)
			attach_inform(saa, i);
}

252
static int debiwait_maxwait;
L
Linus Torvalds 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265

static int wait_for_debi_done(struct saa7146 *saa)
{
	int i;

	/* wait for registers to be programmed */
	for (i = 0; i < 100000 &&
	     !(saaread(SAA7146_MC2) & SAA7146_MC2_UPLD_DEBI); i++)
		saaread(SAA7146_MC2);
	/* wait for transfer to complete */
	for (i = 0; i < 500000 &&
	     (saaread(SAA7146_PSR) & SAA7146_PSR_DEBI_S); i++)
		saaread(SAA7146_MC2);
J
 
Jiri Slaby 已提交
266

L
Linus Torvalds 已提交
267 268 269
	if (i > debiwait_maxwait)
		printk("wait-for-debi-done maxwait: %d\n",
			debiwait_maxwait = i);
J
 
Jiri Slaby 已提交
270

L
Linus Torvalds 已提交
271 272
	if (i == 500000)
		return -1;
J
 
Jiri Slaby 已提交
273

L
Linus Torvalds 已提交
274 275 276 277
	return 0;
}

static int debiwrite(struct saa7146 *saa, u32 config, int addr,
J
 
Jiri Slaby 已提交
278
	u32 val, int count)
L
Linus Torvalds 已提交
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 316 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
{
	u32 cmd;
	if (count <= 0 || count > 32764)
		return -1;
	if (wait_for_debi_done(saa) < 0)
		return -1;
	saawrite(config, SAA7146_DEBI_CONFIG);
	if (count <= 4)		/* immediate transfer */
		saawrite(val, SAA7146_DEBI_AD);
	else			/* block transfer */
		saawrite(virt_to_bus(saa->dmadebi), SAA7146_DEBI_AD);
	saawrite((cmd = (count << 17) | (addr & 0xffff)), SAA7146_DEBI_COMMAND);
	saawrite((SAA7146_MC2_UPLD_DEBI << 16) | SAA7146_MC2_UPLD_DEBI,
		 SAA7146_MC2);
	return 0;
}

static u32 debiread(struct saa7146 *saa, u32 config, int addr, int count)
{
	u32 result = 0;

	if (count > 32764 || count <= 0)
		return 0;
	if (wait_for_debi_done(saa) < 0)
		return 0;
	saawrite(virt_to_bus(saa->dmadebi), SAA7146_DEBI_AD);
	saawrite((count << 17) | 0x10000 | (addr & 0xffff),
		 SAA7146_DEBI_COMMAND);
	saawrite(config, SAA7146_DEBI_CONFIG);
	saawrite((SAA7146_MC2_UPLD_DEBI << 16) | SAA7146_MC2_UPLD_DEBI,
		 SAA7146_MC2);
	if (count > 4)		/* not an immediate transfer */
		return count;
	wait_for_debi_done(saa);
	result = saaread(SAA7146_DEBI_AD);
	if (count == 1)
		result &= 0xff;
	if (count == 2)
		result &= 0xffff;
	if (count == 3)
		result &= 0xffffff;
	return result;
}

static void do_irq_send_data(struct saa7146 *saa)
{
	int split, audbytes, vidbytes;

	saawrite(SAA7146_PSR_PIN1, SAA7146_IER);
	/* if special feature mode in effect, disable audio sending */
	if (saa->playmode != VID_PLAY_NORMAL)
		saa->audtail = saa->audhead = 0;
	if (saa->audhead <= saa->audtail)
		audbytes = saa->audtail - saa->audhead;
	else
		audbytes = 65536 - (saa->audhead - saa->audtail);
	if (saa->vidhead <= saa->vidtail)
		vidbytes = saa->vidtail - saa->vidhead;
	else
		vidbytes = 524288 - (saa->vidhead - saa->vidtail);
	if (audbytes == 0 && vidbytes == 0 && saa->osdtail == saa->osdhead) {
		saawrite(0, SAA7146_IER);
		return;
	}
	/* if at least 1 block audio waiting and audio fifo isn't full */
J
 
Jiri Slaby 已提交
344 345
	if (audbytes >= 2048 && (debiread(saa, debNormal, IBM_MP2_AUD_FIFO, 2)
			& 0xff) < 60) {
L
Linus Torvalds 已提交
346 347 348 349 350 351
		if (saa->audhead > saa->audtail)
			split = 65536 - saa->audhead;
		else
			split = 0;
		audbytes = 2048;
		if (split > 0 && split < 2048) {
J
 
Jiri Slaby 已提交
352
			memcpy(saa->dmadebi, saa->audbuf + saa->audhead, split);
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360
			saa->audhead = 0;
			audbytes -= split;
		} else
			split = 0;
		memcpy(saa->dmadebi + split, saa->audbuf + saa->audhead,
			audbytes);
		saa->audhead += audbytes;
		saa->audhead &= 0xffff;
J
 
Jiri Slaby 已提交
361 362
		debiwrite(saa, debAudio, (NewCard ? IBM_MP2_AUD_FIFO :
			IBM_MP2_AUD_FIFOW), 0, 2048);
L
Linus Torvalds 已提交
363
		wake_up_interruptible(&saa->audq);
J
 
Jiri Slaby 已提交
364
		/* if at least 1 block video waiting and video fifo isn't full */
L
Linus Torvalds 已提交
365
	} else if (vidbytes >= 30720 && (debiread(saa, debNormal,
J
 
Jiri Slaby 已提交
366
						  IBM_MP2_FIFO, 2)) < 16384) {
L
Linus Torvalds 已提交
367 368 369 370 371 372
		if (saa->vidhead > saa->vidtail)
			split = 524288 - saa->vidhead;
		else
			split = 0;
		vidbytes = 30720;
		if (split > 0 && split < 30720) {
J
 
Jiri Slaby 已提交
373
			memcpy(saa->dmadebi, saa->vidbuf + saa->vidhead, split);
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382
			saa->vidhead = 0;
			vidbytes -= split;
		} else
			split = 0;
		memcpy(saa->dmadebi + split, saa->vidbuf + saa->vidhead,
			vidbytes);
		saa->vidhead += vidbytes;
		saa->vidhead &= 0x7ffff;
		debiwrite(saa, debVideo, (NewCard ? IBM_MP2_FIFO :
J
 
Jiri Slaby 已提交
383
					  IBM_MP2_FIFOW), 0, 30720);
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394
		wake_up_interruptible(&saa->vidq);
	}
	saawrite(SAA7146_PSR_DEBI_S | SAA7146_PSR_PIN1, SAA7146_IER);
}

static void send_osd_data(struct saa7146 *saa)
{
	int size = saa->osdtail - saa->osdhead;
	if (size > 30720)
		size = 30720;
	/* ensure some multiple of 8 bytes is transferred */
J
 
Jiri Slaby 已提交
395
	size = 8 * ((size + 8) >> 3);
L
Linus Torvalds 已提交
396 397
	if (size) {
		debiwrite(saa, debNormal, IBM_MP2_OSD_ADDR,
J
 
Jiri Slaby 已提交
398
			  (saa->osdhead >> 3), 2);
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409
		memcpy(saa->dmadebi, &saa->osdbuf[saa->osdhead], size);
		saa->osdhead += size;
		/* block transfer of next 8 bytes to ~32k bytes */
		debiwrite(saa, debNormal, IBM_MP2_OSD_DATA, 0, size);
	}
	if (saa->osdhead >= saa->osdtail) {
		saa->osdhead = saa->osdtail = 0;
		debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
	}
}

410
static irqreturn_t saa7146_irq(int irq, void *dev_id)
L
Linus Torvalds 已提交
411
{
J
 
Jiri Slaby 已提交
412
	struct saa7146 *saa = dev_id;
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
	u32 stat, astat;
	int count;
	int handled = 0;

	count = 0;
	while (1) {
		/* get/clear interrupt status bits */
		stat = saaread(SAA7146_ISR);
		astat = stat & saaread(SAA7146_IER);
		if (!astat)
			break;
		handled = 1;
		saawrite(astat, SAA7146_ISR);
		if (astat & SAA7146_PSR_DEBI_S) {
			do_irq_send_data(saa);
		}
		if (astat & SAA7146_PSR_PIN1) {
			int istat;
			/* the following read will trigger DEBI_S */
			istat = debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
			if (istat & 1) {
				saawrite(0, SAA7146_IER);
				send_osd_data(saa);
				saawrite(SAA7146_PSR_DEBI_S |
					 SAA7146_PSR_PIN1, SAA7146_IER);
			}
			if (istat & 0x20) {	/* Video Start */
				saa->vidinfo.frame_count++;
			}
			if (istat & 0x400) {	/* Picture Start */
				/* update temporal reference */
			}
			if (istat & 0x200) {	/* Picture Resolution Change */
				/* read new resolution */
			}
			if (istat & 0x100) {	/* New User Data found */
				/* read new user data */
			}
			if (istat & 0x1000) {	/* new GOP/SMPTE */
				/* read new SMPTE */
			}
			if (istat & 0x8000) {	/* Sequence Start Code */
				/* reset frame counter, load sizes */
				saa->vidinfo.frame_count = 0;
				saa->vidinfo.h_size = 704;
				saa->vidinfo.v_size = 480;
#if 0
				if (saa->endmarkhead != saa->endmarktail) {
J
 
Jiri Slaby 已提交
461
					saa->audhead =
L
Linus Torvalds 已提交
462 463 464 465 466 467 468 469 470
						saa->endmark[saa->endmarkhead];
					saa->endmarkhead++;
					if (saa->endmarkhead >= MAX_MARKS)
						saa->endmarkhead = 0;
				}
#endif
			}
			if (istat & 0x4000) {	/* Sequence Error Code */
				if (saa->endmarkhead != saa->endmarktail) {
J
 
Jiri Slaby 已提交
471
					saa->audhead =
L
Linus Torvalds 已提交
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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
						saa->endmark[saa->endmarkhead];
					saa->endmarkhead++;
					if (saa->endmarkhead >= MAX_MARKS)
						saa->endmarkhead = 0;
				}
			}
		}
#ifdef IDEBUG
		if (astat & SAA7146_PSR_PPEF) {
			IDEBUG(printk("stradis%d irq: PPEF\n", saa->nr));
		}
		if (astat & SAA7146_PSR_PABO) {
			IDEBUG(printk("stradis%d irq: PABO\n", saa->nr));
		}
		if (astat & SAA7146_PSR_PPED) {
			IDEBUG(printk("stradis%d irq: PPED\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_I1) {
			IDEBUG(printk("stradis%d irq: RPS_I1\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_I0) {
			IDEBUG(printk("stradis%d irq: RPS_I0\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_LATE1) {
			IDEBUG(printk("stradis%d irq: RPS_LATE1\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_LATE0) {
			IDEBUG(printk("stradis%d irq: RPS_LATE0\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_E1) {
			IDEBUG(printk("stradis%d irq: RPS_E1\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_E0) {
			IDEBUG(printk("stradis%d irq: RPS_E0\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_TO1) {
			IDEBUG(printk("stradis%d irq: RPS_TO1\n", saa->nr));
		}
		if (astat & SAA7146_PSR_RPS_TO0) {
			IDEBUG(printk("stradis%d irq: RPS_TO0\n", saa->nr));
		}
		if (astat & SAA7146_PSR_UPLD) {
			IDEBUG(printk("stradis%d irq: UPLD\n", saa->nr));
		}
		if (astat & SAA7146_PSR_DEBI_E) {
			IDEBUG(printk("stradis%d irq: DEBI_E\n", saa->nr));
		}
		if (astat & SAA7146_PSR_I2C_S) {
			IDEBUG(printk("stradis%d irq: I2C_S\n", saa->nr));
		}
		if (astat & SAA7146_PSR_I2C_E) {
			IDEBUG(printk("stradis%d irq: I2C_E\n", saa->nr));
		}
		if (astat & SAA7146_PSR_A2_IN) {
			IDEBUG(printk("stradis%d irq: A2_IN\n", saa->nr));
		}
		if (astat & SAA7146_PSR_A2_OUT) {
			IDEBUG(printk("stradis%d irq: A2_OUT\n", saa->nr));
		}
		if (astat & SAA7146_PSR_A1_IN) {
			IDEBUG(printk("stradis%d irq: A1_IN\n", saa->nr));
		}
		if (astat & SAA7146_PSR_A1_OUT) {
			IDEBUG(printk("stradis%d irq: A1_OUT\n", saa->nr));
		}
		if (astat & SAA7146_PSR_AFOU) {
			IDEBUG(printk("stradis%d irq: AFOU\n", saa->nr));
		}
		if (astat & SAA7146_PSR_V_PE) {
			IDEBUG(printk("stradis%d irq: V_PE\n", saa->nr));
		}
		if (astat & SAA7146_PSR_VFOU) {
			IDEBUG(printk("stradis%d irq: VFOU\n", saa->nr));
		}
		if (astat & SAA7146_PSR_FIDA) {
			IDEBUG(printk("stradis%d irq: FIDA\n", saa->nr));
		}
		if (astat & SAA7146_PSR_FIDB) {
			IDEBUG(printk("stradis%d irq: FIDB\n", saa->nr));
		}
		if (astat & SAA7146_PSR_PIN3) {
			IDEBUG(printk("stradis%d irq: PIN3\n", saa->nr));
		}
		if (astat & SAA7146_PSR_PIN2) {
			IDEBUG(printk("stradis%d irq: PIN2\n", saa->nr));
		}
		if (astat & SAA7146_PSR_PIN0) {
			IDEBUG(printk("stradis%d irq: PIN0\n", saa->nr));
		}
		if (astat & SAA7146_PSR_ECS) {
			IDEBUG(printk("stradis%d irq: ECS\n", saa->nr));
		}
		if (astat & SAA7146_PSR_EC3S) {
			IDEBUG(printk("stradis%d irq: EC3S\n", saa->nr));
		}
		if (astat & SAA7146_PSR_EC0S) {
			IDEBUG(printk("stradis%d irq: EC0S\n", saa->nr));
		}
#endif
		count++;
		if (count > 15)
			printk(KERN_WARNING "stradis%d: irq loop %d\n",
			       saa->nr, count);
		if (count > 20) {
			saawrite(0, SAA7146_IER);
			printk(KERN_ERR
			       "stradis%d: IRQ loop cleared\n", saa->nr);
		}
	}
	return IRQ_RETVAL(handled);
}

static int ibm_send_command(struct saa7146 *saa,
			    int command, int data, int chain)
{
	int i;

	if (chain)
J
 
Jiri Slaby 已提交
590
		debiwrite(saa, debNormal, IBM_MP2_COMMAND, (command << 1)| 1,2);
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	else
		debiwrite(saa, debNormal, IBM_MP2_COMMAND, command << 1, 2);
	debiwrite(saa, debNormal, IBM_MP2_CMD_DATA, data, 2);
	debiwrite(saa, debNormal, IBM_MP2_CMD_STAT, 1, 2);
	for (i = 0; i < 100 &&
	     (debiread(saa, debNormal, IBM_MP2_CMD_STAT, 2) & 1); i++)
		schedule();
	if (i == 100)
		return -1;
	return 0;
}

static void cs4341_setlevel(struct saa7146 *saa, int left, int right)
{
	I2CWrite(saa, 0x22, 0x03, left > 94 ? 94 : left, 2);
	I2CWrite(saa, 0x22, 0x04, right > 94 ? 94 : right, 2);
}

static void initialize_cs4341(struct saa7146 *saa)
{
	int i;
	for (i = 0; i < 200; i++) {
		/* auto mute off, power on, no de-emphasis */
		/* I2S data up to 24-bit 64xFs internal SCLK */
		I2CWrite(saa, 0x22, 0x01, 0x11, 2);
		/* ATAPI mixer settings */
		I2CWrite(saa, 0x22, 0x02, 0x49, 2);
		/* attenuation left 3db */
		I2CWrite(saa, 0x22, 0x03, 0x00, 2);
		/* attenuation right 3db */
		I2CWrite(saa, 0x22, 0x04, 0x00, 2);
		I2CWrite(saa, 0x22, 0x01, 0x10, 2);
		if (I2CRead(saa, 0x22, 0x02, 1) == 0x49)
			break;
		schedule();
	}
	printk("stradis%d: CS4341 initialized (%d)\n", saa->nr, i);
	return;
}

static void initialize_cs8420(struct saa7146 *saa, int pro)
{
	int i;
	u8 *sequence;
	if (pro)
		sequence = mode8420pro;
	else
		sequence = mode8420con;
	for (i = 0; i < INIT8420LEN; i++)
J
 
Jiri Slaby 已提交
640
		I2CWrite(saa, 0x20, init8420[i * 2], init8420[i * 2 + 1], 2);
L
Linus Torvalds 已提交
641
	for (i = 0; i < MODE8420LEN; i++)
J
 
Jiri Slaby 已提交
642
		I2CWrite(saa, 0x20, sequence[i * 2], sequence[i * 2 + 1], 2);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	printk("stradis%d: CS8420 initialized\n", saa->nr);
}

static void initialize_saa7121(struct saa7146 *saa, int dopal)
{
	int i, mod;
	u8 *sequence;
	if (dopal)
		sequence = init7121pal;
	else
		sequence = init7121ntsc;
	mod = saaread(SAA7146_PSR) & 0x08;
	/* initialize PAL/NTSC video encoder */
	for (i = 0; i < INIT7121LEN; i++) {
		if (NewCard) {	/* handle new card encoder differences */
J
 
Jiri Slaby 已提交
658
			if (sequence[i * 2] == 0x3a)
L
Linus Torvalds 已提交
659
				I2CWrite(saa, 0x88, 0x3a, 0x13, 2);
J
 
Jiri Slaby 已提交
660
			else if (sequence[i * 2] == 0x6b)
L
Linus Torvalds 已提交
661
				I2CWrite(saa, 0x88, 0x6b, 0x20, 2);
J
 
Jiri Slaby 已提交
662
			else if (sequence[i * 2] == 0x6c)
L
Linus Torvalds 已提交
663 664
				I2CWrite(saa, 0x88, 0x6c,
					 dopal ? 0x09 : 0xf5, 2);
J
 
Jiri Slaby 已提交
665
			else if (sequence[i * 2] == 0x6d)
L
Linus Torvalds 已提交
666 667
				I2CWrite(saa, 0x88, 0x6d,
					 dopal ? 0x20 : 0x00, 2);
J
 
Jiri Slaby 已提交
668
			else if (sequence[i * 2] == 0x7a)
L
Linus Torvalds 已提交
669 670 671
				I2CWrite(saa, 0x88, 0x7a,
					 dopal ? (PALFirstActive - 1) :
					 (NTSCFirstActive - 4), 2);
J
 
Jiri Slaby 已提交
672
			else if (sequence[i * 2] == 0x7b)
L
Linus Torvalds 已提交
673 674 675
				I2CWrite(saa, 0x88, 0x7b,
					 dopal ? PALLastActive :
					 NTSCLastActive, 2);
J
 
Jiri Slaby 已提交
676 677 678
			else
				I2CWrite(saa, 0x88, sequence[i * 2],
					 sequence[i * 2 + 1], 2);
L
Linus Torvalds 已提交
679
		} else {
J
 
Jiri Slaby 已提交
680 681 682 683
			if (sequence[i * 2] == 0x6b && mod)
				I2CWrite(saa, 0x88, 0x6b,
					 (sequence[i * 2 + 1] ^ 0x09), 2);
			else if (sequence[i * 2] == 0x7a)
L
Linus Torvalds 已提交
684 685 686
				I2CWrite(saa, 0x88, 0x7a,
					 dopal ? (PALFirstActive - 1) :
					 (NTSCFirstActive - 4), 2);
J
 
Jiri Slaby 已提交
687
			else if (sequence[i * 2] == 0x7b)
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
				I2CWrite(saa, 0x88, 0x7b,
					 dopal ? PALLastActive :
					 NTSCLastActive, 2);
			else
				I2CWrite(saa, 0x88, sequence[i * 2],
					 sequence[i * 2 + 1], 2);
		}
	}
}

static void set_genlock_offset(struct saa7146 *saa, int noffset)
{
	int nCode;
	int PixelsPerLine = 858;
	if (CurrentMode == VIDEO_MODE_PAL)
		PixelsPerLine = 864;
	if (noffset > 500)
		noffset = 500;
	else if (noffset < -500)
		noffset = -500;
	nCode = noffset + 0x100;
	if (nCode == 1)
		nCode = 0x401;
J
 
Jiri Slaby 已提交
711 712
	else if (nCode < 1)
		nCode = 0x400 + PixelsPerLine + nCode;
L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720 721
	debiwrite(saa, debNormal, XILINX_GLDELAY, nCode, 2);
}

static void set_out_format(struct saa7146 *saa, int mode)
{
	initialize_saa7121(saa, (mode == VIDEO_MODE_NTSC ? 0 : 1));
	saa->boardcfg[2] = mode;
	/* do not adjust analog video parameters here, use saa7121 init */
	/* you will affect the SDI output on the new card */
J
 
Jiri Slaby 已提交
722
	if (mode == VIDEO_MODE_PAL) {	/* PAL */
L
Linus Torvalds 已提交
723 724 725 726
		debiwrite(saa, debNormal, XILINX_CTL0, 0x0808, 2);
		mdelay(50);
		saawrite(0x012002c0, SAA7146_NUM_LINE_BYTE1);
		if (NewCard) {
J
 
Jiri Slaby 已提交
727
			debiwrite(saa, debNormal, IBM_MP2_DISP_MODE, 0xe100, 2);
L
Linus Torvalds 已提交
728 729 730
			mdelay(50);
		}
		debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
J
 
Jiri Slaby 已提交
731
			  NewCard ? 0xe500 : 0x6500, 2);
L
Linus Torvalds 已提交
732 733
		debiwrite(saa, debNormal, IBM_MP2_DISP_DLY,
			  (1 << 8) |
J
 
Jiri Slaby 已提交
734
			  (NewCard ? PALFirstActive : PALFirstActive - 6), 2);
L
Linus Torvalds 已提交
735 736 737 738 739
	} else {		/* NTSC */
		debiwrite(saa, debNormal, XILINX_CTL0, 0x0800, 2);
		mdelay(50);
		saawrite(0x00f002c0, SAA7146_NUM_LINE_BYTE1);
		debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
J
 
Jiri Slaby 已提交
740
			  NewCard ? 0xe100 : 0x6100, 2);
L
Linus Torvalds 已提交
741 742
		debiwrite(saa, debNormal, IBM_MP2_DISP_DLY,
			  (1 << 8) |
J
 
Jiri Slaby 已提交
743
			  (NewCard ? NTSCFirstActive : NTSCFirstActive - 6), 2);
L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	}
}

/* Intialize bitmangler to map from a byte value to the mangled word that
 * must be output to program the Xilinx part through the DEBI port.
 * Xilinx Data Bit->DEBI Bit: 0->15 1->7 2->6 3->12 4->11 5->2 6->1 7->0
 * transfer FPGA code, init IBM chip, transfer IBM microcode
 * rev2 card mangles: 0->7 1->6 2->5 3->4 4->3 5->2 6->1 7->0
 */
static u16 bitmangler[256];

static int initialize_fpga(struct video_code *bitdata)
{
	int i, num, startindex, failure = 0, loadtwo, loadfile = 0;
	u16 *dmabuf;
	u8 *newdma;
	struct saa7146 *saa;

	/* verify fpga code */
	for (startindex = 0; startindex < bitdata->datasize; startindex++)
		if (bitdata->data[startindex] == 255)
			break;
	if (startindex == bitdata->datasize) {
		printk(KERN_INFO "stradis: bad fpga code\n");
		return -1;
	}
	/* initialize all detected cards */
	for (num = 0; num < saa_num; num++) {
		saa = &saa7146s[num];
		if (saa->boardcfg[0] > 20)
J
 
Jiri Slaby 已提交
774
			continue;	/* card was programmed */
L
Linus Torvalds 已提交
775 776 777
		loadtwo = (saa->boardcfg[18] & 0x10);
		if (!NewCard)	/* we have an old board */
			for (i = 0; i < 256; i++)
J
 
Jiri Slaby 已提交
778 779 780 781 782 783
				bitmangler[i] = ((i & 0x01) << 15) |
					((i & 0x02) << 6) | ((i & 0x04) << 4) |
					((i & 0x08) << 9) | ((i & 0x10) << 7) |
					((i & 0x20) >> 3) | ((i & 0x40) >> 5) |
					((i & 0x80) >> 7);
		else		/* else we have a new board */
L
Linus Torvalds 已提交
784
			for (i = 0; i < 256; i++)
J
 
Jiri Slaby 已提交
785 786 787 788 789
				bitmangler[i] = ((i & 0x01) << 7) |
					((i & 0x02) << 5) | ((i & 0x04) << 3) |
					((i & 0x08) << 1) | ((i & 0x10) >> 1) |
					((i & 0x20) >> 3) | ((i & 0x40) >> 5) |
					((i & 0x80) >> 7);
L
Linus Torvalds 已提交
790 791 792 793 794 795

		dmabuf = (u16 *) saa->dmadebi;
		newdma = (u8 *) saa->dmadebi;
		if (NewCard) {	/* SDM2xxx */
			if (!strncmp(bitdata->loadwhat, "decoder2", 8))
				continue;	/* fpga not for this card */
J
 
Jiri Slaby 已提交
796
			if (!strncmp(&saa->boardcfg[42], bitdata->loadwhat, 8))
L
Linus Torvalds 已提交
797
				loadfile = 1;
J
 
Jiri Slaby 已提交
798 799
			else if (loadtwo && !strncmp(&saa->boardcfg[19],
				       bitdata->loadwhat, 8))
L
Linus Torvalds 已提交
800
				loadfile = 2;
J
 
Jiri Slaby 已提交
801 802 803 804
			else if (!saa->boardcfg[42] && !strncmp("decxl",
					bitdata->loadwhat, 8))
				loadfile = 1;	/* special */
			else
L
Linus Torvalds 已提交
805
				continue;	/* fpga not for this card */
J
 
Jiri Slaby 已提交
806
			if (loadfile != 1 && loadfile != 2)
L
Linus Torvalds 已提交
807
				continue;	/* skip to next card */
J
 
Jiri Slaby 已提交
808
			if (saa->boardcfg[0] && loadfile == 1)
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
				continue;	/* skip to next card */
			if (saa->boardcfg[0] != 1 && loadfile == 2)
				continue;	/* skip to next card */
			saa->boardcfg[0]++;	/* mark fpga handled */
			printk("stradis%d: loading %s\n", saa->nr,
				bitdata->loadwhat);
			if (loadtwo && loadfile == 2)
				goto send_fpga_stuff;
			/* turn on the Audio interface to set PROG low */
			saawrite(0x00400040, SAA7146_GPIO_CTRL);
			saaread(SAA7146_PSR);	/* ensure posted write */
			/* wait for everyone to reset */
			mdelay(10);
			saawrite(0x00400000, SAA7146_GPIO_CTRL);
		} else {	/* original card */
			if (strncmp(bitdata->loadwhat, "decoder2", 8))
				continue;	/* fpga not for this card */
			/* Pull the Xilinx PROG signal WS3 low */
			saawrite(0x02000200, SAA7146_MC1);
			/* Turn on the Audio interface so can set PROG low */
			saawrite(0x000000c0, SAA7146_ACON1);
			/* Pull the Xilinx INIT signal (GPIO2) low */
			saawrite(0x00400000, SAA7146_GPIO_CTRL);
			/* Make sure everybody resets */
			saaread(SAA7146_PSR);	/* ensure posted write */
			mdelay(10);
			/* Release the Xilinx PROG signal */
			saawrite(0x00000000, SAA7146_ACON1);
			/* Turn off the Audio interface */
			saawrite(0x02000000, SAA7146_MC1);
		}
		/* Release Xilinx INIT signal (WS2) */
		saawrite(0x00000000, SAA7146_GPIO_CTRL);
		/* Wait for the INIT to go High */
J
 
Jiri Slaby 已提交
843 844 845
		for (i = 0;
			i < 10000 && !(saaread(SAA7146_PSR) & SAA7146_PSR_PIN2);
			i++)
L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854
			schedule();
		if (i == 1000) {
			printk(KERN_INFO "stradis%d: no fpga INIT\n", saa->nr);
			return -1;
		}
send_fpga_stuff:
		if (NewCard) {
			for (i = startindex; i < bitdata->datasize; i++)
				newdma[i - startindex] =
J
 
Jiri Slaby 已提交
855
				    bitmangler[bitdata->data[i]];
L
Linus Torvalds 已提交
856 857
			debiwrite(saa, 0x01420000, 0, 0,
				((bitdata->datasize - startindex) + 5));
J
 
Jiri Slaby 已提交
858 859 860 861
			if (loadtwo && loadfile == 1) {
				printk("stradis%d: awaiting 2nd FPGA bitfile\n",
				       saa->nr);
				continue;	/* skip to next card */
L
Linus Torvalds 已提交
862 863 864 865 866 867 868 869
			}
		} else {
			for (i = startindex; i < bitdata->datasize; i++)
				dmabuf[i - startindex] =
					bitmangler[bitdata->data[i]];
			debiwrite(saa, 0x014a0000, 0, 0,
				((bitdata->datasize - startindex) + 5) * 2);
		}
J
 
Jiri Slaby 已提交
870 871 872
		for (i = 0;
			i < 1000 && !(saaread(SAA7146_PSR) & SAA7146_PSR_PIN2);
			i++)
L
Linus Torvalds 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
			schedule();
		if (i == 1000) {
			printk(KERN_INFO "stradis%d: FPGA load failed\n",
			       saa->nr);
			failure++;
			continue;
		}
		if (!NewCard) {
			/* Pull the Xilinx INIT signal (GPIO2) low */
			saawrite(0x00400000, SAA7146_GPIO_CTRL);
			saaread(SAA7146_PSR);	/* ensure posted write */
			mdelay(2);
			saawrite(0x00000000, SAA7146_GPIO_CTRL);
			mdelay(2);
		}
		printk(KERN_INFO "stradis%d: FPGA Loaded\n", saa->nr);
		saa->boardcfg[0] = 26;	/* mark fpga programmed */
		/* set VXCO to its lowest frequency */
		debiwrite(saa, debNormal, XILINX_PWM, 0, 2);
		if (NewCard) {
			/* mute CS3310 */
			if (HaveCS3310)
				debiwrite(saa, debNormal, XILINX_CS3310_CMPLT,
J
 
Jiri Slaby 已提交
896
					0, 2);
L
Linus Torvalds 已提交
897 898 899 900 901 902
			/* set VXCO to PWM mode, release reset, blank on */
			debiwrite(saa, debNormal, XILINX_CTL0, 0xffc4, 2);
			mdelay(10);
			/* unmute CS3310 */
			if (HaveCS3310)
				debiwrite(saa, debNormal, XILINX_CTL0,
J
 
Jiri Slaby 已提交
903
					0x2020, 2);
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
		}
		/* set source Black */
		debiwrite(saa, debNormal, XILINX_CTL0, 0x1707, 2);
		saa->boardcfg[4] = 22;	/* set NTSC First Active Line */
		saa->boardcfg[5] = 23;	/* set PAL First Active Line */
		saa->boardcfg[54] = 2;	/* set NTSC Last Active Line - 256 */
		saa->boardcfg[55] = 54;	/* set PAL Last Active Line - 256 */
		set_out_format(saa, VIDEO_MODE_NTSC);
		mdelay(50);
		/* begin IBM chip init */
		debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 4, 2);
		saaread(SAA7146_PSR);	/* wait for reset */
		mdelay(5);
		debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0, 2);
		debiread(saa, debNormal, IBM_MP2_CHIP_CONTROL, 2);
		debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0x10, 2);
		debiwrite(saa, debNormal, IBM_MP2_CMD_ADDR, 0, 2);
		debiwrite(saa, debNormal, IBM_MP2_CHIP_MODE, 0x2e, 2);
		if (NewCard) {
			mdelay(5);
			/* set i2s rate converter to 48KHz */
			debiwrite(saa, debNormal, 0x80c0, 6, 2);
			/* we must init CS8420 first since rev b pulls i2s */
			/* master clock low and CS4341 needs i2s master to */
			/* run the i2c port. */
J
 
Jiri Slaby 已提交
929
			if (HaveCS8420)
L
Linus Torvalds 已提交
930 931
				/* 0=consumer, 1=pro */
				initialize_cs8420(saa, 0);
J
 
Jiri Slaby 已提交
932

L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
			mdelay(5);
			if (HaveCS4341)
				initialize_cs4341(saa);
		}
		debiwrite(saa, debNormal, IBM_MP2_INFC_CTL, 0x48, 2);
		debiwrite(saa, debNormal, IBM_MP2_BEEP_CTL, 0xa000, 2);
		debiwrite(saa, debNormal, IBM_MP2_DISP_LBOR, 0, 2);
		debiwrite(saa, debNormal, IBM_MP2_DISP_TBOR, 0, 2);
		if (NewCard)
			set_genlock_offset(saa, 0);
		debiwrite(saa, debNormal, IBM_MP2_FRNT_ATTEN, 0, 2);
#if 0
		/* enable genlock */
		debiwrite(saa, debNormal, XILINX_CTL0, 0x8000, 2);
#else
		/* disable genlock */
		debiwrite(saa, debNormal, XILINX_CTL0, 0x8080, 2);
#endif
	}
J
 
Jiri Slaby 已提交
952

L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	return failure;
}

static int do_ibm_reset(struct saa7146 *saa)
{
	/* failure if decoder not previously programmed */
	if (saa->boardcfg[0] < 37)
		return -EIO;
	/* mute CS3310 */
	if (HaveCS3310)
		debiwrite(saa, debNormal, XILINX_CS3310_CMPLT, 0, 2);
	/* disable interrupts */
	saawrite(0, SAA7146_IER);
	saa->audhead = saa->audtail = 0;
	saa->vidhead = saa->vidtail = 0;
	/* tristate debi bus, disable debi transfers */
	saawrite(0x00880000, SAA7146_MC1);
	/* ensure posted write */
	saaread(SAA7146_MC1);
	mdelay(50);
	/* re-enable debi transfers */
	saawrite(0x00880088, SAA7146_MC1);
	/* set source Black */
	debiwrite(saa, debNormal, XILINX_CTL0, 0x1707, 2);
	/* begin IBM chip init */
	set_out_format(saa, CurrentMode);
	debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 4, 2);
	saaread(SAA7146_PSR);	/* wait for reset */
	mdelay(5);
	debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, 0, 2);
	debiread(saa, debNormal, IBM_MP2_CHIP_CONTROL, 2);
	debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, ChipControl, 2);
	debiwrite(saa, debNormal, IBM_MP2_CHIP_MODE, 0x2e, 2);
	if (NewCard) {
		mdelay(5);
		/* set i2s rate converter to 48KHz */
		debiwrite(saa, debNormal, 0x80c0, 6, 2);
		/* we must init CS8420 first since rev b pulls i2s */
		/* master clock low and CS4341 needs i2s master to */
		/* run the i2c port. */
J
 
Jiri Slaby 已提交
993
		if (HaveCS8420)
L
Linus Torvalds 已提交
994 995
			/* 0=consumer, 1=pro */
			initialize_cs8420(saa, 1);
J
 
Jiri Slaby 已提交
996

L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		mdelay(5);
		if (HaveCS4341)
			initialize_cs4341(saa);
	}
	debiwrite(saa, debNormal, IBM_MP2_INFC_CTL, 0x48, 2);
	debiwrite(saa, debNormal, IBM_MP2_BEEP_CTL, 0xa000, 2);
	debiwrite(saa, debNormal, IBM_MP2_DISP_LBOR, 0, 2);
	debiwrite(saa, debNormal, IBM_MP2_DISP_TBOR, 0, 2);
	if (NewCard)
		set_genlock_offset(saa, 0);
	debiwrite(saa, debNormal, IBM_MP2_FRNT_ATTEN, 0, 2);
	debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0x2000, 2);
	debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4552, 2);
	if (ibm_send_command(saa, IBM_MP2_CONFIG_DECODER,
J
 
Jiri Slaby 已提交
1011
			(ChipControl == 0x43 ? 0xe800 : 0xe000), 1)) {
L
Linus Torvalds 已提交
1012 1013 1014 1015
		printk(KERN_ERR "stradis%d: IBM config failed\n", saa->nr);
	}
	if (HaveCS3310) {
		int i = CS3310MaxLvl;
J
 
Jiri Slaby 已提交
1016
		debiwrite(saa, debNormal, XILINX_CS3310_CMPLT, ((i << 8)| i),2);
L
Linus Torvalds 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	}
	/* start video decoder */
	debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL, ChipControl, 2);
	/* 256k vid, 3520 bytes aud */
	debiwrite(saa, debNormal, IBM_MP2_RB_THRESHOLD, 0x4037, 2);
	debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4573, 2);
	ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
	/* enable buffer threshold irq */
	debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
	/* clear pending interrupts */
	debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
	debiwrite(saa, debNormal, XILINX_CTL0, 0x1711, 2);
J
 
Jiri Slaby 已提交
1029

L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	return 0;
}

/* load the decoder microcode */
static int initialize_ibmmpeg2(struct video_code *microcode)
{
	int i, num;
	struct saa7146 *saa;

	for (num = 0; num < saa_num; num++) {
		saa = &saa7146s[num];
		/* check that FPGA is loaded */
		debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0xa55a, 2);
J
 
Jiri Slaby 已提交
1043 1044
		i = debiread(saa, debNormal, IBM_MP2_OSD_SIZE, 2);
		if (i != 0xa55a) {
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
			printk(KERN_INFO "stradis%d: %04x != 0xa55a\n",
				saa->nr, i);
#if 0
			return -1;
#endif
		}
		if (!strncmp(microcode->loadwhat, "decoder.vid", 11)) {
			if (saa->boardcfg[0] > 27)
				continue;	/* skip to next card */
			/* load video control store */
J
 
Jiri Slaby 已提交
1055
			saa->boardcfg[1] = 0x13;	/* no-sync default */
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060
			debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 1, 2);
			debiwrite(saa, debNormal, IBM_MP2_PROC_IADDR, 0, 2);
			for (i = 0; i < microcode->datasize / 2; i++)
				debiwrite(saa, debNormal, IBM_MP2_PROC_IDATA,
					(microcode->data[i * 2] << 8) |
J
 
Jiri Slaby 已提交
1061
					microcode->data[i * 2 + 1], 2);
L
Linus Torvalds 已提交
1062 1063 1064
			debiwrite(saa, debNormal, IBM_MP2_PROC_IADDR, 0, 2);
			debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 0, 2);
			debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
J
 
Jiri Slaby 已提交
1065
				ChipControl, 2);
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
			saa->boardcfg[0] = 28;
		}
		if (!strncmp(microcode->loadwhat, "decoder.aud", 11)) {
			if (saa->boardcfg[0] > 35)
				continue;	/* skip to next card */
			/* load audio control store */
			debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 1, 2);
			debiwrite(saa, debNormal, IBM_MP2_AUD_IADDR, 0, 2);
			for (i = 0; i < microcode->datasize; i++)
				debiwrite(saa, debNormal, IBM_MP2_AUD_IDATA,
					microcode->data[i], 1);
			debiwrite(saa, debNormal, IBM_MP2_AUD_IADDR, 0, 2);
			debiwrite(saa, debNormal, IBM_MP2_WR_PROT, 0, 2);
			debiwrite(saa, debNormal, IBM_MP2_OSD_SIZE, 0x2000, 2);
			debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4552, 2);
			if (ibm_send_command(saa, IBM_MP2_CONFIG_DECODER,
J
 
Jiri Slaby 已提交
1082 1083 1084
					0xe000, 1)) {
				printk(KERN_ERR "stradis%d: IBM config "
					"failed\n", saa->nr);
L
Linus Torvalds 已提交
1085 1086 1087 1088 1089
				return -1;
			}
			/* set PWM to center value */
			if (NewCard) {
				debiwrite(saa, debNormal, XILINX_PWM,
J
 
Jiri Slaby 已提交
1090 1091
					saa->boardcfg[14] +
					(saa->boardcfg[13] << 8), 2);
L
Linus Torvalds 已提交
1092
			} else
J
 
Jiri Slaby 已提交
1093 1094
				debiwrite(saa, debNormal, XILINX_PWM, 0x46, 2);

L
Linus Torvalds 已提交
1095 1096
			if (HaveCS3310) {
				i = CS3310MaxLvl;
J
 
Jiri Slaby 已提交
1097 1098
				debiwrite(saa, debNormal, XILINX_CS3310_CMPLT,
					(i << 8) | i, 2);
L
Linus Torvalds 已提交
1099
			}
J
 
Jiri Slaby 已提交
1100 1101 1102
			printk(KERN_INFO "stradis%d: IBM MPEGCD%d Inited\n",
				saa->nr, 18 + (debiread(saa, debNormal,
				IBM_MP2_CHIP_CONTROL, 2) >> 12));
L
Linus Torvalds 已提交
1103 1104 1105
			/* start video decoder */
			debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
				ChipControl, 2);
J
 
Jiri Slaby 已提交
1106 1107
			debiwrite(saa, debNormal, IBM_MP2_RB_THRESHOLD, 0x4037,
				2);	/* 256k vid, 3520 bytes aud */
L
Linus Torvalds 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
			debiwrite(saa, debNormal, IBM_MP2_AUD_CTL, 0x4573, 2);
			ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
			/* enable buffer threshold irq */
			debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00c, 2);
			debiread(saa, debNormal, IBM_MP2_HOST_INT, 2);
			/* enable gpio irq */
			saawrite(0x00002000, SAA7146_GPIO_CTRL);
			/* enable decoder output to HPS */
			debiwrite(saa, debNormal, XILINX_CTL0, 0x1711, 2);
			saa->boardcfg[0] = 37;
		}
	}
J
 
Jiri Slaby 已提交
1120

L
Linus Torvalds 已提交
1121 1122 1123
	return 0;
}

J
 
Jiri Slaby 已提交
1124 1125 1126 1127
static u32 palette2fmt[] = {	/* some of these YUV translations are wrong */
	0xffffffff, 0x86000000, 0x87000000, 0x80000000, 0x8100000, 0x82000000,
	0x83000000, 0x00000000, 0x03000000, 0x03000000, 0x0a00000, 0x03000000,
	0x06000000, 0x00000000, 0x03000000, 0x0a000000, 0x0300000
L
Linus Torvalds 已提交
1128
};
J
 
Jiri Slaby 已提交
1129
static int bpp2fmt[4] = {
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134
	VIDEO_PALETTE_HI240, VIDEO_PALETTE_RGB565, VIDEO_PALETTE_RGB24,
	VIDEO_PALETTE_RGB32
};

/* I wish I could find a formula to calculate these... */
J
 
Jiri Slaby 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
static u32 h_prescale[64] = {
	0x10000000, 0x18040202, 0x18080000, 0x380c0606, 0x38100204, 0x38140808,
	0x38180000, 0x381c0000, 0x3820161c, 0x38242a3b, 0x38281230, 0x382c4460,
	0x38301040, 0x38340080, 0x38380000, 0x383c0000, 0x3840fefe, 0x3844ee9f,
	0x3848ee9f, 0x384cee9f, 0x3850ee9f, 0x38542a3b, 0x38581230, 0x385c0000,
	0x38600000, 0x38640000, 0x38680000, 0x386c0000, 0x38700000, 0x38740000,
	0x38780000, 0x387c0000, 0x30800000, 0x38840000, 0x38880000, 0x388c0000,
	0x38900000, 0x38940000, 0x38980000, 0x389c0000, 0x38a00000, 0x38a40000,
	0x38a80000, 0x38ac0000, 0x38b00000, 0x38b40000, 0x38b80000, 0x38bc0000,
	0x38c00000, 0x38c40000, 0x38c80000, 0x38cc0000, 0x38d00000, 0x38d40000,
	0x38d80000, 0x38dc0000, 0x38e00000, 0x38e40000, 0x38e80000, 0x38ec0000,
	0x38f00000, 0x38f40000, 0x38f80000, 0x38fc0000,
L
Linus Torvalds 已提交
1147
};
J
 
Jiri Slaby 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
static u32 v_gain[64] = {
	0x016000ff, 0x016100ff, 0x016100ff, 0x016200ff, 0x016200ff, 0x016200ff,
	0x016200ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff,
	0x016300ff, 0x016300ff, 0x016300ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
	0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
L
Linus Torvalds 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
};

static void saa7146_set_winsize(struct saa7146 *saa)
{
	u32 format;
	int offset, yacl, ysci;
	saa->win.color_fmt = format =
	    (saa->win.depth == 15) ? palette2fmt[VIDEO_PALETTE_RGB555] :
	    palette2fmt[bpp2fmt[(saa->win.bpp - 1) & 3]];
	offset = saa->win.x * saa->win.bpp + saa->win.y * saa->win.bpl;
	saawrite(saa->win.vidadr + offset, SAA7146_BASE_EVEN1);
	saawrite(saa->win.vidadr + offset + saa->win.bpl, SAA7146_BASE_ODD1);
	saawrite(saa->win.bpl * 2, SAA7146_PITCH1);
	saawrite(saa->win.vidadr + saa->win.bpl * saa->win.sheight,
		 SAA7146_PROT_ADDR1);
	saawrite(0, SAA7146_PAGE1);
J
 
Jiri Slaby 已提交
1176
	saawrite(format | 0x60, SAA7146_CLIP_FORMAT_CTRL);
L
Linus Torvalds 已提交
1177 1178 1179
	offset = (704 / (saa->win.width - 1)) & 0x3f;
	saawrite(h_prescale[offset], SAA7146_HPS_H_PRESCALE);
	offset = (720896 / saa->win.width) / (offset + 1);
J
 
Jiri Slaby 已提交
1180
	saawrite((offset << 12) | 0x0c, SAA7146_HPS_H_SCALE);
L
Linus Torvalds 已提交
1181
	if (CurrentMode == VIDEO_MODE_NTSC) {
J
 
Jiri Slaby 已提交
1182
		yacl = /*(480 / saa->win.height - 1) & 0x3f */ 0;
L
Linus Torvalds 已提交
1183 1184
		ysci = 1024 - (saa->win.height * 1024 / 480);
	} else {
J
 
Jiri Slaby 已提交
1185
		yacl = /*(576 / saa->win.height - 1) & 0x3f */ 0;
L
Linus Torvalds 已提交
1186 1187
		ysci = 1024 - (saa->win.height * 1024 / 576);
	}
J
 
Jiri Slaby 已提交
1188
	saawrite((1 << 31) | (ysci << 21) | (yacl << 15), SAA7146_HPS_V_SCALE);
L
Linus Torvalds 已提交
1189 1190
	saawrite(v_gain[yacl], SAA7146_HPS_V_GAIN);
	saawrite(((SAA7146_MC2_UPLD_DMA1 | SAA7146_MC2_UPLD_HPS_V |
J
 
Jiri Slaby 已提交
1191 1192
		SAA7146_MC2_UPLD_HPS_H) << 16) | (SAA7146_MC2_UPLD_DMA1 |
		SAA7146_MC2_UPLD_HPS_V | SAA7146_MC2_UPLD_HPS_H), SAA7146_MC2);
L
Linus Torvalds 已提交
1193 1194 1195
}

/* clip_draw_rectangle(cm,x,y,w,h) -- handle clipping an area
1196 1197 1198
 * bitmap is fixed width, 128 bytes (1024 pixels represented)
 * arranged most-sigificant-bit-left in 32-bit words
 * based on saa7146 clipping hardware, it swaps bytes if LE
L
Linus Torvalds 已提交
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 1225 1226
 * much of this makes up for egcs brain damage -- so if you
 * are wondering "why did he do this?" it is because the C
 * was adjusted to generate the optimal asm output without
 * writing non-portable __asm__ directives.
 */

static void clip_draw_rectangle(u32 *clipmap, int x, int y, int w, int h)
{
	register int startword, endword;
	register u32 bitsleft, bitsright;
	u32 *temp;
	if (x < 0) {
		w += x;
		x = 0;
	}
	if (y < 0) {
		h += y;
		y = 0;
	}
	if (w <= 0 || h <= 0 || x > 1023 || y > 639)
		return;		/* throw away bad clips */
	if (x + w > 1024)
		w = 1024 - x;
	if (y + h > 640)
		h = 640 - y;
	startword = (x >> 5);
	endword = ((x + w) >> 5);
	bitsleft = (0xffffffff >> (x & 31));
J
 
Jiri Slaby 已提交
1227 1228
	bitsright = (0xffffffff << (~((x + w) - (endword << 5))));
	temp = &clipmap[(y << 5) + startword];
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	w = endword - startword;
	if (!w) {
		bitsleft |= bitsright;
		for (y = 0; y < h; y++) {
			*temp |= bitsleft;
			temp += 32;
		}
	} else {
		for (y = 0; y < h; y++) {
			*temp++ |= bitsleft;
			for (x = 1; x < w; x++)
				*temp++ = 0xffffffff;
			*temp |= bitsright;
			temp += (32 - w);
		}
	}
}

static void make_clip_tab(struct saa7146 *saa, struct video_clip *cr, int ncr)
{
	int i, width, height;
	u32 *clipmap;

	clipmap = saa->dmavid2;
J
 
Jiri Slaby 已提交
1253 1254 1255 1256 1257
	if ((width = saa->win.width) > 1023)
		width = 1023;	/* sanity check */
	if ((height = saa->win.height) > 640)
		height = 639;	/* sanity check */
	if (ncr > 0) {		/* rectangles pased */
L
Linus Torvalds 已提交
1258
		/* convert rectangular clips to a bitmap */
J
 
Jiri Slaby 已提交
1259
		memset(clipmap, 0, VIDEO_CLIPMAP_SIZE);	/* clear map */
L
Linus Torvalds 已提交
1260 1261 1262 1263
		for (i = 0; i < ncr; i++)
			clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
				cr[i].width, cr[i].height);
	}
1264
	/* clip against viewing window AND screen
L
Linus Torvalds 已提交
1265 1266
	   so we do not have to rely on the user program
	 */
J
 
Jiri Slaby 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275
	clip_draw_rectangle(clipmap, (saa->win.x + width > saa->win.swidth) ?
		(saa->win.swidth - saa->win.x) : width, 0, 1024, 768);
	clip_draw_rectangle(clipmap, 0,
		(saa->win.y + height > saa->win.sheight) ?
		(saa->win.sheight - saa->win.y) : height, 1024, 768);
	if (saa->win.x < 0)
		clip_draw_rectangle(clipmap, 0, 0, -saa->win.x, 768);
	if (saa->win.y < 0)
		clip_draw_rectangle(clipmap, 0, 0, 1024, -saa->win.y);
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
}

static int saa_ioctl(struct inode *inode, struct file *file,
		     unsigned int cmd, unsigned long argl)
{
	struct saa7146 *saa = file->private_data;
	void __user *arg = (void __user *)argl;

	switch (cmd) {
	case VIDIOCGCAP:
		{
			struct video_capability b;
			strcpy(b.name, saa->video_dev.name);
J
 
Jiri Slaby 已提交
1289 1290 1291
			b.type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY |
				VID_TYPE_CLIPPING | VID_TYPE_FRAMERAM |
				VID_TYPE_SCALES;
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
			b.channels = 1;
			b.audios = 1;
			b.maxwidth = 768;
			b.maxheight = 576;
			b.minwidth = 32;
			b.minheight = 32;
			if (copy_to_user(arg, &b, sizeof(b)))
				return -EFAULT;
			return 0;
		}
	case VIDIOCGPICT:
		{
			struct video_picture p = saa->picture;
			if (saa->win.depth == 8)
				p.palette = VIDEO_PALETTE_HI240;
			if (saa->win.depth == 15)
				p.palette = VIDEO_PALETTE_RGB555;
			if (saa->win.depth == 16)
				p.palette = VIDEO_PALETTE_RGB565;
			if (saa->win.depth == 24)
				p.palette = VIDEO_PALETTE_RGB24;
			if (saa->win.depth == 32)
				p.palette = VIDEO_PALETTE_RGB32;
			if (copy_to_user(arg, &p, sizeof(p)))
				return -EFAULT;
			return 0;
		}
	case VIDIOCSPICT:
		{
			struct video_picture p;
			u32 format;
			if (copy_from_user(&p, arg, sizeof(p)))
				return -EFAULT;
1325
			if (p.palette < ARRAY_SIZE(palette2fmt)) {
L
Linus Torvalds 已提交
1326 1327
				format = palette2fmt[p.palette];
				saa->win.color_fmt = format;
J
 
Jiri Slaby 已提交
1328 1329
				saawrite(format | 0x60,
					SAA7146_CLIP_FORMAT_CTRL);
L
Linus Torvalds 已提交
1330 1331
			}
			saawrite(((p.brightness & 0xff00) << 16) |
J
 
Jiri Slaby 已提交
1332 1333
				((p.contrast & 0xfe00) << 7) |
				((p.colour & 0xfe00) >> 9), SAA7146_BCS_CTRL);
L
Linus Torvalds 已提交
1334 1335 1336
			saa->picture = p;
			/* upload changed registers */
			saawrite(((SAA7146_MC2_UPLD_HPS_H |
J
 
Jiri Slaby 已提交
1337 1338 1339
				SAA7146_MC2_UPLD_HPS_V) << 16) |
				SAA7146_MC2_UPLD_HPS_H |
				SAA7146_MC2_UPLD_HPS_V, SAA7146_MC2);
L
Linus Torvalds 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
			return 0;
		}
	case VIDIOCSWIN:
		{
			struct video_window vw;
			struct video_clip *vcp = NULL;

			if (copy_from_user(&vw, arg, sizeof(vw)))
				return -EFAULT;

J
 
Jiri Slaby 已提交
1350 1351 1352 1353
			/* stop capture */
			if (vw.flags || vw.width < 16 || vw.height < 16) {
				saawrite((SAA7146_MC1_TR_E_1 << 16),
					SAA7146_MC1);
L
Linus Torvalds 已提交
1354 1355
				return -EINVAL;
			}
J
 
Jiri Slaby 已提交
1356 1357
			/* 32-bit align start and adjust width */
			if (saa->win.bpp < 4) {
L
Linus Torvalds 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
				int i = vw.x;
				vw.x = (vw.x + 3) & ~3;
				i = vw.x - i;
				vw.width -= i;
			}
			saa->win.x = vw.x;
			saa->win.y = vw.y;
			saa->win.width = vw.width;
			if (saa->win.width > 768)
				saa->win.width = 768;
			saa->win.height = vw.height;
			if (CurrentMode == VIDEO_MODE_NTSC) {
				if (saa->win.height > 480)
					saa->win.height = 480;
			} else {
				if (saa->win.height > 576)
					saa->win.height = 576;
			}

			/* stop capture */
			saawrite((SAA7146_MC1_TR_E_1 << 16), SAA7146_MC1);
			saa7146_set_winsize(saa);

			/*
			 *    Do any clips.
			 */
			if (vw.clipcount < 0) {
				if (copy_from_user(saa->dmavid2, vw.clips,
J
 
Jiri Slaby 已提交
1386
						VIDEO_CLIPMAP_SIZE))
L
Linus Torvalds 已提交
1387
					return -EFAULT;
J
 
Jiri Slaby 已提交
1388
			} else if (vw.clipcount > 16384) {
L
Linus Torvalds 已提交
1389 1390
				return -EINVAL;
			} else if (vw.clipcount > 0) {
J
 
Jiri Slaby 已提交
1391 1392 1393 1394
				vcp = vmalloc(sizeof(struct video_clip) *
					vw.clipcount);
				if (vcp == NULL)
					return -ENOMEM;
L
Linus Torvalds 已提交
1395
				if (copy_from_user(vcp, vw.clips,
J
 
Jiri Slaby 已提交
1396 1397
						sizeof(struct video_clip) *
						vw.clipcount)) {
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402
					vfree(vcp);
					return -EFAULT;
				}
			} else	/* nothing clipped */
				memset(saa->dmavid2, 0, VIDEO_CLIPMAP_SIZE);
J
 
Jiri Slaby 已提交
1403

L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
			make_clip_tab(saa, vcp, vw.clipcount);
			if (vw.clipcount > 0)
				vfree(vcp);

			/* start capture & clip dma if we have an address */
			if ((saa->cap & 3) && saa->win.vidadr != 0)
				saawrite(((SAA7146_MC1_TR_E_1 |
					SAA7146_MC1_TR_E_2) << 16) | 0xffff,
					SAA7146_MC1);
			return 0;
		}
	case VIDIOCGWIN:
		{
			struct video_window vw;
			vw.x = saa->win.x;
			vw.y = saa->win.y;
			vw.width = saa->win.width;
			vw.height = saa->win.height;
			vw.chromakey = 0;
			vw.flags = 0;
			if (copy_to_user(arg, &vw, sizeof(vw)))
				return -EFAULT;
			return 0;
		}
	case VIDIOCCAPTURE:
		{
			int v;
			if (copy_from_user(&v, arg, sizeof(v)))
				return -EFAULT;
			if (v == 0) {
				saa->cap &= ~1;
				saawrite((SAA7146_MC1_TR_E_1 << 16),
J
 
Jiri Slaby 已提交
1436
					SAA7146_MC1);
L
Linus Torvalds 已提交
1437 1438
			} else {
				if (saa->win.vidadr == 0 || saa->win.width == 0
J
 
Jiri Slaby 已提交
1439
						|| saa->win.height == 0)
L
Linus Torvalds 已提交
1440 1441 1442
					return -EINVAL;
				saa->cap |= 1;
				saawrite((SAA7146_MC1_TR_E_1 << 16) | 0xffff,
J
 
Jiri Slaby 已提交
1443
					SAA7146_MC1);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449
			}
			return 0;
		}
	case VIDIOCGFBUF:
		{
			struct video_buffer v;
J
 
Jiri Slaby 已提交
1450
			v.base = (void *)saa->win.vidadr;
L
Linus Torvalds 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
			v.height = saa->win.sheight;
			v.width = saa->win.swidth;
			v.depth = saa->win.depth;
			v.bytesperline = saa->win.bpl;
			if (copy_to_user(arg, &v, sizeof(v)))
				return -EFAULT;
			return 0;

		}
	case VIDIOCSFBUF:
		{
			struct video_buffer v;
			if (!capable(CAP_SYS_ADMIN))
				return -EPERM;
			if (copy_from_user(&v, arg, sizeof(v)))
				return -EFAULT;
			if (v.depth != 8 && v.depth != 15 && v.depth != 16 &&
J
 
Jiri Slaby 已提交
1468
			    v.depth != 24 && v.depth != 32 && v.width > 16 &&
L
Linus Torvalds 已提交
1469 1470 1471
			    v.height > 16 && v.bytesperline > 16)
				return -EINVAL;
			if (v.base)
J
 
Jiri Slaby 已提交
1472
				saa->win.vidadr = (unsigned long)v.base;
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478
			saa->win.sheight = v.height;
			saa->win.swidth = v.width;
			saa->win.bpp = ((v.depth + 7) & 0x38) / 8;
			saa->win.depth = v.depth;
			saa->win.bpl = v.bytesperline;

J
 
Jiri Slaby 已提交
1479 1480 1481
			DEBUG(printk("Display at %p is %d by %d, bytedepth %d, "
					"bpl %d\n", v.base, v.width, v.height,
					saa->win.bpp, saa->win.bpl));
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
			saa7146_set_winsize(saa);
			return 0;
		}
	case VIDIOCKEY:
		{
			/* Will be handled higher up .. */
			return 0;
		}

	case VIDIOCGAUDIO:
		{
			struct video_audio v;
			v = saa->audio_dev;
			v.flags &= ~(VIDEO_AUDIO_MUTE | VIDEO_AUDIO_MUTABLE);
			v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
			strcpy(v.name, "MPEG");
			v.mode = VIDEO_SOUND_STEREO;
			if (copy_to_user(arg, &v, sizeof(v)))
				return -EFAULT;
			return 0;
		}
	case VIDIOCSAUDIO:
		{
			struct video_audio v;
			int i;
			if (copy_from_user(&v, arg, sizeof(v)))
				return -EFAULT;
J
 
Jiri Slaby 已提交
1509
			i = (~(v.volume >> 8)) & 0xff;
L
Linus Torvalds 已提交
1510
			if (!HaveCS4341) {
J
 
Jiri Slaby 已提交
1511
				if (v.flags & VIDEO_AUDIO_MUTE)
L
Linus Torvalds 已提交
1512
					debiwrite(saa, debNormal,
J
 
Jiri Slaby 已提交
1513
						IBM_MP2_FRNT_ATTEN, 0xffff, 2);
L
Linus Torvalds 已提交
1514 1515
				if (!(v.flags & VIDEO_AUDIO_MUTE))
					debiwrite(saa, debNormal,
J
 
Jiri Slaby 已提交
1516
						IBM_MP2_FRNT_ATTEN, 0x0000, 2);
L
Linus Torvalds 已提交
1517 1518 1519
				if (v.flags & VIDEO_AUDIO_VOLUME)
					debiwrite(saa, debNormal,
						IBM_MP2_FRNT_ATTEN,
J
 
Jiri Slaby 已提交
1520
						(i << 8) | i, 2);
L
Linus Torvalds 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
			} else {
				if (v.flags & VIDEO_AUDIO_MUTE)
					cs4341_setlevel(saa, 0xff, 0xff);
				if (!(v.flags & VIDEO_AUDIO_MUTE))
					cs4341_setlevel(saa, 0, 0);
				if (v.flags & VIDEO_AUDIO_VOLUME)
					cs4341_setlevel(saa, i, i);
			}
			saa->audio_dev = v;
			return 0;
		}

	case VIDIOCGUNIT:
		{
			struct video_unit vu;
			vu.video = saa->video_dev.minor;
			vu.vbi = VIDEO_NO_UNIT;
			vu.radio = VIDEO_NO_UNIT;
			vu.audio = VIDEO_NO_UNIT;
			vu.teletext = VIDEO_NO_UNIT;
			if (copy_to_user(arg, &vu, sizeof(vu)))
				return -EFAULT;
			return 0;
		}
	case VIDIOCSPLAYMODE:
		{
			struct video_play_mode pmode;
J
 
Jiri Slaby 已提交
1548 1549
			if (copy_from_user((void *)&pmode, arg,
					sizeof(struct video_play_mode)))
L
Linus Torvalds 已提交
1550 1551
				return -EFAULT;
			switch (pmode.mode) {
J
 
Jiri Slaby 已提交
1552 1553
			case VID_PLAY_VID_OUT_MODE:
				if (pmode.p1 != VIDEO_MODE_NTSC &&
L
Linus Torvalds 已提交
1554
						pmode.p1 != VIDEO_MODE_PAL)
J
 
Jiri Slaby 已提交
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
					return -EINVAL;
				set_out_format(saa, pmode.p1);
				return 0;
			case VID_PLAY_GENLOCK:
				debiwrite(saa, debNormal, XILINX_CTL0,
					pmode.p1 ? 0x8000 : 0x8080, 2);
				if (NewCard)
					set_genlock_offset(saa, pmode.p2);
				return 0;
			case VID_PLAY_NORMAL:
				debiwrite(saa, debNormal,
					IBM_MP2_CHIP_CONTROL, ChipControl, 2);
				ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
				saa->playmode = pmode.mode;
				return 0;
			case VID_PLAY_PAUSE:
				/* IBM removed the PAUSE command */
				/* they say use SINGLE_FRAME now */
			case VID_PLAY_SINGLE_FRAME:
				ibm_send_command(saa, IBM_MP2_SINGLE_FRAME,0,0);
				if (saa->playmode == pmode.mode) {
L
Linus Torvalds 已提交
1576 1577 1578
					debiwrite(saa, debNormal,
						IBM_MP2_CHIP_CONTROL,
						ChipControl, 2);
J
 
Jiri Slaby 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
				}
				saa->playmode = pmode.mode;
				return 0;
			case VID_PLAY_FAST_FORWARD:
				ibm_send_command(saa, IBM_MP2_FAST_FORWARD,0,0);
				saa->playmode = pmode.mode;
				return 0;
			case VID_PLAY_SLOW_MOTION:
				ibm_send_command(saa, IBM_MP2_SLOW_MOTION,
					pmode.p1, 0);
				saa->playmode = pmode.mode;
				return 0;
			case VID_PLAY_IMMEDIATE_NORMAL:
				/* ensure transfers resume */
				debiwrite(saa, debNormal,
					IBM_MP2_CHIP_CONTROL, ChipControl, 2);
				ibm_send_command(saa, IBM_MP2_IMED_NORM_PLAY,
					0, 0);
				saa->playmode = VID_PLAY_NORMAL;
				return 0;
			case VID_PLAY_SWITCH_CHANNELS:
				saa->audhead = saa->audtail = 0;
				saa->vidhead = saa->vidtail = 0;
				ibm_send_command(saa, IBM_MP2_FREEZE_FRAME,0,1);
				ibm_send_command(saa, IBM_MP2_RESET_AUD_RATE,
					0, 1);
				debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
					0, 2);
				ibm_send_command(saa, IBM_MP2_CHANNEL_SWITCH,
					0, 1);
				debiwrite(saa, debNormal, IBM_MP2_CHIP_CONTROL,
					ChipControl, 2);
				ibm_send_command(saa, IBM_MP2_PLAY, 0, 0);
				saa->playmode = VID_PLAY_NORMAL;
				return 0;
			case VID_PLAY_FREEZE_FRAME:
				ibm_send_command(saa, IBM_MP2_FREEZE_FRAME,0,0);
				saa->playmode = pmode.mode;
				return 0;
			case VID_PLAY_STILL_MODE:
				ibm_send_command(saa, IBM_MP2_SET_STILL_MODE,
					0, 0);
				saa->playmode = pmode.mode;
				return 0;
			case VID_PLAY_MASTER_MODE:
				if (pmode.p1 == VID_PLAY_MASTER_NONE)
					saa->boardcfg[1] = 0x13;
				else if (pmode.p1 == VID_PLAY_MASTER_VIDEO)
					saa->boardcfg[1] = 0x23;
				else if (pmode.p1 == VID_PLAY_MASTER_AUDIO)
					saa->boardcfg[1] = 0x43;
				else
					return -EINVAL;
				debiwrite(saa, debNormal,
					  IBM_MP2_CHIP_CONTROL, ChipControl, 2);
				return 0;
			case VID_PLAY_ACTIVE_SCANLINES:
				if (CurrentMode == VIDEO_MODE_PAL) {
					if (pmode.p1 < 1 || pmode.p2 > 625)
L
Linus Torvalds 已提交
1638
						return -EINVAL;
J
 
Jiri Slaby 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
					saa->boardcfg[5] = pmode.p1;
					saa->boardcfg[55] = (pmode.p1 +
						(pmode.p2 / 2) - 1) & 0xff;
				} else {
					if (pmode.p1 < 4 || pmode.p2 > 525)
						return -EINVAL;
					saa->boardcfg[4] = pmode.p1;
					saa->boardcfg[54] = (pmode.p1 +
						(pmode.p2 / 2) - 4) & 0xff;
				}
				set_out_format(saa, CurrentMode);
			case VID_PLAY_RESET:
				return do_ibm_reset(saa);
			case VID_PLAY_END_MARK:
				if (saa->endmarktail < saa->endmarkhead) {
					if (saa->endmarkhead -
L
Linus Torvalds 已提交
1655 1656
							saa->endmarktail < 2)
						return -ENOSPC;
J
 
Jiri Slaby 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
				} else if (saa->endmarkhead <=saa->endmarktail){
					if (saa->endmarktail - saa->endmarkhead
							> (MAX_MARKS - 2))
						return -ENOSPC;
				} else
					return -ENOSPC;
				saa->endmark[saa->endmarktail] = saa->audtail;
				saa->endmarktail++;
				if (saa->endmarktail >= MAX_MARKS)
					saa->endmarktail = 0;
L
Linus Torvalds 已提交
1667 1668 1669 1670 1671 1672
			}
			return -EINVAL;
		}
	case VIDIOCSWRITEMODE:
		{
			int mode;
J
 
Jiri Slaby 已提交
1673 1674
			if (copy_from_user((void *)&mode, arg, sizeof(int)))
				return -EFAULT;
L
Linus Torvalds 已提交
1675
			if (mode == VID_WRITE_MPEG_AUD ||
J
 
Jiri Slaby 已提交
1676 1677 1678 1679
					mode == VID_WRITE_MPEG_VID ||
					mode == VID_WRITE_CC ||
					mode == VID_WRITE_TTX ||
					mode == VID_WRITE_OSD) {
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
				saa->writemode = mode;
				return 0;
			}
			return -EINVAL;
		}
	case VIDIOCSMICROCODE:
		{
			struct video_code ucode;
			__u8 *udata;
			int i;
			if (copy_from_user(&ucode, arg, sizeof(ucode)))
				return -EFAULT;
			if (ucode.datasize > 65536 || ucode.datasize < 1024 ||
J
 
Jiri Slaby 已提交
1693
					strncmp(ucode.loadwhat, "dec", 3))
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701
				return -EINVAL;
			if ((udata = vmalloc(ucode.datasize)) == NULL)
				return -ENOMEM;
			if (copy_from_user(udata, ucode.data, ucode.datasize)) {
				vfree(udata);
				return -EFAULT;
			}
			ucode.data = udata;
J
 
Jiri Slaby 已提交
1702 1703
			if (!strncmp(ucode.loadwhat, "decoder.aud", 11) ||
				!strncmp(ucode.loadwhat, "decoder.vid", 11))
L
Linus Torvalds 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
				i = initialize_ibmmpeg2(&ucode);
			else
				i = initialize_fpga(&ucode);
			vfree(udata);
			if (i)
				return -EINVAL;
			return 0;

		}
	case VIDIOCGCHAN:	/* this makes xawtv happy */
		{
			struct video_channel v;
			if (copy_from_user(&v, arg, sizeof(v)))
				return -EFAULT;
			v.flags = VIDEO_VC_AUDIO;
			v.tuners = 0;
			v.type = VID_TYPE_MPEG_DECODER;
			v.norm = CurrentMode;
			strcpy(v.name, "MPEG2");
			if (copy_to_user(arg, &v, sizeof(v)))
				return -EFAULT;
			return 0;
		}
	case VIDIOCSCHAN:	/* this makes xawtv happy */
		{
			struct video_channel v;
			if (copy_from_user(&v, arg, sizeof(v)))
				return -EFAULT;
			/* do nothing */
			return 0;
		}
	default:
		return -ENOIOCTLCMD;
	}
	return 0;
}

static int saa_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct saa7146 *saa = file->private_data;
	printk(KERN_DEBUG "stradis%d: saa_mmap called\n", saa->nr);
	return -EINVAL;
}

J
 
Jiri Slaby 已提交
1748 1749
static ssize_t saa_read(struct file *file, char __user * buf,
	size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
1750 1751 1752 1753
{
	return -EINVAL;
}

J
 
Jiri Slaby 已提交
1754 1755
static ssize_t saa_write(struct file *file, const char __user * buf,
	size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
{
	struct saa7146 *saa = file->private_data;
	unsigned long todo = count;
	int blocksize, split;
	unsigned long flags;

	while (todo > 0) {
		if (saa->writemode == VID_WRITE_MPEG_AUD) {
			spin_lock_irqsave(&saa->lock, flags);
			if (saa->audhead <= saa->audtail)
J
 
Jiri Slaby 已提交
1766 1767
				blocksize = 65536 -
					(saa->audtail - saa->audhead);
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772
			else
				blocksize = saa->audhead - saa->audtail;
			spin_unlock_irqrestore(&saa->lock, flags);
			if (blocksize < 16384) {
				saawrite(SAA7146_PSR_DEBI_S |
J
 
Jiri Slaby 已提交
1773
					SAA7146_PSR_PIN1, SAA7146_IER);
L
Linus Torvalds 已提交
1774 1775 1776 1777 1778 1779
				saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
				/* wait for buffer space to open */
				interruptible_sleep_on(&saa->audq);
			}
			spin_lock_irqsave(&saa->lock, flags);
			if (saa->audhead <= saa->audtail) {
J
 
Jiri Slaby 已提交
1780 1781
				blocksize = 65536 -
					(saa->audtail - saa->audhead);
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
				split = 65536 - saa->audtail;
			} else {
				blocksize = saa->audhead - saa->audtail;
				split = 65536;
			}
			spin_unlock_irqrestore(&saa->lock, flags);
			blocksize--;
			if (blocksize > todo)
				blocksize = todo;
			/* double check that we really have space */
			if (!blocksize)
				return -ENOSPC;
			if (split < blocksize) {
				if (copy_from_user(saa->audbuf +
J
 
Jiri Slaby 已提交
1796
						saa->audtail, buf, split))
L
Linus Torvalds 已提交
1797 1798 1799 1800 1801 1802 1803
					return -EFAULT;
				buf += split;
				todo -= split;
				blocksize -= split;
				saa->audtail = 0;
			}
			if (copy_from_user(saa->audbuf + saa->audtail, buf,
J
 
Jiri Slaby 已提交
1804
					blocksize))
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812
				return -EFAULT;
			saa->audtail += blocksize;
			todo -= blocksize;
			buf += blocksize;
			saa->audtail &= 0xffff;
		} else if (saa->writemode == VID_WRITE_MPEG_VID) {
			spin_lock_irqsave(&saa->lock, flags);
			if (saa->vidhead <= saa->vidtail)
J
 
Jiri Slaby 已提交
1813 1814
				blocksize = 524288 -
					(saa->vidtail - saa->vidhead);
L
Linus Torvalds 已提交
1815 1816 1817 1818 1819
			else
				blocksize = saa->vidhead - saa->vidtail;
			spin_unlock_irqrestore(&saa->lock, flags);
			if (blocksize < 65536) {
				saawrite(SAA7146_PSR_DEBI_S |
J
 
Jiri Slaby 已提交
1820
					SAA7146_PSR_PIN1, SAA7146_IER);
L
Linus Torvalds 已提交
1821 1822 1823 1824 1825 1826
				saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
				/* wait for buffer space to open */
				interruptible_sleep_on(&saa->vidq);
			}
			spin_lock_irqsave(&saa->lock, flags);
			if (saa->vidhead <= saa->vidtail) {
J
 
Jiri Slaby 已提交
1827 1828
				blocksize = 524288 -
					(saa->vidtail - saa->vidhead);
L
Linus Torvalds 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
				split = 524288 - saa->vidtail;
			} else {
				blocksize = saa->vidhead - saa->vidtail;
				split = 524288;
			}
			spin_unlock_irqrestore(&saa->lock, flags);
			blocksize--;
			if (blocksize > todo)
				blocksize = todo;
			/* double check that we really have space */
			if (!blocksize)
				return -ENOSPC;
			if (split < blocksize) {
				if (copy_from_user(saa->vidbuf +
J
 
Jiri Slaby 已提交
1843
						saa->vidtail, buf, split))
L
Linus Torvalds 已提交
1844 1845 1846 1847 1848 1849 1850
					return -EFAULT;
				buf += split;
				todo -= split;
				blocksize -= split;
				saa->vidtail = 0;
			}
			if (copy_from_user(saa->vidbuf + saa->vidtail, buf,
J
 
Jiri Slaby 已提交
1851
					blocksize))
L
Linus Torvalds 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
				return -EFAULT;
			saa->vidtail += blocksize;
			todo -= blocksize;
			buf += blocksize;
			saa->vidtail &= 0x7ffff;
		} else if (saa->writemode == VID_WRITE_OSD) {
			if (count > 131072)
				return -ENOSPC;
			if (copy_from_user(saa->osdbuf, buf, count))
				return -EFAULT;
			buf += count;
			saa->osdhead = 0;
			saa->osdtail = count;
			debiwrite(saa, debNormal, IBM_MP2_OSD_ADDR, 0, 2);
			debiwrite(saa, debNormal, IBM_MP2_OSD_LINK_ADDR, 0, 2);
			debiwrite(saa, debNormal, IBM_MP2_MASK0, 0xc00d, 2);
			debiwrite(saa, debNormal, IBM_MP2_DISP_MODE,
J
 
Jiri Slaby 已提交
1869 1870
				debiread(saa, debNormal,
					IBM_MP2_DISP_MODE, 2) | 1, 2);
L
Linus Torvalds 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
			/* trigger osd data transfer */
			saawrite(SAA7146_PSR_DEBI_S |
				 SAA7146_PSR_PIN1, SAA7146_IER);
			saawrite(SAA7146_PSR_PIN1, SAA7146_PSR);
		}
	}
	return count;
}

static int saa_open(struct inode *inode, struct file *file)
{
J
 
Jiri Slaby 已提交
1882 1883
	struct video_device *vdev = video_devdata(file);
	struct saa7146 *saa = container_of(vdev, struct saa7146, video_dev);
L
Linus Torvalds 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897

	file->private_data = saa;

	saa->user++;
	if (saa->user > 1)
		return 0;	/* device open already, don't reset */
	saa->writemode = VID_WRITE_MPEG_VID;	/* default to video */
	return 0;
}

static int saa_release(struct inode *inode, struct file *file)
{
	struct saa7146 *saa = file->private_data;
	saa->user--;
J
 
Jiri Slaby 已提交
1898

L
Linus Torvalds 已提交
1899 1900 1901 1902 1903 1904
	if (saa->user > 0)	/* still someone using device */
		return 0;
	saawrite(0x007f0000, SAA7146_MC1);	/* stop all overlay dma */
	return 0;
}

1905
static const struct file_operations saa_fops = {
J
 
Jiri Slaby 已提交
1906 1907 1908 1909
	.owner = THIS_MODULE,
	.open = saa_open,
	.release = saa_release,
	.ioctl = saa_ioctl,
1910
#ifdef CONFIG_COMPAT
J
 
Jiri Slaby 已提交
1911
	.compat_ioctl = v4l_compat_ioctl32,
1912
#endif
J
 
Jiri Slaby 已提交
1913 1914 1915 1916
	.read = saa_read,
	.llseek = no_llseek,
	.write = saa_write,
	.mmap = saa_mmap,
L
Linus Torvalds 已提交
1917 1918 1919
};

/* template for video_device-structure */
J
 
Jiri Slaby 已提交
1920 1921 1922 1923
static struct video_device saa_template = {
	.name = "SAA7146A",
	.fops = &saa_fops,
	.minor = -1,
L
Linus Torvalds 已提交
1924 1925
};

J
 
Jiri Slaby 已提交
1926
static int __devinit configure_saa7146(struct pci_dev *pdev, int num)
L
Linus Torvalds 已提交
1927
{
J
 
Jiri Slaby 已提交
1928 1929
	int retval;
	struct saa7146 *saa = pci_get_drvdata(pdev);
J
 
Jiri Slaby 已提交
1930

L
Linus Torvalds 已提交
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
	saa->endmarkhead = saa->endmarktail = 0;
	saa->win.x = saa->win.y = 0;
	saa->win.width = saa->win.cropwidth = 720;
	saa->win.height = saa->win.cropheight = 480;
	saa->win.cropx = saa->win.cropy = 0;
	saa->win.bpp = 2;
	saa->win.depth = 16;
	saa->win.color_fmt = palette2fmt[VIDEO_PALETTE_RGB565];
	saa->win.bpl = 1024 * saa->win.bpp;
	saa->win.swidth = 1024;
	saa->win.sheight = 768;
	saa->picture.brightness = 32768;
	saa->picture.contrast = 38768;
	saa->picture.colour = 32768;
	saa->cap = 0;
	saa->nr = num;
	saa->playmode = VID_PLAY_NORMAL;
	memset(saa->boardcfg, 0, 64);	/* clear board config area */
	saa->saa7146_mem = NULL;
	saa->dmavid1 = saa->dmavid2 = saa->dmavid3 = saa->dmaa1in =
	    saa->dmaa1out = saa->dmaa2in = saa->dmaa2out =
	    saa->pagevid1 = saa->pagevid2 = saa->pagevid3 = saa->pagea1in =
	    saa->pagea1out = saa->pagea2in = saa->pagea2out =
	    saa->pagedebi = saa->dmaRPS1 = saa->dmaRPS2 = saa->pageRPS1 =
	    saa->pageRPS2 = NULL;
	saa->audbuf = saa->vidbuf = saa->osdbuf = saa->dmadebi = NULL;
	saa->audhead = saa->vidtail = 0;

	init_waitqueue_head(&saa->i2cq);
	init_waitqueue_head(&saa->audq);
	init_waitqueue_head(&saa->debiq);
	init_waitqueue_head(&saa->vidq);
	spin_lock_init(&saa->lock);

J
 
Jiri Slaby 已提交
1965 1966 1967 1968 1969
	retval = pci_enable_device(pdev);
	if (retval) {
		dev_err(&pdev->dev, "%d: pci_enable_device failed!\n", num);
		goto err;
	}
J
 
Jiri Slaby 已提交
1970

J
 
Jiri Slaby 已提交
1971 1972
	saa->id = pdev->device;
	saa->irq = pdev->irq;
L
Linus Torvalds 已提交
1973
	saa->video_dev.minor = -1;
J
 
Jiri Slaby 已提交
1974 1975
	saa->saa7146_adr = pci_resource_start(pdev, 0);
	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &saa->revision);
L
Linus Torvalds 已提交
1976 1977

	saa->saa7146_mem = ioremap(saa->saa7146_adr, 0x200);
J
 
Jiri Slaby 已提交
1978 1979 1980 1981 1982
	if (saa->saa7146_mem == NULL) {
		dev_err(&pdev->dev, "%d: ioremap failed!\n", num);
		retval = -EIO;
		goto err;
	}
L
Linus Torvalds 已提交
1983 1984 1985

	memcpy(&saa->video_dev, &saa_template, sizeof(saa_template));
	saawrite(0, SAA7146_IER);	/* turn off all interrupts */
J
 
Jiri Slaby 已提交
1986

1987
	retval = request_irq(saa->irq, saa7146_irq, IRQF_SHARED | IRQF_DISABLED,
J
 
Jiri Slaby 已提交
1988 1989
		"stradis", saa);
	if (retval == -EINVAL)
J
 
Jiri Slaby 已提交
1990
		dev_err(&pdev->dev, "%d: Bad irq number or handler\n", num);
J
 
Jiri Slaby 已提交
1991
	else if (retval == -EBUSY)
J
 
Jiri Slaby 已提交
1992 1993
		dev_err(&pdev->dev, "%d: IRQ %ld busy, change your PnP config "
			"in BIOS\n", num, saa->irq);
J
 
Jiri Slaby 已提交
1994 1995 1996
	if (retval < 0)
		goto errio;

J
 
Jiri Slaby 已提交
1997
	pci_set_master(pdev);
J
 
Jiri Slaby 已提交
1998 1999
	retval = video_register_device(&saa->video_dev, VFL_TYPE_GRABBER,
		video_nr);
J
 
Jiri Slaby 已提交
2000
	if (retval < 0) {
J
 
Jiri Slaby 已提交
2001 2002 2003
		dev_err(&pdev->dev, "%d: error in registering video device!\n",
			num);
		goto errio;
L
Linus Torvalds 已提交
2004
	}
J
 
Jiri Slaby 已提交
2005

L
Linus Torvalds 已提交
2006
	return 0;
J
 
Jiri Slaby 已提交
2007 2008 2009 2010
errio:
	iounmap(saa->saa7146_mem);
err:
	return retval;
L
Linus Torvalds 已提交
2011 2012
}

J
 
Jiri Slaby 已提交
2013
static int __devinit init_saa7146(struct pci_dev *pdev)
L
Linus Torvalds 已提交
2014
{
J
 
Jiri Slaby 已提交
2015
	struct saa7146 *saa = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033

	saa->user = 0;
	/* reset the saa7146 */
	saawrite(0xffff0000, SAA7146_MC1);
	mdelay(5);
	/* enable debi and i2c transfers and pins */
	saawrite(((SAA7146_MC1_EDP | SAA7146_MC1_EI2C |
		   SAA7146_MC1_TR_E_DEBI) << 16) | 0xffff, SAA7146_MC1);
	/* ensure proper state of chip */
	saawrite(0x00000000, SAA7146_PAGE1);
	saawrite(0x00f302c0, SAA7146_NUM_LINE_BYTE1);
	saawrite(0x00000000, SAA7146_PAGE2);
	saawrite(0x01400080, SAA7146_NUM_LINE_BYTE2);
	saawrite(0x00000000, SAA7146_DD1_INIT);
	saawrite(0x00000000, SAA7146_DD1_STREAM_B);
	saawrite(0x00000000, SAA7146_DD1_STREAM_A);
	saawrite(0x00000000, SAA7146_BRS_CTRL);
	saawrite(0x80400040, SAA7146_BCS_CTRL);
J
 
Jiri Slaby 已提交
2034
	saawrite(0x0000e000 /*| (1<<29) */ , SAA7146_HPS_CTRL);
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039
	saawrite(0x00000060, SAA7146_CLIP_FORMAT_CTRL);
	saawrite(0x00000000, SAA7146_ACON1);
	saawrite(0x00000000, SAA7146_ACON2);
	saawrite(0x00000600, SAA7146_I2C_STATUS);
	saawrite(((SAA7146_MC2_UPLD_D1_B | SAA7146_MC2_UPLD_D1_A |
J
 
Jiri Slaby 已提交
2040 2041 2042 2043
		SAA7146_MC2_UPLD_BRS | SAA7146_MC2_UPLD_HPS_H |
		SAA7146_MC2_UPLD_HPS_V | SAA7146_MC2_UPLD_DMA2 |
		SAA7146_MC2_UPLD_DMA1 | SAA7146_MC2_UPLD_I2C) << 16) | 0xffff,
		SAA7146_MC2);
L
Linus Torvalds 已提交
2044 2045 2046 2047 2048
	/* setup arbitration control registers */
	saawrite(0x1412121a, SAA7146_PCI_BT_V1);

	/* allocate 32k dma buffer + 4k for page table */
	if ((saa->dmadebi = kmalloc(32768 + 4096, GFP_KERNEL)) == NULL) {
J
 
Jiri Slaby 已提交
2049 2050
		dev_err(&pdev->dev, "%d: debi kmalloc failed\n", saa->nr);
		goto err;
L
Linus Torvalds 已提交
2051 2052 2053 2054 2055 2056 2057 2058 2059
	}
#if 0
	saa->pagedebi = saa->dmadebi + 32768;	/* top 4k is for mmu */
	saawrite(virt_to_bus(saa->pagedebi) /*|0x800 */ , SAA7146_DEBI_PAGE);
	for (i = 0; i < 12; i++)	/* setup mmu page table */
		saa->pagedebi[i] = virt_to_bus((saa->dmadebi + i * 4096));
#endif
	saa->audhead = saa->vidhead = saa->osdhead = 0;
	saa->audtail = saa->vidtail = saa->osdtail = 0;
J
 
Jiri Slaby 已提交
2060 2061 2062 2063 2064 2065
	if (saa->vidbuf == NULL && (saa->vidbuf = vmalloc(524288)) == NULL) {
		dev_err(&pdev->dev, "%d: malloc failed\n", saa->nr);
		goto err;
	}
	if (saa->audbuf == NULL && (saa->audbuf = vmalloc(65536)) == NULL) {
		dev_err(&pdev->dev, "%d: malloc failed\n", saa->nr);
2066
		goto errfree;
J
 
Jiri Slaby 已提交
2067 2068 2069
	}
	if (saa->osdbuf == NULL && (saa->osdbuf = vmalloc(131072)) == NULL) {
		dev_err(&pdev->dev, "%d: malloc failed\n", saa->nr);
2070
		goto errfree;
J
 
Jiri Slaby 已提交
2071
	}
L
Linus Torvalds 已提交
2072
	/* allocate 81920 byte buffer for clipping */
J
 
Jiri Slaby 已提交
2073 2074
	if ((saa->dmavid2 = kzalloc(VIDEO_CLIPMAP_SIZE, GFP_KERNEL)) == NULL) {
		dev_err(&pdev->dev, "%d: clip kmalloc failed\n", saa->nr);
2075
		goto errfree;
L
Linus Torvalds 已提交
2076 2077 2078 2079 2080
	}
	/* setup clipping registers */
	saawrite(virt_to_bus(saa->dmavid2), SAA7146_BASE_EVEN2);
	saawrite(virt_to_bus(saa->dmavid2) + 128, SAA7146_BASE_ODD2);
	saawrite(virt_to_bus(saa->dmavid2) + VIDEO_CLIPMAP_SIZE,
J
 
Jiri Slaby 已提交
2081
		 SAA7146_PROT_ADDR2);
L
Linus Torvalds 已提交
2082
	saawrite(256, SAA7146_PITCH2);
J
 
Jiri Slaby 已提交
2083
	saawrite(4, SAA7146_PAGE2);	/* dma direction: read, no byteswap */
L
Linus Torvalds 已提交
2084 2085 2086
	saawrite(((SAA7146_MC2_UPLD_DMA2) << 16) | SAA7146_MC2_UPLD_DMA2,
		 SAA7146_MC2);
	I2CBusScan(saa);
J
 
Jiri Slaby 已提交
2087

L
Linus Torvalds 已提交
2088
	return 0;
2089
errfree:
J
 
Jiri Slaby 已提交
2090 2091 2092
	vfree(saa->osdbuf);
	vfree(saa->audbuf);
	vfree(saa->vidbuf);
2093
	saa->audbuf = saa->osdbuf = saa->vidbuf = NULL;
J
 
Jiri Slaby 已提交
2094 2095
err:
	return -ENOMEM;
L
Linus Torvalds 已提交
2096 2097
}

J
 
Jiri Slaby 已提交
2098
static void stradis_release_saa(struct pci_dev *pdev)
L
Linus Torvalds 已提交
2099 2100
{
	u8 command;
J
 
Jiri Slaby 已提交
2101
	struct saa7146 *saa = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
2102

J
 
Jiri Slaby 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
	/* turn off all capturing, DMA and IRQs */
	saawrite(0xffff0000, SAA7146_MC1);	/* reset chip */
	saawrite(0, SAA7146_MC2);
	saawrite(0, SAA7146_IER);
	saawrite(0xffffffffUL, SAA7146_ISR);

	/* disable PCI bus-mastering */
	pci_read_config_byte(pdev, PCI_COMMAND, &command);
	command &= ~PCI_COMMAND_MASTER;
	pci_write_config_byte(pdev, PCI_COMMAND, command);

	/* unmap and free memory */
	saa->audhead = saa->audtail = saa->osdhead = 0;
	saa->vidhead = saa->vidtail = saa->osdtail = 0;
	vfree(saa->vidbuf);
	vfree(saa->audbuf);
	vfree(saa->osdbuf);
	kfree(saa->dmavid2);
	saa->audbuf = saa->vidbuf = saa->osdbuf = NULL;
	saa->dmavid2 = NULL;
	kfree(saa->dmadebi);
	kfree(saa->dmavid1);
	kfree(saa->dmavid3);
	kfree(saa->dmaa1in);
	kfree(saa->dmaa1out);
	kfree(saa->dmaa2in);
	kfree(saa->dmaa2out);
	kfree(saa->dmaRPS1);
	kfree(saa->dmaRPS2);
	free_irq(saa->irq, saa);
	if (saa->saa7146_mem)
		iounmap(saa->saa7146_mem);
	if (saa->video_dev.minor != -1)
		video_unregister_device(&saa->video_dev);
}
L
Linus Torvalds 已提交
2138

J
 
Jiri Slaby 已提交
2139 2140 2141 2142
static int __devinit stradis_probe(struct pci_dev *pdev,
	const struct pci_device_id *ent)
{
	int retval = -EINVAL;
L
Linus Torvalds 已提交
2143

J
 
Jiri Slaby 已提交
2144 2145
	if (saa_num >= SAA7146_MAX)
		goto err;
L
Linus Torvalds 已提交
2146

J
 
Jiri Slaby 已提交
2147 2148 2149 2150 2151
	if (!pdev->subsystem_vendor)
		dev_info(&pdev->dev, "%d: rev1 decoder\n", saa_num);
	else
		dev_info(&pdev->dev, "%d: SDM2xx found\n", saa_num);

J
 
Jiri Slaby 已提交
2152
	pci_set_drvdata(pdev, &saa7146s[saa_num]);
J
 
Jiri Slaby 已提交
2153 2154 2155 2156 2157 2158 2159

	retval = configure_saa7146(pdev, saa_num);
	if (retval) {
		dev_err(&pdev->dev, "%d: error in configuring\n", saa_num);
		goto err;
	}

J
 
Jiri Slaby 已提交
2160
	if (init_saa7146(pdev) < 0) {
J
 
Jiri Slaby 已提交
2161 2162 2163
		dev_err(&pdev->dev, "%d: error in initialization\n", saa_num);
		retval = -EIO;
		goto errrel;
L
Linus Torvalds 已提交
2164
	}
J
 
Jiri Slaby 已提交
2165 2166 2167 2168 2169 2170 2171 2172

	saa_num++;

	return 0;
errrel:
	stradis_release_saa(pdev);
err:
	return retval;
L
Linus Torvalds 已提交
2173 2174
}

J
 
Jiri Slaby 已提交
2175 2176 2177 2178 2179 2180 2181 2182 2183
static void __devexit stradis_remove(struct pci_dev *pdev)
{
	stradis_release_saa(pdev);
}

static struct pci_device_id stradis_pci_tbl[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146) },
	{ 0 }
};
J
 
Jiri Slaby 已提交
2184

J
 
Jiri Slaby 已提交
2185 2186

static struct pci_driver stradis_driver = {
J
 
Jiri Slaby 已提交
2187 2188 2189 2190
	.name = "stradis",
	.id_table = stradis_pci_tbl,
	.probe = stradis_probe,
	.remove = __devexit_p(stradis_remove)
J
 
Jiri Slaby 已提交
2191
};
L
Linus Torvalds 已提交
2192

2193
static int __init stradis_init(void)
L
Linus Torvalds 已提交
2194
{
J
 
Jiri Slaby 已提交
2195
	int retval;
L
Linus Torvalds 已提交
2196 2197 2198

	saa_num = 0;

J
 
Jiri Slaby 已提交
2199 2200 2201 2202 2203
	retval = pci_register_driver(&stradis_driver);
	if (retval)
		printk(KERN_ERR "stradis: Unable to register pci driver.\n");

	return retval;
L
Linus Torvalds 已提交
2204 2205
}

2206
static void __exit stradis_exit(void)
L
Linus Torvalds 已提交
2207
{
J
 
Jiri Slaby 已提交
2208
	pci_unregister_driver(&stradis_driver);
L
Linus Torvalds 已提交
2209 2210 2211 2212 2213
	printk(KERN_INFO "stradis: module cleanup complete\n");
}

module_init(stradis_init);
module_exit(stradis_exit);