lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20130216024235.GA25913@core.coreip.homeip.net>
Date:	Fri, 15 Feb 2013 18:42:35 -0800
From:	Dmitry Torokhov <dmitry.torokhov@...il.com>
To:	Wolfram Sang <wolfram@...-dreams.de>
Cc:	Ben Dooks <ben-linux@...ff.org>, Jean Delvare <khali@...ux-fr.org>,
	linux-i2c@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: [PATCH] I2C: add i2c_master_send_exact() and friends

Many i2c users consider short transfers to be an error and would prefer
getting -EIO instead of a positive return value and having to convert
it to error code by themselves. So let's add the following new helpers:

	i2c_master_send_exact()
	i2c_master_recv_exact()
	i2c_transfer_exact()

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@...il.com>
---

Resending to Wolfram's new address...

 drivers/i2c/i2c-core.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c.h    |   11 ++++++++
 2 files changed, 80 insertions(+)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index e388590..6cddb5d 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1430,6 +1430,33 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 EXPORT_SYMBOL(i2c_transfer);
 
 /**
+ * i2c_transfer_exact - transfer given number of I2C messages
+ * @adap: Handle to I2C bus
+ * @msgs: One or more messages to execute before STOP is issued to
+ *	terminate the operation; each message begins with a START.
+ * @num: Number of messages to be executed.
+ *
+ * Returns negative errno (including -EIO on short transfer),
+ * or 0 if all messages have been tranferred successfully.
+ *
+ * Note that there is no requirement that each message be sent to
+ * the same slave address, although that is the most common model.
+ */
+int i2c_transfer_exact(struct i2c_adapter *adap,
+		       struct i2c_msg *msgs, int num)
+{
+	int ret;
+
+	ret = i2c_transfer(adap, msgs, num);
+	if (ret == num)
+		return 0;
+
+	return ret < 0 ? ret : -EIO;
+
+}
+EXPORT_SYMBOL(i2c_transfer_exact);
+
+/**
  * 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
@@ -1459,6 +1486,27 @@ int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
 EXPORT_SYMBOL(i2c_master_send);
 
 /**
+ * i2c_master_send_exact - send exact number of bytes in master transmit mode
+ * @client: Handle to slave device
+ * @buf: Data that will be written to the slave
+ * @count: How many bytes to write, must be less than 64k since msg.len is u16
+ *
+ * Returns negative errno (including -EIO on short transfer), or 0.
+ */
+int i2c_master_send_exact(const struct i2c_client *client,
+			  const char *buf, int count)
+{
+	int ret;
+
+	ret = i2c_master_send(client, buf, count);
+	if (ret == count)
+		return 0;
+
+	return ret < 0 ? ret : -EIO;
+}
+EXPORT_SYMBOL(i2c_master_send_exact);
+
+/**
  * 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
@@ -1488,6 +1536,27 @@ int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
 }
 EXPORT_SYMBOL(i2c_master_recv);
 
+/**
+ * i2c_master_recv_exact - read exact number of bytes in master receive mode
+ * @client: Handle to slave device
+ * @buf: Where to store data read from slave
+ * @count: How many bytes to read, must be less than 64k since msg.len is u16
+ *
+ * Returns negative errno (including -EIO on short transfer), or 0.
+ */
+int i2c_master_recv_exact(const struct i2c_client *client,
+			  char *buf, int count)
+{
+	int ret;
+
+	ret = i2c_master_recv(client, buf, count);
+	if (ret == count)
+		return 0;
+
+	return ret < 0 ? ret : -EIO;
+}
+EXPORT_SYMBOL(i2c_master_recv_exact);
+
 /* ----------------------------------------------------
  * the i2c address scanning function
  * Will not work for 10-bit addresses!
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index d0c4db7..3d76059 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -59,13 +59,24 @@ struct module;
  */
 extern int i2c_master_send(const struct i2c_client *client, const char *buf,
 			   int count);
+
+extern int i2c_master_send_exact(const struct i2c_client *client,
+				 const char *buf, int count);
+
 extern int i2c_master_recv(const struct i2c_client *client, char *buf,
 			   int count);
 
+extern int i2c_master_recv_exact(const struct i2c_client *client,
+				 char *buf, int count);
+
 /* Transfer num messages.
  */
 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 			int num);
+
+extern int i2c_transfer_exact(struct i2c_adapter *adap,
+			      struct i2c_msg *msgs, int num);
+
 /* Unlocked flavor */
 extern int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 			  int num);
-- 
Dmitry
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