libusb-compat: get rid of usbbus_dev_handle

This commit is contained in:
Philippe Teuwen 2013-10-15 01:21:56 +02:00
parent 1a6217060a
commit 2334a35202
4 changed files with 44 additions and 78 deletions

View File

@ -76,16 +76,6 @@
ent->next = NULL; \ ent->next = NULL; \
} while (0) } while (0)
struct usbbus_dev_handle {
libusb_device_handle *handle;
struct usbbus_device *device;
/* libusb-0.1 is buggy w.r.t. interface claiming. it allows you to claim
* multiple interfaces but only tracks the most recently claimed one,
* which is used for usb_set_altinterface(). we clone the buggy behaviour
* here. */
int last_claimed_interface;
};
#define USBBUS_DT_CONFIG_SIZE 9 #define USBBUS_DT_CONFIG_SIZE 9
#define USBBUS_DT_INTERFACE_SIZE 9 #define USBBUS_DT_INTERFACE_SIZE 9
#define USBBUS_DT_ENDPOINT_AUDIO_SIZE 9 #define USBBUS_DT_ENDPOINT_AUDIO_SIZE 9
@ -640,53 +630,40 @@ int usbbus_prepare(void)
return 0; return 0;
} }
usbbus_dev_handle *usbbus_open(struct usbbus_device *dev) usbbus_device_handle *usbbus_open(struct usbbus_device *dev)
{ {
int r; int r;
usbbus_dev_handle *udev = malloc(sizeof(*udev)); usbbus_device_handle *udev;
if (!udev) r = libusb_open((libusb_device *) dev->dev, (libusb_device_handle **)&udev);
return NULL;
r = libusb_open((libusb_device *) dev->dev, &udev->handle);
if (r < 0) { if (r < 0) {
free(udev);
errno = libusb_to_errno(r); errno = libusb_to_errno(r);
return NULL; return NULL;
} }
return (usbbus_device_handle *)udev;
udev->last_claimed_interface = -1;
udev->device = dev;
return udev;
} }
int usbbus_close(usbbus_dev_handle *dev) void usbbus_close(usbbus_device_handle *dev)
{ {
libusb_close(dev->handle); libusb_close((libusb_device_handle *)dev);
free(dev);
return 0;
} }
int usbbus_set_configuration(usbbus_dev_handle *dev, int configuration) int usbbus_set_configuration(usbbus_device_handle *dev, int configuration)
{ {
return compat_err(libusb_set_configuration(dev->handle, configuration)); return compat_err(libusb_set_configuration((libusb_device_handle *)dev, configuration));
} }
int usbbus_get_string_simple(usbbus_dev_handle *dev, int index, char *buf, size_t buflen) int usbbus_get_string_simple(usbbus_device_handle *dev, int index, char *buf, size_t buflen)
{ {
int r; return libusb_get_string_descriptor_ascii((libusb_device_handle *)dev, index & 0xff,
r = libusb_get_string_descriptor_ascii(dev->handle, index & 0xff,
(unsigned char *) buf, (int) buflen); (unsigned char *) buf, (int) buflen);
if (r >= 0)
return r;
return compat_err(r);
} }
static int usbbus_bulk_io(usbbus_dev_handle *dev, int ep, unsigned char *bytes, static int usbbus_bulk_io(usbbus_device_handle *dev, int ep, unsigned char *bytes,
int size, int timeout) int size, int timeout)
{ {
int actual_length; int actual_length;
int r; int r;
r = libusb_bulk_transfer(dev->handle, ep & 0xff, bytes, size, r = libusb_bulk_transfer((libusb_device_handle *)dev, ep & 0xff, bytes, size,
&actual_length, timeout); &actual_length, timeout);
/* if we timed out but did transfer some data, report as successful short /* if we timed out but did transfer some data, report as successful short
@ -697,46 +674,39 @@ static int usbbus_bulk_io(usbbus_dev_handle *dev, int ep, unsigned char *bytes,
return compat_err(r); return compat_err(r);
} }
int usbbus_bulk_read(usbbus_dev_handle *dev, int ep, char *bytes, int size, int timeout) int usbbus_bulk_read(usbbus_device_handle *dev, int ep, char *bytes, int size, int timeout)
{ {
return usbbus_bulk_io(dev, ep, (unsigned char *) bytes, size, timeout); return usbbus_bulk_io(dev, ep, (unsigned char *) bytes, size, timeout);
} }
int usbbus_bulk_write(usbbus_dev_handle *dev, int ep, const char *bytes, int size, int timeout) int usbbus_bulk_write(usbbus_device_handle *dev, int ep, const char *bytes, int size, int timeout)
{ {
return usbbus_bulk_io(dev, ep, (unsigned char *)bytes, size, timeout); return usbbus_bulk_io(dev, ep, (unsigned char *)bytes, size, timeout);
} }
int usbbus_claim_interface(usbbus_dev_handle *dev, int interface) int usbbus_claim_interface(usbbus_device_handle *dev, int interface)
{ {
int r; int r;
r = libusb_claim_interface(dev->handle, interface); r = libusb_claim_interface((libusb_device_handle *)dev, interface);
if (r == 0) { if (r == 0) {
dev->last_claimed_interface = interface;
return 0; return 0;
} }
return compat_err(r); return compat_err(r);
} }
int usbbus_release_interface(usbbus_dev_handle *dev, int interface) int usbbus_release_interface(usbbus_device_handle *dev, int interface)
{ {
int r; return compat_err(libusb_release_interface((libusb_device_handle *)dev, interface));
r = libusb_release_interface(dev->handle, interface);
if (r == 0)
dev->last_claimed_interface = -1;
return compat_err(r);
} }
int usbbus_set_altinterface(usbbus_dev_handle *dev, int alternate) int usbbus_set_interface_alt_setting(usbbus_device_handle *dev, int interface, int alternate)
{ {
if (dev->last_claimed_interface < 0) return libusb_set_interface_alt_setting((libusb_device_handle *)dev, interface, alternate);
return -(errno = EINVAL);
return compat_err(libusb_set_interface_alt_setting(dev->handle, dev->last_claimed_interface, alternate));
} }
int usbbus_reset(usbbus_dev_handle *dev) int usbbus_reset(usbbus_device_handle *dev)
{ {
return compat_err(libusb_reset_device(dev->handle)); return compat_err(libusb_reset_device((libusb_device_handle *)dev));
} }
struct usbbus_bus *usbbus_get_busses(void) struct usbbus_bus *usbbus_get_busses(void)

View File

@ -60,8 +60,8 @@ int usbbus_prepare(void);
#define USBBUS_PATH_MAX 4096 #define USBBUS_PATH_MAX 4096
#endif #endif
struct usbbus_dev_handle; struct usbbus_device_handle;
typedef struct usbbus_dev_handle usbbus_dev_handle; typedef struct usbbus_device_handle usbbus_device_handle;
struct usbbus_endpoint_descriptor { struct usbbus_endpoint_descriptor {
uint8_t bLength; uint8_t bLength;
@ -166,16 +166,16 @@ struct usbbus_bus {
}; };
usbbus_dev_handle *usbbus_open(struct usbbus_device *dev); usbbus_device_handle *usbbus_open(struct usbbus_device *dev);
int usbbus_close(usbbus_dev_handle *dev); void usbbus_close(usbbus_device_handle *dev);
int usbbus_set_configuration(usbbus_dev_handle *dev, int configuration); int usbbus_set_configuration(usbbus_device_handle *dev, int configuration);
int usbbus_get_string_simple(usbbus_dev_handle *dev, int index, char *buf, size_t buflen); int usbbus_get_string_simple(usbbus_device_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_read(usbbus_device_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_bulk_write(usbbus_device_handle *dev, int ep, const char *bytes, int size, int timeout);
int usbbus_claim_interface(usbbus_dev_handle *dev, int interface); int usbbus_claim_interface(usbbus_device_handle *dev, int interface);
int usbbus_release_interface(usbbus_dev_handle *dev, int interface); int usbbus_release_interface(usbbus_device_handle *dev, int interface);
int usbbus_set_altinterface(usbbus_dev_handle *dev, int alternate); int usbbus_set_interface_alt_setting(usbbus_device_handle *dev, int interface, int alternate);
int usbbus_reset(usbbus_dev_handle *dev); int usbbus_reset(usbbus_device_handle *dev);
struct usbbus_bus *usbbus_get_busses(void); struct usbbus_bus *usbbus_get_busses(void);
#endif // __NFC_BUS_USB_H__ #endif // __NFC_BUS_USB_H__

View File

@ -172,7 +172,7 @@ struct acr122_usb_apdu_frame {
// Internal data struct // Internal data struct
struct acr122_usb_data { struct acr122_usb_data {
usbbus_dev_handle *pudh; usbbus_device_handle *pudh;
uint32_t uiEndPointIn; uint32_t uiEndPointIn;
uint32_t uiEndPointOut; uint32_t uiEndPointOut;
uint32_t uiMaxPacketSize; uint32_t uiMaxPacketSize;
@ -322,7 +322,7 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const
continue; continue;
} }
usbbus_dev_handle *udev = usbbus_open(dev); usbbus_device_handle *udev = usbbus_open(dev);
if (udev == NULL) if (udev == NULL)
continue; continue;
@ -350,7 +350,7 @@ struct acr122_usb_descriptor {
}; };
static bool static bool
acr122_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_dev_handle *udev, char *buffer, size_t len) acr122_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_device_handle *udev, char *buffer, size_t len)
{ {
*buffer = '\0'; *buffer = '\0';
@ -426,7 +426,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
goto free_mem; goto free_mem;
} }
res = usbbus_set_altinterface(data.pudh, 0); res = usbbus_set_interface_alt_setting(data.pudh, 0, 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)", usbbus_strerror(res)); 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); usbbus_close(data.pudh);
@ -492,9 +492,7 @@ acr122_usb_close(nfc_device *pnd)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", usbbus_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", usbbus_strerror(res));
} }
if ((res = usbbus_close(DRIVER_DATA(pnd)->pudh)) < 0) { usbbus_close(DRIVER_DATA(pnd)->pudh);
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);
} }

View File

@ -72,7 +72,7 @@ typedef enum {
// Internal data struct // Internal data struct
struct pn53x_usb_data { struct pn53x_usb_data {
usbbus_dev_handle *pudh; usbbus_device_handle *pudh;
pn53x_usb_model model; pn53x_usb_model model;
uint32_t uiEndPointIn; uint32_t uiEndPointIn;
uint32_t uiEndPointOut; uint32_t uiEndPointOut;
@ -84,7 +84,7 @@ 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 usbbus_device *dev, usbbus_dev_handle *udev, char *buffer, size_t len); bool pn53x_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_device_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
@ -204,7 +204,7 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s
continue; continue;
} }
usbbus_dev_handle *udev = usbbus_open(dev); usbbus_device_handle *udev = usbbus_open(dev);
if (udev == NULL) if (udev == NULL)
continue; continue;
@ -240,7 +240,7 @@ struct pn53x_usb_descriptor {
}; };
bool bool
pn53x_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_dev_handle *udev, char *buffer, size_t len) pn53x_usb_get_usb_device_name(struct usbbus_device *dev, usbbus_device_handle *udev, char *buffer, size_t len)
{ {
*buffer = '\0'; *buffer = '\0';
@ -412,9 +412,7 @@ pn53x_usb_close(nfc_device *pnd)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", usbbus_strerror(res)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", usbbus_strerror(res));
} }
if ((res = usbbus_close(DRIVER_DATA(pnd)->pudh)) < 0) { usbbus_close(DRIVER_DATA(pnd)->pudh);
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);
} }