ddbridge-core.c 83.5 KB
Newer Older
1
/*
2 3 4 5 6
 * ddbridge-core.c: Digital Devices bridge core functions
 *
 * Copyright (C) 2010-2017 Digital Devices GmbH
 *                         Marcus Metzler <mocm@metzlerbros.de>
 *                         Ralph Metzler <rjkm@metzlerbros.de>
7 8 9 10 11 12 13 14 15 16 17 18
 *
 *
 * 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.
 *
19 20
 * To obtain the license, point your browser to
 * http://www.gnu.org/copyleft/gpl.html
21 22 23 24 25 26 27 28
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/poll.h>
29
#include <linux/io.h>
30 31 32 33 34 35 36
#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>

37 38
#include "ddbridge.h"
#include "ddbridge-i2c.h"
39
#include "ddbridge-regs.h"
40
#include "ddbridge-max.h"
41
#include "ddbridge-ci.h"
42
#include "ddbridge-io.h"
43 44 45 46 47 48

#include "tda18271c2dd.h"
#include "stv6110x.h"
#include "stv090x.h"
#include "lnbh24.h"
#include "drxk.h"
49 50
#include "stv0367.h"
#include "stv0367_priv.h"
51
#include "cxd2841er.h"
52
#include "tda18212.h"
53 54 55
#include "stv0910.h"
#include "stv6111.h"
#include "lnbh25.h"
56
#include "cxd2099.h"
57
#include "dvb_dummy_fe.h"
58 59 60 61

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

#define DDB_MAX_ADAPTER 64
62

63 64
/****************************************************************************/

65 66
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

67 68 69 70 71
static int adapter_alloc;
module_param(adapter_alloc, int, 0444);
MODULE_PARM_DESC(adapter_alloc,
		 "0-one adapter per io, 1-one per tab with io, 2-one per tab, 3-one for all");

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static int ci_bitrate = 70000;
module_param(ci_bitrate, int, 0444);
MODULE_PARM_DESC(ci_bitrate, " Bitrate in KHz for output to CI.");

static int ts_loop = -1;
module_param(ts_loop, int, 0444);
MODULE_PARM_DESC(ts_loop, "TS in/out test loop on port ts_loop");

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");

#ifdef __arm__
static int alt_dma = 1;
#else
static int alt_dma;
#endif
module_param(alt_dma, int, 0444);
MODULE_PARM_DESC(alt_dma, "use alternative DMA buffer handling");

static int no_init;
module_param(no_init, int, 0444);
MODULE_PARM_DESC(no_init, "do not initialize most devices");

static int stv0910_single;
module_param(stv0910_single, int, 0444);
MODULE_PARM_DESC(stv0910_single, "use stv0910 cards as single demods");

100 101 102 103 104 105 106 107 108
static int dma_buf_num = 8;
module_param(dma_buf_num, int, 0444);
MODULE_PARM_DESC(dma_buf_num, "Number of DMA buffers, possible values: 8-32");

static int dma_buf_size = 21;
module_param(dma_buf_size, int, 0444);
MODULE_PARM_DESC(dma_buf_size,
		 "DMA buffer size as multiple of 128*47, possible values: 1-43");

109 110 111 112 113
static int dummy_tuner;
module_param(dummy_tuner, int, 0444);
MODULE_PARM_DESC(dummy_tuner,
		 "attach dummy tuner to port 0 on Octopus V3 or Octopus Mini cards");

114 115
/****************************************************************************/

116
static DEFINE_MUTEX(redirect_lock);
117

118
static struct workqueue_struct *ddb_wq;
119

120 121 122 123 124
static struct ddb *ddbs[DDB_MAX_ADAPTER];

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
125

126 127 128 129 130 131 132 133 134 135
struct ddb_irq *ddb_irq_set(struct ddb *dev, u32 link, u32 nr,
			    void (*handler)(void *), void *data)
{
	struct ddb_irq *irq = &dev->link[link].irq[nr];

	irq->handler = handler;
	irq->data = data;
	return irq;
}

136
static void ddb_set_dma_table(struct ddb_io *io)
137
{
138 139 140
	struct ddb *dev = io->port->dev;
	struct ddb_dma *dma = io->dma;
	u32 i;
141 142
	u64 mem;

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	if (!dma)
		return;
	for (i = 0; i < dma->num; i++) {
		mem = dma->pbuf[i];
		ddbwritel(dev, mem & 0xffffffff, dma->bufregs + i * 8);
		ddbwritel(dev, mem >> 32, dma->bufregs + i * 8 + 4);
	}
	dma->bufval = ((dma->div & 0x0f) << 16) |
		((dma->num & 0x1f) << 11) |
		((dma->size >> 7) & 0x7ff);
}

static void ddb_set_dma_tables(struct ddb *dev)
{
	u32 i;

	for (i = 0; i < DDB_MAX_PORT; i++) {
		if (dev->port[i].input[0])
			ddb_set_dma_table(dev->port[i].input[0]);
		if (dev->port[i].input[1])
			ddb_set_dma_table(dev->port[i].input[1]);
		if (dev->port[i].output)
			ddb_set_dma_table(dev->port[i].output);
166 167 168
	}
}

169 170 171 172 173 174 175
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

static void ddb_redirect_dma(struct ddb *dev,
			     struct ddb_dma *sdma,
			     struct ddb_dma *ddma)
176
{
177
	u32 i, base;
178
	u64 mem;
179 180 181 182 183 184 185 186 187 188 189 190

	sdma->bufval = ddma->bufval;
	base = sdma->bufregs;
	for (i = 0; i < ddma->num; i++) {
		mem = ddma->pbuf[i];
		ddbwritel(dev, mem & 0xffffffff, base + i * 8);
		ddbwritel(dev, mem >> 32, base + i * 8 + 4);
	}
}

static int ddb_unredirect(struct ddb_port *port)
{
191 192
	struct ddb_input *oredi, *iredi = NULL;
	struct ddb_output *iredo = NULL;
193 194 195 196 197 198 199 200

	/* dev_info(port->dev->dev,
	 * "unredirect %d.%d\n", port->dev->nr, port->nr);
	 */
	mutex_lock(&redirect_lock);
	if (port->output->dma->running) {
		mutex_unlock(&redirect_lock);
		return -EBUSY;
201
	}
202 203 204 205 206 207 208 209 210 211 212 213 214 215
	oredi = port->output->redi;
	if (!oredi)
		goto done;
	if (port->input[0]) {
		iredi = port->input[0]->redi;
		iredo = port->input[0]->redo;

		if (iredo) {
			iredo->port->output->redi = oredi;
			if (iredo->port->input[0]) {
				iredo->port->input[0]->redi = iredi;
				ddb_redirect_dma(oredi->port->dev,
						 oredi->dma, iredo->dma);
			}
216
			port->input[0]->redo = NULL;
217
			ddb_set_dma_table(port->input[0]);
218
		}
219
		oredi->redi = iredi;
220
		port->input[0]->redi = NULL;
221
	}
222 223
	oredi->redo = NULL;
	port->output->redi = NULL;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

	ddb_set_dma_table(oredi);
done:
	mutex_unlock(&redirect_lock);
	return 0;
}

static int ddb_redirect(u32 i, u32 p)
{
	struct ddb *idev = ddbs[(i >> 4) & 0x3f];
	struct ddb_input *input, *input2;
	struct ddb *pdev = ddbs[(p >> 4) & 0x3f];
	struct ddb_port *port;

	if (!idev || !pdev)
		return -EINVAL;
240 241
	if (!idev->has_dma || !pdev->has_dma)
		return -EINVAL;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

	port = &pdev->port[p & 0x0f];
	if (!port->output)
		return -EINVAL;
	if (ddb_unredirect(port))
		return -EBUSY;

	if (i == 8)
		return 0;

	input = &idev->input[i & 7];
	if (!input)
		return -EINVAL;

	mutex_lock(&redirect_lock);
	if (port->output->dma->running || input->dma->running) {
		mutex_unlock(&redirect_lock);
		return -EBUSY;
260
	}
261 262 263 264
	input2 = port->input[0];
	if (input2) {
		if (input->redi) {
			input2->redi = input->redi;
265
			input->redi = NULL;
266
		} else {
267
			input2->redi = input;
268
		}
269 270 271 272 273 274 275
	}
	input->redo = port->output;
	port->output->redi = input;

	ddb_redirect_dma(input->port->dev, input->dma, port->output->dma);
	mutex_unlock(&redirect_lock);
	return 0;
276 277
}

278 279 280 281 282
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

static void dma_free(struct pci_dev *pdev, struct ddb_dma *dma, int dir)
283 284 285
{
	int i;

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	if (!dma)
		return;
	for (i = 0; i < dma->num; i++) {
		if (dma->vbuf[i]) {
			if (alt_dma) {
				dma_unmap_single(&pdev->dev, dma->pbuf[i],
						 dma->size,
						 dir ? DMA_TO_DEVICE :
						 DMA_FROM_DEVICE);
				kfree(dma->vbuf[i]);
				dma->vbuf[i] = NULL;
			} else {
				dma_free_coherent(&pdev->dev, dma->size,
						  dma->vbuf[i], dma->pbuf[i]);
			}

			dma->vbuf[i] = NULL;
303 304 305 306
		}
	}
}

307
static int dma_alloc(struct pci_dev *pdev, struct ddb_dma *dma, int dir)
308 309 310
{
	int i;

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	if (!dma)
		return 0;
	for (i = 0; i < dma->num; i++) {
		if (alt_dma) {
			dma->vbuf[i] = kmalloc(dma->size, __GFP_RETRY_MAYFAIL);
			if (!dma->vbuf[i])
				return -ENOMEM;
			dma->pbuf[i] = dma_map_single(&pdev->dev,
						      dma->vbuf[i],
						      dma->size,
						      dir ? DMA_TO_DEVICE :
						      DMA_FROM_DEVICE);
			if (dma_mapping_error(&pdev->dev, dma->pbuf[i])) {
				kfree(dma->vbuf[i]);
				dma->vbuf[i] = NULL;
				return -ENOMEM;
			}
		} else {
			dma->vbuf[i] = dma_alloc_coherent(&pdev->dev,
							  dma->size,
							  &dma->pbuf[i],
							  GFP_KERNEL);
			if (!dma->vbuf[i])
				return -ENOMEM;
		}
336 337 338 339
	}
	return 0;
}

340
int ddb_buffers_alloc(struct ddb *dev)
341 342 343 344
{
	int i;
	struct ddb_port *port;

345
	for (i = 0; i < dev->port_num; i++) {
346 347 348
		port = &dev->port[i];
		switch (port->class) {
		case DDB_PORT_TUNER:
349 350 351 352 353 354 355 356
			if (port->input[0]->dma)
				if (dma_alloc(dev->pdev, port->input[0]->dma, 0)
					< 0)
					return -1;
			if (port->input[1]->dma)
				if (dma_alloc(dev->pdev, port->input[1]->dma, 0)
					< 0)
					return -1;
357 358
			break;
		case DDB_PORT_CI:
359 360 361 362 363 364 365 366 367
		case DDB_PORT_LOOP:
			if (port->input[0]->dma)
				if (dma_alloc(dev->pdev, port->input[0]->dma, 0)
					< 0)
					return -1;
			if (port->output->dma)
				if (dma_alloc(dev->pdev, port->output->dma, 1)
					< 0)
					return -1;
368 369 370 371 372
			break;
		default:
			break;
		}
	}
373
	ddb_set_dma_tables(dev);
374 375 376
	return 0;
}

377
void ddb_buffers_free(struct ddb *dev)
378 379 380 381
{
	int i;
	struct ddb_port *port;

382
	for (i = 0; i < dev->port_num; i++) {
383
		port = &dev->port[i];
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

		if (port->input[0] && port->input[0]->dma)
			dma_free(dev->pdev, port->input[0]->dma, 0);
		if (port->input[1] && port->input[1]->dma)
			dma_free(dev->pdev, port->input[1]->dma, 0);
		if (port->output && port->output->dma)
			dma_free(dev->pdev, port->output->dma, 1);
	}
}

static void calc_con(struct ddb_output *output, u32 *con, u32 *con2, u32 flags)
{
	struct ddb *dev = output->port->dev;
	u32 bitrate = output->port->obr, max_bitrate = 72000;
	u32 gap = 4, nco = 0;

	*con = 0x1c;
	if (output->port->gap != 0xffffffff) {
		flags |= 1;
		gap = output->port->gap;
404
		max_bitrate = 0;
405 406 407 408 409 410 411 412 413
	}
	if (dev->link[0].info->type == DDB_OCTOPUS_CI && output->port->nr > 1) {
		*con = 0x10c;
		if (dev->link[0].ids.regmapid >= 0x10003 && !(flags & 1)) {
			if (!(flags & 2)) {
				/* NCO */
				max_bitrate = 0;
				gap = 0;
				if (bitrate != 72000) {
414
					if (bitrate >= 96000) {
415
						*con |= 0x800;
416
					} else {
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
						*con |= 0x1000;
						nco = (bitrate * 8192 + 71999)
							/ 72000;
					}
				}
			} else {
				/* Divider and gap */
				*con |= 0x1810;
				if (bitrate <= 64000) {
					max_bitrate = 64000;
					nco = 8;
				} else if (bitrate <= 72000) {
					max_bitrate = 72000;
					nco = 7;
				} else {
					max_bitrate = 96000;
					nco = 5;
				}
			}
		} else {
			if (bitrate > 72000) {
				*con |= 0x810; /* 96 MBit/s and gap */
				max_bitrate = 96000;
			}
441
			*con |= 0x10; /* enable gap */
442
		}
443
	}
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	if (max_bitrate > 0) {
		if (bitrate > max_bitrate)
			bitrate = max_bitrate;
		if (bitrate < 31000)
			bitrate = 31000;
		gap = ((max_bitrate - bitrate) * 94) / bitrate;
		if (gap < 2)
			*con &= ~0x10; /* Disable gap */
		else
			gap -= 2;
		if (gap > 127)
			gap = 127;
	}

	*con2 = (nco << 16) | gap;
459 460 461 462 463
}

