Unbreak and merge PN53x USB drivers.

While here fix some includes that would not work if SRCDIR != BUILDDIR.
This commit is contained in:
Romain Tartiere 2011-03-05 19:54:52 +00:00
parent 094db12a1a
commit f1f31d1b4c
16 changed files with 391 additions and 487 deletions

View file

@ -30,7 +30,7 @@
#include "uart.h"
#include <nfc/nfc.h>
#include "libnfc/nfc-internal.h"
#include "nfc-internal.h"
// Test if we are dealing with unix operating systems
#ifndef _WIN32

View file

@ -39,8 +39,9 @@
#include "pn53x.h"
#include "pn53x-internal.h"
#include "libnfc/mirror-subr.h"
#include "libnfc/nfc-internal.h"
#include "mirror-subr.h"
#include "nfc-internal.h"
#ifdef _WIN32
# include "contrib/windows.h"

View file

@ -32,17 +32,9 @@
# include "drivers/acr122.h"
# endif /* DRIVER_ACR122_ENABLED */
# if defined (DRIVER_PN531_USB_ENABLED) || defined (DRIVER_PN533_USB_ENABLED)
# if defined (DRIVER_PN53X_USB_ENABLED)
# include "drivers/pn53x_usb.h"
# endif /* DRIVER_PN531_USB_ENABLED || DRIVER_PN533_USB_ENABLED */
# if defined (DRIVER_PN531_USB_ENABLED)
# include "drivers/pn531_usb.h"
# endif /* DRIVER_PN531_USB_ENABLED */
# if defined (DRIVER_PN533_USB_ENABLED)
# include "drivers/pn533_usb.h"
# endif /* DRIVER_PN533_USB_ENABLED */
# endif /* DRIVER_PN53X_USB_ENABLED */
# if defined (DRIVER_ARYGON_ENABLED)
# include "drivers/arygon.h"

View file

@ -1,7 +1,7 @@
# set the include path found by configure
INCLUDES= $(all_includes) $(LIBNFC_CFLAGS)
noinst_HEADERS = acr122.h arygon.h pn531_usb.h pn532_uart.h pn533_usb.h pn53x_usb.h
noinst_HEADERS = acr122.h arygon.h pn532_uart.h pn53x_usb.h
noinst_LTLIBRARIES = libnfcdrivers.la
libnfcdrivers_la_SOURCES =
@ -16,15 +16,10 @@ if DRIVER_ARYGON_ENABLED
libnfcdrivers_la_SOURCES += arygon.c
endif
if DRIVER_PN531_USB_ENABLED
libnfcdrivers_la_SOURCES += pn531_usb.c
if DRIVER_PN53X_USB_ENABLED
libnfcdrivers_la_SOURCES += pn53x_usb.c
endif
if DRIVER_PN533_USB_ENABLED
libnfcdrivers_la_SOURCES += pn533_usb.c
endif
if DRIVER_PN532_UART_ENABLED
libnfcdrivers_la_SOURCES += pn532_uart.c
endif
@ -35,7 +30,6 @@ if PCSC_ENABLED
endif
if LIBUSB_ENABLED
libnfcdrivers_la_SOURCES += pn53x_usb.c
libnfcdrivers_la_CFLAGS += @libusb_CFLAGS@
libnfcdrivers_la_LIBADD += @libusb_LIBS@
endif

View file

@ -39,10 +39,10 @@
#include <nfc/nfc.h>
#include "libnfc/drivers.h"
#include "libnfc/nfc-internal.h"
#include "libnfc/chips/pn53x.h"
#include "libnfc/chips/pn53x-internal.h"
#include "drivers.h"
#include "nfc-internal.h"
#include "chips/pn53x.h"
#include "chips/pn53x-internal.h"
#include "uart.h"
/** @def DEV_ARYGON_PROTOCOL_ARYGON_ASCII

View file

@ -1,76 +0,0 @@
/*-
* Public platform independent Near Field Communication (NFC) library
*
* Copyright (C) 2009, Roel Verdult
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file pn531_usb.c
* @brief Driver for PN531 chip using USB
*/
/*
Thanks to d18c7db and Okko for example code
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdlib.h>
#include "libnfc/drivers.h"
nfc_device_desc_t *
pn531_usb_pick_device (void)
{
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
if (!pn531_usb_list_devices (pndd, 1, &szN)) {
DBG ("%s", "pn531_usb_list_devices failed");
free (pndd);
return NULL;
}
if (szN == 0) {
DBG ("%s", "No device found");
free (pndd);
return NULL;
}
}
return pndd;
}
bool
pn531_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
// array of {vendor,product} pairs for USB devices
usb_candidate_t candidates[] = { {0x04CC, 0x0531}
, {0x054c, 0x0193}
};
return pn53x_usb_list_devices (&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0],
sizeof (candidates) / sizeof (usb_candidate_t), PN531_USB_DRIVER_NAME);
}
nfc_device_t *
pn531_usb_connect (const nfc_device_desc_t * pndd)
{
return pn53x_usb_connect (pndd, pndd->acDevice, NC_PN531);
}

View file

@ -1,34 +0,0 @@
/**
* Public platform independent Near Field Communication (NFC) library
*
* Copyright (C) 2009, Roel Verdult
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*
* @file pn531_usb.h
* @brief
*/
#ifndef __NFC_DRIVER_PN531_USB_H__
# define __NFC_DRIVER_PN531_USB_H__
# define PN531_USB_DRIVER_NAME "PN531_USB"
// Functions used by developer to handle connection to this device
nfc_device_t *pn531_usb_connect (const nfc_device_desc_t * pndd);
bool pn531_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_desc_t *pn531_usb_pick_device (void);
#endif // ! __NFC_DRIVER_PN531_USB_H__

