planb.c 60.6 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
    planb - PlanB frame grabber driver

    PlanB is used in the 7x00/8x00 series of PowerMacintosh
    Computers as video input DMA controller.

    Copyright (C) 1998 Michel Lanners (mlan@cpu.lu)

    Based largely on the bttv driver by Ralph Metzler (rjkm@thp.uni-koeln.de)

    Additional debugging and coding by Takashi Oe (toe@unlserve.unl.edu)

    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.
*/

/* $Id: planb.c,v 1.18 1999/05/02 17:36:34 mlan Exp $ */

#include <linux/init.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/videodev.h>
43
#include <media/v4l2-common.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/dbdma.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <asm/irq.h>
52
#include <linux/mutex.h>
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#include "planb.h"
#include "saa7196.h"

/* Would you mind for some ugly debugging? */
#if 0
#define DEBUG(x...) printk(KERN_DEBUG ## x) /* Debug driver */
#else
#define DEBUG(x...) 		/* Don't debug driver */
#endif

#if 0
#define IDEBUG(x...) printk(KERN_DEBUG ## x) /* Debug interrupt part */
#else
#define IDEBUG(x...) 		/* Don't debug interrupt part */
#endif

/* Ever seen a Mac with more than 1 of these? */
#define PLANB_MAX 1

static int planb_num;
static struct planb planbs[PLANB_MAX];
static volatile struct planb_registers *planb_regs;

static int def_norm = PLANB_DEF_NORM;	/* default norm */
static int video_nr = -1;

80
module_param(def_norm, int, 0);
L
Linus Torvalds 已提交
81
MODULE_PARM_DESC(def_norm, "Default startup norm (0=PAL, 1=NTSC, 2=SECAM)");
82
module_param(video_nr, int, 0);
L
Linus Torvalds 已提交
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139
MODULE_LICENSE("GPL");


/* ------------------ PlanB Exported Functions ------------------ */
static long planb_write(struct video_device *, const char *, unsigned long, int);
static long planb_read(struct video_device *, char *, unsigned long, int);
static int planb_open(struct video_device *, int);
static void planb_close(struct video_device *);
static int planb_ioctl(struct video_device *, unsigned int, void *);
static int planb_init_done(struct video_device *);
static int planb_mmap(struct video_device *, const char *, unsigned long);
static void release_planb(void);
int init_planbs(struct video_init *);

/* ------------------ PlanB Internal Functions ------------------ */
static int planb_prepare_open(struct planb *);
static void planb_prepare_close(struct planb *);
static void saa_write_reg(unsigned char, unsigned char);
static unsigned char saa_status(int, struct planb *);
static void saa_set(unsigned char, unsigned char, struct planb *);
static void saa_init_regs(struct planb *);
static int grabbuf_alloc(struct planb *);
static int vgrab(struct planb *, struct video_mmap *);
static void add_clip(struct planb *, struct video_clip *);
static void fill_cmd_buff(struct planb *);
static void cmd_buff(struct planb *);
static volatile struct dbdma_cmd *setup_grab_cmd(int, struct planb *);
static void overlay_start(struct planb *);
static void overlay_stop(struct planb *);
static inline void tab_cmd_dbdma(volatile struct dbdma_cmd *, unsigned short,
	unsigned int);
static inline void tab_cmd_store(volatile struct dbdma_cmd *, unsigned int,
	unsigned int);
static inline void tab_cmd_gen(volatile struct dbdma_cmd *, unsigned short,
	unsigned short, unsigned int, unsigned int);
static int init_planb(struct planb *);
static int find_planb(void);
static void planb_pre_capture(int, int, struct planb *);
static volatile struct dbdma_cmd *cmd_geo_setup(volatile struct dbdma_cmd *,
					int, int, int, int, int, struct planb *);
static inline void planb_dbdma_stop(volatile struct dbdma_regs *);
static unsigned int saa_geo_setup(int, int, int, int, struct planb *);
static inline int overlay_is_active(struct planb *);

/*******************************/
/* Memory management functions */
/*******************************/

static int grabbuf_alloc(struct planb *pb)
{
	int i, npage;

	npage = MAX_GBUFFERS * ((PLANB_MAX_FBUF / PAGE_SIZE + 1)
#ifndef PLANB_GSCANLINE
		+ MAX_LNUM
#endif /* PLANB_GSCANLINE */
		);
140
	if ((pb->rawbuf = kmalloc(npage
L
Linus Torvalds 已提交
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 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
				* sizeof(unsigned long), GFP_KERNEL)) == 0)
		return -ENOMEM;
	for (i = 0; i < npage; i++) {
		pb->rawbuf[i] = (unsigned char *)__get_free_pages(GFP_KERNEL
								|GFP_DMA, 0);
		if (!pb->rawbuf[i])
			break;
		SetPageReserved(virt_to_page(pb->rawbuf[i]));
	}
	if (i-- < npage) {
		printk(KERN_DEBUG "PlanB: init_grab: grab buffer not allocated\n");
		for (; i > 0; i--) {
			ClearPageReserved(virt_to_page(pb->rawbuf[i]));
			free_pages((unsigned long)pb->rawbuf[i], 0);
		}
		kfree(pb->rawbuf);
		return -ENOBUFS;
	}
	pb->rawbuf_size = npage;
	return 0;
}

/*****************************/
/* Hardware access functions */
/*****************************/

static void saa_write_reg(unsigned char addr, unsigned char val)
{
	planb_regs->saa_addr = addr; eieio();
	planb_regs->saa_regval = val; eieio();
	return;
}

/* return  status byte 0 or 1: */
static unsigned char saa_status(int byte, struct planb *pb)
{
	saa_regs[pb->win.norm][SAA7196_STDC] =
		(saa_regs[pb->win.norm][SAA7196_STDC] & ~2) | ((byte & 1) << 1);
	saa_write_reg (SAA7196_STDC, saa_regs[pb->win.norm][SAA7196_STDC]);

	/* Let's wait 30msec for this one */
	msleep_interruptible(30);

	return (unsigned char)in_8 (&planb_regs->saa_status);
}

static void saa_set(unsigned char addr, unsigned char val, struct planb *pb)
{
	if(saa_regs[pb->win.norm][addr] != val) {
		saa_regs[pb->win.norm][addr] = val;
		saa_write_reg (addr, val);
	}
	return;
}

static void saa_init_regs(struct planb *pb)
{
	int i;

	for (i = 0; i < SAA7196_NUMREGS; i++)
		saa_write_reg (i, saa_regs[pb->win.norm][i]);
}

static unsigned int saa_geo_setup(int width, int height, int interlace, int bpp,
	struct planb *pb)
{
	int ht, norm = pb->win.norm;

	switch(bpp) {
	case 2:
		/* RGB555+a 1x16-bit + 16-bit transparent */
		saa_regs[norm][SAA7196_FMTS] &= ~0x3;
		break;
	case 1:
	case 4:
		/* RGB888 1x24-bit + 8-bit transparent */
		saa_regs[norm][SAA7196_FMTS] &= ~0x1;
		saa_regs[norm][SAA7196_FMTS] |= 0x2;
		break;
	default:
		return -EINVAL;
	}
	ht = (interlace ? height / 2 : height);
	saa_regs[norm][SAA7196_OUTPIX] = (unsigned char) (width & 0x00ff);
	saa_regs[norm][SAA7196_HFILT] = (saa_regs[norm][SAA7196_HFILT] & ~0x3)
						| (width >> 8 & 0x3);
	saa_regs[norm][SAA7196_OUTLINE] = (unsigned char) (ht & 0xff);
	saa_regs[norm][SAA7196_VYP] = (saa_regs[norm][SAA7196_VYP] & ~0x3)
						| (ht >> 8 & 0x3);
	/* feed both fields if interlaced, or else feed only even fields */
	saa_regs[norm][SAA7196_FMTS] = (interlace) ?
					(saa_regs[norm][SAA7196_FMTS] & ~0x60)
					: (saa_regs[norm][SAA7196_FMTS] | 0x60);
	/* transparent mode; extended format enabled */
	saa_regs[norm][SAA7196_DPATH] |= 0x3;

	return 0;
}

/***************************/
/* DBDMA support functions */
/***************************/

static inline void planb_dbdma_restart(volatile struct dbdma_regs *ch)
{
	out_le32(&ch->control, PLANB_CLR(RUN));
	out_le32(&ch->control, PLANB_SET(RUN|WAKE) | PLANB_CLR(PAUSE));
}

static inline void planb_dbdma_stop(volatile struct dbdma_regs *ch)
{
	int i = 0;

	out_le32(&ch->control, PLANB_CLR(RUN) | PLANB_SET(FLUSH));
	while((in_le32(&ch->status) == (ACTIVE | FLUSH)) && (i < 999)) {
		IDEBUG("PlanB: waiting for DMA to stop\n");
		i++;
	}
}

static inline void tab_cmd_dbdma(volatile struct dbdma_cmd *ch,
	unsigned short command, unsigned int cmd_dep)
{
	st_le16(&ch->command, command);
	st_le32(&ch->cmd_dep, cmd_dep);
}

static inline void tab_cmd_store(volatile struct dbdma_cmd *ch,
	unsigned int phy_addr, unsigned int cmd_dep)
{
	st_le16(&ch->command, STORE_WORD | KEY_SYSTEM);
	st_le16(&ch->req_count, 4);
	st_le32(&ch->phy_addr, phy_addr);
	st_le32(&ch->cmd_dep, cmd_dep);
}

static inline void tab_cmd_gen(volatile struct dbdma_cmd *ch,
	unsigned short command, unsigned short req_count,
	unsigned int phy_addr, unsigned int cmd_dep)
{
	st_le16(&ch->command, command);
	st_le16(&ch->req_count, req_count);
	st_le32(&ch->phy_addr, phy_addr);
	st_le32(&ch->cmd_dep, cmd_dep);
}

static volatile struct dbdma_cmd *cmd_geo_setup(
	volatile struct dbdma_cmd *c1, int width, int height, int interlace,
	int bpp, int clip, struct planb *pb)
{
	int norm = pb->win.norm;

	if((saa_geo_setup(width, height, interlace, bpp, pb)) != 0)
		return (volatile struct dbdma_cmd *)NULL;
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_addr),
							SAA7196_FMTS);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_regval),
					saa_regs[norm][SAA7196_FMTS]);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_addr),
							SAA7196_DPATH);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_regval),
					saa_regs[norm][SAA7196_DPATH]);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->even),
					bpp | ((clip)? PLANB_CLIPMASK: 0));
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->odd),
					bpp | ((clip)? PLANB_CLIPMASK: 0));
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_addr),
							SAA7196_OUTPIX);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_regval),
					saa_regs[norm][SAA7196_OUTPIX]);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_addr),
							SAA7196_HFILT);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_regval),
					saa_regs[norm][SAA7196_HFILT]);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_addr),
							SAA7196_OUTLINE);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_regval),
					saa_regs[norm][SAA7196_OUTLINE]);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_addr),
							SAA7196_VYP);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->saa_regval),
					saa_regs[norm][SAA7196_VYP]);
	return c1;
}

