Isolate libusb dependencies in buses/usbbus.c

This commit is contained in:
Philippe Teuwen 2013-10-13 16:50:45 +02:00
parent 7e5257dd44
commit b12cc161c4
4 changed files with 311 additions and 115 deletions

View File

@ -36,12 +36,20 @@
#include <stdlib.h> #include <stdlib.h>
#ifndef _WIN32
// Under POSIX system, we use libusb (>= 0.1.12)
#include <usb.h>
#else
// Under Windows we use libusb-win32 (>= 1.2.5)
#include <lusb0_usb.h>
#endif
#include "usbbus.h" #include "usbbus.h"
#include "log.h" #include "log.h"
#define LOG_CATEGORY "libnfc.buses.usbbus" #define LOG_CATEGORY "libnfc.buses.usbbus"
#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define LOG_GROUP NFC_LOG_GROUP_DRIVER
int usb_prepare(void) int usbbus_prepare(void)
{ {
static bool usb_initialized = false; static bool usb_initialized = false;
if (!usb_initialized) { if (!usb_initialized) {
@ -64,16 +72,76 @@ int usb_prepare(void)
// number of changes since previous call to this function (total of new // number of changes since previous call to this function (total of new
// busses and busses removed). // busses and busses removed).
if ((res = usb_find_busses()) < 0) { if ((res = usb_find_busses()) < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", usbbus_strerror(res));
return -1; return -1;
} }
// usb_find_devices will find all of the devices on each bus. This should be // 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 // called after usb_find_busses. Returns the number of changes since the
// previous call to this function (total of new device and devices removed). // previous call to this function (total of new device and devices removed).
if ((res = usb_find_devices()) < 0) { if ((res = usb_find_devices()) < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", usbbus_strerror(res));
return -1; return -1;
} }
return 0; return 0;
} }
usbbus_dev_handle *usbbus_open(struct usbbus_device *dev)
{
return (usbbus_dev_handle *) usb_open((struct usb_device *) dev);
}
int usbbus_close(usbbus_dev_handle *dev)
{
return usb_close((usb_dev_handle *) dev);
}
int usbbus_set_configuration(usbbus_dev_handle *dev, int configuration)
{
return usb_set_configuration((usb_dev_handle *)dev, configuration);
}
int usbbus_get_string_simple(usbbus_dev_handle *dev, int index, char *buf, size_t buflen)
{
return usb_get_string_simple((usb_dev_handle *)dev, index, buf, buflen);
}
int usbbus_bulk_read(usbbus_dev_handle *dev, int ep, char *bytes, int size, int timeout)
{
return usb_bulk_read((usb_dev_handle *)dev, ep, bytes, size, timeout);
}
int usbbus_bulk_write(usbbus_dev_handle *dev, int ep, const char *bytes, int size, int timeout)
{
return usb_bulk_write((usb_dev_handle *)dev, ep, bytes, size, timeout);
}
int usbbus_claim_interface(usbbus_dev_handle *dev, int interface)
{
return usb_claim_interface((usb_dev_handle *)dev, interface);
}
int usbbus_release_interface(usbbus_dev_handle *dev, int interface)
{
return usb_release_interface((usb_dev_handle *)dev, interface);
}
int usbbus_set_altinterface(usbbus_dev_handle *dev, int alternate)
{
return usb_set_altinterface((usb_dev_handle *)dev, alternate);
}
int usbbus_reset(usbbus_dev_handle *dev)
{
return usb_reset((usb_dev_handle *)dev);
}
struct usbbus_device *usbbus_device(usbbus_dev_handle *dev)
{
return (struct usbbus_device *) usb_device((usb_dev_handle *)dev);
}
struct usbbus_bus *usbbus_get_busses(void)
{
return (struct usbbus_bus *) usb_get_busses();
}

View File

@ -33,21 +33,149 @@
#ifndef __NFC_BUS_USB_H__ #ifndef __NFC_BUS_USB_H__
# define __NFC_BUS_USB_H__ # define __NFC_BUS_USB_H__
#ifndef _WIN32
// Under POSIX system, we use libusb (>= 0.1.12)
#include <usb.h>
#define USB_TIMEDOUT ETIMEDOUT
#define _usb_strerror( X ) strerror(-X)
#else
// Under Windows we use libusb-win32 (>= 1.2.5)
#include <lusb0_usb.h>
#define USB_TIMEDOUT 116
#define _usb_strerror( X ) usb_strerror()
#endif
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
int usb_prepare(void); #ifndef _WIN32
// Under POSIX system, we use libusb (>= 0.1.12)
#define USBBUS_TIMEDOUT ETIMEDOUT
#define usbbus_strerror( X ) strerror(-X)
#else
// Under Windows we use libusb-win32 (>= 1.2.5)
#define USBBUS_TIMEDOUT 116
#define usbbus_strerror( X ) usb_strerror()
#endif
#define USBBUS_ENDPOINT_DIR_MASK 0x80
#define USBBUS_ENDPOINT_TYPE_BULK 2
#define USBBUS_ENDPOINT_IN 0x80
#define USBBUS_ENDPOINT_OUT 0x00
#ifdef PATH_MAX
#define USBBUS_PATH_MAX PATH_MAX
#else
#define USBBUS_PATH_MAX 4096
#endif
struct usbbus_dev_handle;
typedef struct usbbus_dev_handle usbbus_dev_handle;
struct usbbus_endpoint_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
uint8_t bRefresh;
uint8_t bSynchAddress;
unsigned char *extra;
int extralen;
};
struct usbbus_interface_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
struct usbbus_endpoint_descriptor *endpoint;
unsigned char *extra;
int extralen;
};
struct usbbus_interface {
struct usbbus_interface_descriptor *altsetting;
int num_altsetting;
};
struct usbbus_config_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t MaxPower;
struct usbbus_interface *interface;
unsigned char *extra;
int extralen;
};
/* Device descriptor */
struct usbbus_device_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} __attribute__((packed));
struct usbbus_device;
struct usbbus_bus;
struct usbbus_device {
struct usbbus_device *next, *prev;
char filename[USBBUS_PATH_MAX + 1];
struct usbbus_bus *bus;
struct usbbus_device_descriptor descriptor;
struct usbbus_config_descriptor *config;
void *dev;
uint8_t devnum;
unsigned char num_children;
struct usbbus_device **children;
};
struct usbbus_bus {
struct usbbus_bus *next, *prev;
char dirname[USBBUS_PATH_MAX + 1];
struct usbbus_device *devices;
uint32_t location;
struct usbbus_device *root_dev;
};
int usbbus_prepare(void);
usbbus_dev_handle *usbbus_open(struct usbbus_device *dev);
int usbbus_close(usbbus_dev_handle *dev);
int usbbus_set_configuration(usbbus_dev_handle *dev, int configuration);
int usbbus_get_string_simple(usbbus_dev_handle *dev, int index, char *buf, size_t buflen);
int usbbus_bulk_read(usbbus_dev_handle *dev, int ep, char *bytes, int size, int timeout);
int usbbus_bulk_write(usbbus_dev_handle *dev, int ep, const char *bytes, int size, int timeout);
int usbbus_claim_interface(usbbus_dev_handle *dev, int interface);
int usbbus_release_interface(usbbus_dev_handle *dev, int interface);
int usbbus_set_altinterface(usbbus_dev_handle *dev, int alternate);
int usbbus_reset(usbbus_dev_handle *dev);
struct usbbus_device *usbbus_device(usbbus_dev_handle *dev);
struct usbbus_bus *usbbus_get_busses(void);
#endif // __NFC_BUS_USB_H__ #endif // __NFC_BUS_USB_H__