View file

@ -36,10 +36,10 @@
#include <nfc/nfc.h>
#include "libnfc/drivers.h"
#include "libnfc/nfc-internal.h"
#include "libnfc/chips/pn53x.h"
#include "libnfc/chips/pn53x-internal.h"
#include "drivers.h"
#include "nfc-internal.h"
#include "chips/pn53x.h"
#include "chips/pn53x-internal.h"
#include "uart.h"
#define PN532_UART_DEFAULT_SPEED 115200

View file

@ -1,94 +0,0 @@
/*-
* Public platform independent Near Field Communication (NFC) library
*
* Copyright (C) 2009, Roel Verdult
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file pn533_usb.c
* @brief Driver for PN533 chip using USB
*/
/*
Thanks to d18c7db and Okko for example code
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdlib.h>
#include "libnfc/drivers.h"
nfc_device_desc_t *
pn533_usb_pick_device (void)
{
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
if (!pn533_usb_list_devices (pndd, 1, &szN)) {
DBG ("%s", "pn533_usb_list_devices failed");
free (pndd);
return NULL;
}
if (szN == 0) {
DBG ("%s", "No device found");
free (pndd);
return NULL;
}
}
return pndd;
}
bool
pn533_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
// array of {vendor,product} pairs for USB devices
usb_candidate_t candidates[] = {
{ 0x04CC, 0x2533 }, // NXP - PN533
{ 0x04E6, 0x5591 }, // SCM Micro - SCL3711-NFC&RW
{ 0x1FD3, 0x0608 } // ASK - LoGO
};
return pn53x_usb_list_devices (&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0],
sizeof (candidates) / sizeof (usb_candidate_t), PN533_USB_DRIVER_NAME);
}
nfc_device_t *
pn533_usb_connect (const nfc_device_desc_t * pndd)
{
return pn53x_usb_connect (pndd, pndd->acDevice, NC_PN533);
}
void
pn533_usb_init (nfc_device_t * pnd)
{
usb_spec_t* pus = (usb_spec_t*) pnd->nds;
DBG ("pus->uc.idVendor == 0x%04x, pus->uc.idProduct == 0x%04x", pus->uc.idVendor, pus->uc.idProduct);
if ((pus->uc.idVendor == 0x1FD3) && (pus->uc.idProduct == 0x0608)) { // ASK - LoGO
DBG ("ASK LoGO initialization.");
pn53x_set_reg (pnd, 0x6106, 0xFF, 0x1B);
pn53x_set_reg (pnd, 0x6306, 0xFF, 0x14);
pn53x_set_reg (pnd, 0xFFFD, 0xFF, 0x37);
pn53x_set_reg (pnd, 0xFFB0, 0xFF, 0x3B);
}
}

View file

@ -1,35 +0,0 @@
/**
* Public platform independent Near Field Communication (NFC) library
*
* Copyright (C) 2009, Roel Verdult
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*
* @file pn533_usb.h
* @brief
*/
#ifndef __NFC_DRIVER_PN533_USB_H__
# define __NFC_DRIVER_PN533_USB_H__
# define PN533_USB_DRIVER_NAME "PN533_USB"
// Functions used by developer to handle connection to this device
nfc_device_t *pn533_usb_connect (const nfc_device_desc_t * pndd);
void pn533_usb_init (nfc_device_t * pnd);
bool pn533_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_desc_t *pn533_usb_pick_device (void);
#endif // ! __NFC_DRIVER_PN533_USB_H__