/******************************/
/* misc. supporting functions */
/******************************/

static inline void planb_lock(struct planb *pb)
{
332
	mutex_lock(&pb->lock);
L
Linus Torvalds 已提交
333 334 335 336
}

static inline void planb_unlock(struct planb *pb)
{
337
	mutex_unlock(&pb->lock);
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

/***************/
/* Driver Core */
/***************/

static int planb_prepare_open(struct planb *pb)
{
	int	i, size;

	/* allocate memory for two plus alpha command buffers (size: max lines,
	   plus 40 commands handling, plus 1 alignment), plus dummy command buf,
	   plus clipmask buffer, plus frame grabbing status */
	size = (pb->tab_size*(2+MAX_GBUFFERS*TAB_FACTOR)+1+MAX_GBUFFERS
		* PLANB_DUMMY)*sizeof(struct dbdma_cmd)
		+(PLANB_MAXLINES*((PLANB_MAXPIXELS+7)& ~7))/8
		+MAX_GBUFFERS*sizeof(unsigned int);
355
	if ((pb->priv_space = kzalloc (size, GFP_KERNEL)) == 0)
L
Linus Torvalds 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 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 407 408 409 410 411 412 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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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
		return -ENOMEM;
	pb->overlay_last1 = pb->ch1_cmd = (volatile struct dbdma_cmd *)
						DBDMA_ALIGN (pb->priv_space);
	pb->overlay_last2 = pb->ch2_cmd = pb->ch1_cmd + pb->tab_size;
	pb->ch1_cmd_phys = virt_to_bus(pb->ch1_cmd);
	pb->cap_cmd[0] = pb->ch2_cmd + pb->tab_size;
	pb->pre_cmd[0] = pb->cap_cmd[0] + pb->tab_size * TAB_FACTOR;
	for (i = 1; i < MAX_GBUFFERS; i++) {
		pb->cap_cmd[i] = pb->pre_cmd[i-1] + PLANB_DUMMY;
		pb->pre_cmd[i] = pb->cap_cmd[i] + pb->tab_size * TAB_FACTOR;
	}
	pb->frame_stat=(volatile unsigned int *)(pb->pre_cmd[MAX_GBUFFERS-1]
						+ PLANB_DUMMY);
	pb->mask = (unsigned char *)(pb->frame_stat+MAX_GBUFFERS);

	pb->rawbuf = NULL;
	pb->rawbuf_size = 0;
	pb->grabbing = 0;
	for (i = 0; i < MAX_GBUFFERS; i++) {
		pb->frame_stat[i] = GBUFFER_UNUSED;
		pb->gwidth[i] = 0;
		pb->gheight[i] = 0;
		pb->gfmt[i] = 0;
		pb->gnorm_switch[i] = 0;
#ifndef PLANB_GSCANLINE
		pb->lsize[i] = 0;
		pb->lnum[i] = 0;
#endif /* PLANB_GSCANLINE */
	}
	pb->gcount = 0;
	pb->suspend = 0;
	pb->last_fr = -999;
	pb->prev_last_fr = -999;

	/* Reset DMA controllers */
	planb_dbdma_stop(&pb->planb_base->ch2);
	planb_dbdma_stop(&pb->planb_base->ch1);

	return 0;
}

static void planb_prepare_close(struct planb *pb)
{
	int i;

	/* make sure the dma's are idle */
	planb_dbdma_stop(&pb->planb_base->ch2);
	planb_dbdma_stop(&pb->planb_base->ch1);
	/* free kernel memory of command buffers */
	if(pb->priv_space != 0) {
		kfree (pb->priv_space);
		pb->priv_space = 0;
		pb->cmd_buff_inited = 0;
	}
	if(pb->rawbuf) {
		for (i = 0; i < pb->rawbuf_size; i++) {
			ClearPageReserved(virt_to_page(pb->rawbuf[i]));
			free_pages((unsigned long)pb->rawbuf[i], 0);
		}
		kfree(pb->rawbuf);
	}
	pb->rawbuf = NULL;
}

/*****************************/
/* overlay support functions */
/*****************************/

static inline int overlay_is_active(struct planb *pb)
{
	unsigned int size = pb->tab_size * sizeof(struct dbdma_cmd);
	unsigned int caddr = (unsigned)in_le32(&pb->planb_base->ch1.cmdptr);

	return (in_le32(&pb->overlay_last1->cmd_dep) == pb->ch1_cmd_phys)
			&& (caddr < (pb->ch1_cmd_phys + size))
			&& (caddr >= (unsigned)pb->ch1_cmd_phys);
}

static void overlay_start(struct planb *pb)
{

	DEBUG("PlanB: overlay_start()\n");

	if(ACTIVE & in_le32(&pb->planb_base->ch1.status)) {

		DEBUG("PlanB: presumably, grabbing is in progress...\n");

		planb_dbdma_stop(&pb->planb_base->ch2);
		out_le32 (&pb->planb_base->ch2.cmdptr,
						virt_to_bus(pb->ch2_cmd));
		planb_dbdma_restart(&pb->planb_base->ch2);
		st_le16 (&pb->ch1_cmd->command, DBDMA_NOP);
		tab_cmd_dbdma(pb->last_cmd[pb->last_fr],
					DBDMA_NOP | BR_ALWAYS,
					virt_to_bus(pb->ch1_cmd));
		eieio();
		pb->prev_last_fr = pb->last_fr;
		pb->last_fr = -2;
		if(!(ACTIVE & in_le32(&pb->planb_base->ch1.status))) {
			IDEBUG("PlanB: became inactive "
				"in the mean time... reactivating\n");
			planb_dbdma_stop(&pb->planb_base->ch1);
			out_le32 (&pb->planb_base->ch1.cmdptr,
						virt_to_bus(pb->ch1_cmd));
			planb_dbdma_restart(&pb->planb_base->ch1);
		}
	} else {

		DEBUG("PlanB: currently idle, so can do whatever\n");

		planb_dbdma_stop(&pb->planb_base->ch2);
		planb_dbdma_stop(&pb->planb_base->ch1);
		st_le32 (&pb->planb_base->ch2.cmdptr,
						virt_to_bus(pb->ch2_cmd));
		st_le32 (&pb->planb_base->ch1.cmdptr,
						virt_to_bus(pb->ch1_cmd));
		out_le16 (&pb->ch1_cmd->command, DBDMA_NOP);
		planb_dbdma_restart(&pb->planb_base->ch2);
		planb_dbdma_restart(&pb->planb_base->ch1);
		pb->last_fr = -1;
	}
	return;
}

static void overlay_stop(struct planb *pb)
{
	DEBUG("PlanB: overlay_stop()\n");

	if(pb->last_fr == -1) {

		DEBUG("PlanB: no grabbing, it seems...\n");

		planb_dbdma_stop(&pb->planb_base->ch2);
		planb_dbdma_stop(&pb->planb_base->ch1);
		pb->last_fr = -999;
	} else if(pb->last_fr == -2) {
		unsigned int cmd_dep;
		tab_cmd_dbdma(pb->cap_cmd[pb->prev_last_fr], DBDMA_STOP, 0);
		eieio();
		cmd_dep = (unsigned int)in_le32(&pb->overlay_last1->cmd_dep);
		if(overlay_is_active(pb)) {

			DEBUG("PlanB: overlay is currently active\n");

			planb_dbdma_stop(&pb->planb_base->ch2);
			planb_dbdma_stop(&pb->planb_base->ch1);
			if(cmd_dep != pb->ch1_cmd_phys) {
				out_le32(&pb->planb_base->ch1.cmdptr,
						virt_to_bus(pb->overlay_last1));
				planb_dbdma_restart(&pb->planb_base->ch1);
			}
		}
		pb->last_fr = pb->prev_last_fr;
		pb->prev_last_fr = -999;
	}
	return;
}

static void suspend_overlay(struct planb *pb)
{
	int fr = -1;
	struct dbdma_cmd last;

	DEBUG("PlanB: suspend_overlay: %d\n", pb->suspend);

	if(pb->suspend++)
		return;
	if(ACTIVE & in_le32(&pb->planb_base->ch1.status)) {
		if(pb->last_fr == -2) {
			fr = pb->prev_last_fr;
			memcpy(&last, (void*)pb->last_cmd[fr], sizeof(last));
			tab_cmd_dbdma(pb->last_cmd[fr], DBDMA_STOP, 0);
		}
		if(overlay_is_active(pb)) {
			planb_dbdma_stop(&pb->planb_base->ch2);
			planb_dbdma_stop(&pb->planb_base->ch1);
			pb->suspended.overlay = 1;
			pb->suspended.frame = fr;
			memcpy(&pb->suspended.cmd, &last, sizeof(last));
			return;
		}
	}
	pb->suspended.overlay = 0;
	pb->suspended.frame = fr;
	memcpy(&pb->suspended.cmd, &last, sizeof(last));
	return;
}

static void resume_overlay(struct planb *pb)
{

	DEBUG("PlanB: resume_overlay: %d\n", pb->suspend);

	if(pb->suspend > 1)
		return;
	if(pb->suspended.frame != -1) {
		memcpy((void*)pb->last_cmd[pb->suspended.frame],
				&pb->suspended.cmd, sizeof(pb->suspended.cmd));
	}
	if(ACTIVE & in_le32(&pb->planb_base->ch1.status)) {
		goto finish;
	}
	if(pb->suspended.overlay) {

		DEBUG("PlanB: overlay being resumed\n");

		st_le16 (&pb->ch1_cmd->command, DBDMA_NOP);
		st_le16 (&pb->ch2_cmd->command, DBDMA_NOP);
		/* Set command buffer addresses */
		st_le32(&pb->planb_base->ch1.cmdptr,
					virt_to_bus(pb->overlay_last1));
		out_le32(&pb->planb_base->ch2.cmdptr,
					virt_to_bus(pb->overlay_last2));
		/* Start the DMA controller */
		out_le32 (&pb->planb_base->ch2.control,
				PLANB_CLR(PAUSE) | PLANB_SET(RUN|WAKE));
		out_le32 (&pb->planb_base->ch1.control,
				PLANB_CLR(PAUSE) | PLANB_SET(RUN|WAKE));
	} else if(pb->suspended.frame != -1) {
		out_le32(&pb->planb_base->ch1.cmdptr,
				virt_to_bus(pb->last_cmd[pb->suspended.frame]));
		out_le32 (&pb->planb_base->ch1.control,
				PLANB_CLR(PAUSE) | PLANB_SET(RUN|WAKE));
	}

finish:
	pb->suspend--;
	wake_up_interruptible(&pb->suspendq);
}

586
static void add_clip(struct planb *pb, struct video_clip *clip)
L
Linus Torvalds 已提交
587 588 589 590 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
{
	volatile unsigned char	*base;
	int	xc = clip->x, yc = clip->y;
	int	wc = clip->width, hc = clip->height;
	int	ww = pb->win.width, hw = pb->win.height;
	int	x, y, xtmp1, xtmp2;

	DEBUG("PlanB: clip %dx%d+%d+%d\n", wc, hc, xc, yc);

	if(xc < 0) {
		wc += xc;
		xc = 0;
	}
	if(yc < 0) {
		hc += yc;
		yc = 0;
	}
	if(xc + wc > ww)
		wc = ww - xc;
	if(wc <= 0) /* Nothing to do */
		return;
	if(yc + hc > hw)
		hc = hw - yc;

	for (y = yc; y < yc+hc; y++) {
		xtmp1=xc>>3;
		xtmp2=(xc+wc)>>3;
		base = pb->mask + y*96;
		if(xc != 0 || wc >= 8)
			*(base + xtmp1) &= (unsigned char)(0x00ff &
				(0xff00 >> (xc&7)));
		for (x = xtmp1 + 1; x < xtmp2; x++) {
			*(base + x) = 0;
		}
		if(xc < (ww & ~0x7))
			*(base + xtmp2) &= (unsigned char)(0x00ff >>
				((xc+wc) & 7));
	}

	return;
}

static void fill_cmd_buff(struct planb *pb)
{
	int restore = 0;
	volatile struct dbdma_cmd last;

	DEBUG("PlanB: fill_cmd_buff()\n");

	if(pb->overlay_last1 != pb->ch1_cmd) {
		restore = 1;
		last = *(pb->overlay_last1);
	}
	memset ((void *) pb->ch1_cmd, 0, 2 * pb->tab_size
					* sizeof(struct dbdma_cmd));
	cmd_buff (pb);
	if(restore)
		*(pb->overlay_last1) = last;
	if(pb->suspended.overlay) {
		unsigned long jump_addr = in_le32(&pb->overlay_last1->cmd_dep);
		if(jump_addr != pb->ch1_cmd_phys) {
			int i;

			DEBUG("PlanB: adjusting ch1's jump address\n");

			for(i = 0; i < MAX_GBUFFERS; i++) {
				if(pb->need_pre_capture[i]) {
				    if(jump_addr == virt_to_bus(pb->pre_cmd[i]))
					goto found;
				} else {
				    if(jump_addr == virt_to_bus(pb->cap_cmd[i]))
					goto found;
				}
			}

			DEBUG("PlanB: not found...\n");

			goto out;
found:
			if(pb->need_pre_capture[i])
				out_le32(&pb->pre_cmd[i]->phy_addr,
						virt_to_bus(pb->overlay_last1));
			else
				out_le32(&pb->cap_cmd[i]->phy_addr,
						virt_to_bus(pb->overlay_last1));
		}
	}
out:
	pb->cmd_buff_inited = 1;

	return;
}

static void cmd_buff(struct planb *pb)
{
	int		i, bpp, count, nlines, stepsize, interlace;
	unsigned long	base, jump, addr_com, addr_dep;
	volatile struct dbdma_cmd *c1 = pb->ch1_cmd;
	volatile struct dbdma_cmd *c2 = pb->ch2_cmd;

	interlace = pb->win.interlace;
	bpp = pb->win.bpp;
	count = (bpp * ((pb->win.x + pb->win.width > pb->win.swidth) ?
		(pb->win.swidth - pb->win.x) : pb->win.width));
	nlines = ((pb->win.y + pb->win.height > pb->win.sheight) ?
		(pb->win.sheight - pb->win.y) : pb->win.height);

	/* Do video in: */

	/* Preamble commands: */
	addr_com = virt_to_bus(c1);
	addr_dep = virt_to_bus(&c1->cmd_dep);
	tab_cmd_dbdma(c1++, DBDMA_NOP, 0);
	jump = virt_to_bus(c1+16); /* 14 by cmd_geo_setup() and 2 for padding */
	if((c1 = cmd_geo_setup(c1, pb->win.width, pb->win.height, interlace,
					bpp, 1, pb)) == NULL) {
		printk(KERN_WARNING "PlanB: encountered serious problems\n");
		tab_cmd_dbdma(pb->ch1_cmd + 1, DBDMA_STOP, 0);
		tab_cmd_dbdma(pb->ch2_cmd + 1, DBDMA_STOP, 0);
		return;
	}
	tab_cmd_store(c1++, addr_com, (unsigned)(DBDMA_NOP | BR_ALWAYS) << 16);
	tab_cmd_store(c1++, addr_dep, jump);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.wait_sel),
							PLANB_SET(FIELD_SYNC));
		/* (1) wait for field sync to be set */
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFCLR, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
							PLANB_SET(ODD_FIELD));
		/* wait for field sync to be cleared */
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFSET, 0);
		/* if not odd field, wait until field sync is set again */
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_IFSET, virt_to_bus(c1-3)); c1++;
		/* assert ch_sync to ch2 */
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch2.control),
							PLANB_SET(CH_SYNC));
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
							PLANB_SET(DMA_ABORT));

	base = (pb->frame_buffer_phys + pb->offset + pb->win.y * (pb->win.bpl
					+ pb->win.pad) + pb->win.x * bpp);

	if (interlace) {
		stepsize = 2;
		jump = virt_to_bus(c1 + (nlines + 1) / 2);
	} else {
		stepsize = 1;
		jump = virt_to_bus(c1 + nlines);
	}

	/* even field data: */
	for (i=0; i < nlines; i += stepsize, c1++)
		tab_cmd_gen(c1, INPUT_MORE | KEY_STREAM0 | BR_IFSET,
			count, base + i * (pb->win.bpl + pb->win.pad), jump);

	/* For non-interlaced, we use even fields only */
	if (!interlace)
		goto cmd_tab_data_end;

	/* Resync to odd field */
		/* (2) wait for field sync to be set */
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFCLR, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
							PLANB_SET(ODD_FIELD));
		/* wait for field sync to be cleared */
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFSET, 0);
		/* if not odd field, wait until field sync is set again */
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_IFCLR, virt_to_bus(c1-3)); c1++;
		/* assert ch_sync to ch2 */
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch2.control),
							PLANB_SET(CH_SYNC));
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
							PLANB_SET(DMA_ABORT));
