i2cdetect -l is broken
Hi all,
i2cdetect -l is broken on drivers which handle more than a single i2c bus (radeonfb in my case). It'll print the same name for all busses, and shouldn't.
I took a look at the sysfs tree and I couldn't find any way to correct it. The fact that i2c-adapters and i2c-devs don't use the same numbering policy doesn't give us much chance.
As far as I can tell, i2c-adapters numbering policy is to use N+1 for a new adapter, where N is the biggest number already use at the moment, while i2c-devs numbering policy is to use the lowest available.
Why don't we just chose one common policy for both? I have no strong preference, both policies are OK, but having two names for the same thing at a given moment is a real pain. Not being able to deduce the correct i2cdetect or i2cdump command from the output of sensors will make helping users way harder, I fear.
OK, I guess that I have been missing something. If it was that easy, wouldn't it have already been fixed?
Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 9 lines · Jean Delvare
Hi all,
i2cdetect -l is broken on drivers which handle more than a single i2c bus (radeonfb in my case). It'll print the same name for all busses, and shouldn't.
I took a look at the sysfs tree and I couldn't find any way to correct it. The fact that i2c-adapters and i2c-devs don't use the same numbering policy doesn't give us much chance.
But what about the linkages:
$ tree /sys/class/i2c-dev/
/sys/class/i2c-dev/
`-- i2c-0
|-- dev
|-- device -> ../../../devices/platform/i2c-0
`-- driver -> ../../../bus/i2c/drivers/i2c_adapter$ tree /sys/class/i2c-adapter/
/sys/class/i2c-adapter/
`-- i2c-0
|-- device -> ../../../devices/platform/i2c-0
`-- driver -> ../../../bus/i2c/drivers/i2c_adapterYou can match these devices up by looking at the 'device' symlink. This works no matter what numbering scheme there currently is, right?
Quoted 3 lines
As far as I can tell, i2c-adapters numbering policy is to use N+1 for a new adapter, where N is the biggest number already use at the moment, while i2c-devs numbering policy is to use the lowest available.
I think this is the same for 2.4, meaning the same problem is there too, right? But for 2.4, you don't have the sysfs linkage to help you out. How do you solve the problem there?
thanks,
greg k-h
Quoted 24 lines · Jean Delvare
Quoted 24 lines
Quoted 7 lines
i2cdetect -l is broken on drivers which handle more than a single i2c bus (radeonfb in my case). It'll print the same name for all busses, and shouldn't.
I took a look at the sysfs tree and I couldn't find any way to correct it. The fact that i2c-adapters and i2c-devs don't use the same numbering policy doesn't give us much chance.
But what about the linkages:
$ tree /sys/class/i2c-dev/ /sys/class/i2c-dev/ `-- i2c-0 |-- dev |-- device -> ../../../devices/platform/i2c-0 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter$ tree /sys/class/i2c-adapter/ /sys/class/i2c-adapter/ `-- i2c-0 |-- device -> ../../../devices/platform/i2c-0 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter
Quoted 7 lines
Quoted 2 lines
You can match these devices up by looking at the 'device' symlink. This works no matter what numbering scheme there currently is, right?
Yes, it looks like it does. But this requires that we walk both dirs and compare the entries. Not something easy to do in C, either we introduce data structures and have to handle memory allocations, or we end up with a non-linear algorithm.
libsysfs should be able to help you out here.
Quoted 2 lines
With our 2.4 system, the same operations was as simple as "cat
/proc/bus/i2c".
What did that show?
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
As far as I can tell, i2c-adapters numbering policy is to use N+1 for a new adapter, where N is the biggest number already use at the moment, while i2c-devs numbering policy is to use the lowest available.
I think this is the same for 2.4, meaning the same problem is there too, right? But for 2.4, you don't have the sysfs linkage to help you out. How do you solve the problem there?
Quoted 7 lines
I don't know much about the details, and I don't even understand why there are two series of identifiers, so I can't speeak about that. But for 2.4, the number given by sensors was also the one you could use in i2cdump. It never looked like there were two distinct series of identifiers there. Either they were in sync, or there was only one series, I can't say. And we have the list in
/proc/bus/i2c. No need to parse a complete tree.
Can you see if the numbers get out of sync:
modprobe i2c_SOME_ADAPTER_DRIVER
modprobe i2c_SOME_OTHER_ADAPTER_DRIVER
modprobe i2c_dev
rmmod i2c_SOME_ADAPTER_DRIVER
modprobe i2c_SOME_ADAPTER_DRIVER
where you have both types of adapters in your system (like something and i2c-isa). That should get your numbers out of sync on 2.6, right? What happens on 2.4?
Quoted 3 lines
What I see from a user-space developer, user and support-guy point of view is that this double numbering is a source of problems, and I'd like to see it solved if possible.
Ok, can you come up with a patch that might solve this?
thanks,
greg k-h
Quoted 24 lines
Quoted 7 lines
i2cdetect -l is broken on drivers which handle more than a single i2c bus (radeonfb in my case). It'll print the same name for all busses, and shouldn't.
I took a look at the sysfs tree and I couldn't find any way to correct it. The fact that i2c-adapters and i2c-devs don't use the same numbering policy doesn't give us much chance.
But what about the linkages:
$ tree /sys/class/i2c-dev/ /sys/class/i2c-dev/ `-- i2c-0 |-- dev |-- device -> ../../../devices/platform/i2c-0 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter$ tree /sys/class/i2c-adapter/ /sys/class/i2c-adapter/ `-- i2c-0 |-- device -> ../../../devices/platform/i2c-0 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter
Quoted 2 lines
You can match these devices up by looking at the 'device' symlink. This works no matter what numbering scheme there currently is, right?
Yes, it looks like it does. But this requires that we walk both dirs and compare the entries. Not something easy to do in C, either we introduce data structures and have to handle memory allocations, or we end up with a non-linear algorithm.
With our 2.4 system, the same operations was as simple as "cat /proc/bus/i2c".
Quoted 8 lines
Quoted 4 lines
As far as I can tell, i2c-adapters numbering policy is to use N+1 for a new adapter, where N is the biggest number already use at the moment, while i2c-devs numbering policy is to use the lowest available.
I think this is the same for 2.4, meaning the same problem is there too, right? But for 2.4, you don't have the sysfs linkage to help you out. How do you solve the problem there?
I don't know much about the details, and I don't even understand why there are two series of identifiers, so I can't speeak about that. But for 2.4, the number given by sensors was also the one you could use in i2cdump. It never looked like there were two distinct series of identifiers there. Either they were in sync, or there was only one series, I can't say. And we have the list in /proc/bus/i2c. No need to parse a complete tree.
What I see from a user-space developer, user and support-guy point of view is that this double numbering is a source of problems, and I'd like to see it solved if possible.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
But what about the linkages:
$ tree /sys/class/i2c-dev/ /sys/class/i2c-dev/ `-- i2c-0 |-- dev |-- device -> ../../../devices/platform/i2c-0 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter$ tree /sys/class/i2c-adapter/ /sys/class/i2c-adapter/ `-- i2c-0 |-- device -> ../../../devices/platform/i2c-0 `-- driver -> ../../../bus/i2c/drivers/i2c_adapterYou can match these devices up by looking at the 'device' symlink.
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
This works no matter what numbering scheme there currently is, right?
Yes, it looks like it does. But this requires that we walk both dirs and compare the entries. Not something easy to do in C, either we introduce data structures and have to handle memory allocations, or we end up with a non-linear algorithm.
BTW: Now that I come to think of it, no, it doesn't work. It works in your specific case because this isn't a PCI adapter. See these trees with a PCI adapter instead:
i2c-dev
|-- i2c-0
| |-- dev
| |-- device -> ../../../devices/pci0000:00/0000:00:04.4
| `-- driver -> ../../../bus/pci/drivers/vt596 smbus
`-- i2c-1
|-- dev
|-- device -> ../../../devices/platform/i2c-1
`-- driver -> ../../../bus/i2c/drivers/i2c_adapter6 directories, 2 files
i2c-adapter/
|-- i2c-1
| |-- device -> ../../../devices/platform/i2c-1
| `-- driver -> ../../../bus/i2c/drivers/i2c_adapter
`-- i2c-2
|-- device -> ../../../devices/pci0000:00/0000:00:04.4/i2c-2
`-- driver -> ../../../bus/i2c/drivers/i2c_adapter6 directories, 0 files
Adapter i2c-2 matches dev i2c-0. At first sight, you'd say I can guess that because both's device link point to PCI device "pci0000:00/0000:00:04.4". But what if that PCI device has more than one i2c bus? I'm lost, as i2cdetect is. So, unless I missed a thing, there is no way to find out which adapter matched with which device in such a case. I can go up to the PCI device, but am unable to pick the correct i2c adapter once there.
But I may be missing the whole thing once again. As said earlier, I just have no idea as to why there are two distincts numberings now while there wasn't before.
Quoted 1 line
libsysfs should be able to help you out here.
Could be, but we don't want i2cdetect to need libsysfs (what about Linux 2.4 users?), and we also need the information from a Perl script.
(Thanks for pointing out the existence of it however; it could be of much use for our future libsensors-new library.)
Quoted 4 lines
Quoted 2 lines
With our 2.4 system, the same operations was as simple as "cat
/proc/bus/i2c".What did that show?
i2c-0 smbus SMBus I801 adapter at 1880 Non-I2C SMBus adapter i2c-1 i2c ADM1032 evaluation board Bit-shift algorithm
Quoted 9 lines
Can you see if the numbers get out of sync:
modprobei2c_SOME_ADAPTER_DRIVER
modprobei2c_SOME_OTHER_ADAPTER_DRIVER
modprobei2c_dev
rmmodi2c_SOME_ADAPTER_DRIVER
modprobei2c_SOME_ADAPTER_DRIVERwhere you have both types of adapters in your system (like something and i2c-isa). That should get your numbers out of sync on 2.6, right?
Yes, it does. Sensors refer to SOME_ADAPTER_DRIVER as i2c-2, while i2cdump and i2detect refer to it as i2c-0.
Quoted 1 line
What happens on 2.4?
SOME_ADAPTER_DRIVER gets i2c-0 back as I load it the second time. No desync, sensors and i2cdump both refer to it as i2c-0.
Quoted 5 lines
Quoted 3 lines
What I see from a user-space developer, user and support-guy point of view is that this double numbering is a source of problems, and I'd like to see it solved if possible.
Ok, can you come up with a patch that might solve this?
I don't think I can. I don't know anything about sysfs, and I don't know anything about how the 2.4 drivers where handling the problem. I'm probably the less qualified among us all for that task.
Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 14 lines · Jean Delvare
BTW: Now that I come to think of it, no, it doesn't work. It works in your specific case because this isn't a PCI adapter. See these trees with a PCI adapter instead:
i2c-dev
|-- i2c-0 | |-- dev | |-- device -> ../../../devices/pci0000:00/0000:00:04.4 | `-- driver -> ../../../bus/pci/drivers/vt596 smbus `-- i2c-1 |-- dev |-- device -> ../../../devices/platform/i2c-1 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter
Quoted 8 lines
6 directories, 2 files
i2c-adapter/|-- i2c-1 | |-- device -> ../../../devices/platform/i2c-1 | `-- driver -> ../../../bus/i2c/drivers/i2c_adapter `-- i2c-2 |-- device -> ../../../devices/pci0000:00/0000:00:04.4/i2c-2 `-- driver -> ../../../bus/i2c/drivers/i2c_adapter
Hm, good point. Yeah, that's not nice :(
Quoted 5 lines
Quoted 1 line
Ok, can you come up with a patch that might solve this?
I don't think I can. I don't know anything about sysfs, and I don't know anything about how the 2.4 drivers where handling the problem. I'm probably the less qualified among us all for that task.
It's not a sysfs issue, it's an issue with the following line in i2c_add_adapter():
adap->nr = nr++;
We need to be smarter about how to number our adapters instead of just picking the next number. Something that matches the logic in get_free_i2c_dev() in i2c-dev.c.
Or we can modify i2c-dev.c to use the adapter number in it's device id. But if we do that we will eventually run out of minor numbers if we insert and remove adapters all of the time...
Any opinions on which would be better?
thanks,
greg k-h
Quoted 8 lines
It's not a sysfs issue, it's an issue with the following line in
i2c_add_adapter():adap->nr = nr++;We need to be smarter about how to number our adapters instead of just picking the next number. Something that matches the logic in
get_free_i2c_dev()ini2c-dev.c.
Quoted 5 lines
Or we can modify
i2c-dev.cto use the adapter number in it's device id. But if we do that we will eventually run out of minor numbers if we insert and remove adapters all of the time...Any opinions on which would be better?
Unless solution #1 is technically awful or even impossible, I vote for it, mainly because of the minor runout issue you mention with solution #2, and also because it will bring us back to the policy we had in 2.4.
If I'm not mistaking, this basically requires that we can access the already registered adapters list from i2c_add_adapter(). Depending on the form in which it is stored, the algorithm to get the lowest available number will be more or less sympathetic. In get_free_i2c_dev() this is a simple array of pointers, all we need to look for is a NULL value, so the complexity is O(N). I fear it won't be that simple with adapters.
If you could tell me how we do access the adapters list, so that I could take a look...
BTW, I found something to be fixed in i2c-core:
drivers/i2c/i2c-core.c renamed from drivers/i2c/i2c-core.c.orig +1 -1
@@ -175,7 +175,7 @@ driver = list_entry(item, struct i2c_driver, list);if (driver->detach_adapter)if ((res = driver->detach_adapter (adap))) { -dev_warn (&adap->dev,"can't detach adapter" +dev_warn (&adap->dev,"can't detach adapter " "while detaching driver %s: driver not " "detached!" , driver->name); goto out_unlock;
Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 5 lines · Jean Delvare
Quoted 2 lines
As we discussed before one problem is that adapter numbers don't match device numbers.
This is the main problem for sure. I guess (hope) that Greg will take a look someday if he has some free time?
Yeah, I'll try to get to that next week...
Quoted 5 lines
Quoted 2 lines
So can we move, link, or create an additional 'name' entry in each directory
/sys/class/i2c-dev/i2c-x??This is the kind of question we want to ask to Greg (which supposes you CC him ;)).
What would that "name" file hold?
thanks,
greg k-h
Quoted 3 lines
Thanks for finding the problem in i2cdetect -l which
is the code ini2cbusses.c, which I wrote but am not particularly proud of.
It could be worse. And you're not reponsible for sysfs not providing what procfs used to give us.
Quoted 2 lines
Remember that my one and only goal was to reproduce, exactly, what was in
/proc/bus/i2c.
Yes, I got it. Reason for it is obvious and I have to agree.
Quoted 2 lines
As we discussed before one problem is that adapter numbers don't match device numbers.
This is the main problem for sure. I guess (hope) that Greg will take a look someday if he has some free time?
Quoted 7 lines
(somewhat reviewing to make sure I understand the problem) But in this case the simpler problem is that there is no 'name' entry in
/sys/class/i2c-dev/i2c-x/directories.
Instead it is in/sys/class/i2c-dev/i2c-x/device/i2c-y/directory where x != y in the general case. Which is still fine unless a device has more than one bus, then you don't know which 'y'. And thei2cbusses.ccode assumes only one bus.
Correct.
Quoted 2 lines
So can we move, link, or create an additional 'name' entry in each directory
/sys/class/i2c-dev/i2c-x??
This is the kind of question we want to ask to Greg (which supposes you CC him ;)).
Quoted 1 line
Then we know what device it is. Problem solved.
This would solve the i2cdetect -l issue. Not the sensors vs. i2cdump one, which is more important, methinks.
Quoted 1 line
Can this be done in sysfs?
I'm sure it is. The question is: how uneasy will it be?
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 5 lines · Mark Studebaker
Exactly the same as what's in
/sys/class/i2c-dev/i2c-x/device/i2c-y/namefile now, for exampleSMBus AMD756 adapter at 80e0
Ah, that would be nice. I'll see if I can get that to work...
thanks,
greg k-h
Thanks for finding the problem in i2cdetect -l which
is the code in i2cbusses.c, which I wrote but am not particularly proud of.
Remember that my one and only goal was to reproduce, exactly, what was in /proc/bus/i2c.
As we discussed before one problem is that adapter numbers don't match device numbers.
(somewhat reviewing to make sure I understand the problem) But in this case the simpler problem is that there is no 'name' entry in /sys/class/i2c-dev/i2c-x/ directories.
Instead it is in /sys/class/i2c-dev/i2c-x/device/i2c-y/ directory where x != y in the general case. Which is still fine unless a device has more than one bus, then you don't know which 'y'. And the i2cbusses.c code assumes only one bus.
So can we move, link, or create an additional 'name' entry in each directory /sys/class/i2c-dev/i2c-x ??
Then we know what device it is. Problem solved.
Can this be done in sysfs?
Quoted 47 lines · Jean Delvare
Quoted 8 lines
It's not a sysfs issue, it's an issue with the following line in
i2c_add_adapter():adap->nr = nr++;We need to be smarter about how to number our adapters instead of just picking the next number. Something that matches the logic in
get_free_i2c_dev()ini2c-dev.c.Quoted 5 lines
Or we can modify
i2c-dev.cto use the adapter number in it's device id. But if we do that we will eventually run out of minor numbers if we insert and remove adapters all of the time...Any opinions on which would be better?
Unless solution #1 is technically awful or even impossible, I vote for it, mainly because of the minor runout issue you mention with solution #2, and also because it will bring us back to the policy we had in 2.4.
If I'm not mistaking, this basically requires that we can access the already registered adapters list from
i2c_add_adapter(). Depending on the form in which it is stored, the algorithm to get the lowest available number will be more or less sympathetic. Inget_free_i2c_dev()this is a simple array of pointers, all we need to look for is a NULL value, so the complexity is O(N). I fear it won't be that simple with adapters.If you could tell me how we do access the adapters list, so that I could take a look...
BTW, I found something to be fixed in i2c-core:
drivers/i2c/i2c-core.c renamed from drivers/i2c/i2c-core.c.orig +1
-1@@ -175,7 +175,7 @@ driver = list_entry(item, struct i2c_driver, list);if (driver->detach_adapter)if ((res = driver->detach_adapter (adap))) { -dev_warn (&adap->dev,"can't detach adapter" +dev_warn (&adap->dev,"can't detach adapter " "while detaching driver %s: driver not " "detached!" , driver->name); goto out_unlock;Thanks.
Exactly the same as what's in /sys/class/i2c-dev/i2c-x/device/i2c-y/name file now, for example
SMBus AMD756 adapter at 80e0
Quoted 26 lines · Greg KH
Quoted 5 lines · Jean Delvare
Quoted 2 lines
As we discussed before one problem is that adapter numbers don't match device numbers.
This is the main problem for sure. I guess (hope) that Greg will take a look someday if he has some free time?
Yeah, I'll try to get to that next week...
Quoted 5 lines
Quoted 2 lines
So can we move, link, or create an additional 'name' entry in each directory
/sys/class/i2c-dev/i2c-x??This is the kind of question we want to ask to Greg (which supposes you CC him ;)).
What would that "name" file hold?
thanks,
greg k-h
Greg KH
AppliedQuoted 1 line · Jean Delvare
BTW, I found something to be fixed in i2c-core:
Applied, thanks.
greg k-h
Greg, any luck with this?
Quoted 15 lines · Greg KH
Quoted 5 lines · Mark Studebaker
Exactly the same as what's in
/sys/class/i2c-dev/i2c-x/device/i2c-y/namefile now, for exampleSMBus AMD756 adapter at 80e0
Ah, that would be nice. I'll see if I can get that to work...
thanks,
greg k-h
Quoted 13 lines · Mark Studebaker
Greg, any luck with this?
Quoted 10 lines · Greg KH
Quoted 5 lines · Mark Studebaker
Exactly the same as what's in
/sys/class/i2c-dev/i2c-x/device/i2c-y/namefile now, for exampleSMBus AMD756 adapter at 80e0
Ah, that would be nice. I'll see if I can get that to work...
How about the following patch (only compile tested...)
thanks,
greg k-h
# I2C: show adapter name in i2c-dev class directory to make it easier for userspace tools.
drivers/i2c/i2c-dev.c +8 -0
@@ -124,6 +124,13 @@ } static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL); +static ssize_t show_adapter_name (struct class_device *class_dev,char *buf) +{ +struct i2c_dev *i2c_dev =to_i2c_dev (class_dev); +return sprintf (buf,"%s\n" , i2c_dev->adap->name); +} +static CLASS_DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL); +static ssize_t i2cdev_read (struct file *file,char __user *buf,size_t count,loff_t *offset) {
@@ -459,6 +466,7 @@if (retval) goto error;class_device_create_file (&i2c_dev->class_dev, &class_device_attr_dev); +class_device_create_file (&i2c_dev->class_dev, &class_device_attr_name);return 0 ; error:return_i2c_dev (i2c_dev);
Quoted 4 lines
works great!
I'll fix up our userspace so that if the 'name' file is present we will use that rather than descend into the i2c-adapter directory to get it. thanks
I tested this on my 7-busses system and it works great :) Thanks guys.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
works great!
I'll fix up our userspace so that if the 'name' file is present we will use that rather than descend into the i2c-adapter directory to get it. thanks
mds
Quoted 53 lines · Greg KH
Quoted 15 lines · Mark Studebaker
Greg, any luck with this?
Quoted 11 lines · Greg KH
Quoted 5 lines · Mark Studebaker
Exactly the same as what's in
/sys/class/i2c-dev/i2c-x/device/i2c-y/namefile now, for exampleSMBus AMD756 adapter at 80e0
Ah, that would be nice. I'll see if I can get that to work...
How about the following patch (only compile tested...)
thanks,
greg k-h
# I2C: show adapter name in i2c-dev class directory to make it easier for userspace tools.
diff -Nru
a/drivers/i2c/i2c-dev.cb/drivers/i2c/i2c-dev.cdrivers/i2c/i2c-dev.c +8
-0@@ -124,6 +124,13 @@ } static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL); +static ssize_t show_adapter_name (struct class_device *class_dev,char *buf) +{ +struct i2c_dev *i2c_dev =to_i2c_dev (class_dev); +return sprintf (buf,"%s\n" , i2c_dev->adap->name); +} +static CLASS_DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL); +static ssize_t i2cdev_read (struct file *file,char __user *buf,size_t count,loff_t *offset) {@@ -459,6 +466,7 @@if (retval) goto error;class_device_create_file (&i2c_dev->class_dev, &class_device_attr_dev); +class_device_create_file (&i2c_dev->class_dev, &class_device_attr_name);return 0 ; error:return_i2c_dev (i2c_dev);