View file

@ -3,6 +3,7 @@
*
* Copyright (C) 2009, Roel Verdult
* Copyright (C) 2010, Romain Tartière, Romuald Conty
* Copyright (C) 2011, Romain Tartière, Romuald Conty
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
@ -36,22 +37,88 @@ Thanks to d18c7db and Okko for example code
#include <usb.h>
#include <string.h>
#include "libnfc/drivers.h"
#include "libnfc/chips/pn53x.h"
#include <nfc/nfc.h>
#define BUFFER_LENGTH 256
#include "nfc-internal.h"
#include "chips/pn53x.h"
#include "chips/pn53x-internal.h"
#include "drivers/pn53x_usb.h"
#define PN53X_USB_DRIVER_NAME "PN53x USB"
#define USB_TIMEOUT 0
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
#define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data))
typedef enum {
UNKNOWN,
NXP_PN531,
NXP_PN533,
ASK_LOGO,
SCM_SCL3711
} pn53x_usb_model;
struct pn53x_usb_data {
usb_dev_handle *pudh;
pn53x_usb_model model;
uint32_t uiEndPointIn;
uint32_t uiEndPointOut;
uint32_t uiMaxPacketSize;
};
const struct pn53x_io pn53x_usb_io;
bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len);
int
pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx)
{
int ret = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, USB_TIMEOUT);
PRINT_HEX ("RX", abtRx, ret);
return ret;
}
int
pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t szTx)
{
PRINT_HEX ("TX", abtTx, szTx);
return usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, USB_TIMEOUT);
}
struct pn53x_usb_supported_device {
uint16_t vendor_id;
uint16_t product_id;
pn53x_usb_model model;
const char *name;
};
const struct pn53x_usb_supported_device pn53x_usb_supported_devices[] = {
{ 0x04CC, 0x0531, NXP_PN531, "Philips / USB TAMA" },
{ 0x04CC, 0x2533, NXP_PN533, "NXP / PN533" },
{ 0x04E6, 0x5591, SCM_SCL3711, "SCM Micro / SCL3711-NFC&RW" },
{ 0x054c, 0x0193, NXP_PN531, "TOPPAN FORMS ???? / TN31CUD001SW ????" },
{ 0x1FD3, 0x0608, ASK_LOGO, "ASK / LoGO" }
};
pn53x_usb_model
pn53x_usb_get_device_model (uint16_t vendor_id, uint16_t product_id)
{
for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) {
if ((vendor_id == pn53x_usb_supported_devices[n].vendor_id) &&
(product_id == pn53x_usb_supported_devices[n].product_id))
return pn53x_usb_supported_devices[n].model;
}
return UNKNOWN;
}
// TODO Move this HACK1 into an upper level in order to benefit to other devices that use PN53x
static const byte_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
void pn53x_usb_ack (nfc_device_t * pnd);
int pn53x_usb_ack (nfc_device_t * pnd);
// Find transfer endpoints for bulk transfers
void
get_end_points (struct usb_device *dev, usb_spec_t * pus)
pn53x_usb_get_end_points (struct usb_device *dev, struct pn53x_usb_data *data)
{
uint32_t uiIndex;
uint32_t uiEndPoint;
@ -68,47 +135,41 @@ get_end_points (struct usb_device *dev, usb_spec_t * pus)
// Test if we dealing with a bulk IN endpoint
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) {
pus->uiEndPointIn = uiEndPoint;
pus->wMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
data->uiEndPointIn = uiEndPoint;
data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
}
// Test if we dealing with a bulk OUT endpoint
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) {
pus->uiEndPointOut = uiEndPoint;
pus->wMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
data->uiEndPointOut = uiEndPoint;
data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
}
}
}
bool
pn53x_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound,
usb_candidate_t candidates[], int num_candidates, char *target_name)
pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
int ret,
i;
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *udev;
uint32_t uiBusIndex = 0;
char string[256];
string[0] = '\0';
usb_init ();
int res;
// usb_find_busses will find all of the busses on the system. Returns the number of changes since previous call to this function (total of new busses and busses removed).
if ((ret = usb_find_busses () < 0))
if ((res = usb_find_busses () < 0))
return false;
// usb_find_devices will find all of the devices on each bus. This should be called after usb_find_busses. Returns the number of changes since the previous call to this function (total of new device and devices removed).
if ((ret = usb_find_devices () < 0))
if ((res = usb_find_devices () < 0))
return false;
*pszDeviceFound = 0;
uint32_t uiBusIndex = 0;
struct usb_bus *bus;
for (bus = usb_get_busses (); bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
for (i = 0; i < num_candidates; ++i) {
for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) {
// DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct,candidates[i].idVendor,candidates[i].idProduct);
if (candidates[i].idVendor == dev->descriptor.idVendor && candidates[i].idProduct == dev->descriptor.idProduct) {
if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) &&
(pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) {
// Make sure there are 2 endpoints available
// with libusb-win32 we got some null pointers so be robust before looking at endpoints:
if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL) {
@ -119,22 +180,11 @@ pn53x_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_
// Nope, we maybe want the next one, let's try to find another
continue;
}
if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) {
udev = usb_open (dev);
if (udev) {
usb_get_string_simple (udev, dev->descriptor.iManufacturer, string, sizeof (string));
if (strlen (string) > 0)
strcpy (string + strlen (string), " / ");
usb_get_string_simple (udev, dev->descriptor.iProduct, string + strlen (string),
sizeof (string) - strlen (string));
}
usb_close (udev);
}
if (strlen (string) == 0)
strcpy (pnddDevices[*pszDeviceFound].acDevice, target_name);
else
strcpy (pnddDevices[*pszDeviceFound].acDevice, string);
pnddDevices[*pszDeviceFound].pcDriver = target_name;
usb_dev_handle *udev = usb_open (dev);
pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[*pszDeviceFound].acDevice, sizeof (pnddDevices[*pszDeviceFound].acDevice));
usb_close (udev);
pnddDevices[*pszDeviceFound].pcDriver = PN53X_USB_DRIVER_NAME;
pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex;
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
@ -150,21 +200,46 @@ pn53x_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_
return false;
}
bool
pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len)
{
*buffer = '\0';
if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) {
if (udev) {
usb_get_string_simple (udev, dev->descriptor.iManufacturer, buffer, len);
if (strlen (buffer) > 0)
strcpy (buffer + strlen (buffer), " / ");
usb_get_string_simple (udev, dev->descriptor.iProduct, buffer + strlen (buffer), len - strlen (buffer));
}
}
if (!*buffer) {
for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) {
if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) &&
(pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) {
strncpy (buffer, pn53x_usb_supported_devices[n].name, len);
return true;
}
}
}
return false;
}
nfc_device_t *
pn53x_usb_connect (const nfc_device_desc_t * pndd, const char *target_name, int target_chip)
pn53x_usb_connect (const nfc_device_desc_t *pndd)
{
nfc_device_t *pnd = NULL;
usb_spec_t *pus;
usb_spec_t us;
struct pn53x_usb_data data = {
.pudh = NULL,
.uiEndPointIn = 0,
.uiEndPointOut = 0,
};
struct usb_bus *bus;
struct usb_device *dev;
uint32_t uiBusIndex;
us.uiEndPointIn = 0;
us.uiEndPointOut = 0;
us.pudh = NULL;
DBG ("Attempt to connect to %s device", target_name);
usb_init ();
uiBusIndex = pndd->uiBusIndex;
@ -174,216 +249,311 @@ pn53x_usb_connect (const nfc_device_desc_t * pndd, const char *target_name, int
DBG ("Checking device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
if (uiBusIndex == 0) {
// Open the USB device
us.pudh = usb_open (dev);
data.pudh = usb_open (dev);
get_end_points (dev, &us);
if (usb_set_configuration (us.pudh, 1) < 0) {
pn53x_usb_get_end_points (dev, &data);
if (usb_set_configuration (data.pudh, 1) < 0) {
ERR ("Unable to set USB configuration, please check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
usb_close (us.pudh);
usb_close (data.pudh);
// we failed to use the specified device
return NULL;
}
if (usb_claim_interface (us.pudh, 0) < 0) {
if (usb_claim_interface (data.pudh, 0) < 0) {
DBG ("%s", "Can't claim interface");
usb_close (us.pudh);
usb_close (data.pudh);
// we failed to use the specified device
return NULL;
}
// Copy VendorId and ProductId
us.uc.idVendor = dev->descriptor.idVendor;
us.uc.idProduct = dev->descriptor.idProduct;
data.model = pn53x_usb_get_device_model (dev->descriptor.idVendor, dev->descriptor.idProduct);
// Allocate memory for the device info and specification, fill it and return the info
pus = malloc (sizeof (usb_spec_t));
*pus = us;
pnd = malloc (sizeof (nfc_device_t));
strcpy (pnd->acName, target_name);
pnd->nc = target_chip;
pnd->nds = (nfc_device_spec_t) pus;
pnd->bActive = true;
pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->acName, sizeof (pnd->acName));
pnd->driver_data = malloc(sizeof(struct pn53x_usb_data));
*DRIVER_DATA (pnd) = data;
pnd->chip_data = malloc(sizeof(struct pn53x_data));
CHIP_DATA (pnd)->state = NORMAL;
CHIP_DATA (pnd)->io = &pn53x_usb_io;
pnd->driver = &pn53x_usb_driver;
// HACK1: Send first an ACK as Abort command, to reset chip before talking to it:
pn53x_usb_ack (pnd);
// HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device
// in case host used set_configuration and expects the device to have reset its toggle bit, which PN53x doesn't do
#if 1
if (!pn53x_init (pnd)) {
usb_close (data.pudh);
goto error;
}
#else
byte_t abtTx[] = { 0x00, 0x00, 0xff, 0x02, 0xfe, 0xd4, 0x02, 0x2a, 0x00 };
byte_t abtRx[BUFFER_LENGTH];
int ret;
#ifdef DEBUG
PRINT_HEX ("TX", abtTx, sizeof(abtTx));
#endif
ret = usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) abtTx, sizeof(abtTx), USB_TIMEOUT);
ret = pn53x_usb_bulk_write (data, abtTx, sizeof(abtTx));
if (ret < 0) {
DBG ("usb_bulk_write failed with error %d", ret);
usb_close (us.pudh);
usb_close (data.pudh);
// we failed to use the specified device
return NULL;
goto error;
}
ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
ret = pn53x_usb_bulk_read (data, (char *) abtRx, s);
if (ret < 0) {
DBG ("usb_bulk_read failed with error %d", ret);
usb_close (us.pudh);
// we failed to use the specified device
return NULL;
goto error;
}
#ifdef DEBUG
PRINT_HEX ("RX", abtRx, ret);
#endif
if (ret == 6) { // we got the ACK/NACK properly
if (!pn53x_check_ack_frame_callback (pnd, abtRx, ret)) {
DBG ("usb_bulk_read failed getting ACK");
if (!pn53x_check_ack_frame (pnd, abtRx, ret)) {
DBG ("pn53x_check_ack_frame failed");
usb_close (us.pudh);
// we failed to use the specified device
return NULL;
goto error;
}
ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
ret = pn53x_usb_bulk_read (data, (char *) abtRx, BUFFER_LENGTH);
if (ret < 0) {
DBG ("usb_bulk_read failed with error %d", ret);
usb_close (us.pudh);
// we failed to use the specified device
return NULL;
goto error;
}
#ifdef DEBUG
PRINT_HEX ("RX", abtRx, ret);
#endif
}
#endif
return pnd;
}
}
}
// We ran out of devices before the index required
DBG ("%s", "Device index not found!");
return NULL;
error:
// Free allocated structure on error.
free(pnd->driver_data);
free(pnd->chip_data);
free(pnd);
return NULL;
}
void
pn53x_usb_disconnect (nfc_device_t * pnd)
{
usb_spec_t *pus = (usb_spec_t *) pnd->nds;
int ret;
int res;
pn53x_usb_ack (pnd);
if ((ret = usb_release_interface (pus->pudh, 0)) < 0) {
ERR ("usb_release_interface failed (%i)", ret);
if ((res = usb_release_interface (DRIVER_DATA (pnd)->pudh, 0)) < 0) {
ERR ("usb_release_interface failed (%i)", res);
}
if ((ret = usb_close (pus->pudh)) < 0) {
ERR ("usb_close failed (%i)", ret);
if ((res = usb_close (DRIVER_DATA (pnd)->pudh)) < 0) {
ERR ("usb_close failed (%i)", res);
}
/*
if((ret = usb_reset(pus->pudh)) < 0) {
ERR("usb_reset failed (%i, if errno: %s)",ret, strerror(-ret));
}
*/
free (pnd->nds);
free (pnd);
free(pnd->driver_data);
free(pnd->chip_data);
free(pnd);
}
#define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD)
bool
pn53x_usb_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx)
pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
{
size_t uiPos = 0;
int ret = 0;
byte_t abtTx[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
byte_t abtRx[BUFFER_LENGTH];
usb_spec_t *pus = (usb_spec_t *) pnd->nds;
// TODO: Move this one level up for libnfc-1.6
uint8_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
byte_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
pnd->iLastCommand = pbtData[0];
size_t szFrame = 0;
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
abtTx[3] = szTx;
// Packet length checksum
abtTx[4] = 0x0100 - abtTx[3];
// Copy the PN53X command into the packet abtTx
memmove (abtTx + 5, pbtTx, szTx);
pn53x_build_frame (abtFrame, &szFrame, pbtData, szData);
// Calculate data payload checksum
abtTx[szTx + 5] = 0;
for (uiPos = 0; uiPos < szTx; uiPos++) {
abtTx[szTx + 5] -= abtTx[uiPos + 5];
}
// End of stream marker
abtTx[szTx + 6] = 0;
#ifdef DEBUG
PRINT_HEX ("TX", abtTx, szTx + 7);
#endif
ret = usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) abtTx, szTx + 7, USB_TIMEOUT);
int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame);
// HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details
if ((ret % pus->wMaxPacketSize) == 0) {
usb_bulk_write (pus->pudh, pus->uiEndPointOut, "\0", 0, USB_TIMEOUT);
if ((res % DRIVER_DATA (pnd)->uiMaxPacketSize) == 0) {
usb_bulk_write (DRIVER_DATA (pnd)->pudh, DRIVER_DATA(pnd)->uiEndPointOut, "\0", 0, USB_TIMEOUT);
}
if (ret < 0) {
DBG ("usb_bulk_write failed with error %d", ret);
if (res < 0) {
DBG ("usb_bulk_write failed with error %d", res);
pnd->iLastError = DEIO;
return false;
}
ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
DBG ("usb_bulk_read failed with error %d", ret);
byte_t abtRxBuf[6];
res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf));
if (res < 0) {
DBG ("usb_bulk_read failed with error %d", res);
pnd->iLastError = DEIO;
// try to interrupt current device state
pn53x_usb_ack(pnd);
return false;
}
#ifdef DEBUG
PRINT_HEX ("RX", abtRx, ret);
#endif
if (!pn53x_check_ack_frame_callback (pnd, abtRx, ret))
return false;
ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
DBG ("usb_bulk_read failed with error %d", ret);
pnd->iLastError = DEIO;
// try to interrupt current device state
pn53x_usb_ack(pnd);
if (pn53x_check_ack_frame (pnd, abtRxBuf, res)) {
((struct pn53x_data*)(pnd->chip_data))->state = EXECUTE;
} else {
return false;
}
#ifdef DEBUG
PRINT_HEX ("RX", abtRx, ret);
#endif
#ifdef DEBUG
PRINT_HEX ("TX", ack_frame, 6);
#endif
usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) ack_frame, 6, USB_TIMEOUT);
if (!pn53x_check_error_frame_callback (pnd, abtRx, ret))
return false;
// When the answer should be ignored, just return a succesful result
if (pbtRx == NULL || pszRx == NULL)
return true;
// Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
if (ret < 9) {
DBG ("%s", "No data");
pnd->iLastError = DEINVAL;
return false;
}
// Remove the preceding and appending bytes 00 00 FF xx Fx .. .. .. xx 00 (x = variable)
*pszRx = ret - 7 - 2;
memcpy (pbtRx, abtRx + 7, *pszRx);
return true;
}
void
int
pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen)
{
size_t len;
off_t offset = 0;
int abort_fd = 0;
switch (pnd->iLastCommand) {
case InAutoPoll:
case TgInitAsTarget:
case TgGetData:
abort_fd = pnd->iAbortFds[1];
break;
default:
break;
}
byte_t abtRxBuf[PN53X_USB_BUFFER_LEN];
int res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf));
if (res < 0) {
DBG ("usb_bulk_read failed with error %d", res);
pnd->iLastError = DEIO;
// try to interrupt current device state
pn53x_usb_ack(pnd);
return false;
}
pn53x_usb_ack (pnd);
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
ERR ("%s", "Frame preamble+start code mismatch");
pnd->iLastError = DEIO;
return -1;
}
offset += 3;
if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
// Error frame
ERR ("%s", "Application level error detected");
pnd->iLastError = DEISERRFRAME;
return -1;
} else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
// Extended frame
// FIXME: Code this
abort ();
offset += 3;
} else {
// Normal frame
if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) {
// TODO: Retry
ERR ("%s", "Length checksum mismatch");
pnd->iLastError = DEIO;
return -1;
}
// abtRxBuf[3] (LEN) include TFI + (CC+1)
len = abtRxBuf[offset] - 2;
offset += 2;
}
if (len > szDataLen) {
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
pnd->iLastError = DEIO;
return -1;
}
// TFI + PD0 (CC+1)
if (abtRxBuf[offset] != 0xD5) {
ERR ("%s", "TFI Mismatch");
pnd->iLastError = DEIO;
return -1;
}
offset += 1;
if (abtRxBuf[offset] != pnd->iLastCommand + 1) {
ERR ("%s", "Command Code verification failed");
pnd->iLastError = DEIO;
return -1;
}
offset += 1;
memcpy (pbtData, abtRxBuf + offset, len);
offset += len;
byte_t btDCS = (256 - 0xD5);
btDCS -= pnd->iLastCommand + 1;
for (size_t szPos = 0; szPos < len; szPos++) {
btDCS -= pbtData[szPos];
}
if (btDCS != abtRxBuf[offset]) {
ERR ("%s", "Data checksum mismatch");
pnd->iLastError = DEIO;
return -1;
}
offset += 1;
if (0x00 != abtRxBuf[offset]) {
ERR ("%s", "Frame postamble mismatch");
pnd->iLastError = DEIO;
return -1;
}
CHIP_DATA (pnd)->state = NORMAL;
return len;
}
int
pn53x_usb_ack (nfc_device_t * pnd)
{
usb_spec_t *pus = (usb_spec_t *) pnd->nds;
#ifdef DEBUG
PRINT_HEX ("TX", ack_frame, sizeof (ack_frame));
#endif
usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) ack_frame, sizeof (ack_frame), USB_TIMEOUT);
return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *) ack_frame, sizeof (ack_frame));
}
bool
pn53x_usb_initiator_init (nfc_device_t *pnd)
{
if (!pn53x_initiator_init (pnd))
return false;
if (ASK_LOGO == DRIVER_DATA (pnd)->model) {
DBG ("ASK LoGO initialization.");
pn53x_write_register (pnd, 0x6106, 0xFF, 0x1B);
pn53x_write_register (pnd, 0x6306, 0xFF, 0x14);
pn53x_write_register (pnd, 0xFFFD, 0xFF, 0x37);
pn53x_write_register (pnd, 0xFFB0, 0xFF, 0x3B);
}
return true;
}
const struct pn53x_io pn53x_usb_io = {
.send = pn53x_usb_send,
.receive = pn53x_usb_receive,
};
const struct nfc_driver_t pn53x_usb_driver = {
.name = PN53X_USB_DRIVER_NAME,
.probe = pn53x_usb_probe,
.connect = pn53x_usb_connect,
.disconnect = pn53x_usb_disconnect,
.strerror = pn53x_strerror,
.initiator_init = pn53x_usb_initiator_init,
.initiator_select_passive_target = pn53x_initiator_select_passive_target,
.initiator_poll_targets = pn53x_initiator_poll_targets,
.initiator_select_dep_target = pn53x_initiator_select_dep_target,
.initiator_deselect_target = pn53x_initiator_deselect_target,
.initiator_transceive_bytes = pn53x_initiator_transceive_bytes,
.initiator_transceive_bits = pn53x_initiator_transceive_bits,
.target_init = pn53x_target_init,
.target_send_bytes = pn53x_target_send_bytes,
.target_receive_bytes = pn53x_target_receive_bytes,
.target_send_bits = pn53x_target_send_bits,
.target_receive_bits = pn53x_target_receive_bits,
.configure = pn53x_configure,
};