760

L
Linus Torvalds 已提交
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 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 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 843 844 845
	/* odd field data: */
	jump = virt_to_bus(c1 + nlines / 2);
	for (i=1; i < nlines; i += stepsize, c1++)
		tab_cmd_gen(c1, INPUT_MORE | KEY_STREAM0 | BR_IFSET, count,
			base + i * (pb->win.bpl + pb->win.pad), jump);

	/* And jump back to the start */
cmd_tab_data_end:
	pb->overlay_last1 = c1;	/* keep a pointer to the last command */
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_ALWAYS, virt_to_bus(pb->ch1_cmd));

	/* Clipmask command buffer */

	/* Preamble commands: */
	tab_cmd_dbdma(c2++, DBDMA_NOP, 0);
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch2.wait_sel),
							PLANB_SET(CH_SYNC));
		/* wait until ch1 asserts ch_sync */
	tab_cmd_dbdma(c2++, DBDMA_NOP | WAIT_IFCLR, 0);
		/* clear ch_sync asserted by ch1 */
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch2.control),
							PLANB_CLR(CH_SYNC));
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch2.wait_sel),
							PLANB_SET(FIELD_SYNC));
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch2.br_sel),
							PLANB_SET(ODD_FIELD));

	/* jump to end of even field if appropriate */
	/* this points to (interlace)? pos. C: pos. B */
	jump = (interlace) ? virt_to_bus(c2 + (nlines + 1) / 2 + 2):
						virt_to_bus(c2 + nlines + 2);
		/* if odd field, skip over to odd field clipmasking */
	tab_cmd_dbdma(c2++, DBDMA_NOP | BR_IFSET, jump);

	/* even field mask: */
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch2.br_sel),
							PLANB_SET(DMA_ABORT));
	/* this points to pos. B */
	jump = (interlace) ? virt_to_bus(c2 + nlines + 1):
						virt_to_bus(c2 + nlines);
	base = virt_to_bus(pb->mask);
	for (i=0; i < nlines; i += stepsize, c2++)
		tab_cmd_gen(c2, OUTPUT_MORE | KEY_STREAM0 | BR_IFSET, 96,
			base + i * 96, jump);

	/* For non-interlaced, we use only even fields */
	if(!interlace)
		goto cmd_tab_mask_end;

	/* odd field mask: */
/* C */	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch2.br_sel),
							PLANB_SET(DMA_ABORT));
	/* this points to pos. B */
	jump = virt_to_bus(c2 + nlines / 2);
	base = virt_to_bus(pb->mask);
	for (i=1; i < nlines; i += 2, c2++)     /* abort if set */
		tab_cmd_gen(c2, OUTPUT_MORE | KEY_STREAM0 | BR_IFSET, 96,
			base + i * 96, jump);

	/* Inform channel 1 and jump back to start */
cmd_tab_mask_end:
	/* ok, I just realized this is kind of flawed. */
	/* this part is reached only after odd field clipmasking. */
	/* wanna clean up? */
		/* wait for field sync to be set */
		/* corresponds to fsync (1) of ch1 */
/* B */	tab_cmd_dbdma(c2++, DBDMA_NOP | WAIT_IFCLR, 0);
		/* restart ch1, meant to clear any dead bit or something */
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch1.control),
							PLANB_CLR(RUN));
	tab_cmd_store(c2++, (unsigned)(&pb->planb_base_phys->ch1.control),
							PLANB_SET(RUN));
	pb->overlay_last2 = c2;	/* keep a pointer to the last command */
		/* start over even field clipmasking */
	tab_cmd_dbdma(c2, DBDMA_NOP | BR_ALWAYS, virt_to_bus(pb->ch2_cmd));

	eieio();
	return;
}

/*********************************/
/* grabdisplay support functions */
/*********************************/

