Free Electrons

Embedded Linux Experts

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
/*
 * Copyright (C) 2010 Michael Krufky (mkrufky@kernellabs.com)
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation, version 2.
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */

#include <linux/vmalloc.h>
#include <linux/i2c.h>

#include "mxl111sf.h"
#include "mxl111sf-reg.h"
#include "mxl111sf-phy.h"
#include "mxl111sf-i2c.h"
#include "mxl111sf-gpio.h"

#include "mxl111sf-demod.h"
#include "mxl111sf-tuner.h"

#include "lgdt3305.h"

int dvb_usb_mxl111sf_debug;
module_param_named(debug, dvb_usb_mxl111sf_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level "
		 "(1=info, 2=xfer, 4=i2c, 8=reg, 16=adv (or-able)).");

int dvb_usb_mxl111sf_isoc;
module_param_named(isoc, dvb_usb_mxl111sf_isoc, int, 0644);
MODULE_PARM_DESC(isoc, "enable usb isoc xfer (0=bulk, 1=isoc).");

#define ANT_PATH_AUTO 0
#define ANT_PATH_EXTERNAL 1
#define ANT_PATH_INTERNAL 2

int dvb_usb_mxl111sf_rfswitch =
#if 0
		ANT_PATH_AUTO;
#else
		ANT_PATH_EXTERNAL;
#endif

module_param_named(rfswitch, dvb_usb_mxl111sf_rfswitch, int, 0644);
MODULE_PARM_DESC(rfswitch, "force rf switch position (0=auto, 1=ext, 2=int).");

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

#define deb_info(args...)   dprintk(dvb_usb_mxl111sf_debug, 0x13, args)
#define deb_reg(args...)    dprintk(dvb_usb_mxl111sf_debug, 0x08, args)
#define deb_adv(args...)    dprintk(dvb_usb_mxl111sf_debug, MXL_ADV_DBG, args)

int mxl111sf_ctrl_msg(struct dvb_usb_device *d,
		      u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
{
	int wo = (rbuf == NULL || rlen == 0); /* write-only */
	int ret;
	u8 sndbuf[1+wlen];

	deb_adv("%s(wlen = %d, rlen = %d)\n", __func__, wlen, rlen);

	memset(sndbuf, 0, 1+wlen);

	sndbuf[0] = cmd;
	memcpy(&sndbuf[1], wbuf, wlen);

	ret = (wo) ? dvb_usb_generic_write(d, sndbuf, 1+wlen) :
		dvb_usb_generic_rw(d, sndbuf, 1+wlen, rbuf, rlen, 0);
	mxl_fail(ret);

	return ret;
}

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

#define MXL_CMD_REG_READ	0xaa
#define MXL_CMD_REG_WRITE	0x55

int mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data)
{
	u8 buf[2];
	int ret;

	ret = mxl111sf_ctrl_msg(state->d, MXL_CMD_REG_READ, &addr, 1, buf, 2);
	if (mxl_fail(ret)) {
		mxl_debug("error reading reg: 0x%02x", addr);
		goto fail;
	}

	if (buf[0] == addr)
		*data = buf[1];
	else {
		err("invalid response reading reg: 0x%02x != 0x%02x, 0x%02x",
		    addr, buf[0], buf[1]);
		ret = -EINVAL;
	}

	deb_reg("R: (0x%02x, 0x%02x)\n", addr, *data);
fail:
	return ret;
}

int mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data)
{
	u8 buf[] = { addr, data };
	int ret;

	deb_reg("W: (0x%02x, 0x%02x)\n", addr, data);

	ret = mxl111sf_ctrl_msg(state->d, MXL_CMD_REG_WRITE, buf, 2, NULL, 0);
	if (mxl_fail(ret))
		err("error writing reg: 0x%02x, val: 0x%02x", addr, data);
	return ret;
}

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

int mxl111sf_write_reg_mask(struct mxl111sf_state *state,
				   u8 addr, u8 mask, u8 data)
{
	int ret;
	u8 val;

	if (mask != 0xff) {
		ret = mxl111sf_read_reg(state, addr, &val);
#if 1
		/* dont know why this usually errors out on the first try */
		if (mxl_fail(ret))
			err("error writing addr: 0x%02x, mask: 0x%02x, "
			    "data: 0x%02x, retrying...", addr, mask, data);

		ret = mxl111sf_read_reg(state, addr, &val);
#endif
		if (mxl_fail(ret))
			goto fail;
	}
	val &= ~mask;
	val |= data;

	ret = mxl111sf_write_reg(state, addr, val);
	mxl_fail(ret);
fail:
	return ret;
}

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

int mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
			       struct mxl111sf_reg_ctrl_info *ctrl_reg_info)
{
	int i, ret = 0;

	for (i = 0;  ctrl_reg_info[i].addr |
		     ctrl_reg_info[i].mask |
		     ctrl_reg_info[i].data;  i++) {

		ret = mxl111sf_write_reg_mask(state,
					      ctrl_reg_info[i].addr,
					      ctrl_reg_info[i].mask,
					      ctrl_reg_info[i].data);
		if (mxl_fail(ret)) {
			err("failed on reg #%d (0x%02x)", i,
			    ctrl_reg_info[i].addr);
			break;
		}
	}
	return ret;
}

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

static int mxl1x1sf_get_chip_info(struct mxl111sf_state *state)
{
	int ret;
	u8 id, ver;
	char *mxl_chip, *mxl_rev;

	if ((state->chip_id) && (state->chip_ver))
		return 0;

	ret = mxl111sf_read_reg(state, CHIP_ID_REG, &id);
	if (mxl_fail(ret))
		goto fail;
	state->chip_id = id;

	ret = mxl111sf_read_reg(state, TOP_CHIP_REV_ID_REG, &ver);
	if (mxl_fail(ret))
		goto fail;
	state->chip_ver = ver;

	switch (id) {
	case 0x61:
		mxl_chip = "MxL101SF";
		break;
	case 0x63:
		mxl_chip = "MxL111SF";
		break;
	default:
		mxl_chip = "UNKNOWN MxL1X1";
		break;
	}
	switch (ver) {
	case 0x36:
		state->chip_rev = MXL111SF_V6;
		mxl_rev = "v6";
		break;
	case 0x08:
		state->chip_rev = MXL111SF_V8_100;
		mxl_rev = "v8_100";
		break;
	case 0x18:
		state->chip_rev = MXL111SF_V8_200;
		mxl_rev = "v8_200";
		break;
	default:
		state->chip_rev = 0;
		mxl_rev = "UNKNOWN REVISION";
		break;
	}
	info("%s detected, %s (0x%x)", mxl_chip, mxl_rev, ver);
fail:
	return ret;
}

#define get_chip_info(state)						\
({									\
	int ___ret;							\
	___ret = mxl1x1sf_get_chip_info(state);				\
	if (mxl_fail(___ret)) {						\
		mxl_debug("failed to get chip info"			\
			  " on first probe attempt");			\
		___ret = mxl1x1sf_get_chip_info(state);			\
		if (mxl_fail(___ret))					\
			err("failed to get chip info during probe");	\
		else							\
			mxl_debug("probe needed a retry "		\
				  "in order to succeed.");		\
	}								\
	___ret;								\
})

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

static int mxl111sf_power_ctrl(struct dvb_usb_device *d, int onoff)
{
	/* power control depends on which adapter is being woken:
	 * save this for init, instead, via mxl111sf_adap_fe_init */
	return 0;
}

static int mxl111sf_adap_fe_init(struct dvb_frontend *fe)
{
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	struct mxl111sf_adap_state *adap_state = adap->fe_adap[fe->id].priv;

	int err;

	/* exit if we didnt initialize the driver yet */
	if (!state->chip_id) {
		mxl_debug("driver not yet initialized, exit.");
		goto fail;
	}

	deb_info("%s()\n", __func__);

	mutex_lock(&state->fe_lock);

	state->alt_mode = adap_state->alt_mode;

	if (usb_set_interface(adap->dev->udev, 0, state->alt_mode) < 0)
		err("set interface failed");

	err = mxl1x1sf_soft_reset(state);
	mxl_fail(err);
	err = mxl111sf_init_tuner_demod(state);
	mxl_fail(err);
	err = mxl1x1sf_set_device_mode(state, adap_state->device_mode);

	mxl_fail(err);
	mxl111sf_enable_usb_output(state);
	mxl_fail(err);
	mxl1x1sf_top_master_ctrl(state, 1);
	mxl_fail(err);

	if ((MXL111SF_GPIO_MOD_DVBT != adap_state->gpio_mode) &&
	    (state->chip_rev > MXL111SF_V6)) {
		mxl111sf_config_pin_mux_modes(state,
					      PIN_MUX_TS_SPI_IN_MODE_1);
		mxl_fail(err);
	}
	err = mxl111sf_init_port_expander(state);
	if (!mxl_fail(err)) {
		state->gpio_mode = adap_state->gpio_mode;
		err = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
		mxl_fail(err);
#if 0
		err = fe->ops.init(fe);
#endif
		msleep(100); /* add short delay after enabling
			      * the demod before touching it */
	}

	return (adap_state->fe_init) ? adap_state->fe_init(fe) : 0;
fail:
	return -ENODEV;
}