View file

@ -18,29 +18,20 @@
*
*
* @file pn53x_usb.h
* @brief
* @brief Drive for PN53x USB devices
*/
#include <usb.h>
#ifndef __NFC_DRIVER_PN53X_USB_H__
# define __NFC_DRIVER_PN53X_USB_H__
typedef struct {
uint16_t idVendor;
uint16_t idProduct;
} usb_candidate_t;
# include <nfc/nfc-types.h>
typedef struct {
usb_dev_handle *pudh;
usb_candidate_t uc;
uint32_t uiEndPointIn;
uint32_t uiEndPointOut;
uint32_t wMaxPacketSize;
} usb_spec_t;
void get_end_points (struct usb_device *dev, usb_spec_t * pus);
bool pn53x_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound,
usb_candidate_t candidates[], int num_candidates, char *target_name);
nfc_device_t *pn53x_usb_connect (const nfc_device_desc_t * pndd, const char *target_name, int target_chip);
bool pn53x_usb_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx,
size_t * pszRx);
bool pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_t *pn53x_usb_connect (const nfc_device_desc_t * pndd);
bool pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData);
int pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData);
void pn53x_usb_disconnect (nfc_device_t * pnd);
extern const struct nfc_driver_t pn53x_usb_driver;
#endif // ! __NFC_DRIVER_PN53X_USB_H__