static int palette2fmt[] = {
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	0,
	PLANB_GRAY,
	0,
	0,
	0,
	PLANB_COLOUR32,
	PLANB_COLOUR15,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 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 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 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 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 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
};

#define PLANB_PALETTE_MAX 15

static int vgrab(struct planb *pb, struct video_mmap *mp)
{
	unsigned int fr = mp->frame;
	unsigned int format;

	if(pb->rawbuf==NULL) {
		int err;
		if((err=grabbuf_alloc(pb)))
			return err;
	}

	IDEBUG("PlanB: grab %d: %dx%d(%u)\n", pb->grabbing,
						mp->width, mp->height, fr);

	if(pb->grabbing >= MAX_GBUFFERS)
		return -ENOBUFS;
	if(fr > (MAX_GBUFFERS - 1) || fr < 0)
		return -EINVAL;
	if(mp->height <= 0 || mp->width <= 0)
		return -EINVAL;
	if(mp->format < 0 || mp->format >= PLANB_PALETTE_MAX)
		return -EINVAL;
	if((format = palette2fmt[mp->format]) == 0)
		return -EINVAL;
	if (mp->height * mp->width * format > PLANB_MAX_FBUF) /* format = bpp */
		return -EINVAL;

	planb_lock(pb);
	if(mp->width != pb->gwidth[fr] || mp->height != pb->gheight[fr] ||
			format != pb->gfmt[fr] || (pb->gnorm_switch[fr])) {
		int i;
#ifndef PLANB_GSCANLINE
		unsigned int osize = pb->gwidth[fr] * pb->gheight[fr]
								* pb->gfmt[fr];
		unsigned int nsize = mp->width * mp->height * format;
#endif

		IDEBUG("PlanB: gwidth = %d, gheight = %d, mp->format = %u\n",
					mp->width, mp->height, mp->format);

#ifndef PLANB_GSCANLINE
		if(pb->gnorm_switch[fr])
			nsize = 0;
		if (nsize < osize) {
			for(i = pb->gbuf_idx[fr]; osize > 0; i++) {
				memset((void *)pb->rawbuf[i], 0, PAGE_SIZE);
				osize -= PAGE_SIZE;
			}
		}
		for(i = pb->l_fr_addr_idx[fr]; i < pb->l_fr_addr_idx[fr]
							+ pb->lnum[fr]; i++)
			memset((void *)pb->rawbuf[i], 0, PAGE_SIZE);
#else
/* XXX TODO */
/*
		if(pb->gnorm_switch[fr])
			memset((void *)pb->gbuffer[fr], 0,
					pb->gbytes_per_line * pb->gheight[fr]);
		else {
			if(mp->
			for(i = 0; i < pb->gheight[fr]; i++) {
				memset((void *)(pb->gbuffer[fr]
					+ pb->gbytes_per_line * i
			}
		}
*/
#endif
		pb->gwidth[fr] = mp->width;
		pb->gheight[fr] = mp->height;
		pb->gfmt[fr] = format;
		pb->last_cmd[fr] = setup_grab_cmd(fr, pb);
		planb_pre_capture(fr, pb->gfmt[fr], pb); /* gfmt = bpp */
		pb->need_pre_capture[fr] = 1;
		pb->gnorm_switch[fr] = 0;
	} else
		pb->need_pre_capture[fr] = 0;
	pb->frame_stat[fr] = GBUFFER_GRABBING;
	if(!(ACTIVE & in_le32(&pb->planb_base->ch1.status))) {

		IDEBUG("PlanB: ch1 inactive, initiating grabbing\n");

		planb_dbdma_stop(&pb->planb_base->ch1);
		if(pb->need_pre_capture[fr]) {

			IDEBUG("PlanB: padding pre-capture sequence\n");

			out_le32 (&pb->planb_base->ch1.cmdptr,
						virt_to_bus(pb->pre_cmd[fr]));
		} else {
			tab_cmd_dbdma(pb->last_cmd[fr], DBDMA_STOP, 0);
			tab_cmd_dbdma(pb->cap_cmd[fr], DBDMA_NOP, 0);
		/* let's be on the safe side. here is not timing critical. */
			tab_cmd_dbdma((pb->cap_cmd[fr] + 1), DBDMA_NOP, 0);
			out_le32 (&pb->planb_base->ch1.cmdptr,
						virt_to_bus(pb->cap_cmd[fr]));
		}
		planb_dbdma_restart(&pb->planb_base->ch1);
		pb->last_fr = fr;
	} else {
		int i;

		IDEBUG("PlanB: ch1 active, grabbing being queued\n");

		if((pb->last_fr == -1) || ((pb->last_fr == -2) &&
						overlay_is_active(pb))) {

			IDEBUG("PlanB: overlay is active, grabbing defered\n");

			tab_cmd_dbdma(pb->last_cmd[fr],
					DBDMA_NOP | BR_ALWAYS,
					virt_to_bus(pb->ch1_cmd));
			if(pb->need_pre_capture[fr]) {

				IDEBUG("PlanB: padding pre-capture sequence\n");

				tab_cmd_store(pb->pre_cmd[fr],
				    virt_to_bus(&pb->overlay_last1->cmd_dep),
						virt_to_bus(pb->ch1_cmd));
				eieio();
				out_le32 (&pb->overlay_last1->cmd_dep,
						virt_to_bus(pb->pre_cmd[fr]));
			} else {
				tab_cmd_store(pb->cap_cmd[fr],
				    virt_to_bus(&pb->overlay_last1->cmd_dep),
						virt_to_bus(pb->ch1_cmd));
				tab_cmd_dbdma((pb->cap_cmd[fr] + 1),
								DBDMA_NOP, 0);
				eieio();
				out_le32 (&pb->overlay_last1->cmd_dep,
						virt_to_bus(pb->cap_cmd[fr]));
			}
			for(i = 0; overlay_is_active(pb) && i < 999; i++)
				IDEBUG("PlanB: waiting for overlay done\n");
			tab_cmd_dbdma(pb->ch1_cmd, DBDMA_NOP, 0);
			pb->prev_last_fr = fr;
			pb->last_fr = -2;
		} else if(pb->last_fr == -2) {

			IDEBUG("PlanB: mixed mode detected, grabbing"
				" will be done before activating overlay\n");

			tab_cmd_dbdma(pb->ch1_cmd, DBDMA_NOP, 0);
			if(pb->need_pre_capture[fr]) {

				IDEBUG("PlanB: padding pre-capture sequence\n");

				tab_cmd_dbdma(pb->last_cmd[pb->prev_last_fr],
						DBDMA_NOP | BR_ALWAYS,
						virt_to_bus(pb->pre_cmd[fr]));
				eieio();
			} else {
				tab_cmd_dbdma(pb->cap_cmd[fr], DBDMA_NOP, 0);
				if(pb->gwidth[pb->prev_last_fr] !=
								pb->gwidth[fr]
					|| pb->gheight[pb->prev_last_fr] !=
								pb->gheight[fr]
					|| pb->gfmt[pb->prev_last_fr] !=
								pb->gfmt[fr])
					tab_cmd_dbdma((pb->cap_cmd[fr] + 1),
								DBDMA_NOP, 0);
				else
					tab_cmd_dbdma((pb->cap_cmd[fr] + 1),
					    DBDMA_NOP | BR_ALWAYS,
					    virt_to_bus(pb->cap_cmd[fr] + 16));
				tab_cmd_dbdma(pb->last_cmd[pb->prev_last_fr],
						DBDMA_NOP | BR_ALWAYS,
						virt_to_bus(pb->cap_cmd[fr]));
				eieio();
			}
			tab_cmd_dbdma(pb->last_cmd[fr],
					DBDMA_NOP | BR_ALWAYS,
					virt_to_bus(pb->ch1_cmd));
			eieio();
			pb->prev_last_fr = fr;
			pb->last_fr = -2;
		} else {

			IDEBUG("PlanB: active grabbing session detected\n");

			if(pb->need_pre_capture[fr]) {

				IDEBUG("PlanB: padding pre-capture sequence\n");

				tab_cmd_dbdma(pb->last_cmd[pb->last_fr],
						DBDMA_NOP | BR_ALWAYS,
						virt_to_bus(pb->pre_cmd[fr]));
				eieio();
			} else {
				tab_cmd_dbdma(pb->last_cmd[fr], DBDMA_STOP, 0);
				tab_cmd_dbdma(pb->cap_cmd[fr], DBDMA_NOP, 0);
				if(pb->gwidth[pb->last_fr] != pb->gwidth[fr]
					|| pb->gheight[pb->last_fr] !=
								pb->gheight[fr]
					|| pb->gfmt[pb->last_fr] !=
								pb->gfmt[fr])
					tab_cmd_dbdma((pb->cap_cmd[fr] + 1),
								DBDMA_NOP, 0);
				else
					tab_cmd_dbdma((pb->cap_cmd[fr] + 1),
					    DBDMA_NOP | BR_ALWAYS,
					    virt_to_bus(pb->cap_cmd[fr] + 16));
				tab_cmd_dbdma(pb->last_cmd[pb->last_fr],
						DBDMA_NOP | BR_ALWAYS,
						virt_to_bus(pb->cap_cmd[fr]));
				eieio();
			}
			pb->last_fr = fr;
		}
		if(!(ACTIVE & in_le32(&pb->planb_base->ch1.status))) {

			IDEBUG("PlanB: became inactive in the mean time..."
				"reactivating\n");

			planb_dbdma_stop(&pb->planb_base->ch1);
			out_le32 (&pb->planb_base->ch1.cmdptr,
						virt_to_bus(pb->cap_cmd[fr]));
			planb_dbdma_restart(&pb->planb_base->ch1);
		}
	}
	pb->grabbing++;
	planb_unlock(pb);

	return 0;
}

static void planb_pre_capture(int fr, int bpp, struct planb *pb)
{
	volatile struct dbdma_cmd *c1 = pb->pre_cmd[fr];
	int interlace = (pb->gheight[fr] > pb->maxlines/2)? 1: 0;

	tab_cmd_dbdma(c1++, DBDMA_NOP, 0);
	if((c1 = cmd_geo_setup(c1, pb->gwidth[fr], pb->gheight[fr], interlace,
						bpp, 0, pb)) == NULL) {
		printk(KERN_WARNING "PlanB: encountered some problems\n");
		tab_cmd_dbdma(pb->pre_cmd[fr] + 1, DBDMA_STOP, 0);
		return;
	}
	/* Sync to even field */
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.wait_sel),
		PLANB_SET(FIELD_SYNC));
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFCLR, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(ODD_FIELD));
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFSET, 0);
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_IFSET, virt_to_bus(c1-3)); c1++;
	tab_cmd_dbdma(c1++, DBDMA_NOP | INTR_ALWAYS, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(DMA_ABORT));
	/* For non-interlaced, we use even fields only */
	if (pb->gheight[fr] <= pb->maxlines/2)
		goto cmd_tab_data_end;
	/* Sync to odd field */
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFCLR, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(ODD_FIELD));
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFSET, 0);
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_IFCLR, virt_to_bus(c1-3)); c1++;
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(DMA_ABORT));
cmd_tab_data_end:
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_ALWAYS, virt_to_bus(pb->cap_cmd[fr]));

	eieio();
}