static int mxl111sf_adap_fe_sleep(struct dvb_frontend *fe)
{
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	struct mxl111sf_adap_state *adap_state = adap->fe_adap[fe->id].priv;
	int err;

	/* exit if we didnt initialize the driver yet */
	if (!state->chip_id) {
		mxl_debug("driver not yet initialized, exit.");
		goto fail;
	}

	deb_info("%s()\n", __func__);

	err = (adap_state->fe_sleep) ? adap_state->fe_sleep(fe) : 0;

	mutex_unlock(&state->fe_lock);

	return err;
fail:
	return -ENODEV;
}


static int mxl111sf_ep6_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	struct mxl111sf_adap_state *adap_state = adap->fe_adap[adap->active_fe].priv;
	int ret = 0;
	u8 tmp;

	deb_info("%s(%d)\n", __func__, onoff);

	if (onoff) {
		ret = mxl111sf_enable_usb_output(state);
		mxl_fail(ret);
		ret = mxl111sf_config_mpeg_in(state, 1, 1,
					      adap_state->ep6_clockphase,
					      0, 0);
		mxl_fail(ret);
#if 0
	} else {
		ret = mxl111sf_disable_656_port(state);
		mxl_fail(ret);
#endif
	}

	return ret;
}

static int mxl111sf_ep4_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	int ret = 0;

	deb_info("%s(%d)\n", __func__, onoff);

	if (onoff) {
		ret = mxl111sf_enable_usb_output(state);
		mxl_fail(ret);
	}

	return ret;
}

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

static struct lgdt3305_config hauppauge_lgdt3305_config = {
	.i2c_addr           = 0xb2 >> 1,
	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
	.tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
	.deny_i2c_rptr      = 1,
	.spectral_inversion = 0,
	.qam_if_khz         = 6000,
	.vsb_if_khz         = 6000,
};

static int mxl111sf_lgdt3305_frontend_attach(struct dvb_usb_adapter *adap)
{
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	int fe_id = adap->num_frontends_initialized;
	struct mxl111sf_adap_state *adap_state = adap->fe_adap[fe_id].priv;
	int ret;

	deb_adv("%s()\n", __func__);

	/* save a pointer to the dvb_usb_device in device state */
	state->d = d;
	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
	state->alt_mode = adap_state->alt_mode;

	if (usb_set_interface(adap->dev->udev, 0, state->alt_mode) < 0)
		err("set interface failed");

	state->gpio_mode = MXL111SF_GPIO_MOD_ATSC;
	adap_state->gpio_mode = state->gpio_mode;
	adap_state->device_mode = MXL_TUNER_MODE;
	adap_state->ep6_clockphase = 1;

	ret = mxl1x1sf_soft_reset(state);
	if (mxl_fail(ret))
		goto fail;
	ret = mxl111sf_init_tuner_demod(state);
	if (mxl_fail(ret))
		goto fail;

	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
	if (mxl_fail(ret))
		goto fail;

	ret = mxl111sf_enable_usb_output(state);
	if (mxl_fail(ret))
		goto fail;
	ret = mxl1x1sf_top_master_ctrl(state, 1);
	if (mxl_fail(ret))
		goto fail;

	ret = mxl111sf_init_port_expander(state);
	if (mxl_fail(ret))
		goto fail;
	ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
	if (mxl_fail(ret))
		goto fail;

	adap->fe_adap[fe_id].fe = dvb_attach(lgdt3305_attach,
				 &hauppauge_lgdt3305_config,
				 &adap->dev->i2c_adap);
	if (adap->fe_adap[fe_id].fe) {
		adap_state->fe_init = adap->fe_adap[fe_id].fe->ops.init;
		adap->fe_adap[fe_id].fe->ops.init = mxl111sf_adap_fe_init;
		adap_state->fe_sleep = adap->fe_adap[fe_id].fe->ops.sleep;
		adap->fe_adap[fe_id].fe->ops.sleep = mxl111sf_adap_fe_sleep;
		return 0;
	}
	ret = -EIO;
fail:
	return ret;
}