View file

@ -51,6 +51,9 @@
nfc_device_desc_t *nfc_pick_device (void);
const struct nfc_driver_t *nfc_drivers[] = {
# if defined (DRIVER_PN53X_USB_ENABLED)
&pn53x_usb_driver,
# endif /* DRIVER_PN53X_USB_ENABLED */
# if defined (DRIVER_PN532_UART_ENABLED)
&pn532_uart_driver,
# endif /* DRIVER_PN532_UART_ENABLED */

View file

@ -4,7 +4,7 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
[
AC_MSG_CHECKING(which drivers to build)
AC_ARG_WITH(drivers,
AC_HELP_STRING([--with-drivers=DRIVERS], [Use a custom driver set, where DRIVERS is a coma-separated list of drivers to build support for. Available drivers are: 'acr122', 'arygon', 'pn531_usb', 'pn533_usb' and 'pn532_uart'. Default drivers set is 'acr122,arygon,pn531_usb,pn533_usb'. The special driver set 'all' compile all available drivers.]),
AC_HELP_STRING([--with-drivers=DRIVERS], [Use a custom driver set, where DRIVERS is a coma-separated list of drivers to build support for. Available drivers are: 'acr122', 'arygon', 'pn532_uart' and 'pn53x_usb'. Default drivers set is 'acr122,arygon,pn53x_usb'. The special driver set 'all' compile all available drivers.]),
[ case "${withval}" in
yes | no)
dnl ignore calls without any arguments
@ -25,18 +25,17 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
case "${DRIVER_BUILD_LIST}" in
default)
DRIVER_BUILD_LIST="acr122 arygon pn531_usb pn533_usb"
DRIVER_BUILD_LIST="acr122 arygon pn53x_usb"
;;
all)
DRIVER_BUILD_LIST="acr122 arygon pn531_usb pn533_usb pn532_uart"
DRIVER_BUILD_LIST="acr122 arygon pn53x_usb pn532_uart"
;;
esac
DRIVERS_CFLAGS=""
driver_acr122_enabled="no"
driver_pn531_usb_enabled="no"
driver_pn533_usb_enabled="no"
driver_pn53x_usb_enabled="no"
driver_arygon_enabled="no"
driver_pn532_uart_enabled="no"
@ -48,15 +47,10 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
driver_acr122_enabled="yes"
DRIVERS_CFLAGS="$DRIVERS_CFLAGS -DDRIVER_ACR122_ENABLED"
;;
pn531_usb)
pn53x_usb)
libusb_required="yes"
driver_pn531_usb_enabled="yes"
DRIVERS_CFLAGS="$DRIVERS_CFLAGS -DDRIVER_PN531_USB_ENABLED"
;;
pn533_usb)
libusb_required="yes"
driver_pn533_usb_enabled="yes"
DRIVERS_CFLAGS="$DRIVERS_CFLAGS -DDRIVER_PN533_USB_ENABLED"
driver_pn53x_usb_enabled="yes"
DRIVERS_CFLAGS="$DRIVERS_CFLAGS -DDRIVER_PN53X_USB_ENABLED"
;;
arygon)
driver_arygon_enabled="yes"
@ -73,8 +67,7 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
done
AC_SUBST(DRIVERS_CFLAGS)
AM_CONDITIONAL(DRIVER_ACR122_ENABLED, [test x"$driver_acr122_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN531_USB_ENABLED, [test x"$driver_pn531_usb_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN533_USB_ENABLED, [test x"$driver_pn533_usb_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN53X_USB_ENABLED, [test x"$driver_pn53x_usb_enabled" = xyes])
AM_CONDITIONAL(DRIVER_ARYGON_ENABLED, [test x"$driver_arygon_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN532_UART_ENABLED, [test x"$driver_pn532_uart_enabled" = xyes])
])
@ -84,7 +77,6 @@ echo
echo "Selected drivers:"
echo " acr122........... $driver_acr122_enabled"
echo " arygon........... $driver_arygon_enabled"
echo " pn531_usb........ $driver_pn531_usb_enabled"
echo " pn53x_usb........ $driver_pn53x_usb_enabled"
echo " pn532_uart....... $driver_pn532_uart_enabled"
echo " pn533_usb........ $driver_pn533_usb_enabled"
])

View file

@ -1,7 +1,7 @@
#include <cutter.h>
#include <nfc/nfc.h>
#include "libnfc/chips/pn53x.h"
#include "chips/pn53x.h"
#define MAX_DEVICE_COUNT 1
#define MAX_TARGET_COUNT 1

View file

@ -5,7 +5,7 @@
#define MAX_DEVICE_COUNT 1
#define MAX_TARGET_COUNT 1
#include "libnfc/chips/pn53x.h"
#include "chips/pn53x.h"
void
test_register_endianness (void)