[PATCH] drivers/media/dvb/: fix polling loops

From: Marcin Slusarz <marcin.slusarz@gmail.com>

Signed-off-by: Marcin Slusarz <marcin.slusarz@gmail.com>
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>

Index: linux-kj/drivers/media/dvb/bt8xx/dst.c
===================================================================
--- linux-kj.orig/drivers/media/dvb/bt8xx/dst.c	2006-01-18 02:21:28.000000000 +0300
+++ linux-kj/drivers/media/dvb/bt8xx/dst.c	2006-01-18 16:45:55.000000000 +0300
@@ -182,9 +182,11 @@ EXPORT_SYMBOL(dst_pio_disable);
 int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode)
 {
 	u8 reply;
-	int i;
+	int i = 0;
+	unsigned long end_time;
 
-	for (i = 0; i < 200; i++) {
+	end_time = jiffies + msecs_to_jiffies(2000);
+	while (time_before(jiffies, end_time)) {
 		if (dst_gpio_inb(state, &reply) < 0) {
 			dprintk(verbose, DST_ERROR, 1, "dst_gpio_inb ERROR !");
 			return -1;
@@ -194,6 +196,7 @@ int dst_wait_dst_ready(struct dst_state 
 			return 1;
 		}
 		msleep(10);
+		++i;
 	}
 	dprintk(verbose, DST_NOTICE, 1, "dst wait NOT ready after %d", i);
 
Index: linux-kj/drivers/media/dvb/dvb-usb/dtt200u-fe.c
===================================================================
--- linux-kj.orig/drivers/media/dvb/dvb-usb/dtt200u-fe.c	2006-01-18 02:21:28.000000000 +0300
+++ linux-kj/drivers/media/dvb/dvb-usb/dtt200u-fe.c	2006-01-18 16:45:55.000000000 +0300
@@ -105,7 +105,7 @@ static int dtt200u_fe_set_frontend(struc
 				  struct dvb_frontend_parameters *fep)
 {
 	struct dtt200u_fe_state *state = fe->demodulator_priv;
-	int i;
+	unsigned long end_time;
 	fe_status_t st;
 	u16 freq = fep->frequency / 250000;
 	u8 bwbuf[2] = { SET_BANDWIDTH, 0 },freqbuf[3] = { SET_RF_FREQ, 0, 0 };
@@ -125,7 +125,8 @@ static int dtt200u_fe_set_frontend(struc
 	freqbuf[2] = (freq >> 8) & 0xff;
 	dvb_usb_generic_write(state->d,freqbuf,3);
 
-	for (i = 0; i < 30; i++) {
+	end_time = jiffies + msecs_to_jiffies(600);
+	while (time_before(jiffies, end_time)) {
 		msleep(20);
 		dtt200u_fe_read_status(fe, &st);
 		if (st & FE_TIMEDOUT)
Index: linux-kj/drivers/media/dvb/frontends/nxt2002.c
===================================================================
--- linux-kj.orig/drivers/media/dvb/frontends/nxt2002.c	2006-01-18 02:21:28.000000000 +0300
+++ linux-kj/drivers/media/dvb/frontends/nxt2002.c	2006-01-18 16:45:56.000000000 +0300
@@ -159,18 +159,19 @@ static int nxt2002_readreg_multibyte (st
 
 static void nxt2002_microcontroller_stop (struct nxt2002_state* state)
 {
-	u8 buf[2],counter = 0;
+	u8 buf[2];
+	unsigned long end_time;
 	dprintk("%s\n", __FUNCTION__);
 
 	buf[0] = 0x80;
 	i2c_writebytes(state,0x22,buf,1);
 
-	while (counter < 20) {
+	end_time = jiffies + msecs_to_jiffies(200);
+	while (time_before(jiffies, end_time)) {
 		i2c_readbytes(state,0x31,buf,1);
 		if (buf[0] & 0x40)
 			return;
 		msleep(10);
-		counter++;
 	}
 
 	dprintk("Timeout waiting for micro to stop.. This is ok after firmware upload\n");
@@ -188,7 +189,8 @@ static void nxt2002_microcontroller_star
 
 static int nxt2002_writetuner (struct nxt2002_state* state, u8* data)
 {
-	u8 buf,count = 0;
+	u8 buf;
+	unsigned long end_time;
 
 	dprintk("Tuner Bytes: %02X %02X %02X %02X\n",data[0],data[1],data[2],data[3]);
 
@@ -215,12 +217,12 @@ static int nxt2002_writetuner (struct nx
 	buf = 0x80;
 	i2c_writebytes(state,0x21,&buf,1);
 
-	while (count < 20) {
+	end_time = jiffies + msecs_to_jiffies(2000);
+	while (time_before(jiffies, end_time)) {
 		i2c_readbytes(state,0x21,&buf,1);
 		if ((buf & 0x80)== 0x00)
 			return 0;
 		msleep(100);
-		count++;
 	}
 
 	printk("nxt2002: timeout error writing tuner\n");
Index: linux-kj/drivers/media/dvb/frontends/tda80xx.c
===================================================================
--- linux-kj.orig/drivers/media/dvb/frontends/tda80xx.c	2006-01-18 02:21:28.000000000 +0300
+++ linux-kj/drivers/media/dvb/frontends/tda80xx.c	2006-01-18 16:45:56.000000000 +0300
@@ -389,9 +389,10 @@ static void tda80xx_worklet(void *priv)
 
 static void tda80xx_wait_diseqc_fifo(struct tda80xx_state* state)
 {
-	size_t i;
+	unsigned long end_time;
 
-	for (i = 0; i < 100; i++) {
+	end_time = jiffies + msecs_to_jiffies(1000);
+	while (time_before(jiffies, end_time)) {
 		if (tda80xx_readreg(state, 0x02) & 0x80)
 			break;
 		msleep(10);
Index: linux-kj/drivers/media/dvb/ttpci/av7110.c
===================================================================
--- linux-kj.orig/drivers/media/dvb/ttpci/av7110.c	2006-01-18 02:21:28.000000000 +0300
+++ linux-kj/drivers/media/dvb/ttpci/av7110.c	2006-01-18 16:45:56.000000000 +0300
@@ -1998,7 +1998,7 @@ static int nexusca_stv0297_pll_set(struc
 	u8 data[4];
 	struct i2c_msg msg = { .addr = 0x63, .flags = 0, .buf = data, .len = sizeof(data) };
 	struct i2c_msg readmsg = { .addr = 0x63, .flags = I2C_M_RD, .buf = data, .len = 1 };
-	int i;
+	unsigned long end_time;
 
 	div = (params->frequency + 36150000 + 31250) / 62500;
 
@@ -2024,8 +2024,8 @@ static int nexusca_stv0297_pll_set(struc
 	}
 
 	// wait for PLL lock
-	for(i = 0; i < 20; i++) {
-
+	end_time = jiffies + msecs_to_jiffies(200);
+	while (time_before(jiffies, end_time)) {
 		stv0297_enable_plli2c(fe);
 		if (i2c_transfer(&av7110->i2c_adap, &readmsg, 1) == 1)
 			if (data[0] & 0x40) break;
Index: linux-kj/drivers/media/dvb/ttpci/budget-av.c
===================================================================
--- linux-kj.orig/drivers/media/dvb/ttpci/budget-av.c	2006-01-18 02:21:28.000000000 +0300
+++ linux-kj/drivers/media/dvb/ttpci/budget-av.c	2006-01-18 16:45:56.000000000 +0300
@@ -193,7 +193,7 @@ static int ciintf_slot_reset(struct dvb_
 {
 	struct budget_av *budget_av = (struct budget_av *) ca->data;
 	struct saa7146_dev *saa = budget_av->budget.dev;
-	int timeout = 50; // 5 seconds (4.4.6 Ready)
+	unsigned long end_time;
 
 	if (slot != 0)
 		return -EINVAL;
@@ -211,10 +211,11 @@ static int ciintf_slot_reset(struct dvb_
 
 	/* This should have been based on pin 16 READY of the pcmcia port,
 	 * but AFAICS it is not routed to the saa7146 */
-	while (--timeout > 0 && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
+	end_time = jiffies + msecs_to_jiffies(5000); // 5 seconds (4.4.6 Ready)
+	while (time_before(jiffies, end_time) && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
 		msleep(100);
 
-	if (timeout <= 0)
+	if (time_after_eq(jiffies, end_time))
 	{
 		printk(KERN_ERR "budget-av: cam reset failed (timeout).\n");
 		saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