static void ddb_output_start(struct ddb_output *output)
{
	struct ddb *dev = output->port->dev;
464 465
	u32 con = 0x11c, con2 = 0;

466 467 468 469 470
	spin_lock_irq(&output->dma->lock);
	output->dma->cbuf = 0;
	output->dma->coff = 0;
	output->dma->stat = 0;
	ddbwritel(dev, 0, DMA_BUFFER_CONTROL(output->dma));
471 472 473 474 475 476 477 478 479 480 481 482

	if (output->port->input[0]->port->class == DDB_PORT_LOOP)
		con = (1UL << 13) | 0x14;
	else
		calc_con(output, &con, &con2, 0);

	ddbwritel(dev, 0, TS_CONTROL(output));
	ddbwritel(dev, 2, TS_CONTROL(output));
	ddbwritel(dev, 0, TS_CONTROL(output));
	ddbwritel(dev, con, TS_CONTROL(output));
	ddbwritel(dev, con2, TS_CONTROL2(output));

483 484 485 486 487
	ddbwritel(dev, output->dma->bufval,
		  DMA_BUFFER_SIZE(output->dma));
	ddbwritel(dev, 0, DMA_BUFFER_ACK(output->dma));
	ddbwritel(dev, 1, DMA_BASE_READ);
	ddbwritel(dev, 7, DMA_BUFFER_CONTROL(output->dma));
488 489

	ddbwritel(dev, con | 1, TS_CONTROL(output));
490

491 492
	output->dma->running = 1;
	spin_unlock_irq(&output->dma->lock);
493 494 495 496 497 498
}

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

499
	spin_lock_irq(&output->dma->lock);
500 501 502

	ddbwritel(dev, 0, TS_CONTROL(output));

503 504 505
	ddbwritel(dev, 0, DMA_BUFFER_CONTROL(output->dma));
	output->dma->running = 0;
	spin_unlock_irq(&output->dma->lock);
506 507
}

508 509 510
static void ddb_input_stop(struct ddb_input *input)
{
	struct ddb *dev = input->port->dev;
511 512
	u32 tag = DDB_LINK_TAG(input->port->lnr);

513 514
	spin_lock_irq(&input->dma->lock);

515
	ddbwritel(dev, 0, tag | TS_CONTROL(input));
516 517 518 519

	ddbwritel(dev, 0, DMA_BUFFER_CONTROL(input->dma));
	input->dma->running = 0;
	spin_unlock_irq(&input->dma->lock);
520 521 522 523 524 525
}

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

526 527 528 529 530 531
	spin_lock_irq(&input->dma->lock);
	input->dma->cbuf = 0;
	input->dma->coff = 0;
	input->dma->stat = 0;
	ddbwritel(dev, 0, DMA_BUFFER_CONTROL(input->dma));

532 533 534 535
	ddbwritel(dev, 0, TS_CONTROL(input));
	ddbwritel(dev, 2, TS_CONTROL(input));
	ddbwritel(dev, 0, TS_CONTROL(input));

536 537 538 539 540
	ddbwritel(dev, input->dma->bufval,
		  DMA_BUFFER_SIZE(input->dma));
	ddbwritel(dev, 0, DMA_BUFFER_ACK(input->dma));
	ddbwritel(dev, 1, DMA_BASE_WRITE);
	ddbwritel(dev, 3, DMA_BUFFER_CONTROL(input->dma));
541 542 543

	ddbwritel(dev, 0x09, TS_CONTROL(input));

544 545 546
	if (input->port->type == DDB_TUNER_DUMMY)
		ddbwritel(dev, 0x000fff01, TS_CONTROL2(input));

547 548
	input->dma->running = 1;
	spin_unlock_irq(&input->dma->lock);
549 550 551 552 553 554
}

static void ddb_input_start_all(struct ddb_input *input)
{
	struct ddb_input *i = input;
	struct ddb_output *o;
555

556 557 558 559 560 561 562 563 564 565
	mutex_lock(&redirect_lock);
	while (i && (o = i->redo)) {
		ddb_output_start(o);
		i = o->port->input[0];
		if (i)
			ddb_input_start(i);
	}
	ddb_input_start(input);
	mutex_unlock(&redirect_lock);
}
566

567 568 569 570
static void ddb_input_stop_all(struct ddb_input *input)
{
	struct ddb_input *i = input;
	struct ddb_output *o;
571

572 573 574 575 576 577 578 579 580
	mutex_lock(&redirect_lock);
	ddb_input_stop(input);
	while (i && (o = i->redo)) {
		ddb_output_stop(o);
		i = o->port->input[0];
		if (i)
			ddb_input_stop(i);
	}
	mutex_unlock(&redirect_lock);
581 582
}

583 584
static u32 ddb_output_free(struct ddb_output *output)
{
585
	u32 idx, off, stat = output->dma->stat;
586 587 588 589 590
	s32 diff;

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

591 592
	if (output->dma->cbuf != idx) {
		if ((((output->dma->cbuf + 1) % output->dma->num) == idx) &&
593
		    (output->dma->size - output->dma->coff <= (2 * 188)))
594 595 596
			return 0;
		return 188;
	}
597
	diff = off - output->dma->coff;
598
	if (diff <= 0 || diff > (2 * 188))
599 600 601 602
		return 188;
	return 0;
}

603
static ssize_t ddb_output_write(struct ddb_output *output,
604
				const __user u8 *buf, size_t count)
605 606
{
	struct ddb *dev = output->port->dev;
607
	u32 idx, off, stat = output->dma->stat;
608 609 610 611 612 613
	u32 left = count, len;

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

	while (left) {
614 615
		len = output->dma->size - output->dma->coff;
		if ((((output->dma->cbuf + 1) % output->dma->num) == idx) &&
616
		    off == 0) {
617
			if (len <= 188)
618
				break;
619
			len -= 188;
620
		}
621 622 623
		if (output->dma->cbuf == idx) {
			if (off > output->dma->coff) {
				len = off - output->dma->coff;
624 625 626 627 628 629 630 631
				len -= (len % 188);
				if (len <= 188)
					break;
				len -= 188;
			}
		}
		if (len > left)
			len = left;
632 633
		if (copy_from_user(output->dma->vbuf[output->dma->cbuf] +
				   output->dma->coff,
634 635
				   buf, len))
			return -EIO;
636
		if (alt_dma)
637 638
			dma_sync_single_for_device(
				dev->dev,
639 640
				output->dma->pbuf[output->dma->cbuf],
				output->dma->size, DMA_TO_DEVICE);
641 642
		left -= len;
		buf += len;
643 644 645 646 647
		output->dma->coff += len;
		if (output->dma->coff == output->dma->size) {
			output->dma->coff = 0;
			output->dma->cbuf = ((output->dma->cbuf + 1) %
					     output->dma->num);
648
		}
649 650 651 652
		ddbwritel(dev,
			  (output->dma->cbuf << 11) |
			  (output->dma->coff >> 7),
			  DMA_BUFFER_ACK(output->dma));
653 654 655 656 657 658 659
	}
	return count - left;
}

static u32 ddb_input_avail(struct ddb_input *input)
{
	struct ddb *dev = input->port->dev;
660 661
	u32 idx, off, stat = input->dma->stat;
	u32 ctrl = ddbreadl(dev, DMA_BUFFER_CONTROL(input->dma));
662 663 664 665 666

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

	if (ctrl & 4) {
667 668
		dev_err(dev->dev, "IA %d %d %08x\n", idx, off, ctrl);
		ddbwritel(dev, stat, DMA_BUFFER_ACK(input->dma));
669 670
		return 0;
	}
671
	if (input->dma->cbuf != idx)
672 673 674 675
		return 188;
	return 0;
}

676
static ssize_t ddb_input_read(struct ddb_input *input,
677
			      __user u8 *buf, size_t count)
678 679 680
{
	struct ddb *dev = input->port->dev;
	u32 left = count;
681
	u32 idx, free, stat = input->dma->stat;
682 683 684 685 686
	int ret;

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

	while (left) {
687
		if (input->dma->cbuf == idx)
688
			return count - left;
689
		free = input->dma->size - input->dma->coff;
690 691
		if (free > left)
			free = left;
692
		if (alt_dma)
693 694
			dma_sync_single_for_cpu(
				dev->dev,
695 696 697 698
				input->dma->pbuf[input->dma->cbuf],
				input->dma->size, DMA_FROM_DEVICE);
		ret = copy_to_user(buf, input->dma->vbuf[input->dma->cbuf] +
				   input->dma->coff, free);
699 700
		if (ret)
			return -EFAULT;
701 702 703 704 705
		input->dma->coff += free;
		if (input->dma->coff == input->dma->size) {
			input->dma->coff = 0;
			input->dma->cbuf = (input->dma->cbuf + 1) %
				input->dma->num;
706 707
		}
		left -= free;
708 709 710 711
		buf += free;
		ddbwritel(dev,
			  (input->dma->cbuf << 11) | (input->dma->coff >> 7),
			  DMA_BUFFER_ACK(input->dma));
712 713 714 715
	}
	return count;
}

716 717 718 719 720 721 722 723
/****************************************************************************/
/****************************************************************************/

static ssize_t ts_write(struct file *file, const __user char *buf,
			size_t count, loff_t *ppos)
{
	struct dvb_device *dvbdev = file->private_data;
	struct ddb_output *output = dvbdev->priv;
724
	struct ddb *dev = output->port->dev;
725 726 727
	size_t left = count;
	int stat;

728 729
	if (!dev->has_dma)
		return -EINVAL;
730 731 732 733 734
	while (left) {
		if (ddb_output_free(output) < 188) {
			if (file->f_flags & O_NONBLOCK)
				break;
			if (wait_event_interruptible(
735 736
				    output->dma->wq,
				    ddb_output_free(output) >= 188) < 0)
737 738 739 740
				break;
		}
		stat = ddb_output_write(output, buf, left);
		if (stat < 0)
741
			return stat;
742 743 744 745 746 747 748 749 750 751 752 753
		buf += stat;
		left -= stat;
	}
	return (left == count) ? -EAGAIN : (count - left);
}

static ssize_t ts_read(struct file *file, __user char *buf,
		       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];
754 755 756
	struct ddb *dev = output->port->dev;
	size_t left = count;
	int stat;
757

758 759
	if (!dev->has_dma)
		return -EINVAL;
760 761 762 763 764
	while (left) {
		if (ddb_input_avail(input) < 188) {
			if (file->f_flags & O_NONBLOCK)
				break;
			if (wait_event_interruptible(
765 766
				    input->dma->wq,
				    ddb_input_avail(input) >= 188) < 0)
767 768
				break;
		}
769 770 771 772 773
		stat = ddb_input_read(input, buf, left);
		if (stat < 0)
			return stat;
		left -= stat;
		buf += stat;
774
	}
775
	return (count && (left == count)) ? -EAGAIN : (count - left);
776 777
}

A
Al Viro 已提交
778
static __poll_t ts_poll(struct file *file, poll_table *wait)
779 780 781 782
{
	struct dvb_device *dvbdev = file->private_data;
	struct ddb_output *output = dvbdev->priv;
	struct ddb_input *input = output->port->input[0];
783

A
Al Viro 已提交
784
	__poll_t mask = 0;
785

786 787 788
	poll_wait(file, &input->dma->wq, wait);
	poll_wait(file, &output->dma->wq, wait);
	if (ddb_input_avail(input) >= 188)
789
		mask |= EPOLLIN | EPOLLRDNORM;
790
	if (ddb_output_free(output) >= 188)
791
		mask |= EPOLLOUT | EPOLLWRNORM;
792 793 794
	return mask;
}

795 796 797
static int ts_release(struct inode *inode, struct file *file)
{
	struct dvb_device *dvbdev = file->private_data;
798 799 800 801 802 803 804
	struct ddb_output *output = NULL;
	struct ddb_input *input = NULL;

	if (dvbdev) {
		output = dvbdev->priv;
		input = output->port->input[0];
	}
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
		if (!input)
			return -EINVAL;
		ddb_input_stop(input);
	} else if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
		if (!output)
			return -EINVAL;
		ddb_output_stop(output);
	}
	return dvb_generic_release(inode, file);
}

static int ts_open(struct inode *inode, struct file *file)
{
	int err;
	struct dvb_device *dvbdev = file->private_data;
822 823 824 825 826 827 828
	struct ddb_output *output = NULL;
	struct ddb_input *input = NULL;

	if (dvbdev) {
		output = dvbdev->priv;
		input = output->port->input[0];
	}
829 830 831 832 833 834 835 836 837

	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
		if (!input)
			return -EINVAL;
		if (input->redo || input->redi)
			return -EBUSY;
	} else if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
		if (!output)
			return -EINVAL;
838
	} else {
839
		return -EINVAL;
840 841
	}

842 843 844 845 846 847 848 849 850 851
	err = dvb_generic_open(inode, file);
	if (err < 0)
		return err;
	if ((file->f_flags & O_ACCMODE) == O_RDONLY)
		ddb_input_start(input);
	else if ((file->f_flags & O_ACCMODE) == O_WRONLY)
		ddb_output_start(output);
	return err;
}

852 853 854 855
static const struct file_operations ci_fops = {
	.owner   = THIS_MODULE,
	.read    = ts_read,
	.write   = ts_write,
856 857
	.open    = ts_open,
	.release = ts_release,
858
	.poll    = ts_poll,
859
	.mmap    = NULL,
860 861 862
};

static struct dvb_device dvbdev_ci = {
863
	.priv    = NULL,
864 865 866
	.readers = 1,
	.writers = 1,
	.users   = 2,
867 868 869
	.fops    = &ci_fops,
};

870 871
/****************************************************************************/
/****************************************************************************/
872

