ddbridge-core.c 57.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * ddbridge.c: Digital Devices PCIe bridge driver
 *
 * Copyright (C) 2010-2011 Digital Devices GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 only, as published by the Free Software Foundation.
 *
 *
 * 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.
 *
16 17
 * To obtain the license, point your browser to
 * http://www.gnu.org/copyleft/gpl.html
18 19
 */

20 21
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

22 23 24 25 26 27
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/poll.h>
28
#include <linux/io.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/swab.h>
#include <linux/vmalloc.h>
#include "ddbridge.h"

#include "ddbridge-regs.h"

#include "tda18271c2dd.h"
#include "stv6110x.h"
#include "stv090x.h"
#include "lnbh24.h"
#include "drxk.h"
44 45
#include "stv0367.h"
#include "stv0367_priv.h"
46
#include "cxd2841er.h"
47
#include "tda18212.h"
48

49 50 51 52
static int xo2_speed = 2;
module_param(xo2_speed, int, 0444);
MODULE_PARM_DESC(xo2_speed, "default transfer speed for xo2 based duoflex, 0=55,1=75,2=90,3=104 MBit/s, default=2, use attribute to change for individual cards");

53 54 55 56 57 58 59
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

/* MSI had problems with lost interrupts, fixed but needs testing */
#undef CONFIG_PCI_MSI

/******************************************************************************/

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static int i2c_io(struct i2c_adapter *adapter, u8 adr,
		  u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen)
{
	struct i2c_msg msgs[2] = {{.addr = adr,  .flags = 0,
				   .buf  = wbuf, .len   = wlen },
				  {.addr = adr,  .flags = I2C_M_RD,
				   .buf  = rbuf,  .len   = rlen } };
	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
}

static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
{
	struct i2c_msg msg = {.addr = adr, .flags = 0,
			      .buf = data, .len = len};

	return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1;
}

78 79 80
static int i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val)
{
	struct i2c_msg msgs[1] = {{.addr = adr,  .flags = I2C_M_RD,
81
				   .buf  = val,  .len   = 1 } };
82 83 84
	return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1;
}

85 86
static int i2c_read_regs(struct i2c_adapter *adapter,
			 u8 adr, u8 reg, u8 *val, u8 len)
87 88 89 90
{
	struct i2c_msg msgs[2] = {{.addr = adr,  .flags = 0,
				   .buf  = &reg, .len   = 1 },
				  {.addr = adr,  .flags = I2C_M_RD,
91
				   .buf  = val,  .len   = len } };
92 93 94
	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
}

95 96 97 98 99
static int i2c_read_reg(struct i2c_adapter *adapter, u8 adr, u8 reg, u8 *val)
{
	return i2c_read_regs(adapter, adr, reg, val, 1);
}

100 101 102 103 104 105 106
static int i2c_read_reg16(struct i2c_adapter *adapter, u8 adr,
			  u16 reg, u8 *val)
{
	u8 msg[2] = {reg>>8, reg&0xff};
	struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
				   .buf  = msg, .len   = 2},
				  {.addr = adr, .flags = I2C_M_RD,
107
				   .buf  = val, .len   = 1} };
108 109 110
	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
}

111 112 113 114 115 116 117 118
static int i2c_write_reg(struct i2c_adapter *adap, u8 adr,
			 u8 reg, u8 val)
{
	u8 msg[2] = {reg, val};

	return i2c_write(adap, adr, msg, 2);
}

119 120 121 122 123 124 125 126 127 128 129 130 131
static inline u32 safe_ddbreadl(struct ddb *dev, u32 adr)
{
	u32 val = ddbreadl(adr);

	/* (ddb)readl returns (uint)-1 (all bits set) on failure, catch that */
	if (val == ~0) {
		printk(KERN_ERR "ddbreadl failure, adr=%08x\n", adr);
		return 0;
	}

	return val;
}

132 133 134
static int ddb_i2c_cmd(struct ddb_i2c *i2c, u32 adr, u32 cmd)
{
	struct ddb *dev = i2c->dev;
135
	long stat;
136 137 138 139 140
	u32 val;

	i2c->done = 0;
	ddbwritel((adr << 9) | cmd, i2c->regs + I2C_COMMAND);
	stat = wait_event_timeout(i2c->wq, i2c->done == 1, HZ);
141
	if (stat == 0) {
142
		dev_err(&dev->pdev->dev, "I2C timeout\n");
143 144
		{ /* MSI debugging*/
			u32 istat = ddbreadl(INTERRUPT_STATUS);
145
			dev_err(&dev->pdev->dev, "IRS %08x\n", istat);
146 147 148 149
			ddbwritel(istat, INTERRUPT_ACK);
		}
		return -EIO;
	}
150
	val = ddbreadl(i2c->regs+I2C_COMMAND);
151 152 153 154 155 156 157 158 159 160
	if (val & 0x70000)
		return -EIO;
	return 0;
}

static int ddb_i2c_master_xfer(struct i2c_adapter *adapter,
			       struct i2c_msg msg[], int num)
{
	struct ddb_i2c *i2c = (struct ddb_i2c *)i2c_get_adapdata(adapter);
	struct ddb *dev = i2c->dev;
161
	u8 addr = 0;
162 163 164 165 166 167 168

	if (num)
		addr = msg[0].addr;

	if (num == 2 && msg[1].flags & I2C_M_RD &&
	    !(msg[0].flags & I2C_M_RD)) {
		memcpy_toio(dev->regs + I2C_TASKMEM_BASE + i2c->wbuf,
169
			    msg[0].buf, msg[0].len);
170 171 172 173 174 175 176 177 178 179 180
		ddbwritel(msg[0].len|(msg[1].len << 16),
			  i2c->regs+I2C_TASKLENGTH);
		if (!ddb_i2c_cmd(i2c, addr, 1)) {
			memcpy_fromio(msg[1].buf,
				      dev->regs + I2C_TASKMEM_BASE + i2c->rbuf,
				      msg[1].len);
			return num;
		}
	}

	if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
181
		ddbcpyto(I2C_TASKMEM_BASE + i2c->wbuf, msg[0].buf, msg[0].len);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		ddbwritel(msg[0].len, i2c->regs + I2C_TASKLENGTH);
		if (!ddb_i2c_cmd(i2c, addr, 2))
			return num;
	}
	if (num == 1 && (msg[0].flags & I2C_M_RD)) {
		ddbwritel(msg[0].len << 16, i2c->regs + I2C_TASKLENGTH);
		if (!ddb_i2c_cmd(i2c, addr, 3)) {
			ddbcpyfrom(msg[0].buf,
				   I2C_TASKMEM_BASE + i2c->rbuf, msg[0].len);
			return num;
		}
	}
	return -EIO;
}


static u32 ddb_i2c_functionality(struct i2c_adapter *adap)
{
	return I2C_FUNC_SMBUS_EMUL;
}

203
static struct i2c_algorithm ddb_i2c_algo = {
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
	.master_xfer   = ddb_i2c_master_xfer,
	.functionality = ddb_i2c_functionality,
};

static void ddb_i2c_release(struct ddb *dev)
{
	int i;
	struct ddb_i2c *i2c;
	struct i2c_adapter *adap;

	for (i = 0; i < dev->info->port_num; i++) {
		i2c = &dev->i2c[i];
		adap = &i2c->adap;
		i2c_del_adapter(adap);
	}
}

static int ddb_i2c_init(struct ddb *dev)
{
	int i, j, stat = 0;
	struct ddb_i2c *i2c;
	struct i2c_adapter *adap;

	for (i = 0; i < dev->info->port_num; i++) {
		i2c = &dev->i2c[i];
		i2c->dev = dev;
		i2c->nr = i;
		i2c->wbuf = i * (I2C_TASKMEM_SIZE / 4);
		i2c->rbuf = i2c->wbuf + (I2C_TASKMEM_SIZE / 8);
		i2c->regs = 0x80 + i * 0x20;
		ddbwritel(I2C_SPEED_100, i2c->regs + I2C_TIMING);
		ddbwritel((i2c->rbuf << 16) | i2c->wbuf,
			  i2c->regs + I2C_TASKADDRESS);
		init_waitqueue_head(&i2c->wq);

		adap = &i2c->adap;
		i2c_set_adapdata(adap, i2c);
#ifdef I2C_ADAP_CLASS_TV_DIGITAL
		adap->class = I2C_ADAP_CLASS_TV_DIGITAL|I2C_CLASS_TV_ANALOG;
#else
#ifdef I2C_CLASS_TV_ANALOG
		adap->class = I2C_CLASS_TV_ANALOG;
#endif
#endif
		strcpy(adap->name, "ddbridge");
		adap->algo = &ddb_i2c_algo;
		adap->algo_data = (void *)i2c;
		adap->dev.parent = &dev->pdev->dev;
		stat = i2c_add_adapter(adap);
		if (stat)
			break;
	}
	if (stat)
		for (j = 0; j < i; j++) {
			i2c = &dev->i2c[j];
			adap = &i2c->adap;
			i2c_del_adapter(adap);
		}
	return stat;
}


/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

270
#if 0
271 272 273 274 275 276 277 278 279 280 281 282 283
static void set_table(struct ddb *dev, u32 off,
		      dma_addr_t *pbuf, u32 num)
{
	u32 i, base;
	u64 mem;

	base = DMA_BASE_ADDRESS_TABLE + off;
	for (i = 0; i < num; i++) {
		mem = pbuf[i];
		ddbwritel(mem & 0xffffffff, base + i * 8);
		ddbwritel(mem >> 32, base + i * 8 + 4);
	}
}
284
#endif
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