static struct mxl111sf_demod_config mxl_demod_config = {
	.read_reg        = mxl111sf_read_reg,
	.write_reg       = mxl111sf_write_reg,
	.program_regs    = mxl111sf_ctrl_program_regs,
};

static int mxl111sf_attach_demod(struct dvb_usb_adapter *adap)
{
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	int fe_id = adap->num_frontends_initialized;
	struct mxl111sf_adap_state *adap_state = adap->fe_adap[fe_id].priv;
	int ret;

	deb_adv("%s()\n", __func__);

	/* save a pointer to the dvb_usb_device in device state */
	state->d = d;
	adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 1 : 2;
	state->alt_mode = adap_state->alt_mode;

	if (usb_set_interface(adap->dev->udev, 0, state->alt_mode) < 0)
		err("set interface failed");

	state->gpio_mode = MXL111SF_GPIO_MOD_DVBT;
	adap_state->gpio_mode = state->gpio_mode;
	adap_state->device_mode = MXL_SOC_MODE;
	adap_state->ep6_clockphase = 1;

	ret = mxl1x1sf_soft_reset(state);
	if (mxl_fail(ret))
		goto fail;
	ret = mxl111sf_init_tuner_demod(state);
	if (mxl_fail(ret))
		goto fail;

	ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
	if (mxl_fail(ret))
		goto fail;

	ret = mxl111sf_enable_usb_output(state);
	if (mxl_fail(ret))
		goto fail;
	ret = mxl1x1sf_top_master_ctrl(state, 1);
	if (mxl_fail(ret))
		goto fail;

	/* dont care if this fails */
	mxl111sf_init_port_expander(state);

	adap->fe_adap[fe_id].fe = dvb_attach(mxl111sf_demod_attach, state,
			      &mxl_demod_config);
	if (adap->fe_adap[fe_id].fe) {
		adap_state->fe_init = adap->fe_adap[fe_id].fe->ops.init;
		adap->fe_adap[fe_id].fe->ops.init = mxl111sf_adap_fe_init;
		adap_state->fe_sleep = adap->fe_adap[fe_id].fe->ops.sleep;
		adap->fe_adap[fe_id].fe->ops.sleep = mxl111sf_adap_fe_sleep;
		return 0;
	}
	ret = -EIO;
fail:
	return ret;
}

static inline int mxl111sf_set_ant_path(struct mxl111sf_state *state,
					int antpath)
{
	return mxl111sf_idac_config(state, 1, 1,
				    (antpath == ANT_PATH_INTERNAL) ?
				    0x3f : 0x00, 0);
}

#define DbgAntHunt(x, pwr0, pwr1, pwr2, pwr3) \
	err("%s(%d) FINAL input set to %s rxPwr:%d|%d|%d|%d\n", \
	    __func__, __LINE__, \
	    (ANT_PATH_EXTERNAL == x) ? "EXTERNAL" : "INTERNAL", \
	    pwr0, pwr1, pwr2, pwr3)

#define ANT_HUNT_SLEEP 90
#define ANT_EXT_TWEAK 0

static int mxl111sf_ant_hunt(struct dvb_frontend *fe)
{
	struct dvb_usb_adapter *adap = fe->dvb->priv;
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;

	int antctrl = dvb_usb_mxl111sf_rfswitch;

	u16 rxPwrA, rxPwr0, rxPwr1, rxPwr2;

	/* FIXME: must force EXTERNAL for QAM - done elsewhere */
	mxl111sf_set_ant_path(state, antctrl == ANT_PATH_AUTO ?
			      ANT_PATH_EXTERNAL : antctrl);

	if (antctrl == ANT_PATH_AUTO) {
#if 0
		msleep(ANT_HUNT_SLEEP);
#endif
		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwrA);

		mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
		msleep(ANT_HUNT_SLEEP);
		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr0);

		mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
		msleep(ANT_HUNT_SLEEP);
		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr1);

		mxl111sf_set_ant_path(state, ANT_PATH_INTERNAL);
		msleep(ANT_HUNT_SLEEP);
		fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr2);

		if (rxPwr1+ANT_EXT_TWEAK >= rxPwr2) {
			/* return with EXTERNAL enabled */
			mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
			DbgAntHunt(ANT_PATH_EXTERNAL, rxPwrA,
				   rxPwr0, rxPwr1, rxPwr2);
		} else {
			/* return with INTERNAL enabled */
			DbgAntHunt(ANT_PATH_INTERNAL, rxPwrA,
				   rxPwr0, rxPwr1, rxPwr2);
		}
	}
	return 0;
}