static volatile struct dbdma_cmd *setup_grab_cmd(int fr, struct planb *pb)
{
	int		i, bpp, count, nlines, stepsize, interlace;
#ifdef PLANB_GSCANLINE
	int		scanline;
#else
	int		nlpp, leftover1;
	unsigned long	base;
#endif
	unsigned long	jump;
	int		pagei;
	volatile struct dbdma_cmd *c1;
	volatile struct dbdma_cmd *jump_addr;

	c1 = pb->cap_cmd[fr];
	interlace = (pb->gheight[fr] > pb->maxlines/2)? 1: 0;
	bpp = pb->gfmt[fr];	/* gfmt = bpp */
	count = bpp * pb->gwidth[fr];
	nlines = pb->gheight[fr];
#ifdef PLANB_GSCANLINE
	scanline = pb->gbytes_per_line;
#else
	pb->lsize[fr] = count;
	pb->lnum[fr] = 0;
#endif

	/* Do video in: */

	/* Preamble commands: */
	tab_cmd_dbdma(c1++, DBDMA_NOP, 0);
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_ALWAYS, virt_to_bus(c1 + 16)); c1++;
	if((c1 = cmd_geo_setup(c1, pb->gwidth[fr], pb->gheight[fr], interlace,
						bpp, 0, pb)) == NULL) {
		printk(KERN_WARNING "PlanB: encountered serious problems\n");
		tab_cmd_dbdma(pb->cap_cmd[fr] + 1, DBDMA_STOP, 0);
		return (pb->cap_cmd[fr] + 2);
	}
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.wait_sel),
		PLANB_SET(FIELD_SYNC));
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFCLR, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(ODD_FIELD));
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFSET, 0);
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_IFSET, virt_to_bus(c1-3)); c1++;
	tab_cmd_dbdma(c1++, DBDMA_NOP | INTR_ALWAYS, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(DMA_ABORT));

	if (interlace) {
		stepsize = 2;
		jump_addr = c1 + TAB_FACTOR * (nlines + 1) / 2;
	} else {
		stepsize = 1;
		jump_addr = c1 + TAB_FACTOR * nlines;
	}
	jump = virt_to_bus(jump_addr);

	/* even field data: */

	pagei = pb->gbuf_idx[fr];
#ifdef PLANB_GSCANLINE
	for (i = 0; i < nlines; i += stepsize) {
		tab_cmd_gen(c1++, INPUT_MORE | BR_IFSET, count,
					virt_to_bus(pb->rawbuf[pagei
					+ i * scanline / PAGE_SIZE]), jump);
	}
#else
	i = 0;
	leftover1 = 0;
	do {
	    int j;

	    base = virt_to_bus(pb->rawbuf[pagei]);
	    nlpp = (PAGE_SIZE - leftover1) / count / stepsize;
	    for(j = 0; j < nlpp && i < nlines; j++, i += stepsize, c1++)
		tab_cmd_gen(c1, INPUT_MORE | KEY_STREAM0 | BR_IFSET,
			  count, base + count * j * stepsize + leftover1, jump);
	    if(i < nlines) {
		int lov0 = PAGE_SIZE - count * nlpp * stepsize - leftover1;

		if(lov0 == 0)
		    leftover1 = 0;
		else {
		    if(lov0 >= count) {
			tab_cmd_gen(c1++, INPUT_MORE | BR_IFSET, count, base
				+ count * nlpp * stepsize + leftover1, jump);
		    } else {
			pb->l_to_addr[fr][pb->lnum[fr]] = pb->rawbuf[pagei]
					+ count * nlpp * stepsize + leftover1;
			pb->l_to_next_idx[fr][pb->lnum[fr]] = pagei + 1;
			pb->l_to_next_size[fr][pb->lnum[fr]] = count - lov0;
			tab_cmd_gen(c1++, INPUT_MORE | BR_IFSET, count,
				virt_to_bus(pb->rawbuf[pb->l_fr_addr_idx[fr]
						+ pb->lnum[fr]]), jump);
			if(++pb->lnum[fr] > MAX_LNUM)
				pb->lnum[fr]--;
		    }
		    leftover1 = count * stepsize - lov0;
		    i += stepsize;
		}
	    }
	    pagei++;
	} while(i < nlines);
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_ALWAYS, jump);
	c1 = jump_addr;
#endif /* PLANB_GSCANLINE */

	/* For non-interlaced, we use even fields only */
	if (!interlace)
		goto cmd_tab_data_end;

	/* Sync to odd field */
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFCLR, 0);
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(ODD_FIELD));
	tab_cmd_dbdma(c1++, DBDMA_NOP | WAIT_IFSET, 0);
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_IFCLR, virt_to_bus(c1-3)); c1++;
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->ch1.br_sel),
		PLANB_SET(DMA_ABORT));
1249

L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 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
	/* odd field data: */
	jump_addr = c1 + TAB_FACTOR * nlines / 2;
	jump = virt_to_bus(jump_addr);
#ifdef PLANB_GSCANLINE
	for (i = 1; i < nlines; i += stepsize) {
		tab_cmd_gen(c1++, INPUT_MORE | BR_IFSET, count,
					virt_to_bus(pb->rawbuf[pagei
					+ i * scanline / PAGE_SIZE]), jump);
	}
#else
	i = 1;
	leftover1 = 0;
	pagei = pb->gbuf_idx[fr];
	if(nlines <= 1)
	    goto skip;
	do {
	    int j;

	    base = virt_to_bus(pb->rawbuf[pagei]);
	    nlpp = (PAGE_SIZE - leftover1) / count / stepsize;
	    if(leftover1 >= count) {
		tab_cmd_gen(c1++, INPUT_MORE | KEY_STREAM0 | BR_IFSET, count,
						base + leftover1 - count, jump);
		i += stepsize;
	    }
	    for(j = 0; j < nlpp && i < nlines; j++, i += stepsize, c1++)
		tab_cmd_gen(c1, INPUT_MORE | KEY_STREAM0 | BR_IFSET, count,
			base + count * (j * stepsize + 1) + leftover1, jump);
	    if(i < nlines) {
		int lov0 = PAGE_SIZE - count * nlpp * stepsize - leftover1;

		if(lov0 == 0)
		    leftover1 = 0;
		else {
		    if(lov0 > count) {
			pb->l_to_addr[fr][pb->lnum[fr]] = pb->rawbuf[pagei]
				+ count * (nlpp * stepsize + 1) + leftover1;
			pb->l_to_next_idx[fr][pb->lnum[fr]] = pagei + 1;
			pb->l_to_next_size[fr][pb->lnum[fr]] = count * stepsize
									- lov0;
			tab_cmd_gen(c1++, INPUT_MORE | BR_IFSET, count,
				virt_to_bus(pb->rawbuf[pb->l_fr_addr_idx[fr]
							+ pb->lnum[fr]]), jump);
			if(++pb->lnum[fr] > MAX_LNUM)
				pb->lnum[fr]--;
			i += stepsize;
		    }
		    leftover1 = count * stepsize - lov0;
		}
	    }
	    pagei++;
	} while(i < nlines);
skip:
	tab_cmd_dbdma(c1, DBDMA_NOP | BR_ALWAYS, jump);
	c1 = jump_addr;
#endif /* PLANB_GSCANLINE */

cmd_tab_data_end:
	tab_cmd_store(c1++, (unsigned)(&pb->planb_base_phys->intr_stat),
			(fr << 9) | PLANB_FRM_IRQ | PLANB_GEN_IRQ);
	/* stop it */
	tab_cmd_dbdma(c1, DBDMA_STOP, 0);

	eieio();
	return c1;
}

1317
static irqreturn_t planb_irq(int irq, void *dev_id)
L
Linus Torvalds 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 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 1358 1359
{
	unsigned int stat, astat;
	struct planb *pb = (struct planb *)dev_id;

	IDEBUG("PlanB: planb_irq()\n");

	/* get/clear interrupt status bits */
	eieio();
	stat = in_le32(&pb->planb_base->intr_stat);
	astat = stat & pb->intr_mask;
	out_le32(&pb->planb_base->intr_stat, PLANB_FRM_IRQ
					& ~astat & stat & ~PLANB_GEN_IRQ);
	IDEBUG("PlanB: stat = %X, astat = %X\n", stat, astat);

	if(astat & PLANB_FRM_IRQ) {
		unsigned int fr = stat >> 9;
#ifndef PLANB_GSCANLINE
		int i;
#endif
		IDEBUG("PlanB: PLANB_FRM_IRQ\n");

		pb->gcount++;

		IDEBUG("PlanB: grab %d: fr = %d, gcount = %d\n",
				pb->grabbing, fr, pb->gcount);
#ifndef PLANB_GSCANLINE
		IDEBUG("PlanB: %d * %d bytes are being copied over\n",
				pb->lnum[fr], pb->lsize[fr]);
		for(i = 0; i < pb->lnum[fr]; i++) {
			int first = pb->lsize[fr] - pb->l_to_next_size[fr][i];

			memcpy(pb->l_to_addr[fr][i],
				pb->rawbuf[pb->l_fr_addr_idx[fr] + i],
				first);
			memcpy(pb->rawbuf[pb->l_to_next_idx[fr][i]],
				pb->rawbuf[pb->l_fr_addr_idx[fr] + i] + first,
						pb->l_to_next_size[fr][i]);
		}
#endif
		pb->frame_stat[fr] = GBUFFER_DONE;
		pb->grabbing--;
		wake_up_interruptible(&pb->capq);
1360
		return IRQ_HANDLED;
L
Linus Torvalds 已提交
1361 1362 1363 1364 1365 1366
	}
	/* incorrect interrupts? */
	pb->intr_mask = PLANB_CLR_IRQ;
	out_le32(&pb->planb_base->intr_stat, PLANB_CLR_IRQ);
	printk(KERN_ERR "PlanB: IRQ lockup, cleared intrrupts"
							" unconditionally\n");
1367
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
}

/*******************************
 * Device Operations functions *
 *******************************/

