* i2c_smbus_write_* will never work
@ 2005-05-19 6:25 Kögler Peter
2005-05-19 6:25 ` Kögler Peter
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Kögler Peter @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
Hello,
i found a problem while using the driver at91_i2c.c from the arm linux
project: It is never possible to do a write operation.
The following is a description of the error:
If you look at i2c_smbus_xfer_emulated() in i2c-core.c a message array
msg[2] with 2 messages is initialized. In the second message (msg[1]) the
flags member is always set to I2C_M_RD.
So if you call any of the following methods
-i2c_smbus_read_byte
-i2c_smbus_write_byte
-i2c_smbus_read_byte_data
-i2c_smbus_write_byte_data
the flag is set to I2C_M_RD.
In the driver at91_i2c.c the bit MREAD of the TWI Master Mode Register is
always set according to the flags member of the second message and this
member is always set to I2C_M_RD. MREAD is responsible for read/write, so it
is never possible to do a write operation.
This is the answer of the Atmel people who maintain the i2c driver for the
AT91RM9200:
Hi Peter.
Andrew forwarded me your e-mail concerning the
possible change of at91_i2c.c you proposed to
correct the explained I2C_M_RD bit which get set
incorrectly in the I2C emulating function
(i2c_smbus_xfer_emulated).
I have looked at the code, and saw that your founding
to be true ( well at least for the one I'm currently
working on now which is linux-2.4.25-vrs2 ),
that msg[1].flag is always READ. This however,
as far as I can tell, is incorrect.
What I do suggested to Andrew (and now to you)
is not to change the at91_i2c.c module to work around
the problem, but rather eliminate the source of the
problem, thus rather fix the i2c_smbus_xfer_emulated
function in i2c-core.c.
So my recommendation would be to fix-up the emulation
function by ensuring that msg[1].flags = msg[0].flags.
This *should* fix the problem.
By doing it this way, one also ensures that the at91_i2c
driver stays generic and compatible between devices on
the shared bus.
So I suggest you contact the i2c maintainer/developers,
and submitting a patch to the emulation function.
This is now an i2c issue, and not an arm specific one.
Hope this helps.
Kind regards,
Pieter Loubser
wbr
Peter
--------------------------------------------------------------
Peter K?gler
EL-ME AG
Gewerbering 1
D-84072 Au i. d. Hallertau
Tel.: +49 / 8752 / 864 - 527
Fax.: +49 / 8752 / 864 - 100
mailto.: p.koegler@el-me.de
Web: www.el-me.de
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
2005-05-19 6:25 ` Kögler Peter
2005-05-19 6:25 ` Jean Delvare
@ 2005-05-19 6:25 ` Mark Studebaker
2005-05-19 6:25 ` Jean Delvare
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Mark Studebaker @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
seems hard to believe but I think you're right.
Hopefully somebody on this list can verify
(you need a chip on an emulated bus to test, preferably something besides an eeprom you don't want to corrupt...
Don't think I have anything I could test on.)
i2cset would be a good tool to test with.
It's looks to have been broken for years.
If I don't hear from anybody I'll get brave and test it on my
DDC monitor eeprom on my voodoo-3 card.
The fix is not to set msg[1].flags = msg[0].flags, since the
first message is almost always a write (to set the address),
even when the whole transaction type is a read.
The correct fix is something like...
if(read_write = I2C_SMBUS_WRITE)
msg[1].flags &= ~I2C_M_RD;
which could be placed just before the i2c_transfer line in
i2c_smbus_xfer_emulated() in i2c-core.c.
Can you verify that your fix (or my fix) works?
thanks
mds
K?gler Peter wrote:
> Hello,
> i found a problem while using the driver at91_i2c.c from the arm linux
> project: It is never possible to do a write operation.
>
> The following is a description of the error:
> If you look at i2c_smbus_xfer_emulated() in i2c-core.c a message array
> msg[2] with 2 messages is initialized. In the second message (msg[1]) the
> flags member is always set to I2C_M_RD.
>
> So if you call any of the following methods
> -i2c_smbus_read_byte
> -i2c_smbus_write_byte
> -i2c_smbus_read_byte_data
> -i2c_smbus_write_byte_data
>
> the flag is set to I2C_M_RD.
>
> In the driver at91_i2c.c the bit MREAD of the TWI Master Mode Register is
> always set according to the flags member of the second message and this
> member is always set to I2C_M_RD. MREAD is responsible for read/write, so it
> is never possible to do a write operation.
>
> This is the answer of the Atmel people who maintain the i2c driver for the
> AT91RM9200:
>
> Hi Peter.
> Andrew forwarded me your e-mail concerning the
> possible change of at91_i2c.c you proposed to
> correct the explained I2C_M_RD bit which get set
> incorrectly in the I2C emulating function
> (i2c_smbus_xfer_emulated).
>
> I have looked at the code, and saw that your founding
> to be true ( well at least for the one I'm currently
> working on now which is linux-2.4.25-vrs2 ),
> that msg[1].flag is always READ. This however,
> as far as I can tell, is incorrect.
>
> What I do suggested to Andrew (and now to you)
> is not to change the at91_i2c.c module to work around
> the problem, but rather eliminate the source of the
> problem, thus rather fix the i2c_smbus_xfer_emulated
> function in i2c-core.c.
>
> So my recommendation would be to fix-up the emulation
> function by ensuring that msg[1].flags = msg[0].flags.
> This *should* fix the problem.
>
> By doing it this way, one also ensures that the at91_i2c
> driver stays generic and compatible between devices on
> the shared bus.
> So I suggest you contact the i2c maintainer/developers,
> and submitting a patch to the emulation function.
> This is now an i2c issue, and not an arm specific one.
>
>
> Hope this helps.
>
> Kind regards,
> Pieter Loubser
>
> wbr
> Peter
>
> --------------------------------------------------------------
> Peter K?gler
> EL-ME AG
> Gewerbering 1
> D-84072 Au i. d. Hallertau
>
> Tel.: +49 / 8752 / 864 - 527
> Fax.: +49 / 8752 / 864 - 100
> mailto.: p.koegler@el-me.de
> Web: www.el-me.de
>
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
` (3 preceding siblings ...)
2005-05-19 6:25 ` Jean Delvare
@ 2005-05-19 6:25 ` Jean Delvare
2005-05-19 6:25 ` Kögler Peter
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jean Delvare @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
> seems hard to believe but I think you're right.
> Hopefully somebody on this list can verify
> (you need a chip on an emulated bus to test, preferably something
> besides an eeprom you don't want to corrupt... Don't think I have
> anything I could test on.) i2cset would be a good tool to test with.
> It's looks to have been broken for years.
> If I don't hear from anybody I'll get brave and test it on my
> DDC monitor eeprom on my voodoo-3 card.
I'm using evaluation boards with various Analog Devices chips over
parallel port. The bus driver being based on i2c-algo-bit, I assume that
it does rely on i2c_smbus_xfer_emulated? And it happens that everything
always worked OK, including writes, so how could there be such an
important bug in there?
--
Jean Delvare
http://khali.linux-fr.org/
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
` (5 preceding siblings ...)
2005-05-19 6:25 ` Kögler Peter
@ 2005-05-19 6:25 ` Mark Studebaker
2005-05-19 6:25 ` Mark Studebaker
7 siblings, 0 replies; 9+ messages in thread
From: Mark Studebaker @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
Correction, i2cset can't be used to reproduce the problem because it uses i2c-dev.h, not
the functions in i2c-core.c.
However using admxxxx drivers on a driver with i2c-algo-bit should show the problem if it exists.
Jean Delvare wrote:
>>seems hard to believe but I think you're right.
>>Hopefully somebody on this list can verify
>>(you need a chip on an emulated bus to test, preferably something
>>besides an eeprom you don't want to corrupt... Don't think I have
>>anything I could test on.) i2cset would be a good tool to test with.
>>It's looks to have been broken for years.
>>If I don't hear from anybody I'll get brave and test it on my
>>DDC monitor eeprom on my voodoo-3 card.
>
>
> I'm using evaluation boards with various Analog Devices chips over
> parallel port. The bus driver being based on i2c-algo-bit, I assume that
> it does rely on i2c_smbus_xfer_emulated? And it happens that everything
> always worked OK, including writes, so how could there be such an
> important bug in there?
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
` (2 preceding siblings ...)
2005-05-19 6:25 ` Mark Studebaker
@ 2005-05-19 6:25 ` Jean Delvare
2005-05-19 6:25 ` Jean Delvare
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jean Delvare @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
>Correction, i2cset can't be used to reproduce the problem because it uses
>i2c-dev.h, not the functions in i2c-core.c.
How does it matter? i2c-dev is just the access point, it has no real
(I2C/SMBus-related) code, has it? So I guess that the commands will end
up in i2c-core, whether they come from a chip driver or i2c-dev. After
all, i2c-dev is nothing but a universal, user-space controllable chip
driver.
>However using admxxxx drivers on a driver with i2c-algo-bit should show the
>problem if it exists.
I did that several times and can confirm it always worked fine. So,
whatever the exact problem is, it's more complex than just "emulated
smbus writes don't work".
Jean Delvare
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
` (4 preceding siblings ...)
2005-05-19 6:25 ` Jean Delvare
@ 2005-05-19 6:25 ` Kögler Peter
2005-05-19 6:25 ` Mark Studebaker
2005-05-19 6:25 ` Mark Studebaker
7 siblings, 0 replies; 9+ messages in thread
From: Kögler Peter @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
hi jean,
can you confirm that i2c_smbus_xfer_emulated is called and not smbus_xfer
when you communicate (see i2c_smbus_xfer) to the chip.
hi mark,
until now i didn't have the time to test your fix. i will do it the next
days.
wbr
peter
--------------------------------------------------------------
Peter K?gler
EL-ME AG
Gewerbering 1
D-84072 Au i. d. Hallertau
Tel.: +49 / 8752 / 864 - 527
Fax.: +49 / 8752 / 864 - 100
mailto.: p.koegler@el-me.de
Web: www.el-me.de
-----Original Message-----
From: Jean Delvare [mailto:khali@linux-fr.org]
Sent: Monday, July 12, 2004 11:42 AM
To: sensors@Stimpy.netroedge.com
Cc: K?gler Peter
Subject: Re: i2c_smbus_write_* will never work
>Correction, i2cset can't be used to reproduce the problem because it uses
>i2c-dev.h, not the functions in i2c-core.c.
How does it matter? i2c-dev is just the access point, it has no real
(I2C/SMBus-related) code, has it? So I guess that the commands will end
up in i2c-core, whether they come from a chip driver or i2c-dev. After
all, i2c-dev is nothing but a universal, user-space controllable chip
driver.
>However using admxxxx drivers on a driver with i2c-algo-bit should show the
>problem if it exists.
I did that several times and can confirm it always worked fine. So,
whatever the exact problem is, it's more complex than just "emulated
smbus writes don't work".
Jean Delvare
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
2005-05-19 6:25 ` Kögler Peter
@ 2005-05-19 6:25 ` Jean Delvare
2005-05-19 6:25 ` Mark Studebaker
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Jean Delvare @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
Hi Peter, all,
>can you confirm that i2c_smbus_xfer_emulated is called and not smbus_xfer
>when you communicate (see i2c_smbus_xfer) to the chip.
To be honest, I didn't know much about all the internals of i2c-core,
i2c_smbux_xfer, i2c_smbus_xfer_emulated etc... so I just took a deeper
look at how things work. Things *look* rather clear to me now and I'm
quite confident that there is no bug in i2c_smbus_xfer_emulated.
However, remember that I am not the authority when it comes to the i2c
subsystem internals, and only really read that code carefully for the
first time, well, 10 minutes ago ;) but since I wrote a few chip drivers
and read quite a handful datasheets dealing with I2C, SMBus and the
like, I think that my understanding of the problem is correct.
First, to answer your question: yes, i2c_smbus_xfer_emulated was used in
my case. The i2c-algo-bit algorithm does define master_xfer but doesn't
define master_smbus. If you look at i2c_smbus_xfer (in i2c-core), which
is the real dispatcher function, you'll find this:
if (adap->algo->smbus_xfer) {
down(&adap->bus);
res = adap->algo->smbus_xfer(adap,addr,flags,read_write,
command,size,data);
up(&adap->bus);
} else
res = i2c_smbus_xfer_emulated(adap,addr,flags,read_write,
command,size,data);
So i2c_smbus_xfer_emulated has to be called when smbus_xfer isn't
defined, which is the case for me.
Now, looking at the i2c_smbus_xfer_emulated function itself, you'll see
that it defines two i2c messages:
unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+2];
unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
int num = read_write = I2C_SMBUS_READ?2:1;
struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
{ addr, flags | I2C_M_RD, 0, msgbuf1 }
};
But you should also notice that both are not necessarily used. More
precisely, the number of messages used is defined by "num". For
"read" commands, two messages are used. For "write" commands, only
one.
The reason is pretty obvious if you know the SMBus protocol. When you
write to a chip, you send a single write message. But when you read from
a chip, you must first "write" a few control bytes to the chip (such
as the register you want to read from; here, "write" is to be
understood as "bytes are emitted by the master to the attention of a
slave chip", not necessarily something that will be written to a
register in the chip), and only then read the answer of the chip
(separated by a repeated-start if I remember correctly). So for read
commands you have a "write" message first and a "read" message then.
Two messages total.
This means that the first (and only, in the case of a write command)
message is always a "write" one, and the second, when used (i.e. in
the case of a read command), is always a "read" one. Thus the way
flags are initialized for msg. Note that the flags parameter as passed
to the function are not allowed to have the I2C_M_RD bit set (any flags
but I2C_M_TEN and I2C_CLIENT_PEC are filtered out by i2c_smbus_xfer), so
we are not "forcing" the second message to be a "read" one. We are
really setting both messages mode (although it happens to be invisible
for write mode, because 1 is read and 0 is write).
So all in all the code looks fine to me.
On the other hand, quoting your original post:
> In the driver at91_i2c.c the bit MREAD of the TWI Master Mode Register is
> always set according to the flags member of the second message and this
> member is always set to I2C_M_RD. MREAD is responsible for read/write, so
> it is never possible to do a write operation.
This doesn't sound good at all. Setting the MREAD bit from the second
message's flags is not correct. For one thing, in the case of a write,
this second message isn't even supposed to be there (num=1!). For
another, as I just explained, the bit is always set to 1 (read) because,
if the second message is used, it is always for a read message.
I don't know why you do need to set the MREAD bit in your at91_i2c
driver. It sounds a bit strange since if you're going to handle "read
commands" and "write commands" (as opposed to just messages) then you
place yourself at the SMBus level and then the driver should not rely on
our emulation code. If for some reason you really have to do that, a
quick fix would be to use the message count (num) to detect the mode.
num=1 means we are writing, num=2 means we are reading. It looks a big
ugly and I suspect that a cleaner fix must exist, but only someone used
to at91_i2c code can deal with this (so not me). At any rate, the
problem you reported isn't due to a bug in our code, but to at91_i2c
misusing it (IMVHO).
I'd appreciate if Mark (or anyone with more experience about this than I
have myself, Phil maybe?) could confirm my analysis.
Thanks,
Jean Delvare
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
@ 2005-05-19 6:25 ` Kögler Peter
2005-05-19 6:25 ` Jean Delvare
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Kögler Peter @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
hi jean,
your explanation is very detailed. thank you very much.
i posted this "bug" in reaction of a mail of pieter@sanpeople.com. he is
responsible for at91_i2c.c, the arm AT91RM9200 part of the I2C. so i gona
forward your mail to him and see what he thinks.
wbr
peter
--------------------------------------------------------------
Peter K?gler
EL-ME AG
Gewerbering 1
D-84072 Au i. d. Hallertau
Tel.: +49 / 8752 / 864 - 527
Fax.: +49 / 8752 / 864 - 100
mailto.: p.koegler@el-me.de
Web: www.el-me.de
-----Original Message-----
From: Jean Delvare [mailto:khali@linux-fr.org]
Sent: Monday, July 12, 2004 1:43 PM
To: p.koegler@el-me.de
Cc: sensors@Stimpy.netroedge.com
Subject: RE: i2c_smbus_write_* will never work
Hi Peter, all,
>can you confirm that i2c_smbus_xfer_emulated is called and not smbus_xfer
>when you communicate (see i2c_smbus_xfer) to the chip.
To be honest, I didn't know much about all the internals of i2c-core,
i2c_smbux_xfer, i2c_smbus_xfer_emulated etc... so I just took a deeper
look at how things work. Things *look* rather clear to me now and I'm
quite confident that there is no bug in i2c_smbus_xfer_emulated.
However, remember that I am not the authority when it comes to the i2c
subsystem internals, and only really read that code carefully for the
first time, well, 10 minutes ago ;) but since I wrote a few chip drivers
and read quite a handful datasheets dealing with I2C, SMBus and the
like, I think that my understanding of the problem is correct.
First, to answer your question: yes, i2c_smbus_xfer_emulated was used in
my case. The i2c-algo-bit algorithm does define master_xfer but doesn't
define master_smbus. If you look at i2c_smbus_xfer (in i2c-core), which
is the real dispatcher function, you'll find this:
if (adap->algo->smbus_xfer) {
down(&adap->bus);
res = adap->algo->smbus_xfer(adap,addr,flags,read_write,
command,size,data);
up(&adap->bus);
} else
res = i2c_smbus_xfer_emulated(adap,addr,flags,read_write,
command,size,data);
So i2c_smbus_xfer_emulated has to be called when smbus_xfer isn't
defined, which is the case for me.
Now, looking at the i2c_smbus_xfer_emulated function itself, you'll see
that it defines two i2c messages:
unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+2];
unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
int num = read_write = I2C_SMBUS_READ?2:1;
struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
{ addr, flags | I2C_M_RD, 0, msgbuf1 }
};
But you should also notice that both are not necessarily used. More
precisely, the number of messages used is defined by "num". For
"read" commands, two messages are used. For "write" commands, only
one.
The reason is pretty obvious if you know the SMBus protocol. When you
write to a chip, you send a single write message. But when you read from
a chip, you must first "write" a few control bytes to the chip (such
as the register you want to read from; here, "write" is to be
understood as "bytes are emitted by the master to the attention of a
slave chip", not necessarily something that will be written to a
register in the chip), and only then read the answer of the chip
(separated by a repeated-start if I remember correctly). So for read
commands you have a "write" message first and a "read" message then.
Two messages total.
This means that the first (and only, in the case of a write command)
message is always a "write" one, and the second, when used (i.e. in
the case of a read command), is always a "read" one. Thus the way
flags are initialized for msg. Note that the flags parameter as passed
to the function are not allowed to have the I2C_M_RD bit set (any flags
but I2C_M_TEN and I2C_CLIENT_PEC are filtered out by i2c_smbus_xfer), so
we are not "forcing" the second message to be a "read" one. We are
really setting both messages mode (although it happens to be invisible
for write mode, because 1 is read and 0 is write).
So all in all the code looks fine to me.
On the other hand, quoting your original post:
> In the driver at91_i2c.c the bit MREAD of the TWI Master Mode Register is
> always set according to the flags member of the second message and this
> member is always set to I2C_M_RD. MREAD is responsible for read/write, so
> it is never possible to do a write operation.
This doesn't sound good at all. Setting the MREAD bit from the second
message's flags is not correct. For one thing, in the case of a write,
this second message isn't even supposed to be there (num=1!). For
another, as I just explained, the bit is always set to 1 (read) because,
if the second message is used, it is always for a read message.
I don't know why you do need to set the MREAD bit in your at91_i2c
driver. It sounds a bit strange since if you're going to handle "read
commands" and "write commands" (as opposed to just messages) then you
place yourself at the SMBus level and then the driver should not rely on
our emulation code. If for some reason you really have to do that, a
quick fix would be to use the message count (num) to detect the mode.
num=1 means we are writing, num=2 means we are reading. It looks a big
ugly and I suspect that a cleaner fix must exist, but only someone used
to at91_i2c code can deal with this (so not me). At any rate, the
problem you reported isn't due to a bug in our code, but to at91_i2c
misusing it (IMVHO).
I'd appreciate if Mark (or anyone with more experience about this than I
have myself, Phil maybe?) could confirm my analysis.
Thanks,
Jean Delvare
^ permalink raw reply [flat|nested] 9+ messages in thread
* i2c_smbus_write_* will never work
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
` (6 preceding siblings ...)
2005-05-19 6:25 ` Mark Studebaker
@ 2005-05-19 6:25 ` Mark Studebaker
7 siblings, 0 replies; 9+ messages in thread
From: Mark Studebaker @ 2005-05-19 6:25 UTC (permalink / raw)
To: lm-sensors
Khali, you are right of course.
I *knew* I was missing something - that num = 1 for writes,
so the second message is not used.
Also, you are right that i2c-dev.h uses the same code path.
Peter, my "fix" is incorrect and is not necessary.
Khali has tested that our code is correct with i2c-algo-bit adapters.
It sounds like at91_i2c.c is severely broken.
It should treat each message independently, i.e. it should have a loop
for (i = 0; i < num; i++) {
process the message...
}
If this is the code in this message here:
http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-July/023172.html
then it does look broken indeed.
See our i2c-algo-bit.c bit_xfer() for the correct method.
Sorry I was so slow to figure out it wasn't our problem...
(thanks Khali)
Please try again with Atmel.
mds
Jean Delvare wrote:
> Hi Peter, all,
>
>
>>can you confirm that i2c_smbus_xfer_emulated is called and not smbus_xfer
>>when you communicate (see i2c_smbus_xfer) to the chip.
>
>
> To be honest, I didn't know much about all the internals of i2c-core,
> i2c_smbux_xfer, i2c_smbus_xfer_emulated etc... so I just took a deeper
> look at how things work. Things *look* rather clear to me now and I'm
> quite confident that there is no bug in i2c_smbus_xfer_emulated.
> However, remember that I am not the authority when it comes to the i2c
> subsystem internals, and only really read that code carefully for the
> first time, well, 10 minutes ago ;) but since I wrote a few chip drivers
> and read quite a handful datasheets dealing with I2C, SMBus and the
> like, I think that my understanding of the problem is correct.
>
> First, to answer your question: yes, i2c_smbus_xfer_emulated was used in
> my case. The i2c-algo-bit algorithm does define master_xfer but doesn't
> define master_smbus. If you look at i2c_smbus_xfer (in i2c-core), which
> is the real dispatcher function, you'll find this:
>
> if (adap->algo->smbus_xfer) {
> down(&adap->bus);
> res = adap->algo->smbus_xfer(adap,addr,flags,read_write,
> command,size,data);
> up(&adap->bus);
> } else
> res = i2c_smbus_xfer_emulated(adap,addr,flags,read_write,
> command,size,data);
>
> So i2c_smbus_xfer_emulated has to be called when smbus_xfer isn't
> defined, which is the case for me.
>
> Now, looking at the i2c_smbus_xfer_emulated function itself, you'll see
> that it defines two i2c messages:
>
> unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+2];
> unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
> int num = read_write = I2C_SMBUS_READ?2:1;
> struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
> { addr, flags | I2C_M_RD, 0, msgbuf1 }
> };
>
> But you should also notice that both are not necessarily used. More
> precisely, the number of messages used is defined by "num". For
> "read" commands, two messages are used. For "write" commands, only
> one.
>
> The reason is pretty obvious if you know the SMBus protocol. When you
> write to a chip, you send a single write message. But when you read from
> a chip, you must first "write" a few control bytes to the chip (such
> as the register you want to read from; here, "write" is to be
> understood as "bytes are emitted by the master to the attention of a
> slave chip", not necessarily something that will be written to a
> register in the chip), and only then read the answer of the chip
> (separated by a repeated-start if I remember correctly). So for read
> commands you have a "write" message first and a "read" message then.
> Two messages total.
>
> This means that the first (and only, in the case of a write command)
> message is always a "write" one, and the second, when used (i.e. in
> the case of a read command), is always a "read" one. Thus the way
> flags are initialized for msg. Note that the flags parameter as passed
> to the function are not allowed to have the I2C_M_RD bit set (any flags
> but I2C_M_TEN and I2C_CLIENT_PEC are filtered out by i2c_smbus_xfer), so
> we are not "forcing" the second message to be a "read" one. We are
> really setting both messages mode (although it happens to be invisible
> for write mode, because 1 is read and 0 is write).
>
> So all in all the code looks fine to me.
>
> On the other hand, quoting your original post:
>
>
>>In the driver at91_i2c.c the bit MREAD of the TWI Master Mode Register is
>>always set according to the flags member of the second message and this
>>member is always set to I2C_M_RD. MREAD is responsible for read/write, so
>>it is never possible to do a write operation.
>
>
> This doesn't sound good at all. Setting the MREAD bit from the second
> message's flags is not correct. For one thing, in the case of a write,
> this second message isn't even supposed to be there (num=1!). For
> another, as I just explained, the bit is always set to 1 (read) because,
> if the second message is used, it is always for a read message.
>
> I don't know why you do need to set the MREAD bit in your at91_i2c
> driver. It sounds a bit strange since if you're going to handle "read
> commands" and "write commands" (as opposed to just messages) then you
> place yourself at the SMBus level and then the driver should not rely on
> our emulation code. If for some reason you really have to do that, a
> quick fix would be to use the message count (num) to detect the mode.
> num=1 means we are writing, num=2 means we are reading. It looks a big
> ugly and I suspect that a cleaner fix must exist, but only someone used
> to at91_i2c code can deal with this (so not me). At any rate, the
> problem you reported isn't due to a bug in our code, but to at91_i2c
> misusing it (IMVHO).
>
> I'd appreciate if Mark (or anyone with more experience about this than I
> have myself, Phil maybe?) could confirm my analysis.
>
> Thanks,
> Jean Delvare
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2005-05-19 6:25 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-19 6:25 i2c_smbus_write_* will never work Kögler Peter
2005-05-19 6:25 ` Kögler Peter
2005-05-19 6:25 ` Jean Delvare
2005-05-19 6:25 ` Mark Studebaker
2005-05-19 6:25 ` Jean Delvare
2005-05-19 6:25 ` Jean Delvare
2005-05-19 6:25 ` Kögler Peter
2005-05-19 6:25 ` Mark Studebaker
2005-05-19 6:25 ` Mark Studebaker
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.