static struct mxl111sf_tuner_config mxl_tuner_config = {
	.if_freq         = MXL_IF_6_0, /* applies to external IF output, only */
	.invert_spectrum = 0,
	.read_reg        = mxl111sf_read_reg,
	.write_reg       = mxl111sf_write_reg,
	.program_regs    = mxl111sf_ctrl_program_regs,
	.top_master_ctrl = mxl1x1sf_top_master_ctrl,
	.ant_hunt        = mxl111sf_ant_hunt,
};

static int mxl111sf_attach_tuner(struct dvb_usb_adapter *adap)
{
	struct dvb_usb_device *d = adap->dev;
	struct mxl111sf_state *state = d->priv;
	int fe_id = adap->num_frontends_initialized;

	deb_adv("%s()\n", __func__);

	if (NULL != dvb_attach(mxl111sf_tuner_attach,
			       adap->fe_adap[fe_id].fe, state,
			       &mxl_tuner_config))
		return 0;

	return -EIO;
}

static int mxl111sf_fe_ioctl_override(struct dvb_frontend *fe,
				      unsigned int cmd, void *parg,
				      unsigned int stage)
{
	int err = 0;

	switch (stage) {
	case DVB_FE_IOCTL_PRE:

		switch (cmd) {
		case FE_READ_SIGNAL_STRENGTH:
			err = fe->ops.tuner_ops.get_rf_strength(fe, parg);
			/* If no error occurs, prevent dvb-core from handling
			 * this IOCTL, otherwise return the error */
			if (0 == err)
				err = 1;
			break;
		}
		break;

	case DVB_FE_IOCTL_POST:
		/* no post-ioctl handling required */
		break;
	}
	return err;
};

static u32 mxl111sf_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

struct i2c_algorithm mxl111sf_i2c_algo = {
	.master_xfer   = mxl111sf_i2c_xfer,
	.functionality = mxl111sf_i2c_func,
#ifdef NEED_ALGO_CONTROL
	.algo_control = dummy_algo_control,
#endif
};

static struct dvb_usb_device_properties mxl111sf_dvbt_bulk_properties;
static struct dvb_usb_device_properties mxl111sf_dvbt_isoc_properties;
static struct dvb_usb_device_properties mxl111sf_atsc_bulk_properties;
static struct dvb_usb_device_properties mxl111sf_atsc_isoc_properties;

static int mxl111sf_probe(struct usb_interface *intf,
			  const struct usb_device_id *id)
{
	struct dvb_usb_device *d = NULL;

	deb_adv("%s()\n", __func__);

	if (((dvb_usb_mxl111sf_isoc) &&
	     (0 == dvb_usb_device_init(intf,
				       &mxl111sf_dvbt_isoc_properties,
				       THIS_MODULE, &d, adapter_nr) ||
	      0 == dvb_usb_device_init(intf,
				       &mxl111sf_atsc_isoc_properties,
				       THIS_MODULE, &d, adapter_nr))) ||
	    0 == dvb_usb_device_init(intf,
				     &mxl111sf_dvbt_bulk_properties,
				     THIS_MODULE, &d, adapter_nr) ||
	    0 == dvb_usb_device_init(intf,
				     &mxl111sf_atsc_bulk_properties,
				     THIS_MODULE, &d, adapter_nr) || 0) {

		struct mxl111sf_state *state = d->priv;
		static u8 eeprom[256];
		struct i2c_client c;
		int ret;

		ret = get_chip_info(state);
		if (mxl_fail(ret))
			err("failed to get chip info during probe");

		mutex_init(&state->fe_lock);

		if (state->chip_rev > MXL111SF_V6)
			mxl111sf_config_pin_mux_modes(state,
						      PIN_MUX_TS_SPI_IN_MODE_1);

		c.adapter = &d->i2c_adap;
		c.addr = 0xa0 >> 1;

		ret = tveeprom_read(&c, eeprom, sizeof(eeprom));
		if (mxl_fail(ret))
			return 0;
		tveeprom_hauppauge_analog(&c, &state->tv,
					  (0x84 == eeprom[0xa0]) ?
					  eeprom + 0xa0 : eeprom + 0x80);
#if 0
		switch (state->tv.model) {
		case 117001:
		case 126001:
		case 138001:
			break;
		default:
			printk(KERN_WARNING "%s: warning: "
			       "unknown hauppauge model #%d\n",
			       __func__, state->tv.model);
		}
#endif
		return 0;
	}
	err("Your device is not yet supported by this driver. "
	    "See kernellabs.com for more info");
	return -EINVAL;
}

