35 | 35 |
#include <errno.h>
|
36 | 36 |
#include <stdio.h>
|
37 | 37 |
#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 |
|
39 | 52 |
#include <string.h>
|
40 | 53 |
|
41 | 54 |
#include <nfc/nfc.h>
|
|
46 | 59 |
#include "drivers/pn53x_usb.h"
|
47 | 60 |
|
48 | 61 |
#define PN53X_USB_DRIVER_NAME "PN53x USB"
|
49 | |
#define USB_TIMEOUT 0
|
50 | 62 |
|
51 | 63 |
#define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data))
|
52 | 64 |
|
|
77 | 89 |
pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx)
|
78 | 90 |
{
|
79 | 91 |
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 |
}
|
82 | 97 |
return res;
|
83 | 98 |
}
|
84 | 99 |
int
|
|
86 | 101 |
pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx, int timeout)
|
87 | 102 |
{
|
88 | 103 |
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 |
}
|
91 | 111 |
return res;
|
92 | 112 |
}
|
93 | 113 |
|
|
96 | 116 |
{
|
97 | 117 |
PRINT_HEX ("TX", abtTx, szTx);
|
98 | 118 |
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));
|
102 | 126 |
}
|
103 | 127 |
return res;
|
104 | 128 |
}
|
|
173 | 197 |
usb_init ();
|
174 | 198 |
|
175 | 199 |
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));
|
178 | 205 |
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));
|
181 | 212 |
return false;
|
|
213 |
}
|
182 | 214 |
|
183 | 215 |
*pszDeviceFound = 0;
|
184 | 216 |
|
|
186 | 218 |
struct usb_bus *bus;
|
187 | 219 |
for (bus = usb_get_busses (); bus; bus = bus->next) {
|
188 | 220 |
struct usb_device *dev;
|
|
221 |
|
189 | 222 |
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
|
190 | 223 |
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);
|
192 | 225 |
if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) &&
|
193 | 226 |
(pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) {
|
194 | 227 |
// Make sure there are 2 endpoints available
|
|
207 | 240 |
// Set configuration
|
208 | 241 |
int res = usb_set_configuration (udev, 1);
|
209 | 242 |
if (res < 0) {
|
|
243 |
ERR ("Unable to set USB configuration (%s)", _usb_strerror (res));
|
210 | 244 |
usb_close (udev);
|
211 | 245 |
// we failed to use the device
|
212 | 246 |
continue;
|
|
284 | 318 |
// Set configuration
|
285 | 319 |
int res = usb_set_configuration (data.pudh, 1);
|
286 | 320 |
if (res < 0) {
|
287 | |
ERR ("Unable to set USB configuration (%s)", strerror (-res));
|
|
321 |
ERR ("Unable to set USB configuration (%s)", _usb_strerror (res));
|
288 | 322 |
if (EPERM == -res) {
|
289 | 323 |
WARN ("Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
|
290 | 324 |
}
|
|
295 | 329 |
|
296 | 330 |
res = usb_claim_interface (data.pudh, 0);
|
297 | 331 |
if (res < 0) {
|
298 | |
DBG ("Can't claim interface (%s)", strerror (-res));
|
|
332 |
ERR ("Unable to claim USB interface (%s)", _usb_strerror (res));
|
299 | 333 |
usb_close (data.pudh);
|
300 | 334 |
// we failed to use the specified device
|
301 | 335 |
return NULL;
|
|
369 | 403 |
|
370 | 404 |
int res;
|
371 | 405 |
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));
|
373 | 407 |
}
|
374 | 408 |
|
375 | 409 |
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));
|
377 | 411 |
}
|
378 | 412 |
pn53x_data_free (pnd);
|
379 | 413 |
nfc_device_free (pnd);
|
|
392 | 426 |
int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame);
|
393 | 427 |
|
394 | 428 |
if (res < 0) {
|
395 | |
DBG ("usb_bulk_write failed with error %d", res);
|
396 | 429 |
pnd->iLastError = DEIO;
|
397 | 430 |
return false;
|
398 | 431 |
}
|
|
400 | 433 |
byte_t abtRxBuf[PN53X_USB_BUFFER_LEN];
|
401 | 434 |
res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf));
|
402 | 435 |
if (res < 0) {
|
403 | |
DBG ("usb_bulk_read failed with error %d", res);
|
404 | 436 |
pnd->iLastError = DEIO;
|
405 | 437 |
// try to interrupt current device state
|
406 | 438 |
pn53x_usb_ack(pnd);
|
|
418 | 450 |
// packet.
|
419 | 451 |
int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *)nack_frame, sizeof(nack_frame));
|
420 | 452 |
if (res < 0) {
|
421 | |
DBG ("usb_bulk_write failed with error %d", res);
|
422 | 453 |
pnd->iLastError = DEIO;
|
423 | 454 |
// try to interrupt current device state
|
424 | 455 |
pn53x_usb_ack(pnd);
|
|
465 | 496 |
}
|
466 | 497 |
|
467 | 498 |
if (res < 0) {
|
468 | |
DBG ("usb_bulk_read failed with error %d (%s)", res, strerror(-res));
|
469 | 499 |
pnd->iLastError = DEIO;
|
470 | 500 |
// try to interrupt current device state
|
471 | 501 |
pn53x_usb_ack(pnd);
|