[i2c] [PATCH v2] i2c: Fix the i2c_smbus_read_i2c_block_data() prototype
Jean Delvare
khali at linux-fr.org
Tue May 15 13:57:42 CEST 2007
Hi Mark,
On Sun, 13 May 2007 18:46:28 -0400, Mark M. Hoffman wrote:
> Hi Jean, et. al.:
>
> * Jean Delvare <khali at linux-fr.org> [2007-05-11 20:31:03 +0200]:
> > [Grant, can you please try using this patch and confirm that it's what
> > you need for your Dallas DS1682 driver?]
> >
> > Let the drivers specify how many bytes they want to read with
> > i2c_smbus_read_i2c_block_data(). So far, the block count was
> > hard-coded to I2C_SMBUS_BLOCK_MAX (32), which did not make much sense.
> > Many driver authors complained about this before, and I believe it's
> > about time to fix it. Right now, authors have to do technically stupid
> > things, such as individual byte reads or full-fledged I2C messaging,
> > to work around the problem. We do not want to encourage that.
> >
> > I even found that some bus drivers (e.g. i2c-amd8111) already
> > implemented I2C block read the "right" way, that is, they didn't
> > follow the old, broken standard. The fact that it was never noticed
> > before just shows how little i2c_smbus_read_i2c_block_data() was used,
> > which isn't that surprising given how broken its prototype was so far.
> >
> > There are some obvious compatiblity considerations:
> > * This changes the i2c_smbus_read_i2c_block_data() prototype. Users
> > outside the kernel tree will notice at compilation time, and will
> > have to update their code.
> > * User-space has access to i2c_smbus_xfer() directly using i2c-dev, so
> > the change affects tools such as i2cdump. This isn't necessarily a
> > bad thing, as the broken i2c_smbus_read_i2c_block_data() prototype
> > was also hindering user-space applications, so we should update
> > i2c-dev.h in the same way. I've added a size check in i2c-dev to
> > catch and reject I2C block reads with invalid sizes. This seems to
> > be the best way to have people notice the change and update their
> > user-space code. Worst thing that can happen is a short read
> > (i2c_smbus_read_i2c_block_data() returning less bytes than the
> > caller expected), but actually I doubt any other application was
> > using it anyway.
>
> (summary of May 12, 2007 IRC conversation between Jean and myself)
>
> This patch very subtly breaks the existing userspace interface (through the
> i2c-dev ioctl), and I objected to that. Rather than modify the behavior of
> ioctl cmd #6, I counter-proposed to add a new ioctl cmd #8, with the desired
> behavior (additional len argument). The in-kernel interface change is OK,
> of course.
>
> Jean has asked me to prepare a new patch; so if there's no objection to that
> plan, I'll send one shortly.
As Andrew Morton picked another patch (Grant's DS1682 driver) which
relies on this one, and now complains about build breakage, we have to
hurry up a bit, so I fixed my patch myself. Here is the updated
version, which (hopefully) implements what you had in mind. Can you
please review it? The interesting bits are in i2c.h and i2c-dev.c.
Thanks for your very valuable advice.
* * * * *
Let the drivers specify how many bytes they want to read with
i2c_smbus_read_i2c_block_data(). So far, the block count was
hard-coded to I2C_SMBUS_BLOCK_MAX (32), which did not make much sense.
Many driver authors complained about this before, and I believe it's
about time to fix it. Right now, authors have to do technically stupid
things, such as individual byte reads or full-fledged I2C messaging,
to work around the problem. We do not want to encourage that.
I even found that some bus drivers (e.g. i2c-amd8111) already
implemented I2C block read the "right" way, that is, they didn't
follow the old, broken standard. The fact that it was never noticed
before just shows how little i2c_smbus_read_i2c_block_data() was used,
which isn't that surprising given how broken its prototype was so far.
There are some obvious compatiblity considerations:
* This changes the i2c_smbus_read_i2c_block_data() prototype. Users
outside the kernel tree will notice at compilation time, and will
have to update their code.
* User-space has access to i2c_smbus_xfer() directly using i2c-dev, so
the changed expectations would affect tools such as i2cdump. In order
to preserve binary compatibility, we give I2C_SMBUS_I2C_BLOCK_DATA
a new numeric value, and define I2C_SMBUS_I2C_BLOCK_BROKEN with the
old numeric value. When i2c-dev receives a transaction with the
old value, it can convert it to the new format on the fly.
Signed-off-by: Jean Delvare <khali at linux-fr.org>
---
Documentation/i2c/chips/max6875 | 2 +-
Documentation/i2c/writing-clients | 2 +-
drivers/i2c/busses/i2c-powermac.c | 3 +--
drivers/i2c/busses/i2c-viapro.c | 2 +-
drivers/i2c/busses/scx200_acb.c | 2 --
drivers/i2c/chips/eeprom.c | 6 ++++--
drivers/i2c/chips/max6875.c | 1 +
drivers/i2c/i2c-core.c | 12 +++++++-----
drivers/i2c/i2c-dev.c | 9 +++++++++
drivers/macintosh/windfarm_smu_sat.c | 28 ++++------------------------
include/linux/i2c.h | 5 +++--
11 files changed, 32 insertions(+), 40 deletions(-)
--- linux-2.6.22-rc1.orig/drivers/i2c/i2c-core.c 2007-05-15 09:11:47.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/i2c-core.c 2007-05-15 09:11:48.000000000 +0200
@@ -1343,10 +1343,14 @@ s32 i2c_smbus_write_block_data(struct i2
EXPORT_SYMBOL(i2c_smbus_write_block_data);
/* Returns the number of read bytes */
-s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
+s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
+ u8 length, u8 *values)
{
union i2c_smbus_data data;
+ if (length > I2C_SMBUS_BLOCK_MAX)
+ length = I2C_SMBUS_BLOCK_MAX;
+ data.block[0] = length;
if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
I2C_SMBUS_READ,command,
I2C_SMBUS_I2C_BLOCK_DATA,&data))
@@ -1467,7 +1471,7 @@ static s32 i2c_smbus_xfer_emulated(struc
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
- msg[1].len = I2C_SMBUS_BLOCK_MAX;
+ msg[1].len = data->block[0];
} else {
msg[0].len = data->block[0] + 1;
if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
@@ -1523,9 +1527,7 @@ static s32 i2c_smbus_xfer_emulated(struc
data->word = msgbuf1[0] | (msgbuf1[1] << 8);
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
- /* fixed at 32 for now */
- data->block[0] = I2C_SMBUS_BLOCK_MAX;
- for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
+ for (i = 0; i < data->block[0]; i++)
data->block[i+1] = msgbuf1[i];
break;
case I2C_SMBUS_BLOCK_DATA:
--- linux-2.6.22-rc1.orig/Documentation/i2c/chips/max6875 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/Documentation/i2c/chips/max6875 2007-05-15 09:11:48.000000000 +0200
@@ -99,7 +99,7 @@ And then read the data
or
- count = i2c_smbus_read_i2c_block_data(fd, 0x84, buffer);
+ count = i2c_smbus_read_i2c_block_data(fd, 0x84, 16, buffer);
The block read should read 16 bytes.
0x84 is the block read command.
--- linux-2.6.22-rc1.orig/Documentation/i2c/writing-clients 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/Documentation/i2c/writing-clients 2007-05-15 09:11:48.000000000 +0200
@@ -571,7 +571,7 @@ SMBus communication
u8 command, u8 length,
u8 *values);
extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
- u8 command, u8 *values);
+ u8 command, u8 length, u8 *values);
These ones were removed in Linux 2.6.10 because they had no users, but could
be added back later if needed:
--- linux-2.6.22-rc1.orig/drivers/i2c/chips/eeprom.c 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/chips/eeprom.c 2007-05-15 09:11:48.000000000 +0200
@@ -88,8 +88,10 @@ static void eeprom_update_client(struct
dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
- for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_BLOCK_MAX)
- if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_BLOCK_MAX)
+ for (i = slice << 5; i < (slice + 1) << 5; i += 32)
+ if (i2c_smbus_read_i2c_block_data(client, i,
+ 32, data->data + i)
+ != 32)
goto exit;
} else {
if (i2c_smbus_write_byte(client, slice << 5)) {
--- linux-2.6.22-rc1.orig/drivers/i2c/chips/max6875.c 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/chips/max6875.c 2007-05-15 09:11:48.000000000 +0200
@@ -106,6 +106,7 @@ static void max6875_update_slice(struct
I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
if (i2c_smbus_read_i2c_block_data(client,
MAX6875_CMD_BLK_READ,
+ SLICE_SIZE,
buf) != SLICE_SIZE) {
goto exit_up;
}
--- linux-2.6.22-rc1.orig/include/linux/i2c.h 2007-05-15 09:11:47.000000000 +0200
+++ linux-2.6.22-rc1/include/linux/i2c.h 2007-05-15 09:11:48.000000000 +0200
@@ -90,7 +90,7 @@ extern s32 i2c_smbus_write_block_data(st
const u8 *values);
/* Returns the number of read bytes */
extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
- u8 command, u8 *values);
+ u8 command, u8 length, u8 *values);
extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
u8 command, u8 length,
const u8 *values);
@@ -524,8 +524,9 @@ union i2c_smbus_data {
#define I2C_SMBUS_WORD_DATA 3
#define I2C_SMBUS_PROC_CALL 4
#define I2C_SMBUS_BLOCK_DATA 5
-#define I2C_SMBUS_I2C_BLOCK_DATA 6
+#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
+#define I2C_SMBUS_I2C_BLOCK_DATA 8
/* ----- commands for the ioctl like i2c_command call:
--- linux-2.6.22-rc1.orig/drivers/macintosh/windfarm_smu_sat.c 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/drivers/macintosh/windfarm_smu_sat.c 2007-05-15 09:11:48.000000000 +0200
@@ -67,26 +67,6 @@ static struct i2c_driver wf_sat_driver =
.detach_client = wf_sat_detach,
};
-/*
- * XXX i2c_smbus_read_i2c_block_data doesn't pass the requested
- * length down to the low-level driver, so we use this, which
- * works well enough with the SMU i2c driver code...
- */
-static int sat_read_block(struct i2c_client *client, u8 command,
- u8 *values, int len)
-{
- union i2c_smbus_data data;
- int err;
-
- data.block[0] = len;
- err = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
- I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA,
- &data);
- if (!err)
- memcpy(values, data.block, len);
- return err;
-}
-
struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
unsigned int *size)
{
@@ -124,8 +104,8 @@ struct smu_sdbp_header *smu_sat_get_sdb_
return NULL;
for (i = 0; i < len; i += 4) {
- err = sat_read_block(&sat->i2c, 0xa, data, 4);
- if (err) {
+ err = i2c_smbus_read_i2c_block_data(&sat->i2c, 0xa, 4, data);
+ if (err < 0) {
printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
err);
goto fail;
@@ -157,8 +137,8 @@ static int wf_sat_read_cache(struct wf_s
{
int err;
- err = sat_read_block(&sat->i2c, 0x3f, sat->cache, 16);
- if (err)
+ err = i2c_smbus_read_i2c_block_data(&sat->i2c, 0x3f, 16, sat->cache);
+ if (err < 0)
return err;
sat->last_read = jiffies;
#ifdef LOTSA_DEBUG
--- linux-2.6.22-rc1.orig/drivers/i2c/busses/i2c-powermac.c 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/busses/i2c-powermac.c 2007-05-15 09:11:48.000000000 +0200
@@ -121,8 +121,7 @@ static s32 i2c_powermac_smbus_xfer( stru
if (rc)
goto bail;
rc = pmac_i2c_xfer(bus, addrdir, 1, command,
- read ? data->block : &data->block[1],
- data->block[0]);
+ &data->block[1], data->block[0]);
break;
default:
--- linux-2.6.22-rc1.orig/drivers/i2c/busses/i2c-viapro.c 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/busses/i2c-viapro.c 2007-05-15 09:11:48.000000000 +0200
@@ -235,7 +235,7 @@ static s32 vt596_access(struct i2c_adapt
if (!(vt596_features & FEATURE_I2CBLOCK))
goto exit_unsupported;
if (read_write == I2C_SMBUS_READ)
- outb_p(I2C_SMBUS_BLOCK_MAX, SMBHSTDAT0);
+ outb_p(data->block[0], SMBHSTDAT0);
/* Fall through */
case I2C_SMBUS_BLOCK_DATA:
outb_p(command, SMBHSTCMD);
--- linux-2.6.22-rc1.orig/drivers/i2c/busses/scx200_acb.c 2007-05-15 09:11:44.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/busses/scx200_acb.c 2007-05-15 09:11:48.000000000 +0200
@@ -310,8 +310,6 @@ static s32 scx200_acb_smbus_xfer(struct
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
- if (rw == I2C_SMBUS_READ)
- data->block[0] = I2C_SMBUS_BLOCK_MAX; /* For now */
len = data->block[0];
if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
return -EINVAL;
--- linux-2.6.22-rc1.orig/drivers/i2c/i2c-dev.c 2007-05-15 09:11:26.000000000 +0200
+++ linux-2.6.22-rc1/drivers/i2c/i2c-dev.c 2007-05-15 10:31:25.000000000 +0200
@@ -283,6 +283,7 @@ static int i2cdev_ioctl(struct inode *in
(data_arg.size != I2C_SMBUS_WORD_DATA) &&
(data_arg.size != I2C_SMBUS_PROC_CALL) &&
(data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
+ (data_arg.size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
(data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
(data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
dev_dbg(&client->adapter->dev,
@@ -329,10 +330,18 @@ static int i2cdev_ioctl(struct inode *in
if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
(data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
+ (data_arg.size == I2C_SMBUS_I2C_BLOCK_DATA) ||
(data_arg.read_write == I2C_SMBUS_WRITE)) {
if (copy_from_user(&temp, data_arg.data, datasize))
return -EFAULT;
}
+ if (data_arg.size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
+ /* Convert old I2C block commands to the new
+ convention. This preserves binary compatibility. */
+ data_arg.size = I2C_SMBUS_I2C_BLOCK_DATA;
+ if (data_arg.read_write == I2C_SMBUS_READ)
+ temp.block[0] = I2C_SMBUS_BLOCK_MAX;
+ }
res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
data_arg.read_write,
data_arg.command,data_arg.size,&temp);
--
Jean Delvare
More information about the i2c
mailing list