static struct usb_device_id mxl111sf_table[] = {
/* 0 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc600) }, /* ATSC+ IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc601) }, /* ATSC         */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc602) }, /*     +        */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc603) }, /* ATSC+        */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc604) }, /* DVBT         */
/* 5 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc609) }, /* ATSC  IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60a) }, /*     + IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60b) }, /* ATSC+ IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60c) }, /* DVBT  IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc653) }, /* ATSC+        */
/*10 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc65b) }, /* ATSC+ IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb700) }, /* ATSC+ sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb701) }, /* ATSC  sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb702) }, /*     + sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb703) }, /* ATSC+ sw     */
/*15 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb704) }, /* DVBT  sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb753) }, /* ATSC+ sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb763) }, /* ATSC+ no     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb764) }, /* DVBT  no     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd853) }, /* ATSC+ sw     */
/*20 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd854) }, /* DVBT  sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd863) }, /* ATSC+ no     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd864) }, /* DVBT  no     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d3) }, /* ATSC+ sw     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d4) }, /* DVBT  sw     */
/*25 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e3) }, /* ATSC+ no     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e4) }, /* DVBT  no     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8ff) }, /* ATSC+        */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc612) }, /*     +        */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc613) }, /* ATSC+        */
/*30 */	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61a) }, /*     + IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61b) }, /* ATSC+ IR     */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb757) }, /* ATSC+DVBT sw */
	{ USB_DEVICE(USB_VID_HAUPPAUGE, 0xb767) }, /* ATSC+DVBT no */
	{}		/* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, mxl111sf_table);


#define MXL111SF_EP4_BULK_STREAMING_CONFIG		\
	.size_of_priv = sizeof(struct mxl111sf_adap_state), \
	.streaming_ctrl = mxl111sf_ep4_streaming_ctrl,	\
	.stream = {					\
		.type = USB_BULK,			\
		.count = 5,				\
		.endpoint = 0x04,			\
		.u = {					\
			.bulk = {			\
				.buffersize = 8192,	\
			}				\
		}					\
	}

/* FIXME: works for v6 but not v8 silicon */
#define MXL111SF_EP4_ISOC_STREAMING_CONFIG		\
	.size_of_priv = sizeof(struct mxl111sf_adap_state), \
	.streaming_ctrl = mxl111sf_ep4_streaming_ctrl,	\
	.stream = {					\
		.type = USB_ISOC,			\
		.count = 5,				\
		.endpoint = 0x04,			\
		.u = {					\
			.isoc = {			\
				.framesperurb = 96,	\
				/* FIXME: v6 SILICON: */	\
				.framesize = 564,	\
				.interval = 1,		\
			}				\
		}					\
	}

#define MXL111SF_EP6_BULK_STREAMING_CONFIG		\
	.size_of_priv = sizeof(struct mxl111sf_adap_state), \
	.streaming_ctrl = mxl111sf_ep6_streaming_ctrl,	\
	.stream = {					\
		.type = USB_BULK,			\
		.count = 5,				\
		.endpoint = 0x06,			\
		.u = {					\
			.bulk = {			\
				.buffersize = 8192,	\
			}				\
		}					\
	}

/* FIXME */
#define MXL111SF_EP6_ISOC_STREAMING_CONFIG		\
	.size_of_priv = sizeof(struct mxl111sf_adap_state), \
	.streaming_ctrl = mxl111sf_ep6_streaming_ctrl,	\
	.stream = {					\
		.type = USB_ISOC,			\
		.count = 5,				\
		.endpoint = 0x06,			\
		.u = {					\
			.isoc = {			\
				.framesperurb = 24,	\
				.framesize = 3072,	\
				.interval = 1,		\
			}				\
		}					\
	}