static void ddb_address_table(struct ddb *dev)
{
	u32 i, j, base;
	u64 mem;
	dma_addr_t *pbuf;

	for (i = 0; i < dev->info->port_num * 2; i++) {
		base = DMA_BASE_ADDRESS_TABLE + i * 0x100;
		pbuf = dev->input[i].pbuf;
		for (j = 0; j < dev->input[i].dma_buf_num; j++) {
			mem = pbuf[j];
			ddbwritel(mem & 0xffffffff, base + j * 8);
			ddbwritel(mem >> 32, base + j * 8 + 4);
		}
	}
	for (i = 0; i < dev->info->port_num; i++) {
		base = DMA_BASE_ADDRESS_TABLE + 0x800 + i * 0x100;
		pbuf = dev->output[i].pbuf;
		for (j = 0; j < dev->output[i].dma_buf_num; j++) {
			mem = pbuf[j];
			ddbwritel(mem & 0xffffffff, base + j * 8);
			ddbwritel(mem >> 32, base + j * 8 + 4);
		}
	}
}

static void io_free(struct pci_dev *pdev, u8 **vbuf,
		    dma_addr_t *pbuf, u32 size, int num)
{
	int i;

	for (i = 0; i < num; i++) {
		if (vbuf[i]) {
			pci_free_consistent(pdev, size, vbuf[i], pbuf[i]);
320
			vbuf[i] = NULL;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 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
		}
	}
}

static int io_alloc(struct pci_dev *pdev, u8 **vbuf,
		    dma_addr_t *pbuf, u32 size, int num)
{
	int i;

	for (i = 0; i < num; i++) {
		vbuf[i] = pci_alloc_consistent(pdev, size, &pbuf[i]);
		if (!vbuf[i])
			return -ENOMEM;
	}
	return 0;
}

static int ddb_buffers_alloc(struct ddb *dev)
{
	int i;
	struct ddb_port *port;

	for (i = 0; i < dev->info->port_num; i++) {
		port = &dev->port[i];
		switch (port->class) {
		case DDB_PORT_TUNER:
			if (io_alloc(dev->pdev, port->input[0]->vbuf,
				     port->input[0]->pbuf,
				     port->input[0]->dma_buf_size,
				     port->input[0]->dma_buf_num) < 0)
				return -1;
			if (io_alloc(dev->pdev, port->input[1]->vbuf,
				     port->input[1]->pbuf,
				     port->input[1]->dma_buf_size,
				     port->input[1]->dma_buf_num) < 0)
				return -1;
			break;
		case DDB_PORT_CI:
			if (io_alloc(dev->pdev, port->input[0]->vbuf,
				     port->input[0]->pbuf,
				     port->input[0]->dma_buf_size,
				     port->input[0]->dma_buf_num) < 0)
				return -1;
			if (io_alloc(dev->pdev, port->output->vbuf,
				     port->output->pbuf,
				     port->output->dma_buf_size,
				     port->output->dma_buf_num) < 0)
				return -1;
			break;
		default:
			break;
		}
	}
	ddb_address_table(dev);
	return 0;
}

static void ddb_buffers_free(struct ddb *dev)
{
	int i;
	struct ddb_port *port;

	for (i = 0; i < dev->info->port_num; i++) {
		port = &dev->port[i];
		io_free(dev->pdev, port->input[0]->vbuf,
			port->input[0]->pbuf,
			port->input[0]->dma_buf_size,
			port->input[0]->dma_buf_num);
		io_free(dev->pdev, port->input[1]->vbuf,
			port->input[1]->pbuf,
			port->input[1]->dma_buf_size,
			port->input[1]->dma_buf_num);
		io_free(dev->pdev, port->output->vbuf,
			port->output->pbuf,
			port->output->dma_buf_size,
			port->output->dma_buf_num);
	}
}

static void ddb_input_start(struct ddb_input *input)
{
	struct ddb *dev = input->port->dev;

	spin_lock_irq(&input->lock);
	input->cbuf = 0;
	input->coff = 0;

	/* reset */
	ddbwritel(0, TS_INPUT_CONTROL(input->nr));
	ddbwritel(2, TS_INPUT_CONTROL(input->nr));
	ddbwritel(0, TS_INPUT_CONTROL(input->nr));

	ddbwritel((1 << 16) |
		  (input->dma_buf_num << 11) |
		  (input->dma_buf_size >> 7),
		  DMA_BUFFER_SIZE(input->nr));
	ddbwritel(0, DMA_BUFFER_ACK(input->nr));

	ddbwritel(1, DMA_BASE_WRITE);
	ddbwritel(3, DMA_BUFFER_CONTROL(input->nr));
	ddbwritel(9, TS_INPUT_CONTROL(input->nr));
	input->running = 1;
	spin_unlock_irq(&input->lock);
}

static void ddb_input_stop(struct ddb_input *input)
{
	struct ddb *dev = input->port->dev;

	spin_lock_irq(&input->lock);
	ddbwritel(0, TS_INPUT_CONTROL(input->nr));
	ddbwritel(0, DMA_BUFFER_CONTROL(input->nr));
	input->running = 0;
	spin_unlock_irq(&input->lock);
}

static void ddb_output_start(struct ddb_output *output)
{
	struct ddb *dev = output->port->dev;

	spin_lock_irq(&output->lock);
	output->cbuf = 0;
	output->coff = 0;
	ddbwritel(0, TS_OUTPUT_CONTROL(output->nr));
	ddbwritel(2, TS_OUTPUT_CONTROL(output->nr));
	ddbwritel(0, TS_OUTPUT_CONTROL(output->nr));
	ddbwritel(0x3c, TS_OUTPUT_CONTROL(output->nr));
	ddbwritel((1 << 16) |
		  (output->dma_buf_num << 11) |
		  (output->dma_buf_size >> 7),
		  DMA_BUFFER_SIZE(output->nr + 8));
	ddbwritel(0, DMA_BUFFER_ACK(output->nr + 8));

	ddbwritel(1, DMA_BASE_READ);
	ddbwritel(3, DMA_BUFFER_CONTROL(output->nr + 8));
456
	/* ddbwritel(0xbd, TS_OUTPUT_CONTROL(output->nr)); */
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
	ddbwritel(0x1d, TS_OUTPUT_CONTROL(output->nr));
	output->running = 1;
	spin_unlock_irq(&output->lock);
}

static void ddb_output_stop(struct ddb_output *output)
{
	struct ddb *dev = output->port->dev;

	spin_lock_irq(&output->lock);
	ddbwritel(0, TS_OUTPUT_CONTROL(output->nr));
	ddbwritel(0, DMA_BUFFER_CONTROL(output->nr + 8));
	output->running = 0;
	spin_unlock_irq(&output->lock);
}

static u32 ddb_output_free(struct ddb_output *output)
{
	u32 idx, off, stat = output->stat;
	s32 diff;

	idx = (stat >> 11) & 0x1f;
	off = (stat & 0x7ff) << 7;

	if (output->cbuf != idx) {
		if ((((output->cbuf + 1) % output->dma_buf_num) == idx) &&
		    (output->dma_buf_size - output->coff <= 188))
			return 0;
		return 188;
	}
	diff = off - output->coff;
	if (diff <= 0 || diff > 188)
		return 188;
	return 0;
}

493
static ssize_t ddb_output_write(struct ddb_output *output,
494
				const __user u8 *buf, size_t count)
495 496 497 498 499 500 501 502 503 504 505 506
{
	struct ddb *dev = output->port->dev;
	u32 idx, off, stat = output->stat;
	u32 left = count, len;

	idx = (stat >> 11) & 0x1f;
	off = (stat & 0x7ff) << 7;

	while (left) {
		len = output->dma_buf_size - output->coff;
		if ((((output->cbuf + 1) % output->dma_buf_num) == idx) &&
		    (off == 0)) {
507
			if (len <= 188)
508
				break;
509
			len -= 188;
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
		}
		if (output->cbuf == idx) {
			if (off > output->coff) {
#if 1
				len = off - output->coff;
				len -= (len % 188);
				if (len <= 188)

#endif
					break;
				len -= 188;
			}
		}
		if (len > left)
			len = left;
		if (copy_from_user(output->vbuf[output->cbuf] + output->coff,
				   buf, len))
			return -EIO;
		left -= len;
		buf += len;
		output->coff += len;
		if (output->coff == output->dma_buf_size) {
			output->coff = 0;
			output->cbuf = ((output->cbuf + 1) % output->dma_buf_num);
		}
		ddbwritel((output->cbuf << 11) | (output->coff >> 7),
			  DMA_BUFFER_ACK(output->nr + 8));
	}
	return count - left;
}

static u32 ddb_input_avail(struct ddb_input *input)
{
	struct ddb *dev = input->port->dev;
	u32 idx, off, stat = input->stat;
	u32 ctrl = ddbreadl(DMA_BUFFER_CONTROL(input->nr));

	idx = (stat >> 11) & 0x1f;
	off = (stat & 0x7ff) << 7;

	if (ctrl & 4) {
551
		dev_err(&dev->pdev->dev, "IA %d %d %08x\n", idx, off, ctrl);
552 553 554 555 556 557 558 559
		ddbwritel(input->stat, DMA_BUFFER_ACK(input->nr));
		return 0;
	}
	if (input->cbuf != idx)
		return 188;
	return 0;
}

560
static ssize_t ddb_input_read(struct ddb_input *input, __user u8 *buf, size_t count)
561 562 563
{
	struct ddb *dev = input->port->dev;
	u32 left = count;
564
	u32 idx, free, stat = input->stat;
565 566 567 568 569 570 571 572 573 574 575 576
	int ret;

	idx = (stat >> 11) & 0x1f;

	while (left) {
		if (input->cbuf == idx)
			return count - left;
		free = input->dma_buf_size - input->coff;
		if (free > left)
			free = left;
		ret = copy_to_user(buf, input->vbuf[input->cbuf] +
				   input->coff, free);
577 578
		if (ret)
			return -EFAULT;
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		input->coff += free;
		if (input->coff == input->dma_buf_size) {
			input->coff = 0;
			input->cbuf = (input->cbuf+1) % input->dma_buf_num;
		}
		left -= free;
		ddbwritel((input->cbuf << 11) | (input->coff >> 7),
			  DMA_BUFFER_ACK(input->nr));
	}
	return count;
}

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

