V4L/DVB (11595): stv090x: fixes a few bugs

This patch fixes:
* Cut revision was read too late
* Missing increment
* wrong return value
* mismatched entries

Signed-off-by: Andreas Regel <andreas.regel@gmx.de>
Signed-off-by: Manu Abraham <manu@linuxtv.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
diff --git a/drivers/media/dvb/frontends/stv090x.c b/drivers/media/dvb/frontends/stv090x.c
index 3601132..004b774 100644
--- a/drivers/media/dvb/frontends/stv090x.c
+++ b/drivers/media/dvb/frontends/stv090x.c
@@ -1943,6 +1943,7 @@
 
 	srate = state->srate;
 	car_max = state->search_range / 1000;
+	car_max += car_max / 10;
 	car_max  = 65536 * (car_max / 2);
 	car_max /= (state->mclk / 1000);
 
@@ -2003,6 +2004,7 @@
 
 	offst_car  = STV090x_READ_DEMOD(state, CFR2) << 8;
 	offst_car |= STV090x_READ_DEMOD(state, CFR1);
+	offst_car = comp2(offst_car, 16);
 
 	agc2  = STV090x_READ_DEMOD(state, AGC2I1) << 8;
 	agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
@@ -2065,6 +2067,9 @@
 			STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x1);
 			if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
 				goto err;
+			STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x0);
+			if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
+				goto err;
 		}
 
 		if (zigzag) {
@@ -2076,6 +2081,8 @@
 			offst_freq += 2 * inc;
 		}
 
+		cpt_step++;
+
 		lock = stv090x_get_dmdlock(state, timeout);
 		no_signal = stv090x_chk_signal(state);
 
@@ -2149,7 +2156,7 @@
 			if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
 				goto err;
 		}
-		if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x69) < 0)
+		if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
 			goto err;
 		zigzag = 0;
 		break;
@@ -2310,7 +2317,7 @@
 	if (state->algo == STV090x_BLIND_SEARCH) {
 		tmg = STV090x_READ_DEMOD(state, TMGREG2);
 		STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
-		while ((i <= 50) && (!tmg) && (tmg != 0xff)) {
+		while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
 			tmg = STV090x_READ_DEMOD(state, TMGREG2);
 			msleep(5);
 			i += 5;
@@ -2347,7 +2354,7 @@
 		stv090x_i2c_gate_ctrl(fe, 0);
 
 		if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
-			return  STV090x_RANGEOK;
+			return STV090x_RANGEOK;
 		else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
 			return STV090x_RANGEOK;
 		else
@@ -2569,8 +2576,8 @@
 
 	case STV090x_DVBS2:
 		reg = STV090x_READ_DEMOD(state, DMDCFGMD);
-		STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
-		STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
+		STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
+		STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
 		if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
 			goto err;
 		if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
@@ -3115,7 +3122,7 @@
 	if ((signal_state == STV090x_NODATA) && (!no_signal)) {
 		if (state->dev_ver <= 0x11) {
 			reg = STV090x_READ_DEMOD(state, DMDSTATE);
-			if (((STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD)) == STV090x_DVBS2) && (state->inversion == INVERSION_AUTO))
+			if (((STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD)) == STV090x_DVBS1) && (state->inversion == INVERSION_AUTO))
 				signal_state = stv090x_acq_fixs1(state);
 		}
 	}
@@ -3139,7 +3146,7 @@
 	state->fec = STV090x_PRERR;
 	state->search_range = 2000000;
 
-	if (!stv090x_algo(state)) {
+	if (stv090x_algo(state) == STV090x_RANGEOK) {
 		dprintk(FE_DEBUG, 1, "Search success!");
 		return DVBFE_ALGO_SEARCH_SUCCESS;
 	} else {
@@ -3949,12 +3956,13 @@
 	msleep(5);
 
 	/* write initval */
+	dprintk(FE_DEBUG, 1, "Setting up initial values");
 	for (i = 0; i < t1_size; i++) {
-		dprintk(FE_DEBUG, 1, "Setting up initial values");
 		if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
 			goto err;
 	}
 
+	state->dev_ver = stv090x_read_reg(state, STV090x_MID);
 	if (state->dev_ver >= 0x20) {
 		if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
 			goto err;
@@ -4047,7 +4055,6 @@
 		dprintk(FE_ERROR, 1, "Error waking device");
 		goto error;
 	}
-	state->dev_ver = stv090x_read_reg(state, STV090x_MID);
 
 	dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n",
 	       state->device == STV0900 ? "STV0900" : "STV0903",