#define MXL111SF_DEFAULT_DEVICE_PROPERTIES			\
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,			\
	.usb_ctrl = DEVICE_SPECIFIC,				\
	/* use usb alt setting 1 for EP4 ISOC transfer (dvb-t),	\
				     EP6 BULK transfer (atsc/qam), \
	   use usb alt setting 2 for EP4 BULK transfer (dvb-t),	\
				     EP6 ISOC transfer (atsc/qam), \
	*/							\
	.power_ctrl       = mxl111sf_power_ctrl,		\
	.i2c_algo         = &mxl111sf_i2c_algo,			\
	.generic_bulk_ctrl_endpoint          = MXL_EP2_REG_WRITE, \
	.generic_bulk_ctrl_endpoint_response = MXL_EP1_REG_READ, \
	.size_of_priv     = sizeof(struct mxl111sf_state)

static struct dvb_usb_device_properties mxl111sf_dvbt_bulk_properties = {
	MXL111SF_DEFAULT_DEVICE_PROPERTIES,

	.num_adapters = 1,
	.adapter = {
		{
		.fe_ioctl_override = mxl111sf_fe_ioctl_override,
		.num_frontends = 1,
		.fe = {{
			.frontend_attach  = mxl111sf_attach_demod,
			.tuner_attach     = mxl111sf_attach_tuner,

			MXL111SF_EP4_BULK_STREAMING_CONFIG,
		} },
		},
	},
	.num_device_descs = 4,
	.devices = {
		{   "Hauppauge 126xxx DVBT (bulk)",
			{ NULL },
			{ &mxl111sf_table[4], &mxl111sf_table[8],
			  NULL },
		},
		{   "Hauppauge 117xxx DVBT (bulk)",
			{ NULL },
			{ &mxl111sf_table[15], &mxl111sf_table[18],
			  NULL },
		},
		{   "Hauppauge 138xxx DVBT (bulk)",
			{ NULL },
			{ &mxl111sf_table[20], &mxl111sf_table[22],
			  &mxl111sf_table[24], &mxl111sf_table[26],
			  NULL },
		},
		{   "Hauppauge 126xxx (tp-bulk)",
			{ NULL },
			{ &mxl111sf_table[28], &mxl111sf_table[30],
			  NULL },
		},
	}
};

static struct dvb_usb_device_properties mxl111sf_dvbt_isoc_properties = {
	MXL111SF_DEFAULT_DEVICE_PROPERTIES,

	.num_adapters = 1,
	.adapter = {
		{
		.fe_ioctl_override = mxl111sf_fe_ioctl_override,
		.num_frontends = 1,
		.fe = {{
			.frontend_attach  = mxl111sf_attach_demod,
			.tuner_attach     = mxl111sf_attach_tuner,

			MXL111SF_EP4_ISOC_STREAMING_CONFIG,
		} },
		},
	},
	.num_device_descs = 4,
	.devices = {
		{   "Hauppauge 126xxx DVBT (isoc)",
			{ NULL },
			{ &mxl111sf_table[4], &mxl111sf_table[8],
			  NULL },
		},
		{   "Hauppauge 117xxx DVBT (isoc)",
			{ NULL },
			{ &mxl111sf_table[15], &mxl111sf_table[18],
			  NULL },
		},
		{   "Hauppauge 138xxx DVBT (isoc)",
			{ NULL },
			{ &mxl111sf_table[20], &mxl111sf_table[22],
			  &mxl111sf_table[24], &mxl111sf_table[26],
			  NULL },
		},
		{   "Hauppauge 126xxx (tp-isoc)",
			{ NULL },
			{ &mxl111sf_table[28], &mxl111sf_table[30],
			  NULL },
		},
	}
};

static struct dvb_usb_device_properties mxl111sf_atsc_bulk_properties = {
	MXL111SF_DEFAULT_DEVICE_PROPERTIES,