#if 0
static struct ddb_input *fe2input(struct ddb *dev, struct dvb_frontend *fe)
{
	int i;

	for (i = 0; i < dev->info->port_num * 2; i++) {
601
		if (dev->input[i].fe == fe)
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
			return &dev->input[i];
	}
	return NULL;
}
#endif

static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
{
	struct ddb_input *input = fe->sec_priv;
	struct ddb_port *port = input->port;
	int status;

	if (enable) {
		mutex_lock(&port->i2c_gate_lock);
		status = input->gate_ctrl(fe, 1);
	} else {
		status = input->gate_ctrl(fe, 0);
		mutex_unlock(&port->i2c_gate_lock);
	}
	return status;
}

static int demod_attach_drxk(struct ddb_input *input)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
	struct dvb_frontend *fe;
628
	struct drxk_config config;
629
	struct device *dev = &input->port->dev->pdev->dev;
630

631
	memset(&config, 0, sizeof(config));
632
	config.microcode_name = "drxk_a3.mc";
633
	config.qam_demod_parameter_count = 4;
634 635
	config.adr = 0x29 + (input->nr & 1);

636
	fe = input->fe = dvb_attach(drxk_attach, &config, i2c);
637
	if (!input->fe) {
638
		dev_err(dev, "No DRXK found!\n");
639 640 641 642 643 644 645 646 647 648 649 650
		return -ENODEV;
	}
	fe->sec_priv = input;
	input->gate_ctrl = fe->ops.i2c_gate_ctrl;
	fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
	return 0;
}

static int tuner_attach_tda18271(struct ddb_input *input)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
	struct dvb_frontend *fe;
651
	struct device *dev = &input->port->dev->pdev->dev;
652 653 654 655 656

	if (input->fe->ops.i2c_gate_ctrl)
		input->fe->ops.i2c_gate_ctrl(input->fe, 1);
	fe = dvb_attach(tda18271c2dd_attach, input->fe, i2c, 0x60);
	if (!fe) {
657
		dev_err(dev, "No TDA18271 found!\n");
658 659 660 661 662 663 664 665 666 667 668
		return -ENODEV;
	}
	if (input->fe->ops.i2c_gate_ctrl)
		input->fe->ops.i2c_gate_ctrl(input->fe, 0);
	return 0;
}

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
static struct stv0367_config ddb_stv0367_config[] = {
	{
		.demod_address = 0x1f,
		.xtal = 27000000,
		.if_khz = 0,
		.if_iq_mode = FE_TER_NORMAL_IF_TUNER,
		.ts_mode = STV0367_SERIAL_PUNCT_CLOCK,
		.clk_pol = STV0367_CLOCKPOLARITY_DEFAULT,
	}, {
		.demod_address = 0x1e,
		.xtal = 27000000,
		.if_khz = 0,
		.if_iq_mode = FE_TER_NORMAL_IF_TUNER,
		.ts_mode = STV0367_SERIAL_PUNCT_CLOCK,
		.clk_pol = STV0367_CLOCKPOLARITY_DEFAULT,
	},
};

static int demod_attach_stv0367(struct ddb_input *input)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
690
	struct device *dev = &input->port->dev->pdev->dev;
691 692 693 694 695 696

	/* attach frontend */
	input->fe = dvb_attach(stv0367ddb_attach,
		&ddb_stv0367_config[(input->nr & 1)], i2c);

	if (!input->fe) {
697
		dev_err(dev, "stv0367ddb_attach failed (not found?)\n");
698 699 700 701 702 703 704 705 706 707 708 709 710
		return -ENODEV;
	}

	input->fe->sec_priv = input;
	input->gate_ctrl = input->fe->ops.i2c_gate_ctrl;
	input->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;

	return 0;
}

static int tuner_tda18212_ping(struct ddb_input *input, unsigned short adr)
{
	struct i2c_adapter *adapter = &input->port->i2c->adap;
711 712
	struct device *dev = &input->port->dev->pdev->dev;

713 714 715
	u8 tda_id[2];
	u8 subaddr = 0x00;

716
	dev_dbg(dev, "stv0367-tda18212 tuner ping\n");
717 718 719 720
	if (input->fe->ops.i2c_gate_ctrl)
		input->fe->ops.i2c_gate_ctrl(input->fe, 1);

	if (i2c_read_regs(adapter, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
721
		dev_dbg(dev, "tda18212 ping 1 fail\n");
722
	if (i2c_read_regs(adapter, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
723
		dev_warn(dev, "tda18212 ping failed, expect problems\n");
724 725 726 727 728 729 730

	if (input->fe->ops.i2c_gate_ctrl)
		input->fe->ops.i2c_gate_ctrl(input->fe, 0);

	return 0;
}

731 732 733
static int demod_attach_cxd28xx(struct ddb_input *input, int par, int osc24)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
734
	struct device *dev = &input->port->dev->pdev->dev;
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
	struct cxd2841er_config cfg;

	/* the cxd2841er driver expects 8bit/shifted I2C addresses */
	cfg.i2c_addr = ((input->nr & 1) ? 0x6d : 0x6c) << 1;

	cfg.xtal = osc24 ? SONY_XTAL_24000 : SONY_XTAL_20500;
	cfg.flags = CXD2841ER_AUTO_IFHZ | CXD2841ER_EARLY_TUNE |
		CXD2841ER_NO_WAIT_LOCK | CXD2841ER_NO_AGCNEG |
		CXD2841ER_TSBITS;

	if (!par)
		cfg.flags |= CXD2841ER_TS_SERIAL;

	/* attach frontend */
	input->fe = dvb_attach(cxd2841er_attach_t_c, &cfg, i2c);

	if (!input->fe) {
752
		dev_err(dev, "No Sony CXD28xx found!\n");
753 754 755 756 757 758 759 760 761 762
		return -ENODEV;
	}

	input->fe->sec_priv = input;
	input->gate_ctrl = input->fe->ops.i2c_gate_ctrl;
	input->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;

	return 0;
}

763 764 765
static int tuner_attach_tda18212(struct ddb_input *input, u32 porttype)
{
	struct i2c_adapter *adapter = &input->port->i2c->adap;
766
	struct device *dev = &input->port->dev->pdev->dev;
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
	struct i2c_client *client;
	struct tda18212_config config = {
		.fe = input->fe,
		.if_dvbt_6 = 3550,
		.if_dvbt_7 = 3700,
		.if_dvbt_8 = 4150,
		.if_dvbt2_6 = 3250,
		.if_dvbt2_7 = 4000,
		.if_dvbt2_8 = 4000,
		.if_dvbc = 5000,
	};
	struct i2c_board_info board_info = {
		.type = "tda18212",
		.platform_data = &config,
	};

	if (input->nr & 1)
		board_info.addr = 0x63;
	else
		board_info.addr = 0x60;

	/* due to a hardware quirk with the I2C gate on the stv0367+tda18212
	 * combo, the tda18212 must be probed by reading it's id _twice_ when
	 * cold started, or it very likely will fail.
	 */
	if (porttype == DDB_TUNER_DVBCT_ST)
		tuner_tda18212_ping(input, board_info.addr);

	request_module(board_info.type);

	/* perform tuner init/attach */
	client = i2c_new_device(adapter, &board_info);
	if (client == NULL || client->dev.driver == NULL)
		goto err;

	if (!try_module_get(client->dev.driver->owner)) {
		i2c_unregister_device(client);
		goto err;
	}

	input->i2c_client[0] = client;

	return 0;
err:
811
	dev_warn(dev, "TDA18212 tuner not found. Device is not fully operational.\n");
812 813 814 815 816 817 818
	return -ENODEV;
}

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
static struct stv090x_config stv0900 = {
	.device         = STV0900,
	.demod_mode     = STV090x_DUAL,
	.clk_mode       = STV090x_CLK_EXT,

	.xtal           = 27000000,
	.address        = 0x69,

	.ts1_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,
	.ts2_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,

	.repeater_level = STV090x_RPTLEVEL_16,

	.adc1_range	= STV090x_ADC_1Vpp,
	.adc2_range	= STV090x_ADC_1Vpp,

	.diseqc_envelope_mode = true,
};

static struct stv090x_config stv0900_aa = {
	.device         = STV0900,
	.demod_mode     = STV090x_DUAL,
	.clk_mode       = STV090x_CLK_EXT,

	.xtal           = 27000000,
	.address        = 0x68,

	.ts1_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,
	.ts2_mode       = STV090x_TSMODE_SERIAL_PUNCTURED,

	.repeater_level = STV090x_RPTLEVEL_16,

	.adc1_range	= STV090x_ADC_1Vpp,
	.adc2_range	= STV090x_ADC_1Vpp,

	.diseqc_envelope_mode = true,
};

static struct stv6110x_config stv6110a = {
	.addr    = 0x60,
	.refclk	 = 27000000,
	.clk_div = 1,
};

static struct stv6110x_config stv6110b = {
	.addr    = 0x63,
	.refclk	 = 27000000,
	.clk_div = 1,
};

static int demod_attach_stv0900(struct ddb_input *input, int type)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
872
	struct device *dev = &input->port->dev->pdev->dev;
873 874
	struct stv090x_config *feconf = type ? &stv0900_aa : &stv0900;

875 876 877
	input->fe = dvb_attach(stv090x_attach, feconf, i2c,
			       (input->nr & 1) ? STV090x_DEMODULATOR_1
			       : STV090x_DEMODULATOR_0);
878
	if (!input->fe) {
879
		dev_err(dev, "No STV0900 found!\n");
880 881 882 883 884
		return -ENODEV;
	}
	if (!dvb_attach(lnbh24_attach, input->fe, i2c, 0,
			0, (input->nr & 1) ?
			(0x09 - type) : (0x0b - type))) {
885
		dev_err(dev, "No LNBH24 found!\n");
886 887 888 889 890 891 892 893
		return -ENODEV;
	}
	return 0;
}

static int tuner_attach_stv6110(struct ddb_input *input, int type)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
894
	struct device *dev = &input->port->dev->pdev->dev;
895 896 897
	struct stv090x_config *feconf = type ? &stv0900_aa : &stv0900;
	struct stv6110x_config *tunerconf = (input->nr & 1) ?
		&stv6110b : &stv6110a;
898
	const struct stv6110x_devctl *ctl;
899 900 901

	ctl = dvb_attach(stv6110x_attach, input->fe, tunerconf, i2c);
	if (!ctl) {
902
		dev_err(dev, "No STV6110X found!\n");
903 904
		return -ENODEV;
	}
905
	dev_info(dev, "attach tuner input %d adr %02x\n",
906
			 input->nr, tunerconf->addr);
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

	feconf->tuner_init          = ctl->tuner_init;
	feconf->tuner_sleep         = ctl->tuner_sleep;
	feconf->tuner_set_mode      = ctl->tuner_set_mode;
	feconf->tuner_set_frequency = ctl->tuner_set_frequency;
	feconf->tuner_get_frequency = ctl->tuner_get_frequency;
	feconf->tuner_set_bandwidth = ctl->tuner_set_bandwidth;
	feconf->tuner_get_bandwidth = ctl->tuner_get_bandwidth;
	feconf->tuner_set_bbgain    = ctl->tuner_set_bbgain;
	feconf->tuner_get_bbgain    = ctl->tuner_get_bbgain;
	feconf->tuner_set_refclk    = ctl->tuner_set_refclk;
	feconf->tuner_get_status    = ctl->tuner_get_status;

	return 0;
}

923
static int my_dvb_dmx_ts_card_init(struct dvb_demux *dvbdemux, char *id,
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
			    int (*start_feed)(struct dvb_demux_feed *),
			    int (*stop_feed)(struct dvb_demux_feed *),
			    void *priv)
{
	dvbdemux->priv = priv;

	dvbdemux->filternum = 256;
	dvbdemux->feednum = 256;
	dvbdemux->start_feed = start_feed;
	dvbdemux->stop_feed = stop_feed;
	dvbdemux->write_to_decoder = NULL;
	dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
				      DMX_SECTION_FILTERING |
				      DMX_MEMORY_BASED_FILTERING);
	return dvb_dmx_init(dvbdemux);
}