873
static int locked_gate_ctrl(struct dvb_frontend *fe, int enable)
874 875 876
{
	struct ddb_input *input = fe->sec_priv;
	struct ddb_port *port = input->port;
877
	struct ddb_dvb *dvb = &port->dvb[input->nr & 1];
878 879 880 881
	int status;

	if (enable) {
		mutex_lock(&port->i2c_gate_lock);
882
		status = dvb->i2c_gate_ctrl(fe, 1);
883
	} else {
884
		status = dvb->i2c_gate_ctrl(fe, 0);
885 886 887 888 889 890 891 892
		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;
893 894
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
895
	struct drxk_config config;
896

897 898
	memset(&config, 0, sizeof(config));
	config.adr = 0x29 + (input->nr & 1);
899
	config.microcode_name = "drxk_a3.mc";
900

901 902
	dvb->fe = dvb_attach(drxk_attach, &config, i2c);
	if (!dvb->fe) {
903
		dev_err(dev, "No DRXK found!\n");
904 905
		return -ENODEV;
	}
906 907 908
	dvb->fe->sec_priv = input;
	dvb->i2c_gate_ctrl = dvb->fe->ops.i2c_gate_ctrl;
	dvb->fe->ops.i2c_gate_ctrl = locked_gate_ctrl;
909 910 911 912 913 914
	return 0;
}

static int tuner_attach_tda18271(struct ddb_input *input)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
915 916
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
917 918
	struct dvb_frontend *fe;

919 920 921 922 923
	if (dvb->fe->ops.i2c_gate_ctrl)
		dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 1);
	fe = dvb_attach(tda18271c2dd_attach, dvb->fe, i2c, 0x60);
	if (dvb->fe->ops.i2c_gate_ctrl)
		dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 0);
924
	if (!fe) {
925
		dev_err(dev, "No TDA18271 found!\n");
926 927 928 929 930 931 932 933 934
		return -ENODEV;
	}
	return 0;
}

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

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
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;
956 957
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
958 959

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

963 964
	if (!dvb->fe) {
		dev_err(dev, "No stv0367 found!\n");
965 966
		return -ENODEV;
	}
967 968 969
	dvb->fe->sec_priv = input;
	dvb->i2c_gate_ctrl = dvb->fe->ops.i2c_gate_ctrl;
	dvb->fe->ops.i2c_gate_ctrl = locked_gate_ctrl;
970 971 972 973 974 975
	return 0;
}

static int tuner_tda18212_ping(struct ddb_input *input, unsigned short adr)
{
	struct i2c_adapter *adapter = &input->port->i2c->adap;
976 977
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
978 979 980
	u8 tda_id[2];
	u8 subaddr = 0x00;

981
	dev_dbg(dev, "stv0367-tda18212 tuner ping\n");
982 983
	if (dvb->fe->ops.i2c_gate_ctrl)
		dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 1);
984 985

	if (i2c_read_regs(adapter, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
986
		dev_dbg(dev, "tda18212 ping 1 fail\n");
987
	if (i2c_read_regs(adapter, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
988
		dev_warn(dev, "tda18212 ping failed, expect problems\n");
989

990 991
	if (dvb->fe->ops.i2c_gate_ctrl)
		dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 0);
992 993 994 995

	return 0;
}

996 997 998
static int demod_attach_cxd28xx(struct ddb_input *input, int par, int osc24)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
999 1000
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	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 */
1015
	dvb->fe = dvb_attach(cxd2841er_attach_t_c, &cfg, i2c);
1016

1017 1018
	if (!dvb->fe) {
		dev_err(dev, "No cxd2837/38/43/54 found!\n");
1019 1020
		return -ENODEV;
	}
1021 1022 1023
	dvb->fe->sec_priv = input;
	dvb->i2c_gate_ctrl = dvb->fe->ops.i2c_gate_ctrl;
	dvb->fe->ops.i2c_gate_ctrl = locked_gate_ctrl;
1024 1025 1026
	return 0;
}

1027 1028 1029
static int tuner_attach_tda18212(struct ddb_input *input, u32 porttype)
{
	struct i2c_adapter *adapter = &input->port->i2c->adap;
1030 1031
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
1032 1033
	struct i2c_client *client;
	struct tda18212_config config = {
1034
		.fe = dvb->fe,
1035 1036 1037 1038 1039 1040 1041 1042
		.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,
	};
1043
	u8 addr = (input->nr & 1) ? 0x63 : 0x60;
1044 1045 1046 1047 1048 1049

	/* 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)
1050
		tuner_tda18212_ping(input, addr);
1051

1052 1053 1054
	/* perform tuner probe/init/attach */
	client = dvb_module_probe("tda18212", NULL, adapter, addr, &config);
	if (!client)
1055 1056
		goto err;

1057
	dvb->i2c_client[0] = client;
1058 1059
	return 0;
err:
1060
	dev_err(dev, "TDA18212 tuner not found. Device is not fully operational.\n");
1061 1062 1063
	return -ENODEV;
}

1064 1065 1066
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
1067

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
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,

1079 1080 1081
	.ts1_tei        = 1,
	.ts2_tei        = 1,

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	.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,

1101 1102 1103
	.ts1_tei        = 1,
	.ts2_tei        = 1,

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	.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;
	struct stv090x_config *feconf = type ? &stv0900_aa : &stv0900;
1128 1129
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
1130

1131 1132 1133 1134
	dvb->fe = dvb_attach(stv090x_attach, feconf, i2c,
			     (input->nr & 1) ? STV090x_DEMODULATOR_1
			     : STV090x_DEMODULATOR_0);
	if (!dvb->fe) {
1135
		dev_err(dev, "No STV0900 found!\n");
1136 1137
		return -ENODEV;
	}
1138
	if (!dvb_attach(lnbh24_attach, dvb->fe, i2c, 0,
1139 1140
			0, (input->nr & 1) ?
			(0x09 - type) : (0x0b - type))) {
1141
		dev_err(dev, "No LNBH24 found!\n");
1142
		dvb_frontend_detach(dvb->fe);
1143 1144 1145 1146 1147 1148 1149 1150
		return -ENODEV;
	}
	return 0;
}

static int tuner_attach_stv6110(struct ddb_input *input, int type)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
1151 1152
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
1153 1154 1155
	struct stv090x_config *feconf = type ? &stv0900_aa : &stv0900;
	struct stv6110x_config *tunerconf = (input->nr & 1) ?
		&stv6110b : &stv6110a;
1156
	const struct stv6110x_devctl *ctl;
1157

1158
	ctl = dvb_attach(stv6110x_attach, dvb->fe, tunerconf, i2c);
1159
	if (!ctl) {
1160
		dev_err(dev, "No STV6110X found!\n");
1161 1162
		return -ENODEV;
	}
1163
	dev_info(dev, "attach tuner input %d adr %02x\n",
1164
		 input->nr, tunerconf->addr);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

	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;
}

1181
static const struct stv0910_cfg stv0910_p = {
1182 1183 1184 1185
	.adr      = 0x68,
	.parallel = 1,
	.rptlvl   = 4,
	.clk      = 30000000,
1186
	.tsspeed  = 0x28,
1187 1188
};

1189
static const struct lnbh25_config lnbh25_cfg = {
1190 1191 1192 1193
	.i2c_address = 0x0c << 1,
	.data2_config = LNBH25_TEN
};

1194 1195 1196 1197 1198 1199 1200
static int has_lnbh25(struct i2c_adapter *i2c, u8 adr)
{
	u8 val;

	return i2c_read_reg(i2c, adr, 0, &val) ? 0 : 1;
}

1201
static int demod_attach_stv0910(struct ddb_input *input, int type, int tsfast)
1202 1203
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
1204 1205
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
1206 1207 1208
	struct stv0910_cfg cfg = stv0910_p;
	struct lnbh25_config lnbcfg = lnbh25_cfg;

1209 1210 1211
	if (stv0910_single)
		cfg.single = 1;

1212 1213
	if (type)
		cfg.parallel = 2;
1214 1215 1216 1217 1218 1219

	if (tsfast) {
		dev_info(dev, "Enabling stv0910 higher speed TS\n");
		cfg.tsspeed = 0x10;
	}

1220 1221
	dvb->fe = dvb_attach(stv0910_attach, i2c, &cfg, (input->nr & 1));
	if (!dvb->fe) {
1222
		cfg.adr = 0x6c;
1223 1224
		dvb->fe = dvb_attach(stv0910_attach, i2c,
				     &cfg, (input->nr & 1));
1225
	}
1226
	if (!dvb->fe) {
1227 1228 1229 1230 1231 1232 1233
		dev_err(dev, "No STV0910 found!\n");
		return -ENODEV;
	}

	/* attach lnbh25 - leftshift by one as the lnbh25 driver expects 8bit
	 * i2c addresses
	 */
1234 1235 1236
	if (has_lnbh25(i2c, 0x0d))
		lnbcfg.i2c_address = (((input->nr & 1) ? 0x0d : 0x0c) << 1);
	else
1237
		lnbcfg.i2c_address = (((input->nr & 1) ? 0x09 : 0x08) << 1);
1238 1239 1240 1241 1242

	if (!dvb_attach(lnbh25_attach, dvb->fe, &lnbcfg, i2c)) {
		dev_err(dev, "No LNBH25 found!\n");
		dvb_frontend_detach(dvb->fe);
		return -ENODEV;
1243 1244 1245 1246 1247 1248 1249 1250
	}

	return 0;
}

static int tuner_attach_stv6111(struct ddb_input *input, int type)
{
	struct i2c_adapter *i2c = &input->port->i2c->adap;
1251 1252
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;
1253 1254 1255
	struct dvb_frontend *fe;
	u8 adr = (type ? 0 : 4) + ((input->nr & 1) ? 0x63 : 0x60);

1256
	fe = dvb_attach(stv6111_attach, dvb->fe, i2c, adr);
1257
	if (!fe) {
1258
		fe = dvb_attach(stv6111_attach, dvb->fe, i2c, adr & ~4);
1259 1260 1261 1262 1263 1264 1265 1266
		if (!fe) {
			dev_err(dev, "No STV6111 found at 0x%02x!\n", adr);
			return -ENODEV;
		}
	}
	return 0;
}

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
static int demod_attach_dummy(struct ddb_input *input)
{
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct device *dev = input->port->dev->dev;

	dvb->fe = dvb_attach(dvb_dummy_fe_qam_attach);
	if (!dvb->fe) {
		dev_err(dev, "QAM dummy attach failed!\n");
		return -ENODEV;
	}

	return 0;
}

1281 1282 1283 1284
static int start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct ddb_input *input = dvbdmx->priv;
1285
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
1286

1287 1288
	if (!dvb->users)
		ddb_input_start_all(input);
1289

1290
	return ++dvb->users;
1291 1292 1293 1294 1295 1296
}

static int stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct ddb_input *input = dvbdmx->priv;
1297
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
1298

1299 1300
	if (--dvb->users)
		return dvb->users;
1301

1302
	ddb_input_stop_all(input);
1303 1304 1305 1306 1307
	return 0;
}

static void dvb_input_detach(struct ddb_input *input)
{
1308 1309
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
	struct dvb_demux *dvbdemux = &dvb->demux;
1310

1311 1312 1313 1314 1315 1316 1317 1318
	switch (dvb->attached) {
	case 0x31:
		if (dvb->fe2)
			dvb_unregister_frontend(dvb->fe2);
		if (dvb->fe)
			dvb_unregister_frontend(dvb->fe);
		/* fallthrough */
	case 0x30:
1319 1320
		dvb_module_release(dvb->i2c_client[0]);
		dvb->i2c_client[0] = NULL;
1321

1322 1323 1324 1325
		if (dvb->fe2)
			dvb_frontend_detach(dvb->fe2);
		if (dvb->fe)
			dvb_frontend_detach(dvb->fe);
1326 1327
		dvb->fe = NULL;
		dvb->fe2 = NULL;
1328 1329 1330 1331 1332
		/* fallthrough */
	case 0x20:
		dvb_net_release(&dvb->dvbnet);
		/* fallthrough */
	case 0x12:
1333
		dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
1334
					      &dvb->hw_frontend);
1335
		dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 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 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 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
					      &dvb->mem_frontend);
		/* fallthrough */
	case 0x11:
		dvb_dmxdev_release(&dvb->dmxdev);
		/* fallthrough */
	case 0x10:
		dvb_dmx_release(&dvb->demux);
		/* fallthrough */
	case 0x01:
		break;
	}
	dvb->attached = 0x00;
}

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

	if (adapter_alloc == 3) {
		port = &dev->port[0];
		adap = port->dvb[0].adap;
		ret = dvb_register_adapter(adap, "DDBridge", THIS_MODULE,
					   port->dev->dev,
					   adapter_nr);
		if (ret < 0)
			return ret;
		port->dvb[0].adap_registered = 1;
		for (i = 0; i < dev->port_num; i++) {
			port = &dev->port[i];
			port->dvb[0].adap = adap;
			port->dvb[1].adap = adap;
		}
		return 0;
	}

	for (i = 0; i < dev->port_num; i++) {
		port = &dev->port[i];
		switch (port->class) {
		case DDB_PORT_TUNER:
			adap = port->dvb[0].adap;
			ret = dvb_register_adapter(adap, "DDBridge",
						   THIS_MODULE,
						   port->dev->dev,
						   adapter_nr);
			if (ret < 0)
				return ret;
			port->dvb[0].adap_registered = 1;

			if (adapter_alloc > 0) {
				port->dvb[1].adap = port->dvb[0].adap;
				break;
			}
			adap = port->dvb[1].adap;
			ret = dvb_register_adapter(adap, "DDBridge",
						   THIS_MODULE,
						   port->dev->dev,
						   adapter_nr);
			if (ret < 0)
				return ret;
			port->dvb[1].adap_registered = 1;
			break;

		case DDB_PORT_CI:
		case DDB_PORT_LOOP:
			adap = port->dvb[0].adap;
			ret = dvb_register_adapter(adap, "DDBridge",
						   THIS_MODULE,
						   port->dev->dev,
						   adapter_nr);
			if (ret < 0)
				return ret;
			port->dvb[0].adap_registered = 1;
			break;
		default:
			if (adapter_alloc < 2)
				break;
			adap = port->dvb[0].adap;
			ret = dvb_register_adapter(adap, "DDBridge",
						   THIS_MODULE,
						   port->dev->dev,
						   adapter_nr);
			if (ret < 0)
				return ret;
			port->dvb[0].adap_registered = 1;
			break;
		}
	}
	return ret;
}

static void dvb_unregister_adapters(struct ddb *dev)
{
	int i;
	struct ddb_port *port;
	struct ddb_dvb *dvb;

	for (i = 0; i < dev->link[0].info->port_num; i++) {
		port = &dev->port[i];

		dvb = &port->dvb[0];
		if (dvb->adap_registered)
			dvb_unregister_adapter(dvb->adap);
		dvb->adap_registered = 0;

		dvb = &port->dvb[1];
		if (dvb->adap_registered)
			dvb_unregister_adapter(dvb->adap);
		dvb->adap_registered = 0;
1446 1447 1448 1449 1450
	}
}