	.num_adapters = 1,
	.adapter = {
		{
		.fe_ioctl_override = mxl111sf_fe_ioctl_override,
		.num_frontends = 2,
		.fe = {{
			.frontend_attach  = mxl111sf_lgdt3305_frontend_attach,
			.tuner_attach     = mxl111sf_attach_tuner,

			MXL111SF_EP6_BULK_STREAMING_CONFIG,
		},
		{
			.frontend_attach  = mxl111sf_attach_demod,
			.tuner_attach     = mxl111sf_attach_tuner,

			MXL111SF_EP4_BULK_STREAMING_CONFIG,
		}},
		},
	},
	.num_device_descs = 6,
	.devices = {
		{   "Hauppauge 126xxx ATSC (bulk)",
			{ NULL },
			{ &mxl111sf_table[1], &mxl111sf_table[5],
			  NULL },
		},
		{   "Hauppauge 117xxx ATSC (bulk)",
			{ NULL },
			{ &mxl111sf_table[12],
			  NULL },
		},
		{   "Hauppauge 126xxx ATSC+ (bulk)",
			{ NULL },
			{ &mxl111sf_table[0], &mxl111sf_table[3],
			  &mxl111sf_table[7], &mxl111sf_table[9],
			  &mxl111sf_table[10], NULL },
		},
		{   "Hauppauge 117xxx ATSC+ (bulk)",
			{ NULL },
			{ &mxl111sf_table[11], &mxl111sf_table[14],
			  &mxl111sf_table[16], &mxl111sf_table[17],
			  &mxl111sf_table[32], &mxl111sf_table[33],
			  NULL },
		},
		{   "Hauppauge Mercury (tp-bulk)",
			{ NULL },
			{ &mxl111sf_table[19], &mxl111sf_table[21],
			  &mxl111sf_table[23], &mxl111sf_table[25],
			  &mxl111sf_table[27], NULL },
		},
		{   "Hauppauge WinTV-Aero-M",
			{ NULL },
			{ &mxl111sf_table[29], &mxl111sf_table[31],
			  NULL },
		},
	}
};

static struct dvb_usb_device_properties mxl111sf_atsc_isoc_properties = {
	MXL111SF_DEFAULT_DEVICE_PROPERTIES,

	.num_adapters = 1,
	.adapter = {
		{
		.fe_ioctl_override = mxl111sf_fe_ioctl_override,
		.num_frontends = 2,
		.fe = {{
			.frontend_attach  = mxl111sf_lgdt3305_frontend_attach,
			.tuner_attach     = mxl111sf_attach_tuner,

			MXL111SF_EP6_ISOC_STREAMING_CONFIG,
		},
		{
			.frontend_attach  = mxl111sf_attach_demod,
			.tuner_attach     = mxl111sf_attach_tuner,

			MXL111SF_EP4_ISOC_STREAMING_CONFIG,
		}},
		},
	},
	.num_device_descs = 6,
	.devices = {
		{   "Hauppauge 126xxx ATSC (isoc)",
			{ NULL },
			{ &mxl111sf_table[1], &mxl111sf_table[5],
			  NULL },
		},
		{   "Hauppauge 117xxx ATSC (isoc)",
			{ NULL },
			{ &mxl111sf_table[12],
			  NULL },
		},
		{   "Hauppauge 126xxx ATSC+ (isoc)",
			{ NULL },
			{ &mxl111sf_table[0], &mxl111sf_table[3],
			  &mxl111sf_table[7], &mxl111sf_table[9],
			  &mxl111sf_table[10], NULL },
		},
		{   "Hauppauge 117xxx ATSC+ (isoc)",
			{ NULL },
			{ &mxl111sf_table[11], &mxl111sf_table[14],
			  &mxl111sf_table[16], &mxl111sf_table[17],
			  &mxl111sf_table[32], &mxl111sf_table[33],
			  NULL },
		},
		{   "Hauppauge Mercury (tp-isoc)",
			{ NULL },
			{ &mxl111sf_table[19], &mxl111sf_table[21],
			  &mxl111sf_table[23], &mxl111sf_table[25],
			  &mxl111sf_table[27], NULL },
		},
		{   "Hauppauge WinTV-Aero-M (tp-isoc)",
			{ NULL },
			{ &mxl111sf_table[29], &mxl111sf_table[31],
			  NULL },
		},
	}
};

static struct usb_driver mxl111sf_driver = {
	.name		= "dvb_usb_mxl111sf",
	.probe		= mxl111sf_probe,
	.disconnect     = dvb_usb_device_exit,
	.id_table	= mxl111sf_table,
};

module_usb_driver(mxl111sf_driver);

MODULE_AUTHOR("Michael Krufky <mkrufky@kernellabs.com>");
MODULE_DESCRIPTION("Driver for MaxLinear MxL111SF");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");

/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */