diff mbox

[03/10] tpm: Use read/write_bytes for drivers without more specialized methods

Message ID 1460323386-16892-4-git-send-email-christophe-h.ricard@st.com (mailing list archive)
State New, archived
Headers show

Commit Message

Christophe Ricard April 10, 2016, 9:22 p.m. UTC
Some drivers might choose to implement only functions for transferring an
arbitrary number of bytes, without special handling of word or dword
transfers.

Signed-off-by: Alexander Steffen <Alexander.Steffen@infineon.com>
Signed-off-by: Christophe Ricard <christophe-h.ricard@st.com>
---
 drivers/char/tpm/tpm.h | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
diff mbox

Patch

diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index e93e19b..68ce900 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -475,17 +475,35 @@  static inline int tpm_read_bytes(struct tpm_chip *chip, u32 addr, u16 len,
 
 static inline int tpm_read8(struct tpm_chip *chip, u32 addr, u8 *result)
 {
-	return chip->lowlevel->read_bytes(chip, addr, 1, result);
+	return chip->lowlevel->read_bytes(chip, addr, sizeof(u8), result);
 }
 
 static inline int tpm_read16(struct tpm_chip *chip, u32 addr, u16 *result)
 {
-	return chip->lowlevel->read16(chip, addr, result);
+	int rc;
+
+	if (chip->lowlevel->read16)
+		return chip->lowlevel->read16(chip, addr, result);
+
+	rc = chip->lowlevel->read_bytes(chip, addr, sizeof(u16), (u8 *)result);
+	if (!rc)
+		*result = le16_to_cpu(*result);
+
+	return rc;
 }
 
 static inline int tpm_read32(struct tpm_chip *chip, u32 addr, u32 *result)
 {
-	return chip->lowlevel->read32(chip, addr, result);
+	int rc;
+
+	if (chip->lowlevel->read32)
+		return chip->lowlevel->read32(chip, addr, result);
+
+	rc = chip->lowlevel->read_bytes(chip, addr, sizeof(u32), (u8 *)result);
+	if (!rc)
+		*result = le32_to_cpu(*result);
+
+	return rc;
 }
 
 static inline int tpm_write_bytes(struct tpm_chip *chip, u32 addr, u16 len,
@@ -496,12 +514,17 @@  static inline int tpm_write_bytes(struct tpm_chip *chip, u32 addr, u16 len,
 
 static inline int tpm_write8(struct tpm_chip *chip, u32 addr, u8 value)
 {
-	return chip->lowlevel->write_bytes(chip, addr, 1, &value);
+	return chip->lowlevel->write_bytes(chip, addr, sizeof(u8), &value);
 }
 
 static inline int tpm_write32(struct tpm_chip *chip, u32 addr, u32 value)
 {
-	return chip->lowlevel->write32(chip, addr, value);
+	if (chip->lowlevel->write32)
+		return chip->lowlevel->write32(chip, addr, value);
+
+	value = cpu_to_le32(value);
+	return chip->lowlevel->write_bytes(chip, addr, sizeof(u32),
+					   (u8 *)&value);
 }
 
 extern struct class *tpm_class;