Skip to contentSkip to conversation

RFC PATCH add set_value locking

PATCH Grant Coady 2 participants 10 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

Greetings,

This patch adds locking around set operations that access data->something and go out to the chip.

for:
asb100, max1619, ds1621, pcf8574, fscher, pcf8591, fscpos, sis5595, gl518sm, via686a, gl520sm, w83627hf, w83781d

as separate files from:
http://scatter.mine.nu/lmsensors/lock-on-set/

drivers/i2c/chips/asb100.c +25 -3
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/asb100.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/asb100.c
@@ -246,9 +246,12 @@
 	struct i2c_client *client = to_i2c_client(dev); \
 	struct asb100_data *data = i2c_get_clientdata(client); \
 	unsigned long val = simple_strtoul(buf, NULL, 10); \
+\
+	down(&data->update_lock); \
 	data->in_##reg[nr] = IN_TO_REG(val); \
 	asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
 		data->in_##reg[nr]); \
+	up(&data->update_lock); \
 	return count; \
 }
 
@@ -329,8 +332,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct asb100_data *data = i2c_get_clientdata(client);
 	u32 val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
+	up(&data->update_lock);
+
 	return count;
 }
 
@@ -343,11 +350,14 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct asb100_data *data = i2c_get_clientdata(client);
-	unsigned long min = FAN_FROM_REG(data->fan_min[nr],
-			DIV_FROM_REG(data->fan_div[nr]));
+	unsigned long min;
 	unsigned long val = simple_strtoul(buf, NULL, 10);
 	int reg;
-	
+
+	down(&data->update_lock);
+
+	min = FAN_FROM_REG(data->fan_min[nr],
+			DIV_FROM_REG(data->fan_div[nr]));
 	data->fan_div[nr] = DIV_TO_REG(val);
 
 	switch(nr) {
@@ -373,6 +383,9 @@
 	data->fan_min[nr]  		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
+
+	up(&data->update_lock);
+
 	return count;
 }
 
@@ -450,6 +463,8 @@
 	struct i2c_client *client = to_i2c_client(dev); \
 	struct asb100_data *data = i2c_get_clientdata(client); \
 	unsigned long val = simple_strtoul(buf, NULL, 10); \
+\
+	down(&data->update_lock); \
 	switch (nr) { \
 	case 1: case 2: \
 		data->reg[nr] = LM75_TEMP_TO_REG(val); \
@@ -460,6 +475,7 @@
 	} \
 	asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
 			data->reg[nr]); \
+	up(&data->update_lock); \
 	return count; \
 }
 
@@ -560,9 +576,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct asb100_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->pwm &= 0x80; /* keep the enable bit */
 	data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
 	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -578,9 +597,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct asb100_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->pwm &= 0x0f; /* keep the duty cycle bits */
 	data->pwm |= (val ? 0x80 : 0x00);
 	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/ds1621.c +3 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/ds1621.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/ds1621.c
@@ -153,8 +153,11 @@
 {									\
 	struct i2c_client *client = to_i2c_client(dev);			\
 	struct ds1621_data *data = ds1621_update_client(dev);		\
+\
+	down(&data->update_lock);					\
 	data->value = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10));	\
 	ds1621_write_value(client, reg, data->value);			\
+	up(&data->update_lock);						\
 	return count;							\
 }
 
drivers/i2c/chips/fscher.c +25 -8
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/fscher.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/fscher.c
@@ -464,9 +464,11 @@
 {
 	/* bits 0..1, 3..7 reserved => mask with 0x04 */  
 	unsigned long v = simple_strtoul(buf, NULL, 10) & 0x04;
+	
+	down(&data->update_lock);
 	data->fan_status[FAN_INDEX_FROM_NUM(nr)] &= ~v;
-
 	fscher_write_value(client, reg, v);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -480,9 +482,11 @@
 		       const char *buf, size_t count, int nr, int reg)
 {
 	unsigned long v = simple_strtoul(buf, NULL, 10);
-	data->fan_min[FAN_INDEX_FROM_NUM(nr)] = v > 0xff ? 0xff : v;
 
+	down(&data->update_lock);
+	data->fan_min[FAN_INDEX_FROM_NUM(nr)] = v > 0xff ? 0xff : v;
 	fscher_write_value(client, reg, data->fan_min[FAN_INDEX_FROM_NUM(nr)]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -497,6 +501,8 @@
 	/* supported values: 2, 4, 8 */
 	unsigned long v = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	switch (v) {
 	case 2: v = 1; break;
 	case 4: v = 2; break;
@@ -504,14 +510,16 @@
 	default:
 		dev_err(&client->dev, "fan_div value %ld not "
 			 "supported. Choose one of 2, 4 or 8!\n", v);
+		up(&data->update_lock);
 		return -EINVAL;
 	}
 
 	/* bits 2..7 reserved => mask with 0x03 */
 	data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] &= ~0x03;
 	data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] |= v;
-
 	fscher_write_value(client, reg, data->fan_ripple[FAN_INDEX_FROM_NUM(nr)]);
+
+	up(&data->update_lock);
 	return count;
 }
 
