summaryrefslogtreecommitdiffstats
path: root/Linux_x86
diff options
context:
space:
mode:
authorNick Pelly <npelly@google.com>2011-06-22 15:20:48 -0700
committerNick Pelly <npelly@google.com>2011-06-22 19:20:21 -0700
commitd4cb91ee6f74f187cc0e7ba9dc073b0a77c27dfa (patch)
treee5cd2a7c2aaf788faac5af55a27ccc96dc559de9 /Linux_x86
parent23482d091c046310ef4c57f625c67e0d0e776500 (diff)
downloadexternal_libnfc-nxp-d4cb91ee6f74f187cc0e7ba9dc073b0a77c27dfa.zip
external_libnfc-nxp-d4cb91ee6f74f187cc0e7ba9dc073b0a77c27dfa.tar.gz
external_libnfc-nxp-d4cb91ee6f74f187cc0e7ba9dc073b0a77c27dfa.tar.bz2
Implement power control and firmware download for UART.
Also clean up some UART and I2C DAL functions. o Re-enable FW download o Use /system/vendor/firmware for pn544 firmware now o Clean up read() and write() paths for both I2C and UART o Use new sysfs entry for power and FW control on I2C platforms o Remove userspace sleep()'s on power control toggle. They are now done in the kernel as they should be. Change-Id: I39956d36e02c1eb526a5c546bd3cb74edce4ff18
Diffstat (limited to 'Linux_x86')
-rw-r--r--Linux_x86/phDal4Nfc.c1
-rw-r--r--Linux_x86/phDal4Nfc_i2c.c102
-rw-r--r--Linux_x86/phDal4Nfc_uart.c117
-rw-r--r--Linux_x86/phOsalNfc.c2
4 files changed, 121 insertions, 101 deletions
diff --git a/Linux_x86/phDal4Nfc.c b/Linux_x86/phDal4Nfc.c
index d052477..df069f3 100644
--- a/Linux_x86/phDal4Nfc.c
+++ b/Linux_x86/phDal4Nfc.c
@@ -556,7 +556,6 @@ NFCSTATUS phDal4Nfc_Config(pphDal4Nfc_sConfig_t config,void **phwref)
gLinkFunc.open_and_configure = phDal4Nfc_uart_open_and_configure;
gLinkFunc.read = phDal4Nfc_uart_read;
gLinkFunc.write = phDal4Nfc_uart_write;
- gLinkFunc.download = phDal4Nfc_uart_download;
gLinkFunc.reset = phDal4Nfc_uart_reset;
}
break;
diff --git a/Linux_x86/phDal4Nfc_i2c.c b/Linux_x86/phDal4Nfc_i2c.c
index 2d0e113..140fc93 100644
--- a/Linux_x86/phDal4Nfc_i2c.c
+++ b/Linux_x86/phDal4Nfc_i2c.c
@@ -23,7 +23,7 @@
*/
#define LOG_TAG "NFC_i2c"
-#include <utils/Log.h>
+#include <cutils/log.h>
#include <stdlib.h>
#include <unistd.h>
@@ -31,6 +31,7 @@
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/select.h>
+#include <errno.h>
#include <phDal4Nfc_debug.h>
#include <phDal4Nfc_i2c.h>
@@ -188,20 +189,29 @@ PURPOSE: Reads nNbBytesToRead bytes and writes them in pBuffer.
int phDal4Nfc_i2c_read(uint8_t * pBuffer, int nNbBytesToRead)
{
- int ret;
- DAL_ASSERT_STR(gI2cPortContext.nOpened == 1, "read called but not opened!");
-
- DAL_DEBUG("Reading %d bytes\n", nNbBytesToRead);
- ret = read(gI2cPortContext.nHandle, pBuffer, nNbBytesToRead);
- if (ret < 0)
- {
- DAL_DEBUG("Read failed: read() returned %d\n", ret);
- }
- else
- {
- DAL_DEBUG("Read succeed (%d bytes)\n", ret);
- }
- return ret;
+ int ret;
+ int numRead = 0;
+
+ DAL_ASSERT_STR(gI2cPortContext.nOpened == 1, "read called but not opened!");
+ DAL_DEBUG("_i2c_read() called to read %d bytes", nNbBytesToRead);
+
+ while (numRead < nNbBytesToRead) {
+ ret = read(gI2cPortContext.nHandle, pBuffer + numRead, nNbBytesToRead - numRead);
+ if (ret > 0) {
+ DAL_DEBUG("read %d bytes", ret);
+ numRead += ret;
+ } else if (ret == 0) {
+ DAL_PRINT("_i2c_read() EOF");
+ return -1;
+ } else {
+ DAL_DEBUG("_i2c_read() errno=%d", errno);
+ if (errno == EINTR || errno == EAGAIN) {
+ continue;
+ }
+ return -1;
+ }
+ }
+ return numRead;
}
/*-----------------------------------------------------------------------------
@@ -215,23 +225,32 @@ PURPOSE: Writes nNbBytesToWrite bytes from pBuffer to the link
int phDal4Nfc_i2c_write(uint8_t * pBuffer, int nNbBytesToWrite)
{
- int ret;
- DAL_ASSERT_STR(gI2cPortContext.nOpened == 1, "write called but not opened!");
-
- DAL_DEBUG("Writing %d bytes\n", nNbBytesToWrite);
- ret = write(gI2cPortContext.nHandle, pBuffer, nNbBytesToWrite);
- if (ret < 0)
- {
- DAL_DEBUG("Write failed: write() returned %d \n", ret);
- }
- else
- {
- DAL_DEBUG("Write succeed (%d bytes)\n", ret);
- }
- return ret;
+ int ret;
+ int numWrote = 0;
+
+ DAL_ASSERT_STR(gI2cPortContext.nOpened == 1, "write called but not opened!");
+ DAL_DEBUG("_i2c_write() called to write %d bytes\n", nNbBytesToWrite);
+
+ while (numWrote < nNbBytesToWrite) {
+ ret = write(gI2cPortContext.nHandle, pBuffer + numWrote, nNbBytesToWrite - numWrote);
+ if (ret > 0) {
+ DAL_DEBUG("wrote %d bytes", ret);
+ numWrote += ret;
+ } else if (ret == 0) {
+ DAL_PRINT("_i2c_write() EOF");
+ return -1;
+ } else {
+ DAL_DEBUG("_i2c_write() errno=%d", errno);
+ if (errno == EINTR || errno == EAGAIN) {
+ continue;
+ }
+ return -1;
+ }
+ }
+
+ return numWrote;
}
-
/*-----------------------------------------------------------------------------
FUNCTION: phDal4Nfc_i2c_reset
@@ -241,26 +260,7 @@ PURPOSE: Reset the PN544, using the VEN pin
-----------------------------------------------------------------------------*/
int phDal4Nfc_i2c_reset(long level)
{
- int ret = NFCSTATUS_SUCCESS;
-
- DAL_DEBUG("phDal4Nfc_i2c_reset, VEN level = %ld",level);
-
- ret = ioctl(gI2cPortContext.nHandle, PN544_SET_PWR, level);
-
- /* HACK to increase reset time
- * TODO: move this to kernel
- */
- if (level == 0) {
- LOGW("sleeping a little longer...");
- usleep(50000);
- } else {
- usleep(10000);
- }
+ DAL_DEBUG("phDal4Nfc_i2c_reset, VEN level = %ld", level);
- return ret;
+ return ioctl(gI2cPortContext.nHandle, PN544_SET_PWR, level);
}
-
-
-
-
-
diff --git a/Linux_x86/phDal4Nfc_uart.c b/Linux_x86/phDal4Nfc_uart.c
index c4526e0..b4a282d 100644
--- a/Linux_x86/phDal4Nfc_uart.c
+++ b/Linux_x86/phDal4Nfc_uart.c
@@ -26,12 +26,17 @@
*
*/
+#define LOG_TAG "NFC_uart"
+#include <cutils/log.h>
+
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/select.h>
+#include <stdio.h>
+#include <errno.h>
#include <phDal4Nfc_debug.h>
#include <phDal4Nfc_uart.h>
@@ -266,13 +271,13 @@ PURPOSE: Reads nNbBytesToRead bytes and writes them in pBuffer.
-----------------------------------------------------------------------------*/
int phDal4Nfc_uart_read(uint8_t * pBuffer, int nNbBytesToRead)
{
- int ret;
- int numRead = 0;
+ int ret;
+ int numRead = 0;
- DAL_ASSERT_STR(gComPortContext.nOpened == 1, "read called but not opened!");
- DAL_DEBUG("_uart_read() called to read %d bytes", nNbBytesToRead);
+ DAL_ASSERT_STR(gComPortContext.nOpened == 1, "read called but not opened!");
+ DAL_DEBUG("_uart_read() called to read %d bytes", nNbBytesToRead);
- while (numRead != nNbBytesToRead) {
+ while (numRead < nNbBytesToRead) {
ret = read(gComPortContext.nHandle, pBuffer + numRead, nNbBytesToRead - numRead);
if (ret > 0) {
DAL_DEBUG("read %d bytes", ret);
@@ -287,8 +292,8 @@ int phDal4Nfc_uart_read(uint8_t * pBuffer, int nNbBytesToRead)
}
return -1;
}
- }
- return numRead;
+ }
+ return numRead;
}
/*-----------------------------------------------------------------------------
@@ -302,28 +307,30 @@ PURPOSE: Writes nNbBytesToWrite bytes from pBuffer to the link
int phDal4Nfc_uart_write(uint8_t * pBuffer, int nNbBytesToWrite)
{
- fd_set wfds;
- struct timeval tv;
- int ret;
-
- DAL_ASSERT_STR(gComPortContext.nOpened == 1, "write called but not opened!");
-
- FD_ZERO(&wfds);
- FD_SET(gComPortContext.nHandle, &wfds);
-
- /* select will block for 10 sec */
- tv.tv_sec = 2;
- tv.tv_usec = 0;
-
- ret = select(gComPortContext.nHandle + 1, NULL, &wfds, NULL, &tv);
-
- if (ret == -1)
- return -1;
-
- if (ret)
- return write(gComPortContext.nHandle, pBuffer, nNbBytesToWrite);
-
- return 0;
+ int ret;
+ int numWrote = 0;
+
+ DAL_ASSERT_STR(gComPortContext.nOpened == 1, "write called but not opened!");
+ DAL_DEBUG("_uart_write() called to write %d bytes\n", nNbBytesToWrite);
+
+ while (numWrote < nNbBytesToWrite) {
+ ret = write(gComPortContext.nHandle, pBuffer + numWrote, nNbBytesToWrite - numWrote);
+ if (ret > 0) {
+ DAL_DEBUG("wrote %d bytes", ret);
+ numWrote += ret;
+ } else if (ret == 0) {
+ DAL_PRINT("_uart_write() EOF");
+ return -1;
+ } else {
+ DAL_DEBUG("_uart_write() errno=%d", errno);
+ if (errno == EINTR || errno == EAGAIN) {
+ continue;
+ }
+ return -1;
+ }
+ }
+
+ return numWrote;
}
/*-----------------------------------------------------------------------------
@@ -333,24 +340,38 @@ FUNCTION: phDal4Nfc_uart_reset
PURPOSE: Reset the PN544, using the VEN pin
-----------------------------------------------------------------------------*/
-int phDal4Nfc_uart_reset()
-{
- DAL_PRINT("phDal4Nfc_uart_reset");
-
- return NFCSTATUS_FEATURE_NOT_SUPPORTED;
-}
-
-/*-----------------------------------------------------------------------------
-
-FUNCTION: phDal4Nfc_uart_write
-
-PURPOSE: Put the PN544 in download mode, using the GPIO4 pin
-
------------------------------------------------------------------------------*/
-int phDal4Nfc_uart_download()
+int phDal4Nfc_uart_reset(long level)
{
- DAL_PRINT("phDal4Nfc_uart_download");
-
- return NFCSTATUS_FEATURE_NOT_SUPPORTED;
+ static const char NFC_POWER_PATH[] = "/sys/devices/platform/nfc-power/nfc_power";
+ int sz;
+ int fd = -1;
+ int ret = NFCSTATUS_FAILED;
+ char buffer[2];
+
+ DAL_DEBUG("phDal4Nfc_uart_reset, VEN level = %ld", level);
+
+ if (snprintf(buffer, sizeof(buffer), "%u", (unsigned int)level) != 1) {
+ LOGE("Bad nfc power level (%u)", (unsigned int)level);
+ goto out;
+ }
+
+ fd = open(NFC_POWER_PATH, O_WRONLY);
+ if (fd < 0) {
+ LOGE("open(%s) for write failed: %s (%d)", NFC_POWER_PATH,
+ strerror(errno), errno);
+ goto out;
+ }
+ sz = write(fd, &buffer, sizeof(buffer) - 1);
+ if (sz < 0) {
+ LOGE("write(%s) failed: %s (%d)", NFC_POWER_PATH, strerror(errno),
+ errno);
+ goto out;
+ }
+ ret = NFCSTATUS_SUCCESS;
+
+out:
+ if (fd >= 0) {
+ close(fd);
+ }
+ return ret;
}
-
diff --git a/Linux_x86/phOsalNfc.c b/Linux_x86/phOsalNfc.c
index 9765e2c..db336f1 100644
--- a/Linux_x86/phOsalNfc.c
+++ b/Linux_x86/phOsalNfc.c
@@ -169,7 +169,7 @@ void phOsalNfc_PrintData(const char *pString, uint32_t length, uint8_t *pBuffer)
for (i = 0; i < length; i++) {
snprintf(&print_buffer[i*5], 6, " 0x%02X", pBuffer[i]);
}
- LOGD("> NFC I2C %s: %s", pString,print_buffer);
+ LOGD("> NFC %s: %s", pString, print_buffer);
}
}