PATCH: lm92: port for 2.6 kernel
The following patch adds 2.6 kernel support to the lm92 driver from sensors-2.9.0. Tested with linux-2.6.11-rc5 on ppc32.
- change i2c API for 2.6
- fix endian bugs - use cpu_to_le16(), not swab16()
- if max6635 is detected, show a different device name in sysfs
/james
-------------- next part --------------
lm92.c +125 -113
@@ -1,10 +1,12 @@ -/* * LM92 - Part of lm_sensors, Linux kernel modules for hardware * monitoring * * Author: Abraham van der Merwe <[email protected]> * +* - James Chapman <[email protected]> +* Modify for 2.6 kernels +* * Linux support for the National Semiconductor LM92 Temperature * Sensor. *
@@ -19,16 +21,28 @@#include <linux/config.h> #include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/i2c.h> +#include <linux/i2c-sensor.h> + +#include <linux/config.h> +#include <linux/module.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/slab.h> #include <linux/init.h> #include <linux/i2c.h> -#include <linux/i2c-proc.h> #include <linux/proc_fs.h> #include <linux/sysctl.h> #include <asm/semaphore.h> -#include "version.h" + +/* +* Addresses to scan +*/ + +static unsigned short normal_i2c[] = {0x48 ,0x4f ,I2C_CLIENT_END }; +static unsigned int normal_isa[] = {I2C_CLIENT_ISA_END };/* if defined, 4 faults must occur consecutively to set alarm flags */ /* #define ENABLE_FAULT_QUEUE */
@@ -55,9 +69,6 @@#define NATIVE_TO_PROC (x) ((x) * 625)#define CELSIUS (x) ((x) * 16) -static void lm92_temp (struct i2c_client *client,int operation,int ctl_name,int *nrels_mag,long *results); -static void lm92_alarms (struct i2c_client *client,int operation,int ctl_name,int *nrels_mag,long *results); -/* -- SENSORS SYSCTL START -- */ #define LM92_SYSCTL_ALARMS 2001/* high, low, critical */ #define LM92_SYSCTL_TEMP 1200/* high, low, critical, hysteresis, input */
@@ -71,16 +82,6 @@#define LM92_TEMP_HYST 0x40#define LM92_TEMP_INPUT 0x80 -/* -- SENSORS SYSCTL END -- */ - -static ctl_table lm92_dir_table[] = { - {LM92_SYSCTL_TEMP ,"temp" ,NULL ,0 ,0644 ,NULL , &i2c_proc_real, - &i2c_sysctl_real,NULL , &lm92_temp,NULL }, - {LM92_SYSCTL_ALARMS ,"alarms" ,NULL ,0 ,0444 ,NULL , &i2c_proc_real, - &i2c_sysctl_real,NULL , &lm92_alarms,NULL }, - {0 } -}; -/* NOTE: all temperatures are degrees centigrade * 16 */ typedef struct { struct i2c_client client;
@@ -103,17 +104,8 @@/* this is needed for each client driver method */ static struct i2c_driver lm92_driver; -/* ensure exclusive access to chip and static variables */ -static DECLARE_MUTEX (mutex); - -/* addresses to scan */ -static unsigned short normal_i2c[] = {SENSORS_I2C_END }; -static unsigned short normal_i2c_range[] = {0x48 ,0x4f ,SENSORS_I2C_END }; -static unsigned int normal_isa[] = {SENSORS_ISA_END }; -static unsigned int normal_isa_range[] = {SENSORS_ISA_END }; -/* insmod parameters */ -SENSORS_INSMOD_1 (lm92); +SENSORS_INSMOD_2 (lm92, max6635);static inline int lm92_write8 (struct i2c_client *client,u8 reg,u8 value) {
@@ -126,16 +118,16 @@if (tmp <0 )return (-EIO ); -/* convert the data to little endian format */ - *value =swab16 ((u16 ) tmp); +/* register values are little endian format */ + *value =le16_to_cpu ((u16 ) tmp);return (0 ); }static inline int lm92_write16 (struct i2c_client *client,u8 reg,u16 value) { -/* convert the data to big endian format */ -if (i2c_smbus_write_word_data (client, reg,swab16 (value)) <0 ) +/* register values are little endian format */ +if (i2c_smbus_write_word_data (client, reg,cpu_to_le16 (value)) <0 )return -EIO ;return 0 ;
@@ -143,7 +135,7 @@static int lm92_read (struct i2c_client *client) { -lm92_t *data = (lm92_t *) client->data; +lm92_t *data = (lm92_t *)i2c_get_clientdata (client);u16 value[5 ];if ((jiffies - data->timestamp) >HZ ) {
@@ -172,7 +164,7 @@static int lm92_write (struct i2c_client *client) { -lm92_t *data = (lm92_t *) client->data; +lm92_t *data = (lm92_t *)i2c_get_clientdata (client);LIMIT (data->temp.hyst);LIMIT (data->temp.crit);
@@ -188,49 +180,58 @@return (0 ); } -static void lm92_temp (struct i2c_client *client,int operation,int ctl_name,int *nrels_mag,long *results) -{ -if (!down_interruptible (&mutex)) { -lm92_t *data = (lm92_t *) client->data; - -if (operation =SENSORS_PROC_REAL_READ ) { -lm92_read (client); - results[0 ] =NATIVE_TO_PROC (data->temp.input); - results[1 ] =NATIVE_TO_PROC (data->temp.high); - results[2 ] =NATIVE_TO_PROC (data->temp.low); - results[3 ] =NATIVE_TO_PROC (data->temp.crit); - results[4 ] =NATIVE_TO_PROC (data->temp.hyst); - *nrels_mag =5 ; - }else if (operation =SENSORS_PROC_REAL_WRITE && *nrels_mag =4 ) { - data->temp.high =PROC_TO_NATIVE (results[0 ]); - data->temp.low =PROC_TO_NATIVE (results[1 ]); - data->temp.crit =PROC_TO_NATIVE (results[2 ]); - data->temp.hyst =PROC_TO_NATIVE (results[3 ]); -lm92_write (client); - }else if (operation =SENSORS_PROC_REAL_INFO ) { - *nrels_mag =4 ; - } - -up (&mutex); - } -} - -static void lm92_alarms (struct i2c_client *client,int operation,int ctl_name,int *nrels_mag,long *results) -{ -if (!down_interruptible (&mutex)) { -lm92_t *data = (lm92_t *) client->data; - -if (operation =SENSORS_PROC_REAL_READ ) { -lm92_read (client); - results[0 ] = data->alarms.high || (data->alarms.low <<1 ) || (data->alarms.crit <<2 ); - *nrels_mag =1 ; - }else if (operation =SENSORS_PROC_REAL_INFO ) { - *nrels_mag =0 ; - } +/* +* Sysfs stuff +*/ -up (&mutex); - } -} +#define show_temp (value, converter) \ +static ssize_t show_temp_##value(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + lm92_t *data = i2c_get_clientdata(client); \ + lm92_read(client); \ + return sprintf(buf, "%ld\n", converter(data->temp.value)); \ +} +show_temp (input,NATIVE_TO_PROC ); +show_temp (low,NATIVE_TO_PROC ); +show_temp (high,NATIVE_TO_PROC ); +show_temp (crit,NATIVE_TO_PROC ); +show_temp (hyst,NATIVE_TO_PROC ); + +#define set_temp (value, reg) \ +static ssize_t set_temp_##value(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + lm92_t *data = i2c_get_clientdata(client); \ + data->temp.value = PROC_TO_NATIVE(simple_strtol(buf, NULL, 10)); \ + lm92_write8(client, reg, data->temp.value); \ + return count; \ +} +set_temp (low,LM92_REG_TRIP_LOW ); +set_temp (high,LM92_REG_TRIP_HIGH ); +set_temp (crit,LM92_REG_TRIP_CRITICAL ); +set_temp (hyst,LM92_REG_TRIP_HYSTERESIS ); + +static ssize_t show_alarms (struct device *dev,char *buf) +{ +struct i2c_client *client =to_i2c_client (dev); +lm92_t *data =i2c_get_clientdata (client); +lm92_read (client); +return sprintf (buf,"%ld\n" , (data->alarms.crit <<2 ) | + (data->alarms.high <<1 ) | (data->alarms.low <<0 )); +} + +static DEVICE_ATTR(temp_input, S_IRUGO, show_temp_input, NULL); +static DEVICE_ATTR(temp_min, S_IWUSR | S_IRUGO, show_temp_low, + set_temp_low); +static DEVICE_ATTR(temp_max, S_IWUSR | S_IRUGO, show_temp_high, + set_temp_high); +static DEVICE_ATTR(temp_crit, S_IWUSR | S_IRUGO, show_temp_crit, + set_temp_crit); +static DEVICE_ATTR(temp_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, + set_temp_hyst); +static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);static int max6635_check (struct i2c_client *client) {
@@ -263,7 +264,7 @@static int lm92_init_client (struct i2c_client *client) { -lm92_t *data = (lm92_t *) client->data; +lm92_t *data = (lm92_t *)i2c_get_clientdata (client);u8 value =0 ;int result;
@@ -297,13 +298,13 @@return (0 ); } -static int lm92_detect (struct i2c_adapter *adapter,int address,unsigned short flags,int kind) +static int lm92_detect (struct i2c_adapter *adapter,int address,int kind) {static int id =0 ;struct i2c_client *client;lm92_t *data; -int result; -u16 manufacturer; +int result = -ENODEV ; +const char *name ="" ;if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE_DATA ))return (-ENODEV );
@@ -311,58 +312,68 @@if (!(data =kmalloc (sizeof (lm92_t),GFP_KERNEL )))return (-ENOMEM ); +memset (data,0 ,sizeof (lm92_t)); + client = &data->client; + client->id = id++; client->addr = address; - client->data = data; +i2c_set_clientdata (client, data); client->adapter = adapter; client->driver = &lm92_driver; client->flags =0 ; -strcpy (client->name,lm92_driver.name); -if (down_interruptible (&mutex)) { - result = -ERESTARTSYS ; - goto ERROR1; - } +/* Default to an LM92 if forced */ +if (kind =0 ) + kind = lm92;if (kind <0 ) { -/* Is it an lm92? */ -if (address <0x4c - && (lm92_read16 (client,LM92_REG_MANUFACTURER ,&manufacturer) <0 - || manufacturer !=LM92_MANUFACTURER_ID )) { +u16 man_id =0xffff ; + +if (lm92_read16 (client,LM92_REG_MANUFACTURER , &man_id) <0 ) {/* Is it a MAX6635/MAX6635/MAX6635? */ -if (!max6635_check (client)) { - result = -ENODEV ; - goto ERROR2; - } +if (!max6635_check (client)) + kind = max6635; + } + +/* National Semiconductor LM92? */ +if (man_id =LM92_MANUFACTURER_ID ) + kind = lm92; + +if (kind <=0 ) {/* identification failed */ +dev_info (&adapter->dev, +"Unsupported chip (man_id=0x%04X)\n" , + man_id); + goto ERROR1; } } -if ((result =i2c_attach_client (client))) { - goto ERROR2; - } +if (kind = lm92) + name ="lm92" ; +else if (kind = max6635) + name ="max6635" ; -if ((result =i2c_register_entry (client, client->name, lm92_dir_table, -THIS_MODULE )) <0 ) { - goto ERROR3; +strlcpy (client->name, name,I2C_NAME_SIZE ); + +if ((result =i2c_attach_client (client))) { + goto ERROR1; } - data->sysctl_id = result;if ((result =lm92_init_client (client)) <0 ) { - goto ERROR4; + goto ERROR2; } - client->id = id++; - -up (&mutex); +/* Register sysfs hooks */ +device_create_file (&client->dev, &dev_attr_temp_input); +device_create_file (&client->dev, &dev_attr_temp_min); +device_create_file (&client->dev, &dev_attr_temp_max); +device_create_file (&client->dev, &dev_attr_temp_hyst); +device_create_file (&client->dev, &dev_attr_temp_crit); +device_create_file (&client->dev, &dev_attr_alarms);return (0 ); -ERROR4: -i2c_deregister_entry (data->sysctl_id); -ERROR3: -i2c_detach_client (client); ERROR2: -up (&mutex); +i2c_detach_client (client); ERROR1:kfree (data);return result;
@@ -370,25 +381,27 @@static int lm92_attach_adapter (struct i2c_adapter *adapter) { -return i2c_detect (adapter,&addr_data,lm92_detect); +if (!(adapter->class &I2C_CLASS_HWMON )) +return 0 ; + +return i2c_detect (adapter, &addr_data, lm92_detect); }static int lm92_detach_client (struct i2c_client *client) {int result; -i2c_deregister_entry (((lm92_t *) (client->data))->sysctl_id); -if ((result =i2c_detach_client (client)))return (result); -kfree (client->data); +kfree (i2c_get_clientdata (client));return (0 ); }static struct i2c_driver lm92_driver = { + .owner =THIS_MODULE , .name ="lm92" , .id =I2C_DRIVERID_LM92 , .flags =I2C_DF_NOTIFY ,
@@ -398,7 +411,6 @@static int __init sm_lm92_init (void ) { -printk ("lm92.o version %s (%s)\n" ,LM_VERSION ,LM_DATE );return i2c_add_driver (&lm92_driver); }
Hi James,
Quoted 5 lines
Quoted 3 lines
Damn. I have done the exact same work one week ago... I have set a link to my patch on our "Supported Devices" page, but maybe I should have advertised more about it. I'm really sorry we duplicated the work.
When do you plan to push the work to the 2.6 kernel?
Right after you have confirmed it works for you. My driver is supposed to be finished, it really only needs testing.
Quoted 1 line
Is there a BitKeeper kernel tree for I2C changes?
Yes, Greg KH's bk-i2c tree.
Quoted 1 line
I thought the I2C devices pulled data from devices as a byte stream.
They do.
Quoted 2 lines
Don't the i2c_smbus_[read|write]_word_data() routines pass the u16 data in little endian format because that's the endian of the SMbus?
Not exactly. The bytes arrive LSB first, but the i2c bus drivers then convert the word to the endianess of the system. At least they are supposed to, those who don't are broken, if any.
Quoted 2 lines
Surely if
swab16()is used rather thancpu_to_le16(), different values are passed to thexxx_word_data()routines depending on cpu endian?
No, since the conversion is done by the bus drivers. The only reason why swab16 is used is because the chips do not respect the SMBus specs (with good reasons, but still...)
Quoted 2 lines
My CPU is a ppc. On big endian CPUs,
cpu_to_le16()andswab16()are equivalent. It's little endian CPUs that I'm worried about...
swab16 (or equivalent) has been used in lm_sensors since 1999 or so, and like 95% of the lm_sensors users are on x86, so you really shouldn't worry about them, they are working just fine ;)
Quoted 2 lines
The I2C bus driver is a mv64x60 (Marvell Discovery). The driver was recently written by Mark Greer. I'm cc'ing him on this.
I partly reviewed Mark's code, IIRC. It was rather good so I would not suspect it. Anyway I think I made it clear that there is no endianess bug at all.
Quoted 3 lines
Looking at other drivers, they put different chip names into sysfs even though the drivers work in exactly the same way for each chip. So why not LM92?
Not so many do. lm90, adm1025 maybe... any other? It happens that I wrote both drivers, and added several "types" for otherwise siilar chips, against the majority's opinion, back then (I was new to the project). I have made up my mind though, for a reason given right below.
Quoted 2 lines
I think it's really useful to identify the exact chip name now that it's easy to do so with 2.6.
It wasn't difficult with 2.4 either, that I can see. But you can printk the name of the chip if you want, it doesn't really have to make it into sysfs.
Quoted 2 lines
In fact I was going to submit another patch to differentiate the 6633/6634/6635 devices too.
That's a different matter since the MAX6633 and MAX6634 are not exact clones of the LM92. Possible addresse ranges differ, and some functions are missing. Reliable differenciation promises to be complex, BTW. I wasn't planning to add support for the MAX6633 and MAX6634 yet, not until someone really needed it. We never had any report for these chips. Same goes for the LM76 (we only had one report and it was a home made design under linux 2.4 IIRC).
Quoted 2 lines
Why does the libsensors code care what the chip is called? It knows the driver name from the
/sys/bus/i2c/drivers/xxxdirectory entry...
I invite you to take a look at the libsensors code. You'll find out that libsensors is really stupid. It is not currently able to parse the sysfs directories and find out the chip features on its own. Instead, each feature of each chip must be individually listed. This explains the size of both libsensors and the sensors program
Yes, this is a pity, but originally the library was really only handling two chips, not 100+ like it does now. The whole thing needs a full rewrite, but I'm out of time.
Quoted 2 lines
Well lm90 has 4 other compatible devices. The driver puts those chip names out into /sysfs... Why make another rule for lm92?
As said above the lm90 isn't the correct example to follow. Actually it would be the exception, not the rule. And I am not exactly proud of it.
Quoted 1 line
The
/sys/bus/i2cfiles should be as accurate and meaningful as possible.
Sure it should. And libsensors should be rewritten to be freed of any chip-dependant stuff. And the sysfs interface for sensors should be made totally free of chip-dependant stuff. And all of these must be done in the reverse order, so this is a long way to go. Feel free to contribute :)
Thanks,
--
Jean Delvare
Hi James,
Quoted 6 lines
Quoted 4 lines
It is known to be broken, in particular setting limits doesn't work. I have fixed almost everything yesterday. I'll finish the job this evening, then I'll publish the new version, and would be very happy if you could test it.
Sure I'll test it. I'll wait until you post the new version.
Here it is:
http://jdelvare.net1.nerim.net/sensors/linux-2.6.11-rc5-i2c-lm92-rc1.diff
Tested to work using i2c-stub (I have no LM92/MAX6635 chip myself). Note that you'll need lm_sensors CVS for proper libsensors support and sample sensors.conf section.
Waiting for your feedback :)
Thanks,
Signature · 1 line
Jean Delvare
Hi James,
Quoted 6 lines
Quoted 6 lines
I invite you to take a look at the libsensors code. You'll find out that libsensors is really stupid. It is not currently able to parse the sysfs directories and find out the chip features on its own. Instead, each feature of each chip must be individually listed. This explains the size of both libsensors and the sensors program
Quoted 8 lines
Quoted 3 lines
Yes, this is a pity, but originally the library was really only handling two chips, not 100+ like it does now. The whole thing needs a full rewrite, but I'm out of time.
Not everyone who makes use of the Linux support of sensor chips uses libsensors... Indeed, many embedded devices do their own hardware monitoring using custom applications. The kernel's i2c sensors API should not be restricted by one application.
Reading raw values is possible for some sensors but not for all. Some sensors need scaling (typically voltages), some temperature sensors even need complete thermistor computation. Libsensors also centralizes labels and channel discarding ("ignore" lines). I sincerely believe that libsensors is a good idea. What sucks is the current implementation, which is in part the result of the original design of the Linux 2.4 procfs interface for sensors. The lm_sensors project really shows its age, and the fact that it wasn't designed for the extreme variety of hardware we have now. With libsensors and sensors weighting 165kB and 128kB, repsectively, I definitely can understand why embedded folks prefer direct accesses.
So, in fact everyone should (IMHO) use libsensors, which should be chip-independent and lightweight.
Among others, libsensors has the advantage that it (now) supports both the 2.4 and 2.6 kernel interfaces, and we update it when the interface changes or chip drivers are ported. This is a valid reason to invite application developers to rely on it.
The kernel API isn't exactly restricted by libsensors, as you seem to think. It is in fact restricted by real world users who dislike compatbility breakage. I would really like to redesign the whole sysfs interface for sensors again. There are half a dozen major things I would change. However, this would break compatibility so badly that there would be no way to adapt libsensors, which would mean rewriting it entirely, dropping 2.4 support, rewriting the sensors program, and helping two dozen developers port their own applications to the new library. I would love being paid for the job, but at the moment I am working on this on my sole free time, and the amount of work is way above my capabilities and knowledge.
Quoted 3 lines
My concern about taking shortcuts in the implementation of /sysfs for the i2c chip drivers is that these shortcuts tend to live for a long time.
The shortcuts have been taken 6 years ago, and we live with this history now. If you have ideas to make things better without breaking everything, they are very welcome.
Quoted 4 lines
If libsensors dictates that the sysfs node
/sys/bus/i2c/drivers/xxx/yyy/nameis always the name of the driver xxx rather than the actual chip detected, there is no simple way for an application to discover the actual chip identities.
name isn't always xxx. xxx is a driver name, name is a "kind" (or type) name. What libsensors dictates is that we avoid adding clones names, just because it creates additional dependencies between kernel and libsensors versions, with little benefit.
In fact, having the same kind for two chips (like lm92 for both the LM92 and the MAX6635) is considered a benefit in some cases. Sometimes hardware manufaturer use various compatible chips for a given board model. People buy many boards of a model, or many system based on that board, and then wonder why lm_sensors works on some and not on others, while the board is supposedly the same. By masking the fact that some chips are clones of others, we make life easier for them.
Quoted 4 lines
I think the i2c /sysfs files should be designed such that an application can determine which devices exist and what features are supported. Right now it seems we can only determine which i2c drivers exist and what features the devices detected by those drivers support.
I see no real difference. Where several chips share a type name, all these chips do have the same feature set, so the user application wouldn't make a difference anyway. The only information missing is the name of the physical chip. Frankly it isn't that important for most users if both chips are real clones.
Quoted 9 lines
Quoted 5 lines
Sure it should. And libsensors should be rewritten to be freed of any chip-dependant stuff. And the sysfs interface for sensors should be made totally free of chip-dependant stuff. And all of these must be done in the reverse order, so this is a long way to go. Feel free to contribute :)
:) I'm trying. :)
Is anyone working on such a rewrite?
Not that I know. I do have some precise ideas of what should be done (and what shouldn't) but unfortunately there are technical points that prevent me from going on: configuration file parsing and library interface to name only the major two. But I'd be very happy to discuss this with you if you want to give it a try.
Thanks,
Signature · 1 line
Jean Delvare
Hi Mark,
Quoted 5 lines
Uhh oh... The
i2c-mv64xxx.cdriver--note that the ctlr is *not* and smbus ctlr and only provides one byte at a time to the driver--that I wrote simply puts the incoming bytes into the buffer in the order that they arrive (and sends them out in the order that they appear in the buffer (i.e., as a byte-stream)). That is wrong?!?!
No, it's all OK. Don't worry :)
Quoted 4 lines
So, after the read transfer is completed, I should go thru the buffer applying
le16_to_cpu()to each pair of bytes? What if there are an odd number of bytes? Also, I guess I should applycpu_to_le16()to the buffer before I send it out?
No. The byte swapping thing only makes sense for word transfers anyway, as defined by the SMBus specification. In you case, your driver provides low control (byte-level and flags) control over the bus, if I read correctly. You do not implement SMBus functions individually. The i2c-core takes care of it for you, through an smbus-over-i2c-emulation function (i2c_smbus_xfer_emulated). Look at the function and you'll see this:
case I2C_SMBUS_WORD_DATA:
(...)
data->word = msgbuf1[0] | (msgbuf1[1] << 8);
break;So as you see it'll work regardless of the endianess (although not necessarily in the most efficient way).
No need to worry, really, your code is alright as far as I can see :)
Signature · 1 line
Jean Delvare
Hi Jean,
Quoted 6 lines · Jean Delvare
Quoted 2 lines
The following patch adds 2.6 kernel support to the lm92 driver from sensors-2.9.0. Tested with linux-2.6.11-rc5 on ppc32.
Damn. I have done the exact same work one week ago... I have set a link to my patch on our "Supported Devices" page, but maybe I should have advertised more about it. I'm really sorry we duplicated the work.
When do you plan to push the work to the 2.6 kernel? Is there a BitKeeper kernel tree for I2C changes?
Quoted 9 lines
Quoted 1 line
- fix endian bugs - use
cpu_to_le16(), notswab16()I do not think there actually is a bug. The SMBus specs say that word values are transmitted LSB first (hint: SMBus was developed by Intel). The LM92 (and every other hardware monitoring chip I know of, for that matter) transmits MSB first. Rationale for this is that it allows people not interested in decimals to only read the MSB, or so I read. So the
swab16()is there for this reason, which isn't related to the CPU of the host system in any way.
I thought the I2C devices pulled data from devices as a byte stream. Don't the i2c_smbus_[read|write]_word_data() routines pass the u16 data in little endian format because that's the endian of the SMbus?
Surely if swab16() is used rather than cpu_to_le16(), different values are passed to the xxx_word_data() routines depending on cpu endian?
Quoted 3 lines
If you really needed to do that change for the lm92 driver to work properly, then I would suspect a bug in the i2c bus driver you are using. Which is it, BTW?
My CPU is a ppc. On big endian CPUs, cpu_to_le16() and swab16() are equivalent. It's little endian CPUs that I'm worried about...
The I2C bus driver is a mv64x60 (Marvell Discovery). The driver was recently written by Mark Greer. I'm cc'ing him on this.
Quoted 7 lines
Quoted 1 line
- if max6635 is detected, show a different device name in sysfs
I had done this in the first place too, but now I am not sure it is a good idea. Doing this requires adding some code to libsensors, sensors and about every program relying on libsensors. I don't think it's worth the effort, especially since the LM92 doesn't seem to be found on any PC motherboard.
Looking at other drivers, they put different chip names into sysfs even though the drivers work in exactly the same way for each chip. So why not LM92?
I think it's really useful to identify the exact chip name now that it's easy to do so with 2.6. In fact I was going to submit another patch to differentiate the 6633/6634/6635 devices too.
Why does the libsensors code care what the chip is called? It knows the driver name from the /sys/bus/i2c/drivers/xxx directory entry...
Quoted 2 lines
I suspect it is actually only found on homebrew
designs. Is it your case?
My board is a commercial ppc32 VME card.
Quoted 2 lines
The MAX6635 is really a clone of the LM92, so
I see little benefit in having a separate prefix.
Well lm90 has 4 other compatible devices. The driver puts those chip names out into /sysfs... Why make another rule for lm92?
The /sys/bus/i2c files should be as accurate and meaningful as possible.
Quoted 7 lines
My patch for the new lm92 driver is here:
http://jdelvare.net1.nerim.net/sensors/linux-2.6.11-rc4-bk5-i2c-lm92.diffIt is known to be broken, in particular setting limits doesn't work. I have fixed almost everything yesterday. I'll finish the job this evening, then I'll publish the new version, and would be very happy if you could test it.
Sure I'll test it. I'll wait until you post the new version.
Quoted 7 lines
Of course another possibility would be that we go with your patch. However, since I have a great experience with hardware monitoring drivers in Linux 2.6, it's likely that there will be less fixes required if we start from my version. Also, for the lm92 I did not really port the original driver but merely rewrote it, because the original driver was very different from the other ones, making code maintainance over time much harder.
I don't care which version we go with as long as it works and someone is working on getting it integrated into the kernel tree.
Signature · 2 lines
James Chapman PGP key : http://www.katalix.com/~jchapman/pgpkey.txt
Hi Jean,
Your lm92 driver works just fine. Tested on my ppc32 board using mv64xxx i2c bus driver.
/james
Quoted 20 lines · Jean Delvare
Hi James,
Quoted 6 lines
Quoted 4 lines
It is known to be broken, in particular setting limits doesn't work. I have fixed almost everything yesterday. I'll finish the job this evening, then I'll publish the new version, and would be very happy if you could test it.
Sure I'll test it. I'll wait until you post the new version.
Here it is:
http://jdelvare.net1.nerim.net/sensors/linux-2.6.11-rc5-i2c-lm92-rc1.diffTested to work using i2c-stub (I have no LM92/MAX6635 chip myself). Note that you'll need
lm_sensorsCVS for proper libsensors support and sample sensors.conf section.Waiting for your feedback :)
Hi Jean,
Quoted 7 lines · Jean Delvare
Quoted 2 lines
Don't the i2c_smbus_[read|write]_word_data() routines pass the u16 data in little endian format because that's the endian of the SMbus?
Not exactly. The bytes arrive LSB first, but the i2c bus drivers then convert the word to the endianess of the system. At least they are supposed to, those who don't are broken, if any.
Quoted 6 lines
Quoted 2 lines
Surely if
swab16()is used rather thancpu_to_le16(), different values are passed to thexxx_word_data()routines depending on cpu endian?No, since the conversion is done by the bus drivers. The only reason why swab16 is used is because the chips do not respect the SMBus specs (with good reasons, but still...)
I see. Thanks for taking the time to explain this.
Quoted 9 lines
Quoted 2 lines
Why does the libsensors code care what the chip is called? It knows the driver name from the
/sys/bus/i2c/drivers/xxxdirectory entry...I invite you to take a look at the libsensors code. You'll find out that libsensors is really stupid. It is not currently able to parse the sysfs directories and find out the chip features on its own. Instead, each feature of each chip must be individually listed. This explains the size of both libsensors and the sensors program
Quoted 3 lines
Yes, this is a pity, but originally the library was really only handling two chips, not 100+ like it does now. The whole thing needs a full rewrite, but I'm out of time.
Not everyone who makes use of the Linux support of sensor chips uses libsensors... Indeed, many embedded devices do their own hardware monitoring using custom applications. The kernel's i2c sensors API should not be restricted by one application.
My concern about taking shortcuts in the implementation of /sysfs for the i2c chip drivers is that these shortcuts tend to live for a long time. If libsensors dictates that the sysfs node /sys/bus/i2c/drivers/xxx/yyy/name is always the name of the driver xxx rather than the actual chip detected, there is no simple way for an application to discover the actual chip identities.
I think the i2c /sysfs files should be designed such that an application can determine which devices exist and what features are supported. Right now it seems we can only determine which i2c drivers exist and what features the devices detected by those drivers support.
Quoted 7 lines
Quoted 1 line
The
/sys/bus/i2cfiles should be as accurate and meaningful as possible.Sure it should. And libsensors should be rewritten to be freed of any chip-dependant stuff. And the sysfs interface for sensors should be made totally free of chip-dependant stuff. And all of these must be done in the reverse order, so this is a long way to go. Feel free to contribute :)
:) I'm trying. :)
Is anyone working on such a rewrite?
Signature · 2 lines
James Chapman PGP key : http://www.katalix.com/~jchapman/pgpkey.txt
Quoted 11 lines · Jean Delvare
Quoted 4 lines
Don't the i2c_smbus_[read|write]_word_data() routines pass the u16 data in little endian format because that's the endian of the SMbus?
Not exactly. The bytes arrive LSB first, but the i2c bus drivers then convert the word to the endianess of the system. At least they are supposed to, those who don't are broken, if any.
Quoted 9 lines
Quoted 4 lines
Surely if
swab16()is used rather thancpu_to_le16(), different values are passed to thexxx_word_data()routines depending on cpu endian?No, since the conversion is done by the bus drivers. The only reason why swab16 is used is because the chips do not respect the SMBus specs (with good reasons, but still...)
Uhh oh... The i2c-mv64xxx.c driver--note that the ctlr is *not* and smbus ctlr and only provides one byte at a time to the driver--that I wrote simply puts the incoming bytes into the buffer in the order that they arrive (and sends them out in the order that they appear in the buffer (i.e., as a byte-stream)). That is wrong?!?!
So, after the read transfer is completed, I should go thru the buffer applying le16_to_cpu() to each pair of bytes? What if there are an odd number of bytes? Also, I guess I should apply cpu_to_le16() to the buffer before I send it out?
Mark
Quoted 12 lines · Jean Delvare
Hi Mark,
Quoted 7 lines
Uhh oh... The
i2c-mv64xxx.cdriver--note that the ctlr is *not* and smbus ctlr and only provides one byte at a time to the driver--that I wrote simply puts the incoming bytes into the buffer in the order that they arrive (and sends them out in the order that they appear in the buffer (i.e., as a byte-stream)). That is wrong?!?!
Quoted 2 lines
No, it's all OK. Don't worry :)
Whew!
/me's heart rate drops below 200 bpm. :)
Quoted 7 lines
Quoted 6 lines
So, after the read transfer is completed, I should go thru the buffer applying
le16_to_cpu()to each pair of bytes? What if there are an odd number of bytes? Also, I guess I should applycpu_to_le16()to the buffer before I send it out?
Quoted 5 lines
No. The byte swapping thing only makes sense for word transfers anyway, as defined by the SMBus specification. In you case, your driver provides low control (byte-level and flags) control over the bus, if I read correctly.
You do.
Quoted 10 lines
You do not implement SMBus functions individually. The i2c-core takes care of it for you, through an smbus-over-i2c-emulation function (
i2c_smbus_xfer_emulated). Look at the function and you'll see this:case I2C_SMBUS_WORD_DATA: (...) data->word = msgbuf1[0] | (msgbuf1[1] << 8); break;
Ah, missed that part during my panic search of the emulated stuff.
Quoted 6 lines
So as you see it'll work regardless of the endianess (although not necessarily in the most efficient way).
No need to worry, really, your code is alright as far as I can see :)
Thanks Jean. I can go back to sleep now. ;)
Mark
Quoted 16 lines · Mark A. Greer
Quoted 7 lines · Jean Delvare
Quoted 2 lines
Don't the i2c_smbus_[read|write]_word_data() routines pass the u16 data in little endian format because that's the endian of the SMbus?
Not exactly. The bytes arrive LSB first, but the i2c bus drivers then convert the word to the endianess of the system. At least they are supposed to, those who don't are broken, if any.
Quoted 7 lines
Quoted 2 lines
Surely if
swab16()is used rather thancpu_to_le16(), different values are passed to thexxx_word_data()routines depending on cpu endian?No, since the conversion is done by the bus drivers. The only reason why swab16 is used is because the chips do not respect the SMBus specs (with good reasons, but still...)
Quoted 5 lines
Uhh oh... The
i2c-mv64xxx.cdriver--note that the ctlr is *not* and smbus ctlr and only provides one byte at a time to the driver--that I wrote simply puts the incoming bytes into the buffer in the order that they arrive (and sends them out in the order that they appear in the buffer (i.e., as a byte-stream)). That is wrong?!?!
This was what was confusing me too. As I understand it now (Jean please correct me if I'm wrong) the core code behind the smbus read/write word_data() routines presents the u16 value as a little endian value. If the chip driver knows the chip implements 16-bit registers as big endian then it must swap the bytes, regardless of cpu endian.
I don't think Jean's statement that "the conversion is done by the bus drivers" is true, but maybe I still don't understand... :)
Quoted 4 lines
So, after the read transfer is completed, I should go thru the buffer applying
le16_to_cpu()to each pair of bytes? What if there are an odd number of bytes? Also, I guess I should applycpu_to_le16()to the buffer before I send it out?
I don't see how a bus driver can do this when all it has is a single "xfer" routine to transfer a byte stream. I already have a driver that reads 8 bytes from consecutive byte-wide registers as an 8-byte transfer. Just because the byte stream has more than one byte, we can't endian-convert byte pairs...
/james
Hi James,
Quoted 2 lines
The following patch adds 2.6 kernel support to the lm92 driver from sensors-2.9.0. Tested with linux-2.6.11-rc5 on ppc32.
Damn. I have done the exact same work one week ago... I have set a link to my patch on our "Supported Devices" page, but maybe I should have advertised more about it. I'm really sorry we duplicated the work.
Quoted 1 line
- fix endian bugs - use
cpu_to_le16(), notswab16()
I do not think there actually is a bug. The SMBus specs say that word values are transmitted LSB first (hint: SMBus was developed by Intel). The LM92 (and every other hardware monitoring chip I know of, for that matter) transmits MSB first. Rationale for this is that it allows people not interested in decimals to only read the MSB, or so I read. So the swab16() is there for this reason, which isn't related to the CPU of the host system in any way.
If you really needed to do that change for the lm92 driver to work properly, then I would suspect a bug in the i2c bus driver you are using. Which is it, BTW?
Quoted 1 line
- if max6635 is detected, show a different device name in sysfs
I had done this in the first place too, but now I am not sure it is a good idea. Doing this requires adding some code to libsensors, sensors and about every program relying on libsensors. I don't think it's worth the effort, especially since the LM92 doesn't seem to be found on any PC motherboard. I suspect it is actually only found on homebrew designs. Is it your case? The MAX6635 is really a clone of the LM92, so I see little benefit in having a separate prefix.
My patch for the new lm92 driver is here:
http://jdelvare.net1.nerim.net/sensors/linux-2.6.11-rc4-bk5-i2c-lm92.diff
It is known to be broken, in particular setting limits doesn't work. I have fixed almost everything yesterday. I'll finish the job this evening, then I'll publish the new version, and would be very happy if you could test it.
Of course another possibility would be that we go with your patch. However, since I have a great experience with hardware monitoring drivers in Linux 2.6, it's likely that there will be less fixes required if we start from my version. Also, for the lm92 I did not really port the original driver but merely rewrote it, because the original driver was very different from the other ones, making code maintainance over time much harder.
Thanks,
--
Jean Delvare
Hi James,
Quoted 5 lines
This was what was confusing me too. As I understand it now (Jean please correct me if I'm wrong) the core code behind the smbus read/write
word_data()routines presents the u16 value as a little endian value. If the chip driver knows the chip implements 16-bit registers as big endian then it must swap the bytes, regardless of cpu endian.
Correct.
Quoted 2 lines
I don't think Jean's statement that "the conversion is done by the bus drivers" is true, but maybe I still don't understand... :)
It wasn't true in the case of Mark's driver, you are right. It is in fact true only for SMBus master drivers. For I2C masters, the i2c-core emulates the SMBus protocol, so the conversion is done by i2c-core.
What I wanted to focus on was the fact that, in the general case, any CPU endianness issue would need to be fixed in the bus driver, not the chip driver.
--
Jean Delvare