From ee3dc75d3eb86af554f3d27dafd36d82d069b193 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 13 Jul 2020 11:16:18 -0700 Subject: [PATCH] Fixes for SPI CS auto-detection on Infineon SLB9670. I was able to reproduce issue on Pi 3b+. First run detects on `/dev/spidev0.1` then finds on `/dev/spidev0.0` afterwards (odd). --- examples/tpm_io.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/tpm_io.c b/examples/tpm_io.c index dc67866..a53ab25 100644 --- a/examples/tpm_io.c +++ b/examples/tpm_io.c @@ -262,20 +262,24 @@ #ifdef WOLFTPM_DEBUG_TIMEOUT printf("SPI Ready Timeout %d\n", TPM_SPI_WAIT_RETRY - timeout); #endif - if (size != 1 || timeout <= 0) { - close(spiDev); - return TPM_RC_FAILURE; + if (size == 1 && timeout > 0) { + ret = TPM_RC_SUCCESS; } } - - /* Remainder of message */ - spi.tx_buf = (unsigned long)&txBuf[TPM_TIS_HEADER_SZ]; - spi.rx_buf = (unsigned long)&rxBuf[TPM_TIS_HEADER_SZ]; - spi.len = xferSz - TPM_TIS_HEADER_SZ; - size = ioctl(spiDev, SPI_IOC_MESSAGE(1), &spi); - - if (size == (size_t)xferSz - TPM_TIS_HEADER_SZ) + else { ret = TPM_RC_SUCCESS; + } + + if (ret == TPM_RC_SUCCESS) { + /* Remainder of message */ + spi.tx_buf = (unsigned long)&txBuf[TPM_TIS_HEADER_SZ]; + spi.rx_buf = (unsigned long)&rxBuf[TPM_TIS_HEADER_SZ]; + spi.len = xferSz - TPM_TIS_HEADER_SZ; + size = ioctl(spiDev, SPI_IOC_MESSAGE(1), &spi); + + if (size == (size_t)xferSz - TPM_TIS_HEADER_SZ) + ret = TPM_RC_SUCCESS; + } #else /* Send Entire Message - no wait states */ spi.tx_buf = (unsigned long)txBuf; @@ -292,7 +296,7 @@ #ifdef WOLFTPM_AUTODETECT /* if response is not 0xFF then we "found" something */ if (!foundSpiDev) { - if (rxBuf[0] != 0xFF) { + if (ret == TPM_RC_SUCCESS && rxBuf[0] != 0xFF) { #ifdef DEBUG_WOLFTPM printf("Found TPM @ %s\n", TPM2_SPI_DEV); #endif