941
static int my_dvb_dmxdev_ts_card_init(struct dmxdev *dmxdev,
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
			       struct dvb_demux *dvbdemux,
			       struct dmx_frontend *hw_frontend,
			       struct dmx_frontend *mem_frontend,
			       struct dvb_adapter *dvb_adapter)
{
	int ret;

	dmxdev->filternum = 256;
	dmxdev->demux = &dvbdemux->dmx;
	dmxdev->capabilities = 0;
	ret = dvb_dmxdev_init(dmxdev, dvb_adapter);
	if (ret < 0)
		return ret;

	hw_frontend->source = DMX_FRONTEND_0;
	dvbdemux->dmx.add_frontend(&dvbdemux->dmx, hw_frontend);
	mem_frontend->source = DMX_MEMORY_FE;
	dvbdemux->dmx.add_frontend(&dvbdemux->dmx, mem_frontend);
	return dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, hw_frontend);
}

static int start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct ddb_input *input = dvbdmx->priv;

	if (!input->users)
		ddb_input_start(input);

	return ++input->users;
}

static int stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct ddb_input *input = dvbdmx->priv;

	if (--input->users)
		return input->users;

	ddb_input_stop(input);
	return 0;
}


static void dvb_input_detach(struct ddb_input *input)
{
	struct dvb_adapter *adap = &input->adap;
	struct dvb_demux *dvbdemux = &input->demux;
991
	struct i2c_client *client;
992 993 994

	switch (input->attached) {
	case 5:
995 996 997 998 999 1000
		client = input->i2c_client[0];
		if (client) {
			module_put(client->dev.driver->owner);
			i2c_unregister_device(client);
		}
		if (input->fe2) {
1001
			dvb_unregister_frontend(input->fe2);
1002 1003
			input->fe2 = NULL;
		}
1004 1005 1006 1007 1008
		if (input->fe) {
			dvb_unregister_frontend(input->fe);
			dvb_frontend_detach(input->fe);
			input->fe = NULL;
		}
1009
		/* fall-through */
1010 1011
	case 4:
		dvb_net_release(&input->dvbnet);
1012
		/* fall-through */
1013 1014 1015 1016 1017 1018 1019
	case 3:
		dvbdemux->dmx.close(&dvbdemux->dmx);
		dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
					      &input->hw_frontend);
		dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
					      &input->mem_frontend);
		dvb_dmxdev_release(&input->dmxdev);
1020
		/* fall-through */
1021 1022
	case 2:
		dvb_dmx_release(&input->demux);
1023
		/* fall-through */
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	case 1:
		dvb_unregister_adapter(adap);
	}
	input->attached = 0;
}

static int dvb_input_attach(struct ddb_input *input)
{
	int ret;
	struct ddb_port *port = input->port;
	struct dvb_adapter *adap = &input->adap;
	struct dvb_demux *dvbdemux = &input->demux;
1036
	struct device *dev = &input->port->dev->pdev->dev;
1037
	int sony_osc24 = 0, sony_tspar = 0;
1038

1039
	ret = dvb_register_adapter(adap, "DDBridge", THIS_MODULE,
1040 1041 1042
				   &input->port->dev->pdev->dev,
				   adapter_nr);
	if (ret < 0) {
1043
		dev_err(dev, "Could not register adapter. Check if you enabled enough adapters in dvb-core!\n");
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		return ret;
	}
	input->attached = 1;

	ret = my_dvb_dmx_ts_card_init(dvbdemux, "SW demux",
				      start_feed,
				      stop_feed, input);
	if (ret < 0)
		return ret;
	input->attached = 2;

	ret = my_dvb_dmxdev_ts_card_init(&input->dmxdev, &input->demux,
					 &input->hw_frontend,
					 &input->mem_frontend, adap);
	if (ret < 0)
		return ret;
	input->attached = 3;

	ret = dvb_net_init(adap, &input->dvbnet, input->dmxdev.demux);
	if (ret < 0)
		return ret;
	input->attached = 4;

1067
	input->fe = NULL;
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
	switch (port->type) {
	case DDB_TUNER_DVBS_ST:
		if (demod_attach_stv0900(input, 0) < 0)
			return -ENODEV;
		if (tuner_attach_stv6110(input, 0) < 0)
			return -ENODEV;
		if (input->fe) {
			if (dvb_register_frontend(adap, input->fe) < 0)
				return -ENODEV;
		}
		break;
	case DDB_TUNER_DVBS_ST_AA:
		if (demod_attach_stv0900(input, 1) < 0)
			return -ENODEV;
		if (tuner_attach_stv6110(input, 1) < 0)
			return -ENODEV;
		if (input->fe) {
			if (dvb_register_frontend(adap, input->fe) < 0)
				return -ENODEV;
		}
		break;
	case DDB_TUNER_DVBCT_TR:
		if (demod_attach_drxk(input) < 0)
			return -ENODEV;
		if (tuner_attach_tda18271(input) < 0)
			return -ENODEV;
1094 1095
		if (dvb_register_frontend(adap, input->fe) < 0)
			return -ENODEV;
1096 1097 1098
		if (input->fe2) {
			if (dvb_register_frontend(adap, input->fe2) < 0)
				return -ENODEV;
1099
			input->fe2->tuner_priv = input->fe->tuner_priv;
1100 1101 1102 1103 1104
			memcpy(&input->fe2->ops.tuner_ops,
			       &input->fe->ops.tuner_ops,
			       sizeof(struct dvb_tuner_ops));
		}
		break;
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	case DDB_TUNER_DVBCT_ST:
		if (demod_attach_stv0367(input) < 0)
			return -ENODEV;
		if (tuner_attach_tda18212(input, port->type) < 0)
			return -ENODEV;
		if (input->fe) {
			if (dvb_register_frontend(adap, input->fe) < 0)
				return -ENODEV;
		}
		break;
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
	case DDB_TUNER_DVBC2T2I_SONY_P:
	case DDB_TUNER_DVBCT2_SONY_P:
	case DDB_TUNER_DVBC2T2_SONY_P:
	case DDB_TUNER_ISDBT_SONY_P:
		if (port->type == DDB_TUNER_DVBC2T2I_SONY_P)
			sony_osc24 = 1;
		if (input->port->dev->info->ts_quirks & TS_QUIRK_ALT_OSC)
			sony_osc24 = 0;
		if (input->port->dev->info->ts_quirks & TS_QUIRK_SERIAL)
			sony_tspar = 0;
		else
			sony_tspar = 1;

		if (demod_attach_cxd28xx(input, sony_tspar, sony_osc24) < 0)
			return -ENODEV;
		if (tuner_attach_tda18212(input, port->type) < 0)
			return -ENODEV;
		if (input->fe) {
			if (dvb_register_frontend(adap, input->fe) < 0)
				return -ENODEV;
		}
		break;
	case DDB_TUNER_XO2_DVBC2T2I_SONY:
	case DDB_TUNER_XO2_DVBCT2_SONY:
	case DDB_TUNER_XO2_DVBC2T2_SONY:
	case DDB_TUNER_XO2_ISDBT_SONY:
		if (port->type == DDB_TUNER_XO2_DVBC2T2I_SONY)
			sony_osc24 = 1;

		if (demod_attach_cxd28xx(input, 0, sony_osc24) < 0)
			return -ENODEV;
		if (tuner_attach_tda18212(input, port->type) < 0)
			return -ENODEV;
		if (input->fe) {
			if (dvb_register_frontend(adap, input->fe) < 0)
				return -ENODEV;
		}
		break;
1153
	}
1154

1155 1156 1157 1158 1159 1160 1161
	input->attached = 5;
	return 0;
}

/****************************************************************************/
/****************************************************************************/

