i2c driver changes for 2.5.72
PATCH 1/1 i2c driver changes for 2.5.72
Hi,
Here are some i2c driver changes for 2.5.72. These consist of some more bug fixes, and the addition of a lm78 i2c driver.
Please pull from: bk://kernel.bkbits.net/gregkh/linux/i2c-2.5
thanks,
greg k-h
drivers/i2c/busses/i2c-ali15x3.c | 18 drivers/i2c/busses/i2c-i801.c | 10 drivers/i2c/chips/Kconfig | 14 drivers/i2c/chips/Makefile | 1 drivers/i2c/chips/adm1021.c | 18 drivers/i2c/chips/lm78.c | 895 +++++++++++++++++++++++++++++++++++++++ drivers/i2c/chips/lm85.c | 41 + drivers/i2c/chips/w83781d.c | 86 ++- 8 files changed, 1019 insertions(+), 64 deletions(-)
-----
<margitsw:t-online.de>:
o I2C: Sensors patch for adm1021
o I2C: lm85 fixups
<mhoffman:lightlink.com>:
o I2C: w83781d bugfix
o i2c: Add lm78 sensor chip support
Greg Kroah-Hartman:
o I2C: fix resource leak in i2c-ali15x3.c
o I2C: add lm78 chip to Makefile
Martin Schlemmer:
o I2C: fix for previous W83627THF sensor chip patch
o I2C: ICH5 SMBus and W83627THF additions
PATCH 1/1 i2c driver changes for 2.5.72
ChangeSet 1.1318.3.1, 2003/06/16 11:31:30-07:00, [email protected]
[PATCH] i2c: Add lm78 sensor chip support
This patch vs. 2.5.70 adds support for LM78, LM78-J, and LM79 sensors chips based on lm_sensors project CVS. This works on one of my boards.
I want to draw attention to something I did with this driver by comparing it to it87.c in 2.5.70:
Quoted 2 lines
#define
IT87_INIT_TEMP_HIGH_1600
#defineIT87_INIT_TEMP_LOW_1200
The hardware uses degrees C, and sysfs uses degrees C * 1000. But these #defines are apparently in units of degrees C * 10. This arbitrary intermediate representation bugs me. And given the new 2.5 sysfs standard, it's unnecessary.
In this patch for lm78, I rewrote the conversion routines in terms of the sysfs units - getting rid of the intermediate nonsense. If there are no objections, I'm going to start passing patches to do this to the other sensor chip drivers in 2.5 as well. It would be nice to get some help with this too... especially since I don't have all that hardware at hand to test the results.
drivers/i2c/chips/Kconfig | 14 drivers/i2c/chips/lm78.c | 895 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 909 insertions(+)
drivers/i2c/chips/Kconfig +14 -0
@@ -62,6 +62,20 @@ in the lm_sensors package, which you can download at http://www.lm-sensors.nu +config SENSORS_LM78 + tristate " National Semiconductors LM78 and compatibles" + depends on I2C && EXPERIMENTAL + help + If you say yes here you get support for National Semiconductor LM78, + LM78-J and LM79. This can also be built as a module which can be + inserted and removed while the kernel is running. + + The module will be called lm78. + + You will also need the latest user-space utilties: you can find them + in the lm_sensors package, which you can download at + http://www.lm-sensors.nu + config SENSORS_VIA686A tristate " VIA686A" depends on I2C && EXPERIMENTAL
drivers/i2c/chips/lm78.c new file +895 -0
895 lines not on this page · raw message
ChangeSet 1.1318.3.2, 2003/06/16 11:31:43-07:00, [email protected]
[PATCH] I2C: lm85 fixups
OK Here's the patch which :
1) Fixes the race conditions
2) Correctly reports the temps :-)
3) Removes a bit of gunk in the defines which I forgot
drivers/i2c/chips/lm85.c | 41 ++++++++++++++++++++++++++++------------- 1 files changed, 28 insertions(+), 13 deletions(-)
drivers/i2c/chips/lm85.c +28 -13
@@ -148,20 +148,17 @@#define SCALE (val,from,to) (((val)*(to) + ((from)/2))/(from))#define INS_TO_REG (n,val) (SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255))#define INSEXT_FROM_REG (n,val,ext) (SCALE((val)*4 + (ext),192*4,lm85_scaling[n])) -/* #define INS_FROM_REG (n,val) (INSEXT_FROM_REG(n,val,0)) -*/ -#define INS_FROM_REG (n,val) ( ( (val*4*lm85_scaling[n]) + (192*4/2) ) / (192*4) )/* FAN speed is measured using 90kHz clock */ #define FAN_TO_REG (val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))#define FAN_FROM_REG (val) ((val)==0?-1:(val)==0xffff?0:5400000/(val)) -/* Temperature is reported in .01 degC increments */ -#define TEMP_TO_REG (val) (SENSORS_LIMIT(((val)+50)/100,-127,127)) -#define TEMPEXT_FROM_REG (val,ext) ((val)*100 + (ext)*25) +/* Temperature is reported in .001 degC increments */ +#define TEMP_TO_REG (val) (SENSORS_LIMIT(((val)+500)/1000,-127,127)) +#define TEMPEXT_FROM_REG (val,ext) ((val)*1000 + (ext)*250)#define TEMP_FROM_REG (val) (TEMPEXT_FROM_REG(val,0)) -#define EXTTEMP_TO_REG (val) (SENSORS_LIMIT((val)/25,-127,127)) +#define EXTTEMP_TO_REG (val) (SENSORS_LIMIT((val)/250,-127,127))#define PWM_TO_REG (val) (SENSORS_LIMIT(val,0,255))#define PWM_FROM_REG (val) (val)
@@ -437,10 +434,13 @@ {struct i2c_client *client =to_i2c_client (dev);struct lm85_data *data =i2c_get_clientdata (client); +int val; -int val =simple_strtol (buf,NULL ,10 ); +down (&data->update_lock); + val =simple_strtol (buf,NULL ,10 ); data->fan_min[nr] =FAN_TO_REG (val);lm85_write_value (client,LM85_REG_FAN_MIN (nr), data->fan_min[nr]); +up (&data->update_lock);return count; }
@@ -528,10 +528,13 @@ {struct i2c_client *client =to_i2c_client (dev);struct lm85_data *data =i2c_get_clientdata (client); +int val; -int val =simple_strtol (buf,NULL ,10 ); +down (&data->update_lock); + val =simple_strtol (buf,NULL ,10 ); data->pwm[nr] =PWM_TO_REG (val);lm85_write_value (client,LM85_REG_PWM (nr), data->pwm[nr]); +up (&data->update_lock);return count; }static ssize_t show_pwm_enable (struct device *dev,char *buf,int nr)
@@ -590,10 +593,13 @@ {struct i2c_client *client =to_i2c_client (dev);struct lm85_data *data =i2c_get_clientdata (client); +int val; -int val =simple_strtol (buf,NULL ,10 ); +down (&data->update_lock); + val =simple_strtol (buf,NULL ,10 ); data->in_min[nr] =INS_TO_REG (nr, val);lm85_write_value (client,LM85_REG_IN_MIN (nr), data->in_min[nr]); +up (&data->update_lock);return count; }static ssize_t show_in_max (struct device *dev,char *buf,int nr)
@@ -609,10 +615,13 @@ {struct i2c_client *client =to_i2c_client (dev);struct lm85_data *data =i2c_get_clientdata (client); +int val; -int val =simple_strtol (buf,NULL ,10 ); +down (&data->update_lock); + val =simple_strtol (buf,NULL ,10 ); data->in_max[nr] =INS_TO_REG (nr, val);lm85_write_value (client,LM85_REG_IN_MAX (nr), data->in_max[nr]); +up (&data->update_lock);return count; }#define show_in_reg (offset) \
@@ -673,10 +682,13 @@ {struct i2c_client *client =to_i2c_client (dev);struct lm85_data *data =i2c_get_clientdata (client); +int val; -int val =simple_strtol (buf,NULL ,10 ); +down (&data->update_lock); + val =simple_strtol (buf,NULL ,10 ); data->temp_min[nr] =TEMP_TO_REG (val);lm85_write_value (client,LM85_REG_TEMP_MIN (nr), data->temp_min[nr]); +up (&data->update_lock);return count; }static ssize_t show_temp_max (struct device *dev,char *buf,int nr)
@@ -692,10 +704,13 @@ {struct i2c_client *client =to_i2c_client (dev);struct lm85_data *data =i2c_get_clientdata (client); +int val; -int val =simple_strtol (buf,NULL ,10 ); +down (&data->update_lock); + val =simple_strtol (buf,NULL ,10 ); data->temp_max[nr] =TEMP_TO_REG (val);lm85_write_value (client,LM85_REG_TEMP_MAX (nr), data->temp_max[nr]); +up (&data->update_lock);return count; }#define show_temp_reg (offset) \
ChangeSet 1.1318.3.3, 2003/06/16 11:31:55-07:00, [email protected]
[PATCH] I2C: w83781d bugfix
My first patch was naive; the patch below solves the problem by letting w83781d_detach_client remove the three clients (1 * primary
+ 2 * subclients) independently. It's a noisy patch because I had to change the way the subclients were kmalloc'ed - sorry. The meat is around line 1422. This patch works for me... comments?
drivers/i2c/chips/w83781d.c | 76 +++++++++++++++++++++++--------------------- 1 files changed, 40 insertions(+), 36 deletions(-)
drivers/i2c/chips/w83781d.c +40 -36
@@ -299,8 +299,8 @@char valid;/* !=0 if following fields are valid */ unsigned long last_updated;/* In jiffies */ -struct i2c_client *lm75;/* for secondary I2C addresses */ -/* pointer to array of 2 subclients */ +struct i2c_client *lm75[2 ];/* for secondary I2C addresses */ +/* array of 2 pointers to subclients */ u8 in[9 ];/* Register value - 8 & 9 for 782D only */ u8 in_max[9 ];/* Register value - 8 & 9 for 782D only */
@@ -1043,12 +1043,12 @@const char *client_name;struct w83781d_data *data =i2c_get_clientdata (new_client); -if (!(data->lm75 =kmalloc (2 *sizeof (struct i2c_client ), -GFP_KERNEL ))) { + data->lm75[0 ] =kmalloc (sizeof (struct i2c_client ),GFP_KERNEL ); +if (!(data->lm75[0 ])) { err = -ENOMEM ; goto ERROR_SC_0; } -memset (data->lm75,0x00 ,2 *sizeof (struct i2c_client )); +memset (data->lm75[0 ],0x00 ,sizeof (struct i2c_client )); id =i2c_adapter_id (adapter);
@@ -1066,25 +1066,33 @@w83781d_write_value (new_client,W83781D_REG_I2C_SUBADDR , (force_subclients[2 ] &0x07 ) | ((force_subclients[3 ] &0x07 ) <<4 )); - data->lm75[0 ].addr = force_subclients[2 ]; + data->lm75[0 ]->addr = force_subclients[2 ]; }else { val1 =w83781d_read_value (new_client,W83781D_REG_I2C_SUBADDR ); - data->lm75[0 ].addr =0x48 + (val1 &0x07 ); + data->lm75[0 ]->addr =0x48 + (val1 &0x07 ); }if (kind != w83783s) { + + data->lm75[1 ] =kmalloc (sizeof (struct i2c_client ),GFP_KERNEL ); +if (!(data->lm75[1 ])) { + err = -ENOMEM ; + goto ERROR_SC_1; + } +memset (data->lm75[1 ],0x0 ,sizeof (struct i2c_client )); +if (force_subclients[0 ] == id && force_subclients[1 ] == address) { - data->lm75[1 ].addr = force_subclients[3 ]; + data->lm75[1 ]->addr = force_subclients[3 ]; }else { - data->lm75[1 ].addr =0x48 + ((val1 >>4 ) &0x07 ); + data->lm75[1 ]->addr =0x48 + ((val1 >>4 ) &0x07 ); } -if (data->lm75[0 ].addr == data->lm75[1 ].addr) { +if (data->lm75[0 ]->addr == data->lm75[1 ]->addr) {dev_err (&new_client->dev,"Duplicate addresses 0x%x for subclients.\n" , - data->lm75[0 ].addr); + data->lm75[0 ]->addr); err = -EBUSY ; - goto ERROR_SC_1; + goto ERROR_SC_2; } }
@@ -1103,19 +1111,19 @@for (i =0 ; i <=1 ; i++) {/* store all data in w83781d */ -i2c_set_clientdata (&data->lm75[i],NULL ); - data->lm75[i].adapter = adapter; - data->lm75[i].driver = &w83781d_driver; - data->lm75[i].flags =0 ; -strlcpy (data->lm75[i].dev.name, client_name, +i2c_set_clientdata (data->lm75[i],NULL ); + data->lm75[i]->adapter = adapter; + data->lm75[i]->driver = &w83781d_driver; + data->lm75[i]->flags =0 ; +strlcpy (data->lm75[i]->dev.name, client_name,DEVICE_NAME_SIZE ); -if ((err =i2c_attach_client (&(data->lm75[i])))) { +if ((err =i2c_attach_client (data->lm75[i]))) {dev_err (&new_client->dev,"Subclient %d " "registration at address 0x%x " -"failed.\n" , i, data->lm75[i].addr); +"failed.\n" , i, data->lm75[i]->addr);if (i ==1 ) - goto ERROR_SC_2; - goto ERROR_SC_1; + goto ERROR_SC_3; + goto ERROR_SC_2; }if (kind == w83783s)break ;
@@ -1124,10 +1132,14 @@return 0 ;/* Undo inits in case of errors */ +ERROR_SC_3: +i2c_detach_client (data->lm75[0 ]); ERROR_SC_2: -i2c_detach_client (&(data->lm75[0 ])); +if (NULL != data->lm75[1 ]) +kfree (data->lm75[1 ]); ERROR_SC_1: -kfree (data->lm75); +if (NULL != data->lm75[0 ]) +kfree (data->lm75[0 ]); ERROR_SC_0:return err; }
@@ -1326,7 +1338,8 @@ kind, new_client))) goto ERROR3; }else { - data->lm75 =NULL ; + data->lm75[0 ] =NULL ; + data->lm75[1 ] =NULL ; }device_create_file_in (new_client,0 );
@@ -1409,20 +1422,11 @@static int w83781d_detach_client (struct i2c_client *client) { -struct w83781d_data *data =i2c_get_clientdata (client);int err; -/* release ISA region or I2C subclients first */ -if (i2c_is_isa_client (client)) { +if (i2c_is_isa_client (client))release_region (client->addr,W83781D_EXTENT ); - }else { -i2c_detach_client (&data->lm75[0 ]); -if (data->type != w83783s) -i2c_detach_client (&data->lm75[1 ]); -kfree (data->lm75); - } -/* now it's safe to scrap the rest */ if ((err =i2c_detach_client (client))) {dev_err (&client->dev,"Client deregistration failed, client not detached.\n" );
@@ -1484,7 +1488,7 @@ res =i2c_smbus_read_byte_data (client, reg &0xff ); }else {/* switch to subclient */ - cl = &data->lm75[bank -1 ]; + cl = data->lm75[bank -1 ];/* convert from ISA to LM75 I2C addresses */ switch (reg &0xff ) {case 0x50 :/* TEMP */
@@ -1555,7 +1559,7 @@ value & 0xff); }else {/* switch to subclient */ - cl = &data->lm75[bank -1 ]; + cl = data->lm75[bank -1 ];/* convert from ISA to LM75 I2C addresses */ switch (reg &0xff ) {case 0x52 :/* CONFIG */
ChangeSet 1.1318.3.4, 2003/06/16 11:32:06-07:00, [email protected]
[PATCH] I2C: ICH5 SMBus and W83627THF additions
I have been trying to get the W83627THF chip working on this board. It is an Asus P4C800 with Intel 875p chipset and a W83627THF connected via the SMBus.
There are no data sheet for the W83627THF as far as I can see, but supposidly it is a W83627HF with advance Fan control, etc.
I have applied attached patches, and tried various other things to get it to work, but no avail. The SMBus on the ICH5 seems to work, and it seems to detect the sensor chip just fine, but it do not seem to read any values from the W83627THF.
drivers/i2c/busses/i2c-i801.c | 10 +++++++++- drivers/i2c/chips/w83781d.c | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-i801.c +9 -1
@@ -27,6 +27,7 @@ 82801BA 2443 82801CA/CAM 2483 82801DB 24C3 (HW PEC supported, 32 byte buffer not supported) + 82801EB 24D3 (HW PEC supported, 32 byte buffer not supported) This driver supports several versions of Intel's I/O Controller Hubs (ICH). For SMBus support, they are similar to the PIIX4 and are part
@@ -121,7 +122,8 @@return -ENODEV ; I801_dev = dev; -if (dev->device ==PCI_DEVICE_ID_INTEL_82801DB_3 ) +if ((dev->device ==PCI_DEVICE_ID_INTEL_82801DB_3 ) || + (dev->device ==PCI_DEVICE_ID_INTEL_82801EB_3 )) isich4 =1 ;else isich4 =0 ;
@@ -584,6 +586,12 @@ .device = PCI_DEVICE_ID_INTEL_82801DB_3, .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, + }, + { + .vendor = PCI_VENDOR_ID_INTEL, + .device = PCI_DEVICE_ID_INTEL_82801EB_3, + .subvendor = PCI_ANY_ID, + .subdevice = PCI_ANY_ID, }, { 0, } };
drivers/i2c/chips/w83781d.c +1 -1
@@ -1285,7 +1285,7 @@ kind = w83782d; else if (val1 == 0x40 && vendid == winbond && !is_isa) kind = w83783s; - else if (val1 == 0x20 && vendid == winbond) + else if ((val1 == 0x20 || val1 == 0x72) && vendid == winbond) kind = w83627hf; else if (val1 == 0x30 && vendid == asus && !is_isa) kind = as99127f;
ChangeSet 1.1318.3.5, 2003/06/16 11:32:48-07:00, [email protected]
[PATCH] I2C: add lm78 chip to Makefile
drivers/i2c/chips/Makefile | 1 + 1 files changed, 1 insertion(+)
drivers/i2c/chips/Makefile +1 -0
@@ -5,6 +5,7 @@ obj-$(CONFIG_SENSORS_ADM1021) += adm1021.o obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_LM75) += lm75.o +obj-$(CONFIG_SENSORS_LM78) += lm78.o obj-$(CONFIG_SENSORS_LM85) += lm85.o obj-$(CONFIG_SENSORS_VIA686A) += via686a.o obj-$(CONFIG_SENSORS_W83781D) += w83781d.o
ChangeSet 1.1318.3.6, 2003/06/16 11:40:37-07:00, [email protected]
[PATCH] I2C: Sensors patch for adm1021
Patch for adm1021
This corrects temp reporting and a major error whereby "alarms" and "die_code" were being put though the "TEMP" macro. Compiled but don't have the hardware to test.
drivers/i2c/chips/adm1021.c | 18 ++++++++++++++---- 1 files changed, 14 insertions(+), 4 deletions(-)
drivers/i2c/chips/adm1021.c +14 -4
@@ -88,8 +88,8 @@these macros are called: arguments may be evaluated more than once. Fixing this is just not worth it. */ /* Conversions note: 1021 uses normal integer signed-byte format*/ -#define TEMP_FROM_REG (val) (val > 127 ? val-256 : val) -#define TEMP_TO_REG (val) (SENSORS_LIMIT((val < 0 ? val+256 : val),0,255)) +#define TEMP_FROM_REG (val) (val > 127 ? (val-256)*1000 : val*1000) +#define TEMP_TO_REG (val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))/* Initial values */
@@ -172,8 +172,18 @@show (remote_temp_max);show (remote_temp_hyst);show (remote_temp_input); -show (alarms); -show (die_code); + +#define show2 (value) \ +static ssize_t show_##value(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct adm1021_data *data = i2c_get_clientdata(client); \ + \ + adm1021_update_client(client); \ + return sprintf(buf, "%d\n", data->value); \ +} +show2 (alarms); +show2 (die_code);#define set (value, reg) \ static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
ChangeSet 1.1318.3.7, 2003/06/17 15:34:14-07:00, [email protected]
[PATCH] I2C: fix for previous W83627THF sensor chip patch
Ok, I was wrong in assuming that the W83627THF was on the I2C bus. It is on the ISA bus, id 0x90 (thanks to Alex Van Kaam author of MBM who corrected my assumption).
drivers/i2c/chips/w83781d.c | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/chips/w83781d.c +6 -2
@@ -28,6 +28,7 @@ asb100 "bach" (type_name = as99127f) 0x30 0x0694 yes no w83781d 7 3 0 3 0x10 0x5ca3 yes yes w83627hf 9 3 2 3 0x20 0x5ca3 yes yes(LPC) + w83627thf 9 3 2 3 0x90 0x5ca3 no yes(LPC) w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
@@ -1285,7 +1286,7 @@ kind = w83782d; else if (val1 == 0x40 && vendid == winbond && !is_isa) kind = w83783s; - else if ((val1 == 0x20 || val1 == 0x72) && vendid == winbond) + else if ((val1 == 0x20 || val1 == 0x90) && vendid == winbond) kind = w83627hf; else if (val1 == 0x30 && vendid == asus && !is_isa) kind = as99127f;
@@ -1309,7 +1310,10 @@ }else if (kind == w83783s) { client_name ="W83783S chip" ; }else if (kind == w83627hf) { - client_name ="W83627HF chip" ; +if (val1 ==0x90 ) + client_name ="W83627THF chip" ; +else + client_name ="W83627HF chip" ; }else if (kind == as99127f) { client_name ="AS99127F chip" ; }else if (kind == w83697hf) {
ChangeSet 1.1318.3.8, 2003/06/17 15:37:40-07:00, [email protected]
I2C: fix resource leak in i2c-ali15x3.c
drivers/i2c/busses/i2c-ali15x3.c | 18 +++++++++++------- 1 files changed, 11 insertions(+), 7 deletions(-)
drivers/i2c/busses/i2c-ali15x3.c +11 -7
@@ -177,17 +177,18 @@if (force_addr) {dev_info (&ALI15X3_dev->dev,"forcing ISA address 0x%04X\n" , ali15x3_smba); -if (PCIBIOS_SUCCESSFUL != -pci_write_config_word (ALI15X3_dev,SMBBA , ali15x3_smba)) -return -ENODEV ; -if (PCIBIOS_SUCCESSFUL != -pci_read_config_word (ALI15X3_dev,SMBBA , &a)) -return -ENODEV ; +if (PCIBIOS_SUCCESSFUL !=pci_write_config_word (ALI15X3_dev, +SMBBA , + ali15x3_smba)) + goto error; +if (PCIBIOS_SUCCESSFUL !=pci_read_config_word (ALI15X3_dev, +SMBBA , &a)) + goto error;if ((a & ~(ALI15X3_SMB_IOSIZE -1 )) != ali15x3_smba) {/* make sure it works */ dev_err (&ALI15X3_dev->dev,"force address failed - not supported?\n" ); -return -ENODEV ; + goto error; } }/* check if whole device is enabled */
@@ -219,6 +220,9 @@dev_dbg (&ALI15X3_dev->dev,"iALI15X3_smba = 0x%X\n" , ali15x3_smba);return 0 ; +error: +release_region (ali15x3_smba,ALI15X3_SMB_IOSIZE ); +return -ENODEV ; }/* Internally used pause function */
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 6 lines · Greg KH
ChangeSet 1.1318.3.2, 2003/06/16 11:31:43-07:00, [email protected]
[PATCH] I2C: lm85 fixups
OK Here's the patch which :
1) Fixes the race conditions
<cut>
Quoted 4 lines
(quoted diff) +0
-0@@ -437,10 +434,13 @@ { struct i2c_client *client = to_i2c_client(dev); struct lm85_data *data = i2c_get_clientdata(client);
Review line 445
Quoted 10 lines
(quoted diff) +4
-1+ int val; - int val = simple_strtol(buf, NULL, 10); + down(&data->update_lock); + val = simple_strtol(buf, NULL, 10); data->fan_min[nr] = FAN_TO_REG(val); lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); + up(&data->update_lock); return count; }
Ugh. Looks like this sort of fix is needed in every single chip driver in 2.5. And the CVS chip drivers have the same problem.
So... are we going to tackle this for the sensors 2.8.0 release?
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 16 lines · Mark M. Hoffman
Quoted 6 lines · Greg KH
ChangeSet 1.1318.3.2, 2003/06/16 11:31:43-07:00, [email protected]
[PATCH] I2C: lm85 fixups
OK Here's the patch which :
1) Fixes the race conditions<cut>
Quoted 6 lines
(quoted diff) +1
-0@@ -437,10 +434,13 @@ { struct i2c_client *client = to_i2c_client(dev); struct lm85_data *data = i2c_get_clientdata(client); + int val;
Quoted 11 lines
Review line 445
Quoted 8 lines
(quoted diff) +3
-1- int val = simple_strtol(buf, NULL, 10); + down(&data->update_lock); + val = simple_strtol(buf, NULL, 10); data->fan_min[nr] = FAN_TO_REG(val); lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); + up(&data->update_lock); return count; }Ugh. Looks like this sort of fix is needed in every single chip driver in 2.5. And the CVS chip drivers have the same problem.
Hm, wait, what are we trying to protect with this lock? The data-> values? Or access to the hardware? Or something else?
thanks,
greg k-h
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 1 line · Philip Pokorny
The
data->fooand device register need to be in sync. So I suppose both...
The device registers are protected by the I2C bus locking or ISA bus locking in xxxx_write_value. The race (in the original) that I see looked like this:
process a in show_bar() process b in set_foo()
data->foo = baz;
/* changes data->foo */xxxx_update_client();
/* wrong, doesn't = baz anymore */ xxxx_write_value(..., data->foo);
But now that I look again, why not this?
{
struct i2c_client *client = to_i2c_client(dev);
u8 reg ;
int val = simple_strtol(buf, NULL, 10);
reg = FAN_TO_REG(val);
lm85_write_value(client, LM85_REG_FAN_MIN(nr), reg);
return count;
}Now put the data->fan_min[nr] = value; part into xxxx_write_value() (along with the proper locking) and be done with it; less code this way and harder to accidentally break. Actually, the more I look at it, the more I like this idea (he writes after deleting 50 lines of his first attempt.) Comments?
Quoted 47 lines
At first I thought that if I always updated the chip first and then the
data->foocached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling
update_client) and the result being that thedata->foocached value was different from actual device register.:v)
Quoted 35 lines · Greg KH
Quoted 17 lines · Mark M. Hoffman
Quoted 6 lines · Greg KH
ChangeSet 1.1318.3.2, 2003/06/16 11:31:43-07:00, [email protected]
[PATCH] I2C: lm85 fixups
OK Here's the patch which :
1) Fixes the race conditions<cut>
Quoted 6 lines
(quoted diff) +1
-0@@ -437,10 +434,13 @@ { struct i2c_client *client = to_i2c_client(dev); struct lm85_data *data = i2c_get_clientdata(client); + int val;Quoted 11 lines
Review line 445
Quoted 8 lines
(quoted diff) +3
-1- int val = simple_strtol(buf, NULL, 10); + down(&data->update_lock); + val = simple_strtol(buf, NULL, 10); data->fan_min[nr] = FAN_TO_REG(val); lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); + up(&data->update_lock); return count; }Ugh. Looks like this sort of fix is needed in every single chip driver in 2.5. And the CVS chip drivers have the same problem.
Hm, wait, what are we trying to protect with this lock? The data-> values? Or access to the hardware? Or something else?
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHThe data->foo and device register need to be in sync. So I suppose both...
At first I thought that if I always updated the chip first and then the data->foo cached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...
The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling update_client) and the result being that the data->foo cached value was different from actual device register.
:v)
Quoted 39 lines · Greg KH
Quoted 17 lines · Mark M. Hoffman
Quoted 6 lines · Greg KH
ChangeSet 1.1318.3.2, 2003/06/16 11:31:43-07:00, [email protected]
[PATCH] I2C: lm85 fixups
OK Here's the patch which :
1) Fixes the race conditions<cut>
Quoted 6 lines
(quoted diff) +1
-0@@ -437,10 +434,13 @@ { struct i2c_client *client = to_i2c_client(dev); struct lm85_data *data = i2c_get_clientdata(client); + int val;Quoted 11 lines
Review line 445
Quoted 8 lines
(quoted diff) +3
-1- int val = simple_strtol(buf, NULL, 10); + down(&data->update_lock); + val = simple_strtol(buf, NULL, 10); data->fan_min[nr] = FAN_TO_REG(val); lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); + up(&data->update_lock); return count; }Ugh. Looks like this sort of fix is needed in every single chip driver in 2.5. And the CVS chip drivers have the same problem.
Hm, wait, what are we trying to protect with this lock? The data-> values? Or access to the hardware? Or something else?
thanks,
greg k-h
Signature · 4 lines
Philip Pokorny, Director of Engineering Tel: 415-358-2635 Fax: 415-358-2646 Toll Free: 888-PENGUIN PENGUIN COMPUTING, INC. www.penguincomputing.com
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 10 lines · Philip Pokorny
Quoted 2 lines
Now put the
data->fan_min[nr] = value;part intoxxxx_write_value()(along with the proper locking) and be done with it; less code thisAre you saying that the assignment 'data->fan_min[nr] = value' would happen inside the
xxxx_write_valuefunction?If so, how does
xxxx_write_valueknow which data member to store into? Are you going to pass a pointer to data->fan_min[nr] as an argument towrite_value? What about type and size issues. Some values are u8, others are s8, and u16.
Ack. I was just about to modify lm78.c as an example when I realized the same thing just as your message came in.
Quoted 6 lines
The other issue I see is that of partially split values. Perhaps I'm the only one that's done this, but some registers contain bits for several different sensors. (fan divisors for example...) I have been breaking those appart and putting them back together as necessary for the job at hand. In fact
fan_divisorsare a excellent example since they *have* to be split apart to make the fan RPM calculations easy.
Yeah, this is common to many chip drivers.
Quoted 3 lines
What if we hoist the down/up out of
update_clientinstead? Is there someplace higher up that we could serialize access to all the show/set functions? Is that a worse idea?
I don't know (to all three questions, more or less). I'm going to step back a bit and look at what we're trying to accomplish with this structure cache of the register values in the first place.
Right, the way these chips typically work... they stop sampling while they service read or write requests. We want to allow updates no more often than a defined frequency. So is there a better way to accomplish that?
Quoted 54 lines
:v)
Quoted 51 lines · Mark M. Hoffman
* Philip Pokorny <[email protected]> [2003-06-19 18:45:36 -0700]:
Quoted 2 lines
The
data->fooand device register need to be in sync. So I suppose both...The device registers are protected by the I2C bus locking or ISA bus locking in
xxxx_write_value. The race (in the original) that I see looked like this:process a in
show_bar()process b inset_foo()data->foo = baz;/* changes
data->foo*/xxxx_update_client();/* wrong, doesn't = baz anymore */ xxxx_write_value(..., data->foo);But now that I look again, why not this?
{ struct i2c_client *client = to_i2c_client(dev); u8 reg ; int val = simple_strtol(buf, NULL, 10); reg = FAN_TO_REG(val); lm85_write_value(client, LM85_REG_FAN_MIN(nr), reg); return count; }Now put the
data->fan_min[nr] = value;part intoxxxx_write_value()(along with the proper locking) and be done with it; less code this way and harder to accidentally break. Actually, the more I look at it, the more I like this idea (he writes after deleting 50 lines of his first attempt.) Comments?Quoted 11 lines
At first I thought that if I always updated the chip first and then the
data->foocached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling
update_client) and the result being that thedata->foocached value was different from actual device register.:v)
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 28 lines · Mark M. Hoffman
Quoted 10 lines · Philip Pokorny
Quoted 2 lines
Now put the
data->fan_min[nr] = value;part intoxxxx_write_value()(along with the proper locking) and be done with it; less code thisAre you saying that the assignment 'data->fan_min[nr] = value' would happen inside the
xxxx_write_valuefunction?If so, how does
xxxx_write_valueknow which data member to store into? Are you going to pass a pointer to data->fan_min[nr] as an argument towrite_value? What about type and size issues. Some values are u8, others are s8, and u16.Ack. I was just about to modify
lm78.cas an example when I realized the same thing just as your message came in.Quoted 6 lines
The other issue I see is that of partially split values. Perhaps I'm the only one that's done this, but some registers contain bits for several different sensors. (fan divisors for example...) I have been breaking those appart and putting them back together as necessary for the job at hand. In fact
fan_divisorsare a excellent example since they *have* to be split apart to make the fan RPM calculations easy.Yeah, this is common to many chip drivers.
Quoted 3 lines
What if we hoist the down/up out of
update_clientinstead? Is there someplace higher up that we could serialize access to all the show/set functions? Is that a worse idea?
Quoted 7 lines
I don't know (to all three questions, more or less). I'm going to step back a bit and look at what we're trying to accomplish with this structure cache of the register values in the first place.
Right, the way these chips typically work... they stop sampling while they service read or write requests. We want to allow updates no more often than a defined frequency. So is there a better way to accomplish that?
Sorry to reply to myself - attempt #3:
Remove this line from all set_foo():
data->foo = FOO_TO_REG(...);
And add this to xxxx_write_value():
data->valid = 0;
Hmmm, probably still need data->update_lock in xxxx_write_value(), but that should take care of it no?
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 11 lines · Mark M. Hoffman
Remove this line from all
set_foo():data->foo = FOO_TO_REG(...);And add this to
xxxx_write_value():data->valid = 0;Hmmm, probably still need
data->update_lockinxxxx_write_value(), but that should take care of it no?
*Sigh* no. There would still be races around the set_* functions that read the fan divisor. OK Phil, I'm done trying to improve on your patch. ;)
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 10 lines · Philip Pokorny
The
data->fooand device register need to be in sync. So I suppose both...At first I thought that if I always updated the chip first and then the
data->foocached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling
update_client) and the result being that thedata->foocached value was different from actual device register.
But if that happens, then the worse thing that could happen is we would show "stale" data in the resulting value.
Hm, so I can understand if we need to protect access to the hardware when we read two values in a row, like this:
res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;or the same thing when writing.
But what about this simpler patch, that pushes the lock down to the hardware reads and writes for multiple registers. And it doesn't try to mirror the write in the "data" structure, as it's always read with the latest values for every read.
Does it look ok? It has the added benefit of making the .c and .o files smaller :)
thanks,
greg k-h
# I2C: clean up lm85's locking code.
drivers/i2c/chips/lm85.c +22 -34
@@ -433,14 +433,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->fan_min[nr] =FAN_TO_REG (val); -lm85_write_value (client,LM85_REG_FAN_MIN (nr), data->fan_min[nr]); -up (&data->update_lock); + val =FAN_TO_REG (val); +lm85_write_value (client,LM85_REG_FAN_MIN (nr), val);return count; }
@@ -527,14 +524,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->pwm[nr] =PWM_TO_REG (val); -lm85_write_value (client,LM85_REG_PWM (nr), data->pwm[nr]); -up (&data->update_lock); + val =PWM_TO_REG (val); +lm85_write_value (client,LM85_REG_PWM (nr), val);return count; }static ssize_t show_pwm_enable (struct device *dev,char *buf,int nr)
@@ -592,14 +586,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->in_min[nr] =INS_TO_REG (nr, val); -lm85_write_value (client,LM85_REG_IN_MIN (nr), data->in_min[nr]); -up (&data->update_lock); + val =INS_TO_REG (nr, val); +lm85_write_value (client,LM85_REG_IN_MIN (nr), val);return count; }static ssize_t show_in_max (struct device *dev,char *buf,int nr)
@@ -614,14 +605,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->in_max[nr] =INS_TO_REG (nr, val); -lm85_write_value (client,LM85_REG_IN_MAX (nr), data->in_max[nr]); -up (&data->update_lock); + val =INS_TO_REG (nr, val); +lm85_write_value (client,LM85_REG_IN_MAX (nr), val);return count; }#define show_in_reg (offset) \
@@ -681,14 +669,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->temp_min[nr] =TEMP_TO_REG (val); -lm85_write_value (client,LM85_REG_TEMP_MIN (nr), data->temp_min[nr]); -up (&data->update_lock); + val =TEMP_TO_REG (val); +lm85_write_value (client,LM85_REG_TEMP_MIN (nr), val);return count; }static ssize_t show_temp_max (struct device *dev,char *buf,int nr)
@@ -703,14 +688,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->temp_max[nr] =TEMP_TO_REG (val); -lm85_write_value (client,LM85_REG_TEMP_MAX (nr), data->temp_max[nr]); -up (&data->update_lock); + val =TEMP_TO_REG (val); +lm85_write_value (client,LM85_REG_TEMP_MAX (nr), val);return count; }#define show_temp_reg (offset) \
@@ -955,8 +937,12 @@int lm85_read_value (struct i2c_client *client,u8 reg) { +struct lm85_data *data =i2c_get_clientdata (client);int res; +/* serialize access to the hardware */ +down (&data->update_lock); +/* What size location is it? */ switch ( reg ) {case LM85_REG_FAN (0 ) :/* Read WORD data */
@@ -980,14 +966,19 @@ res =i2c_smbus_read_byte_data (client, reg);break ; } +up (&data->update_lock);return res ; }int lm85_write_value (struct i2c_client *client,u8 reg,int value) { +struct lm85_data *data =i2c_get_clientdata (client);int res ; +/* serialize access to the hardware */ +down (&data->update_lock); +switch ( reg ) {case LM85_REG_FAN (0 ) :/* Write WORD data */ case LM85_REG_FAN (1 ) :
@@ -1009,6 +1000,7 @@ res =i2c_smbus_write_byte_data (client, reg, value);break ; } +up (&data->update_lock);return res ; }
@@ -1070,8 +1062,6 @@struct lm85_data *data =i2c_get_clientdata (client);int i; -down (&data->update_lock); -if ( !data->valid || (jiffies - data->last_reading >LM85_DATA_INTERVAL ) ) {/* Things that change quickly */
@@ -1209,8 +1199,6 @@ };/* last_config */ data->valid =1 ; - -up (&data->update_lock); }
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 7 lines · Greg KH
Quoted 6 lines · Philip Pokorny
The
data->fooand device register need to be in sync. So I suppose both...At first I thought that if I always updated the chip first and then the
data->foocached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...
Quoted 7 lines
Quoted 4 lines
The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling
update_client) and the result being that thedata->foocached value was different from actual device register.But if that happens, then the worse thing that could happen is we would show "stale" data in the resulting value.
Nope - as it was originally written (before Philip's most recent patch), we could lose the value we meant to write.
Quoted 6 lines
Hm, so I can understand if we need to protect access to the hardware when we read two values in a row, like this:
res = i2c_smbus_read_byte_data(client, reg) & 0xff ; res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;or the same thing when writing.
Good point. The I2C core provides I2C/SMBus locking to prevent trashing the bus itself, but that doesn't help the above. Which brings about another question... why wasn't i2c_smbus_read_word_data() used in that case? Philip?
Quoted 4 lines
But what about this simpler patch, that pushes the lock down to the hardware reads and writes for multiple registers. And it doesn't try to mirror the write in the "data" structure, as it's always read with the latest values for every read.
It looks like this scheme would actually work for lm85.c But, for most chip drivers with fan divisors there would still be a race. E.g. look at set_fan_min() and set_fan_div() in lm78.c; the meaning of data->fan_min depends on data->fan_div so those two must be coherent. That locking can't be pushed further down.
That lm85 doesn't use fan divisors is the exception rather than the rule. I mean, I don't hate this patch... but it doesn't tell us anything about how to fix the rest of them. I still think Philip's original brute force locking ;) is necessary there.
Quoted 15 lines
Does it look ok? It has the added benefit of making the .c and .o files smaller :)
int
lm85_write_value(struct i2c_client *client, u8 reg, int value){(quoted diff) +4
-0+ struct lm85_data *data = i2c_get_clientdata(client); int res ; + /* serialize access to the hardware */ + down(&data->update_lock); + switch( reg ) { case LM85_REG_FAN(0) : /* Write WORD data */ case LM85_REG_FAN(1) :
Review line 1006
Quoted 7 lines
(quoted diff) +1
-0@@ -1009,6 +1000,7 @@ res = i2c_smbus_write_byte_data(client, reg, value); break ; } + up(&data->update_lock); return res ; }
I would add "data->valid = 0;" somewhere before the up(). Then you're guaranteed never to read stale data after a write.
Quoted 10 lines
(quoted diff) +0
-2@@ -1070,8 +1062,6 @@ struct lm85_data *data = i2c_get_clientdata(client); int i; - down(&data->update_lock); - if ( !data->valid || (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) { /* Things that change quickly */
Review line 1202
Quoted 6 lines
(quoted diff) +0
-2@@ -1209,8 +1199,6 @@ }; /* last_config */ data->valid = 1; - - up(&data->update_lock); }
We only want to service one update_client() call per some interval. I think this lock needs to stay.
And one last off-topic nit: data->fan_ppr is set but not used?
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KH* Mark M. Hoffman <[email protected]> [2003-06-23 23:32:39 -0400]:
(replying to himself again...)
Quoted 13 lines
Quoted 13 lines
(quoted diff) +0
-2@@ -1070,8 +1062,6 @@ struct lm85_data *data = i2c_get_clientdata(client); int i; - down(&data->update_lock); - if ( !data->valid || (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) { /* Things that change quickly */@@ -1209,8 +1199,6 @@ }; /* last_config */
Quoted 7 lines
Review line 1202
Quoted 4 lines
(quoted diff) +0
-2data->valid = 1; - - up(&data->update_lock); }We only want to service one
update_client()call per some interval. I think this lock needs to stay.
If this one stays then the one you added in lm85_read_value() must go. Anyway, the latter causes the update_lock to be used before it's initialized - lines 779-780 in lm85_detect().
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHQuoted 20 lines · Philip Pokorny
Quoted 7 lines · Mark M. Hoffman
Quoted 5 lines · Greg KH
Hm, so I can understand if we need to protect access to the hardware when we read two values in a row, like this:
res = i2c_smbus_read_byte_data(client, reg) & 0xff ; res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;Quoted 7 lines
Quoted 1 line
or the same thing when writing.
Good point. The I2C core provides I2C/SMBus locking to prevent trashing the bus itself, but that doesn't help the above. Which brings about another question... why wasn't
i2c_smbus_read_word_data()used in that case? Philip?I actually tried that, but it didn't work. My original code had a #define that would let you try and re-enable it, but I ditched it when I didn't see any other drivers using
read_wordvariants.
Quoted 5 lines
I should have spoken up that
read_wordseemed to be broken at the time.But as I write this, I think I read in the chip data sheet that it doesn't support SMBus word reads... (Go figure...) That was probably the real reason I pulled the
read_wordcode...
It's just as well... maybe some bus drivers don't support it either. It doesn't hurt to use lowest common denominator SMBus routines if possible.
Quoted 5 lines
Quoted 1 line
And one last off-topic nit:
data->fan_ppris set but not used?The
fan_pprfeatures is only on the ADM1027 variant. Perhaps Margit didn't migrate that code? Otherwise, there are calls toPPR_FROM_REG()in the cvs code...
Maybe it was left out because there's no mention of what to do with it in sysfs? I have no suggestion for that; I don't know what PPR is.
Also btw, data->lock is never used. There could be others.
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHI don't think so...
In addition to the two-byte values, you've also got related values like the extended precision bits for the A/D on the ADM1027. If two processes both trigger an update_client() at the same time, bad things would happen. I think the potential problem is much worse than just "stale" data, but even "stale" data is bad.
Also, the reads are not always coming from the registers. (Or did I miss a piece of your patch that changed update_client() and LM85_DATA_TIMEOUT) update_client() only reads the registers after the jiffies "timer" expires.
For the LM85, the _config_ data (like min/max values, etc.) is re-read *much* less often than the actual readings. I expected that the cache would be kept up to date on writes so that the re-read would be largely unnecessary and only a backup. With the auto-fan control, there are significant amounts of config data that would slow down access to the readings we really care about.
Come to think of it, we can't really be sure that the cached values won't be changed by another CPU running update_client while we're accessing the cache. That would suggest that the down/up pair needs to be pulled up out of update_client into the individual sysctl or set/show functions.
Even if your version is smaller, I think it's likely to be slower or broken in some cases...
:v)
Quoted 135 lines · Greg KH
Quoted 10 lines · Philip Pokorny
The
data->fooand device register need to be in sync. So I suppose both...At first I thought that if I always updated the chip first and then the
data->foocached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling
update_client) and the result being that thedata->foocached value was different from actual device register.But if that happens, then the worse thing that could happen is we would show "stale" data in the resulting value.
Hm, so I can understand if we need to protect access to the hardware when we read two values in a row, like this:
res = i2c_smbus_read_byte_data(client, reg) & 0xff ; res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;or the same thing when writing.
But what about this simpler patch, that pushes the lock down to the hardware reads and writes for multiple registers. And it doesn't try to mirror the write in the "data" structure, as it's always read with the latest values for every read.
Does it look ok? It has the added benefit of making the .c and .o files smaller :)
thanks,
greg k-h
# I2C: clean up lm85's locking code.
diff -Nru
a/drivers/i2c/chips/lm85.cb/drivers/i2c/chips/lm85.cdrivers/i2c/chips/lm85.c +14
-14@@ -433,14 +433,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->fan_min[nr] =FAN_TO_REG (val); -lm85_write_value (client,LM85_REG_FAN_MIN (nr), data->fan_min[nr]); -up (&data->update_lock); + val =FAN_TO_REG (val); +lm85_write_value (client,LM85_REG_FAN_MIN (nr), val);return count; }@@ -527,14 +524,11 @@ size_t count, int nr) {struct i2c_client *client =to_i2c_client (dev); -struct lm85_data *data =i2c_get_clientdata (client);int val; -down (&data->update_lock); val =simple_strtol (buf,NULL ,10 ); - data->pwm[nr] =PWM_TO_REG (val); -lm85_write_value (client,LM85_REG_PWM (nr), data->pwm[nr]); -up (&data->update_lock); + val =PWM_TO_REG (val); +lm85_write_value (client,LM85_REG_PWM (nr), val);return count; } [ snip ] \@@ -955,8 +937,12 @@int lm85_read_value (struct i2c_client *client,u8 reg) { +struct lm85_data *data =i2c_get_clientdata (client);int res; +/* serialize access to the hardware */ +down (&data->update_lock); +/* What size location is it? */ switch ( reg ) {case LM85_REG_FAN (0 ) :/* Read WORD data */ @@ -980,14 +966,19 @@ res =i2c_smbus_read_byte_data (client, reg);break ; } +up (&data->update_lock);return res ; }int lm85_write_value (struct i2c_client *client,u8 reg,int value) { +struct lm85_data *data =i2c_get_clientdata (client);int res ; +/* serialize access to the hardware */ +down (&data->update_lock); +switch ( reg ) {case LM85_REG_FAN (0 ) :/* Write WORD data */ case LM85_REG_FAN (1 ) :@@ -1009,6 +1000,7 @@ res =i2c_smbus_write_byte_data (client, reg, value);break ; } +up (&data->update_lock);return res ; }@@ -1070,8 +1062,6 @@struct lm85_data *data =i2c_get_clientdata (client);int i; -down (&data->update_lock); -if ( !data->valid || (jiffies - data->last_reading >LM85_DATA_INTERVAL ) ) {/* Things that change quickly */ @@ -1209,8 +1199,6 @@ };/* last_config */ data->valid =1 ; - -up (&data->update_lock); }
Signature · 4 lines
Philip Pokorny, Director of Engineering Tel: 415-358-2635 Fax: 415-358-2646 Toll Free: 888-PENGUIN PENGUIN COMPUTING, INC. www.penguincomputing.com
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHA few comments below...
:v)
Quoted 28 lines · Mark M. Hoffman
Quoted 8 lines · Greg KH
Quoted 6 lines · Philip Pokorny
The
data->fooand device register need to be in sync. So I suppose both...At first I thought that if I always updated the chip first and then the
data->foocached value, I'd be OK, but it seemed like there were too many exceptions where values were broken apart, or recombined...Quoted 7 lines
Quoted 4 lines
The primary thing I was trying to avoid was one CPU writing a value while another CPU was reading a value (and therefore calling
update_client) and the result being that thedata->foocached value was different from actual device register.But if that happens, then the worse thing that could happen is we would show "stale" data in the resulting value.
Nope - as it was originally written (before Philip's most recent patch), we could lose the value we meant to write.
Quoted 5 lines
Hm, so I can understand if we need to protect access to the hardware when we read two values in a row, like this:
res = i2c_smbus_read_byte_data(client, reg) & 0xff ; res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
Quoted 7 lines
Quoted 1 line
or the same thing when writing.
Good point. The I2C core provides I2C/SMBus locking to prevent trashing the bus itself, but that doesn't help the above. Which brings about another question... why wasn't
i2c_smbus_read_word_data()used in that case? Philip?
I actually tried that, but it didn't work. My original code had a #define that would let you try and re-enable it, but I ditched it when I didn't see any other drivers using read_word variants.
I should have spoken up that read_word seemed to be broken at the time.
But as I write this, I think I read in the chip data sheet that it doesn't support SMBus word reads... (Go figure...) That was probably the real reason I pulled the read_word code...
Quoted 68 lines
Quoted 4 lines
But what about this simpler patch, that pushes the lock down to the hardware reads and writes for multiple registers. And it doesn't try to mirror the write in the "data" structure, as it's always read with the latest values for every read.
It looks like this scheme would actually work for
lm85.cBut, for most chip drivers with fan divisors there would still be a race. E.g. look atset_fan_min()andset_fan_div()inlm78.c; the meaning ofdata->fan_mindepends ondata->fan_divso those two must be coherent. That locking can't be pushed further down.That lm85 doesn't use fan divisors is the exception rather than the rule. I mean, I don't hate this patch... but it doesn't tell us anything about how to fix the rest of them. I still think Philip's original brute force locking ;) is necessary there.
Quoted 16 lines
Does it look ok? It has the added benefit of making the .c and .o files smaller :)
int
lm85_write_value(struct i2c_client *client, u8 reg, int value){(quoted diff) +1
-0+ struct lm85_data *data = i2c_get_clientdata(client);int res ;drivers/i2c/chips/lm78.c new file +3
-0+/* serialize access to the hardware */ +down (&data->update_lock); +switch( reg ) { case LM85_REG_FAN(0) : /* Write WORD data */ case LM85_REG_FAN(1) :Review line 1006
Quoted 7 lines
(quoted diff) +1
-0@@ -1009,6 +1000,7 @@ res = i2c_smbus_write_byte_data(client, reg, value); break ; } + up(&data->update_lock); return res ; }I would add "
data->valid= 0;" somewhere before theup(). Then you're guaranteed never to read stale data after a write.Quoted 10 lines
(quoted diff) +0
-2@@ -1070,8 +1062,6 @@ struct lm85_data *data = i2c_get_clientdata(client); int i; - down(&data->update_lock); - if ( !data->valid || (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) { /* Things that change quickly */Review line 1202
Quoted 6 lines
(quoted diff) +0
-2@@ -1209,8 +1199,6 @@ }; /* last_config */ data->valid = 1; - - up(&data->update_lock); }We only want to service one
update_client()call per some interval. I think this lock needs to stay.
Quoted 1 line
And one last off-topic nit:
data->fan_ppris set but not used?
The fan_ppr features is only on the ADM1027 variant. Perhaps Margit didn't migrate that code? Otherwise, there are calls to PPR_FROM_REG() in the cvs code...
:v)
Signature · 4 lines
Philip Pokorny, Director of Engineering Tel: 415-358-2635 Fax: 415-358-2646 Toll Free: 888-PENGUIN PENGUIN COMPUTING, INC. www.penguincomputing.com
PATCH 1/1 i2c driver changes for 2.5.72
In reply to Greg KHI'll chime in here to agree.
You can't generally think of read_word_data as getting one byte and the next byte. If a chip supports it at all it may return a "word" (2 bytes)
from a single address (see Winbond chips vendor ID at address 0x4f). eeproms do it right by doing "autoincrement" to the next byte.
You can use 'i2cdump z 0xzz w' to experiment with
word reads and see how a chip behaves....
Quoted 35 lines · Mark M. Hoffman
Quoted 22 lines · Philip Pokorny
Quoted 8 lines · Mark M. Hoffman
Quoted 5 lines · Greg KH
Hm, so I can understand if we need to protect access to the hardware when we read two values in a row, like this:
res = i2c_smbus_read_byte_data(client, reg) & 0xff ; res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;Quoted 7 lines
Quoted 1 line
or the same thing when writing.
Good point. The I2C core provides I2C/SMBus locking to prevent trashing the bus itself, but that doesn't help the above. Which brings about another question... why wasn't
i2c_smbus_read_word_data()used in that case? Philip?I actually tried that, but it didn't work. My original code had a #define that would let you try and re-enable it, but I ditched it when I didn't see any other drivers using
read_wordvariants.Quoted 5 lines
I should have spoken up that
read_wordseemed to be broken at the time.But as I write this, I think I read in the chip data sheet that it doesn't support SMBus word reads... (Go figure...) That was probably the real reason I pulled the
read_wordcode...It's just as well... maybe some bus drivers don't support it either. It doesn't hurt to use lowest common denominator SMBus routines if possible.