static int dvb_input_attach(struct ddb_input *input)
{
1451 1452
	int ret = 0;
	struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
1453
	struct ddb_port *port = input->port;
1454 1455
	struct dvb_adapter *adap = dvb->adap;
	struct dvb_demux *dvbdemux = &dvb->demux;
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
	struct ddb_ids *devids = &input->port->dev->link[input->port->lnr].ids;
	int par = 0, osc24 = 0, tsfast = 0;

	/*
	 * Determine if bridges with stv0910 demods can run with fast TS and
	 * thus support high bandwidth transponders.
	 * STV0910_PR and STV0910_P tuner types covers all relevant bridges,
	 * namely the CineS2 V7(A) and the Octopus CI S2 Pro/Advanced. All
	 * DuoFlex S2 V4(A) have type=DDB_TUNER_DVBS_STV0910 without any suffix
	 * and are limited by the serial link to the bridge, thus won't work
	 * in fast TS mode.
	 */
	if (port->nr == 0 &&
	    (port->type == DDB_TUNER_DVBS_STV0910_PR ||
	     port->type == DDB_TUNER_DVBS_STV0910_P)) {
		/* fast TS on port 0 requires FPGA version >= 1.7 */
		if ((devids->hwid & 0x00ffffff) >= 0x00010007)
			tsfast = 1;
	}
1475 1476 1477 1478 1479 1480 1481 1482

	dvb->attached = 0x01;

	dvbdemux->priv = input;
	dvbdemux->dmx.capabilities = DMX_TS_FILTERING |
		DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
	dvbdemux->start_feed = start_feed;
	dvbdemux->stop_feed = stop_feed;
1483 1484
	dvbdemux->filternum = 256;
	dvbdemux->feednum = 256;
1485 1486
	ret = dvb_dmx_init(dvbdemux);
	if (ret < 0)
1487
		return ret;
1488
	dvb->attached = 0x10;
1489

1490 1491 1492
	dvb->dmxdev.filternum = 256;
	dvb->dmxdev.demux = &dvbdemux->dmx;
	ret = dvb_dmxdev_init(&dvb->dmxdev, adap);
1493
	if (ret < 0)
1494
		goto err_detach;
1495
	dvb->attached = 0x11;
1496

1497 1498 1499 1500 1501
	dvb->mem_frontend.source = DMX_MEMORY_FE;
	dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->mem_frontend);
	dvb->hw_frontend.source = DMX_FRONTEND_0;
	dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->hw_frontend);
	ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &dvb->hw_frontend);
1502
	if (ret < 0)
1503
		goto err_detach;
1504
	dvb->attached = 0x12;
1505

1506
	ret = dvb_net_init(adap, &dvb->dvbnet, dvb->dmxdev.demux);
1507
	if (ret < 0)
1508
		goto err_detach;
1509
	dvb->attached = 0x20;
1510

1511 1512
	dvb->fe = NULL;
	dvb->fe2 = NULL;
1513
	switch (port->type) {
1514
	case DDB_TUNER_MXL5XX:
1515
		if (ddb_fe_attach_mxl5xx(input) < 0)
1516
			goto err_detach;
1517
		break;
1518 1519
	case DDB_TUNER_DVBS_ST:
		if (demod_attach_stv0900(input, 0) < 0)
1520
			goto err_detach;
1521
		if (tuner_attach_stv6110(input, 0) < 0)
1522
			goto err_tuner;
1523 1524 1525
		break;
	case DDB_TUNER_DVBS_ST_AA:
		if (demod_attach_stv0900(input, 1) < 0)
1526
			goto err_detach;
1527
		if (tuner_attach_stv6110(input, 1) < 0)
1528
			goto err_tuner;
1529
		break;
1530
	case DDB_TUNER_DVBS_STV0910:
1531
		if (demod_attach_stv0910(input, 0, tsfast) < 0)
1532
			goto err_detach;
1533
		if (tuner_attach_stv6111(input, 0) < 0)
1534
			goto err_tuner;
1535 1536
		break;
	case DDB_TUNER_DVBS_STV0910_PR:
1537
		if (demod_attach_stv0910(input, 1, tsfast) < 0)
1538
			goto err_detach;
1539
		if (tuner_attach_stv6111(input, 1) < 0)
1540
			goto err_tuner;
1541 1542
		break;
	case DDB_TUNER_DVBS_STV0910_P:
1543
		if (demod_attach_stv0910(input, 0, tsfast) < 0)
1544
			goto err_detach;
1545
		if (tuner_attach_stv6111(input, 1) < 0)
1546
			goto err_tuner;
1547
		break;
1548 1549
	case DDB_TUNER_DVBCT_TR:
		if (demod_attach_drxk(input) < 0)
1550
			goto err_detach;
1551
		if (tuner_attach_tda18271(input) < 0)
1552
			goto err_tuner;
1553
		break;
1554 1555
	case DDB_TUNER_DVBCT_ST:
		if (demod_attach_stv0367(input) < 0)
1556
			goto err_detach;
1557 1558
		if (tuner_attach_tda18212(input, port->type) < 0)
			goto err_tuner;
1559
		break;
1560
	case DDB_TUNER_DVBC2T2I_SONY_P:
1561 1562 1563 1564 1565
		if (input->port->dev->link[input->port->lnr].info->ts_quirks &
		    TS_QUIRK_ALT_OSC)
			osc24 = 0;
		else
			osc24 = 1;
1566
		/* fall-through */
1567 1568 1569
	case DDB_TUNER_DVBCT2_SONY_P:
	case DDB_TUNER_DVBC2T2_SONY_P:
	case DDB_TUNER_ISDBT_SONY_P:
1570 1571 1572
		if (input->port->dev->link[input->port->lnr].info->ts_quirks
			& TS_QUIRK_SERIAL)
			par = 0;
1573
		else
1574 1575
			par = 1;
		if (demod_attach_cxd28xx(input, par, osc24) < 0)
1576
			goto err_detach;
1577 1578
		if (tuner_attach_tda18212(input, port->type) < 0)
			goto err_tuner;
1579
		break;
1580 1581
	case DDB_TUNER_DVBC2T2I_SONY:
		osc24 = 1;
1582
		/* fall-through */
1583 1584 1585 1586
	case DDB_TUNER_DVBCT2_SONY:
	case DDB_TUNER_DVBC2T2_SONY:
	case DDB_TUNER_ISDBT_SONY:
		if (demod_attach_cxd28xx(input, 0, osc24) < 0)
1587
			goto err_detach;
1588 1589
		if (tuner_attach_tda18212(input, port->type) < 0)
			goto err_tuner;
1590
		break;
1591 1592 1593 1594
	case DDB_TUNER_DUMMY:
		if (demod_attach_dummy(input) < 0)
			goto err_detach;
		break;
1595 1596 1597 1598
	case DDB_TUNER_MCI:
		if (ddb_fe_attach_mci(input) < 0)
			goto err_detach;
		break;
1599 1600
	default:
		return 0;
1601
	}
1602
	dvb->attached = 0x30;
1603

1604 1605
	if (dvb->fe) {
		if (dvb_register_frontend(adap, dvb->fe) < 0)
1606
			goto err_detach;
1607 1608

		if (dvb->fe2) {
1609 1610 1611 1612
			if (dvb_register_frontend(adap, dvb->fe2) < 0) {
				dvb_unregister_frontend(dvb->fe);
				goto err_detach;
			}
1613 1614 1615 1616 1617
			dvb->fe2->tuner_priv = dvb->fe->tuner_priv;
			memcpy(&dvb->fe2->ops.tuner_ops,
			       &dvb->fe->ops.tuner_ops,
			       sizeof(struct dvb_tuner_ops));
		}
1618
	}
1619

1620
	dvb->attached = 0x31;
1621
	return 0;
1622 1623

err_tuner:
1624
	dev_err(port->dev->dev, "tuner attach failed!\n");
1625 1626 1627 1628 1629

	if (dvb->fe2)
		dvb_frontend_detach(dvb->fe2);
	if (dvb->fe)
		dvb_frontend_detach(dvb->fe);
1630 1631 1632 1633 1634 1635
err_detach:
	dvb_input_detach(input);

	/* return error from ret if set */
	if (ret < 0)
		return ret;
1636 1637

	return -ENODEV;
1638 1639
}

1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
static int port_has_encti(struct ddb_port *port)
{
	struct device *dev = port->dev->dev;
	u8 val;
	int ret = i2c_read_reg(&port->i2c->adap, 0x20, 0, &val);

	if (!ret)
		dev_info(dev, "[0x20]=0x%02x\n", val);
	return ret ? 0 : 1;
}

static int port_has_cxd(struct ddb_port *port, u8 *type)
1652
{
1653
	u8 val;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	u8 probe[4] = { 0xe0, 0x00, 0x00, 0x00 }, data[4];
	struct i2c_msg msgs[2] = {{ .addr = 0x40,  .flags = 0,
				    .buf  = probe, .len   = 4 },
				  { .addr = 0x40,  .flags = I2C_M_RD,
				    .buf  = data,  .len   = 4 } };
	val = i2c_transfer(&port->i2c->adap, msgs, 2);
	if (val != 2)
		return 0;

	if (data[0] == 0x02 && data[1] == 0x2b && data[3] == 0x43)
		*type = 2;
	else
		*type = 1;
	return 1;
1668 1669
}

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

1674 1675 1676 1677
	if (i2c_io(&port->i2c->adap, 0x10, probe, 1, data, 4))
		return 0;
	if (data[0] == 'D' && data[1] == 'F') {
		*id = data[2];
1678
		*type = 1;
1679
		return 1;
1680
	}
1681 1682
	if (data[0] == 'C' && data[1] == 'I') {
		*id = data[2];
1683
		*type = 2;
1684 1685 1686
		return 1;
	}
	return 0;
1687 1688
}

1689
static int port_has_stv0900(struct ddb_port *port)
1690
{
1691
	u8 val;
1692

1693 1694 1695
	if (i2c_read_reg16(&port->i2c->adap, 0x69, 0xf100, &val) < 0)
		return 0;
	return 1;
1696 1697
}

1698
static int port_has_stv0900_aa(struct ddb_port *port, u8 *id)
1699
{
1700 1701 1702
	if (i2c_read_reg16(&port->i2c->adap, 0x68, 0xf100, id) < 0)
		return 0;
	return 1;
1703 1704
}

1705
static int port_has_drxks(struct ddb_port *port)
1706
{
1707
	u8 val;
1708

1709 1710 1711 1712 1713
	if (i2c_read(&port->i2c->adap, 0x29, &val) < 0)
		return 0;
	if (i2c_read(&port->i2c->adap, 0x2a, &val) < 0)
		return 0;
	return 1;
1714 1715
}

1716
static int port_has_stv0367(struct ddb_port *port)
1717
{
1718
	u8 val;
1719

1720 1721 1722 1723 1724 1725 1726 1727 1728
	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;
1729 1730
}

1731 1732 1733
static int init_xo2(struct ddb_port *port)
{
	struct i2c_adapter *i2c = &port->i2c->adap;
1734
	struct ddb *dev = port->dev;
1735 1736 1737 1738 1739 1740 1741 1742
	u8 val, data[2];
	int res;

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

	if (data[0] != 0x01)  {
1743
		dev_info(dev->dev, "Port %d: invalid XO2\n", port->nr);
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
		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 */
1759
	i2c_write_reg(i2c, 0x10, 0x09, xo2_speed);
1760

1761 1762 1763 1764 1765 1766 1767 1768
	if (dev->link[port->lnr].info->con_clock) {
		dev_info(dev->dev, "Setting continuous clock for XO2\n");
		i2c_write_reg(i2c, 0x10, 0x0a, 0x03);
		i2c_write_reg(i2c, 0x10, 0x0b, 0x03);
	} else {
		i2c_write_reg(i2c, 0x10, 0x0a, 0x01);
		i2c_write_reg(i2c, 0x10, 0x0b, 0x01);
	}
1769 1770 1771 1772 1773 1774 1775 1776

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

	return 0;
}

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
static int init_xo2_ci(struct ddb_port *port)
{
	struct i2c_adapter *i2c = &port->i2c->adap;
	struct ddb *dev = port->dev;
	u8 val, data[2];
	int res;

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

	if (data[0] > 1)  {
		dev_info(dev->dev, "Port %d: invalid XO2 CI %02x\n",
1790
			 port->nr, data[0]);
1791 1792 1793
		return -1;
	}
	dev_info(dev->dev, "Port %d: DuoFlex CI %u.%u\n",
1794
		 port->nr, data[0], data[1]);
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821

	i2c_read_reg(i2c, 0x10, 0x08, &val);
	if (val != 0) {
		i2c_write_reg(i2c, 0x10, 0x08, 0x00);
		msleep(100);
	}
	/* Enable both CI */
	i2c_write_reg(i2c, 0x10, 0x08, 3);
	usleep_range(2000, 3000);

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

	i2c_write_reg(i2c, 0x10, 0x08, 0x83);
	usleep_range(2000, 3000);

	if (dev->link[port->lnr].info->con_clock) {
		dev_info(dev->dev, "Setting continuous clock for DuoFlex CI\n");
		i2c_write_reg(i2c, 0x10, 0x0a, 0x03);
		i2c_write_reg(i2c, 0x10, 0x0b, 0x03);
	} else {
		i2c_write_reg(i2c, 0x10, 0x0a, 0x01);
		i2c_write_reg(i2c, 0x10, 0x0b, 0x01);
	}
	return 0;
}

1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
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;
}

1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
static char *xo2names[] = {
	"DUAL DVB-S2", "DUAL DVB-C/T/T2",
	"DUAL DVB-ISDBT", "DUAL DVB-C/C2/T/T2",
	"DUAL ATSC", "DUAL DVB-C/C2/T/T2,ISDB-T",
	"", ""
};

static char *xo2types[] = {
	"DVBS_ST", "DVBCT2_SONY",
	"ISDBT_SONY", "DVBC2T2_SONY",
	"ATSC_ST", "DVBC2T2I_SONY"
};