View File

@ -74,7 +74,7 @@ Thanks to d18c7db and Okko for example code
#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define LOG_GROUP NFC_LOG_GROUP_DRIVER
#define LOG_CATEGORY "libnfc.driver.acr122_usb" #define LOG_CATEGORY "libnfc.driver.acr122_usb"
#define USB_INFINITE_TIMEOUT 0 #define USBBUS_INFINITE_TIMEOUT 0
#define DRIVER_DATA(pnd) ((struct acr122_usb_data*)(pnd->driver_data)) #define DRIVER_DATA(pnd) ((struct acr122_usb_data*)(pnd->driver_data))
@ -172,7 +172,7 @@ struct acr122_usb_apdu_frame {
// Internal data struct // Internal data struct
struct acr122_usb_data { struct acr122_usb_data {
usb_dev_handle *pudh; usbbus_dev_handle *pudh;
uint32_t uiEndPointIn; uint32_t uiEndPointIn;
uint32_t uiEndPointOut; uint32_t uiEndPointOut;
uint32_t uiMaxPacketSize; uint32_t uiMaxPacketSize;
@ -218,13 +218,13 @@ static int acr122_usb_send_apdu(nfc_device *pnd,
static int static int
acr122_usb_bulk_read(struct acr122_usb_data *data, uint8_t abtRx[], const size_t szRx, const int timeout) acr122_usb_bulk_read(struct acr122_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 = usbbus_bulk_read(data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout);
if (res > 0) { if (res > 0) {
LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res); LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res);
} else if (res < 0) { } else if (res < 0) {
if (res != -USB_TIMEDOUT) { if (res != -USBBUS_TIMEDOUT) {
res = NFC_EIO; res = NFC_EIO;
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", usbbus_strerror(res));
} else { } else {
res = NFC_ETIMEOUT; res = NFC_ETIMEOUT;
} }
@ -236,15 +236,15 @@ static int
acr122_usb_bulk_write(struct acr122_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout) acr122_usb_bulk_write(struct acr122_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout)
{ {
LOG_HEX(NFC_LOG_GROUP_COM, "TX", abtTx, szTx); LOG_HEX(NFC_LOG_GROUP_COM, "TX", abtTx, szTx);
int res = usb_bulk_write(data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout); int res = usbbus_bulk_write(data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout);
if (res > 0) { if (res > 0) {
// HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details // 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) { if ((res % data->uiMaxPacketSize) == 0) {
usb_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout); usbbus_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout);
} }
} else if (res < 0) { } else if (res < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", usbbus_strerror(res));
if (res == -USB_TIMEDOUT) { if (res == -USBBUS_TIMEDOUT) {
res = NFC_ETIMEOUT; res = NFC_ETIMEOUT;
} else { } else {
res = NFC_EIO; res = NFC_EIO;
@ -266,28 +266,28 @@ const struct acr122_usb_supported_device acr122_usb_supported_devices[] = {
// Find transfer endpoints for bulk transfers // Find transfer endpoints for bulk transfers
static void static void
acr122_usb_get_end_points(struct usb_device *dev, struct acr122_usb_data *data) acr122_usb_get_end_points(struct usbbus_device *dev, struct acr122_usb_data *data)
{ {
uint32_t uiIndex; uint32_t uiIndex;
uint32_t uiEndPoint; uint32_t uiEndPoint;
struct usb_interface_descriptor *puid = dev->config->interface->altsetting; struct usbbus_interface_descriptor *puid = dev->config->interface->altsetting;
// 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out // 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) { for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) {
// Only accept bulk transfer endpoints (ignore interrupt endpoints) // Only accept bulk transfer endpoints (ignore interrupt endpoints)
if (puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK) if (puid->endpoint[uiIndex].bmAttributes != USBBUS_ENDPOINT_TYPE_BULK)
continue; continue;
// Copy the endpoint to a local var, makes it more readable code // Copy the endpoint to a local var, makes it more readable code
uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress; uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress;
// Test if we dealing with a bulk IN endpoint // Test if we dealing with a bulk IN endpoint
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) { if ((uiEndPoint & USBBUS_ENDPOINT_DIR_MASK) == USBBUS_ENDPOINT_IN) {
data->uiEndPointIn = uiEndPoint; data->uiEndPointIn = uiEndPoint;
data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
} }
// Test if we dealing with a bulk OUT endpoint // Test if we dealing with a bulk OUT endpoint
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) { if ((uiEndPoint & USBBUS_ENDPOINT_DIR_MASK) == USBBUS_ENDPOINT_OUT) {
data->uiEndPointOut = uiEndPoint; data->uiEndPointOut = uiEndPoint;
data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
} }
@ -299,13 +299,13 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const
{ {
(void)context; (void)context;
usb_prepare(); usbbus_prepare();
size_t device_found = 0; size_t device_found = 0;
uint32_t uiBusIndex = 0; uint32_t uiBusIndex = 0;
struct usb_bus *bus; struct usbbus_bus *bus;
for (bus = usb_get_busses(); bus; bus = bus->next) { for (bus = usbbus_get_busses(); bus; bus = bus->next) {
struct usb_device *dev; struct usbbus_device *dev;
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) { for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
for (size_t n = 0; n < sizeof(acr122_usb_supported_devices) / sizeof(struct acr122_usb_supported_device); n++) { for (size_t n = 0; n < sizeof(acr122_usb_supported_devices) / sizeof(struct acr122_usb_supported_device); n++) {
@ -322,14 +322,14 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const
continue; continue;
} }
usb_dev_handle *udev = usb_open(dev); usbbus_dev_handle *udev = usbbus_open(dev);
if (udev == NULL) if (udev == NULL)
continue; continue;
// Set configuration // Set configuration
// acr122_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); // acr122_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice));
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s Name %s", bus->dirname, dev->filename, acr122_usb_supported_devices[n].name); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s Name %s", bus->dirname, dev->filename, acr122_usb_supported_devices[n].name);
usb_close(udev); usbbus_close(udev);
snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename);
device_found++; device_found++;
// Test if we reach the maximum "wanted" devices // Test if we reach the maximum "wanted" devices
@ -350,16 +350,16 @@ struct acr122_usb_descriptor {
}; };
static bool static bool
acr122_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len) acr122_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_dev_handle *udev, char *buffer, size_t len)
{ {
*buffer = '\0'; *buffer = '\0';
if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) { if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) {
if (udev) { if (udev) {
usb_get_string_simple(udev, dev->descriptor.iManufacturer, buffer, len); usbbus_get_string_simple(udev, dev->descriptor.iManufacturer, buffer, len);
if (strlen(buffer) > 0) if (strlen(buffer) > 0)
strcpy(buffer + strlen(buffer), " / "); strcpy(buffer + strlen(buffer), " / ");
usb_get_string_simple(udev, dev->descriptor.iProduct, buffer + strlen(buffer), len - strlen(buffer)); usbbus_get_string_simple(udev, dev->descriptor.iProduct, buffer + strlen(buffer), len - strlen(buffer));
} }
} }
@ -393,12 +393,12 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
.uiEndPointIn = 0, .uiEndPointIn = 0,
.uiEndPointOut = 0, .uiEndPointOut = 0,
}; };
struct usb_bus *bus; struct usbbus_bus *bus;
struct usb_device *dev; struct usbbus_device *dev;
usb_prepare(); usbbus_prepare();
for (bus = usb_get_busses(); bus; bus = bus->next) { for (bus = usbbus_get_busses(); bus; bus = bus->next) {
if (connstring_decode_level > 1) { if (connstring_decode_level > 1) {
// A specific bus have been specified // A specific bus have been specified
if (0 != strcmp(bus->dirname, desc.dirname)) if (0 != strcmp(bus->dirname, desc.dirname))
@ -411,25 +411,25 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
continue; continue;
} }
// Open the USB device // Open the USB device
if ((data.pudh = usb_open(dev)) == NULL) if ((data.pudh = usbbus_open(dev)) == NULL)
continue; continue;
// Reset device // Reset device
usb_reset(data.pudh); usbbus_reset(data.pudh);
// Retrieve end points // Retrieve end points
acr122_usb_get_end_points(dev, &data); acr122_usb_get_end_points(dev, &data);
// Claim interface // Claim interface
int res = usb_claim_interface(data.pudh, 0); int res = usbbus_claim_interface(data.pudh, 0);
if (res < 0) { if (res < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", usbbus_strerror(res));
usb_close(data.pudh); usbbus_close(data.pudh);
// we failed to use the specified device // we failed to use the specified device
goto free_mem; goto free_mem;
} }
res = usb_set_altinterface(data.pudh, 0); res = usbbus_set_altinterface(data.pudh, 0);
if (res < 0) { if (res < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", usbbus_strerror(res));
usb_close(data.pudh); usbbus_close(data.pudh);
// we failed to use the specified device // we failed to use the specified device
goto free_mem; goto free_mem;
} }
@ -461,7 +461,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
pnd->driver = &acr122_usb_driver; pnd->driver = &acr122_usb_driver;
if (acr122_usb_init(pnd) < 0) { if (acr122_usb_init(pnd) < 0) {
usb_close(data.pudh); usbbus_close(data.pudh);
goto error; goto error;
} }
DRIVER_DATA(pnd)->abort_flag = false; DRIVER_DATA(pnd)->abort_flag = false;
@ -488,12 +488,12 @@ acr122_usb_close(nfc_device *pnd)
pn53x_idle(pnd); pn53x_idle(pnd);
int res; int res;
if ((res = usb_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) { if ((res = usbbus_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", usbbus_strerror(res));
} }
if ((res = usb_close(DRIVER_DATA(pnd)->pudh)) < 0) { if ((res = usbbus_close(DRIVER_DATA(pnd)->pudh)) < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to close USB connection (%s)", usbbus_strerror(res));
} }
pn53x_data_free(pnd); pn53x_data_free(pnd);
nfc_device_free(pnd); nfc_device_free(pnd);
@ -570,7 +570,7 @@ acr122_usb_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co
return NFC_SUCCESS; return NFC_SUCCESS;
} }
#define USB_TIMEOUT_PER_PASS 200 #define USBBUS_TIMEOUT_PER_PASS 200
static int static int
acr122_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const int timeout) acr122_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const int timeout)
{ {
@ -580,26 +580,26 @@ acr122_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, co
int res; int res;
/* /*
* If no timeout is specified but the command is blocking, force a 200ms (USB_TIMEOUT_PER_PASS) * If no timeout is specified but the command is blocking, force a 200ms (USBBUS_TIMEOUT_PER_PASS)
* timeout to allow breaking the loop if the user wants to stop it. * timeout to allow breaking the loop if the user wants to stop it.
*/ */
int usb_timeout; int usbbus_timeout;
int remaining_time = timeout; int remaining_time = timeout;
read: read:
if (timeout == USB_INFINITE_TIMEOUT) { if (timeout == USBBUS_INFINITE_TIMEOUT) {
usb_timeout = USB_TIMEOUT_PER_PASS; usbbus_timeout = USBBUS_TIMEOUT_PER_PASS;
} else { } else {
// A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism
remaining_time -= USB_TIMEOUT_PER_PASS; remaining_time -= USBBUS_TIMEOUT_PER_PASS;
if (remaining_time <= 0) { if (remaining_time <= 0) {
pnd->last_error = NFC_ETIMEOUT; pnd->last_error = NFC_ETIMEOUT;
return pnd->last_error; return pnd->last_error;
} else { } else {
usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); usbbus_timeout = MIN(remaining_time, USBBUS_TIMEOUT_PER_PASS);
} }
} }
res = acr122_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), usb_timeout); res = acr122_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), usbbus_timeout);
uint8_t attempted_response = RDR_to_PC_DataBlock; uint8_t attempted_response = RDR_to_PC_DataBlock;
size_t len; size_t len;

View File

@ -56,7 +56,7 @@ Thanks to d18c7db and Okko for example code
#define LOG_CATEGORY "libnfc.driver.pn53x_usb" #define LOG_CATEGORY "libnfc.driver.pn53x_usb"
#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define LOG_GROUP NFC_LOG_GROUP_DRIVER
#define USB_INFINITE_TIMEOUT 0 #define USBBUS_INFINITE_TIMEOUT 0
#define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data)) #define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data))
@ -72,7 +72,7 @@ typedef enum {
// Internal data struct // Internal data struct
struct pn53x_usb_data { struct pn53x_usb_data {
usb_dev_handle *pudh; usbbus_dev_handle *pudh;
pn53x_usb_model model; pn53x_usb_model model;
uint32_t uiEndPointIn; uint32_t uiEndPointIn;
uint32_t uiEndPointOut; uint32_t uiEndPointOut;
@ -84,18 +84,18 @@ struct pn53x_usb_data {
const struct pn53x_io pn53x_usb_io; const struct pn53x_io pn53x_usb_io;
// Prototypes // Prototypes
bool pn53x_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len); bool pn53x_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_dev_handle *udev, char *buffer, size_t len);
int pn53x_usb_init(nfc_device *pnd); int pn53x_usb_init(nfc_device *pnd);
static int 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 = usbbus_bulk_read(data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout);
if (res > 0) { if (res > 0) {
LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res); LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res);
} else if (res < 0) { } else if (res < 0) {
if (res != -USB_TIMEDOUT) if (res != -USBBUS_TIMEDOUT)
log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", usbbus_strerror(res));
} }
return res; return res;
} }
@ -104,14 +104,14 @@ 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(NFC_LOG_GROUP_COM, "TX", abtTx, szTx); LOG_HEX(NFC_LOG_GROUP_COM, "TX", abtTx, szTx);
int res = usb_bulk_write(data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout); int res = usbbus_bulk_write(data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout);
if (res > 0) { if (res > 0) {
// HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details // 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) { if ((res % data->uiMaxPacketSize) == 0) {
usb_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout); usbbus_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout);
} }
} else { } else {
log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res)); log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", usbbus_strerror(res));
} }
return res; return res;
} }
@ -148,28 +148,28 @@ int pn53x_usb_ack(nfc_device *pnd);
// Find transfer endpoints for bulk transfers // Find transfer endpoints for bulk transfers
static void static void
pn53x_usb_get_end_points(struct usb_device *dev, struct pn53x_usb_data *data) pn53x_usb_get_end_points(struct usbbus_device *dev, struct pn53x_usb_data *data)
{ {
uint32_t uiIndex; uint32_t uiIndex;
uint32_t uiEndPoint; uint32_t uiEndPoint;
struct usb_interface_descriptor *puid = dev->config->interface->altsetting; struct usbbus_interface_descriptor *puid = dev->config->interface->altsetting;
// 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out // 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) { for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) {
// Only accept bulk transfer endpoints (ignore interrupt endpoints) // Only accept bulk transfer endpoints (ignore interrupt endpoints)
if (puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK) if (puid->endpoint[uiIndex].bmAttributes != USBBUS_ENDPOINT_TYPE_BULK)
continue; continue;
// Copy the endpoint to a local var, makes it more readable code // Copy the endpoint to a local var, makes it more readable code
uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress; uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress;
// Test if we dealing with a bulk IN endpoint // Test if we dealing with a bulk IN endpoint
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) { if ((uiEndPoint & USBBUS_ENDPOINT_DIR_MASK) == USBBUS_ENDPOINT_IN) {
data->uiEndPointIn = uiEndPoint; data->uiEndPointIn = uiEndPoint;
data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
} }
// Test if we dealing with a bulk OUT endpoint // Test if we dealing with a bulk OUT endpoint
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) { if ((uiEndPoint & USBBUS_ENDPOINT_DIR_MASK) == USBBUS_ENDPOINT_OUT) {
data->uiEndPointOut = uiEndPoint; data->uiEndPointOut = uiEndPoint;
data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
} }
@ -181,13 +181,13 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s
{ {
(void)context; (void)context;
usb_prepare(); usbbus_prepare();
size_t device_found = 0; size_t device_found = 0;
uint32_t uiBusIndex = 0; uint32_t uiBusIndex = 0;
struct usb_bus *bus; struct usbbus_bus *bus;
for (bus = usb_get_busses(); bus; bus = bus->next) { for (bus = usbbus_get_busses(); bus; bus = bus->next) {
struct usb_device *dev; struct usbbus_device *dev;
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) { 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++) {
@ -204,22 +204,22 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s
continue; continue;
} }
usb_dev_handle *udev = usb_open(dev); usbbus_dev_handle *udev = usbbus_open(dev);
if (udev == NULL) if (udev == NULL)
continue; continue;
// Set configuration // Set configuration
int res = usb_set_configuration(udev, 1); int res = usbbus_set_configuration(udev, 1);
if (res < 0) { if (res < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", usbbus_strerror(res));
usb_close(udev); usbbus_close(udev);
// we failed to use the device // we failed to use the device
continue; continue;
} }
// pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice));
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s", bus->dirname, dev->filename); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s", bus->dirname, dev->filename);
usb_close(udev); usbbus_close(udev);
snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename);
device_found++; device_found++;
// Test if we reach the maximum "wanted" devices // Test if we reach the maximum "wanted" devices
@ -240,16 +240,16 @@ struct pn53x_usb_descriptor {
}; };
bool 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 usbbus_device *dev, usbbus_dev_handle *udev, char *buffer, size_t len)
{ {
*buffer = '\0'; *buffer = '\0';
if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) { if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) {
if (udev) { if (udev) {
usb_get_string_simple(udev, dev->descriptor.iManufacturer, buffer, len); usbbus_get_string_simple(udev, dev->descriptor.iManufacturer, buffer, len);
if (strlen(buffer) > 0) if (strlen(buffer) > 0)
strcpy(buffer + strlen(buffer), " / "); strcpy(buffer + strlen(buffer), " / ");
usb_get_string_simple(udev, dev->descriptor.iProduct, buffer + strlen(buffer), len - strlen(buffer)); usbbus_get_string_simple(udev, dev->descriptor.iProduct, buffer + strlen(buffer), len - strlen(buffer));
} }
} }
@ -283,12 +283,12 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring)
.uiEndPointIn = 0, .uiEndPointIn = 0,
.uiEndPointOut = 0, .uiEndPointOut = 0,
}; };
struct usb_bus *bus; struct usbbus_bus *bus;
struct usb_device *dev; struct usbbus_device *dev;
usb_prepare(); usbbus_prepare();
for (bus = usb_get_busses(); bus; bus = bus->next) { for (bus = usbbus_get_busses(); bus; bus = bus->next) {
if (connstring_decode_level > 1) { if (connstring_decode_level > 1) {
// A specific bus have been specified // A specific bus have been specified
if (0 != strcmp(bus->dirname, desc.dirname)) if (0 != strcmp(bus->dirname, desc.dirname))
@ -301,26 +301,26 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring)
continue; continue;
} }
// Open the USB device // Open the USB device
if ((data.pudh = usb_open(dev)) == NULL) if ((data.pudh = usbbus_open(dev)) == NULL)
continue; continue;
// Retrieve end points // Retrieve end points
pn53x_usb_get_end_points(dev, &data); pn53x_usb_get_end_points(dev, &data);
// Set configuration // Set configuration
int res = usb_set_configuration(data.pudh, 1); int res = usbbus_set_configuration(data.pudh, 1);
if (res < 0) { if (res < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", usbbus_strerror(res));
if (EPERM == -res) { if (EPERM == -res) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
} }
usb_close(data.pudh); usbbus_close(data.pudh);
// we failed to use the specified device // we failed to use the specified device
goto free_mem; goto free_mem;
} }
res = usb_claim_interface(data.pudh, 0); res = usbbus_claim_interface(data.pudh, 0);
if (res < 0) { if (res < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", usbbus_strerror(res));
usb_close(data.pudh); usbbus_close(data.pudh);
// we failed to use the specified device // we failed to use the specified device
goto free_mem; goto free_mem;
} }
@ -374,7 +374,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring)
// HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device // 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 // 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) { if (pn53x_usb_init(pnd) < 0) {
usb_close(data.pudh); usbbus_close(data.pudh);
goto error; goto error;
} }
DRIVER_DATA(pnd)->abort_flag = false; DRIVER_DATA(pnd)->abort_flag = false;
@ -408,12 +408,12 @@ pn53x_usb_close(nfc_device *pnd)
pn53x_idle(pnd); pn53x_idle(pnd);
int res; int res;
if ((res = usb_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) { if ((res = usbbus_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", usbbus_strerror(res));
} }
if ((res = usb_close(DRIVER_DATA(pnd)->pudh)) < 0) { if ((res = usbbus_close(DRIVER_DATA(pnd)->pudh)) < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to close USB connection (%s)", usbbus_strerror(res));
} }
pn53x_data_free(pnd); pn53x_data_free(pnd);
nfc_device_free(pnd); nfc_device_free(pnd);
@ -467,7 +467,7 @@ pn53x_usb_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, con
return NFC_SUCCESS; return NFC_SUCCESS;
} }
#define USB_TIMEOUT_PER_PASS 200 #define USBBUS_TIMEOUT_PER_PASS 200
static int static 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)
{ {
@ -478,28 +478,28 @@ pn53x_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, con
int res; int res;
/* /*
* If no timeout is specified but the command is blocking, force a 200ms (USB_TIMEOUT_PER_PASS) * If no timeout is specified but the command is blocking, force a 200ms (USBBUS_TIMEOUT_PER_PASS)
* timeout to allow breaking the loop if the user wants to stop it. * timeout to allow breaking the loop if the user wants to stop it.
*/ */
int usb_timeout; int usbbus_timeout;
int remaining_time = timeout; int remaining_time = timeout;
read: read:
if (timeout == USB_INFINITE_TIMEOUT) { if (timeout == USBBUS_INFINITE_TIMEOUT) {
usb_timeout = USB_TIMEOUT_PER_PASS; usbbus_timeout = USBBUS_TIMEOUT_PER_PASS;
} else { } else {
// A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism
remaining_time -= USB_TIMEOUT_PER_PASS; remaining_time -= USBBUS_TIMEOUT_PER_PASS;
if (remaining_time <= 0) { if (remaining_time <= 0) {
pnd->last_error = NFC_ETIMEOUT; pnd->last_error = NFC_ETIMEOUT;
return pnd->last_error; return pnd->last_error;
} else { } else {
usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); usbbus_timeout = MIN(remaining_time, USBBUS_TIMEOUT_PER_PASS);
} }
} }
res = pn53x_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), usb_timeout); res = pn53x_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), usbbus_timeout);
if (res == -USB_TIMEDOUT) { if (res == -USBBUS_TIMEDOUT) {
if (DRIVER_DATA(pnd)->abort_flag) { if (DRIVER_DATA(pnd)->abort_flag) {
DRIVER_DATA(pnd)->abort_flag = false; DRIVER_DATA(pnd)->abort_flag = false;
pn53x_usb_ack(pnd); pn53x_usb_ack(pnd);