static int planb_open(struct video_device *dev, int mode)
{
	struct planb *pb = (struct planb *)dev;

	if (pb->user == 0) {
		int err;
		if((err = planb_prepare_open(pb)) != 0)
			return err;
	}
	pb->user++;

	DEBUG("PlanB: device opened\n");
1386
	return 0;
L
Linus Torvalds 已提交
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 1419 1420 1421 1422 1423 1424 1425 1426
}

static void planb_close(struct video_device *dev)
{
	struct planb *pb = (struct planb *)dev;

	if(pb->user < 1) /* ??? */
		return;
	planb_lock(pb);
	if (pb->user == 1) {
		if (pb->overlay) {
			planb_dbdma_stop(&pb->planb_base->ch2);
			planb_dbdma_stop(&pb->planb_base->ch1);
			pb->overlay = 0;
		}
		planb_prepare_close(pb);
	}
	pb->user--;
	planb_unlock(pb);

	DEBUG("PlanB: device closed\n");
}

static long planb_read(struct video_device *v, char *buf, unsigned long count,
				int nonblock)
{
	DEBUG("planb: read request\n");
	return -EINVAL;
}

static long planb_write(struct video_device *v, const char *buf,
				unsigned long count, int nonblock)
{
	DEBUG("planb: write request\n");
	return -EINVAL;
}

static int planb_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
{
	struct planb *pb=(struct planb *)dev;
1427

L
Linus Torvalds 已提交
1428
	switch (cmd)
1429
	{
L
Linus Torvalds 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
		case VIDIOCGCAP:
		{
			struct video_capability b;

			DEBUG("PlanB: IOCTL VIDIOCGCAP\n");

			strcpy (b.name, pb->video_dev.name);
			b.type = VID_TYPE_OVERLAY | VID_TYPE_CLIPPING |
				 VID_TYPE_FRAMERAM | VID_TYPE_SCALES |
				 VID_TYPE_CAPTURE;
			b.channels = 2;	/* composite & svhs */
			b.audios = 0;
			b.maxwidth = PLANB_MAXPIXELS;
1443 1444 1445 1446 1447
			b.maxheight = PLANB_MAXLINES;
			b.minwidth = 32; /* wild guess */
			b.minheight = 32;
			if (copy_to_user(arg,&b,sizeof(b)))
				return -EFAULT;
L
Linus Torvalds 已提交
1448 1449 1450 1451
			return 0;
		}
		case VIDIOCSFBUF:
		{
1452
			struct video_buffer v;
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457
			unsigned short bpp;
			unsigned int fmt;

			DEBUG("PlanB: IOCTL VIDIOCSFBUF\n");

1458
			if (!capable(CAP_SYS_ADMIN)
L
Linus Torvalds 已提交
1459
			|| !capable(CAP_SYS_RAWIO))
1460 1461 1462
				return -EPERM;
			if (copy_from_user(&v, arg,sizeof(v)))
				return -EFAULT;
L
Linus Torvalds 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
			planb_lock(pb);
			switch(v.depth) {
			case 8:
				bpp = 1;
				fmt = PLANB_GRAY;
				break;
			case 15:
			case 16:
				bpp = 2;
				fmt = PLANB_COLOUR15;
				break;
			case 24:
			case 32:
				bpp = 4;
				fmt = PLANB_COLOUR32;
				break;
			default:
				planb_unlock(pb);
1481
				return -EINVAL;
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
			}
			if (bpp * v.width > v.bytesperline) {
				planb_unlock(pb);
				return -EINVAL;
			}
			pb->win.bpp = bpp;
			pb->win.color_fmt = fmt;
			pb->frame_buffer_phys = (unsigned long) v.base;
			pb->win.sheight = v.height;
			pb->win.swidth = v.width;
			pb->picture.depth = pb->win.depth = v.depth;
			pb->win.bpl = pb->win.bpp * pb->win.swidth;
			pb->win.pad = v.bytesperline - pb->win.bpl;

1496
			DEBUG("PlanB: Display at %p is %d by %d, bytedepth %d,"
L
Linus Torvalds 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
				" bpl %d (+ %d)\n", v.base, v.width,v.height,
				pb->win.bpp, pb->win.bpl, pb->win.pad);

			pb->cmd_buff_inited = 0;
			if(pb->overlay) {
				suspend_overlay(pb);
				fill_cmd_buff(pb);
				resume_overlay(pb);
			}
			planb_unlock(pb);
1507
			return 0;
L
Linus Torvalds 已提交
1508 1509 1510
		}
		case VIDIOCGFBUF:
		{
1511
			struct video_buffer v;
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520

			DEBUG("PlanB: IOCTL VIDIOCGFBUF\n");

			v.base = (void *)pb->frame_buffer_phys;
			v.height = pb->win.sheight;
			v.width = pb->win.swidth;
			v.depth = pb->win.depth;
			v.bytesperline = pb->win.bpl + pb->win.pad;
			if (copy_to_user(arg, &v, sizeof(v)))
1521
				return -EFAULT;
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527
			return 0;
		}
		case VIDIOCCAPTURE:
		{
			int i;

1528 1529
			if(copy_from_user(&i, arg, sizeof(i)))
				return -EFAULT;
L
Linus Torvalds 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 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 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
			if(i==0) {
				DEBUG("PlanB: IOCTL VIDIOCCAPTURE Stop\n");

				if (!(pb->overlay))
					return 0;
				planb_lock(pb);
				pb->overlay = 0;
				overlay_stop(pb);
				planb_unlock(pb);
			} else {
				DEBUG("PlanB: IOCTL VIDIOCCAPTURE Start\n");

				if (pb->frame_buffer_phys == 0 ||
					  pb->win.width == 0 ||
					  pb->win.height == 0)
					return -EINVAL;
				if (pb->overlay)
					return 0;
				planb_lock(pb);
				pb->overlay = 1;
				if(!(pb->cmd_buff_inited))
					fill_cmd_buff(pb);
				overlay_start(pb);
				planb_unlock(pb);
			}
			return 0;
		}
		case VIDIOCGCHAN:
		{
			struct video_channel v;

			DEBUG("PlanB: IOCTL VIDIOCGCHAN\n");

			if(copy_from_user(&v, arg,sizeof(v)))
				return -EFAULT;
			v.flags = 0;
			v.tuners = 0;
			v.type = VIDEO_TYPE_CAMERA;
			v.norm = pb->win.norm;
			switch(v.channel)
			{
			case 0:
				strcpy(v.name,"Composite");
				break;
			case 1:
				strcpy(v.name,"SVHS");
				break;
			default:
				return -EINVAL;
				break;
			}
			if(copy_to_user(arg,&v,sizeof(v)))
				return -EFAULT;

			return 0;
		}
		case VIDIOCSCHAN:
		{
			struct video_channel v;

			DEBUG("PlanB: IOCTL VIDIOCSCHAN\n");

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

			if (v.norm != pb->win.norm) {
				int i, maxlines;

				switch (v.norm)
				{
				case VIDEO_MODE_PAL:
				case VIDEO_MODE_SECAM:
					maxlines = PLANB_MAXLINES;
					break;
				case VIDEO_MODE_NTSC:
					maxlines = PLANB_NTSC_MAXLINES;
					break;
				default:
					return -EINVAL;
					break;
				}
				planb_lock(pb);
				/* empty the grabbing queue */
				wait_event(pb->capq, !pb->grabbing);
				pb->maxlines = maxlines;
				pb->win.norm = v.norm;
				/* Stop overlay if running */
				suspend_overlay(pb);
				for(i = 0; i < MAX_GBUFFERS; i++)
					pb->gnorm_switch[i] = 1;
				/* I know it's an overkill, but.... */
				fill_cmd_buff(pb);
				/* ok, now init it accordingly */
				saa_init_regs (pb);
				/* restart overlay if it was running */
				resume_overlay(pb);
				planb_unlock(pb);
			}

			switch(v.channel)
			{
			case 0:	/* Composite	*/
				saa_set (SAA7196_IOCC,
					((saa_regs[pb->win.norm][SAA7196_IOCC] &
					  ~7) | 3), pb);
				break;
			case 1:	/* SVHS		*/
				saa_set (SAA7196_IOCC,
					((saa_regs[pb->win.norm][SAA7196_IOCC] &
					  ~7) | 4), pb);
				break;
			default:
				return -EINVAL;
				break;
			}

			return 0;
		}
		case VIDIOCGPICT:
		{
			struct video_picture vp = pb->picture;

			DEBUG("PlanB: IOCTL VIDIOCGPICT\n");

			switch(pb->win.color_fmt) {
			case PLANB_GRAY:
				vp.palette = VIDEO_PALETTE_GREY;
			case PLANB_COLOUR15:
				vp.palette = VIDEO_PALETTE_RGB555;
				break;
			case PLANB_COLOUR32:
				vp.palette = VIDEO_PALETTE_RGB32;
				break;
			default:
				vp.palette = 0;
				break;
			}

			if(copy_to_user(arg,&vp,sizeof(vp)))
				return -EFAULT;
			return 0;
		}
		case VIDIOCSPICT:
		{
			struct video_picture vp;

			DEBUG("PlanB: IOCTL VIDIOCSPICT\n");

			if(copy_from_user(&vp,arg,sizeof(vp)))
				return -EFAULT;
			pb->picture = vp;
			/* Should we do sanity checks here? */
			saa_set (SAA7196_BRIG, (unsigned char)
			    ((pb->picture.brightness) >> 8), pb);
			saa_set (SAA7196_HUEC, (unsigned char)
			    ((pb->picture.hue) >> 8) ^ 0x80, pb);
			saa_set (SAA7196_CSAT, (unsigned char)
			    ((pb->picture.colour) >> 9), pb);
			saa_set (SAA7196_CONT, (unsigned char)
			    ((pb->picture.contrast) >> 9), pb);

			return 0;
		}
		case VIDIOCSWIN:
		{
			struct video_window	vw;
			struct video_clip	clip;
			int 			i;
1698

L
Linus Torvalds 已提交
1699 1700 1701 1702 1703 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 1748 1749 1750 1751
			DEBUG("PlanB: IOCTL VIDIOCSWIN\n");

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

			planb_lock(pb);
			/* Stop overlay if running */
			suspend_overlay(pb);
			pb->win.interlace = (vw.height > pb->maxlines/2)? 1: 0;
			if (pb->win.x != vw.x ||
			    pb->win.y != vw.y ||
			    pb->win.width != vw.width ||
			    pb->win.height != vw.height ||
			    !pb->cmd_buff_inited) {
				pb->win.x = vw.x;
				pb->win.y = vw.y;
				pb->win.width = vw.width;
				pb->win.height = vw.height;
				fill_cmd_buff(pb);
			}
			/* Reset clip mask */
			memset ((void *) pb->mask, 0xff, (pb->maxlines
					* ((PLANB_MAXPIXELS + 7) & ~7)) / 8);
			/* Add any clip rects */
			for (i = 0; i < vw.clipcount; i++) {
				if (copy_from_user(&clip, vw.clips + i,
						sizeof(struct video_clip)))
					return -EFAULT;
				add_clip(pb, &clip);
			}
			/* restart overlay if it was running */
			resume_overlay(pb);
			planb_unlock(pb);
			return 0;
		}
		case VIDIOCGWIN:
		{
			struct video_window vw;

			DEBUG("PlanB: IOCTL VIDIOCGWIN\n");

			vw.x=pb->win.x;
			vw.y=pb->win.y;
			vw.width=pb->win.width;
			vw.height=pb->win.height;
			vw.chromakey=0;
			vw.flags=0;
			if(pb->win.interlace)
				vw.flags|=VIDEO_WINDOW_INTERLACE;
			if(copy_to_user(arg,&vw,sizeof(vw)))
				return -EFAULT;
			return 0;
		}
1752
		case VIDIOCSYNC: {
L
Linus Torvalds 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761
			int i;

			IDEBUG("PlanB: IOCTL VIDIOCSYNC\n");

			if(copy_from_user((void *)&i,arg,sizeof(int)))
				return -EFAULT;

			IDEBUG("PlanB: sync to frame %d\n", i);

1762 1763
			if(i > (MAX_GBUFFERS - 1) || i < 0)
				return -EINVAL;
L
Linus Torvalds 已提交
1764
chk_grab:
1765 1766 1767
			switch (pb->frame_stat[i]) {
			case GBUFFER_UNUSED:
				return -EINVAL;
L
Linus Torvalds 已提交
1768 1769 1770
			case GBUFFER_GRABBING:
				IDEBUG("PlanB: waiting for grab"
							" done (%d)\n", i);
1771
				interruptible_sleep_on(&pb->capq);
L
Linus Torvalds 已提交
1772 1773 1774
				if(signal_pending(current))
					return -EINTR;
				goto chk_grab;
1775 1776 1777 1778 1779
			case GBUFFER_DONE:
				pb->frame_stat[i] = GBUFFER_UNUSED;
				break;
			}
			return 0;
L
Linus Torvalds 已提交
1780 1781
		}

1782
		case VIDIOCMCAPTURE:
L
Linus Torvalds 已提交
1783
		{
1784
			struct video_mmap vm;
L
Linus Torvalds 已提交
1785 1786 1787 1788 1789 1790
			volatile unsigned int status;

			IDEBUG("PlanB: IOCTL VIDIOCMCAPTURE\n");

			if(copy_from_user((void *) &vm,(void *)arg,sizeof(vm)))
				return -EFAULT;
1791 1792 1793
			status = pb->frame_stat[vm.frame];
			if (status != GBUFFER_UNUSED)
				return -EBUSY;
L
Linus Torvalds 已提交
1794

1795
			return vgrab(pb, &vm);
L
Linus Torvalds 已提交
1796
		}
1797

L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
		case VIDIOCGMBUF:
		{
			int i;
			struct video_mbuf vm;

			DEBUG("PlanB: IOCTL VIDIOCGMBUF\n");

			memset(&vm, 0 , sizeof(vm));
			vm.size = PLANB_MAX_FBUF * MAX_GBUFFERS;
			vm.frames = MAX_GBUFFERS;
			for(i = 0; i<MAX_GBUFFERS; i++)
				vm.offsets[i] = PLANB_MAX_FBUF * i;
			if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
				return -EFAULT;
			return 0;
		}
1814

L
Linus Torvalds 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
		case PLANBIOCGSAAREGS:
		{
			struct planb_saa_regs preg;

			DEBUG("PlanB: IOCTL PLANBIOCGSAAREGS\n");

			if(copy_from_user(&preg, arg, sizeof(preg)))
				return -EFAULT;
			if(preg.addr >= SAA7196_NUMREGS)
				return -EINVAL;
			preg.val = saa_regs[pb->win.norm][preg.addr];
			if(copy_to_user((void *)arg, (void *)&preg,
								sizeof(preg)))
				return -EFAULT;
			return 0;
		}
1831

L
Linus Torvalds 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
		case PLANBIOCSSAAREGS:
		{
			struct planb_saa_regs preg;

			DEBUG("PlanB: IOCTL PLANBIOCSSAAREGS\n");

			if(copy_from_user(&preg, arg, sizeof(preg)))
				return -EFAULT;
			if(preg.addr >= SAA7196_NUMREGS)
				return -EINVAL;
			saa_set (preg.addr, preg.val, pb);
			return 0;
		}
1845

L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
		case PLANBIOCGSTAT:
		{
			struct planb_stat_regs pstat;

			DEBUG("PlanB: IOCTL PLANBIOCGSTAT\n");

			pstat.ch1_stat = in_le32(&pb->planb_base->ch1.status);
			pstat.ch2_stat = in_le32(&pb->planb_base->ch2.status);
			pstat.saa_stat0 = saa_status(0, pb);
			pstat.saa_stat1 = saa_status(1, pb);

			if(copy_to_user((void *)arg, (void *)&pstat,
							sizeof(pstat)))
				return -EFAULT;
			return 0;
		}
1862

L
Linus Torvalds 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 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 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
		case PLANBIOCSMODE: {
			int v;

			DEBUG("PlanB: IOCTL PLANBIOCSMODE\n");

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

			switch(v)
			{
			case PLANB_TV_MODE:
				saa_set (SAA7196_STDC,
					(saa_regs[pb->win.norm][SAA7196_STDC] &
					  0x7f), pb);
				break;
			case PLANB_VTR_MODE:
				saa_set (SAA7196_STDC,
					(saa_regs[pb->win.norm][SAA7196_STDC] |
					  0x80), pb);
				break;
			default:
				return -EINVAL;
				break;
			}
			pb->win.mode = v;
			return 0;
		}
		case PLANBIOCGMODE: {
			int v=pb->win.mode;

			DEBUG("PlanB: IOCTL PLANBIOCGMODE\n");

			if(copy_to_user(arg,&v,sizeof(v)))
				return -EFAULT;
			return 0;
		}
#ifdef PLANB_GSCANLINE
		case PLANBG_GRAB_BPL: {
			int v=pb->gbytes_per_line;

			DEBUG("PlanB: IOCTL PLANBG_GRAB_BPL\n");

			if(copy_to_user(arg,&v,sizeof(v)))
				return -EFAULT;
			return 0;
		}
#endif /* PLANB_GSCANLINE */
		case PLANB_INTR_DEBUG: {
			int i;

			DEBUG("PlanB: IOCTL PLANB_INTR_DEBUG\n");

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

			/* avoid hang ups all together */
			for (i = 0; i < MAX_GBUFFERS; i++) {
				if(pb->frame_stat[i] == GBUFFER_GRABBING) {
					pb->frame_stat[i] = GBUFFER_DONE;
				}
			}
			if(pb->grabbing)
				pb->grabbing--;
			wake_up_interruptible(&pb->capq);
			return 0;
		}
		case PLANB_INV_REGS: {
			int i;
			struct planb_any_regs any;

			DEBUG("PlanB: IOCTL PLANB_INV_REGS\n");

			if(copy_from_user(&any, arg, sizeof(any)))
				return -EFAULT;
			if(any.offset < 0 || any.offset + any.bytes > 0x400)
				return -EINVAL;
			if(any.bytes > 128)
				return -EINVAL;
			for (i = 0; i < any.bytes; i++) {
				any.data[i] =
					in_8((unsigned char *)pb->planb_base
							+ any.offset + i);
			}
			if(copy_to_user(arg,&any,sizeof(any)))
				return -EFAULT;
			return 0;
		}
		default:
		{
			DEBUG("PlanB: Unimplemented IOCTL\n");
			return -ENOIOCTLCMD;
		}
	/* Some IOCTLs are currently unsupported on PlanB */
		case VIDIOCGTUNER: {
		DEBUG("PlanB: IOCTL VIDIOCGTUNER\n");
			goto unimplemented; }
		case VIDIOCSTUNER: {
		DEBUG("PlanB: IOCTL VIDIOCSTUNER\n");
			goto unimplemented; }
		case VIDIOCSFREQ: {
		DEBUG("PlanB: IOCTL VIDIOCSFREQ\n");
			goto unimplemented; }
		case VIDIOCGFREQ: {
		DEBUG("PlanB: IOCTL VIDIOCGFREQ\n");
			goto unimplemented; }
		case VIDIOCKEY: {
		DEBUG("PlanB: IOCTL VIDIOCKEY\n");
			goto unimplemented; }
		case VIDIOCSAUDIO: {
		DEBUG("PlanB: IOCTL VIDIOCSAUDIO\n");
			goto unimplemented; }
		case VIDIOCGAUDIO: {
		DEBUG("PlanB: IOCTL VIDIOCGAUDIO\n");
			goto unimplemented; }
unimplemented:
		DEBUG("       Unimplemented\n");
			return -ENOIOCTLCMD;
	}
	return 0;
}