1849 1850 1851
static void ddb_port_probe(struct ddb_port *port)
{
	struct ddb *dev = port->dev;
1852
	u32 l = port->lnr;
1853
	struct ddb_link *link = &dev->link[l];
1854
	u8 id, type;
1855

1856 1857
	port->name = "NO MODULE";
	port->type_name = "NONE";
1858 1859
	port->class = DDB_PORT_NONE;

1860 1861
	/* Handle missing ports and ports without I2C */

1862
	if (dummy_tuner && !port->nr &&
1863
	    link->ids.device == 0x0005) {
1864 1865 1866 1867 1868 1869 1870
		port->name = "DUMMY";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DUMMY;
		port->type_name = "DUMMY";
		return;
	}

1871 1872 1873 1874 1875 1876
	if (port->nr == ts_loop) {
		port->name = "TS LOOP";
		port->class = DDB_PORT_LOOP;
		return;
	}

1877 1878
	if (port->nr == 1 && link->info->type == DDB_OCTOPUS_CI &&
	    link->info->i2c_mask == 1) {
1879 1880 1881 1882 1883
		port->name = "NO TAB";
		port->class = DDB_PORT_NONE;
		return;
	}

1884
	if (link->info->type == DDB_OCTOPUS_MAX) {
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
		port->name = "DUAL DVB-S2 MAX";
		port->type_name = "MXL5XX";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_MXL5XX;
		if (port->i2c)
			ddbwritel(dev, I2C_SPEED_400,
				  port->i2c->regs + I2C_TIMING);
		return;
	}

1895 1896
	if (link->info->type == DDB_OCTOPUS_MCI) {
		if (port->nr >= link->info->mci)
1897 1898 1899 1900 1901 1902 1903 1904
			return;
		port->name = "DUAL MCI";
		port->type_name = "MCI";
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_MCI;
		return;
	}

1905
	if (port->nr > 1 && link->info->type == DDB_OCTOPUS_CI) {
1906 1907
		port->name = "CI internal";
		port->type_name = "INTERNAL";
1908
		port->class = DDB_PORT_CI;
1909 1910
		port->type = DDB_CI_INTERNAL;
	}
1911

1912 1913
	if (!port->i2c)
		return;
1914

1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
	/* Probe ports with I2C */

	if (port_has_cxd(port, &id)) {
		if (id == 1) {
			port->name = "CI";
			port->type_name = "CXD2099";
			port->class = DDB_PORT_CI;
			port->type = DDB_CI_EXTERNAL_SONY;
			ddbwritel(dev, I2C_SPEED_400,
				  port->i2c->regs + I2C_TIMING);
		} else {
			dev_info(dev->dev, "Port %d: Uninitialized DuoFlex\n",
1927
				 port->nr);
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
			return;
		}
	} else if (port_has_xo2(port, &type, &id)) {
		ddbwritel(dev, I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
		/*dev_info(dev->dev, "XO2 ID %02x\n", id);*/
		if (type == 2) {
			port->name = "DuoFlex CI";
			port->class = DDB_PORT_CI;
			port->type = DDB_CI_EXTERNAL_XO2;
			port->type_name = "CI_XO2";
			init_xo2_ci(port);
			return;
		}
		id >>= 2;
		if (id > 5) {
			port->name = "unknown XO2 DuoFlex";
			port->type_name = "UNKNOWN";
		} else {
			port->name = xo2names[id];
			port->class = DDB_PORT_TUNER;
			port->type = DDB_TUNER_XO2 + id;
			port->type_name = xo2types[id];
1950 1951
			init_xo2(port);
		}
1952 1953
	} else if (port_has_cxd28xx(port, &id)) {
		switch (id) {
1954
		case 0xa4:
1955
			port->name = "DUAL DVB-C2T2 CXD2843";
1956
			port->type = DDB_TUNER_DVBC2T2_SONY_P;
1957
			port->type_name = "DVBC2T2_SONY";
1958 1959
			break;
		case 0xb1:
1960
			port->name = "DUAL DVB-CT2 CXD2837";
1961
			port->type = DDB_TUNER_DVBCT2_SONY_P;
1962
			port->type_name = "DVBCT2_SONY";
1963 1964
			break;
		case 0xb0:
1965
			port->name = "DUAL ISDB-T CXD2838";
1966
			port->type = DDB_TUNER_ISDBT_SONY_P;
1967
			port->type_name = "ISDBT_SONY";
1968 1969
			break;
		case 0xc1:
1970
			port->name = "DUAL DVB-C2T2 ISDB-T CXD2854";
1971
			port->type = DDB_TUNER_DVBC2T2I_SONY_P;
1972
			port->type_name = "DVBC2T2I_ISDBT_SONY";
1973 1974
			break;
		default:
1975
			return;
1976
		}
1977 1978
		port->class = DDB_PORT_TUNER;
		ddbwritel(dev, I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
1979
	} else if (port_has_stv0900(port)) {
1980
		port->name = "DUAL DVB-S2";
1981 1982
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBS_ST;
1983 1984 1985 1986
		port->type_name = "DVBS_ST";
		ddbwritel(dev, I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
	} else if (port_has_stv0900_aa(port, &id)) {
		port->name = "DUAL DVB-S2";
1987
		port->class = DDB_PORT_TUNER;
1988 1989
		if (id == 0x51) {
			if (port->nr == 0 &&
1990
			    link->info->ts_quirks & TS_QUIRK_REVERSED)
1991 1992 1993
				port->type = DDB_TUNER_DVBS_STV0910_PR;
			else
				port->type = DDB_TUNER_DVBS_STV0910_P;
1994 1995
			port->type_name = "DVBS_ST_0910";
		} else {
1996
			port->type = DDB_TUNER_DVBS_ST_AA;
1997
			port->type_name = "DVBS_ST_AA";
1998
		}
1999
		ddbwritel(dev, I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
2000
	} else if (port_has_drxks(port)) {
2001
		port->name = "DUAL DVB-C/T";
2002 2003
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBCT_TR;
2004 2005
		port->type_name = "DVBCT_TR";
		ddbwritel(dev, I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
2006
	} else if (port_has_stv0367(port)) {
2007
		port->name = "DUAL DVB-C/T";
2008 2009
		port->class = DDB_PORT_TUNER;
		port->type = DDB_TUNER_DVBCT_ST;
2010 2011 2012 2013 2014
		port->type_name = "DVBCT_ST";
		ddbwritel(dev, I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
	} else if (port_has_encti(port)) {
		port->name = "ENCTI";
		port->class = DDB_PORT_LOOP;
2015
	}
2016 2017 2018 2019 2020 2021
}

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

2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
static int ddb_port_attach(struct ddb_port *port)
{
	int ret = 0;

	switch (port->class) {
	case DDB_PORT_TUNER:
		ret = dvb_input_attach(port->input[0]);
		if (ret < 0)
			break;
		ret = dvb_input_attach(port->input[1]);
2032 2033
		if (ret < 0) {
			dvb_input_detach(port->input[0]);
2034
			break;
2035
		}
2036 2037
		port->input[0]->redi = port->input[0];
		port->input[1]->redi = port->input[1];
2038 2039
		break;
	case DDB_PORT_CI:
2040
		ret = ddb_ci_attach(port, ci_bitrate);
2041 2042
		if (ret < 0)
			break;
2043
		/* fall-through */
2044 2045 2046
	case DDB_PORT_LOOP:
		ret = dvb_register_device(port->dvb[0].adap,
					  &port->dvb[0].dev,
2047
					  &dvbdev_ci, (void *)port->output,
2048
					  DVB_DEVICE_SEC, 0);
2049 2050 2051 2052 2053
		break;
	default:
		break;
	}
	if (ret < 0)
2054 2055
		dev_err(port->dev->dev, "port_attach on port %d failed\n",
			port->nr);
2056 2057 2058
	return ret;
}

2059
int ddb_ports_attach(struct ddb *dev)
2060
{
2061
	int i, numports, err_ports = 0, ret = 0;
2062 2063
	struct ddb_port *port;

2064 2065 2066 2067 2068 2069 2070
	if (dev->port_num) {
		ret = dvb_register_adapters(dev);
		if (ret < 0) {
			dev_err(dev->dev, "Registering adapters failed. Check DVB_MAX_ADAPTERS in config.\n");
			return ret;
		}
	}
2071 2072 2073

	numports = dev->port_num;

2074
	for (i = 0; i < dev->port_num; i++) {
2075
		port = &dev->port[i];
2076 2077 2078 2079 2080 2081 2082
		if (port->class != DDB_PORT_NONE) {
			ret = ddb_port_attach(port);
			if (ret)
				err_ports++;
		} else {
			numports--;
		}
2083
	}
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095

	if (err_ports) {
		if (err_ports == numports) {
			dev_err(dev->dev, "All connected ports failed to initialise!\n");
			return -ENODEV;
		}

		dev_warn(dev->dev, "%d of %d connected ports failed to initialise!\n",
			 err_ports, numports);
	}

	return 0;
2096 2097
}

2098
void ddb_ports_detach(struct ddb *dev)
2099 2100 2101 2102
{
	int i;
	struct ddb_port *port;

2103
	for (i = 0; i < dev->port_num; i++) {
2104
		port = &dev->port[i];
2105

2106 2107 2108
		switch (port->class) {
		case DDB_PORT_TUNER:
			dvb_input_detach(port->input[1]);
2109
			dvb_input_detach(port->input[0]);
2110 2111
			break;
		case DDB_PORT_CI:
2112
		case DDB_PORT_LOOP:
2113
			ddb_ci_detach(port);
2114 2115 2116
			break;
		}
	}
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	dvb_unregister_adapters(dev);
}

/* Copy input DMA pointers to output DMA and ACK. */

static void input_write_output(struct ddb_input *input,
			       struct ddb_output *output)
{
	ddbwritel(output->port->dev,
		  input->dma->stat, DMA_BUFFER_ACK(output->dma));
	output->dma->cbuf = (input->dma->stat >> 11) & 0x1f;
	output->dma->coff = (input->dma->stat & 0x7ff) << 7;
2129 2130
}

2131 2132
static void output_ack_input(struct ddb_output *output,
			     struct ddb_input *input)
2133
{
2134 2135 2136 2137 2138 2139 2140 2141 2142
	ddbwritel(input->port->dev,
		  output->dma->stat, DMA_BUFFER_ACK(input->dma));
}

static void input_write_dvb(struct ddb_input *input,
			    struct ddb_input *input2)
{
	struct ddb_dvb *dvb = &input2->port->dvb[input2->nr & 1];
	struct ddb_dma *dma, *dma2;
2143
	struct ddb *dev = input->port->dev;
2144
	int ack = 1;
2145

2146 2147 2148 2149
	dma = input->dma;
	dma2 = input->dma;
	/*
	 * if there also is an output connected, do not ACK.
2150 2151 2152 2153 2154 2155
	 * input_write_output will ACK.
	 */
	if (input->redo) {
		dma2 = input->redo->dma;
		ack = 0;
	}
2156 2157
	while (dma->cbuf != ((dma->stat >> 11) & 0x1f) ||
	       (4 & dma->ctrl)) {
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
		if (4 & dma->ctrl) {
			/* dev_err(dev->dev, "Overflow dma %d\n", dma->nr); */
			ack = 1;
		}
		if (alt_dma)
			dma_sync_single_for_cpu(dev->dev, dma2->pbuf[dma->cbuf],
						dma2->size, DMA_FROM_DEVICE);
		dvb_dmx_swfilter_packets(&dvb->demux,
					 dma2->vbuf[dma->cbuf],
					 dma2->size / 188);
		dma->cbuf = (dma->cbuf + 1) % dma2->num;
		if (ack)
			ddbwritel(dev, (dma->cbuf << 11),
				  DMA_BUFFER_ACK(dma));
		dma->stat = safe_ddbreadl(dev, DMA_BUFFER_CURRENT(dma));
		dma->ctrl = safe_ddbreadl(dev, DMA_BUFFER_CONTROL(dma));
2174
	}
2175
}
2176

2177 2178 2179
static void input_work(struct work_struct *work)
{
	struct ddb_dma *dma = container_of(work, struct ddb_dma, work);
2180
	struct ddb_input *input = (struct ddb_input *)dma->io;
2181 2182
	struct ddb *dev = input->port->dev;
	unsigned long flags;
2183

2184 2185 2186 2187
	spin_lock_irqsave(&dma->lock, flags);
	if (!dma->running) {
		spin_unlock_irqrestore(&dma->lock, flags);
		return;
2188
	}
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
	dma->stat = ddbreadl(dev, DMA_BUFFER_CURRENT(dma));
	dma->ctrl = ddbreadl(dev, DMA_BUFFER_CONTROL(dma));

	if (input->redi)
		input_write_dvb(input, input->redi);
	if (input->redo)
		input_write_output(input, input->redo);
	wake_up(&dma->wq);
	spin_unlock_irqrestore(&dma->lock, flags);
}

2200
static void input_handler(void *data)
2201
{
2202
	struct ddb_input *input = (struct ddb_input *)data;
2203 2204
	struct ddb_dma *dma = input->dma;

2205
	queue_work(ddb_wq, &dma->work);
2206 2207
}

2208
static void output_work(struct work_struct *work)
2209
{
2210 2211
	struct ddb_dma *dma = container_of(work, struct ddb_dma, work);
	struct ddb_output *output = (struct ddb_output *)dma->io;
2212
	struct ddb *dev = output->port->dev;
2213
	unsigned long flags;
2214

2215 2216 2217
	spin_lock_irqsave(&dma->lock, flags);
	if (!dma->running)
		goto unlock_exit;
2218 2219 2220 2221 2222
	dma->stat = ddbreadl(dev, DMA_BUFFER_CURRENT(dma));
	dma->ctrl = ddbreadl(dev, DMA_BUFFER_CONTROL(dma));
	if (output->redi)
		output_ack_input(output, output->redi);
	wake_up(&dma->wq);
2223 2224
unlock_exit:
	spin_unlock_irqrestore(&dma->lock, flags);
2225 2226
}

2227 2228 2229 2230 2231 2232 2233 2234
static void output_handler(void *data)
{
	struct ddb_output *output = (struct ddb_output *)data;
	struct ddb_dma *dma = output->dma;

	queue_work(ddb_wq, &dma->work);
}

2235 2236 2237
/****************************************************************************/
/****************************************************************************/

2238
static const struct ddb_regmap *io_regmap(struct ddb_io *io, int link)
2239
{
2240
	const struct ddb_info *info;
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255

	if (link)
		info = io->port->dev->link[io->port->lnr].info;
	else
		info = io->port->dev->link[0].info;

	if (!info)
		return NULL;

	return info->regmap;
}

static void ddb_dma_init(struct ddb_io *io, int nr, int out)
{
	struct ddb_dma *dma;
2256
	const struct ddb_regmap *rm = io_regmap(io, 0);
2257 2258 2259 2260 2261 2262 2263 2264

	dma = out ? &io->port->dev->odma[nr] : &io->port->dev->idma[nr];
	io->dma = dma;
	dma->io = io;

	spin_lock_init(&dma->lock);
	init_waitqueue_head(&dma->wq);
	if (out) {
2265
		INIT_WORK(&dma->work, output_work);
2266 2267
		dma->regs = rm->odma->base + rm->odma->size * nr;
		dma->bufregs = rm->odma_buf->base + rm->odma_buf->size * nr;
2268 2269 2270
		dma->num = dma_buf_num;
		dma->size = dma_buf_size * 128 * 47;
		dma->div = 1;
2271 2272 2273 2274
	} else {
		INIT_WORK(&dma->work, input_work);
		dma->regs = rm->idma->base + rm->idma->size * nr;
		dma->bufregs = rm->idma_buf->base + rm->idma_buf->size * nr;
2275 2276 2277
		dma->num = dma_buf_num;
		dma->size = dma_buf_size * 128 * 47;
		dma->div = 1;
2278 2279 2280 2281 2282 2283 2284
	}
	ddbwritel(io->port->dev, 0, DMA_BUFFER_ACK(dma));
	dev_dbg(io->port->dev->dev, "init link %u, io %u, dma %u, dmaregs %08x bufregs %08x\n",
		io->port->lnr, io->nr, nr, dma->regs, dma->bufregs);
}

static void ddb_input_init(struct ddb_port *port, int nr, int pnr, int anr)
2285 2286
{
	struct ddb *dev = port->dev;
2287
	struct ddb_input *input = &dev->input[anr];
2288
	const struct ddb_regmap *rm;
2289

2290
	port->input[pnr] = input;
2291 2292
	input->nr = nr;
	input->port = port;
2293 2294 2295 2296 2297 2298 2299
	rm = io_regmap(input, 1);
	input->regs = DDB_LINK_TAG(port->lnr) |
		(rm->input->base + rm->input->size * nr);
	dev_dbg(dev->dev, "init link %u, input %u, regs %08x\n",
		port->lnr, nr, input->regs);

	if (dev->has_dma) {
2300
		const struct ddb_regmap *rm0 = io_regmap(input, 0);
2301 2302 2303 2304 2305 2306 2307
		u32 base = rm0->irq_base_idma;
		u32 dma_nr = nr;

		if (port->lnr)
			dma_nr += 32 + (port->lnr - 1) * 8;

		dev_dbg(dev->dev, "init link %u, input %u, handler %u\n",
2308
			port->lnr, nr, dma_nr + base);
2309

2310
		ddb_irq_set(dev, 0, dma_nr + base, &input_handler, input);
2311 2312
		ddb_dma_init(input, dma_nr, 0);
	}
2313 2314 2315 2316 2317 2318
}

static void ddb_output_init(struct ddb_port *port, int nr)
{
	struct ddb *dev = port->dev;
	struct ddb_output *output = &dev->output[nr];
2319
	const struct ddb_regmap *rm;
2320 2321

	port->output = output;
2322 2323
	output->nr = nr;
	output->port = port;
2324 2325 2326 2327 2328
	rm = io_regmap(output, 1);
	output->regs = DDB_LINK_TAG(port->lnr) |
		(rm->output->base + rm->output->size * nr);

	dev_dbg(dev->dev, "init link %u, output %u, regs %08x\n",
2329
		port->lnr, nr, output->regs);
2330

2331
	if (dev->has_dma) {
2332
		const struct ddb_regmap *rm0 = io_regmap(output, 0);
2333 2334
		u32 base = rm0->irq_base_odma;

2335
		ddb_irq_set(dev, 0, nr + base, &output_handler, output);
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
		ddb_dma_init(output, nr, 1);
	}
}

static int ddb_port_match_i2c(struct ddb_port *port)
{
	struct ddb *dev = port->dev;
	u32 i;

	for (i = 0; i < dev->i2c_num; i++) {
		if (dev->i2c[i].link == port->lnr &&
		    dev->i2c[i].nr == port->nr) {
			port->i2c = &dev->i2c[i];
			return 1;
		}
	}
	return 0;
2353 2354
}

2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
static int ddb_port_match_link_i2c(struct ddb_port *port)
{
	struct ddb *dev = port->dev;
	u32 i;

	for (i = 0; i < dev->i2c_num; i++) {
		if (dev->i2c[i].link == port->lnr) {
			port->i2c = &dev->i2c[i];
			return 1;
		}
	}
	return 0;
}

2369
void ddb_ports_init(struct ddb *dev)
2370
{
2371
	u32 i, l, p;
2372
	struct ddb_port *port;
2373
	const struct ddb_info *info;
2374
	const struct ddb_regmap *rm;
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392

	for (p = l = 0; l < DDB_MAX_LINK; l++) {
		info = dev->link[l].info;
		if (!info)
			continue;
		rm = info->regmap;
		if (!rm)
			continue;
		for (i = 0; i < info->port_num; i++, p++) {
			port = &dev->port[p];
			port->dev = dev;
			port->nr = i;
			port->lnr = l;
			port->pnr = p;
			port->gap = 0xffffffff;
			port->obr = ci_bitrate;
			mutex_init(&port->i2c_gate_lock);

2393 2394 2395 2396 2397
			if (!ddb_port_match_i2c(port)) {
				if (info->type == DDB_OCTOPUS_MAX)
					ddb_port_match_link_i2c(port);
			}

2398 2399 2400 2401 2402
			ddb_port_probe(port);

			port->dvb[0].adap = &dev->adap[2 * p];
			port->dvb[1].adap = &dev->adap[2 * p + 1];

2403
			if (port->class == DDB_PORT_NONE && i && p &&
2404 2405 2406 2407 2408 2409
			    dev->port[p - 1].type == DDB_CI_EXTERNAL_XO2) {
				port->class = DDB_PORT_CI;
				port->type = DDB_CI_EXTERNAL_XO2_B;
				port->name = "DuoFlex CI_B";
				port->i2c = dev->port[p - 1].i2c;
			}
2410

2411
			dev_info(dev->dev, "Port %u: Link %u, Link Port %u (TAB %u): %s\n",
2412 2413
				 port->pnr, port->lnr, port->nr, port->nr + 1,
				 port->name);
2414

2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
			if (port->class == DDB_PORT_CI &&
			    port->type == DDB_CI_EXTERNAL_XO2) {
				ddb_input_init(port, 2 * i, 0, 2 * i);
				ddb_output_init(port, i);
				continue;
			}

			if (port->class == DDB_PORT_CI &&
			    port->type == DDB_CI_EXTERNAL_XO2_B) {
				ddb_input_init(port, 2 * i - 1, 0, 2 * i - 1);
				ddb_output_init(port, i);
				continue;
			}

			if (port->class == DDB_PORT_NONE)
				continue;

			switch (dev->link[l].info->type) {
			case DDB_OCTOPUS_CI:
				if (i >= 2) {
					ddb_input_init(port, 2 + i, 0, 2 + i);
					ddb_input_init(port, 4 + i, 1, 4 + i);
					ddb_output_init(port, i);
					break;
				} /* fallthrough */
			case DDB_OCTOPUS:
				ddb_input_init(port, 2 * i, 0, 2 * i);
				ddb_input_init(port, 2 * i + 1, 1, 2 * i + 1);
				ddb_output_init(port, i);
				break;
2445
			case DDB_OCTOPUS_MAX:
2446
			case DDB_OCTOPUS_MAX_CT:
2447
			case DDB_OCTOPUS_MCI:
2448 2449 2450 2451 2452 2453 2454
				ddb_input_init(port, 2 * i, 0, 2 * p);
				ddb_input_init(port, 2 * i + 1, 1, 2 * p + 1);
				break;
			default:
				break;
			}
		}
2455
	}
2456
	dev->port_num = p;
2457 2458
}

2459
void ddb_ports_release(struct ddb *dev)
2460 2461 2462 2463
{
	int i;
	struct ddb_port *port;

2464
	for (i = 0; i < dev->port_num; i++) {
2465
		port = &dev->port[i];
2466 2467 2468 2469 2470 2471
		if (port->input[0] && port->input[0]->dma)
			cancel_work_sync(&port->input[0]->dma->work);
		if (port->input[1] && port->input[1]->dma)
			cancel_work_sync(&port->input[1]->dma->work);
		if (port->output && port->output->dma)
			cancel_work_sync(&port->output->dma->work);
2472 2473 2474 2475 2476 2477 2478
	}
}

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

2479
#define IRQ_HANDLE(_nr) \
2480 2481 2482
	do { if ((s & (1UL << ((_nr) & 0x1f))) && \
		 dev->link[0].irq[_nr].handler) \
		dev->link[0].irq[_nr].handler(dev->link[0].irq[_nr].data); } \
2483 2484
	while (0)

2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
#define IRQ_HANDLE_NIBBLE(_shift) {		     \
	if (s & (0x0000000f << ((_shift) & 0x1f))) { \
		IRQ_HANDLE(0 + (_shift));	     \
		IRQ_HANDLE(1 + (_shift));	     \
		IRQ_HANDLE(2 + (_shift));	     \
		IRQ_HANDLE(3 + (_shift));	     \
	}					     \
}

#define IRQ_HANDLE_BYTE(_shift) {		     \
	if (s & (0x000000ff << ((_shift) & 0x1f))) { \
		IRQ_HANDLE(0 + (_shift));	     \
		IRQ_HANDLE(1 + (_shift));	     \
		IRQ_HANDLE(2 + (_shift));	     \
		IRQ_HANDLE(3 + (_shift));	     \
		IRQ_HANDLE(4 + (_shift));	     \
		IRQ_HANDLE(5 + (_shift));	     \
		IRQ_HANDLE(6 + (_shift));	     \
		IRQ_HANDLE(7 + (_shift));	     \
	}					     \
}

2507
static void irq_handle_msg(struct ddb *dev, u32 s)
2508
{
2509
	dev->i2c_irq++;
2510
	IRQ_HANDLE_NIBBLE(0);
2511
}
2512

2513 2514 2515
static void irq_handle_io(struct ddb *dev, u32 s)
{
	dev->ts_irq++;
2516 2517 2518 2519
	IRQ_HANDLE_NIBBLE(4);
	IRQ_HANDLE_BYTE(8);
	IRQ_HANDLE_BYTE(16);
	IRQ_HANDLE_BYTE(24);
2520 2521
}

2522
irqreturn_t ddb_irq_handler0(int irq, void *dev_id)
2523
{
2524
	struct ddb *dev = (struct ddb *)dev_id;
2525 2526
	u32 mask = 0x8fffff00;
	u32 s = mask & ddbreadl(dev, INTERRUPT_STATUS);
2527

2528 2529
	if (!s)
		return IRQ_NONE;
2530 2531 2532
	do {
		if (s & 0x80000000)
			return IRQ_NONE;
2533
		ddbwritel(dev, s, INTERRUPT_ACK);
2534
		irq_handle_io(dev, s);
2535
	} while ((s = mask & ddbreadl(dev, INTERRUPT_STATUS)));
2536 2537 2538 2539 2540 2541

	return IRQ_HANDLED;
}

irqreturn_t ddb_irq_handler1(int irq, void *dev_id)
{
2542
	struct ddb *dev = (struct ddb *)dev_id;
2543 2544
	u32 mask = 0x8000000f;
	u32 s = mask & ddbreadl(dev, INTERRUPT_STATUS);
2545

2546 2547
	if (!s)
		return IRQ_NONE;
2548
	do {
2549 2550
		if (s & 0x80000000)
			return IRQ_NONE;
2551
		ddbwritel(dev, s, INTERRUPT_ACK);
2552
		irq_handle_msg(dev, s);
2553
	} while ((s = mask & ddbreadl(dev, INTERRUPT_STATUS)));
2554 2555 2556 2557

	return IRQ_HANDLED;
}

2558 2559
irqreturn_t ddb_irq_handler(int irq, void *dev_id)
{
2560
	struct ddb *dev = (struct ddb *)dev_id;
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
	u32 s = ddbreadl(dev, INTERRUPT_STATUS);
	int ret = IRQ_HANDLED;

	if (!s)
		return IRQ_NONE;
	do {
		if (s & 0x80000000)
			return IRQ_NONE;
		ddbwritel(dev, s, INTERRUPT_ACK);

		if (s & 0x0000000f)
			irq_handle_msg(dev, s);
		if (s & 0x0fffff00)
			irq_handle_io(dev, s);
	} while ((s = ddbreadl(dev, INTERRUPT_STATUS)));

	return ret;
}

/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
2583

2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
static int reg_wait(struct ddb *dev, u32 reg, u32 bit)
{
	u32 count = 0;

	while (safe_ddbreadl(dev, reg) & bit) {
		ndelay(10);
		if (++count == 100)
			return -1;
	}
	return 0;
}

static int flashio(struct ddb *dev, u32 lnr, u8 *wbuf, u32 wlen, u8 *rbuf,
2597
		   u32 rlen)
2598 2599
{
	u32 data, shift;
2600 2601
	u32 tag = DDB_LINK_TAG(lnr);
	struct ddb_link *link = &dev->link[lnr];
2602

2603
	mutex_lock(&link->flash_mutex);
2604
	if (wlen > 4)
2605
		ddbwritel(dev, 1, tag | SPI_CONTROL);
2606 2607
	while (wlen > 4) {
		/* FIXME: check for big-endian */
2608
		data = swab32(*(u32 *)wbuf);
2609 2610
		wbuf += 4;
		wlen -= 4;
2611 2612 2613
		ddbwritel(dev, data, tag | SPI_DATA);
		if (reg_wait(dev, tag | SPI_CONTROL, 4))
			goto fail;
2614 2615
	}
	if (rlen)
2616 2617
		ddbwritel(dev, 0x0001 | ((wlen << (8 + 3)) & 0x1f00),
			  tag | SPI_CONTROL);
2618
	else
2619 2620
		ddbwritel(dev, 0x0003 | ((wlen << (8 + 3)) & 0x1f00),
			  tag | SPI_CONTROL);
2621

2622
	data = 0;
2623 2624 2625 2626 2627 2628 2629 2630 2631
	shift = ((4 - wlen) * 8);
	while (wlen) {
		data <<= 8;
		data |= *wbuf;
		wlen--;
		wbuf++;
	}
	if (shift)
		data <<= shift;
2632 2633 2634
	ddbwritel(dev, data, tag | SPI_DATA);
	if (reg_wait(dev, tag | SPI_CONTROL, 4))
		goto fail;
2635 2636

	if (!rlen) {
2637 2638
		ddbwritel(dev, 0, tag | SPI_CONTROL);
		goto exit;
2639 2640
	}
	if (rlen > 4)
2641
		ddbwritel(dev, 1, tag | SPI_CONTROL);
2642 2643

	while (rlen > 4) {
2644 2645 2646 2647
		ddbwritel(dev, 0xffffffff, tag | SPI_DATA);
		if (reg_wait(dev, tag | SPI_CONTROL, 4))
			goto fail;
		data = ddbreadl(dev, tag | SPI_DATA);
2648
		*(u32 *)rbuf = swab32(data);
2649 2650 2651
		rbuf += 4;
		rlen -= 4;
	}
2652
	ddbwritel(dev, 0x0003 | ((rlen << (8 + 3)) & 0x1F00),
2653
		  tag | SPI_CONTROL);
2654 2655 2656
	ddbwritel(dev, 0xffffffff, tag | SPI_DATA);
	if (reg_wait(dev, tag | SPI_CONTROL, 4))
		goto fail;
2657

2658 2659
	data = ddbreadl(dev, tag | SPI_DATA);
	ddbwritel(dev, 0, tag | SPI_CONTROL);
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669

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

	while (rlen > 0) {
		*rbuf = ((data >> 24) & 0xff);
		data <<= 8;
		rbuf++;
		rlen--;
	}
2670 2671
exit:
	mutex_unlock(&link->flash_mutex);
2672
	return 0;
2673 2674 2675
fail:
	mutex_unlock(&link->flash_mutex);
	return -1;
2676 2677
}

2678 2679 2680 2681
int ddbridge_flashread(struct ddb *dev, u32 link, u8 *buf, u32 addr, u32 len)
{
	u8 cmd[4] = {0x03, (addr >> 16) & 0xff,
		     (addr >> 8) & 0xff, addr & 0xff};
2682

2683 2684
	return flashio(dev, link, cmd, 4, buf, len);
}
2685

2686 2687 2688
/*
 * TODO/FIXME: add/implement IOCTLs from upstream driver
 */
2689 2690 2691 2692 2693

#define DDB_NAME "ddbridge"

static u32 ddb_num;
static int ddb_major;
2694 2695 2696 2697 2698 2699 2700 2701 2702
static DEFINE_MUTEX(ddb_mutex);

static int ddb_release(struct inode *inode, struct file *file)
{
	struct ddb *dev = file->private_data;

	dev->ddb_dev_users--;
	return 0;
}
2703 2704 2705 2706 2707

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

2708 2709 2710
	if (dev->ddb_dev_users)
		return -EBUSY;
	dev->ddb_dev_users++;
2711 2712 2713 2714 2715 2716 2717 2718
	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;

2719 2720
	dev_warn(dev->dev, "DDB IOCTLs unsupported (cmd: %d, arg: %lu)\n",
		 cmd, arg);
2721

2722 2723
	return -ENOTTY;
}
2724

2725 2726 2727 2728 2729 2730 2731 2732 2733
static const struct file_operations ddb_fops = {
	.unlocked_ioctl = ddb_ioctl,
	.open           = ddb_open,
	.release        = ddb_release,
};

static char *ddb_devnode(struct device *device, umode_t *mode)
{
	struct ddb *dev = dev_get_drvdata(device);
2734

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

2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
#define __ATTR_MRO(_name, _show) {				\
	.attr	= { .name = __stringify(_name), .mode = 0444 },	\
	.show	= _show,					\
}

#define __ATTR_MWO(_name, _store) {				\
	.attr	= { .name = __stringify(_name), .mode = 0222 },	\
	.store	= _store,					\
}

static ssize_t ports_show(struct device *device,
			  struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "%d\n", dev->port_num);
}

static ssize_t ts_irq_show(struct device *device,
			   struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "%d\n", dev->ts_irq);
}

