[i2c] i2c-dev i2c_smbus_read_i2c_block_data update

Jean Delvare khali at linux-fr.org
Sat May 12 12:32:49 CEST 2007


Hi all,

Here comes the user-space patch completing the
i2c_smbus_read_i2c_block_data kernel patch I sent yesterday. It finally
makes it possible to do I2C block reads from user-space with size less
than 32 bytes.

I'm not too sure about the python changes, I'm not even sure the
original code was correct and I can't test it. Mark, can you please
take a look and comment?

Index: kernel/include/i2c-dev.h
===================================================================
--- kernel/include/i2c-dev.h	(révision 4389)
+++ kernel/include/i2c-dev.h	(copie de travail)
@@ -271,11 +271,17 @@
 }
 
 /* Returns the number of read bytes */
+/* Note: kernel < 2.6.23 will not honor the requested length and will
+   always read 32 bytes */
 static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
-                                                  __u8 *values)
+                                                  __u8 length, __u8 *values)
 {
 	union i2c_smbus_data data;
 	int i;
+
+	if (length > 32)
+		length = 32;
+	data.block[0] = length;
 	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
 	                      I2C_SMBUS_I2C_BLOCK_DATA,&data))
 		return -1;
Index: prog/py-smbus/smbusmodule.c
===================================================================
--- prog/py-smbus/smbusmodule.c	(révision 4389)
+++ prog/py-smbus/smbusmodule.c	(copie de travail)
@@ -486,20 +486,22 @@
 }
 
 PyDoc_STRVAR(SMBus_read_i2c_block_data_doc,
-	"read_i2c_block_data(addr, cmd) -> results\n\n"
+	"read_i2c_block_data(addr, cmd, len) -> results\n\n"
 	"Perform I2C Block Read transaction.\n");
 
 static PyObject *
 SMBus_read_i2c_block_data(SMBus *self, PyObject *args)
 {
-	int addr, cmd;
+	int addr, cmd, len;
 	union i2c_smbus_data data;
 
-	if (!PyArg_ParseTuple(args, "ii:read_block_data", &addr, &cmd))
+	if (!PyArg_ParseTuple(args, "iii:read_i2c_block_data", &addr, &cmd,
+			&len))
 		return NULL;
 
 	SMBus_SET_ADDR(self, addr);
 
+	data.block[0] = len;
 	/* save a bit of code by calling the access function directly */
 	if (i2c_smbus_access(self->fd, I2C_SMBUS_READ, (__u8)cmd,
 				I2C_SMBUS_I2C_BLOCK_DATA, &data)) {
Index: prog/dump/i2cdump.c
===================================================================
--- prog/dump/i2cdump.c	(révision 4389)
+++ prog/dump/i2cdump.c	(copie de travail)
@@ -316,7 +316,7 @@
 			} else {
 				for (res = 0; res < 256; res += i) {
 					i = i2c_smbus_read_i2c_block_data(file,
-						res, cblock + res);
+						res, 32, cblock + res);
 					if (i <= 0)
 						break;
 				}


-- 
Jean Delvare



More information about the i2c mailing list