static int planb_mmap(struct vm_area_struct *vma, struct video_device *dev, const char *adr, unsigned long size)
{
	int i;
	struct planb *pb = (struct planb *)dev;
1988
	unsigned long start = (unsigned long)adr;
L
Linus Torvalds 已提交
1989 1990

	if (size > MAX_GBUFFERS * PLANB_MAX_FBUF)
1991
		return -EINVAL;
L
Linus Torvalds 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
	if (!pb->rawbuf) {
		int err;
		if((err=grabbuf_alloc(pb)))
			return err;
	}
	for (i = 0; i < pb->rawbuf_size; i++) {
		unsigned long pfn;

		pfn = virt_to_phys((void *)pb->rawbuf[i]) >> PAGE_SHIFT;
		if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
			return -EAGAIN;
		start += PAGE_SIZE;
		if (size <= PAGE_SIZE)
			break;
		size -= PAGE_SIZE;
	}
	return 0;
}

static struct video_device planb_template=
{
	.owner		= THIS_MODULE,
	.name		= PLANB_DEVICE_NAME,
	.type		= VID_TYPE_OVERLAY,
	.open		= planb_open,
	.close		= planb_close,
	.read		= planb_read,
	.write		= planb_write,
	.ioctl		= planb_ioctl,
	.mmap		= planb_mmap,	/* mmap? */
};

