fix i2c_msg.len not aligning with i2c_master_send
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Hi, Jean
We found type of i2c_msg.len is __u16, while parameter count in i2c_master_send is int.
The mismatch will truncate count from int to u16.
For example we downloading firmware which is more than 64K (64K+8) via i2c, i2c would transfer u16 (8 bytes) in fact.
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Quoted 8 lines · zhangfei gao
Hi, Jean
We found type of i2c_msg.len is
__u16, while parameter count ini2c_master_sendis int.
The mismatch will truncate count from int to u16.
For example we downloading firmware which is more than 64K (64K+8) via i2c, i2c would transfer u16 (8 bytes) in fact.
I would be wary of changing the i2c_msg structure as this is exported to userspace.
I think a better fix would be to test the count parameter to i2c_master_send and return something like EMSGSIZE to inform the user it is too long.
If you really want i2c transfers of 64K and above, then you might have to add a new struct i2c_msg and update all the callsites to use it. Either that or chain a set of msgs together and pass them to the i2c controller in one go.
Please discuss on the i2c list.
Quoted 42 lines
Quoted 1 line
From
69ec7599bf0fa28441281be1df76a2f573bb9127Mon Sep 17 00:00:00 2001From: Zhangfei Gao <[email protected]>
Date: Fri, 5 Feb 2010 05:12:19 +0800
Subject: [PATCH] i2c: i2c_msg.len modify to__u32typeint i2c_master_send(struct
i2c_client*client,const char *buf ,int
count){ ~ msg.len = count; ~ } Parameter count would truncate from int to __u16Signed-off-by Zhangfei Gao <[email protected]>--- include/linux/i2c.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)include/linux/i2c.h +0
-0@@ -491,7 +491,7 @@ struct i2c_msg {#define I2C_M_IGNORE_NAK 0x1000/* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_NO_RD_ACK 0x0800/* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_RECV_LEN 0x0400/* length will be first received byte */
include/linux/i2c.h +1
-1-__u16 len;/* msg length */ +__u32 len;/* msg length */ __u8 *buf;/* pointer to msg data */ };Signature · 8 lines
-- 1.6.0.4 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel at lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Signature · 5 lines
-- Ben Q: What's a light-year? A: One-third less calories than a regular year.
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Hi, Ben
Thanks for your suggestion.
Quoted 18 lines · Ben Dooks
Quoted 8 lines · zhangfei gao
Hi, Jean
We found type of i2c_msg.len is
__u16, while parameter count ini2c_master_sendis int.
The mismatch will truncate count from int to u16.
For example we downloading firmware which is more than 64K (64K+8) via i2c, i2c would transfer u16 (8 bytes) in fact.I would be wary of changing the
i2c_msgstructure as this is exported to userspace.I think a better fix would be to test the count parameter to
i2c_master_sendand return something like EMSGSIZE to inform the user it is too long.
Quoted 7 lines
If you really want i2c transfers of 64K and above, then you might have to add a new struct
i2c_msgand update all the callsites to use it. Either that or chain a set of msgs together and pass them to the i2c controller in one go.Please discuss on the i2c list.
1. Both i2c_master_send and i2c_msg are exported in include/linux/i2c.h.
2. Both i2c_master_send and i2c_master_recv supposed to support int count, is it suitable to return error when count is bigger than 64K?
3. It may impact a lot if adding new structure :)
include/linux/i2c.h
extern int i2c_master_send(struct i2c_client *client, const char *buf,
int count);
extern int i2c_master_recv(struct i2c_client *client, char *buf, int count);
Thansk
Zhangfei
Quoted 51 lines
Quoted 34 lines
Quoted 1 line
From
69ec7599bf0fa28441281be1df76a2f573bb9127Mon Sep 17 00:00:00 2001From: Zhangfei Gao <[email protected]>
Date: Fri, 5 Feb 2010 05:12:19 +0800
Subject: [PATCH] i2c: i2c_msg.len modify to__u32type? ? int
i2c_master_send(struct i2c_client *client,const char *buf ,int count)
? ? {
? ? ? ? ~
? ? ? ? msg.len = count;
? ? ? ? ~
? ? }
? ? Parameter count would truncate from int to__u16Signed-off-by Zhangfei Gao <[email protected]>---
?include/linux/i2c.h | ? ?2 +-
?1 files changed, 1 insertions(+), 1 deletions(-)include/linux/i2c.h +0
-0@@ -491,7 +491,7 @@ struct i2c_msg {?#define
I2C_M_IGNORE_NAK? ?0x1000 ? ?/* ifI2C_FUNC_PROTOCOL_MANGLING*/ ?#defineI2C_M_NO_RD_ACK? ? ? ?0x0800 ? ?/* ifI2C_FUNC_PROTOCOL_MANGLING*/
?#defineI2C_M_RECV_LEN? ? ? ?0x0400 ? ?/* length will be first received byte */
- ? ?__u16 len; ? ? ? ?/* msg length ? ? ? ? ? ? ? ?*/
+ ? ?__u32 len; ? ? ? ?/* msg length ? ? ? ? ? ? ? ?*/
? ? ?__u8 *buf; ? ? ? ?/* pointer to msg data ? ? ? ? ? ?*/ ?};Quoted 8 lines
--
1.6.0.4_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel--
--
BenQ: ? ? ?What's a light-year?
A: ? ? ?One-third less calories than a regular year.
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
In reply to zhangfei gaoHi Gao,
I'm adding the linux-i2c list to Cc.
Quoted 37 lines · zhangfei gao
We found type of i2c_msg.len is
__u16, while parameter count ini2c_master_sendis int.
The mismatch will truncate count from int to u16.
For example we downloading firmware which is more than 64K (64K+8) via i2c, i2c would transfer u16 (8 bytes) in fact.From
69ec7599bf0fa28441281be1df76a2f573bb9127Mon Sep 17 00:00:00 2001
From: Zhangfei Gao <[email protected]>
Date: Fri, 5 Feb 2010 05:12:19 +0800
Subject: [PATCH] i2c: i2c_msg.len modify to__u32typeint i2c_master_send(struct
i2c_client*client,const char *buf ,int
count){ ~ msg.len = count; ~ } Parameter count would truncate from int to __u16Signed-off-by Zhangfei Gao <[email protected]>--- include/linux/i2c.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)include/linux/i2c.h +0
-0@@ -491,7 +491,7 @@ struct i2c_msg {#define I2C_M_IGNORE_NAK 0x1000/* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_NO_RD_ACK 0x0800/* if I2C_FUNC_PROTOCOL_MANGLING */ #define I2C_M_RECV_LEN 0x0400/* length will be first received byte */
include/linux/i2c.h
Quoted 4 lines
include/linux/i2c.h +1
-1-__u16 len;/* msg length */ +__u32 len;/* msg length */ __u8 *buf;/* pointer to msg data */ };
Unfortunately struct i2c_msg is part of the user-space ABI so you don't get to modify it.
What you can do is add a check on "count" in both i2c_master_send() and i2c_master_recv() so that messages which will not fit are rejected rather than being silently truncated. Alternatively, the count parameters could be turned into u16, so that the limitation is immediately visible. This would be more efficient.
I don't think it makes a lot of sense to transfer more than 64 kB at once over I2C. I don't know at which speed you operate your bus, but even at 400 kHz, it takes 1.5 second to transfer 64 kB. At the regular 100 kHz, it takes no less than 6 seconds. This incurs pretty high latency over the bus in question... Not to mention the system in its entirety if your bus is backed by the never-sleeping i2c-algo-bit or similar.
So if the hardware allows it, you should simply split the firmware in smaller chunks so that it fits into the current interface.
If this is really impossible, then we'd have to introduce a new structure for these long messages. As we can't use these in user-space for compatibility reasons, we would have to translate from one structure type to the other and back for every user-space transaction. Obviously there would be a performance penalty, which is why I'd like to avoid it if possible.
Signature · 1 line
Jean Delvare
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Quoted 74 lines · Jean Delvare
Hi Gao,
I'm adding the linux-i2c list to Cc.
Quoted 32 lines · zhangfei gao
We found type of i2c_msg.len is
__u16, while parameter count ini2c_master_sendis int.
The mismatch will truncate count from int to u16.
For example we downloading firmware which is more than 64K (64K+8) via i2c, i2c would transfer u16 (8 bytes) in fact.From
69ec7599bf0fa28441281be1df76a2f573bb9127Mon Sep 17 00:00:00 2001
From: Zhangfei Gao <[email protected]>
Date: Fri, 5 Feb 2010 05:12:19 +0800
Subject: [PATCH] i2c: i2c_msg.len modify to__u32type? ? int
i2c_master_send(struct i2c_client *client,const char *buf ,int count)
? ? {
? ? ? ? ~
? ? ? ? msg.len = count;
? ? ? ? ~
? ? }
? ? Parameter count would truncate from int to__u16Signed-off-by Zhangfei Gao <[email protected]>---
?include/linux/i2c.h | ? ?2 +-
?1 files changed, 1 insertions(+), 1 deletions(-)include/linux/i2c.h +0
-0@@ -491,7 +491,7 @@ struct i2c_msg {?#define
I2C_M_IGNORE_NAK? ?0x1000 ? ?/* ifI2C_FUNC_PROTOCOL_MANGLING*/Quoted 9 lines
?#define
I2C_M_NO_RD_ACK? ? ? ?0x0800 ? ?/* ifI2C_FUNC_PROTOCOL_MANGLING*/
?#defineI2C_M_RECV_LEN? ? ? ?0x0400 ? ?/* length will be first received byte */
- ? ?__u16 len; ? ? ? ?/* msg length ? ? ? ? ? ? ? ?*/
+ ? ?__u32 len; ? ? ? ?/* msg length ? ? ? ? ? ? ? ?*/
? ? ?__u8 *buf; ? ? ? ?/* pointer to msg data ? ? ? ? ? ?*/ ?};Unfortunately struct
i2c_msgis part of the user-space ABI so you don't get to modify it.What you can do is add a check on "count" in both
i2c_master_send()andi2c_master_recv()so that messages which will not fit are rejected rather than being silently truncated. Alternatively, the count parameters could be turned into u16, so that the limitation is immediately visible. This would be more efficient.I don't think it makes a lot of sense to transfer more than 64 kB at once over I2C. I don't know at which speed you operate your bus, but even at 400 kHz, it takes 1.5 second to transfer 64 kB. At the regular 100 kHz, it takes no less than 6 seconds. This incurs pretty high latency over the bus in question... Not to mention the system in its entirety if your bus is backed by the never-sleeping i2c-algo-bit or similar.
So if the hardware allows it, you should simply split the firmware in smaller chunks so that it fits into the current interface.
If this is really impossible, then we'd have to introduce a new structure for these long messages. As we can't use these in user-space for compatibility reasons, we would have to translate from one structure type to the other and back for every user-space transaction. Obviously there would be a performance penalty, which is why I'd like to avoid it if possible.
Quoted 3 lines
--
Jean Delvare
Hi, Jean
Thanks for your reply.
How about return error in i2c_master_send & i2c_master_recv when count is bigger than 64K, as suggested by Ben.
The device I used could receive 32K one time instead, the firmware download only takes place on-demand in fact.
However, it took some time to debug, since no error info come out. Add error msg may notify users, though transfering more than 64K data one time is rarely happen.
Thanks
Zhangfei
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Quoted 2 lines
How about return error in
i2c_master_send&i2c_master_recvwhen count is bigger than 64K, as suggested by Ben.
I think that's more preferable. Making the count parameter as u16, though is going to generate a warning, yet that's usually ignored by careless programmer, screaming out when this happens might be more useful sometimes.
Quoted 9 lines
The device I used could receive 32K one time instead, the firmware download only takes place on-demand in fact.
However, it took some time to debug, since no error info come out. Add error msg may notify users, though transfering more than 64K data one time is rarely happen.Thanks
Zhangfei
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Quoted 7 lines · Eric Miao
Quoted 2 lines
How about return error in
i2c_master_send&i2c_master_recvwhen count is bigger than 64K, as suggested by Ben.I think that's more preferable. Making the count parameter as u16, though is going to generate a warning, yet that's usually ignored by careless programmer, screaming out when this happens might be more useful sometimes.
Developers ignoring warnings get the pain the deserve.
A check on "count" would come at the price of a small performance hit for every caller, even though in 99% of the cases the check isn't needed.
That being said I don't care too much and will take whatever patch is sent to me.
It would be a good idea to add a note about this limit in Documentation/i2c/writing-clients and/or include/linux/i2c.h.
Quoted 5 lines
Quoted 5 lines
The device I used could receive 32K one time instead, the firmware download only takes place on-demand in fact.
However, it took some time to debug, since no error info come out. Add error msg may notify users, though transfering more than 64K data one time is rarely happen.
Signature · 1 line
Jean Delvare
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Quoted 21 lines · Jean Delvare
Quoted 7 lines · Eric Miao
Quoted 2 lines
How about return error in
i2c_master_send&i2c_master_recvwhen count is bigger than 64K, as suggested by Ben.I think that's more preferable. Making the count parameter as u16, though is going to generate a warning, yet that's usually ignored by careless programmer, screaming out when this happens might be more useful sometimes.
Developers ignoring warnings get the pain the deserve.
A check on "count" would come at the price of a small performance hit for every caller, even though in 99% of the cases the check isn't needed.
That being said I don't care too much and will take whatever patch is sent to me.
It would be a good idea to add a note about this limit in
Documentation/i2c/writing-clientsand/orinclude/linux/i2c.h.
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
The device I used could receive 32K one time instead, the firmware download only takes place on-demand in fact.
However, it took some time to debug, since no error info come out. Add error msg may notify users, though transfering more than 64K data one time is rarely happen.--
Jean Delvare
Hi, Jean
Thanks for your instruction.
Here is patch to modify some comments of i2c_master_send & i2c_master_recv, is this OK.
Thanks
Zhangfei
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
Quoted 6 lines
Hi, Jean
Thanks for your instruction.
Here is patch to modify some comments ofi2c_master_send&i2c_master_recv, is this OK.
Where's the screaming things, dude?
Quoted 71 lines
Thanks
ZhangfeiFrom
30fbf1ebf1facba3d280c887e2ecfd0499e7b04bMon Sep 17 00:00:00 2001
From: Zhangfei Gao <[email protected]>
Date: Sat, 6 Feb 2010 05:38:59 +0800
Subject: [PATCH] i2c: notes ofi2c_master_send&i2c_master_recv? ? ? ?i2c_master_send &
i2c_master_recvnot support more than 64bytes transfer, since msg.len is__u16typeSigned-off-by Zhangfei Gao <[email protected]>---
?Documentation/i2c/writing-clients | ? ?3 ++- ?drivers/i2c/i2c-core.c ? ? ? ? ? ?| ? ?4 ++-- ?include/linux/i2c.h ? ? ? ? ? ? ? | ? ?1 +?3 files changed, 5 insertions(+), 3 deletions(-)
Documentation/i2c/writing-clients +0
-0@@ -318,7 +318,8 @@ Plain I2C communication?These routines read and write some bytes from/to a client. The client ?contains the i2c address, so you do not have to include it. The second ?parameter contains the bytes to read/write, the third the number of bytes
Documentation/i2c/writing-clients +2
-1-to read/write (must be less than the length of the buffer.) Returned is +to read/write (must be less than the length of the buffer, also should be +less than 64K since msg.len is __u16 type.) Returned is?the actual number of bytes read/written.
? ? ? ?int i2c_transfer(struct
i2c_adapter*adap, structi2c_msg*msg,drivers/i2c/i2c-core.c +0
-0@@ -1112,7 +1112,7 @@ EXPORT_SYMBOL(i2c_transfer);?*
i2c_master_send- issue a single I2C message in master transmit mode ?* @client: Handle to slave device
?* @buf: Data that will be written to the slave
- * @count: How many bytes to write
+ * @count: How many bytes to write, should be less than 64K since msg.len is u16
?*
?* Returns negative errno, or else the number of bytes written. ?*/drivers/i2c/i2c-core.c +0
-0@@ -1139,7 +1139,7 @@ EXPORT_SYMBOL(i2c_master_send);?*
i2c_master_recv- issue a single I2C message in master receive mode ?* @client: Handle to slave device
?* @buf: Where to store data read from slave
- * @count: How many bytes to read
+ * @count: How many bytes to read, should be less than 64K since msg.len is u16 ?*
?* Returns negative errno, or else the number of bytes read.
?*/include/linux/i2c.h +0
-0@@ -53,6 +53,7 @@ struct i2c_board_info;?* on a bus (or read from them). Apart from two basic transfer functions to ?* transmit one message at a time, a more complex version can be used to ?* transmit an arbitrary number of messages without interruption.
+ * Parameter count should be less than 64K since msg.len is__u16?*/
?extern inti2c_master_send(struct i2c_client *client, const char *buf, ? ? ? ? ? ? ? ? ? ? ? ? ? int count);
--
1.6.0.4
PATCH 1/1 fix i2c_msg.len not aligning with i2c_master_send
In reply to zhangfei gaoQuoted 47 lines · zhangfei gao
Thanks for your instruction.
Here is patch to modify some comments ofi2c_master_send&i2c_master_recv, is this OK.Thanks
ZhangfeiFrom
30fbf1ebf1facba3d280c887e2ecfd0499e7b04bMon Sep 17 00:00:00 2001
From: Zhangfei Gao <[email protected]>
Date: Sat, 6 Feb 2010 05:38:59 +0800
Subject: [PATCH] i2c: notes ofi2c_master_send&i2c_master_recv
i2c_master_send&i2c_master_recvnot support more than 64bytes
transfer, since msg.len is__u16typeSigned-off-by Zhangfei Gao <[email protected]>--- Documentation/i2c/writing-clients | 3 ++- drivers/i2c/i2c-core.c | 4 ++-- include/linux/i2c.h | 1 + 3 files changed, 5 insertions(+), 3 deletions(-)Documentation/i2c/writing-clients +2
-1@@ -318,7 +318,8 @@ Plain I2C communication These routines read and write some bytes from/to a client. The client contains the i2c address, so you do not have to include it. The second parameter contains the bytes to read/write, the third the number of bytes -to read/write (must be less than the length of the buffer.) Returned is +to read/write (must be less than the length of the buffer, also should be +less than 64K since msg.len is __u16 type.) Returned is the actual number of bytes read/written. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,drivers/i2c/i2c-core.c +1
-1@@ -1112,7 +1112,7 @@ EXPORT_SYMBOL(i2c_transfer);* i2c_master_send - issue a single I2C message in master transmit mode * @client: Handle to slave device * @buf: Data that will be written to the slave -* @count: How many bytes to write +* @count: How many bytes to write, should be less than 64K since
Quoted 1 line
msg.len is u16
Your patch was line-wrapped by your e-mail client, so it doesn't apply. I've fixed it this time, but please make sure it doesn't happen again for future patches you send.
Quoted 17 lines
*
* Returns negative errno, or else the number of bytes written. */drivers/i2c/i2c-core.c +1
-1@@ -1139,7 +1139,7 @@ EXPORT_SYMBOL(i2c_master_send);* i2c_master_recv - issue a single I2C message in master receive mode * @client: Handle to slave device * @buf: Where to store data read from slave -* @count: How many bytes to read +* @count: How many bytes to read, should be less than 64K since msg.len is u16 * * Returns negative errno, or else the number of bytes read. */ include/linux/i2c.h +0
-0
include/linux/i2c.h struct i2c_board_info; · line 59
Quoted 7 lines
include/linux/i2c.h +1
-0@@ -53,6 +53,7 @@ struct i2c_board_info;* on a bus (or read from them). Apart from two basic transfer functions to * transmit one message at a time, a more complex version can be used to * transmit an arbitrary number of messages without interruption. +* Parameter count should be less than 64K since msg.len is __u16 */ extern int i2c_master_send (struct i2c_client *client,const char *buf,int count);
Patch applied with minor reformatting, thanks.
Signature · 1 line
Jean Delvare