Skip to contentSkip to conversation

I2C mux / virtual bus implementation

DISCUSSION Mark Studebaker 2 participants 3 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

part 1

Quoted 149 lines · Ken Harrenstien
Quoted 2 lines · Mark Studebaker

you found me, although I prefer linux mail at [email protected]. would be happy to discuss it with you.

Great!

First, here's the relevant parts of your last message in the thread, to save you some trouble:

=====================
* /To/: Brian Kuschak <bkuschak@xxxxxxxxx <mailto:[email protected]>>
* /Subject/: Re: [PATCH] - i2c virtual adapter driver for multiplexed busses
* /From/: Mark Studebaker <mds@xxxxxxxxxxxx <mailto:[email protected]>>
* /Date/: Fri, 14 Nov 2003 23:30:52 -0500
* /Cc/: sensors@xxxxxxxxxxxxxxxxxxxx <mailto:[email protected]>
* /Message-id/: <[email protected] <msg05086.html>>
* /References/: <[email protected] <msg05067.html>>
* /Sender/: mds@xxxxxxxxxxxxxxxxxxxx <mailto:[email protected]>

------------------------------------------------------------------------

Quoted 9 lines · Brian Kuschak

It's quite possible I muddied the boundaries here. The reason for keeping the main functionality out of i2c-virtual_cb.c is that this file is where the user-specific and platform-specific code resides. The makefiles in the patch don't actually use i2c-virtual_cb.c - it was included more as an example of usage. In my case, at least, the callbacks are where some of our proprietary code resides and I wanted to keep that isolated.

we're in agreement. You did it right, I'm just conceptually mapping what you did onto our naming conventions. i2c-virtual is a generic 'algorithm" and i2c-virtual-cb is one of many possible hardware-specific "adapters" using that algorithm.

Quoted 18 lines
Quoted 8 lines

- I'd rather not add entries to struct
i2c_algorithm, we just went
through that
hell and are finishing a patch to go to Marcelo.
Rather than
xxx_exclusive,
we could use the (essentially unused)
algo_control. i2c-core could

Quoted 9 lines

recognize
that a driver is virtual by adding a functionality
entry to identify
that
it is virtual. If you really want it the way it is
we need to put
#ifdefs everywhere
so we can stay compatible, at least for now...

Quoted 8 lines

Point well taken. This sounds like a reasonable change.

One comment is that the bus exclusivity and multiplexing are two separate things. Even a regular non-multiplexed bus may require 'arbitration' with other blades before being allowed to use it. Likewise some multiplexed busses may not require arbitration.

ok. if they are two separate things let's keep them separate...

Quoted 22 lines
Quoted 7 lines

- If i2c-pca954x also registered as a chip driver
(sensors-style)
(that is, be an i2c client as well) at addresses
0x70-77 then it will
get called whenever a device is found at that
location, it can do
detection

Quoted 9 lines