static int init_planb(struct planb *pb)
{
	unsigned char saa_rev;
	int i, result;

	memset ((void *) &pb->win, 0, sizeof (struct planb_window));
	/* Simple sanity check */
	if(def_norm >= NUM_SUPPORTED_NORM || def_norm < 0) {
		printk(KERN_ERR "PlanB: Option(s) invalid\n");
		return -2;
	}
	pb->win.norm = def_norm;
	pb->win.mode = PLANB_TV_MODE;	/* TV mode */
	pb->win.interlace=1;
	pb->win.x=0;
	pb->win.y=0;
	pb->win.width=768; /* 640 */
	pb->win.height=576; /* 480 */
	pb->maxlines=576;
#if 0
	btv->win.cropwidth=768; /* 640 */
	btv->win.cropheight=576; /* 480 */
	btv->win.cropx=0;
	btv->win.cropy=0;
#endif
	pb->win.pad=0;
	pb->win.bpp=4;
	pb->win.depth=32;
	pb->win.color_fmt=PLANB_COLOUR32;
	pb->win.bpl=1024*pb->win.bpp;
	pb->win.swidth=1024;
	pb->win.sheight=768;
#ifdef PLANB_GSCANLINE
	if((pb->gbytes_per_line = PLANB_MAXPIXELS * 4) > PAGE_SIZE
				|| (pb->gbytes_per_line <= 0))
		return -3;
	else {
		/* page align pb->gbytes_per_line for DMA purpose */
		for(i = PAGE_SIZE; pb->gbytes_per_line < (i>>1);)
			i>>=1;
		pb->gbytes_per_line = i;
	}
#endif
	pb->tab_size = PLANB_MAXLINES + 40;
	pb->suspend = 0;
2069
	mutex_init(&pb->lock);
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
	pb->ch1_cmd = 0;
	pb->ch2_cmd = 0;
	pb->mask = 0;
	pb->priv_space = 0;
	pb->offset = 0;
	pb->user = 0;
	pb->overlay = 0;
	init_waitqueue_head(&pb->suspendq);
	pb->cmd_buff_inited = 0;
	pb->frame_buffer_phys = 0;

	/* Reset DMA controllers */
	planb_dbdma_stop(&pb->planb_base->ch2);
	planb_dbdma_stop(&pb->planb_base->ch1);

	saa_rev =  (saa_status(0, pb) & 0xf0) >> 4;
	printk(KERN_INFO "PlanB: SAA7196 video processor rev. %d\n", saa_rev);
	/* Initialize the SAA registers in memory and on chip */
	saa_init_regs (pb);

	/* clear interrupt mask */
	pb->intr_mask = PLANB_CLR_IRQ;

2093
	result = request_irq(pb->irq, planb_irq, 0, "PlanB", pb);
2094 2095 2096
	if (result < 0) {
		if (result==-EINVAL)
			printk(KERN_ERR "PlanB: Bad irq number (%d) "
L
Linus Torvalds 已提交
2097 2098 2099 2100 2101 2102 2103
						"or handler\n", (int)pb->irq);
		else if (result==-EBUSY)
			printk(KERN_ERR "PlanB: I don't know why, "
					"but IRQ %d is busy\n", (int)pb->irq);
		return result;
	}
	disable_irq(pb->irq);
2104

L
Linus Torvalds 已提交
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 2138 2139 2140 2141 2142 2143 2144
	/* Now add the template and register the device unit. */
	memcpy(&pb->video_dev,&planb_template,sizeof(planb_template));

	pb->picture.brightness=0x90<<8;
	pb->picture.contrast = 0x70 << 8;
	pb->picture.colour = 0x70<<8;
	pb->picture.hue = 0x8000;
	pb->picture.whiteness = 0;
	pb->picture.depth = pb->win.depth;

	pb->frame_stat=NULL;
	init_waitqueue_head(&pb->capq);
	for(i=0; i<MAX_GBUFFERS; i++) {
		pb->gbuf_idx[i] = PLANB_MAX_FBUF * i / PAGE_SIZE;
		pb->gwidth[i]=0;
		pb->gheight[i]=0;
		pb->gfmt[i]=0;
		pb->cap_cmd[i]=NULL;
#ifndef PLANB_GSCANLINE
		pb->l_fr_addr_idx[i] = MAX_GBUFFERS * (PLANB_MAX_FBUF
						/ PAGE_SIZE + 1) + MAX_LNUM * i;
		pb->lsize[i] = 0;
		pb->lnum[i] = 0;
#endif
	}
	pb->rawbuf=NULL;
	pb->grabbing=0;

	/* enable interrupts */
	out_le32(&pb->planb_base->intr_stat, PLANB_CLR_IRQ);
	pb->intr_mask = PLANB_FRM_IRQ;
	enable_irq(pb->irq);

	if(video_register_device(&pb->video_dev, VFL_TYPE_GRABBER, video_nr)<0)
		return -1;

	return 0;
}

/*
2145
 *	Scan for a PlanB controller, request the irq and map the io memory
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
 */

static int find_planb(void)
{
	struct planb		*pb;
	struct device_node	*planb_devices;
	unsigned char		dev_fn, confreg, bus;
	unsigned int		old_base, new_base;
	unsigned int		irq;
	struct pci_dev 		*pdev;
	int rc;

2158
	if (!machine_is(powermac))
L
Linus Torvalds 已提交
2159 2160
		return 0;

2161
	planb_devices = of_find_node_by_name(NULL, "planb");
L
Linus Torvalds 已提交
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
	if (planb_devices == 0) {
		planb_num=0;
		printk(KERN_WARNING "PlanB: no device found!\n");
		return planb_num;
	}

	if (planb_devices->next != NULL)
		printk(KERN_ERR "Warning: only using first PlanB device!\n");
	pb = &planbs[0];
	planb_num = 1;

2173 2174 2175
	if (planb_devices->n_addrs != 1) {
		printk (KERN_WARNING "PlanB: expecting 1 address for planb "
			"(got %d)", planb_devices->n_addrs);
2176
		of_node_put(planb_devices);
L
Linus Torvalds 已提交
2177 2178 2179 2180 2181 2182
		return 0;
	}

	if (planb_devices->n_intrs == 0) {
		printk(KERN_WARNING "PlanB: no intrs for device %s\n",
		       planb_devices->full_name);
2183
		of_node_put(planb_devices);
L
Linus Torvalds 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
		return 0;
	} else {
		irq = planb_devices->intrs[0].line;
	}

	/* Initialize PlanB's PCI registers */

	/* There is a bug with the way OF assigns addresses
	   to the devices behind the chaos bridge.
	   control needs only 0x1000 of space, but decodes only
	   the upper 16 bits. It therefore occupies a full 64K.
	   OF assigns the planb controller memory within this space;
	   so we need to change that here in order to access planb. */

	/* We remap to 0xf1000000 in hope that nobody uses it ! */

	bus = (planb_devices->addrs[0].space >> 16) & 0xff;
	dev_fn = (planb_devices->addrs[0].space >> 8) & 0xff;
	confreg = planb_devices->addrs[0].space & 0xff;
	old_base = planb_devices->addrs[0].address;
	new_base = 0xf1000000;
2205
	of_node_put(planb_devices);
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210

	DEBUG("PlanB: Found on bus %d, dev %d, func %d, "
		"membase 0x%x (base reg. 0x%x)\n",
		bus, PCI_SLOT(dev_fn), PCI_FUNC(dev_fn), old_base, confreg);

2211
	pdev = pci_get_bus_and_slot(bus, dev_fn);
L
Linus Torvalds 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	if (!pdev) {
		printk(KERN_ERR "planb: cannot find slot\n");
		goto err_out;
	}

	/* Enable response in memory space, bus mastering,
	   use memory write and invalidate */
	rc = pci_enable_device(pdev);
	if (rc) {
		printk(KERN_ERR "planb: cannot enable PCI device %s\n",
		       pci_name(pdev));
		goto err_out;
	}
	rc = pci_set_mwi(pdev);
	if (rc) {
		printk(KERN_ERR "planb: cannot enable MWI on PCI device %s\n",
		       pci_name(pdev));
		goto err_out_disable;
	}
	pci_set_master(pdev);

	/* Set the new base address */
	pci_write_config_dword (pdev, confreg, new_base);

	planb_regs = (volatile struct planb_registers *)
						ioremap (new_base, 0x400);
	pb->planb_base = planb_regs;
	pb->planb_base_phys = (struct planb_registers *)new_base;
	pb->irq	= irq;
2241
	pb->dev = pdev;
2242

L
Linus Torvalds 已提交
2243 2244 2245 2246 2247 2248
	return planb_num;

err_out_disable:
	pci_disable_device(pdev);
err_out:
	/* FIXME handle error */   /* comment moved from pci_find_slot, above */
2249
	pci_dev_put(pdev);
L
Linus Torvalds 已提交
2250 2251 2252 2253 2254 2255 2256 2257
	return 0;
}

static void release_planb(void)
{
	int i;
	struct planb *pb;

2258
	for (i=0;i<planb_num; i++)
L
Linus Torvalds 已提交
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
	{
		pb=&planbs[i];

		/* stop and flash DMAs unconditionally */
		planb_dbdma_stop(&pb->planb_base->ch2);
		planb_dbdma_stop(&pb->planb_base->ch1);

		/* clear and free interrupts */
		pb->intr_mask = PLANB_CLR_IRQ;
		out_le32 (&pb->planb_base->intr_stat, PLANB_CLR_IRQ);
		free_irq(pb->irq, pb);

		/* make sure all allocated memory are freed */
		planb_prepare_close(pb);

		printk(KERN_INFO "PlanB: unregistering with v4l\n");
		video_unregister_device(&pb->video_dev);

2277 2278
		pci_dev_put(pb->dev);

L
Linus Torvalds 已提交
2279 2280 2281 2282 2283 2284 2285 2286
		/* note that iounmap() does nothing on the PPC right now */
		iounmap ((void *)pb->planb_base);
	}
}

static int __init init_planbs(void)
{
	int i;
2287

L
Linus Torvalds 已提交
2288 2289 2290 2291 2292 2293 2294 2295 2296
	if (find_planb()<=0)
		return -EIO;

	for (i=0; i<planb_num; i++) {
		if (init_planb(&planbs[i])<0) {
			printk(KERN_ERR "PlanB: error registering device %d"
							" with v4l\n", i);
			release_planb();
			return -EIO;
2297
		}
L
Linus Torvalds 已提交
2298
		printk(KERN_INFO "PlanB: registered device %d with v4l\n", i);
2299
	}
L
Linus Torvalds 已提交
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
	return 0;
}

static void __exit exit_planbs(void)
{
	release_planb();
}

module_init(init_planbs);
module_exit(exit_planbs);