From fd19773c213b857634a4ab0bd32682e7dd75adb2 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 28 Jun 2011 14:05:40 +0000 Subject: [PATCH] drivers/pn53x_usb: Make valgrind more happy :) --- libnfc/chips/pn53x-internal.h | 1 + libnfc/chips/pn53x.c | 23 +++++++++++------ libnfc/nfc.c | 47 ++++++----------------------------- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/libnfc/chips/pn53x-internal.h b/libnfc/chips/pn53x-internal.h index ec729cc..db89ecb 100644 --- a/libnfc/chips/pn53x-internal.h +++ b/libnfc/chips/pn53x-internal.h @@ -118,6 +118,7 @@ typedef struct { } pn53x_command; typedef enum { + PN53X = 0x00, // Unknown PN53x chip type PN531 = 0x01, PN532 = 0x02, PN533 = 0x04, diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e22ba9c..42290ea 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -169,15 +169,18 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b } pnd->iLastError = pbtRx[0] & 0x3f; break; + case ReadRegister: + case WriteRegister: + if (CHIP_DATA(pnd)->type == PN533) { + // PN533 prepends its answer by the status byte + pnd->iLastError = pbtRx[0] & 0x3f; + } else { + pnd->iLastError = 0; + } + break; default: pnd->iLastError = 0; } - if (CHIP_DATA(pnd)->type == PN533) { - if ((pbtTx[0] == ReadRegister) || (pbtTx[0] == WriteRegister)) { - // PN533 prepends its answer by a status byte - pnd->iLastError = pbtRx[0] & 0x3f; - } - } return (0 == pnd->iLastError); } @@ -626,6 +629,9 @@ pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[22]) snprintf (abtFirmwareText, 22, "PN533 v%d.%d (0x%02x)", abtFw[1], abtFw[2], abtFw[3]); pnd->btSupportByte = abtFw[3]; break; + case PN53X: + // Could not happend + break; } return true; } @@ -861,6 +867,7 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd, if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { if (CHIP_DATA(pnd)->type == RCS360) { // TODO add support for RC-S360, at the moment it refuses to send raw frames without a first select + pnd->iLastError = ENOTIMPL; return false; } // No native support in InListPassiveTarget so we do discovery by hand @@ -1921,7 +1928,6 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd, abtCmd[1] = szMaxTargets; // MaxTg - // XXX Is there a better way to do handle supported modulations ? switch(pmInitModulation) { case PM_ISO14443A_106: case PM_FELICA_212: @@ -2492,6 +2498,9 @@ pn53x_data_new (nfc_device_t * pnd, const struct pn53x_io* io) // Keep I/O functions CHIP_DATA (pnd)->io = io; + // Set type to generic (means unknown) + CHIP_DATA (pnd)->type = PN53X; + // Set power mode to normal, if your device starts in LowVBat (ie. PN532 // UART) the driver layer have to correctly set it. CHIP_DATA (pnd)->power_mode = NORMAL; diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 3ec5e24..e38d388 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -43,8 +43,6 @@ #include -nfc_device_desc_t *nfc_pick_device (void); - const struct nfc_driver_t *nfc_drivers[] = { # if defined (DRIVER_PN53X_USB_ENABLED) &pn53x_usb_driver, @@ -82,8 +80,14 @@ nfc_connect (nfc_device_desc_t * pndd) { nfc_device_t *pnd = NULL; - if (pndd == NULL) - pndd = nfc_pick_device (); + if (pndd == NULL) { + size_t szDeviceFound; + nfc_device_desc_t ndd[1]; + nfc_list_devices (ndd, 1, &szDeviceFound); + if (szDeviceFound) { + pndd = &ndd[0]; + } + } if (pndd == NULL) return NULL; @@ -130,41 +134,6 @@ nfc_disconnect (nfc_device_t * pnd) } } -/** - * @brief Probe for the first discoverable supported devices (ie. only available for some drivers) - * @return \a nfc_device_desc_t struct pointer - */ -nfc_device_desc_t * -nfc_pick_device (void) -{ - const struct nfc_driver_t *ndr; - const struct nfc_driver_t **pndr = nfc_drivers; - while ((ndr = *pndr)) { - nfc_device_desc_t *pndd; - - if ((pndd = malloc (sizeof (*pndd)))) { - size_t szN; - - if (!ndr->probe (pndd, 1, &szN)) { - DBG ("%s probe failed", ndr->name); - szN = 0; - } - - if (szN == 0) { - DBG ("No %s device found", ndr->name); - free (pndd); - } else { - DBG ("One %s device found", ndr->name); - return pndd; - } - } - pndr++; - } - - DBG ("%s", "No device found with any driver :-("); - return NULL; -} - /** * @brief Probe for discoverable supported devices (ie. only available for some drivers) * @param[out] pnddDevices array of \a nfc_device_desc_t previously allocated by the caller.