1162
static ssize_t ts_write(struct file *file, const __user char *buf,
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
			size_t count, loff_t *ppos)
{
	struct dvb_device *dvbdev = file->private_data;
	struct ddb_output *output = dvbdev->priv;
	size_t left = count;
	int stat;

	while (left) {
		if (ddb_output_free(output) < 188) {
			if (file->f_flags & O_NONBLOCK)
				break;
			if (wait_event_interruptible(
				    output->wq, ddb_output_free(output) >= 188) < 0)
				break;
		}
		stat = ddb_output_write(output, buf, left);
		if (stat < 0)
			break;
		buf += stat;
		left -= stat;
	}
	return (left == count) ? -EAGAIN : (count - left);
}

1187
static ssize_t ts_read(struct file *file, __user char *buf,
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		       size_t count, loff_t *ppos)
{
	struct dvb_device *dvbdev = file->private_data;
	struct ddb_output *output = dvbdev->priv;
	struct ddb_input *input = output->port->input[0];
	int left, read;

	count -= count % 188;
	left = count;
	while (left) {
		if (ddb_input_avail(input) < 188) {
			if (file->f_flags & O_NONBLOCK)
				break;
			if (wait_event_interruptible(
				    input->wq, ddb_input_avail(input) >= 188) < 0)
				break;
		}
		read = ddb_input_read(input, buf, left);
1206 1207
		if (read < 0)
			return read;
1208 1209 1210 1211 1212 1213 1214 1215
		left -= read;
		buf += read;
	}
	return (left == count) ? -EAGAIN : (count - left);
}

static unsigned int ts_poll(struct file *file, poll_table *wait)
{
1216
	/*
1217 1218 1219
	struct dvb_device *dvbdev = file->private_data;
	struct ddb_output *output = dvbdev->priv;
	struct ddb_input *input = output->port->input[0];
1220
	*/
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	unsigned int mask = 0;

#if 0
	if (data_avail_to_read)
		mask |= POLLIN | POLLRDNORM;
	if (data_avail_to_write)
		mask |= POLLOUT | POLLWRNORM;

	poll_wait(file, &read_queue, wait);
	poll_wait(file, &write_queue, wait);
#endif
	return mask;
}

1235
static const struct file_operations ci_fops = {
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	.owner   = THIS_MODULE,
	.read    = ts_read,
	.write   = ts_write,
	.open    = dvb_generic_open,
	.release = dvb_generic_release,
	.poll    = ts_poll,
};

static struct dvb_device dvbdev_ci = {
	.readers = -1,
	.writers = -1,
	.users   = -1,
	.fops    = &ci_fops,
};

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

static void input_tasklet(unsigned long data)
{
	struct ddb_input *input = (struct ddb_input *) data;
	struct ddb *dev = input->port->dev;

	spin_lock(&input->lock);
	if (!input->running) {
		spin_unlock(&input->lock);
		return;
	}
	input->stat = ddbreadl(DMA_BUFFER_CURRENT(input->nr));

	if (input->port->class == DDB_PORT_TUNER) {
		if (4&ddbreadl(DMA_BUFFER_CONTROL(input->nr)))
1269
			dev_err(&dev->pdev->dev, "Overflow input %d\n", input->nr);
1270
		while (input->cbuf != ((input->stat >> 11) & 0x1f)
1271
		       || (4 & safe_ddbreadl(dev, DMA_BUFFER_CONTROL(input->nr)))) {
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
			dvb_dmx_swfilter_packets(&input->demux,
						 input->vbuf[input->cbuf],
						 input->dma_buf_size / 188);

			input->cbuf = (input->cbuf + 1) % input->dma_buf_num;
			ddbwritel((input->cbuf << 11),
				  DMA_BUFFER_ACK(input->nr));
			input->stat = ddbreadl(DMA_BUFFER_CURRENT(input->nr));
		       }
	}
	if (input->port->class == DDB_PORT_CI)
		wake_up(&input->wq);
	spin_unlock(&input->lock);
}

static void output_tasklet(unsigned long data)
{
	struct ddb_output *output = (struct ddb_output *) data;
	struct ddb *dev = output->port->dev;

	spin_lock(&output->lock);
	if (!output->running) {
		spin_unlock(&output->lock);
		return;
	}
	output->stat = ddbreadl(DMA_BUFFER_CURRENT(output->nr + 8));
	wake_up(&output->wq);
	spin_unlock(&output->lock);
}


1303
static struct cxd2099_cfg cxd_cfg = {
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	.bitrate =  62000,
	.adr     =  0x40,
	.polarity = 1,
	.clock_mode = 1,
};

static int ddb_ci_attach(struct ddb_port *port)
{
	int ret;

	ret = dvb_register_adapter(&port->output->adap,
				   "DDBridge",
				   THIS_MODULE,
				   &port->dev->pdev->dev,
				   adapter_nr);
	if (ret < 0)
		return ret;
	port->en = cxd2099_attach(&cxd_cfg, port, &port->i2c->adap);
	if (!port->en) {
		dvb_unregister_adapter(&port->output->adap);
		return -ENODEV;
	}
	ddb_input_start(port->input[0]);
	ddb_output_start(port->output);
	dvb_ca_en50221_init(&port->output->adap,
			    port->en, 0, 1);
1330 1331
	ret = dvb_register_device(&port->output->adap, &port->output->dev,
				  &dvbdev_ci, (void *) port->output,
1332
				  DVB_DEVICE_SEC, 0);
1333 1334 1335 1336 1337
	return ret;
}

static int ddb_port_attach(struct ddb_port *port)
{
1338
	struct device *dev = &port->dev->pdev->dev;
1339 1340 1341 1342 1343
	int ret = 0;

	switch (port->class) {
	case DDB_PORT_TUNER:
		ret = dvb_input_attach(port->input[0]);
1344
		if (ret < 0)
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
			break;
		ret = dvb_input_attach(port->input[1]);
		break;
	case DDB_PORT_CI:
		ret = ddb_ci_attach(port);
		break;
	default:
		break;
	}
	if (ret < 0)
1355
		dev_err(dev, "port_attach on port %d failed\n", port->nr);
1356 1357 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
	return ret;
}

static int ddb_ports_attach(struct ddb *dev)
{
	int i, ret = 0;
	struct ddb_port *port;

	for (i = 0; i < dev->info->port_num; i++) {
		port = &dev->port[i];
		ret = ddb_port_attach(port);
		if (ret < 0)
			break;
	}
	return ret;
}

static void ddb_ports_detach(struct ddb *dev)
{
	int i;
	struct ddb_port *port;

	for (i = 0; i < dev->info->port_num; i++) {
		port = &dev->port[i];
		switch (port->class) {
		case DDB_PORT_TUNER:
			dvb_input_detach(port->input[0]);
			dvb_input_detach(port->input[1]);
			break;
		case DDB_PORT_CI:
1386
			dvb_unregister_device(port->output->dev);
1387 1388 1389 1390 1391
			if (port->en) {
				ddb_input_stop(port->input[0]);
				ddb_output_stop(port->output);
				dvb_ca_en50221_release(port->en);
				kfree(port->en);
1392
				port->en = NULL;
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
				dvb_unregister_adapter(&port->output->adap);
			}
			break;
		}
	}
}

/****************************************************************************/
/****************************************************************************/

1403 1404 1405
static int init_xo2(struct ddb_port *port)
{
	struct i2c_adapter *i2c = &port->i2c->adap;
1406
	struct device *dev = &port->dev->pdev->dev;
1407 1408 1409 1410 1411 1412 1413 1414
	u8 val, data[2];
	int res;

	res = i2c_read_regs(i2c, 0x10, 0x04, data, 2);
	if (res < 0)
		return res;

	if (data[0] != 0x01)  {
1415
		dev_info(dev, "Port %d: invalid XO2\n", port->nr);
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
		return -1;
	}

	i2c_read_reg(i2c, 0x10, 0x08, &val);
	if (val != 0) {
		i2c_write_reg(i2c, 0x10, 0x08, 0x00);
		msleep(100);
	}
	/* Enable tuner power, disable pll, reset demods */
	i2c_write_reg(i2c, 0x10, 0x08, 0x04);
	usleep_range(2000, 3000);
	/* Release demod resets */
	i2c_write_reg(i2c, 0x10, 0x08, 0x07);

	/* speed: 0=55,1=75,2=90,3=104 MBit/s */
	i2c_write_reg(i2c, 0x10, 0x09,
		((xo2_speed >= 0 && xo2_speed <= 3) ? xo2_speed : 2));

	i2c_write_reg(i2c, 0x10, 0x0a, 0x01);
	i2c_write_reg(i2c, 0x10, 0x0b, 0x01);

	usleep_range(2000, 3000);
	/* Start XO2 PLL */
	i2c_write_reg(i2c, 0x10, 0x08, 0x87);

	return 0;
}

static int port_has_xo2(struct ddb_port *port, u8 *type, u8 *id)
{
	u8 probe[1] = { 0x00 }, data[4];

	*type = DDB_XO2_TYPE_NONE;

	if (i2c_io(&port->i2c->adap, 0x10, probe, 1, data, 4))
		return 0;
	if (data[0] == 'D' && data[1] == 'F') {
		*id = data[2];
		*type = DDB_XO2_TYPE_DUOFLEX;
		return 1;
	}
	if (data[0] == 'C' && data[1] == 'I') {
		*id = data[2];
		*type = DDB_XO2_TYPE_CI;
		return 1;
	}
	return 0;
}

/****************************************************************************/
/****************************************************************************/

1468 1469 1470
static int port_has_ci(struct ddb_port *port)
{
	u8 val;
1471
	return i2c_read_reg(&port->i2c->adap, 0x40, 0, &val) ? 0 : 1;
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
}

static int port_has_stv0900(struct ddb_port *port)
{
	u8 val;
	if (i2c_read_reg16(&port->i2c->adap, 0x69, 0xf100, &val) < 0)
		return 0;
	return 1;
}