static ssize_t i2c_irq_show(struct device *device,
			    struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "%d\n", dev->i2c_irq);
}

static ssize_t fan_show(struct device *device,
			struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);
	u32 val;

	val = ddbreadl(dev, GPIO_OUTPUT) & 1;
	return sprintf(buf, "%d\n", val);
}

static ssize_t fan_store(struct device *device, struct device_attribute *d,
			 const char *buf, size_t count)
{
	struct ddb *dev = dev_get_drvdata(device);
	u32 val;

	if (sscanf(buf, "%u\n", &val) != 1)
		return -EINVAL;
	ddbwritel(dev, 1, GPIO_DIRECTION);
	ddbwritel(dev, val & 1, GPIO_OUTPUT);
	return count;
}

static ssize_t fanspeed_show(struct device *device,
2796
			     struct device_attribute *attr, char *buf)
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
{
	struct ddb *dev = dev_get_drvdata(device);
	int num = attr->attr.name[8] - 0x30;
	struct ddb_link *link = &dev->link[num];
	u32 spd;

	spd = ddblreadl(link, TEMPMON_FANCONTROL) & 0xff;
	return sprintf(buf, "%u\n", spd * 100);
}

static ssize_t temp_show(struct device *device,
			 struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);
	struct ddb_link *link = &dev->link[0];
	struct i2c_adapter *adap;
	int temp, temp2;
	u8 tmp[2];

	if (!link->info->temp_num)
		return sprintf(buf, "no sensor\n");
	adap = &dev->i2c[link->info->temp_bus].adap;
	if (i2c_read_regs(adap, 0x48, 0, tmp, 2) < 0)
		return sprintf(buf, "read_error\n");
	temp = (tmp[0] << 3) | (tmp[1] >> 5);
	temp *= 125;
	if (link->info->temp_num == 2) {
		if (i2c_read_regs(adap, 0x49, 0, tmp, 2) < 0)
			return sprintf(buf, "read_error\n");
		temp2 = (tmp[0] << 3) | (tmp[1] >> 5);
		temp2 *= 125;
		return sprintf(buf, "%d %d\n", temp, temp2);
	}
	return sprintf(buf, "%d\n", temp);
}

