diff --git a/libnfc/buses/libusb-compat-core.c b/libnfc/buses/libusb-compat-core.c index e798966..c331ce3 100644 --- a/libnfc/buses/libusb-compat-core.c +++ b/libnfc/buses/libusb-compat-core.c @@ -33,13 +33,6 @@ static libusb_context *ctx = NULL; static int usb_debug = 0; -enum usbi_log_level { - LOG_LEVEL_DEBUG, - LOG_LEVEL_INFO, - LOG_LEVEL_WARNING, - LOG_LEVEL_ERROR, -}; - #ifdef ENABLE_LOGGING #define _usbi_log(level, fmt...) usbi_log(level, __FUNCTION__, fmt) #else @@ -94,49 +87,6 @@ static int libusb_to_errno(int result) } } -static void usbi_log(enum usbi_log_level level, const char *function, - const char *format, ...) -{ - va_list args; - FILE *stream = stdout; - const char *prefix; - -#ifndef ENABLE_DEBUG_LOGGING - if (!usb_debug) - return; -#endif - - switch (level) { - case LOG_LEVEL_INFO: - prefix = "info"; - break; - case LOG_LEVEL_WARNING: - stream = stderr; - prefix = "warning"; - break; - case LOG_LEVEL_ERROR: - stream = stderr; - prefix = "error"; - break; - case LOG_LEVEL_DEBUG: - stream = stderr; - prefix = "debug"; - break; - default: - stream = stderr; - prefix = "unknown"; - break; - } - - fprintf(stream, "libusb-compat %s: %s: ", prefix, function); - - va_start(args, format); - vfprintf(stream, format, args); - va_end(args); - - fprintf(stream, "\n"); -} - static void _usb_finalize(void) { if (ctx) { @@ -740,17 +690,6 @@ int usb_set_altinterface(usb_dev_handle *dev, int alternate) dev->last_claimed_interface, alternate)); } -int usb_resetep(usb_dev_handle *dev, unsigned int ep) -{ - return compat_err(usb_clear_halt(dev, ep)); -} - -int usb_clear_halt(usb_dev_handle *dev, unsigned int ep) -{ - usbi_dbg("endpoint %x", ep); - return compat_err(libusb_clear_halt(dev->handle, ep & 0xff)); -} - int usb_reset(usb_dev_handle *dev) { usbi_dbg(""); @@ -775,7 +714,7 @@ static int usb_bulk_io(usb_dev_handle *dev, int ep, char *bytes, } int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, - int size, int timeout) + int size, int timeout) { if (!(ep & USB_ENDPOINT_IN)) { /* libusb-0.1 will strangely fix up a read request from endpoint @@ -789,7 +728,7 @@ int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, } int usb_bulk_write(usb_dev_handle *dev, int ep, const char *bytes, - int size, int timeout) + int size, int timeout) { if (ep & USB_ENDPOINT_IN) { /* libusb-0.1 on BSD strangely fix up a write request to endpoint @@ -802,80 +741,8 @@ int usb_bulk_write(usb_dev_handle *dev, int ep, const char *bytes, return usb_bulk_io(dev, ep, (char *)bytes, size, timeout); } -static int usb_interrupt_io(usb_dev_handle *dev, int ep, char *bytes, - int size, int timeout) -{ - int actual_length; - int r; - usbi_dbg("endpoint %x size %d timeout %d", ep, size, timeout); - r = libusb_interrupt_transfer(dev->handle, ep & 0xff, bytes, size, - &actual_length, timeout); - - /* if we timed out but did transfer some data, report as successful short - * read. FIXME: is this how libusb-0.1 works? */ - if (r == 0 || (r == LIBUSB_ERROR_TIMEOUT && actual_length > 0)) - return actual_length; - - return compat_err(r); -} - -int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, - int size, int timeout) -{ - if (!(ep & USB_ENDPOINT_IN)) { - /* libusb-0.1 will strangely fix up a read request from endpoint - * 0x01 to be from endpoint 0x81. do the same thing here, but - * warn about this silly behaviour. */ - usbi_warn("endpoint %x is missing IN direction bit, fixing"); - ep |= USB_ENDPOINT_IN; - } - return usb_interrupt_io(dev, ep, bytes, size, timeout); -} - -int usb_interrupt_write(usb_dev_handle *dev, int ep, const char *bytes, - int size, int timeout) -{ - if (ep & USB_ENDPOINT_IN) { - /* libusb-0.1 on BSD strangely fix up a write request to endpoint - * 0x81 to be to endpoint 0x01. do the same thing here, but - * warn about this silly behaviour. */ - usbi_warn("endpoint %x has excessive IN direction bit, fixing"); - ep &= ~USB_ENDPOINT_IN; - } - - return usb_interrupt_io(dev, ep, (char *)bytes, size, timeout); -} - -int usb_control_msg(usb_dev_handle *dev, int bmRequestType, - int bRequest, int wValue, int wIndex, char *bytes, int size, int timeout) -{ - int r; - usbi_dbg("RQT=%x RQ=%x V=%x I=%x len=%d timeout=%d", bmRequestType, - bRequest, wValue, wIndex, size, timeout); - - r = libusb_control_transfer(dev->handle, bmRequestType & 0xff, - bRequest & 0xff, wValue & 0xffff, wIndex & 0xffff, bytes, size & 0xffff, - timeout); - - if (r >= 0) - return r; - - return compat_err(r); -} - -int usb_get_string(usb_dev_handle *dev, int desc_index, int langid, - char *buf, size_t buflen) -{ - int r; - r = libusb_get_string_descriptor(dev->handle, desc_index & 0xff, - langid & 0xffff, buf, (int) buflen); - if (r >= 0) - return r; - return compat_err(r); -} - int usb_get_string_simple(usb_dev_handle *dev, int desc_index, - char *buf, size_t buflen) + char *buf, size_t buflen) { int r; r = libusb_get_string_descriptor_ascii(dev->handle, desc_index & 0xff, @@ -884,65 +751,3 @@ int usb_get_string_simple(usb_dev_handle *dev, int desc_index, return r; return compat_err(r); } - -int usb_get_descriptor(usb_dev_handle *dev, unsigned char type, - unsigned char desc_index, void *buf, int size) -{ - int r; - r = libusb_get_descriptor(dev->handle, type, desc_index, buf, size); - if (r >= 0) - return r; - return compat_err(r); -} - -int usb_get_descriptor_by_endpoint(usb_dev_handle *dev, int ep, - unsigned char type, unsigned char desc_index, void *buf, int size) -{ - /* this function doesn't make much sense - the specs don't talk about - * getting a descriptor "by endpoint". libusb-1.0 does not provide this - * functionality so we just send a control message directly */ - int r; - r = libusb_control_transfer(dev->handle, - LIBUSB_ENDPOINT_IN | (ep & 0xff), LIBUSB_REQUEST_GET_DESCRIPTOR, - (type << 8) | desc_index, 0, buf, size, 1000); - if (r >= 0) - return r; - return compat_err(r); -} - -int usb_get_driver_np(usb_dev_handle *dev, int interface, - char *name, unsigned int namelen) -{ - int r = libusb_kernel_driver_active(dev->handle, interface); - if (r == 1) { - /* libusb-1.0 doesn't expose driver name, so fill in a dummy value */ - snprintf(name, namelen, "dummy"); - return 0; - } else if (r == 0) { - return -(errno = ENODATA); - } else { - return compat_err(r); - } -} - -int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface) -{ - int r = compat_err(libusb_detach_kernel_driver(dev->handle, interface)); - switch (r) { - case LIBUSB_SUCCESS: - return 0; - case LIBUSB_ERROR_NOT_FOUND: - return -ENODATA; - case LIBUSB_ERROR_INVALID_PARAM: - return -EINVAL; - case LIBUSB_ERROR_NO_DEVICE: - return -ENODEV; - case LIBUSB_ERROR_OTHER: - return -errno; - /* default can be reached only in non-Linux implementations, - * mostly with LIBUSB_ERROR_NOT_SUPPORTED. */ - default: - return -ENOSYS; - } -} - diff --git a/libnfc/buses/libusb-compat-usb.h b/libnfc/buses/libusb-compat-usb.h index 0cc747b..c4f1a8d 100644 --- a/libnfc/buses/libusb-compat-usb.h +++ b/libnfc/buses/libusb-compat-usb.h @@ -39,43 +39,9 @@ * not subject to change */ -/* - * Device and/or Interface Class codes - */ -#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ -#define USB_CLASS_AUDIO 1 -#define USB_CLASS_COMM 2 -#define USB_CLASS_HID 3 -#define USB_CLASS_PRINTER 7 -#define USB_CLASS_PTP 6 -#define USB_CLASS_MASS_STORAGE 8 -#define USB_CLASS_HUB 9 -#define USB_CLASS_DATA 10 -#define USB_CLASS_VENDOR_SPEC 0xff - -/* - * Descriptor types - */ -#define USB_DT_DEVICE 0x01 -#define USB_DT_CONFIG 0x02 -#define USB_DT_STRING 0x03 -#define USB_DT_INTERFACE 0x04 -#define USB_DT_ENDPOINT 0x05 - -#define USB_DT_HID 0x21 -#define USB_DT_REPORT 0x22 -#define USB_DT_PHYSICAL 0x23 -#define USB_DT_HUB 0x29 - -/* - * Descriptor sizes per descriptor type - */ -#define USB_DT_DEVICE_SIZE 18 -#define USB_DT_CONFIG_SIZE 9 -#define USB_DT_INTERFACE_SIZE 9 -#define USB_DT_ENDPOINT_SIZE 7 -#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ -#define USB_DT_HUB_NONVAR_SIZE 7 +#define USB_DT_CONFIG_SIZE 9 +#define USB_DT_INTERFACE_SIZE 9 +#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* All standard descriptors have these 2 fields in common */ struct usb_descriptor_header { @@ -90,18 +56,6 @@ struct usb_string_descriptor { u_int16_t wData[1]; }; -/* HID descriptor */ -struct usb_hid_descriptor { - u_int8_t bLength; - u_int8_t bDescriptorType; - u_int16_t bcdHID; - u_int8_t bCountryCode; - u_int8_t bNumDescriptors; - /* u_int8_t bReportDescriptorType; */ - /* u_int16_t wDescriptorLength; */ - /* ... */ -}; - /* Endpoint descriptor */ #define USB_MAXENDPOINTS 32 struct usb_endpoint_descriptor { @@ -189,41 +143,6 @@ struct usb_device_descriptor { u_int8_t bNumConfigurations; }; -struct usb_ctrl_setup { - u_int8_t bRequestType; - u_int8_t bRequest; - u_int16_t wValue; - u_int16_t wIndex; - u_int16_t wLength; -}; - -/* - * Standard requests - */ -#define USB_REQ_GET_STATUS 0x00 -#define USB_REQ_CLEAR_FEATURE 0x01 -/* 0x02 is reserved */ -#define USB_REQ_SET_FEATURE 0x03 -/* 0x04 is reserved */ -#define USB_REQ_SET_ADDRESS 0x05 -#define USB_REQ_GET_DESCRIPTOR 0x06 -#define USB_REQ_SET_DESCRIPTOR 0x07 -#define USB_REQ_GET_CONFIGURATION 0x08 -#define USB_REQ_SET_CONFIGURATION 0x09 -#define USB_REQ_GET_INTERFACE 0x0A -#define USB_REQ_SET_INTERFACE 0x0B -#define USB_REQ_SYNCH_FRAME 0x0C - -#define USB_TYPE_STANDARD (0x00 << 5) -#define USB_TYPE_CLASS (0x01 << 5) -#define USB_TYPE_VENDOR (0x02 << 5) -#define USB_TYPE_RESERVED (0x03 << 5) - -#define USB_RECIP_DEVICE 0x00 -#define USB_RECIP_INTERFACE 0x01 -#define USB_RECIP_ENDPOINT 0x02 -#define USB_RECIP_OTHER 0x03 - /* * Various libusb API related stuff */ @@ -287,46 +206,23 @@ extern "C" { /* usb.c */ usb_dev_handle *usb_open(struct usb_device *dev); int usb_close(usb_dev_handle *dev); -int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, - size_t buflen); int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); -/* descriptors.c */ -int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, - unsigned char type, unsigned char index, void *buf, int size); -int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, - unsigned char index, void *buf, int size); - /* .c */ int usb_bulk_write(usb_dev_handle *dev, int ep, const char *bytes, int size, int timeout); int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout); -int usb_interrupt_write(usb_dev_handle *dev, int ep, const char *bytes, - int size, int timeout); -int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, - int timeout); -int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, - int value, int index, char *bytes, int size, int timeout); int usb_set_configuration(usb_dev_handle *dev, int configuration); int usb_claim_interface(usb_dev_handle *dev, int interface); int usb_release_interface(usb_dev_handle *dev, int interface); int usb_set_altinterface(usb_dev_handle *dev, int alternate); -int usb_resetep(usb_dev_handle *dev, unsigned int ep); -int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); int usb_reset(usb_dev_handle *dev); -#define LIBUSB_HAS_GET_DRIVER_NP 1 -int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, - unsigned int namelen); -#define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1 -int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface); - char *usb_strerror(void); void usb_init(void); -void usb_set_debug(int level); int usb_find_busses(void); int usb_find_devices(void); struct usb_device *usb_device(usb_dev_handle *dev); diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c index 7cf6c1e..86d32e4 100644 --- a/libnfc/buses/usbbus.c +++ b/libnfc/buses/usbbus.c @@ -38,9 +38,9 @@ #ifndef _WIN32 // #ifdef LIBUSB10_ENABLED - #include "libusb-compat-usb.h" +#include "libusb-compat-usb.h" // #else - // Under POSIX system, we use libusb (>= 0.1.12) +// Under POSIX system, we use libusb (>= 0.1.12) // #include // #endif #else diff --git a/libnfc/buses/usbbus.h b/libnfc/buses/usbbus.h index f5fc5fb..589568c 100644 --- a/libnfc/buses/usbbus.h +++ b/libnfc/buses/usbbus.h @@ -46,6 +46,9 @@ #define usbbus_strerror( X ) usb_strerror() #endif +int usbbus_prepare(void); + +// Libusb-0.1 API: #define USBBUS_ENDPOINT_DIR_MASK 0x80 #define USBBUS_ENDPOINT_TYPE_BULK 2 #define USBBUS_ENDPOINT_IN 0x80 @@ -164,7 +167,6 @@ struct usbbus_bus { }; -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);