static int port_has_stv0900_aa(struct ddb_port *port)
{
	u8 val;
	if (i2c_read_reg16(&port->i2c->adap, 0x68, 0xf100, &val) < 0)
		return 0;
	return 1;
}

static int port_has_drxks(struct ddb_port *port)
{
	u8 val;
	if (i2c_read(&port->i2c->adap, 0x29, &val) < 0)
		return 0;
	if (i2c_read(&port->i2c->adap, 0x2a, &val) < 0)
		return 0;
	return 1;
}

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
static int port_has_stv0367(struct ddb_port *port)
{
	u8 val;
	if (i2c_read_reg16(&port->i2c->adap, 0x1e, 0xf000, &val) < 0)
		return 0;
	if (val != 0x60)
		return 0;
	if (i2c_read_reg16(&port->i2c->adap, 0x1f, 0xf000, &val) < 0)
		return 0;
	if (val != 0x60)
		return 0;
	return 1;
}

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
static int port_has_cxd28xx(struct ddb_port *port, u8 *id)
{
	struct i2c_adapter *i2c = &port->i2c->adap;
	int status;

	status = i2c_write_reg(&port->i2c->adap, 0x6e, 0, 0);
	if (status)
		return 0;
	status = i2c_read_reg(i2c, 0x6e, 0xfd, id);
	if (status)
		return 0;
	return 1;
}