static ssize_t ctemp_show(struct device *device,
2834
			  struct device_attribute *attr, char *buf)
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
{
	struct ddb *dev = dev_get_drvdata(device);
	struct i2c_adapter *adap;
	int temp;
	u8 tmp[2];
	int num = attr->attr.name[4] - 0x30;

	adap = &dev->i2c[num].adap;
	if (!adap)
		return 0;
	if (i2c_read_regs(adap, 0x49, 0, tmp, 2) < 0)
		if (i2c_read_regs(adap, 0x4d, 0, tmp, 2) < 0)
			return sprintf(buf, "no sensor\n");
	temp = tmp[0] * 1000;
	return sprintf(buf, "%d\n", temp);
}

static ssize_t led_show(struct device *device,
			struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);
	int num = attr->attr.name[3] - 0x30;

	return sprintf(buf, "%d\n", dev->leds & (1 << num) ? 1 : 0);
}

static void ddb_set_led(struct ddb *dev, int num, int val)
{
	if (!dev->link[0].info->led_num)
		return;
	switch (dev->port[num].class) {
	case DDB_PORT_TUNER:
		switch (dev->port[num].type) {
		case DDB_TUNER_DVBS_ST:
			i2c_write_reg16(&dev->i2c[num].adap,
					0x69, 0xf14c, val ? 2 : 0);
			break;
		case DDB_TUNER_DVBCT_ST:
			i2c_write_reg16(&dev->i2c[num].adap,
					0x1f, 0xf00e, 0);
			i2c_write_reg16(&dev->i2c[num].adap,
					0x1f, 0xf00f, val ? 1 : 0);
			break;
		case DDB_TUNER_XO2 ... DDB_TUNER_DVBC2T2I_SONY:
		{
			u8 v;

			i2c_read_reg(&dev->i2c[num].adap, 0x10, 0x08, &v);
			v = (v & ~0x10) | (val ? 0x10 : 0);
			i2c_write_reg(&dev->i2c[num].adap, 0x10, 0x08, v);
			break;
		}
		default:
			break;
		}
2890 2891
		break;
	}
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
}

static ssize_t led_store(struct device *device,
			 struct device_attribute *attr,
			 const char *buf, size_t count)
{
	struct ddb *dev = dev_get_drvdata(device);
	int num = attr->attr.name[3] - 0x30;
	u32 val;

	if (sscanf(buf, "%u\n", &val) != 1)
		return -EINVAL;
	if (val)
		dev->leds |= (1 << num);
	else
		dev->leds &= ~(1 << num);
	ddb_set_led(dev, num, val);
	return count;
}

static ssize_t snr_show(struct device *device,
			struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);
	char snr[32];
	int num = attr->attr.name[3] - 0x30;

	if (dev->port[num].type >= DDB_TUNER_XO2) {
		if (i2c_read_regs(&dev->i2c[num].adap, 0x10, 0x10, snr, 16) < 0)
			return sprintf(buf, "NO SNR\n");
		snr[16] = 0;
	} else {
		/* serial number at 0x100-0x11f */
		if (i2c_read_regs16(&dev->i2c[num].adap,
				    0x57, 0x100, snr, 32) < 0)
			if (i2c_read_regs16(&dev->i2c[num].adap,
					    0x50, 0x100, snr, 32) < 0)
				return sprintf(buf, "NO SNR\n");
		snr[31] = 0; /* in case it is not terminated on EEPROM */
2931
	}
2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
	return sprintf(buf, "%s\n", snr);
}

static ssize_t bsnr_show(struct device *device,
			 struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);
	char snr[16];

	ddbridge_flashread(dev, 0, snr, 0x10, 15);
	snr[15] = 0; /* in case it is not terminated on EEPROM */
	return sprintf(buf, "%s\n", snr);
}

static ssize_t bpsnr_show(struct device *device,
2947
			  struct device_attribute *attr, char *buf)
2948 2949
{
	struct ddb *dev = dev_get_drvdata(device);
2950
	unsigned char snr[32];
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965

	if (!dev->i2c_num)
		return 0;

	if (i2c_read_regs16(&dev->i2c[0].adap,
			    0x50, 0x0000, snr, 32) < 0 ||
	    snr[0] == 0xff)
		return sprintf(buf, "NO SNR\n");
	snr[31] = 0; /* in case it is not terminated on EEPROM */
	return sprintf(buf, "%s\n", snr);
}

static ssize_t redirect_show(struct device *device,
			     struct device_attribute *attr, char *buf)
{
2966
	return 0;
2967 2968
}

2969 2970 2971 2972 2973 2974
static ssize_t redirect_store(struct device *device,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
{
	unsigned int i, p;
	int res;
2975

2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
	if (sscanf(buf, "%x %x\n", &i, &p) != 2)
		return -EINVAL;
	res = ddb_redirect(i, p);
	if (res < 0)
		return res;
	dev_info(device, "redirect: %02x, %02x\n", i, p);
	return count;
}

static ssize_t gap_show(struct device *device,
			struct device_attribute *attr, char *buf)
2987 2988
{
	struct ddb *dev = dev_get_drvdata(device);
2989
	int num = attr->attr.name[3] - 0x30;
2990

2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
	return sprintf(buf, "%d\n", dev->port[num].gap);
}

static ssize_t gap_store(struct device *device, struct device_attribute *attr,
			 const char *buf, size_t count)
{
	struct ddb *dev = dev_get_drvdata(device);
	int num = attr->attr.name[3] - 0x30;
	unsigned int val;

	if (sscanf(buf, "%u\n", &val) != 1)
		return -EINVAL;
3003
	if (val > 128)
3004
		return -EINVAL;
3005 3006
	if (val == 128)
		val = 0xffffffff;
3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
	dev->port[num].gap = val;
	return count;
}

static ssize_t version_show(struct device *device,
			    struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "%08x %08x\n",
		       dev->link[0].ids.hwid, dev->link[0].ids.regmapid);
3018 3019
}

3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
static ssize_t hwid_show(struct device *device,
			 struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "0x%08X\n", dev->link[0].ids.hwid);
}

static ssize_t regmap_show(struct device *device,
			   struct device_attribute *attr, char *buf)
{
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "0x%08X\n", dev->link[0].ids.regmapid);
}

3036
static ssize_t fmode_show(struct device *device,
3037
			  struct device_attribute *attr, char *buf)
3038 3039 3040 3041 3042 3043 3044
{
	int num = attr->attr.name[5] - 0x30;
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "%u\n", dev->link[num].lnb.fmode);
}

3045 3046 3047 3048 3049 3050 3051 3052 3053
static ssize_t devid_show(struct device *device,
			  struct device_attribute *attr, char *buf)
{
	int num = attr->attr.name[5] - 0x30;
	struct ddb *dev = dev_get_drvdata(device);

	return sprintf(buf, "%08x\n", dev->link[num].ids.devid);
}

3054
static ssize_t fmode_store(struct device *device, struct device_attribute *attr,
3055
			   const char *buf, size_t count)
3056 3057 3058 3059 3060 3061 3062 3063 3064
{
	struct ddb *dev = dev_get_drvdata(device);
	int num = attr->attr.name[5] - 0x30;
	unsigned int val;

	if (sscanf(buf, "%u\n", &val) != 1)
		return -EINVAL;
	if (val > 3)
		return -EINVAL;
3065
	ddb_lnb_init_fmode(dev, &dev->link[num], val);
3066 3067 3068
	return count;
}