(if possible -doesn't look like it) and then
register the new busses,
and
everything gets bootstrapped. You'll get called
anytime a bus
is registered. That's better than simply scanning
the
adapters already present at module_init time.

Yes, I originally thought about doing exactly that. In our application due to the variety of different HW platforms it made more sense to register these busses manually based on a priori knowledge of the HW.

Quoted 4 lines

I'll try to put together a little pcf954x client driver to do the runtime registration of the virtual busses, since that will likely be more useful for the larger audience.

do that and you don't need the bus scan in i2c-virtual I don't think (i2c-core scans for you).
also you can greatly simplify i2c-virtual-cb by using the i2c_smbus_write_byte function from i2c-core rather than calling master_xfer directly.

=====================

OK....

What I've done so far is to create "pca954x.c" as a sensors-style chip driver supporting all variants of the PCA954x and keeping a cleaned-up version of Brian's i2c-virtual.c, thus leveraging all the existing code exactly as you suggested. It's pretty neat, too, and allows multiple levels of virtual busses.

However, I suddenly realized that this won't work. The code will deadlock because the pca954x client is trying to add new "virtual bus" adapters while the core_lists are already locked. D'oh!

In more detail, the typical scenario is that pca954x.o gets loaded, and its _init function promptly calls i2c_add_driver(), which locks core_lists. Much farther down the line (via driver->attach_adapter to pca954x_attach_adapter() to i2detect() to pca954x_detect()), the mux code tries to create the virtual adapter and calls i2c_add_adapter(), where it locks up since core_lists is locked. It actually is safe since it's all part of the same thread of control, but the code currently has no way to know that.

I'm not quite sure of the best way to resolve this and still conform to the design principles of this package.

Add something to bypass the lock? (Must work for deletes too.) Add a drivers/i2c/i2c-pca954x.c adapter driver to the mess instead? Or....?

I'm hoping that your vastly greater experience with the i2c/lm_sensors infrastructure will allow you to see the most elegant solution -- in particular, one that you'd be willing to integrate into the package, once I get it working and send in the files/patch.

Thanks!
--Ken

#1 lore raw
Quoted 5 lines

Third, you may not have realized that Khali recently did a (2.4 kernel) pca9540 driver. It is in our sensors package. It however doesn't do any of the bootstrapping or bus registering either. It may or may not help you.

It's a really simple driver meant to give manual control over the PCA9540. I wrote it as a temporary solution for someone who had a board with such a chip but that someone vanished so I don't think the driver was ever tested. At any rate, the driver is definitely unsafe. Using it to change the i2c channel while the bus happens not to be idle will certainly result in, huh well, something bad, to say the least. I don't think the driver will be of any help, or at least it won't help more that Philips' datasheet for that chip.

Jean "Khali" Delvare

#2 lore raw
In reply to Mark Studebaker

(copying the mailing list, already forwarded the first mail to the list)

First I want to ask whether you are working on kernel 2.4 or 2.6.

Second, it sounds like you are on the right track, although I'm not the best person to give advice on locking... which is why I'm copying the list.

Third, you may not have realized that Khali recently did a (2.4 kernel) pca9540 driver. It is in our sensors package. It however doesn't do any of the bootstrapping or bus registering either. It may or may not help you.

mds

Quoted 55 lines · Ken Harrenstien

Following up on my own msg:

Quoted 22 lines · Ken Harrenstien

What I've done so far is to create "pca954x.c" as a sensors-style chip driver supporting all variants of the PCA954x and keeping a cleaned-up version of Brian's i2c-virtual.c, thus leveraging all the existing code exactly as you suggested. It's pretty neat, too, and allows multiple levels of virtual busses.

However, I suddenly realized that this won't work. The code will deadlock because the pca954x client is trying to add new "virtual bus" adapters while the core_lists are already locked. D'oh!

In more detail, the typical scenario is that pca954x.o gets loaded, and its _init function promptly calls i2c_add_driver(), which locks core_lists. Much farther down the line (via driver->attach_adapter to pca954x_attach_adapter() to i2detect() to pca954x_detect()), the mux code tries to create the virtual adapter and calls i2c_add_adapter(), where it locks up since core_lists is locked. It actually is safe since it's all part of the same thread of control, but the code currently has no way to know that.

I'm not quite sure of the best way to resolve this and still conform to the design principles of this package.

Quoted 7 lines

Add something to bypass the lock? (Must work for deletes too.) Add a drivers/i2c/i2c-pca954x.c adapter driver to the mess instead? Or....?

I'm hoping that your vastly greater experience with the i2c/lm_sensors infrastructure will allow you to see the most elegant solution -- in particular, one that you'd be willing to integrate into the package, once I get it working and send in the files/patch.

Now that I've had some actual quiet time to let my subconscious chew on this (and grovel painstakingly through all code paths that touch core_lists), I think I have a reasonable solution for bypassing the
lock: modify i2c-core.c to move the locked parts of i2c_add_adapter() and i2c_del_adapter() out into unlocked auxiliary functions, just like i2c_check_addr() calls its auxiliary __i2c_check_addr(). Then the virtual bus code can call those auxiliaries directly when adding/deleting virtual adapters. That sound reasonable?

It would be better, though, to use some naming convention for these auxiliaries other than prefixing "__", since such symbols are supposed to be reserved for the C compiler and its supporting infrastructure. How about the clumsy but brutally clear suffix "_unlocked"?

Unless I hear otherwise, will proceed as above and let you know how it goes (probably by Friday, since I'm going to be out Thursday).

--Ken