From b12cc161c465424b8c1df07d18e174d7f75b62ea Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 13 Oct 2013 16:50:45 +0200 Subject: [PATCH] Isolate libusb dependencies in buses/usbbus.c --- libnfc/buses/usbbus.c | 74 ++++++++++++++++- libnfc/buses/usbbus.h | 154 +++++++++++++++++++++++++++++++++--- libnfc/drivers/acr122_usb.c | 96 +++++++++++----------- libnfc/drivers/pn53x_usb.c | 102 ++++++++++++------------ 4 files changed, 311 insertions(+), 115 deletions(-) diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c index d2477f5..7368c3f 100644 --- a/libnfc/buses/usbbus.c +++ b/libnfc/buses/usbbus.c @@ -36,12 +36,20 @@ #include +#ifndef _WIN32 +// Under POSIX system, we use libusb (>= 0.1.12) +#include +#else +// Under Windows we use libusb-win32 (>= 1.2.5) +#include +#endif + #include "usbbus.h" #include "log.h" #define LOG_CATEGORY "libnfc.buses.usbbus" #define LOG_GROUP NFC_LOG_GROUP_DRIVER -int usb_prepare(void) +int usbbus_prepare(void) { static bool usb_initialized = false; if (!usb_initialized) { @@ -64,16 +72,76 @@ int usb_prepare(void) // 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_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; } // 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_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 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(); +} + diff --git a/libnfc/buses/usbbus.h b/libnfc/buses/usbbus.h index 87f6a85..f5fc5fb 100644 --- a/libnfc/buses/usbbus.h +++ b/libnfc/buses/usbbus.h @@ -33,21 +33,149 @@ #ifndef __NFC_BUS_USB_H__ # define __NFC_BUS_USB_H__ -#ifndef _WIN32 -// Under POSIX system, we use libusb (>= 0.1.12) -#include -#define USB_TIMEDOUT ETIMEDOUT -#define _usb_strerror( X ) strerror(-X) -#else -// Under Windows we use libusb-win32 (>= 1.2.5) -#include -#define USB_TIMEDOUT 116 -#define _usb_strerror( X ) usb_strerror() -#endif - #include #include -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__ diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 8a16920..e7c959e 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -74,7 +74,7 @@ Thanks to d18c7db and Okko for example code #define LOG_GROUP NFC_LOG_GROUP_DRIVER #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)) @@ -172,7 +172,7 @@ struct acr122_usb_apdu_frame { // Internal data struct struct acr122_usb_data { - usb_dev_handle *pudh; + usbbus_dev_handle *pudh; uint32_t uiEndPointIn; uint32_t uiEndPointOut; uint32_t uiMaxPacketSize; @@ -218,13 +218,13 @@ static int acr122_usb_send_apdu(nfc_device *pnd, static int 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) { LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res); } else if (res < 0) { - if (res != -USB_TIMEDOUT) { + if (res != -USBBUS_TIMEDOUT) { 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 { 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) { 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) { // 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); + usbbus_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout); } } else if (res < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res)); - if (res == -USB_TIMEDOUT) { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", usbbus_strerror(res)); + if (res == -USBBUS_TIMEDOUT) { res = NFC_ETIMEOUT; } else { res = NFC_EIO; @@ -266,28 +266,28 @@ const struct acr122_usb_supported_device acr122_usb_supported_devices[] = { // Find transfer endpoints for bulk transfers 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 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 for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) { // 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; // 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) { + if ((uiEndPoint & USBBUS_ENDPOINT_DIR_MASK) == USBBUS_ENDPOINT_IN) { data->uiEndPointIn = uiEndPoint; data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; } // 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->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; } @@ -299,13 +299,13 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const { (void)context; - usb_prepare(); + usbbus_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; - struct usb_bus *bus; - for (bus = usb_get_busses(); bus; bus = bus->next) { - struct usb_device *dev; + struct usbbus_bus *bus; + for (bus = usbbus_get_busses(); bus; bus = bus->next) { + struct usbbus_device *dev; 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++) { @@ -322,14 +322,14 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const continue; } - usb_dev_handle *udev = usb_open(dev); + usbbus_dev_handle *udev = usbbus_open(dev); if (udev == NULL) continue; // Set configuration // 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); - usb_close(udev); + usbbus_close(udev); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename); device_found++; // Test if we reach the maximum "wanted" devices @@ -350,16 +350,16 @@ struct acr122_usb_descriptor { }; 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'; if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) { 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) 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, .uiEndPointOut = 0, }; - struct usb_bus *bus; - struct usb_device *dev; + struct usbbus_bus *bus; + 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) { // A specific bus have been specified if (0 != strcmp(bus->dirname, desc.dirname)) @@ -411,25 +411,25 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) continue; } // Open the USB device - if ((data.pudh = usb_open(dev)) == NULL) + if ((data.pudh = usbbus_open(dev)) == NULL) continue; // Reset device - usb_reset(data.pudh); + usbbus_reset(data.pudh); // Retrieve end points acr122_usb_get_end_points(dev, &data); // Claim interface - int res = usb_claim_interface(data.pudh, 0); + int res = usbbus_claim_interface(data.pudh, 0); if (res < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); - usb_close(data.pudh); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", usbbus_strerror(res)); + usbbus_close(data.pudh); // we failed to use the specified device goto free_mem; } - res = usb_set_altinterface(data.pudh, 0); + res = usbbus_set_altinterface(data.pudh, 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)); - usb_close(data.pudh); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", usbbus_strerror(res)); + usbbus_close(data.pudh); // we failed to use the specified device goto free_mem; } @@ -461,7 +461,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) pnd->driver = &acr122_usb_driver; if (acr122_usb_init(pnd) < 0) { - usb_close(data.pudh); + usbbus_close(data.pudh); goto error; } DRIVER_DATA(pnd)->abort_flag = false; @@ -488,12 +488,12 @@ acr122_usb_close(nfc_device *pnd) pn53x_idle(pnd); int res; - if ((res = usb_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)); + 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)", usbbus_strerror(res)); } - if ((res = usb_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)); + 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)", usbbus_strerror(res)); } pn53x_data_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; } -#define USB_TIMEOUT_PER_PASS 200 +#define USBBUS_TIMEOUT_PER_PASS 200 static int 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; /* - * 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. */ - int usb_timeout; + int usbbus_timeout; int remaining_time = timeout; read: - if (timeout == USB_INFINITE_TIMEOUT) { - usb_timeout = USB_TIMEOUT_PER_PASS; + if (timeout == USBBUS_INFINITE_TIMEOUT) { + usbbus_timeout = USBBUS_TIMEOUT_PER_PASS; } 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 - remaining_time -= USB_TIMEOUT_PER_PASS; + remaining_time -= USBBUS_TIMEOUT_PER_PASS; if (remaining_time <= 0) { pnd->last_error = NFC_ETIMEOUT; return pnd->last_error; } 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; size_t len; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index e75a086..ac1a570 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -56,7 +56,7 @@ Thanks to d18c7db and Okko for example code #define LOG_CATEGORY "libnfc.driver.pn53x_usb" #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)) @@ -72,7 +72,7 @@ typedef enum { // Internal data struct struct pn53x_usb_data { - usb_dev_handle *pudh; + usbbus_dev_handle *pudh; pn53x_usb_model model; uint32_t uiEndPointIn; uint32_t uiEndPointOut; @@ -84,18 +84,18 @@ struct pn53x_usb_data { const struct pn53x_io pn53x_usb_io; // 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); static int 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) { LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res); } else if (res < 0) { - if (res != -USB_TIMEDOUT) - log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); + if (res != -USBBUS_TIMEDOUT) + log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", usbbus_strerror(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) { 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) { // 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); + usbbus_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout); } } 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; } @@ -148,28 +148,28 @@ 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 usbbus_device *dev, struct pn53x_usb_data *data) { uint32_t uiIndex; 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 for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) { // 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; // 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) { + if ((uiEndPoint & USBBUS_ENDPOINT_DIR_MASK) == USBBUS_ENDPOINT_IN) { data->uiEndPointIn = uiEndPoint; data->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; } // 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->uiMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize; } @@ -181,13 +181,13 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s { (void)context; - usb_prepare(); + usbbus_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; - struct usb_bus *bus; - for (bus = usb_get_busses(); bus; bus = bus->next) { - struct usb_device *dev; + struct usbbus_bus *bus; + for (bus = usbbus_get_busses(); bus; bus = bus->next) { + struct usbbus_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++) { @@ -204,22 +204,22 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s continue; } - usb_dev_handle *udev = usb_open(dev); + usbbus_dev_handle *udev = usbbus_open(dev); if (udev == NULL) continue; // Set configuration - int res = usb_set_configuration(udev, 1); + int res = usbbus_set_configuration(udev, 1); if (res < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); - usb_close(udev); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", usbbus_strerror(res)); + usbbus_close(udev); // we failed to use the device continue; } // 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); - usb_close(udev); + usbbus_close(udev); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); device_found++; // Test if we reach the maximum "wanted" devices @@ -240,16 +240,16 @@ struct pn53x_usb_descriptor { }; 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'; if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) { 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) 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, .uiEndPointOut = 0, }; - struct usb_bus *bus; - struct usb_device *dev; + struct usbbus_bus *bus; + 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) { // A specific bus have been specified if (0 != strcmp(bus->dirname, desc.dirname)) @@ -301,26 +301,26 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) continue; } // Open the USB device - if ((data.pudh = usb_open(dev)) == NULL) + if ((data.pudh = usbbus_open(dev)) == NULL) continue; // Retrieve end points pn53x_usb_get_end_points(dev, &data); // Set configuration - int res = usb_set_configuration(data.pudh, 1); + int res = usbbus_set_configuration(data.pudh, 1); 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) { 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 goto free_mem; } - res = usb_claim_interface(data.pudh, 0); + res = usbbus_claim_interface(data.pudh, 0); if (res < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); - usb_close(data.pudh); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", usbbus_strerror(res)); + usbbus_close(data.pudh); // we failed to use the specified device 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 // 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); + usbbus_close(data.pudh); goto error; } DRIVER_DATA(pnd)->abort_flag = false; @@ -408,12 +408,12 @@ pn53x_usb_close(nfc_device *pnd) pn53x_idle(pnd); int res; - if ((res = usb_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)); + 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)", usbbus_strerror(res)); } - if ((res = usb_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)); + 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)", usbbus_strerror(res)); } pn53x_data_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; } -#define USB_TIMEOUT_PER_PASS 200 +#define USBBUS_TIMEOUT_PER_PASS 200 static int 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; /* - * 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. */ - int usb_timeout; + int usbbus_timeout; int remaining_time = timeout; read: - if (timeout == USB_INFINITE_TIMEOUT) { - usb_timeout = USB_TIMEOUT_PER_PASS; + if (timeout == USBBUS_INFINITE_TIMEOUT) { + usbbus_timeout = USBBUS_TIMEOUT_PER_PASS; } 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 - remaining_time -= USB_TIMEOUT_PER_PASS; + remaining_time -= USBBUS_TIMEOUT_PER_PASS; if (remaining_time <= 0) { pnd->last_error = NFC_ETIMEOUT; return pnd->last_error; } 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) { DRIVER_DATA(pnd)->abort_flag = false; pn53x_usb_ack(pnd);