1528 1529 1530 1531
static void ddb_port_probe(struct ddb_port *port)
{
	struct ddb *dev = port->dev;
	char *modname = "NO MODULE";
1532
	u8 xo2_type, xo2_id, cxd_id;
1533 1534 1535 1536 1537 1538 1539

	port->class = DDB_PORT_NONE;

	if (port_has_ci(port)) {
		modname = "CI";
		port->class = DDB_PORT_CI;
		ddbwritel(I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
1540
	} else if (port_has_xo2(port, &xo2_type, &xo2_id)) {
1541
		dev_dbg(&dev->pdev->dev, "Port %d (TAB %d): XO2 type: %d, id: %d\n",
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
			port->nr, port->nr+1, xo2_type, xo2_id);

		ddbwritel(I2C_SPEED_400, port->i2c->regs + I2C_TIMING);

		switch (xo2_type) {
		case DDB_XO2_TYPE_DUOFLEX:
			init_xo2(port);
			switch (xo2_id >> 2) {
			case 0:
				modname = "DUAL DVB-S2 (unsupported)";
				port->class = DDB_PORT_NONE;
				port->type = DDB_TUNER_XO2_DVBS_STV0910;
				break;
			case 1:
1556 1557
				modname = "DUAL DVB-C/T/T2";
				port->class = DDB_PORT_TUNER;
1558 1559 1560
				port->type = DDB_TUNER_XO2_DVBCT2_SONY;
				break;
			case 2:
1561 1562
				modname = "DUAL DVB-ISDBT";
				port->class = DDB_PORT_TUNER;
1563 1564 1565
				port->type = DDB_TUNER_XO2_ISDBT_SONY;
				break;
			case 3:
1566 1567
				modname = "DUAL DVB-C/C2/T/T2";
				port->class = DDB_PORT_TUNER;
1568 1569 1570 1571 1572 1573 1574 1575
				port->type = DDB_TUNER_XO2_DVBC2T2_SONY;
				break;
			case 4:
				modname = "DUAL ATSC (unsupported)";
				port->class = DDB_PORT_NONE;
				port->type = DDB_TUNER_XO2_ATSC_ST;
				break;
			case 5:
1576 1577
				modname = "DUAL DVB-C/C2/T/T2/ISDBT";
				port->class = DDB_PORT_TUNER;
1578 1579 1580 1581 1582 1583 1584 1585
				port->type = DDB_TUNER_XO2_DVBC2T2I_SONY;
				break;
			default:
				modname = "Unknown XO2 DuoFlex module\n";
				break;
			}
			break;
		case DDB_XO2_TYPE_CI:
1586
			dev_info(&dev->pdev->dev, "DuoFlex CI modules not supported\n");
1587 1588
			break;
		default:
1589
			dev_info(&dev->pdev->dev, "Unknown XO2 DuoFlex module\n");
1590 1591
			break;
		}
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
	} else if (port_has_cxd28xx(port, &cxd_id)) {
		switch (cxd_id) {
		case 0xa4:
			modname = "DUAL DVB-C2T2 CXD2843";
			port->class = DDB_PORT_TUNER;
			port->type = DDB_TUNER_DVBC2T2_SONY_P;
			break;
		case 0xb1:
			modname = "DUAL DVB-CT2 CXD2837";
			port->class = DDB_PORT_TUNER;
			port->type = DDB_TUNER_DVBCT2_SONY_P;
			break;
		case 0xb0:
			modname = "DUAL ISDB-T CXD2838";
			port->class = DDB_PORT_TUNER;
			port->type = DDB_TUNER_ISDBT_SONY_P;
			break;
		case 0xc1:
			modname = "DUAL DVB-C2T2 ISDB-T CXD2854";
			port->class = DDB_PORT_TUNER;
			port->type = DDB_TUNER_DVBC2T2I_SONY_P;
			break;
		default:
			modname = "Unknown CXD28xx tuner";
			break;
		}
		ddbwritel(I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
	} else if (port_has_stv0900(port)) {
		modname = "DUAL DVB-S2";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBS_ST;
		ddbwritel(I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
	} else if (port_has_stv0900_aa(port)) {
		modname = "DUAL DVB-S2";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBS_ST_AA;
		ddbwritel(I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
	} else if (port_has_drxks(port)) {
		modname = "DUAL DVB-C/T";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBCT_TR;
		ddbwritel(I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
1634 1635 1636 1637 1638
	} else if (port_has_stv0367(port)) {
		modname = "DUAL DVB-C/T";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBCT_ST;
		ddbwritel(I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
1639
	}
1640

1641
	dev_info(&dev->pdev->dev, "Port %d (TAB %d): %s\n",
1642
			 port->nr, port->nr+1, modname);
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 1698 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
}

static void ddb_input_init(struct ddb_port *port, int nr)
{
	struct ddb *dev = port->dev;
	struct ddb_input *input = &dev->input[nr];

	input->nr = nr;
	input->port = port;
	input->dma_buf_num = INPUT_DMA_BUFS;
	input->dma_buf_size = INPUT_DMA_SIZE;
	ddbwritel(0, TS_INPUT_CONTROL(nr));
	ddbwritel(2, TS_INPUT_CONTROL(nr));
	ddbwritel(0, TS_INPUT_CONTROL(nr));
	ddbwritel(0, DMA_BUFFER_ACK(nr));
	tasklet_init(&input->tasklet, input_tasklet, (unsigned long) input);
	spin_lock_init(&input->lock);
	init_waitqueue_head(&input->wq);
}

static void ddb_output_init(struct ddb_port *port, int nr)
{
	struct ddb *dev = port->dev;
	struct ddb_output *output = &dev->output[nr];
	output->nr = nr;
	output->port = port;
	output->dma_buf_num = OUTPUT_DMA_BUFS;
	output->dma_buf_size = OUTPUT_DMA_SIZE;

	ddbwritel(0, TS_OUTPUT_CONTROL(nr));
	ddbwritel(2, TS_OUTPUT_CONTROL(nr));
	ddbwritel(0, TS_OUTPUT_CONTROL(nr));
	tasklet_init(&output->tasklet, output_tasklet, (unsigned long) output);
	init_waitqueue_head(&output->wq);
}

static void ddb_ports_init(struct ddb *dev)
{
	int i;
	struct ddb_port *port;

	for (i = 0; i < dev->info->port_num; i++) {
		port = &dev->port[i];
		port->dev = dev;
		port->nr = i;
		port->i2c = &dev->i2c[i];
		port->input[0] = &dev->input[2 * i];
		port->input[1] = &dev->input[2 * i + 1];
		port->output = &dev->output[i];

		mutex_init(&port->i2c_gate_lock);
		ddb_port_probe(port);
		ddb_input_init(port, 2 * i);
		ddb_input_init(port, 2 * i + 1);
		ddb_output_init(port, i);
	}
}

static void ddb_ports_release(struct ddb *dev)
{
	int i;
	struct ddb_port *port;

	for (i = 0; i < dev->info->port_num; i++) {
		port = &dev->port[i];
		port->dev = dev;
		tasklet_kill(&port->input[0]->tasklet);
		tasklet_kill(&port->input[1]->tasklet);
		tasklet_kill(&port->output->tasklet);
	}
}

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

static void irq_handle_i2c(struct ddb *dev, int n)
{
	struct ddb_i2c *i2c = &dev->i2c[n];

	i2c->done = 1;
	wake_up(&i2c->wq);
}

static irqreturn_t irq_handler(int irq, void *dev_id)
{
	struct ddb *dev = (struct ddb *) dev_id;
	u32 s = ddbreadl(INTERRUPT_STATUS);

	if (!s)
		return IRQ_NONE;

	do {
		ddbwritel(s, INTERRUPT_ACK);

1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
		if (s & 0x00000001)
			irq_handle_i2c(dev, 0);
		if (s & 0x00000002)
			irq_handle_i2c(dev, 1);
		if (s & 0x00000004)
			irq_handle_i2c(dev, 2);
		if (s & 0x00000008)
			irq_handle_i2c(dev, 3);

		if (s & 0x00000100)
			tasklet_schedule(&dev->input[0].tasklet);
		if (s & 0x00000200)
			tasklet_schedule(&dev->input[1].tasklet);
		if (s & 0x00000400)
			tasklet_schedule(&dev->input[2].tasklet);
		if (s & 0x00000800)
			tasklet_schedule(&dev->input[3].tasklet);
		if (s & 0x00001000)
			tasklet_schedule(&dev->input[4].tasklet);
		if (s & 0x00002000)
			tasklet_schedule(&dev->input[5].tasklet);
		if (s & 0x00004000)
			tasklet_schedule(&dev->input[6].tasklet);
		if (s & 0x00008000)
			tasklet_schedule(&dev->input[7].tasklet);

		if (s & 0x00010000)
			tasklet_schedule(&dev->output[0].tasklet);
		if (s & 0x00020000)
			tasklet_schedule(&dev->output[1].tasklet);
		if (s & 0x00040000)
			tasklet_schedule(&dev->output[2].tasklet);
		if (s & 0x00080000)
			tasklet_schedule(&dev->output[3].tasklet);

		/* if (s & 0x000f0000)	printk(KERN_DEBUG "%08x\n", istat); */
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
	} while ((s = ddbreadl(INTERRUPT_STATUS)));

	return IRQ_HANDLED;
}

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

static int flashio(struct ddb *dev, u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen)
{
	u32 data, shift;

	if (wlen > 4)
		ddbwritel(1, SPI_CONTROL);
	while (wlen > 4) {
		/* FIXME: check for big-endian */
		data = swab32(*(u32 *)wbuf);
		wbuf += 4;
		wlen -= 4;
		ddbwritel(data, SPI_DATA);
1795
		while (safe_ddbreadl(dev, SPI_CONTROL) & 0x0004)
1796
			;
1797 1798 1799 1800 1801 1802 1803
	}

	if (rlen)
		ddbwritel(0x0001 | ((wlen << (8 + 3)) & 0x1f00), SPI_CONTROL);
	else
		ddbwritel(0x0003 | ((wlen << (8 + 3)) & 0x1f00), SPI_CONTROL);

1804
	data = 0;
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
	shift = ((4 - wlen) * 8);
	while (wlen) {
		data <<= 8;
		data |= *wbuf;
		wlen--;
		wbuf++;
	}
	if (shift)
		data <<= shift;
	ddbwritel(data, SPI_DATA);
1815
	while (safe_ddbreadl(dev, SPI_CONTROL) & 0x0004)
1816
		;
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826

	if (!rlen) {
		ddbwritel(0, SPI_CONTROL);
		return 0;
	}
	if (rlen > 4)
		ddbwritel(1, SPI_CONTROL);

	while (rlen > 4) {
		ddbwritel(0xffffffff, SPI_DATA);
1827
		while (safe_ddbreadl(dev, SPI_CONTROL) & 0x0004)
1828
			;
1829 1830 1831 1832 1833 1834 1835
		data = ddbreadl(SPI_DATA);
		*(u32 *) rbuf = swab32(data);
		rbuf += 4;
		rlen -= 4;
	}
	ddbwritel(0x0003 | ((rlen << (8 + 3)) & 0x1F00), SPI_CONTROL);
	ddbwritel(0xffffffff, SPI_DATA);
1836
	while (safe_ddbreadl(dev, SPI_CONTROL) & 0x0004)
1837
		;
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856

	data = ddbreadl(SPI_DATA);
	ddbwritel(0, SPI_CONTROL);

	if (rlen < 4)
		data <<= ((4 - rlen) * 8);

	while (rlen > 0) {
		*rbuf = ((data >> 24) & 0xff);
		data <<= 8;
		rbuf++;
		rlen--;
	}
	return 0;
}

#define DDB_MAGIC 'd'

struct ddb_flashio {
1857
	__user __u8 *write_buf;
1858
	__u32 write_len;
1859
	__user __u8 *read_buf;
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
	__u32 read_len;
};

#define IOCTL_DDB_FLASHIO  _IOWR(DDB_MAGIC, 0x00, struct ddb_flashio)

#define DDB_NAME "ddbridge"

static u32 ddb_num;
static struct ddb *ddbs[32];
static struct class *ddb_class;
static int ddb_major;

static int ddb_open(struct inode *inode, struct file *file)
{
	struct ddb *dev = ddbs[iminor(inode)];

	file->private_data = dev;
	return 0;
}

static long ddb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct ddb *dev = file->private_data;
1883
	__user void *parg = (__user void *)arg;
1884
	int res;
1885 1886 1887 1888 1889 1890 1891 1892

	switch (cmd) {
	case IOCTL_DDB_FLASHIO:
	{
		struct ddb_flashio fio;
		u8 *rbuf, *wbuf;

		if (copy_from_user(&fio, parg, sizeof(fio)))
1893 1894 1895 1896 1897 1898 1899
			return -EFAULT;

		if (fio.write_len > 1028 || fio.read_len > 1028)
			return -EINVAL;
		if (fio.write_len + fio.read_len > 1028)
			return -EINVAL;

1900 1901
		wbuf = &dev->iobuf[0];
		rbuf = wbuf + fio.write_len;
1902 1903 1904 1905 1906 1907

		if (copy_from_user(wbuf, fio.write_buf, fio.write_len))
			return -EFAULT;
		res = flashio(dev, wbuf, fio.write_len, rbuf, fio.read_len);
		if (res)
			return res;
1908
		if (copy_to_user(fio.read_buf, rbuf, fio.read_len))
1909
			return -EFAULT;
1910 1911 1912
		break;
	}
	default:
1913
		return -ENOTTY;
1914
	}
1915
	return 0;
1916 1917
}

1918
static const struct file_operations ddb_fops = {
1919 1920 1921 1922
	.unlocked_ioctl = ddb_ioctl,
	.open           = ddb_open,
};

1923
static char *ddb_devnode(struct device *device, umode_t *mode)
1924 1925 1926 1927 1928 1929 1930 1931
{
	struct ddb *dev = dev_get_drvdata(device);

	return kasprintf(GFP_KERNEL, "ddbridge/card%d", dev->nr);
}

static int ddb_class_create(void)
{
1932 1933
	ddb_major = register_chrdev(0, DDB_NAME, &ddb_fops);
	if (ddb_major < 0)
1934 1935 1936 1937 1938
		return ddb_major;

	ddb_class = class_create(THIS_MODULE, DDB_NAME);
	if (IS_ERR(ddb_class)) {
		unregister_chrdev(ddb_major, DDB_NAME);
1939
		return PTR_ERR(ddb_class);
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
	}
	ddb_class->devnode = ddb_devnode;
	return 0;
}

static void ddb_class_destroy(void)
{
	class_destroy(ddb_class);
	unregister_chrdev(ddb_major, DDB_NAME);
}

static int ddb_device_create(struct ddb *dev)
{
	dev->nr = ddb_num++;
	dev->ddb_dev = device_create(ddb_class, NULL,
				     MKDEV(ddb_major, dev->nr),
				     dev, "ddbridge%d", dev->nr);
	ddbs[dev->nr] = dev;
	if (IS_ERR(dev->ddb_dev))
		return -1;
	return 0;
}

static void ddb_device_destroy(struct ddb *dev)
{
	ddb_num--;
	if (IS_ERR(dev->ddb_dev))
		return;
	device_destroy(ddb_class, MKDEV(ddb_major, 0));
}


/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

static void ddb_unmap(struct ddb *dev)
{
	if (dev->regs)
		iounmap(dev->regs);
	vfree(dev);
}


1984
static void ddb_remove(struct pci_dev *pdev)
1985
{
1986
	struct ddb *dev = pci_get_drvdata(pdev);
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001

	ddb_ports_detach(dev);
	ddb_i2c_release(dev);

	ddbwritel(0, INTERRUPT_ENABLE);
	free_irq(dev->pdev->irq, dev);
#ifdef CONFIG_PCI_MSI
	if (dev->msi)
		pci_disable_msi(dev->pdev);
#endif
	ddb_ports_release(dev);
	ddb_buffers_free(dev);
	ddb_device_destroy(dev);

	ddb_unmap(dev);
2002
	pci_set_drvdata(pdev, NULL);
2003 2004 2005 2006
	pci_disable_device(pdev);
}


2007
static int ddb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2008 2009
{
	struct ddb *dev;
2010
	int stat = 0;
2011 2012
	int irq_flag = IRQF_SHARED;

2013
	if (pci_enable_device(pdev) < 0)
2014 2015
		return -ENODEV;

2016
	dev = vzalloc(sizeof(struct ddb));
2017 2018 2019 2020 2021 2022
	if (dev == NULL)
		return -ENOMEM;

	dev->pdev = pdev;
	pci_set_drvdata(pdev, dev);
	dev->info = (struct ddb_info *) id->driver_data;
2023
	dev_info(&pdev->dev, "Detected %s\n", dev->info->name);
2024

2025 2026
	dev->regs = ioremap(pci_resource_start(dev->pdev, 0),
			    pci_resource_len(dev->pdev, 0));
2027 2028 2029 2030
	if (!dev->regs) {
		stat = -ENOMEM;
		goto fail;
	}
2031
	dev_info(&pdev->dev, "HW %08x FW %08x\n", ddbreadl(0), ddbreadl(4));
2032 2033 2034 2035 2036

#ifdef CONFIG_PCI_MSI
	if (pci_msi_enabled())
		stat = pci_enable_msi(dev->pdev);
	if (stat) {
2037
		dev_info(&pdev->dev, "MSI not available.\n");
2038 2039 2040 2041 2042
	} else {
		irq_flag = 0;
		dev->msi = 1;
	}
#endif
2043 2044 2045
	stat = request_irq(dev->pdev->irq, irq_handler,
			   irq_flag, "DDBridge", (void *) dev);
	if (stat < 0)
2046 2047 2048 2049 2050 2051 2052
		goto fail1;
	ddbwritel(0, DMA_BASE_WRITE);
	ddbwritel(0, DMA_BASE_READ);
	ddbwritel(0xffffffff, INTERRUPT_ACK);
	ddbwritel(0xfff0f, INTERRUPT_ENABLE);
	ddbwritel(0, MSI1_ENABLE);

2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
	/* board control */
	if (dev->info->board_control) {
		ddbwritel(0, DDB_LINK_TAG(0) | BOARD_CONTROL);
		msleep(100);
		ddbwritel(dev->info->board_control_2,
			DDB_LINK_TAG(0) | BOARD_CONTROL);
		usleep_range(2000, 3000);
		ddbwritel(dev->info->board_control_2
			| dev->info->board_control,
			DDB_LINK_TAG(0) | BOARD_CONTROL);
		usleep_range(2000, 3000);
	}

2066 2067 2068 2069
	if (ddb_i2c_init(dev) < 0)
		goto fail1;
	ddb_ports_init(dev);
	if (ddb_buffers_alloc(dev) < 0) {
2070
		dev_err(&pdev->dev, "Could not allocate buffer memory\n");
2071 2072 2073 2074 2075 2076 2077 2078 2079
		goto fail2;
	}
	if (ddb_ports_attach(dev) < 0)
		goto fail3;
	ddb_device_create(dev);
	return 0;

fail3:
	ddb_ports_detach(dev);
2080
	dev_err(&pdev->dev, "fail3\n");
2081 2082
	ddb_ports_release(dev);
fail2:
2083
	dev_err(&pdev->dev, "fail2\n");
2084 2085
	ddb_buffers_free(dev);
fail1:
2086
	dev_err(&pdev->dev, "fail1\n");
2087 2088
	if (dev->msi)
		pci_disable_msi(dev->pdev);
2089 2090
	if (stat == 0)
		free_irq(dev->pdev->irq, dev);
2091
fail:
2092
	dev_err(&pdev->dev, "fail\n");
2093
	ddb_unmap(dev);
2094
	pci_set_drvdata(pdev, NULL);
2095 2096 2097 2098 2099 2100 2101 2102
	pci_disable_device(pdev);
	return -1;
}

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/

J
Julia Lawall 已提交
2103
static const struct ddb_info ddb_none = {
2104 2105 2106 2107
	.type     = DDB_NONE,
	.name     = "Digital Devices PCIe bridge",
};

J
Julia Lawall 已提交
2108
static const struct ddb_info ddb_octopus = {
2109 2110 2111 2112 2113
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Octopus DVB adapter",
	.port_num = 4,
};

J
Julia Lawall 已提交
2114
static const struct ddb_info ddb_octopus_le = {
2115 2116 2117 2118 2119
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Octopus LE DVB adapter",
	.port_num = 2,
};

2120 2121 2122 2123 2124 2125
static const struct ddb_info ddb_octopus_oem = {
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Octopus OEM",
	.port_num = 4,
};

J
Julia Lawall 已提交
2126
static const struct ddb_info ddb_octopus_mini = {
2127 2128 2129 2130 2131
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Octopus Mini",
	.port_num = 4,
};

J
Julia Lawall 已提交
2132
static const struct ddb_info ddb_v6 = {
2133 2134 2135 2136
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Cine S2 V6 DVB adapter",
	.port_num = 3,
};
J
Julia Lawall 已提交
2137
static const struct ddb_info ddb_v6_5 = {
2138 2139 2140 2141 2142
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Cine S2 V6.5 DVB adapter",
	.port_num = 4,
};

J
Julia Lawall 已提交
2143
static const struct ddb_info ddb_dvbct = {
2144 2145 2146 2147 2148
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices DVBCT V6.1 DVB adapter",
	.port_num = 3,
};

2149 2150 2151 2152 2153 2154 2155 2156
static const struct ddb_info ddb_ctv7 = {
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Cine CT V7 DVB adapter",
	.port_num = 4,
	.board_control   = 3,
	.board_control_2 = 4,
};

J
Julia Lawall 已提交
2157
static const struct ddb_info ddb_satixS2v3 = {
2158 2159 2160 2161 2162
	.type     = DDB_OCTOPUS,
	.name     = "Mystique SaTiX-S2 V3 DVB adapter",
	.port_num = 3,
};

J
Julia Lawall 已提交
2163
static const struct ddb_info ddb_octopusv3 = {
2164 2165 2166 2167
	.type     = DDB_OCTOPUS,
	.name     = "Digital Devices Octopus V3 DVB adapter",
	.port_num = 4,
};
2168

2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
/*** MaxA8 adapters ***********************************************************/

static struct ddb_info ddb_ct2_8 = {
	.type     = DDB_OCTOPUS_MAX_CT,
	.name     = "Digital Devices MAX A8 CT2",
	.port_num = 4,
	.board_control   = 0x0ff,
	.board_control_2 = 0xf00,
	.ts_quirks = TS_QUIRK_SERIAL,
};

static struct ddb_info ddb_c2t2_8 = {
	.type     = DDB_OCTOPUS_MAX_CT,
	.name     = "Digital Devices MAX A8 C2T2",
	.port_num = 4,
	.board_control   = 0x0ff,
	.board_control_2 = 0xf00,
	.ts_quirks = TS_QUIRK_SERIAL,
};

static struct ddb_info ddb_isdbt_8 = {
	.type     = DDB_OCTOPUS_MAX_CT,
	.name     = "Digital Devices MAX A8 ISDBT",
	.port_num = 4,
	.board_control   = 0x0ff,
	.board_control_2 = 0xf00,
	.ts_quirks = TS_QUIRK_SERIAL,
};

static struct ddb_info ddb_c2t2i_v0_8 = {
	.type     = DDB_OCTOPUS_MAX_CT,
	.name     = "Digital Devices MAX A8 C2T2I V0",
	.port_num = 4,
	.board_control   = 0x0ff,
	.board_control_2 = 0xf00,
	.ts_quirks = TS_QUIRK_SERIAL | TS_QUIRK_ALT_OSC,
};

static struct ddb_info ddb_c2t2i_8 = {
	.type     = DDB_OCTOPUS_MAX_CT,
	.name     = "Digital Devices MAX A8 C2T2I",
	.port_num = 4,
	.board_control   = 0x0ff,
	.board_control_2 = 0xf00,
	.ts_quirks = TS_QUIRK_SERIAL,
};

/******************************************************************************/

2218 2219
#define DDVID 0xdd01 /* Digital Devices Vendor ID */

2220
#define DDB_ID(_vend, _dev, _subvend, _subdev, _driverdata) {	\
2221 2222 2223 2224
	.vendor      = _vend,    .device    = _dev, \
	.subvendor   = _subvend, .subdevice = _subdev, \
	.driver_data = (unsigned long)&_driverdata }

2225
static const struct pci_device_id ddb_id_tbl[] = {
2226 2227
	DDB_ID(DDVID, 0x0002, DDVID, 0x0001, ddb_octopus),
	DDB_ID(DDVID, 0x0003, DDVID, 0x0001, ddb_octopus),
2228
	DDB_ID(DDVID, 0x0005, DDVID, 0x0004, ddb_octopusv3),
2229
	DDB_ID(DDVID, 0x0003, DDVID, 0x0002, ddb_octopus_le),
2230
	DDB_ID(DDVID, 0x0003, DDVID, 0x0003, ddb_octopus_oem),
2231
	DDB_ID(DDVID, 0x0003, DDVID, 0x0010, ddb_octopus_mini),
2232
	DDB_ID(DDVID, 0x0005, DDVID, 0x0011, ddb_octopus_mini),
2233
	DDB_ID(DDVID, 0x0003, DDVID, 0x0020, ddb_v6),
2234 2235 2236
	DDB_ID(DDVID, 0x0003, DDVID, 0x0021, ddb_v6_5),
	DDB_ID(DDVID, 0x0003, DDVID, 0x0030, ddb_dvbct),
	DDB_ID(DDVID, 0x0003, DDVID, 0xdb03, ddb_satixS2v3),
2237 2238 2239 2240 2241 2242 2243 2244 2245
	DDB_ID(DDVID, 0x0006, DDVID, 0x0031, ddb_ctv7),
	DDB_ID(DDVID, 0x0006, DDVID, 0x0032, ddb_ctv7),
	DDB_ID(DDVID, 0x0006, DDVID, 0x0033, ddb_ctv7),
	DDB_ID(DDVID, 0x0008, DDVID, 0x0034, ddb_ct2_8),
	DDB_ID(DDVID, 0x0008, DDVID, 0x0035, ddb_c2t2_8),
	DDB_ID(DDVID, 0x0008, DDVID, 0x0036, ddb_isdbt_8),
	DDB_ID(DDVID, 0x0008, DDVID, 0x0037, ddb_c2t2i_v0_8),
	DDB_ID(DDVID, 0x0008, DDVID, 0x0038, ddb_c2t2i_8),
	DDB_ID(DDVID, 0x0006, DDVID, 0x0039, ddb_ctv7),
2246 2247
	/* in case sub-ids got deleted in flash */
	DDB_ID(DDVID, 0x0003, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
2248 2249 2250 2251 2252 2253 2254 2255
	DDB_ID(DDVID, 0x0005, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0006, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0007, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0008, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0011, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0013, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0201, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
	DDB_ID(DDVID, 0x0320, PCI_ANY_ID, PCI_ANY_ID, ddb_none),
2256 2257 2258 2259 2260 2261 2262 2263 2264
	{0}
};
MODULE_DEVICE_TABLE(pci, ddb_id_tbl);


static struct pci_driver ddb_pci_driver = {
	.name        = "DDBridge",
	.id_table    = ddb_id_tbl,
	.probe       = ddb_probe,
2265
	.remove      = ddb_remove,
2266 2267 2268 2269
};

static __init int module_init_ddbridge(void)
{
2270 2271
	int ret;

2272
	pr_info("Digital Devices PCIE bridge driver, Copyright (C) 2010-11 Digital Devices GmbH\n");
2273 2274 2275 2276 2277 2278 2279 2280

	ret = ddb_class_create();
	if (ret < 0)
		return ret;
	ret = pci_register_driver(&ddb_pci_driver);
	if (ret < 0)
		ddb_class_destroy();
	return ret;
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
}

static __exit void module_exit_ddbridge(void)
{
	pci_unregister_driver(&ddb_pci_driver);
	ddb_class_destroy();
}

module_init(module_init_ddbridge);
module_exit(module_exit_ddbridge);

MODULE_DESCRIPTION("Digital Devices PCIe Bridge");
MODULE_AUTHOR("Ralph Metzler");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.5");