Codebase list libfreefare / 850b3c2
Improve USB support: - All USB errors are now reported in text format; - USB drivers now relies on libusb-win32 with version >= 1.2.4.x (1.2.4.6 recommended) (Many thanks to Glenn) Romuald Conty 12 years ago
3 changed file(s) with 56 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
66 ============
77
88 - MinGW-w64 compiler toolchain [1]
9 - LibUsb-Win32 0.1.12.2 [2]
10 - CMake 2.6 [3]
9 - LibUsb-Win32 1.2.4.6 (or greater) [2]
10 - CMake 2.8 [3]
1111
1212 This was tested on Windows 7 64 bit, but should work on Windows Vista and
1313 Windows XP and 32 bit as well.
5252 [1] the easiest way is to use the TDM-GCC installer.
5353 Make sure to select MinGW-w64 in the installer, the regular MinGW does not contain headers for PCSC.
5454 http://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm64-gcc-4.5.1.exe/download
55 [2] http://libusb-win32.sourceforge.net/
55 [2] http://sourceforge.net/projects/libusb-win32/files/libusb-win32-snapshots/20110512/libusb-win32-snapshot-1.2.4.6.zip/download
5656 [3] http://www.cmake.org
2525
2626 IF(NOT LIBUSB_FOUND)
2727 IF(WIN32)
28 FIND_PATH(LIBUSB_INCLUDE_DIRS usb.h "$ENV{ProgramFiles}/LibUSB-Win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
28 FIND_PATH(LIBUSB_INCLUDE_DIRS lusb0_usb.h "$ENV{ProgramFiles}/LibUSB-Win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
2929 FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb PATHS "$ENV{ProgramFiles}/LibUSB-Win32/lib/gcc")
3030 SET(LIBUSB_LIBRARY_DIR "$ENV{ProgramFiles}/LibUSB-Win32/bin/x86/")
3131 ELSE(WIN32)
3535 #include <errno.h>
3636 #include <stdio.h>
3737 #include <stdlib.h>
38 #include <usb.h>
38
39 #ifndef _WIN32
40 // Under POSIX system, we use libusb (>= 0.1.12)
41 #include <usb.h>
42 #define USB_TIMEOUT 0
43 #define _usb_strerror( X ) strerror(-X)
44 #else
45 // Under Windows we use libusb-win32 (>= 1.2.4)
46 #include <lusb0_usb.h>
47 // libusb-win32's bug workaround: 0 as timeout does not means infite as expected
48 #define USB_TIMEOUT 10
49 #define _usb_strerror( X ) usb_strerror()
50 #endif
51
3952 #include <string.h>
4053
4154 #include <nfc/nfc.h>
4659 #include "drivers/pn53x_usb.h"
4760
4861 #define PN53X_USB_DRIVER_NAME "PN53x USB"
49 #define USB_TIMEOUT 0
5062
5163 #define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data))
5264
7789 pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx)
7890 {
7991 int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, USB_TIMEOUT);
80 if (res > 0)
81 PRINT_HEX ("RX", abtRx, res);
92 if (res > 0) {
93 PRINT_HEX ("RX", abtRx, res);
94 } else if (res < 0) {
95 ERR ("Unable to read from USB (%s", _usb_strerror (res));
96 }
8297 return res;
8398 }
8499 int
86101 pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx, int timeout)
87102 {
88103 int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout);
89 if (res > 0)
90 PRINT_HEX ("RX", abtRx, res);
104 if (res > 0) {
105 PRINT_HEX ("RX", abtRx, res);
106 } else if (res < 0) {
107 if (-res != ETIMEDOUT) {
108 ERR ("Unable to read from USB (%s)", _usb_strerror (res));
109 }
110 }
91111 return res;
92112 }
93113
96116 {
97117 PRINT_HEX ("TX", abtTx, szTx);
98118 int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, USB_TIMEOUT);
99 // HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details
100 if ((res % data->uiMaxPacketSize) == 0) {
101 usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, USB_TIMEOUT);
119 if (res > 0) {
120 // HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details
121 if ((res % data->uiMaxPacketSize) == 0) {
122 usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, USB_TIMEOUT);
123 }
124 } else {
125 ERR ("Unable to write to USB (%s)", _usb_strerror (res));
102126 }
103127 return res;
104128 }
173197 usb_init ();
174198
175199 int res;
176 // 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).
177 if ((res = usb_find_busses () < 0))
200 // usb_find_busses will find all of the busses on the system. Returns the
201 // number of changes since previous call to this function (total of new
202 // busses and busses removed).
203 if ((res = usb_find_busses () < 0)) {
204 ERR ("Unable to find USB busses (%s)", _usb_strerror (res));
178205 return false;
179 // 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).
180 if ((res = usb_find_devices () < 0))
206 }
207 // usb_find_devices will find all of the devices on each bus. This should be
208 // called after usb_find_busses. Returns the number of changes since the
209 // previous call to this function (total of new device and devices removed).
210 if ((res = usb_find_devices () < 0)) {
211 ERR ("Unable to find USB devices (%s)", _usb_strerror (res));
181212 return false;
213 }
182214
183215 *pszDeviceFound = 0;
184216
186218 struct usb_bus *bus;
187219 for (bus = usb_get_busses (); bus; bus = bus->next) {
188220 struct usb_device *dev;
221
189222 for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
190223 for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) {
191 // DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct,candidates[i].idVendor,candidates[i].idProduct);
224 //DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct);
192225 if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) &&
193226 (pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) {
194227 // Make sure there are 2 endpoints available
207240 // Set configuration
208241 int res = usb_set_configuration (udev, 1);
209242 if (res < 0) {
243 ERR ("Unable to set USB configuration (%s)", _usb_strerror (res));
210244 usb_close (udev);
211245 // we failed to use the device
212246 continue;
284318 // Set configuration
285319 int res = usb_set_configuration (data.pudh, 1);
286320 if (res < 0) {
287 ERR ("Unable to set USB configuration (%s)", strerror (-res));
321 ERR ("Unable to set USB configuration (%s)", _usb_strerror (res));
288322 if (EPERM == -res) {
289323 WARN ("Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
290324 }
295329
296330 res = usb_claim_interface (data.pudh, 0);
297331 if (res < 0) {
298 DBG ("Can't claim interface (%s)", strerror (-res));
332 ERR ("Unable to claim USB interface (%s)", _usb_strerror (res));
299333 usb_close (data.pudh);
300334 // we failed to use the specified device
301335 return NULL;
369403
370404 int res;
371405 if ((res = usb_release_interface (DRIVER_DATA (pnd)->pudh, 0)) < 0) {
372 ERR ("usb_release_interface failed (%i)", res);
406 ERR ("Unable to release USB interface (%s)", _usb_strerror (res));
373407 }
374408
375409 if ((res = usb_close (DRIVER_DATA (pnd)->pudh)) < 0) {
376 ERR ("usb_close failed (%i)", res);
410 ERR ("Unable to close USB connection (%s)", _usb_strerror (res));
377411 }
378412 pn53x_data_free (pnd);
379413 nfc_device_free (pnd);
392426 int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame);
393427
394428 if (res < 0) {
395 DBG ("usb_bulk_write failed with error %d", res);
396429 pnd->iLastError = DEIO;
397430 return false;
398431 }
400433 byte_t abtRxBuf[PN53X_USB_BUFFER_LEN];
401434 res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf));
402435 if (res < 0) {
403 DBG ("usb_bulk_read failed with error %d", res);
404436 pnd->iLastError = DEIO;
405437 // try to interrupt current device state
406438 pn53x_usb_ack(pnd);
418450 // packet.
419451 int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *)nack_frame, sizeof(nack_frame));
420452 if (res < 0) {
421 DBG ("usb_bulk_write failed with error %d", res);
422453 pnd->iLastError = DEIO;
423454 // try to interrupt current device state
424455 pn53x_usb_ack(pnd);
465496 }
466497
467498 if (res < 0) {
468 DBG ("usb_bulk_read failed with error %d (%s)", res, strerror(-res));
469499 pnd->iLastError = DEIO;
470500 // try to interrupt current device state
471501 pn53x_usb_ack(pnd);