astyle --formatted --mode=c --indent=spaces=2 --indent-switches --indent-preprocessor --keep-one-line-blocks --max-instatement-indent=60 --brackets=linux --pad-oper --unpad-paren --pad-header

This commit is contained in:
Philippe Teuwen 2012-05-29 15:54:36 +00:00
parent 562205cc14
commit 01303fab0d
59 changed files with 3178 additions and 3178 deletions

View file

@ -86,34 +86,34 @@ struct pn53x_usb_data {
};
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_init (nfc_device *pnd);
bool pn53x_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len);
int pn53x_usb_init(nfc_device *pnd);
static int
pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, const int timeout)
pn53x_usb_bulk_read(struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, const int timeout)
{
int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout);
int res = usb_bulk_read(data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout);
if (res > 0) {
LOG_HEX ("RX", abtRx, res);
LOG_HEX("RX", abtRx, res);
} else if (res < 0) {
if (res != -USB_TIMEDOUT)
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror (res));
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res));
}
return res;
}
static int
pn53x_usb_bulk_write (struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout)
pn53x_usb_bulk_write(struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout)
{
LOG_HEX ("TX", abtTx, szTx);
int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout);
LOG_HEX("TX", abtTx, szTx);
int res = usb_bulk_write(data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout);
if (res > 0) {
// HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details
if ((res % data->uiMaxPacketSize) == 0) {
usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, timeout);
usb_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout);
}
} else {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror (res));
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res));
}
return res;
}
@ -135,9 +135,9 @@ const struct pn53x_usb_supported_device pn53x_usb_supported_devices[] = {
};
static pn53x_usb_model
pn53x_usb_get_device_model (uint16_t vendor_id, uint16_t product_id)
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++) {
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;
@ -146,11 +146,11 @@ pn53x_usb_get_device_model (uint16_t vendor_id, uint16_t product_id)
return UNKNOWN;
}
int pn53x_usb_ack (nfc_device *pnd);
int pn53x_usb_ack(nfc_device *pnd);
// Find transfer endpoints for bulk transfers
static void
pn53x_usb_get_end_points (struct usb_device *dev, struct pn53x_usb_data *data)
pn53x_usb_get_end_points(struct usb_device *dev, struct pn53x_usb_data *data)
{
uint32_t uiIndex;
uint32_t uiEndPoint;
@ -179,23 +179,23 @@ pn53x_usb_get_end_points (struct usb_device *dev, struct pn53x_usb_data *data)
}
bool
pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound)
pn53x_usb_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound)
{
usb_init ();
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 ((res = usb_find_busses () < 0)) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror (res));
if ((res = usb_find_busses() < 0)) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res));
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 ((res = usb_find_devices () < 0)) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror (res));
if ((res = usb_find_devices() < 0)) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res));
return false;
}
@ -203,11 +203,11 @@ pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *p
uint32_t uiBusIndex = 0;
struct usb_bus *bus;
for (bus = usb_get_busses (); bus; bus = bus->next) {
for (bus = usb_get_busses(); bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) {
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)) {
// Make sure there are 2 endpoints available
@ -221,21 +221,21 @@ pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *p
continue;
}
usb_dev_handle *udev = usb_open (dev);
usb_dev_handle *udev = usb_open(dev);
// Set configuration
res = usb_set_configuration (udev, 1);
res = usb_set_configuration(udev, 1);
if (res < 0) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res));
usb_close (udev);
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res));
usb_close(udev);
// we failed to use the device
continue;
}
// pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[*pszDeviceFound].acDevice, sizeof (pnddDevices[*pszDeviceFound].acDevice));
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s", bus->dirname, dev->filename);
usb_close (udev);
snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename);
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s", bus->dirname, dev->filename);
usb_close(udev);
snprintf(connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename);
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
if ((*pszDeviceFound) == connstrings_len) {
@ -255,51 +255,51 @@ struct pn53x_usb_descriptor {
};
static int
pn53x_usb_connstring_decode (const nfc_connstring connstring, struct pn53x_usb_descriptor *desc)
pn53x_usb_connstring_decode(const nfc_connstring connstring, struct pn53x_usb_descriptor *desc)
{
int n = strlen (connstring) + 1;
char *driver_name = malloc (n);
char *dirname = malloc (n);
char *filename = malloc (n);
int n = strlen(connstring) + 1;
char *driver_name = malloc(n);
char *dirname = malloc(n);
char *filename = malloc(n);
driver_name[0] = '\0';
int res = sscanf (connstring, "%[^:]:%[^:]:%[^:]", driver_name, dirname, filename);
int res = sscanf(connstring, "%[^:]:%[^:]:%[^:]", driver_name, dirname, filename);
if (!res || (0 != strcmp (driver_name, PN53X_USB_DRIVER_NAME))) {
if (!res || (0 != strcmp(driver_name, PN53X_USB_DRIVER_NAME))) {
// Driver name does not match.
res = 0;
} else {
desc->dirname = strdup (dirname);
desc->filename = strdup (filename);
desc->dirname = strdup(dirname);
desc->filename = strdup(filename);
}
free (driver_name);
free (dirname);
free (filename);
free(driver_name);
free(dirname);
free(filename);
return res;
}
bool
pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len)
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));
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++) {
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);
strncpy(buffer, pn53x_usb_supported_devices[n].name, len);
return true;
}
}
@ -309,12 +309,12 @@ pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, cha
}
nfc_device *
pn53x_usb_open (const nfc_connstring connstring)
pn53x_usb_open(const nfc_connstring connstring)
{
nfc_device *pnd = NULL;
struct pn53x_usb_descriptor desc = { NULL, NULL };
int connstring_decode_level = pn53x_usb_connstring_decode (connstring, &desc);
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring);
int connstring_decode_level = pn53x_usb_connstring_decode(connstring, &desc);
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring);
if (connstring_decode_level < 1) {
goto free_mem;
}
@ -327,102 +327,102 @@ pn53x_usb_open (const nfc_connstring connstring)
struct usb_bus *bus;
struct usb_device *dev;
usb_init ();
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 ((res = usb_find_busses () < 0)) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror (res));
if ((res = usb_find_busses() < 0)) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res));
goto free_mem;
}
// 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 ((res = usb_find_devices () < 0)) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror (res));
if ((res = usb_find_devices() < 0)) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res));
goto free_mem;
}
for (bus = usb_get_busses (); bus; bus = bus->next) {
for (bus = usb_get_busses(); bus; bus = bus->next) {
if (connstring_decode_level > 1) {
// A specific bus have been specified
if (0 != strcmp (bus->dirname, desc.dirname))
if (0 != strcmp(bus->dirname, desc.dirname))
continue;
}
for (dev = bus->devices; dev; dev = dev->next) {
if (connstring_decode_level > 2) {
// A specific dev have been specified
if (0 != strcmp (dev->filename, desc.filename))
if (0 != strcmp(dev->filename, desc.filename))
continue;
}
// Open the USB device
data.pudh = usb_open (dev);
data.pudh = usb_open(dev);
// Retrieve end points
pn53x_usb_get_end_points (dev, &data);
pn53x_usb_get_end_points(dev, &data);
// Set configuration
res = usb_set_configuration (data.pudh, 1);
res = usb_set_configuration(data.pudh, 1);
if (res < 0) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res));
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res));
if (EPERM == -res) {
log_put (LOG_CATEGORY, NFC_PRIORITY_WARN, "Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
log_put(LOG_CATEGORY, NFC_PRIORITY_WARN, "Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
}
usb_close (data.pudh);
usb_close(data.pudh);
// we failed to use the specified device
goto free_mem;
}
res = usb_claim_interface (data.pudh, 0);
res = usb_claim_interface(data.pudh, 0);
if (res < 0) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror (res));
usb_close (data.pudh);
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res));
usb_close(data.pudh);
// we failed to use the specified device
goto free_mem;
}
data.model = pn53x_usb_get_device_model (dev->descriptor.idVendor, 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
pnd = nfc_device_new (connstring);
pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->name, sizeof (pnd->name));
pnd = nfc_device_new(connstring);
pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name));
pnd->driver_data = malloc(sizeof(struct pn53x_usb_data));
*DRIVER_DATA (pnd) = data;
*DRIVER_DATA(pnd) = data;
// Alloc and init chip's data
pn53x_data_new (pnd, &pn53x_usb_io);
pn53x_data_new(pnd, &pn53x_usb_io);
switch (DRIVER_DATA (pnd)->model) {
switch (DRIVER_DATA(pnd)->model) {
// empirical tuning
case ASK_LOGO:
CHIP_DATA (pnd)->timer_correction = 50;
CHIP_DATA(pnd)->timer_correction = 50;
break;
case SCM_SCL3711:
case NXP_PN533:
CHIP_DATA (pnd)->timer_correction = 46;
CHIP_DATA(pnd)->timer_correction = 46;
break;
case NXP_PN531:
CHIP_DATA (pnd)->timer_correction = 50;
CHIP_DATA(pnd)->timer_correction = 50;
break;
case SONY_PN531:
CHIP_DATA (pnd)->timer_correction = 54;
CHIP_DATA(pnd)->timer_correction = 54;
break;
case SONY_RCS360:
case UNKNOWN:
CHIP_DATA (pnd)->timer_correction = 0; // TODO: allow user to know if timed functions are available
CHIP_DATA(pnd)->timer_correction = 0; // TODO: allow user to know if timed functions are available
break;
}
pnd->driver = &pn53x_usb_driver;
// HACK1: Send first an ACK as Abort command, to reset chip before talking to it:
pn53x_usb_ack (pnd);
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 (pn53x_usb_init (pnd) < 0) {
usb_close (data.pudh);
if (pn53x_usb_init(pnd) < 0) {
usb_close(data.pudh);
goto error;
}
DRIVER_DATA (pnd)->abort_flag = false;
DRIVER_DATA(pnd)->abort_flag = false;
goto free_mem;
}
}
@ -431,64 +431,64 @@ pn53x_usb_open (const nfc_connstring connstring)
error:
// Free allocated structure on error.
nfc_device_free (pnd);
nfc_device_free(pnd);
pnd = NULL;
free_mem:
free (desc.dirname);
free (desc.filename);
free(desc.dirname);
free(desc.filename);
return pnd;
}
void
pn53x_usb_close (nfc_device *pnd)
pn53x_usb_close(nfc_device *pnd)
{
pn53x_usb_ack (pnd);
pn53x_usb_ack(pnd);
pn53x_idle (pnd);
pn53x_idle(pnd);
if (DRIVER_DATA (pnd)->model == ASK_LOGO) {
if (DRIVER_DATA(pnd)->model == ASK_LOGO) {
/* Set P30, P31, P32, P33, P35 to logic 1 and P34 to 0 logic */
/* ie. Switch all LEDs off and turn off progressive field */
pn53x_write_register (pnd, PN53X_SFR_P3, 0xFF, _BV (P30) | _BV (P31) | _BV (P32) | _BV (P33) | _BV (P35));
pn53x_write_register(pnd, PN53X_SFR_P3, 0xFF, _BV(P30) | _BV(P31) | _BV(P32) | _BV(P33) | _BV(P35));
}
int res;
if ((res = usb_release_interface (DRIVER_DATA (pnd)->pudh, 0)) < 0) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror (res));
if ((res = usb_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res));
}
if ((res = usb_close (DRIVER_DATA (pnd)->pudh)) < 0) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror (res));
if ((res = usb_close(DRIVER_DATA(pnd)->pudh)) < 0) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res));
}
pn53x_data_free (pnd);
nfc_device_free (pnd);
pn53x_data_free(pnd);
nfc_device_free(pnd);
}
#define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD)
int
pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, const int timeout)
pn53x_usb_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, const int timeout)
{
uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
size_t szFrame = 0;
int res = 0;
pn53x_build_frame (abtFrame, &szFrame, pbtData, szData);
pn53x_build_frame(abtFrame, &szFrame, pbtData, szData);
if ((res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout)) < 0) {
if ((res = pn53x_usb_bulk_write(DRIVER_DATA(pnd), abtFrame, szFrame, timeout)) < 0) {
pnd->last_error = res;
return pnd->last_error;
}
uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN];
if ((res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout)) < 0) {
if ((res = pn53x_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), timeout)) < 0) {
// try to interrupt current device state
pn53x_usb_ack(pnd);
pnd->last_error = res;
return pnd->last_error;
}
if (pn53x_check_ack_frame (pnd, abtRxBuf, res) == 0) {
if (pn53x_check_ack_frame(pnd, abtRxBuf, res) == 0) {
// The PN53x is running the sent command
} else {
// For some reasons (eg. send another command while a previous one is
@ -498,7 +498,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co
// pn53x_usb_receive()) will be able to retreive the correct response
// packet.
// FIXME Sony reader is also affected by this bug but NACK is not supported
if ((res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout)) < 0) {
if ((res = pn53x_usb_bulk_write(DRIVER_DATA(pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout)) < 0) {
pnd->last_error = res;
// try to interrupt current device state
pn53x_usb_ack(pnd);
@ -511,7 +511,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co
#define USB_TIMEOUT_PER_PASS 200
int
pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const int timeout)
pn53x_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const int timeout)
{
size_t len;
off_t offset = 0;
@ -539,12 +539,12 @@ read:
}
}
res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), usb_timeout);
res = pn53x_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), usb_timeout);
if (res == -USB_TIMEDOUT) {
if (DRIVER_DATA (pnd)->abort_flag) {
DRIVER_DATA (pnd)->abort_flag = false;
pn53x_usb_ack (pnd);
if (DRIVER_DATA(pnd)->abort_flag) {
DRIVER_DATA(pnd)->abort_flag = false;
pn53x_usb_ack(pnd);
pnd->last_error = NFC_EOPABORTED;
return pnd->last_error;
} else {
@ -560,8 +560,8 @@ read:
}
const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch");
if (0 != (memcmp(abtRxBuf, pn53x_preamble, 3))) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
@ -569,7 +569,7 @@ read:
if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
// Error frame
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected");
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected");
pnd->last_error = NFC_EIO;
return pnd->last_error;
} else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
@ -580,7 +580,7 @@ read:
len = (abtRxBuf[offset] << 8) + abtRxBuf[offset + 1] - 2;
if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) {
// TODO: Retry
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
@ -589,7 +589,7 @@ read:
// Normal frame
if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) {
// TODO: Retry
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
@ -600,44 +600,44 @@ read:
}
if (len > szDataLen) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
// TFI + PD0 (CC+1)
if (abtRxBuf[offset] != 0xD5) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch");
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
offset += 1;
if (abtRxBuf[offset] != CHIP_DATA (pnd)->last_command + 1) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed");
if (abtRxBuf[offset] != CHIP_DATA(pnd)->last_command + 1) {
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
offset += 1;
memcpy (pbtData, abtRxBuf + offset, len);
memcpy(pbtData, abtRxBuf + offset, len);
offset += len;
uint8_t btDCS = (256 - 0xD5);
btDCS -= CHIP_DATA (pnd)->last_command + 1;
btDCS -= CHIP_DATA(pnd)->last_command + 1;
for (size_t szPos = 0; szPos < len; szPos++) {
btDCS -= pbtData[szPos];
}
if (btDCS != abtRxBuf[offset]) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch");
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
offset += 1;
if (0x00 != abtRxBuf[offset]) {
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch");
log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch");
pnd->last_error = NFC_EIO;
return pnd->last_error;
}
@ -647,42 +647,42 @@ read:
}
int
pn53x_usb_ack (nfc_device *pnd)
pn53x_usb_ack(nfc_device *pnd)
{
return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), 0);
return pn53x_usb_bulk_write(DRIVER_DATA(pnd), (uint8_t *) pn53x_ack_frame, sizeof(pn53x_ack_frame), 0);
}
int
pn53x_usb_init (nfc_device *pnd)
pn53x_usb_init(nfc_device *pnd)
{
int res = 0;
// Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one...
//pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead:
const uint8_t abtCmd[] = { GetFirmwareVersion };
pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, 0);
pn53x_transceive(pnd, abtCmd, sizeof(abtCmd), NULL, 0, 0);
// ...and we don't care about error
pnd->last_error = 0;
if (SONY_RCS360 == DRIVER_DATA (pnd)->model) {
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "SONY RC-S360 initialization.");
if (SONY_RCS360 == DRIVER_DATA(pnd)->model) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "SONY RC-S360 initialization.");
const uint8_t abtCmd2[] = { 0x18, 0x01 };
pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, 0, 0);
pn53x_usb_ack (pnd);
pn53x_transceive(pnd, abtCmd2, sizeof(abtCmd2), NULL, 0, 0);
pn53x_usb_ack(pnd);
}
if ((res = pn53x_init (pnd)) < 0)
if ((res = pn53x_init(pnd)) < 0)
return res;
if (ASK_LOGO == DRIVER_DATA (pnd)->model) {
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "ASK LoGO initialization.");
if (ASK_LOGO == DRIVER_DATA(pnd)->model) {
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "ASK LoGO initialization.");
/* Internal registers */
/* Disable 100mA current limit, Power on Secure IC (SVDD) */
pn53x_write_register (pnd, PN53X_REG_Control_switch_rng, 0xFF, SYMBOL_CURLIMOFF | SYMBOL_SIC_SWITCH_EN | SYMBOL_RANDOM_DATAREADY);
pn53x_write_register(pnd, PN53X_REG_Control_switch_rng, 0xFF, SYMBOL_CURLIMOFF | SYMBOL_SIC_SWITCH_EN | SYMBOL_RANDOM_DATAREADY);
/* Select the signal to be output on SIGOUT: Modulation signal (envelope) from the internal coder */
pn53x_write_register (pnd, PN53X_REG_CIU_TxSel, 0xFF, 0x14);
pn53x_write_register(pnd, PN53X_REG_CIU_TxSel, 0xFF, 0x14);
/* SFR Registers */
/* Setup push-pulls for pins from P30 to P35 */
pn53x_write_register (pnd, PN53X_SFR_P3CFGB, 0xFF, 0x37);
pn53x_write_register(pnd, PN53X_SFR_P3CFGB, 0xFF, 0x37);
/*
On ASK LoGO hardware:
@ -704,32 +704,32 @@ pn53x_usb_init (nfc_device *pnd)
/* Set P30, P31, P33, P35 to logic 1 and P32, P34 to 0 logic */
/* ie. Switch LED1 on and turn off progressive field */
pn53x_write_register (pnd, PN53X_SFR_P3, 0xFF, _BV (P30) | _BV (P31) | _BV (P33) | _BV (P35));
pn53x_write_register(pnd, PN53X_SFR_P3, 0xFF, _BV(P30) | _BV(P31) | _BV(P33) | _BV(P35));
}
return NFC_SUCCESS;
}
static int
pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable)
pn53x_usb_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
{
int res = 0;
if ((res = pn53x_set_property_bool (pnd, property, bEnable)) < 0)
if ((res = pn53x_set_property_bool(pnd, property, bEnable)) < 0)
return res;
switch (DRIVER_DATA (pnd)->model) {
switch (DRIVER_DATA(pnd)->model) {
case ASK_LOGO:
if (NP_ACTIVATE_FIELD == property) {
/* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off");
if ((res = pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) < 0)
log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off");
if ((res = pn53x_write_register(pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV(P34) : _BV(P31))) < 0)
return NFC_ECHIP;
}
break;
case SCM_SCL3711:
if (NP_ACTIVATE_FIELD == property) {
// Switch on/off LED according to ACTIVATE_FIELD option
if ((res = pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) < 0)
if ((res = pn53x_write_register(pnd, PN53X_SFR_P3, _BV(P32), bEnable ? 0 : _BV(P32))) < 0)
return res;
}
break;
@ -745,9 +745,9 @@ pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const
}
static int
pn53x_usb_abort_command (nfc_device *pnd)
pn53x_usb_abort_command(nfc_device *pnd)
{
DRIVER_DATA (pnd)->abort_flag = true;
DRIVER_DATA(pnd)->abort_flag = true;
return NFC_SUCCESS;
}