diff --git a/manualmake/linux/Makefile b/manualmake/linux/Makefile index ee677a9..44ed7f5 100644 --- a/manualmake/linux/Makefile +++ b/manualmake/linux/Makefile @@ -24,7 +24,7 @@ LD = gcc CFLAGS = -fPIC -Wall -O4 $(LIBPCSC_HEADERS) $(LIBUSB_HEADERS) LDFLAGS = -fPIC -Wall -O4 -OBJS = dev_pn531.o dev_acr122.o bitutils.o libnfc.o +OBJS = dev_pn531.o dev_pn533.o dev_acr122.o dev_arygon.o bitutils.o libnfc.o rs232.o HEADERS = devices.h bitutils.h defines.h libnfc.h LIBNFC = libnfc.$(LIBNFC_TYPE) EXES = anticol emulate list mftool relay diff --git a/manualmake/macosx/Makefile b/manualmake/macosx/Makefile index 1806d0c..bb38c48 100644 --- a/manualmake/macosx/Makefile +++ b/manualmake/macosx/Makefile @@ -24,7 +24,7 @@ LD = gcc CFLAGS = -fPIC -Wall -O4 $(LIBPCSC_HEADERS) $(LIBUSB_HEADERS) LDFLAGS = -fPIC -Wall -O4 -OBJS = dev_pn531.o dev_acr122.o dev_arygon.o bitutils.o libnfc.o rs232.o +OBJS = dev_pn531.o dev_pn533.o dev_acr122.o dev_arygon.o bitutils.o libnfc.o rs232.o HEADERS = devices.h bitutils.h defines.h libnfc.h LIBNFC = libnfc.$(LIBNFC_TYPE) EXES = anticol emulate list mftool relay diff --git a/src/dev_pn531.c b/src/dev_pn531.c index 0a8a1b8..5d09b9d 100644 --- a/src/dev_pn531.c +++ b/src/dev_pn531.c @@ -39,7 +39,7 @@ typedef struct { } dev_spec_pn531; // Find transfer endpoints for bulk transfers -void get_end_points(struct usb_device *dev, dev_spec_pn531* pdsp) +static void get_end_points(struct usb_device *dev, dev_spec_pn531* pdsp) { ui32 uiIndex; ui32 uiEndPoint; @@ -78,6 +78,8 @@ dev_info* dev_pn531_connect(const ui32 uiIndex) { int idvendor = 0x04CC; int idproduct = 0x0531; + int idvendor_alt = 0x054c; + int idproduct_alt = 0x0193; struct usb_bus *bus; struct usb_device *dev; dev_info* pdi = INVALID_DEVICE_INFO; @@ -100,7 +102,8 @@ dev_info* dev_pn531_connect(const ui32 uiIndex) { for (dev = bus->devices; dev; dev = dev->next) { - if (idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct) + if ((idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct) || + (idvendor_alt==dev->descriptor.idVendor && idproduct_alt==dev->descriptor.idProduct)) { // Make sure there are 2 endpoints available if (dev->config->interface->altsetting->bNumEndpoints < 2) return pdi; diff --git a/src/dev_pn533.c b/src/dev_pn533.c new file mode 100644 index 0000000..9493994 --- /dev/null +++ b/src/dev_pn533.c @@ -0,0 +1,256 @@ +/* + +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 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 General Public License +along with this program. If not, see . + +Thanks to d18c7db and Okko for example code + +*/ + +#include "defines.h" + +#include +#include +#include +#include "dev_pn533.h" +#include "bitutils.h" + +#define BUFFER_LENGTH 256 +#define USB_TIMEOUT 30000 +static char buffer[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" + +typedef struct { + usb_dev_handle* pudh; + ui32 uiEndPointIn; + ui32 uiEndPointOut; +} dev_spec_pn533; + +// Find transfer endpoints for bulk transfers +static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp) +{ + ui32 uiIndex; + ui32 uiEndPoint; + struct usb_interface_descriptor* puid = dev->config->interface->altsetting; + + // 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out + for(uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) + { + // Only accept bulk transfer endpoints (ignore interrupt endpoints) + if(puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK) continue; + + // Copy the endpoint to a local var, makes it more readable code + uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress; + + // Test if we dealing with a bulk IN endpoint + if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) + { + #ifdef _LIBNFC_VERBOSE_ + printf("Bulk endpoint in : 0x%02X\n", uiEndPoint); + #endif + pdsp->uiEndPointIn = uiEndPoint; + } + + // Test if we dealing with a bulk OUT endpoint + if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) + { + #ifdef _LIBNFC_VERBOSE_ + printf("Bulk endpoint in : 0x%02X\n", uiEndPoint); + #endif + pdsp->uiEndPointOut = uiEndPoint; + } + } +} + +dev_info* dev_pn533_connect(const ui32 uiIndex) +{ + int idvendor = 0x04e6; + int idproduct = 0x5591; + struct usb_bus *bus; + struct usb_device *dev; + dev_info* pdi = INVALID_DEVICE_INFO; + dev_spec_pn533* pdsp; + dev_spec_pn533 dsp; + ui32 uiDevIndex; + + dsp.uiEndPointIn = 0; + dsp.uiEndPointOut = 0; + dsp.pudh = null; + + usb_init(); + if (usb_find_busses() < 0) return INVALID_DEVICE_INFO; + if (usb_find_devices() < 0) return INVALID_DEVICE_INFO; + + // Initialize the device index we are seaching for + uiDevIndex = uiIndex; + + for (bus = usb_get_busses(); bus; bus = bus->next) + { + for (dev = bus->devices; dev; dev = dev->next) + { + if (idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct) + { + // Make sure there are 2 endpoints available + if (dev->config->interface->altsetting->bNumEndpoints < 2) return pdi; + + // Test if we are looking for this device according to the current index + if (uiDevIndex != 0) + { + // Nope, we maybe want the next one, let's try to find another + uiDevIndex--; + continue; + } + #ifdef _LIBNFC_VERBOSE_ + printf("Found PN533 device\n"); + #endif + + // Open the PN533 USB device + dsp.pudh = usb_open(dev); + + get_end_points(dev,&dsp); + if(usb_set_configuration(dsp.pudh,1) < 0) + { + #ifdef _LIBNFC_VERBOSE_ + printf("Setting config failed\n"); + #endif + usb_close(dsp.pudh); + return INVALID_DEVICE_INFO; + } + + if(usb_claim_interface(dsp.pudh,0) < 0) + { + #ifdef _LIBNFC_VERBOSE_ + printf("Can't claim interface\n"); + #endif + usb_close(dsp.pudh); + return INVALID_DEVICE_INFO; + } + // Allocate memory for the device info and specification, fill it and return the info + pdsp = malloc(sizeof(dev_spec_pn533)); + *pdsp = dsp; + pdi = malloc(sizeof(dev_info)); + strcpy(pdi->acName,"PN533USB"); + pdi->ct = CT_PN533; + pdi->ds = (dev_spec)pdsp; + pdi->bActive = true; + pdi->bCrc = true; + pdi->bPar = true; + pdi->ui8TxBits = 0; + return pdi; + } + } + } + return pdi; +} + +void dev_pn533_disconnect(dev_info* pdi) +{ + dev_spec_pn533* pdsp = (dev_spec_pn533*)pdi->ds; + usb_release_interface(pdsp->pudh,0); + usb_close(pdsp->pudh); + free(pdi->ds); + free(pdi); +} + +bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen) +{ + ui32 uiPos = 0; + int ret = 0; + char buf[BUFFER_LENGTH]; + dev_spec_pn533* pdsp = (dev_spec_pn533*)ds; + + // Packet length = data length (len) + checksum (1) + end of stream marker (1) + buffer[3] = uiTxLen; + // Packet length checksum + buffer[4] = BUFFER_LENGTH - buffer[3]; + // Copy the PN53X command into the packet buffer + memmove(buffer+5,pbtTx,uiTxLen); + + // Calculate data payload checksum + buffer[uiTxLen+5] = 0; + for(uiPos=0; uiPos < uiTxLen; uiPos++) + { + buffer[uiTxLen+5] -= buffer[uiPos+5]; + } + + // End of stream marker + buffer[uiTxLen+6] = 0; + + #ifdef _LIBNFC_VERBOSE_ + printf("Tx: "); + print_hex((byte*)buffer,uiTxLen+7); + #endif + + ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, buffer, uiTxLen+7, USB_TIMEOUT); + if( ret < 0 ) + { + #ifdef _LIBNFC_VERBOSE_ + printf("usb_bulk_write failed with error %d\n", ret); + #endif + return false; + } + + ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT); + if( ret < 0 ) + { + #ifdef _LIBNFC_VERBOSE_ + printf( "usb_bulk_read failed with error %d\n", ret); + #endif + return false; + } + + #ifdef _LIBNFC_VERBOSE_ + printf("Rx: "); + print_hex((byte*)buf,ret); + #endif + + if( ret == 6 ) + { + ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT); + if( ret < 0 ) + { + #ifdef _LIBNFC_VERBOSE_ + printf("usb_bulk_read failed with error %d\n", ret); + #endif + return false; + } + + #ifdef _LIBNFC_VERBOSE_ + printf("Rx: "); + print_hex((byte*)buf,ret); + #endif + } + + // When the answer should be ignored, just return a succesful result + if(pbtRx == null || puiRxLen == 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) return false; + + // Remove the preceding and appending bytes 00 00 FF xx Fx .. .. .. xx 00 (x = variable) + *puiRxLen = ret - 7 - 2; + + // Get register: nuke extra byte (awful hack) + if ((buf[5]==(char)0xd5) && (buf[6]==(char)0x07) && (*puiRxLen==2)) { + // printf("Got %02x %02x, keep %02x\n", buf[7], buf[8], buf[8]); + *puiRxLen = (*puiRxLen) - 1; + memcpy( pbtRx, buf + 8, *puiRxLen); + return true; + } + + memcpy( pbtRx, buf + 7, *puiRxLen); + + return true; +} diff --git a/src/dev_pn533.h b/src/dev_pn533.h new file mode 100644 index 0000000..387c549 --- /dev/null +++ b/src/dev_pn533.h @@ -0,0 +1,35 @@ +/* + +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 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 General Public License +along with this program. If not, see . + +*/ + +#ifndef _LIBNFC_DEV_PN533_H_ +#define _LIBNFC_DEV_PN533_H_ + +#include "defines.h" +#include "types.h" + +// Functions used by developer to handle connection to this device +dev_info* dev_pn533_connect(const ui32 uiIndex); +void dev_pn533_disconnect(dev_info* pdi); + +// Callback function used by libnfc to transmit commands to the PN53X chip +bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen); + +#endif // _LIBNFC_DEV_PN533_H_ + diff --git a/src/devices.h b/src/devices.h index 0104d5a..15f8333 100644 --- a/src/devices.h +++ b/src/devices.h @@ -25,12 +25,14 @@ along with this program. If not, see . #include "types.h" #include "dev_acr122.h" #include "dev_pn531.h" +#include "dev_pn533.h" #include "dev_arygon.h" const static struct dev_callbacks dev_callbacks_list[] = { // Driver Name Connect Transceive Disconect { "ACR122", dev_acr122_connect, dev_acr122_transceive, dev_acr122_disconnect }, { "PN531USB", dev_pn531_connect, dev_pn531_transceive, dev_pn531_disconnect }, + { "PN533USB", dev_pn533_connect, dev_pn533_transceive, dev_pn533_disconnect }, { "ARYGON", dev_arygon_connect, dev_arygon_transceive, dev_arygon_disconnect } };