@@ -537,9 +545,11 @@
 {
 	/* bits 2..7 reserved, 0 read only => mask with 0x02 */  
 	unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
-	data->temp_status[TEMP_INDEX_FROM_NUM(nr)] &= ~v;
 
+	down(&data->update_lock);
+	data->temp_status[TEMP_INDEX_FROM_NUM(nr)] &= ~v;
 	fscher_write_value(client, reg, v);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -592,9 +602,11 @@
 {
 	/* bits 1..7 reserved => mask with 0x01 */  
 	unsigned long v = simple_strtoul(buf, NULL, 10) & 0x01;
-	data->global_control &= ~v;
 
+	down(&data->update_lock);
+	data->global_control &= ~v;
 	fscher_write_value(client, reg, v);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -612,10 +624,12 @@
 {
 	/* bits 0..3 reserved => mask with 0xf0 */  
 	unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
+
+	down(&data->update_lock);
 	data->watchdog[2] &= ~0xf0;
 	data->watchdog[2] |= v;
-
 	fscher_write_value(client, reg, data->watchdog[2]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -630,9 +644,11 @@
 {
 	/* bits 0, 2..7 reserved => mask with 0x02 */  
 	unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
-	data->watchdog[1] &= ~v;
 
+	down(&data->update_lock);
+	data->watchdog[1] &= ~v;
 	fscher_write_value(client, reg, v);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -645,9 +661,10 @@
 static ssize_t set_watchdog_preset(struct i2c_client *client, struct fscher_data *data,
 				   const char *buf, size_t count, int nr, int reg)
 {
+	down(&data->update_lock);
 	data->watchdog[0] = simple_strtoul(buf, NULL, 10) & 0xff;
-
 	fscher_write_value(client, reg, data->watchdog[0]);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/fscpos.c +9 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/fscpos.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/fscpos.c
@@ -206,11 +206,13 @@
 		return -EINVAL;
 	}
 	
+	down(&data->update_lock);
 	/* bits 2..7 reserved => mask with 0x03 */
 	data->fan_ripple[nr - 1] &= ~0x03;
 	data->fan_ripple[nr - 1] |= v;
 	
 	fscpos_write_value(client, reg, data->fan_ripple[nr - 1]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -228,8 +230,10 @@
 	if (v < 0) v = 0;
 	if (v > 255) v = 255;
 
+	down(&data->update_lock);
 	data->pwm[nr - 1] = v;
 	fscpos_write_value(client, reg, data->pwm[nr - 1]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -271,10 +275,13 @@
 {
 	/* bits 0..3 reserved => mask with 0xf0 */
 	unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
+
+	down(&data->update_lock);
 	data->wdog_control &= ~0xf0;
 	data->wdog_control |= v;
 	
 	fscpos_write_value(client, reg, data->wdog_control);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -296,9 +303,11 @@
 		return -EINVAL;
 	}
 
+	down(&data->update_lock);
 	data->wdog_state &= ~v;
 	
 	fscpos_write_value(client, reg, v);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/gl518sm.c +17 -3
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/gl518sm.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/gl518sm.c
@@ -211,8 +211,11 @@
 	struct i2c_client *client = to_i2c_client(dev);			\
 	struct gl518_data *data = i2c_get_clientdata(client);		\
 	long val = simple_strtol(buf, NULL, 10);			\
+\
+	down(&data->update_lock);					\
 	data->value = type##_TO_REG(val);				\
 	gl518_write_value(client, reg, data->value);			\
+	up(&data->update_lock);						\
 	return count;							\
 }
 
@@ -222,11 +225,15 @@
 {									\
 	struct i2c_client *client = to_i2c_client(dev);			\
 	struct gl518_data *data = i2c_get_clientdata(client);		\
-	int regvalue = gl518_read_value(client, reg);			\
+	int regvalue;							\
 	unsigned long val = simple_strtoul(buf, NULL, 10);		\
+\
+	down(&data->update_lock);					\
+	regvalue = gl518_read_value(client, reg);			\
 	data->value = type##_TO_REG(val);				\
 	regvalue = (regvalue & ~mask) | (data->value << shift);		\
 	gl518_write_value(client, reg, regvalue);			\
+	up(&data->update_lock);						\
 	return count;							\
 }
 
@@ -255,7 +262,10 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct gl518_data *data = i2c_get_clientdata(client);
-	int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
+	int regvalue;
+
+	down(&data->update_lock);
+	regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
 
 	data->fan_min[0] = FAN_TO_REG(simple_strtoul(buf, NULL, 10),
 		DIV_FROM_REG(data->fan_div[0]));
@@ -270,6 +280,7 @@
 	data->beep_mask &= data->alarm_mask;
 	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -277,8 +288,10 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct gl518_data *data = i2c_get_clientdata(client);
-	int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
+	int regvalue;
 
+	down(&data->update_lock);
+	regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
 	data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10),
 		DIV_FROM_REG(data->fan_div[1]));
 	regvalue = (regvalue & 0xff00) | data->fan_min[1];
@@ -292,6 +305,7 @@
 	data->beep_mask &= data->alarm_mask;
 	gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
 
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/gl520sm.c +35 -5
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/gl520sm.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/gl520sm.c
@@ -299,6 +299,8 @@
 static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r;
 
 	if (n = 0)
@@ -313,12 +315,15 @@
 	else
 		gl520_write_value(client, reg, r);
 
+	up(&data->update_lock);
 	return count;
 }
 
 static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r;
 
 	if (n = 0)
@@ -333,6 +338,7 @@
 	else
 		gl520_write_value(client, reg, r);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -363,6 +369,8 @@
 static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
 	unsigned long v = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r = FAN_TO_REG(v, data->fan_div[n - 1]);
 
 	data->fan_min[n - 1] = r;
@@ -380,12 +388,15 @@
 	data->beep_mask &= data->alarm_mask;
 	gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
 
+	up(&data->update_lock);
 	return count;
 }
 
 static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
 	unsigned long v = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r;
 
 	switch (v) {
@@ -394,17 +405,22 @@
 	case 4: r = 2; break;
 	case 8: r = 3; break;
 	default:
-		dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
+		dev_err(&client->dev, "fan_div value %ld not supported. "
+				"Choose one of 1, 2, 4 or 8!\n", v);
+		up(&data->update_lock);
 		return -EINVAL;
 	}
 
 	data->fan_div[n - 1] = r;
 
 	if (n = 1)
-		gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
+		gl520_write_value(client, reg, (gl520_read_value(client, reg) 
+					& ~0xc0) | (r << 6));
 	else
-		gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
+		gl520_write_value(client, reg, (gl520_read_value(client, reg) 
+					& ~0x30) | (r << 4));
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -412,9 +428,12 @@
 {
 	u8 r = simple_strtoul(buf, NULL, 10)?1:0;
 
+	down(&data->update_lock);
 	data->fan_off = r;
-	gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
+	gl520_write_value(client, reg, (gl520_read_value(client, reg) 
+				& ~0x0c) | (r << 2));
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -439,22 +458,28 @@
 static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r = TEMP_TO_REG(v);
 
 	data->temp_max[n - 1] = r;
 	gl520_write_value(client, reg, r);
 
+	up(&data->update_lock);
 	return count;
 }
 
 static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r = TEMP_TO_REG(v);
 
 	data->temp_max_hyst[n - 1] = r;
 	gl520_write_value(client, reg, r);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -477,19 +502,24 @@
 {
 	u8 r = simple_strtoul(buf, NULL, 10)?0:1;
 
+	down(&data->update_lock);
 	data->beep_enable = !r;
-	gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
+	gl520_write_value(client, reg, (gl520_read_value(client, reg) 
+				& ~0x04) | (r << 2));
 
+	up(&data->update_lock);
 	return count;
 }
 
 static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
 {
+	down(&data->update_lock);
 	u8 r = simple_strtoul(buf, NULL, 10) & data->alarm_mask;
 
 	data->beep_mask = r;
 	gl520_write_value(client, reg, r);
 
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/max1619.c +3 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/max1619.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/max1619.c
@@ -141,8 +141,11 @@
 	struct i2c_client *client = to_i2c_client(dev); \
 	struct max1619_data *data = i2c_get_clientdata(client); \
 	long val = simple_strtol(buf, NULL, 10); \
+\
+	down(&data->update_lock); \
 	data->value = TEMP_TO_REG(val); \
 	i2c_smbus_write_byte_data(client, reg, data->value); \
+	up(&data->update_lock); \
 	return count; \
 }
 
drivers/i2c/chips/pcf8574.c +3 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/pcf8574.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/pcf8574.c
@@ -97,8 +97,11 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf8574_data *data = i2c_get_clientdata(client);
+
+	down(&data->update_lock);
 	data->write = simple_strtoul(buf, NULL, 10);
 	i2c_smbus_write_byte(client, data->write);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/pcf8591.c +6 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/pcf8591.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/pcf8591.c
@@ -124,8 +124,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf8591_data *data = i2c_get_clientdata(client);
 	if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
+
+		down(&data->update_lock);
 		data->aout = value;
 		i2c_smbus_write_byte_data(client, data->control, data->aout);
+		up(&data->update_lock);
 		return count;
 	}
 	return -EINVAL;
@@ -144,11 +147,14 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf8591_data *data = i2c_get_clientdata(client);
+
+	down(&data->update_lock);
 	if (simple_strtoul(buf, NULL, 10))
 		data->control |= PCF8591_CONTROL_AOEF;
 	else
 		data->control &= ~PCF8591_CONTROL_AOEF;
 	i2c_smbus_write_byte(client, data->control);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/sis5595.c +24 -3
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/sis5595.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/sis5595.c
@@ -232,8 +232,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct sis5595_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->in_min[nr] = IN_TO_REG(val);
 	sis5595_write_value(client, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -243,8 +246,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct sis5595_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->in_max[nr] = IN_TO_REG(val);
 	sis5595_write_value(client, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -305,8 +311,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct sis5595_data *data = i2c_get_clientdata(client);
 	long val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->temp_over = TEMP_TO_REG(val);
 	sis5595_write_value(client, SIS5595_REG_TEMP_OVER, data->temp_over);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -321,8 +330,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct sis5595_data *data = i2c_get_clientdata(client);
 	long val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->temp_hyst = TEMP_TO_REG(val);
 	sis5595_write_value(client, SIS5595_REG_TEMP_HYST, data->temp_hyst);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -353,8 +365,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct sis5595_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 	sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -373,10 +388,14 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct sis5595_data *data = i2c_get_clientdata(client);
-	unsigned long min = FAN_FROM_REG(data->fan_min[nr],
-			DIV_FROM_REG(data->fan_div[nr]));
+	unsigned long min;
 	unsigned long val = simple_strtoul(buf, NULL, 10);
-	int reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
+	int reg;
+
+	down(&data->update_lock);
+	min = FAN_FROM_REG(data->fan_min[nr],
+			DIV_FROM_REG(data->fan_div[nr]));
+	reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
 	switch (val) {
 	case 1: data->fan_div[nr] = 0; break;
 	case 2: data->fan_div[nr] = 1; break;
@@ -385,6 +404,7 @@
 	default:
 		dev_err(&client->dev, "fan_div value %ld not "
 			"supported. Choose one of 1, 2, 4 or 8!\n", val);
+		up(&data->update_lock);
 		return -EINVAL;
 	}
 	
@@ -400,6 +420,7 @@
 	data->fan_min[nr]  		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 	sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/via686a.c +18 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/via686a.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/via686a.c
@@ -363,9 +363,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->in_min[nr] = IN_TO_REG(val,nr);
 	via686a_write_value(client, VIA686A_REG_IN_MIN(nr), 
 			data->in_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_in_max(struct device *dev, const char *buf, 
@@ -373,9 +376,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->in_max[nr] = IN_TO_REG(val,nr);
 	via686a_write_value(client, VIA686A_REG_IN_MAX(nr), 
 			data->in_max[nr]);
+	up(&data->update_lock);
 	return count;
 }
 #define show_in_offset(offset)					\
@@ -434,8 +440,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->temp_over[nr] = TEMP_TO_REG(val);
 	via686a_write_value(client, VIA686A_REG_TEMP_OVER(nr), data->temp_over[nr]);
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_temp_hyst(struct device *dev, const char *buf, 
@@ -443,8 +452,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->temp_hyst[nr] = TEMP_TO_REG(val);
 	via686a_write_value(client, VIA686A_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
+	up(&data->update_lock);
 	return count;
 }
 #define show_temp_offset(offset)					\
@@ -502,8 +514,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 	via686a_write_value(client, VIA686A_REG_FAN_MIN(nr+1), data->fan_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_fan_div(struct device *dev, const char *buf, 
@@ -511,10 +526,13 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	int old = via686a_read_value(client, VIA686A_REG_FANDIV);
 	data->fan_div[nr] = DIV_TO_REG(val);
 	old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
 	via686a_write_value(client, VIA686A_REG_FANDIV, old);
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/w83627hf.c +29 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/w83627hf.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/w83627hf.c
@@ -354,10 +354,13 @@
 	u32 val; \
 	 \
 	val = simple_strtoul(buf, NULL, 10); \
+\
+	down(&data->update_lock); \
 	data->in_##reg[nr] = IN_TO_REG(val); \
 	w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
 			    data->in_##reg[nr]); \
 	 \
+	up(&data->update_lock); \
 	return count; \
 }
 store_in_reg(MIN, min)
@@ -442,6 +445,9 @@
 	u32 val;
 
 	val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
+	
 	if ((data->vrm_ovt & 0x01) &&
 		(w83627thf = data->type || w83637hf = data->type))
 
@@ -452,6 +458,7 @@
 		data->in_min[0] = IN_TO_REG(val);
 
 	w83627hf_write_value(client, W83781D_REG_IN_MIN(0), data->in_min[0]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -463,6 +470,9 @@
 	u32 val;
 
 	val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
+
 	if ((data->vrm_ovt & 0x01) &&
 		(w83627thf = data->type || w83637hf = data->type))
 		
@@ -473,6 +483,7 @@
 		data->in_max[0] = IN_TO_REG(val);
 
 	w83627hf_write_value(client, W83781D_REG_IN_MAX(0), data->in_max[0]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -508,11 +519,14 @@
 	u32 val;
 
 	val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->fan_min[nr - 1]  	    FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
 	w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
 			    data->fan_min[nr - 1]);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -573,6 +587,8 @@
 	u32 val; \
 	 \
 	val = simple_strtoul(buf, NULL, 10); \
+\
+	down(&data->update_lock); \
 	 \
 	if (nr >= 2) {	/* TEMP2 and TEMP3 */ \
 		data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
@@ -584,6 +600,7 @@
 			data->temp_##reg); \
 	} \
 	 \
+	up(&data->update_lock); \
 	return count; \
 }
 store_temp_reg(OVER, max);
@@ -692,6 +709,8 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	if (update_mask = BEEP_MASK) {	/* We are storing beep_mask */
 		data->beep_mask = BEEP_MASK_TO_REG(val);
 		w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
@@ -708,6 +727,7 @@
 	w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
 			    val2 | data->beep_enable << 7);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -753,6 +773,8 @@
 	unsigned long min;
 	u8 reg;
 
+	down(&data->update_lock);
+
 	/* Save fan_min */
 	min = FAN_FROM_REG(data->fan_min[nr],
 			   DIV_FROM_REG(data->fan_div[nr]));
@@ -773,6 +795,7 @@
 	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 	w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -815,6 +838,8 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	if (data->type = w83627thf) {
 		/* bits 0-3 are reserved  in 627THF */
 		data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
@@ -830,6 +855,7 @@
 				     data->pwm[nr - 1]);
 	}
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -871,6 +897,8 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	switch (val) {
 	case 1:		/* PII/Celeron diode */
 		tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
@@ -903,6 +931,7 @@
 		break;
 	}
 
+	up(&data->update_lock);
 	return count;
 }
 
drivers/i2c/chips/w83781d.c +28 -0
diff -Nur linux-2.6.12-rc1-mm3/drivers/i2c/chips/w83781d.c linux-2.6.12-rc1-mm3b/drivers/i2c/chips/w83781d.c
@@ -302,9 +302,12 @@
 	u32 val; \
 	 \
 	val = simple_strtoul(buf, NULL, 10) / 10; \
+\
+	down(&data->update_lock); \
 	data->in_##reg[nr] = IN_TO_REG(val); \
 	w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
 	 \
+	up(&data->update_lock); \
 	return count; \
 }
 store_in_reg(MIN, min);
@@ -369,11 +372,14 @@
 	u32 val;
 
 	val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->fan_min[nr - 1]  	    FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
 	w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
 			    data->fan_min[nr - 1]);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -431,6 +437,8 @@
 	s32 val; \
 	 \
 	val = simple_strtol(buf, NULL, 10); \
+\
+	down(&data->update_lock); \
 	 \
 	if (nr >= 2) {	/* TEMP2 and TEMP3 */ \
 		data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
@@ -442,6 +450,7 @@
 			data->temp_##reg); \
 	} \
 	 \
+	up(&data->update_lock); \
 	return count; \
 }
 store_temp_reg(OVER, max);
@@ -554,6 +563,8 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	if (update_mask = BEEP_MASK) {	/* We are storing beep_mask */
 		data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
 		w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
@@ -573,6 +584,7 @@
 	w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
 			    val2 | data->beep_enable << 7);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -616,6 +628,8 @@
 	unsigned long min;
 	u8 reg;
 
+	down(&data->update_lock);
+	
 	/* Save fan_min */
 	min = FAN_FROM_REG(data->fan_min[nr],
 			   DIV_FROM_REG(data->fan_div[nr]));
@@ -640,6 +654,7 @@
 	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 	w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -686,9 +701,12 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	data->pwm[nr - 1] = PWM_TO_REG(val);
 	w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -701,6 +719,8 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	switch (val) {
 	case 0:
 	case 1:
@@ -716,9 +736,11 @@
 		break;
 
 	default:
+		up(&data->update_lock);
 		return -EINVAL;
 	}
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -780,6 +802,8 @@
 
 	val = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	switch (val) {
 	case 1:		/* PII/Celeron diode */
 		tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
@@ -811,6 +835,7 @@
 		break;
 	}
 
+	up(&data->update_lock);
 	return count;
 }
 