3069 3070 3071 3072 3073 3074 3075 3076 3077
static struct device_attribute ddb_attrs[] = {
	__ATTR_RO(version),
	__ATTR_RO(ports),
	__ATTR_RO(ts_irq),
	__ATTR_RO(i2c_irq),
	__ATTR(gap0, 0664, gap_show, gap_store),
	__ATTR(gap1, 0664, gap_show, gap_store),
	__ATTR(gap2, 0664, gap_show, gap_store),
	__ATTR(gap3, 0664, gap_show, gap_store),
3078 3079 3080 3081
	__ATTR(fmode0, 0664, fmode_show, fmode_store),
	__ATTR(fmode1, 0664, fmode_show, fmode_store),
	__ATTR(fmode2, 0664, fmode_show, fmode_store),
	__ATTR(fmode3, 0664, fmode_show, fmode_store),
3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
	__ATTR_MRO(devid0, devid_show),
	__ATTR_MRO(devid1, devid_show),
	__ATTR_MRO(devid2, devid_show),
	__ATTR_MRO(devid3, devid_show),
	__ATTR_RO(hwid),
	__ATTR_RO(regmap),
	__ATTR(redirect, 0664, redirect_show, redirect_store),
	__ATTR_MRO(snr,  bsnr_show),
	__ATTR_RO(bpsnr),
	__ATTR_NULL,
};

static struct device_attribute ddb_attrs_temp[] = {
	__ATTR_RO(temp),
};

static struct device_attribute ddb_attrs_fan[] = {
	__ATTR(fan, 0664, fan_show, fan_store),
};

static struct device_attribute ddb_attrs_snr[] = {
3103 3104 3105 3106
	__ATTR_MRO(snr0, snr_show),
	__ATTR_MRO(snr1, snr_show),
	__ATTR_MRO(snr2, snr_show),
	__ATTR_MRO(snr3, snr_show),
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135
};

static struct device_attribute ddb_attrs_ctemp[] = {
	__ATTR_MRO(temp0, ctemp_show),
	__ATTR_MRO(temp1, ctemp_show),
	__ATTR_MRO(temp2, ctemp_show),
	__ATTR_MRO(temp3, ctemp_show),
};

static struct device_attribute ddb_attrs_led[] = {
	__ATTR(led0, 0664, led_show, led_store),
	__ATTR(led1, 0664, led_show, led_store),
	__ATTR(led2, 0664, led_show, led_store),
	__ATTR(led3, 0664, led_show, led_store),
};

static struct device_attribute ddb_attrs_fanspeed[] = {
	__ATTR_MRO(fanspeed0, fanspeed_show),
	__ATTR_MRO(fanspeed1, fanspeed_show),
	__ATTR_MRO(fanspeed2, fanspeed_show),
	__ATTR_MRO(fanspeed3, fanspeed_show),
};

static struct class ddb_class = {
	.name		= "ddbridge",
	.owner          = THIS_MODULE,
	.devnode        = ddb_devnode,
};

3136
static int ddb_class_create(void)
3137
{
3138 3139
	ddb_major = register_chrdev(0, DDB_NAME, &ddb_fops);
	if (ddb_major < 0)
3140
		return ddb_major;
3141 3142
	if (class_register(&ddb_class) < 0)
		return -1;
3143 3144 3145
	return 0;
}

3146
static void ddb_class_destroy(void)
3147
{
3148
	class_unregister(&ddb_class);
3149 3150 3151
	unregister_chrdev(ddb_major, DDB_NAME);
}

3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
static void ddb_device_attrs_del(struct ddb *dev)
{
	int i;

	for (i = 0; i < 4; i++)
		if (dev->link[i].info && dev->link[i].info->tempmon_irq)
			device_remove_file(dev->ddb_dev,
					   &ddb_attrs_fanspeed[i]);
	for (i = 0; i < dev->link[0].info->temp_num; i++)
		device_remove_file(dev->ddb_dev, &ddb_attrs_temp[i]);
	for (i = 0; i < dev->link[0].info->fan_num; i++)
		device_remove_file(dev->ddb_dev, &ddb_attrs_fan[i]);
	for (i = 0; i < dev->i2c_num && i < 4; i++) {
		if (dev->link[0].info->led_num)
			device_remove_file(dev->ddb_dev, &ddb_attrs_led[i]);
		device_remove_file(dev->ddb_dev, &ddb_attrs_snr[i]);
		device_remove_file(dev->ddb_dev, &ddb_attrs_ctemp[i]);
	}
3170
	for (i = 0; ddb_attrs[i].attr.name; i++)
3171 3172 3173 3174 3175 3176 3177
		device_remove_file(dev->ddb_dev, &ddb_attrs[i]);
}

static int ddb_device_attrs_add(struct ddb *dev)
{
	int i;

3178
	for (i = 0; ddb_attrs[i].attr.name; i++)
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
		if (device_create_file(dev->ddb_dev, &ddb_attrs[i]))
			goto fail;
	for (i = 0; i < dev->link[0].info->temp_num; i++)
		if (device_create_file(dev->ddb_dev, &ddb_attrs_temp[i]))
			goto fail;
	for (i = 0; i < dev->link[0].info->fan_num; i++)
		if (device_create_file(dev->ddb_dev, &ddb_attrs_fan[i]))
			goto fail;
	for (i = 0; (i < dev->i2c_num) && (i < 4); i++) {
		if (device_create_file(dev->ddb_dev, &ddb_attrs_snr[i]))
			goto fail;
		if (device_create_file(dev->ddb_dev, &ddb_attrs_ctemp[i]))
			goto fail;
		if (dev->link[0].info->led_num)
			if (device_create_file(dev->ddb_dev,
					       &ddb_attrs_led[i]))
				goto fail;
	}
	for (i = 0; i < 4; i++)
		if (dev->link[i].info && dev->link[i].info->tempmon_irq)
			if (device_create_file(dev->ddb_dev,
					       &ddb_attrs_fanspeed[i]))
				goto fail;
	return 0;
fail:
	return -1;
}

3207
int ddb_device_create(struct ddb *dev)
3208
{
3209 3210 3211 3212 3213 3214 3215 3216
	int res = 0;

	if (ddb_num == DDB_MAX_ADAPTER)
		return -ENOMEM;
	mutex_lock(&ddb_mutex);
	dev->nr = ddb_num;
	ddbs[dev->nr] = dev;
	dev->ddb_dev = device_create(&ddb_class, dev->dev,
3217 3218
				     MKDEV(ddb_major, dev->nr),
				     dev, "ddbridge%d", dev->nr);
3219 3220 3221 3222 3223 3224 3225 3226 3227
	if (IS_ERR(dev->ddb_dev)) {
		res = PTR_ERR(dev->ddb_dev);
		dev_info(dev->dev, "Could not create ddbridge%d\n", dev->nr);
		goto fail;
	}
	res = ddb_device_attrs_add(dev);
	if (res) {
		ddb_device_attrs_del(dev);
		device_destroy(&ddb_class, MKDEV(ddb_major, dev->nr));
3228
		ddbs[dev->nr] = NULL;
3229
		dev->ddb_dev = ERR_PTR(-ENODEV);
3230
	} else {
3231
		ddb_num++;
3232
	}
3233 3234 3235
fail:
	mutex_unlock(&ddb_mutex);
	return res;
3236 3237
}

3238
void ddb_device_destroy(struct ddb *dev)
3239 3240 3241
{
	if (IS_ERR(dev->ddb_dev))
		return;
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
	ddb_device_attrs_del(dev);
	device_destroy(&ddb_class, MKDEV(ddb_major, dev->nr));
}

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

static void tempmon_setfan(struct ddb_link *link)
{
	u32 temp, temp2, pwm;

	if ((ddblreadl(link, TEMPMON_CONTROL) &
	    TEMPMON_CONTROL_OVERTEMP) != 0) {
		dev_info(link->dev->dev, "Over temperature condition\n");
		link->overtemperature_error = 1;
	}
	temp  = (ddblreadl(link, TEMPMON_SENSOR0) >> 8) & 0xFF;
	if (temp & 0x80)
		temp = 0;
	temp2  = (ddblreadl(link, TEMPMON_SENSOR1) >> 8) & 0xFF;
	if (temp2 & 0x80)
		temp2 = 0;
	if (temp2 > temp)
		temp = temp2;

	pwm = (ddblreadl(link, TEMPMON_FANCONTROL) >> 8) & 0x0F;
	if (pwm > 10)
		pwm = 10;

	if (temp >= link->temp_tab[pwm]) {
		while (pwm < 10 && temp >= link->temp_tab[pwm + 1])
			pwm += 1;
	} else {
		while (pwm > 1 && temp < link->temp_tab[pwm - 2])
			pwm -= 1;
	}
	ddblwritel(link, (pwm << 8), TEMPMON_FANCONTROL);
}

3282
static void temp_handler(void *data)
3283
{
3284
	struct ddb_link *link = (struct ddb_link *)data;
3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302

	spin_lock(&link->temp_lock);
	tempmon_setfan(link);
	spin_unlock(&link->temp_lock);
}

static int tempmon_init(struct ddb_link *link, int first_time)
{
	struct ddb *dev = link->dev;
	int status = 0;
	u32 l = link->nr;

	spin_lock_irq(&link->temp_lock);
	if (first_time) {
		static u8 temperature_table[11] = {
			30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80 };

		memcpy(link->temp_tab, temperature_table,
3303
		       sizeof(temperature_table));
3304
	}
3305
	ddb_irq_set(dev, l, link->info->tempmon_irq, temp_handler, link);
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
	ddblwritel(link, (TEMPMON_CONTROL_OVERTEMP | TEMPMON_CONTROL_AUTOSCAN |
			  TEMPMON_CONTROL_INTENABLE),
		   TEMPMON_CONTROL);
	ddblwritel(link, (3 << 8), TEMPMON_FANCONTROL);

	link->overtemperature_error =
		((ddblreadl(link, TEMPMON_CONTROL) &
			TEMPMON_CONTROL_OVERTEMP) != 0);
	if (link->overtemperature_error) {
		dev_info(link->dev->dev, "Over temperature condition\n");
		status = -1;
	}
	tempmon_setfan(link);
	spin_unlock_irq(&link->temp_lock);
	return status;
}

static int ddb_init_tempmon(struct ddb_link *link)
{
3325
	const struct ddb_info *info = link->info;
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342

	if (!info->tempmon_irq)
		return 0;
	if (info->type == DDB_OCTOPUS_MAX_CT)
		if (link->ids.regmapid < 0x00010002)
			return 0;
	spin_lock_init(&link->temp_lock);
	dev_dbg(link->dev->dev, "init_tempmon\n");
	return tempmon_init(link, 1);
}

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

static int ddb_init_boards(struct ddb *dev)
{
3343
	const struct ddb_info *info;
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
	struct ddb_link *link;
	u32 l;

	for (l = 0; l < DDB_MAX_LINK; l++) {
		link = &dev->link[l];
		info = link->info;

		if (!info)
			continue;
		if (info->board_control) {
			ddbwritel(dev, 0, DDB_LINK_TAG(l) | BOARD_CONTROL);
			msleep(100);
			ddbwritel(dev, info->board_control_2,
3357
				  DDB_LINK_TAG(l) | BOARD_CONTROL);
3358 3359
			usleep_range(2000, 3000);
			ddbwritel(dev,
3360 3361
				  info->board_control_2 | info->board_control,
				  DDB_LINK_TAG(l) | BOARD_CONTROL);
3362 3363 3364 3365 3366 3367 3368 3369 3370
			usleep_range(2000, 3000);
		}
		ddb_init_tempmon(link);
	}
	return 0;
}

int ddb_init(struct ddb *dev)
{
3371
	mutex_init(&dev->link[0].lnb.lock);
3372 3373 3374 3375 3376 3377 3378 3379 3380
	mutex_init(&dev->link[0].flash_mutex);
	if (no_init) {
		ddb_device_create(dev);
		return 0;
	}

	ddb_init_boards(dev);

	if (ddb_i2c_init(dev) < 0)
3381
		goto fail1;
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
	ddb_ports_init(dev);
	if (ddb_buffers_alloc(dev) < 0) {
		dev_info(dev->dev, "Could not allocate buffer memory\n");
		goto fail2;
	}
	if (ddb_ports_attach(dev) < 0)
		goto fail3;

	ddb_device_create(dev);

	if (dev->link[0].info->fan_num)	{
		ddbwritel(dev, 1, GPIO_DIRECTION);
		ddbwritel(dev, 1, GPIO_OUTPUT);
	}
	return 0;

fail3:
	dev_err(dev->dev, "fail3\n");
3400 3401
	ddb_ports_detach(dev);
	ddb_buffers_free(dev);
3402 3403
fail2:
	dev_err(dev->dev, "fail2\n");
3404
	ddb_ports_release(dev);
3405
	ddb_i2c_release(dev);
3406
fail1:
3407 3408
	dev_err(dev->dev, "fail1\n");
	return -1;
3409
}
3410 3411 3412 3413 3414 3415 3416

void ddb_unmap(struct ddb *dev)
{
	if (dev->regs)
		iounmap(dev->regs);
	vfree(dev);
}
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434

int ddb_exit_ddbridge(int stage, int error)
{
	switch (stage) {
	default:
	case 2:
		destroy_workqueue(ddb_wq);
		/* fall-through */
	case 1:
		ddb_class_destroy();
		break;
	}

	return error;
}

int ddb_init_ddbridge(void)
{
3435 3436 3437 3438 3439 3440 3441 3442 3443
	if (dma_buf_num < 8)
		dma_buf_num = 8;
	if (dma_buf_num > 32)
		dma_buf_num = 32;
	if (dma_buf_size < 1)
		dma_buf_size = 1;
	if (dma_buf_size > 43)
		dma_buf_size = 43;

3444 3445 3446 3447 3448 3449 3450 3451
	if (ddb_class_create() < 0)
		return -1;
	ddb_wq = alloc_workqueue("ddbridge", 0, 0);
	if (!ddb_wq)
		return ddb_exit_ddbridge(1, -1);

	return 0;
}