@@ -859,6 +884,8 @@
 	struct w83781d_data *data = i2c_get_clientdata(client);
 	u32 val, i;
 
+	down(&data->update_lock);
+
 	for (i = 0; i < count; i++) {
 		val = simple_strtoul(buf + count, NULL, 10);
 
@@ -869,6 +896,7 @@
 				    data->rt[nr - 1][i]);
 	}
 
+	up(&data->update_lock);
 	return count;
 }
 
#1 lore raw
Quoted 8 lines · Grant Coady
Quoted 5 lines · Grant Coady

Greetings,

This patch adds locking around set operations that access data->something and go out to the chip.

Lucky last: it87

it87 needs a small fixup, oops. This applies over previous patch in this series. Again, this time inline, enough for today :o)

drivers/i2c/chips/it87.c renamed from drivers/i2c/chips/it87.c~ +5 -3
@@ -442,8 +442,10 @@
 	    data->sensor |= 1 << nr;
 	else if (val = 2)
 	    data->sensor |= 8 << nr;
-	else if (val != 0)
+	else if (val != 0) {
+		up(&data->update_lock);
 		return -EINVAL;
+	}
 	it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
 	up(&data->update_lock);
 	return count;
@@ -583,11 +585,11 @@
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
 
-	down(&data->update_lock);
-
 	if (val < 0 || val > 255)
 		return -EINVAL;
 
+	down(&data->update_lock);
+
 	data->manual_pwm_ctl[nr] = val;
 	if (data->fan_main_ctrl & (1 << nr))
 		it87_write_value(client, IT87_REG_PWM(nr),
#2 lore raw
In reply to Grant Coady

Hi Grant,

drivers/i2c/chips/ds1621.c

Quoted 2 lines
drivers/i2c/chips/ds1621.c +0 -0

@@ -153,8 +153,11 @@

drivers/i2c/chips/ds1621.c line 156

Quoted 4 lines

{ \

drivers/i2c/chips/ds1621.c +1 -0
 	struct i2c_client *client = to_i2c_client(dev);			\
 	struct ds1621_data *data = ds1621_update_client(dev);		\
+\

Please keep the backslash aligned with the other lines.

Quoted 12 lines
drivers/i2c/chips/fscher.c +2 -0
@@ -497,6 +501,8 @@
 	/* supported values: 2, 4, 8 */
 	unsigned long v = simple_strtoul(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	switch (v) {
 	case 2: v = 1; break;
 	case 4: v = 2; break;

drivers/i2c/chips/fscher.c line 516

Quoted 7 lines
drivers/i2c/chips/fscher.c +1 -0
@@ -504,14 +510,16 @@
 	default:
 		dev_err(&client->dev, "fan_div value %ld not "
 			 "supported. Choose one of 2, 4 or 8!\n", v);
+		up(&data->update_lock);
 		return -EINVAL;
 	}
 

You can move the down() after the swicth block here.

drivers/i2c/chips/fscher.c line 670

Quoted 12 lines
drivers/i2c/chips/fscher.c +2 -1
@@ -645,9 +661,10 @@
 static ssize_t set_watchdog_preset(struct i2c_client *client, struct
 fscher_data *data,
 				   const char *buf, size_t count, int nr, int reg)
 {
+	down(&data->update_lock);
 	data->watchdog[0] = simple_strtoul(buf, NULL, 10) & 0xff;
-
 	fscher_write_value(client, reg, data->watchdog[0]);
+	up(&data->update_lock);
 	return count;
 }

Please move the call to simple_strtoul() outside of the lock, like the other functions do.

drivers/i2c/chips/fscpos.c

Quoted 2 lines
drivers/i2c/chips/fscpos.c +0 -0

You missed set_wdog_preset.

drivers/i2c/chips/gl518sm.c

Quoted 2 lines
drivers/i2c/chips/gl518sm.c +0 -0

@@ -211,8 +211,11 @@

drivers/i2c/chips/gl518sm.c line 214

Quoted 4 lines
drivers/i2c/chips/gl518sm.c +1 -0
 	struct i2c_client *client = to_i2c_client(dev);			\
 	struct gl518_data *data = i2c_get_clientdata(client);		\
 	long val = simple_strtol(buf, NULL, 10);			\
+\

Keep it aligned.

drivers/i2c/chips/gl518sm.c line 230

Quoted 8 lines
drivers/i2c/chips/gl518sm.c +2 -1
@@ -222,11 +225,15 @@
 {									\
 	struct i2c_client *client = to_i2c_client(dev);			\
 	struct gl518_data *data = i2c_get_clientdata(client);		\
-	int regvalue = gl518_read_value(client, reg);			\
+	int regvalue;							\
 	unsigned long val = simple_strtoul(buf, NULL, 10);		\
+\

Same here.

drivers/i2c/chips/gl518sm.c line 266

Quoted 7 lines
drivers/i2c/chips/gl518sm.c +2 -1
@@ -255,7 +262,10 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct gl518_data *data = i2c_get_clientdata(client);
-	int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
+	int regvalue;
+

You could delete think blank line so as to keep set_fan_min1 and set_fan_min2 "visualy identical".

drivers/i2c/chips/gl520sm.c

Quoted 2 lines
drivers/i2c/chips/gl520sm.c +0 -0

@@ -299,6 +299,8 @@

drivers/i2c/chips/gl520sm.c line 304

Quoted 5 lines

static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) {

drivers/i2c/chips/gl520sm.c +2 -0
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r;

Nah, this won't work. All variable declarations must happen prior to any instruction. Additionally, you can move the down() below the if statement where the value of r is computed.

drivers/i2c/chips/gl520sm.c line 304

Quoted 5 lines

static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) {

drivers/i2c/chips/gl520sm.c +2 -0
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r;

Same here, obviously

drivers/i2c/chips/gl520sm.c line 374

Quoted 7 lines
drivers/i2c/chips/gl520sm.c +2 -0
@@ -363,6 +369,8 @@
 static ssize_t set_fan_min(struct i2c_client *client, struct
 gl520_data *data, const char *buf, size_t count, int n, int reg) {
 	unsigned long v = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r = FAN_TO_REG(v, data->fan_div[n - 1]);

Meeeep! ;)
You'll have to split the declatation from the assignment.

drivers/i2c/chips/gl520sm.c line 400

Quoted 5 lines
drivers/i2c/chips/gl520sm.c +2 -0
 static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) {
 	unsigned long v = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r;

Same here. Additionally, you can move the down() after the switch block.

drivers/i2c/chips/gl520sm.c line 421

Quoted 8 lines
drivers/i2c/chips/gl520sm.c +4 -2
 	if (n = 1)
-		gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
+		gl520_write_value(client, reg, (gl520_read_value(client, reg)
+					& ~0xc0) | (r << 6));
 	else
-		gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
+		gl520_write_value(client, reg, (gl520_read_value(client, reg)
+					& ~0x30) | (r << 4));

Please don't include changes which are not related to the locking issue. You'll have all the time to propose changes later.

drivers/i2c/chips/gl520sm.c line 434

Quoted 3 lines
drivers/i2c/chips/gl520sm.c +2 -1
-	gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
+	gl520_write_value(client, reg, (gl520_read_value(client, reg)
+				& ~0x0c) | (r << 2));

Same here, don't.

drivers/i2c/chips/gl520sm.c line 462

Quoted 6 lines
drivers/i2c/chips/gl520sm.c +2 -0
@@ -439,22 +458,28 @@
 static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) {
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r = TEMP_TO_REG(v);

Wrong order again.

drivers/i2c/chips/gl520sm.c line 463

Quoted 5 lines
drivers/i2c/chips/gl520sm.c +2 -0
 static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) {
 	long v = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	u8 r = TEMP_TO_REG(v);

Here as well.

drivers/i2c/chips/gl520sm.c line 508

Quoted 3 lines
drivers/i2c/chips/gl520sm.c +2 -1
-	gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
+	gl520_write_value(client, reg, (gl520_read_value(client, reg)
+				& ~0x04) | (r << 2));

Don't.

drivers/i2c/chips/gl520sm.c line 517

Quoted 3 lines
drivers/i2c/chips/gl520sm.c +1 -0
 static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) {
+	down(&data->update_lock);
 	u8 r = simple_strtoul(buf, NULL, 10) & data->alarm_mask;

Wrong order, you have to split it.

Mmmm, looks like gl520sm wasn't your best one ;)

Skipping pcf8574 and pcf8591, they are different chip and might need special handling.

drivers/i2c/chips/gl520sm.c line 531

Quoted 9 lines
drivers/i2c/chips/gl520sm.c +3 -0
 --- linux-2.6.12-rc1-mm3/drivers/i2c/chips/via686a.c	2005-03-23 06:34:26.000000000 +1100
+++ linux-2.6.12-rc1-mm3b/drivers/i2c/chips/via686a.c	2005-03-26 11:40:20.000000000 +1100
@@ -511,10 +526,13 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct via686a_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	int old = via686a_read_value(client, VIA686A_REG_FANDIV);

Wrong order here, move declaration up.

drivers/i2c/chips/w83627hf.c line 357

Quoted 7 lines
drivers/i2c/chips/w83627hf.c +1 -0
@@ -354,10 +354,13 @@
 	u32 val; \
 	 \
 	val = simple_strtoul(buf, NULL, 10); \
+\

Please align like the other blank lines.

drivers/i2c/chips/w83627hf.c line 590

Quoted 5 lines
drivers/i2c/chips/w83627hf.c +1 -0
@@ -573,6 +587,8 @@
 	u32 val; \
 	 \
 	val = simple_strtoul(buf, NULL, 10); \
+\

Same here.

drivers/i2c/chips/w83781d.c line 305

Quoted 7 lines
drivers/i2c/chips/w83781d.c +1 -0
@@ -302,9 +302,12 @@
 	u32 val; \
 	 \
 	val = simple_strtoul(buf, NULL, 10) / 10; \
+\

Same here.

drivers/i2c/chips/w83781d.c line 441

Quoted 6 lines
drivers/i2c/chips/w83781d.c +2 -0
@@ -431,6 +437,8 @@
 	s32 val; \
 	 \
 	val = simple_strtol(buf, NULL, 10); \
+\
+	down(&data->update_lock); \

And here.

drivers/i2c/chips/w83781d.c line 890

Quoted 8 lines
drivers/i2c/chips/w83781d.c +2 -0
@@ -859,6 +884,8 @@
 	struct w83781d_data *data = i2c_get_clientdata(client);
 	u32 val, i;
 
+	down(&data->update_lock);
+
 	for (i = 0; i < count; i++) {
 		val = simple_strtoul(buf + count, NULL, 10);

BTW, there is a bug here, isn't it? "buf + count" makes no sense? "buf + i" would make more, but would still not work as far as I can see. It's plain broken to me. This would tend to prove that nobody ever used this function... Couldn't we just get rid of that RT thing? I always wondered who was using this, and now the answer seems to be "no one".

Soooo... that's not so bad (except gl520sm), please fix all points I listed here and post new patches.

I would also appreciate if you could review my own patches now. There is no reason you would have made mistakes and I wouldn't, after all. The best way to check is to apply the patch and check the new driver code. Checking the patches directly doesn't help that much, as you'll miss the in-function returns lacking up() and the functions lacking locking altogether.

Remaining drivers:
ds1337, isp1301_omap, m41t00, rtc8564: Don't use sysfs, so we don't care
smsc47b397: read-only, so no changes needed.

So we covered all chip driver :) Once cross-review and fixes are done we can build the big patch and send it over.

Thanks,

Signature · 1 line
Jean Delvare
#3 lore raw
In reply to Grant Coady

Hi Grant

Quoted 1 line

Lucky last: it87

I'm commenting on the second version of the patch.

show_sensor is a read function, not write, but I think it needs locking (the way it is now, at least).

static ssize_t show_sensor(struct device *dev, char *buf, int nr)

{
	struct it87_data *data = it87_update_device(dev);
	if (data->sensor & (1 << nr))
		return sprintf(buf, "3\n");  /* thermal diode */
	if (data->sensor & (8 << nr))
		return sprintf(buf, "2\n");  /* thermistor */
	return sprintf(buf, "0\n");      /* disabled */
}

data->sensor is evaluated twice, and could change in the meantime... e.g. if value is "thermistor first", and changed to "thermal diode" between the calls, we'll return "disabled", which is not correct.

One simple fix if we don't want to lock is to store the value of data->sensor in a local variable, and use it afterwards:

static ssize_t show_sensor(struct device *dev, char *buf, int nr)

{
	struct it87_data *data = it87_update_device(dev);
	u8 reg = data->sensor; /* In case the value is updated while we use it */

	if (reg & (1 << nr))
		return sprintf(buf, "3\n");  /* thermal diode */
	if (reg & (8 << nr))
		return sprintf(buf, "2\n");  /* thermistor */
	return sprintf(buf, "0\n");      /* disabled */
}
Quoted 10 lines

static ssize_t set_fan_div(struct device *dev, const char *buf,
size_t count, int nr)

{
	struct i2c_client *client = to_i2c_client(dev);
	struct it87_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
	int i, min[3];

	down(&data->update_lock);
	u8 old = it87_read_value(client, IT87_REG_FAN_DIV);

Wrong order, you can't do that.

Rest looks OK to me.

Thanks,

Signature · 1 line
Jean Delvare
#4 lore raw
In reply to Grant Coady
Quoted 5 lines
Quoted 1 line

Lucky last: it87

I'm commenting on the second version of the patch. (...)
Rest looks OK to me.

Ooops, missed this:

Review

Quoted 3 lines
(quoted diff) +2 -1
-		it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
+		it87_write_value(client, IT87_REG_PWM(nr),
+				PWM_TO_REG(data->manual_pwm_ctl[nr]));

Don't do that, it doesn't belong there.

Thanks,

Signature · 1 line
Jean Delvare
#5 lore raw
In reply to Grant Coady

Hi Grant,

Quoted 6 lines
Quoted 2 lines

Skipping pcf8574 and pcf8591, they are different chip and might need special handling.

does that mean revert and ignore those two? skip them for now? pcf8591 seems clean, I left it in, the other has a problem, left it out.

I discusses these with Aurelien Jarno, which knows the drivers better than I do as he actually uses them, and our conclusion are:

* pcf8574: no locking is needed AT ALL (nor even in the update function, which BTW should be merged into show_read). So forget about this one for this patch set. I have a specific patch ready for this one, which I'll post separately.

* pcf8591: The lock on data->control is actually needed (second part of the patch) but the lock on data->aout is not, because nowhere else this value is modified.

Quoted 1 line

Alright, I made a separate patch to remove it --> kill #ifdef blocks.

Thanks. You only forgot to kill this line:
#define W83781D_RT 1

Quoted 9 lines

for x in /home/public/patch/set-locking/*.diff; do cat $x | patch -p1; done

patching file drivers/i2c/chips/lm87.c
Hunk #3 FAILED at 326.
Hunk #4 FAILED at 338.
2 out of 8 hunks FAILED -- saving rejects to file
drivers/i2c/chips/lm87.c.rej

Quoted 3 lines

Let's see if I can work it out...

Fixed it, you get yours rediff'd to -mm3 for free :)

Hmm no that's not correct. My original patch was correct but assumed that you apply this preliminary patch before:
http://khali.linux-fr.org/devel/i2c/linux-2.6/linux-2.6.12-rc1-mm2-i2c-lm87-indent.diff

It was posted on the list already:
http://archives.andrew.net.au/lm-sensors/msg30209.html
So I know Greg will pick it before applying our big lock-on-write patch. Please restore my original version of the patch.

Quoted 5 lines

bugfix

pcf8574.c set_write needs something to limit value to byte size, I added "unsigned long val = simple_strtoul(buf, NULL, 10) & 0xff;" but have left it out of the patchset above.

I have come to something better and more complete, I think.
http://jdelvare.net1.nerim.net/sensors/linux-2.6.12-rc1-mm3-i2c-pcf8574-cleanups.diff
Aurelien Jarno will test it.

Quoted 8 lines

fscher.c set_watchdog_status, I had altered the code but put it back to the way it was for you to check logic:

        down(&data->update_lock);
        data->watchdog[1] &= ~v;
        fscher_write_value(client, reg, v);
        up(&data->update_lock);
Quoted 5 lines

safer or different with:

	fscher_write_value(client, reg, data->watchdog[1]); 

.. instead? Dunno, thought I better leave it as found.

The code looks strange to me too, but I wouldn't change it, at least not without reading the datasheet twice first.

Quoted 2 lines

w83781d.c remove W83781D_RT I made a patch to do this, applies after locking patch.

Correct.

Thanks,

Signature · 1 line
Jean Delvare
#6 lore raw
In reply to Grant Coady

Hi Khali,

Quoted 1 line · Jean Delvare

Mmmm, looks like gl520sm wasn't your best one ;)

horrible, musta got distracted.

Quoted 2 lines

Skipping pcf8574 and pcf8591, they are different chip and might need special handling.

does that mean revert and ignore those two? skip them for now? pcf8591 seems clean, I left it in, the other has a problem, left it out.

Quoted 8 lines

drivers/i2c/chips/w83781d.c line 890

Quoted 2 lines
drivers/i2c/chips/w83781d.c +0 -0
 	for (i = 0; i < count; i++) {
 		val = simple_strtoul(buf + count, NULL, 10);

BTW, there is a bug here, isn't it? "buf + count" makes no sense? "buf + i" would make more, but would still not work as far as I can see. It's plain broken to me. This would tend to prove that nobody ever used this function... Couldn't we just get rid of that RT thing? I always wondered who was using this, and now the answer seems to be "no one".

Alright, I made a separate patch to remove it --> kill #ifdef blocks.

on whitespace, line folding:
I see what you mean, no gratuitous cleanups while patching a particular issue. Makes sense.

Quoted 2 lines

Soooo... that's not so bad (except gl520sm), please fix all points I listed here and post new patches.

Thanks. No problems.

other post:

Quoted 6 lines

data->sensor is evaluated twice, and could change in the meantime... e.g. if value is "thermistor first", and changed to "thermal diode" between the calls, we'll return "disabled", which is not correct.

One simple fix if we don't want to lock is to store the value of data->sensor in a local variable, and use it afterwards:

just released lock held during it87_update_device(dev), so we don't take lock again, I put in your suggestion. Can't hurt.

Quoted 6 lines

I would also appreciate if you could review my own patches now. There is no reason you would have made mistakes and I wouldn't, after all. The best way to check is to apply the patch and check the new driver code. Checking the patches directly doesn't help that much, as you'll miss the in-function returns lacking up() and the functions lacking locking altogether.

for x in /home/public/patch/set-locking/*.diff; do cat $x | patch -p1; done

patching file drivers/i2c/chips/lm87.c
Hunk #3 FAILED at 326.
Hunk #4 FAILED at 338.
2 out of 8 hunks FAILED -- saving rejects to file drivers/i2c/chips/lm87.c.rej

Let's see if I can work it out...

Fixed it, you get yours rediff'd to -mm3 for free :)

Quoted 7 lines

Remaining drivers:
ds1337, isp1301_omap, m41t00, rtc8564: Don't use sysfs, so we don't care
smsc47b397: read-only, so no changes needed.

So we covered all chip driver :) Once cross-review and fixes are done we can build the big patch and send it over.

Okay, I review 28 files, adm1016, adm1031, lm85 moved the 'val =' assignment above lock to match the rest, additional small whitespace fixups in my patches removed to match original, found nothing with your patches, apart from mm2 -> mm3 hiccup noted above.

for x in /home/public/*.diff; do cat $x |patch -p1;done patching file drivers/i2c/chips/adm1025.c patching file drivers/i2c/chips/adm1026.c patching file drivers/i2c/chips/adm1031.c patching file drivers/i2c/chips/asb100.c patching file drivers/i2c/chips/ds1621.c patching file drivers/i2c/chips/fscher.c patching file drivers/i2c/chips/fscpos.c patching file drivers/i2c/chips/gl518sm.c patching file drivers/i2c/chips/gl520sm.c patching file drivers/i2c/chips/it87.c patching file drivers/i2c/chips/lm63.c patching file drivers/i2c/chips/lm75.c patching file drivers/i2c/chips/lm77.c patching file drivers/i2c/chips/lm78.c patching file drivers/i2c/chips/lm80.c patching file drivers/i2c/chips/lm83.c patching file drivers/i2c/chips/lm85.c patching file drivers/i2c/chips/lm87.c patching file drivers/i2c/chips/lm90.c patching file drivers/i2c/chips/lm92.c patching file drivers/i2c/chips/max1619.c patching file drivers/i2c/chips/pc87360.c patching file drivers/i2c/chips/pcf8591.c patching file drivers/i2c/chips/sis5595.c patching file drivers/i2c/chips/smsc47m1.c patching file drivers/i2c/chips/via686a.c patching file drivers/i2c/chips/w83627hf.c patching file drivers/i2c/chips/w83781d.c

.config has all sensors as modules + ISA + PIIX4 + algos <m> + debug

Assuming this one compiles okay I'll post one big new patch, for you to review and co-sign if I got my bit right. I'll replace files in http://scatter.mine.nu/lmsensors/lock-on-set/ to suit, as well provide as an all-lock-on-set.tar.bz2.

bugfix

pcf8574.c set_write needs something to limit value to byte size, I added "unsigned long val = simple_strtoul(buf, NULL, 10) & 0xff;" but have left it out of the patchset above.

fscher.c set_watchdog_status, I had altered the code but put it back to the way it was for you to check logic:

        down(&data->update_lock);
        data->watchdog[1] &= ~v;
        fscher_write_value(client, reg, v);
        up(&data->update_lock);

safer or different with:

	fscher_write_value(client, reg, data->watchdog[1]); 

. instead? Dunno, thought I better leave it as found.

w83781d.c remove W83781D_RT I made a patch to do this, applies after locking patch.

All drivers compile tested, and again as a unit from which I'll drag out the big patch matching list above.

Also send bugfix as RFC patches again.

Cheers,
Grant.

#7 lore raw
In reply to Grant Coady
Quoted 5 lines · Grant Coady

Greetings,

This patch adds locking around set operations that access data->something and go out to the chip.

Lucky last: it87

drivers/i2c/chips/it87.c +32 -2
@@ -265,9 +265,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->in_min[nr] = IN_TO_REG(val);
 	it87_write_value(client, IT87_REG_VIN_MIN(nr), 
 			data->in_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_in_max(struct device *dev, const char *buf, 
@@ -276,9 +279,12 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
 	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->in_max[nr] = IN_TO_REG(val);
 	it87_write_value(client, IT87_REG_VIN_MAX(nr), 
 			data->in_max[nr]);
+	up(&data->update_lock);
 	return count;
 }
 
@@ -356,8 +362,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->temp_high[nr] = TEMP_TO_REG(val);
 	it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_temp_min(struct device *dev, const char *buf, 
@@ -366,8 +375,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->temp_low[nr] = TEMP_TO_REG(val);
 	it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
+	up(&data->update_lock);
 	return count;
 }
 #define show_temp_offset(offset)					\
@@ -421,6 +433,8 @@
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	data->sensor &= ~(1 << nr);
 	data->sensor &= ~(8 << nr);
 	/* 3 = thermal diode; 2 = thermistor; 0 = disabled */
@@ -431,6 +445,7 @@
 	else if (val != 0)
 		return -EINVAL;
 	it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
+	up(&data->update_lock);
 	return count;
 }
 #define show_sensor_offset(offset)					\
@@ -484,8 +499,11 @@
 	struct i2c_client *client = to_i2c_client(dev);
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
+
+	down(&data->update_lock);
 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 	it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_fan_div(struct device *dev, const char *buf, 
@@ -495,6 +513,8 @@
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
 	int i, min[3];
+
+	down(&data->update_lock);
 	u8 old = it87_read_value(client, IT87_REG_FAN_DIV);
 
 	for (i = 0; i < 3; i++)
@@ -522,6 +542,7 @@
 		data->fan_min[i]úN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
 		it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
 	}
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_pwm_enable(struct device *dev, const char *buf,
@@ -531,6 +552,8 @@
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	if (val = 0) {
 		int tmp;
 		/* make sure the fan is on when in on/off mode */
@@ -545,9 +568,12 @@
 		it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
 		/* set saved pwm value, clear FAN_CTLX PWM mode bit */
 		it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
-	} else
+	} else {
+		up(&data->update_lock);
 		return -EINVAL;
+	}
 
+	up(&data->update_lock);
 	return count;
 }
 static ssize_t set_pwm(struct device *dev, const char *buf,
@@ -557,13 +583,17 @@
 	struct it87_data *data = i2c_get_clientdata(client);
 	int val = simple_strtol(buf, NULL, 10);
 
+	down(&data->update_lock);
+
 	if (val < 0 || val > 255)
 		return -EINVAL;
 
 	data->manual_pwm_ctl[nr] = val;
 	if (data->fan_main_ctrl & (1 << nr))
-		it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
+		it87_write_value(client, IT87_REG_PWM(nr),
+				PWM_TO_REG(data->manual_pwm_ctl[nr]));
 
+	up(&data->update_lock);
 	return count;
 }
 
#8 lore raw
In reply to Grant Coady

Hi

Quoted 7 lines · Jean Delvare

I discusses these with Aurelien Jarno, which knows the drivers better than I do as he actually uses them, and our conclusion are:

* pcf8574: no locking is needed AT ALL (nor even in the update function, which BTW should be merged into show_read). So forget about this one for this patch set. I have a specific patch ready for this one, which I'll post separately.

Okay, drop it

Quoted 4 lines

* pcf8591: The lock on data->control is actually needed (second part of the patch) but the lock on data->aout is not, because nowhere else this value is modified.

Okay, change it

Quoted 5 lines
Quoted 2 lines

Alright, I made a separate patch to remove it --> kill #ifdef blocks.

Thanks. You only forgot to kill this line:
#define W83781D_RT 1

Grrr forgot to look where ifdef controlled from, will fix

Quoted 5 lines
Quoted 1 line

Fixed it, you get yours rediff'd to -mm3 for free :)

Hmm no that's not correct. My original patch was correct but assumed that you apply this preliminary patch before:
http://khali.linux-fr.org/devel/i2c/linux-2.6/linux-2.6.12-rc1-mm2-i2c-lm87-indent.diff

Oops... That was recent too, remember seeing it, sorry. And I looked at the 4 space tabs when I reviewed it, thinking that's odd, but we not to fix whitespace... didn't click.

Quoted 4 lines

It was posted on the list already:
http://archives.andrew.net.au/lm-sensors/msg30209.html
So I know Greg will pick it before applying our big lock-on-write patch. Please restore my original version of the patch.

Done

Quoted 9 lines
Quoted 5 lines

bugfix

pcf8574.c set_write needs something to limit value to byte size, I added "unsigned long val = simple_strtoul(buf, NULL, 10) & 0xff;" but have left it out of the patchset above.

I have come to something better and more complete, I think.
http://jdelvare.net1.nerim.net/sensors/linux-2.6.12-rc1-mm3-i2c-pcf8574-cleanups.diff
Aurelien Jarno will test it.

Okay, its out

Quoted 8 lines
Quoted 8 lines

fscher.c set_watchdog_status, I had altered the code but put it back to the way it was for you to check logic:

        down(&data->update_lock);
        data->watchdog[1] &= ~v;
        fscher_write_value(client, reg, v);
        up(&data->update_lock);
Quoted 8 lines
Quoted 5 lines

safer or different with:

	fscher_write_value(client, reg, data->watchdog[1]); 

.. instead? Dunno, thought I better leave it as found.

The code looks strange to me too, but I wouldn't change it, at least not without reading the datasheet twice first.

You too? Sort of thing I'd want to see in action.

Quoted 4 lines
Quoted 2 lines

w83781d.c remove W83781D_RT I made a patch to do this, applies after locking patch.

Correct.

After little repair job.

Can I leave the stuff for you to grab from
http://scatter.mine.nu/lmsensors/lock-on-set/

instead of spamming the list again? Mail user agent split a few lines short of full file. Or large diff as .gz attachment okay?

Haven't got sendmail setup to get out direct from from linux boxen yet (been putting that off since last century too).

Cheers,
Grant.

#9 lore raw
In reply to Grant Coady

Greetings,

Quoted 2 lines · Grant Coady

This patch adds locking around set operations that access data->something and go out to the chip.

One more: adm1021

as separate files from:
http://scatter.mine.nu/lmsensors/lock-on-set/

drivers/i2c/chips/adm1021.c +2 -0
@@ -165,8 +165,10 @@
 	struct adm1021_data *data = i2c_get_clientdata(client);	\
 	int temp = simple_strtoul(buf, NULL, 10);		\
 								\
+	down(&data->update_lock);				\
 	data->value = TEMP_TO_REG(temp);			\
 	adm1021_write_value(client, reg, data->value);		\
+	up(&data->update_lock);					\
 	return count;						\
 }
 set(temp_max, ADM1021_REG_TOS_W);