Update libusb dependency to 1.0.27

This commit is contained in:
James Buren 2024-08-04 23:36:00 -05:00
parent af3dc18841
commit bdad1df3d7
103 changed files with 2120 additions and 918 deletions

View file

@ -239,7 +239,7 @@ win32:INCLUDEPATH +=
dependencies/display-library/include \ dependencies/display-library/include \
dependencies/hidapi/hidapi \ dependencies/hidapi/hidapi \
dependencies/winring0/include \ dependencies/winring0/include \
dependencies/libusb-1.0.22/include/libusb-1.0 \ dependencies/libusb-1.0.27/include \
dependencies/mbedtls-2.24.0/include \ dependencies/mbedtls-2.24.0/include \
dependencies/NVFC \ dependencies/NVFC \
wmi/ \ wmi/ \
@ -363,7 +363,7 @@ win32:contains(QMAKE_TARGET.arch, x86_64) {
-lws2_32 \ -lws2_32 \
-liphlpapi \ -liphlpapi \
-L"$$PWD/dependencies/winring0/x64/" -lWinRing0x64 \ -L"$$PWD/dependencies/winring0/x64/" -lWinRing0x64 \
-L"$$PWD/dependencies/libusb-1.0.22/MS64/dll" -llibusb-1.0 \ -L"$$PWD/dependencies/libusb-1.0.27/VS2019/MS64/dll" -llibusb-1.0 \
-L"$$PWD/dependencies/hidapi-win/x64/" -lhidapi \ -L"$$PWD/dependencies/hidapi-win/x64/" -lhidapi \
} }
@ -372,7 +372,7 @@ win32:contains(QMAKE_TARGET.arch, x86) {
-lws2_32 \ -lws2_32 \
-liphlpapi \ -liphlpapi \
-L"$$PWD/dependencies/winring0/Win32/" -lWinRing0 \ -L"$$PWD/dependencies/winring0/Win32/" -lWinRing0 \
-L"$$PWD/dependencies/libusb-1.0.22/MS32/dll" -llibusb-1.0 \ -L"$$PWD/dependencies/libusb-1.0.27/VS2019/MS32/dll" -llibusb-1.0 \
-L"$$PWD/dependencies/hidapi-win/x86/" -lhidapi \ -L"$$PWD/dependencies/hidapi-win/x86/" -lhidapi \
} }
@ -411,10 +411,10 @@ win32:UI_DIR = _intermediate_$$DESTDIR/.ui
#-----------------------------------------------------------------------------------------------# #-----------------------------------------------------------------------------------------------#
win32:contains(QMAKE_TARGET.arch, x86_64) { win32:contains(QMAKE_TARGET.arch, x86_64) {
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/x64/WinRing0x64.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/x64/WinRing0x64.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/x64/WinRing0x64.sys )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/x64/WinRing0x64.sys )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/libusb-1.0.22/MS64/dll/libusb-1.0.dll)\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/libusb-1.0.27/VS2019/MS64/dll/libusb-1.0.dll)\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/hidapi-win/x64/hidapi.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/hidapi-win/x64/hidapi.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
first.depends = $(first) copydata first.depends = $(first) copydata
export(first.depends) export(first.depends)
export(copydata.commands) export(copydata.commands)
@ -422,11 +422,11 @@ win32:contains(QMAKE_TARGET.arch, x86_64) {
} }
win32:contains(QMAKE_TARGET.arch, x86) { win32:contains(QMAKE_TARGET.arch, x86) {
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/Win32/WinRing0.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/Win32/WinRing0.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/Win32/WinRing0.sys )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/Win32/WinRing0.sys )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/x64/WinRing0x64.sys )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/winring0/x64/WinRing0x64.sys )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/libusb-1.0.22/MS32/dll/libusb-1.0.dll)\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/libusb-1.0.27/VS2019/MS32/dll/libusb-1.0.dll)\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/hidapi-win/x86/hidapi.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t) copydata.commands += $(COPY_FILE) \"$$shell_path($$PWD/dependencies/hidapi-win/x86/hidapi.dll )\" \"$$shell_path($$DESTDIR)\" $$escape_expand(\n\t)
first.depends = $(first) copydata first.depends = $(first) copydata
export(first.depends) export(first.depends)

View file

@ -1,256 +0,0 @@
/**
* This file has no copyright assigned and is placed in the Public Domain.
* This file was originally part of the w64 mingw-runtime package.
*/
/* ISO C9x 7.18 Integer types <stdint.h>
* Based on ISO/IEC SC22/WG14 9899 Committee draft (SC22 N2794)
*
* THIS SOFTWARE IS NOT COPYRIGHTED
*
* Contributor: Danny Smith <danny_r_smith_2001@yahoo.co.nz>
* Modified for libusb/MSVC: Pete Batard <pbatard@gmail.com>
*
* This source code is offered for use in the public domain. You may
* use, modify or distribute it freely.
*
* This code is distributed in the hope that it will be useful but
* WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
* DISCLAIMED. This includes but is not limited to warranties of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Date: 2010-04-02
*/
#ifndef _MSC_VER
#error This header should only be used with Microsoft compilers
#endif
#ifndef _STDINT_H
#define _STDINT_H
#ifndef _INTPTR_T_DEFINED
#define _INTPTR_T_DEFINED
#ifndef __intptr_t_defined
#define __intptr_t_defined
#undef intptr_t
#ifdef _WIN64
typedef __int64 intptr_t;
#else
typedef int intptr_t;
#endif /* _WIN64 */
#endif /* __intptr_t_defined */
#endif /* _INTPTR_T_DEFINED */
#ifndef _UINTPTR_T_DEFINED
#define _UINTPTR_T_DEFINED
#ifndef __uintptr_t_defined
#define __uintptr_t_defined
#undef uintptr_t
#ifdef _WIN64
typedef unsigned __int64 uintptr_t;
#else
typedef unsigned int uintptr_t;
#endif /* _WIN64 */
#endif /* __uintptr_t_defined */
#endif /* _UINTPTR_T_DEFINED */
#ifndef _PTRDIFF_T_DEFINED
#define _PTRDIFF_T_DEFINED
#ifndef _PTRDIFF_T_
#define _PTRDIFF_T_
#undef ptrdiff_t
#ifdef _WIN64
typedef __int64 ptrdiff_t;
#else
typedef int ptrdiff_t;
#endif /* _WIN64 */
#endif /* _PTRDIFF_T_ */
#endif /* _PTRDIFF_T_DEFINED */
#ifndef _WCHAR_T_DEFINED
#define _WCHAR_T_DEFINED
#ifndef __cplusplus
typedef unsigned short wchar_t;
#endif /* C++ */
#endif /* _WCHAR_T_DEFINED */
#ifndef _WCTYPE_T_DEFINED
#define _WCTYPE_T_DEFINED
#ifndef _WINT_T
#define _WINT_T
typedef unsigned short wint_t;
typedef unsigned short wctype_t;
#endif /* _WINT_T */
#endif /* _WCTYPE_T_DEFINED */
/* 7.18.1.1 Exact-width integer types */
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
/* 7.18.1.2 Minimum-width integer types */
typedef signed char int_least8_t;
typedef unsigned char uint_least8_t;
typedef short int_least16_t;
typedef unsigned short uint_least16_t;
typedef int int_least32_t;
typedef unsigned uint_least32_t;
typedef __int64 int_least64_t;
typedef unsigned __int64 uint_least64_t;
/* 7.18.1.3 Fastest minimum-width integer types
* Not actually guaranteed to be fastest for all purposes
* Here we use the exact-width types for 8 and 16-bit ints.
*/
typedef __int8 int_fast8_t;
typedef unsigned __int8 uint_fast8_t;
typedef __int16 int_fast16_t;
typedef unsigned __int16 uint_fast16_t;
typedef __int32 int_fast32_t;
typedef unsigned __int32 uint_fast32_t;
typedef __int64 int_fast64_t;
typedef unsigned __int64 uint_fast64_t;
/* 7.18.1.5 Greatest-width integer types */
typedef __int64 intmax_t;
typedef unsigned __int64 uintmax_t;
/* 7.18.2 Limits of specified-width integer types */
/* 7.18.2.1 Limits of exact-width integer types */
#define INT8_MIN (-128)
#define INT16_MIN (-32768)
#define INT32_MIN (-2147483647 - 1)
#define INT64_MIN (-9223372036854775807LL - 1)
#define INT8_MAX 127
#define INT16_MAX 32767
#define INT32_MAX 2147483647
#define INT64_MAX 9223372036854775807LL
#define UINT8_MAX 255
#define UINT16_MAX 65535
#define UINT32_MAX 0xffffffffU /* 4294967295U */
#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */
/* 7.18.2.2 Limits of minimum-width integer types */
#define INT_LEAST8_MIN INT8_MIN
#define INT_LEAST16_MIN INT16_MIN
#define INT_LEAST32_MIN INT32_MIN
#define INT_LEAST64_MIN INT64_MIN
#define INT_LEAST8_MAX INT8_MAX
#define INT_LEAST16_MAX INT16_MAX
#define INT_LEAST32_MAX INT32_MAX
#define INT_LEAST64_MAX INT64_MAX
#define UINT_LEAST8_MAX UINT8_MAX
#define UINT_LEAST16_MAX UINT16_MAX
#define UINT_LEAST32_MAX UINT32_MAX
#define UINT_LEAST64_MAX UINT64_MAX
/* 7.18.2.3 Limits of fastest minimum-width integer types */
#define INT_FAST8_MIN INT8_MIN
#define INT_FAST16_MIN INT16_MIN
#define INT_FAST32_MIN INT32_MIN
#define INT_FAST64_MIN INT64_MIN
#define INT_FAST8_MAX INT8_MAX
#define INT_FAST16_MAX INT16_MAX
#define INT_FAST32_MAX INT32_MAX
#define INT_FAST64_MAX INT64_MAX
#define UINT_FAST8_MAX UINT8_MAX
#define UINT_FAST16_MAX UINT16_MAX
#define UINT_FAST32_MAX UINT32_MAX
#define UINT_FAST64_MAX UINT64_MAX
/* 7.18.2.4 Limits of integer types capable of holding
object pointers */
#ifdef _WIN64
#define INTPTR_MIN INT64_MIN
#define INTPTR_MAX INT64_MAX
#define UINTPTR_MAX UINT64_MAX
#else
#define INTPTR_MIN INT32_MIN
#define INTPTR_MAX INT32_MAX
#define UINTPTR_MAX UINT32_MAX
#endif
/* 7.18.2.5 Limits of greatest-width integer types */
#define INTMAX_MIN INT64_MIN
#define INTMAX_MAX INT64_MAX
#define UINTMAX_MAX UINT64_MAX
/* 7.18.3 Limits of other integer types */
#ifdef _WIN64
#define PTRDIFF_MIN INT64_MIN
#define PTRDIFF_MAX INT64_MAX
#else
#define PTRDIFF_MIN INT32_MIN
#define PTRDIFF_MAX INT32_MAX
#endif
#define SIG_ATOMIC_MIN INT32_MIN
#define SIG_ATOMIC_MAX INT32_MAX
#ifndef SIZE_MAX
#ifdef _WIN64
#define SIZE_MAX UINT64_MAX
#else
#define SIZE_MAX UINT32_MAX
#endif
#endif
#ifndef WCHAR_MIN /* also in wchar.h */
#define WCHAR_MIN 0U
#define WCHAR_MAX 0xffffU
#endif
/*
* wint_t is unsigned short for compatibility with MS runtime
*/
#define WINT_MIN 0U
#define WINT_MAX 0xffffU
/* 7.18.4 Macros for integer constants */
/* 7.18.4.1 Macros for minimum-width integer constants
Accoding to Douglas Gwyn <gwyn@arl.mil>:
"This spec was changed in ISO/IEC 9899:1999 TC1; in ISO/IEC
9899:1999 as initially published, the expansion was required
to be an integer constant of precisely matching type, which
is impossible to accomplish for the shorter types on most
platforms, because C99 provides no standard way to designate
an integer constant with width less than that of type int.
TC1 changed this to require just an integer constant
*expression* with *promoted* type."
The trick used here is from Clive D W Feather.
*/
#define INT8_C(val) (INT_LEAST8_MAX-INT_LEAST8_MAX+(val))
#define INT16_C(val) (INT_LEAST16_MAX-INT_LEAST16_MAX+(val))
#define INT32_C(val) (INT_LEAST32_MAX-INT_LEAST32_MAX+(val))
/* The 'trick' doesn't work in C89 for long long because, without
suffix, (val) will be evaluated as int, not intmax_t */
#define INT64_C(val) val##i64
#define UINT8_C(val) (val)
#define UINT16_C(val) (val)
#define UINT32_C(val) (val##i32)
#define UINT64_C(val) val##ui64
/* 7.18.4.2 Macros for greatest-width integer constants */
#define INTMAX_C(val) val##i64
#define UINTMAX_C(val) val##ui64
#endif

View file

@ -1,277 +0,0 @@
/*
* Test suite program based of libusb-0.1-compat testlibusb
* Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include "libusb.h"
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
int verbose = 0;
static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
{
printf(" USB 3.0 Endpoint Companion:\n");
printf(" bMaxBurst: %d\n", ep_comp->bMaxBurst);
printf(" bmAttributes: 0x%02x\n", ep_comp->bmAttributes);
printf(" wBytesPerInterval: %d\n", ep_comp->wBytesPerInterval);
}
static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
{
int i, ret;
printf(" Endpoint:\n");
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
printf(" bInterval: %d\n", endpoint->bInterval);
printf(" bRefresh: %d\n", endpoint->bRefresh);
printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
for (i = 0; i < endpoint->extra_length;) {
if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {
struct libusb_ss_endpoint_companion_descriptor *ep_comp;
ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
if (LIBUSB_SUCCESS != ret) {
continue;
}
print_endpoint_comp(ep_comp);
libusb_free_ss_endpoint_companion_descriptor(ep_comp);
}
i += endpoint->extra[i];
}
}
static void print_altsetting(const struct libusb_interface_descriptor *interface)
{
uint8_t i;
printf(" Interface:\n");
printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
printf(" iInterface: %d\n", interface->iInterface);
for (i = 0; i < interface->bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)
{
printf(" USB 2.0 Extension Capabilities:\n");
printf(" bDevCapabilityType: %d\n", usb_2_0_ext_cap->bDevCapabilityType);
printf(" bmAttributes: 0x%x\n", usb_2_0_ext_cap->bmAttributes);
}
static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
{
printf(" USB 3.0 Capabilities:\n");
printf(" bDevCapabilityType: %d\n", ss_usb_cap->bDevCapabilityType);
printf(" bmAttributes: 0x%x\n", ss_usb_cap->bmAttributes);
printf(" wSpeedSupported: 0x%x\n", ss_usb_cap->wSpeedSupported);
printf(" bFunctionalitySupport: %d\n", ss_usb_cap->bFunctionalitySupport);
printf(" bU1devExitLat: %d\n", ss_usb_cap->bU1DevExitLat);
printf(" bU2devExitLat: %d\n", ss_usb_cap->bU2DevExitLat);
}
static void print_bos(libusb_device_handle *handle)
{
struct libusb_bos_descriptor *bos;
int ret;
ret = libusb_get_bos_descriptor(handle, &bos);
if (0 > ret) {
return;
}
printf(" Binary Object Store (BOS):\n");
printf(" wTotalLength: %d\n", bos->wTotalLength);
printf(" bNumDeviceCaps: %d\n", bos->bNumDeviceCaps);
if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
ret = libusb_get_usb_2_0_extension_descriptor(NULL, bos->dev_capability[0],&usb_2_0_extension);
if (0 > ret) {
return;
}
print_2_0_ext_cap(usb_2_0_extension);
libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
}
if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
struct libusb_ss_usb_device_capability_descriptor *dev_cap;
ret = libusb_get_ss_usb_device_capability_descriptor(NULL, bos->dev_capability[0],&dev_cap);
if (0 > ret) {
return;
}
print_ss_usb_cap(dev_cap);
libusb_free_ss_usb_device_capability_descriptor(dev_cap);
}
libusb_free_bos_descriptor(bos);
}
static void print_interface(const struct libusb_interface *interface)
{
int i;
for (i = 0; i < interface->num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
static void print_configuration(struct libusb_config_descriptor *config)
{
uint8_t i;
printf(" Configuration:\n");
printf(" wTotalLength: %d\n", config->wTotalLength);
printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
printf(" iConfiguration: %d\n", config->iConfiguration);
printf(" bmAttributes: %02xh\n", config->bmAttributes);
printf(" MaxPower: %d\n", config->MaxPower);
for (i = 0; i < config->bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
static int print_device(libusb_device *dev, int level)
{
struct libusb_device_descriptor desc;
libusb_device_handle *handle = NULL;
char description[256];
char string[256];
int ret;
uint8_t i;
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
fprintf(stderr, "failed to get device descriptor");
return -1;
}
ret = libusb_open(dev, &handle);
if (LIBUSB_SUCCESS == ret) {
if (desc.iManufacturer) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
if (ret > 0)
snprintf(description, sizeof(description), "%s - ", string);
else
snprintf(description, sizeof(description), "%04X - ",
desc.idVendor);
}
else
snprintf(description, sizeof(description), "%04X - ",
desc.idVendor);
if (desc.iProduct) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
if (ret > 0)
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%s", string);
else
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%04X", desc.idProduct);
}
else
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%04X", desc.idProduct);
}
else {
snprintf(description, sizeof(description), "%04X - %04X",
desc.idVendor, desc.idProduct);
}
printf("%.*sDev (bus %d, device %d): %s\n", level * 2, " ",
libusb_get_bus_number(dev), libusb_get_device_address(dev), description);
if (handle && verbose) {
if (desc.iSerialNumber) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
if (ret > 0)
printf("%.*s - Serial Number: %s\n", level * 2,
" ", string);
}
}
if (verbose) {
for (i = 0; i < desc.bNumConfigurations; i++) {
struct libusb_config_descriptor *config;
ret = libusb_get_config_descriptor(dev, i, &config);
if (LIBUSB_SUCCESS != ret) {
printf(" Couldn't retrieve descriptors\n");
continue;
}
print_configuration(config);
libusb_free_config_descriptor(config);
}
if (handle && desc.bcdUSB >= 0x0201) {
print_bos(handle);
}
}
if (handle)
libusb_close(handle);
return 0;
}
int main(int argc, char *argv[])
{
libusb_device **devs;
ssize_t cnt;
int r, i;
if (argc > 1 && !strcmp(argv[1], "-v"))
verbose = 1;
r = libusb_init(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return (int)cnt;
for (i = 0; devs[i]; ++i) {
print_device(devs[i], 0);
}
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}

Binary file not shown.

Binary file not shown.

View file

@ -1,9 +1,12 @@
libusb 1.0 Windows binary snapshot - README libusb 1.0 Windows binaries - README
********************************************************************* *****************************************************************
* The latest version of this snapshot can always be downloaded at: * * The latest version can always be downloaded at: *
* https://sourceforge.net/projects/libusb/files/ * * https://github.com/libusb/libusb/releases *
********************************************************************* *****************************************************************
Note: Binaries are provided as-is and may not work for your toolchain.
In that case, please build libusb from source using your own toolchain.
o Visual Studio: o Visual Studio:
- Open existing or create a new project for your application - Open existing or create a new project for your application
@ -24,24 +27,6 @@ o Visual Studio:
remember that you need to have a copy of the DLL either in the runtime remember that you need to have a copy of the DLL either in the runtime
directory or in system32 directory or in system32
o WDK/DDK:
- The following is an example of a sources files that you can use to compile
a libusb 1.0 based console application. In this sample ..\libusb\ is the
directory where you would have copied libusb.h as well as the relevant
libusb-1.0.lib
TARGETNAME=your_app
TARGETTYPE=PROGRAM
USE_MSVCRT=1
UMTYPE=console
INCLUDES=..\libusb;$(DDK_INC_PATH)
TARGETLIBS=..\libusb\libusb-1.0.lib
SOURCES=your_app.c
- Note that if you plan to use libCMT instead of MSVCRT (USE_LIBCMT=1 instead
of USE_MSVCRT=1), you will need to recompile libusb to use libCMT. This can
easily be achieved, in the DDK environment, by running 'ddk_build /MT'
o MinGW/cygwin o MinGW/cygwin
- Copy libusb.h, from include/libusb-1.0/ to your default include directory, - Copy libusb.h, from include/libusb-1.0/ to your default include directory,
and copy the MinGW32/ or MinGW64/ .a files to your default library directory. and copy the MinGW32/ or MinGW64/ .a files to your default library directory.
@ -55,6 +40,9 @@ o Additional information:
- For some libusb samples (including source), please have a look in examples/ - For some libusb samples (including source), please have a look in examples/
- For additional information on the libusb 1.0 Windows backend please visit: - For additional information on the libusb 1.0 Windows backend please visit:
http://windows.libusb.info http://windows.libusb.info
- Using the UsbDk backend is now a run-time choice rather than a compile-time
choice. For additional information, including example usage, please visit:
http://windows.libusb.info/#Driver_Installation
- The MinGW and MS generated DLLs are fully interchangeable, provided that you - The MinGW and MS generated DLLs are fully interchangeable, provided that you
use the import libs provided or generate one from the .def also provided. use the import libs provided or generate one from the .def also provided.
- If you find any issue, please visit http://libusb.info/ and check the - If you find any issue, please visit http://libusb.info/ and check the

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,711 @@
/*
* libusb example program to manipulate U.are.U 4000B fingerprint scanner.
* Copyright © 2007 Daniel Drake <dsd@gentoo.org>
* Copyright © 2016 Nathan Hjelm <hjelmn@mac.com>
* Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
*
* Basic image capture program only, does not consider the powerup quirks or
* the fact that image encryption may be enabled. Not expected to work
* flawlessly all of the time.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libusb.h"
#if defined(_MSC_VER)
#define snprintf _snprintf
#endif
#if defined(DPFP_THREADED)
#if defined(PLATFORM_POSIX)
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define THREAD_RETURN_VALUE NULL
typedef sem_t * semaphore_t;
typedef pthread_t thread_t;
static inline semaphore_t semaphore_create(void)
{
sem_t *semaphore;
char name[50];
snprintf(name, sizeof(name), "/org.libusb.example.dpfp_threaded:%d", (int)getpid());
semaphore = sem_open(name, O_CREAT | O_EXCL, 0, 0);
if (semaphore == SEM_FAILED)
return NULL;
/* Remove semaphore so that it does not persist after process exits */
(void)sem_unlink(name);
return semaphore;
}
static inline void semaphore_give(semaphore_t semaphore)
{
(void)sem_post(semaphore);
}
static inline void semaphore_take(semaphore_t semaphore)
{
(void)sem_wait(semaphore);
}
static inline void semaphore_destroy(semaphore_t semaphore)
{
(void)sem_close(semaphore);
}
static inline int thread_create(thread_t *thread,
void *(*thread_entry)(void *arg), void *arg)
{
return pthread_create(thread, NULL, thread_entry, arg) == 0 ? 0 : -1;
}
static inline void thread_join(thread_t thread)
{
(void)pthread_join(thread, NULL);
}
#elif defined(PLATFORM_WINDOWS)
#define THREAD_RETURN_VALUE 0
typedef HANDLE semaphore_t;
typedef HANDLE thread_t;
#if defined(__CYGWIN__)
typedef DWORD thread_return_t;
#else
#include <process.h>
typedef unsigned thread_return_t;
#endif
static inline semaphore_t semaphore_create(void)
{
return CreateSemaphore(NULL, 0, 1, NULL);
}
static inline void semaphore_give(semaphore_t semaphore)
{
(void)ReleaseSemaphore(semaphore, 1, NULL);
}
static inline void semaphore_take(semaphore_t semaphore)
{
(void)WaitForSingleObject(semaphore, INFINITE);
}
static inline void semaphore_destroy(semaphore_t semaphore)
{
(void)CloseHandle(semaphore);
}
static inline int thread_create(thread_t *thread,
thread_return_t (__stdcall *thread_entry)(void *arg), void *arg)
{
#if defined(__CYGWIN__)
*thread = CreateThread(NULL, 0, thread_entry, arg, 0, NULL);
#else
*thread = (HANDLE)_beginthreadex(NULL, 0, thread_entry, arg, 0, NULL);
#endif
return *thread != NULL ? 0 : -1;
}
static inline void thread_join(thread_t thread)
{
(void)WaitForSingleObject(thread, INFINITE);
(void)CloseHandle(thread);
}
#endif
#endif
#define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
#define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
#define USB_RQ 0x04
#define INTR_LENGTH 64
enum {
MODE_INIT = 0x00,
MODE_AWAIT_FINGER_ON = 0x10,
MODE_AWAIT_FINGER_OFF = 0x12,
MODE_CAPTURE = 0x20,
MODE_SHUT_UP = 0x30,
MODE_READY = 0x80,
};
static int next_state(void);
enum {
STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
STATE_AWAIT_IRQ_FINGER_DETECTED,
STATE_AWAIT_MODE_CHANGE_CAPTURE,
STATE_AWAIT_IMAGE,
STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
STATE_AWAIT_IRQ_FINGER_REMOVED,
};
static int state = 0;
static libusb_device_handle *devh = NULL;
static unsigned char imgbuf[0x1b340];
static unsigned char irqbuf[INTR_LENGTH];
static struct libusb_transfer *img_transfer = NULL;
static struct libusb_transfer *irq_transfer = NULL;
static int img_idx = 0;
static volatile sig_atomic_t do_exit = 0;
#if defined(DPFP_THREADED)
static semaphore_t exit_semaphore;
static thread_t poll_thread;
#endif
static void request_exit(sig_atomic_t code)
{
do_exit = code;
#if defined(DPFP_THREADED)
semaphore_give(exit_semaphore);
#endif
}
#if defined(DPFP_THREADED)
#if defined(PLATFORM_POSIX)
static void *poll_thread_main(void *arg)
#elif defined(PLATFORM_WINDOWS)
static thread_return_t __stdcall poll_thread_main(void *arg)
#endif
{
(void)arg;
printf("poll thread running\n");
while (!do_exit) {
struct timeval tv = { 1, 0 };
int r;
r = libusb_handle_events_timeout(NULL, &tv);
if (r < 0) {
request_exit(2);
break;
}
}
printf("poll thread shutting down\n");
return THREAD_RETURN_VALUE;
}
#endif
static int find_dpfp_device(void)
{
devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
if (!devh) {
errno = ENODEV;
return -1;
}
return 0;
}
static int print_f0_data(void)
{
unsigned char data[0x10];
size_t i;
int r;
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
sizeof(data), 0);
if (r < 0) {
fprintf(stderr, "F0 error %d\n", r);
return r;
}
if (r < (int)sizeof(data)) {
fprintf(stderr, "short read (%d)\n", r);
return -1;
}
printf("F0 data:");
for (i = 0; i < sizeof(data); i++)
printf(" %02x", data[i]);
printf("\n");
return 0;
}
static int get_hwstat(unsigned char *status)
{
int r;
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
if (r < 0) {
fprintf(stderr, "read hwstat error %d\n", r);
return r;
}
if (r < 1) {
fprintf(stderr, "short read (%d)\n", r);
return -1;
}
printf("hwstat reads %02x\n", *status);
return 0;
}
static int set_hwstat(unsigned char data)
{
int r;
printf("set hwstat to %02x\n", data);
r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
if (r < 0) {
fprintf(stderr, "set hwstat error %d\n", r);
return r;
}
if (r < 1) {
fprintf(stderr, "short write (%d)\n", r);
return -1;
}
return 0;
}
static int set_mode(unsigned char data)
{
int r;
printf("set mode %02x\n", data);
r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
if (r < 0) {
fprintf(stderr, "set mode error %d\n", r);
return r;
}
if (r < 1) {
fprintf(stderr, "short write (%d)\n", r);
return -1;
}
return 0;
}
static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
{
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "mode change transfer not completed!\n");
request_exit(2);
}
printf("async cb_mode_changed length=%d actual_length=%d\n",
transfer->length, transfer->actual_length);
if (next_state() < 0)
request_exit(2);
}
static int set_mode_async(unsigned char data)
{
unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
struct libusb_transfer *transfer;
if (!buf) {
errno = ENOMEM;
return -1;
}
transfer = libusb_alloc_transfer(0);
if (!transfer) {
free(buf);
errno = ENOMEM;
return -1;
}
printf("async set mode %02x\n", data);
libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
1000);
transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
| LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
return libusb_submit_transfer(transfer);
}
static int do_sync_intr(unsigned char *data)
{
int r;
int transferred;
r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
&transferred, 1000);
if (r < 0) {
fprintf(stderr, "intr error %d\n", r);
return r;
}
if (transferred < INTR_LENGTH) {
fprintf(stderr, "short read (%d)\n", r);
return -1;
}
printf("recv interrupt %04x\n", *((uint16_t *)data));
return 0;
}
static int sync_intr(unsigned char type)
{
int r;
unsigned char data[INTR_LENGTH];
while (1) {
r = do_sync_intr(data);
if (r < 0)
return r;
if (data[0] == type)
return 0;
}
}
static int save_to_file(unsigned char *data)
{
FILE *f;
char filename[64];
snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
f = fopen(filename, "w");
if (!f)
return -1;
fputs("P5 384 289 255 ", f);
(void)fwrite(data + 64, 1, 384*289, f);
fclose(f);
printf("saved image to %s\n", filename);
return 0;
}
static int next_state(void)
{
int r = 0;
printf("old state: %d\n", state);
switch (state) {
case STATE_AWAIT_IRQ_FINGER_REMOVED:
state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
r = set_mode_async(MODE_AWAIT_FINGER_ON);
break;
case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
state = STATE_AWAIT_IRQ_FINGER_DETECTED;
break;
case STATE_AWAIT_IRQ_FINGER_DETECTED:
state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
r = set_mode_async(MODE_CAPTURE);
break;
case STATE_AWAIT_MODE_CHANGE_CAPTURE:
state = STATE_AWAIT_IMAGE;
break;
case STATE_AWAIT_IMAGE:
state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
r = set_mode_async(MODE_AWAIT_FINGER_OFF);
break;
case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
state = STATE_AWAIT_IRQ_FINGER_REMOVED;
break;
default:
printf("unrecognised state %d\n", state);
}
if (r < 0) {
fprintf(stderr, "error detected changing state\n");
return r;
}
printf("new state: %d\n", state);
return 0;
}
static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
{
unsigned char irqtype = transfer->buffer[0];
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "irq transfer status %d?\n", transfer->status);
goto err_free_transfer;
}
printf("IRQ callback %02x\n", irqtype);
switch (state) {
case STATE_AWAIT_IRQ_FINGER_DETECTED:
if (irqtype == 0x01) {
if (next_state() < 0)
goto err_free_transfer;
} else {
printf("finger-on-sensor detected in wrong state!\n");
}
break;
case STATE_AWAIT_IRQ_FINGER_REMOVED:
if (irqtype == 0x02) {
if (next_state() < 0)
goto err_free_transfer;
} else {
printf("finger-on-sensor detected in wrong state!\n");
}
break;
}
if (libusb_submit_transfer(irq_transfer) < 0)
goto err_free_transfer;
return;
err_free_transfer:
libusb_free_transfer(transfer);
irq_transfer = NULL;
request_exit(2);
}
static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
{
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "img transfer status %d?\n", transfer->status);
goto err_free_transfer;
}
printf("Image callback\n");
save_to_file(imgbuf);
if (next_state() < 0)
goto err_free_transfer;
if (libusb_submit_transfer(img_transfer) < 0)
goto err_free_transfer;
return;
err_free_transfer:
libusb_free_transfer(transfer);
img_transfer = NULL;
request_exit(2);
}
static int init_capture(void)
{
int r;
r = libusb_submit_transfer(irq_transfer);
if (r < 0)
return r;
r = libusb_submit_transfer(img_transfer);
if (r < 0) {
libusb_cancel_transfer(irq_transfer);
while (irq_transfer)
if (libusb_handle_events(NULL) < 0)
break;
return r;
}
/* start state machine */
state = STATE_AWAIT_IRQ_FINGER_REMOVED;
return next_state();
}
static int do_init(void)
{
unsigned char status;
int r;
r = get_hwstat(&status);
if (r < 0)
return r;
if (!(status & 0x80)) {
r = set_hwstat(status | 0x80);
if (r < 0)
return r;
r = get_hwstat(&status);
if (r < 0)
return r;
}
status &= ~0x80;
r = set_hwstat(status);
if (r < 0)
return r;
r = get_hwstat(&status);
if (r < 0)
return r;
r = sync_intr(0x56);
if (r < 0)
return r;
return 0;
}
static int alloc_transfers(void)
{
img_transfer = libusb_alloc_transfer(0);
if (!img_transfer) {
errno = ENOMEM;
return -1;
}
irq_transfer = libusb_alloc_transfer(0);
if (!irq_transfer) {
errno = ENOMEM;
return -1;
}
libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
sizeof(imgbuf), cb_img, NULL, 0);
libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
sizeof(irqbuf), cb_irq, NULL, 0);
return 0;
}
static void sighandler(int signum)
{
(void)signum;
request_exit(1);
}
static void setup_signals(void)
{
#if defined(PLATFORM_POSIX)
struct sigaction sigact;
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
(void)sigaction(SIGINT, &sigact, NULL);
(void)sigaction(SIGTERM, &sigact, NULL);
(void)sigaction(SIGQUIT, &sigact, NULL);
#else
(void)signal(SIGINT, sighandler);
(void)signal(SIGTERM, sighandler);
#endif
}
int main(void)
{
int r;
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0) {
fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r));
exit(1);
}
r = find_dpfp_device();
if (r < 0) {
fprintf(stderr, "Could not find/open device\n");
goto out;
}
r = libusb_claim_interface(devh, 0);
if (r < 0) {
fprintf(stderr, "claim interface error %d - %s\n", r, libusb_strerror(r));
goto out;
}
printf("claimed interface\n");
r = print_f0_data();
if (r < 0)
goto out_release;
r = do_init();
if (r < 0)
goto out_deinit;
/* async from here onwards */
setup_signals();
r = alloc_transfers();
if (r < 0)
goto out_deinit;
#if defined(DPFP_THREADED)
exit_semaphore = semaphore_create();
if (!exit_semaphore) {
fprintf(stderr, "failed to initialise semaphore\n");
goto out_deinit;
}
r = thread_create(&poll_thread, poll_thread_main, NULL);
if (r) {
semaphore_destroy(exit_semaphore);
goto out_deinit;
}
r = init_capture();
if (r < 0)
request_exit(2);
while (!do_exit)
semaphore_take(exit_semaphore);
#else
r = init_capture();
if (r < 0)
goto out_deinit;
while (!do_exit) {
r = libusb_handle_events(NULL);
if (r < 0)
request_exit(2);
}
#endif
printf("shutting down...\n");
#if defined(DPFP_THREADED)
thread_join(poll_thread);
semaphore_destroy(exit_semaphore);
#endif
if (img_transfer) {
r = libusb_cancel_transfer(img_transfer);
if (r < 0)
fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
}
if (irq_transfer) {
r = libusb_cancel_transfer(irq_transfer);
if (r < 0)
fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
}
while (img_transfer || irq_transfer) {
if (libusb_handle_events(NULL) < 0)
break;
}
if (do_exit == 1)
r = 0;
else
r = 1;
out_deinit:
if (img_transfer)
libusb_free_transfer(img_transfer);
if (irq_transfer)
libusb_free_transfer(irq_transfer);
set_mode(0);
set_hwstat(0x80);
out_release:
libusb_release_interface(devh, 0);
out:
libusb_close(devh);
libusb_exit(NULL);
return r >= 0 ? r : -r;
}

View file

@ -20,6 +20,9 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#include <config.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
@ -29,9 +32,6 @@
#include "libusb.h" #include "libusb.h"
#include "ezusb.h" #include "ezusb.h"
extern void logerror(const char *format, ...)
__attribute__ ((format(printf, 1, 2)));
/* /*
* This file contains functions for uploading firmware into Cypress * This file contains functions for uploading firmware into Cypress
* EZ-USB microcontrollers. These chips use control endpoint 0 and vendor * EZ-USB microcontrollers. These chips use control endpoint 0 and vendor
@ -139,7 +139,11 @@ static int ezusb_write(libusb_device_handle *device, const char *label,
else else
logerror("%s ==> %d\n", label, status); logerror("%s ==> %d\n", label, status);
} }
return (status < 0) ? -EIO : 0; if (status < 0) {
errno = EIO;
return -1;
}
return 0;
} }
/* /*
@ -162,7 +166,11 @@ static int ezusb_read(libusb_device_handle *device, const char *label,
else else
logerror("%s ==> %d\n", label, status); logerror("%s ==> %d\n", label, status);
} }
return (status < 0) ? -EIO : 0; if (status < 0) {
errno = EIO;
return -1;
}
return 0;
} }
/* /*
@ -195,7 +203,7 @@ static bool ezusb_cpucs(libusb_device_handle *device, uint32_t addr, bool doRun)
} }
/* /*
* Send an FX3 jumpt to address command * Send an FX3 jump to address command
* Returns false on error. * Returns false on error.
*/ */
static bool ezusb_fx3_jump(libusb_device_handle *device, uint32_t addr) static bool ezusb_fx3_jump(libusb_device_handle *device, uint32_t addr)
@ -299,7 +307,7 @@ static int parse_ihex(FILE *image, void *context,
/* Read the target offset (address up to 64KB) */ /* Read the target offset (address up to 64KB) */
tmp = buf[7]; tmp = buf[7];
buf[7] = 0; buf[7] = 0;
off = (int)strtoul(buf+3, NULL, 16); off = (unsigned int)strtoul(buf+3, NULL, 16);
buf[7] = tmp; buf[7] = tmp;
/* Initialize data_addr */ /* Initialize data_addr */
@ -514,7 +522,8 @@ static int ram_poke(void *context, uint32_t addr, bool external,
if (external) { if (external) {
logerror("can't write %u bytes external memory at 0x%08x\n", logerror("can't write %u bytes external memory at 0x%08x\n",
(unsigned)len, addr); (unsigned)len, addr);
return -EINVAL; errno = EINVAL;
return -1;
} }
break; break;
case skip_internal: /* CPU must be running */ case skip_internal: /* CPU must be running */
@ -538,7 +547,8 @@ static int ram_poke(void *context, uint32_t addr, bool external,
case _undef: case _undef:
default: default:
logerror("bug\n"); logerror("bug\n");
return -EDOM; errno = EDOM;
return -1;
} }
ctx->total += len; ctx->total += len;

View file

@ -20,23 +20,10 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#if !defined(_MSC_VER)
#include <config.h>
#include <stdbool.h> #include <stdbool.h>
#else
#define __attribute__(x)
#if !defined(bool)
#define bool int
#endif
#if !defined(true)
#define true (1 == 1)
#endif
#if !defined(false)
#define false (!true)
#endif
#if defined(_PREFAST_)
#pragma warning(disable:28193)
#endif
#endif
#define FX_TYPE_UNDEFINED -1 #define FX_TYPE_UNDEFINED -1
#define FX_TYPE_AN21 0 /* Original AnchorChips parts */ #define FX_TYPE_AN21 0 /* Original AnchorChips parts */
@ -59,7 +46,7 @@
extern "C" { extern "C" {
#endif #endif
/* /*
* Automatically identified devices (VID, PID, type, designation). * Automatically identified devices (VID, PID, type, designation).
* TODO: Could use some validation. Also where's the FX2? * TODO: Could use some validation. Also where's the FX2?
*/ */
@ -113,6 +100,8 @@ extern int ezusb_load_eeprom(libusb_device_handle *device,
/* Verbosity level (default 1). Can be increased or decreased with options v/q */ /* Verbosity level (default 1). Can be increased or decreased with options v/q */
extern int verbose; extern int verbose;
extern void logerror(const char *format, ...) PRINTF_FORMAT(1, 2);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -21,6 +21,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#include <config.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -32,7 +34,7 @@
#include "libusb.h" #include "libusb.h"
#include "ezusb.h" #include "ezusb.h"
#if !defined(_WIN32) || defined(__CYGWIN__ ) #if !defined(_WIN32) || defined(__CYGWIN__)
#include <syslog.h> #include <syslog.h>
static bool dosyslog = false; static bool dosyslog = false;
#include <strings.h> #include <strings.h>
@ -47,15 +49,12 @@ static bool dosyslog = false;
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
#endif #endif
void logerror(const char *format, ...)
__attribute__ ((format (__printf__, 1, 2)));
void logerror(const char *format, ...) void logerror(const char *format, ...)
{ {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
#if !defined(_WIN32) || defined(__CYGWIN__ ) #if !defined(_WIN32) || defined(__CYGWIN__)
if (dosyslog) if (dosyslog)
vsyslog(LOG_ERR, format, ap); vsyslog(LOG_ERR, format, ap);
else else
@ -173,9 +172,9 @@ int main(int argc, char*argv[])
} }
/* open the device using libusb */ /* open the device using libusb */
status = libusb_init(NULL); status = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (status < 0) { if (status < 0) {
logerror("libusb_init() failed: %s\n", libusb_error_name(status)); logerror("libusb_init_context() failed: %s\n", libusb_error_name(status));
return -1; return -1;
} }
libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, verbose); libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, verbose);

View file

@ -0,0 +1,147 @@
/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
/*
* libusb example program for hotplug API
* Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include "libusb.h"
int done = 0;
libusb_device_handle *handle = NULL;
static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
int rc;
(void)ctx;
(void)dev;
(void)event;
(void)user_data;
rc = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS == rc) {
printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
} else {
printf ("Device attached\n");
fprintf (stderr, "Error getting device descriptor: %s\n",
libusb_strerror((enum libusb_error)rc));
}
if (handle) {
libusb_close (handle);
handle = NULL;
}
rc = libusb_open (dev, &handle);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "No access to device: %s\n",
libusb_strerror((enum libusb_error)rc));
}
done++;
return 0;
}
static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
int rc;
(void)ctx;
(void)dev;
(void)event;
(void)user_data;
rc = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS == rc) {
printf ("Device detached: %04x:%04x\n", desc.idVendor, desc.idProduct);
} else {
printf ("Device detached\n");
fprintf (stderr, "Error getting device descriptor: %s\n",
libusb_strerror((enum libusb_error)rc));
}
if (handle) {
libusb_close (handle);
handle = NULL;
}
done++;
return 0;
}
int main(int argc, char *argv[])
{
libusb_hotplug_callback_handle hp[2];
int product_id, vendor_id, class_id;
int rc;
vendor_id = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
class_id = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (LIBUSB_SUCCESS != rc)
{
printf ("failed to initialise libusb: %s\n",
libusb_strerror((enum libusb_error)rc));
return EXIT_FAILURE;
}
if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
printf ("Hotplug capabilities are not supported on this platform\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}
rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
product_id, class_id, hotplug_callback, NULL, &hp[0]);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error registering callback 0\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}
rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error registering callback 1\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}
while (done < 2) {
rc = libusb_handle_events (NULL);
if (LIBUSB_SUCCESS != rc)
printf ("libusb_handle_events() failed: %s\n",
libusb_strerror((enum libusb_error)rc));
}
if (handle) {
libusb_close (handle);
}
libusb_exit (NULL);
return EXIT_SUCCESS;
}

View file

@ -55,13 +55,15 @@ int main(void)
int r; int r;
ssize_t cnt; ssize_t cnt;
r = libusb_init(NULL); r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0) if (r < 0)
return r; return r;
cnt = libusb_get_device_list(NULL, &devs); cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) if (cnt < 0){
libusb_exit(NULL);
return (int) cnt; return (int) cnt;
}
print_devs(devs); print_devs(devs);
libusb_free_device_list(devs, 1); libusb_free_device_list(devs, 1);

View file

@ -0,0 +1,228 @@
/*
* libusb example program to measure Atmel SAM3U isochronous performance
* Copyright (C) 2012 Harald Welte <laforge@gnumonks.org>
*
* Copied with the author's permission under LGPL-2.1 from
* http://git.gnumonks.org/cgi-bin/gitweb.cgi?p=sam3u-tests.git;a=blob;f=usb-benchmark-project/host/benchmark.c;h=74959f7ee88f1597286cd435f312a8ff52c56b7e
*
* An Atmel SAM3U test firmware is also available in the above repository.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <time.h>
#include "libusb.h"
#define EP_DATA_IN 0x82
#define EP_ISO_IN 0x86
static volatile sig_atomic_t do_exit = 0;
static struct libusb_device_handle *devh = NULL;
static unsigned long num_bytes = 0, num_xfer = 0;
static struct timeval tv_start;
static void get_timestamp(struct timeval *tv)
{
#if defined(PLATFORM_WINDOWS)
static LARGE_INTEGER frequency;
LARGE_INTEGER counter;
if (!frequency.QuadPart)
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&counter);
counter.QuadPart *= 1000000;
counter.QuadPart /= frequency.QuadPart;
tv->tv_sec = (long)(counter.QuadPart / 1000000ULL);
tv->tv_usec = (long)(counter.QuadPart % 1000000ULL);
#elif defined(HAVE_CLOCK_GETTIME)
struct timespec ts;
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
tv->tv_sec = ts.tv_sec;
tv->tv_usec = (int)(ts.tv_nsec / 1000L);
#else
gettimeofday(tv, NULL);
#endif
}
static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr)
{
int i;
if (xfr->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "transfer status %d\n", xfr->status);
libusb_free_transfer(xfr);
exit(3);
}
if (xfr->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
for (i = 0; i < xfr->num_iso_packets; i++) {
struct libusb_iso_packet_descriptor *pack = &xfr->iso_packet_desc[i];
if (pack->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "Error: pack %d status %d\n", i, pack->status);
exit(5);
}
printf("pack%d length:%u, actual_length:%u\n", i, pack->length, pack->actual_length);
}
}
printf("length:%u, actual_length:%u\n", xfr->length, xfr->actual_length);
for (i = 0; i < xfr->actual_length; i++) {
printf("%02x", xfr->buffer[i]);
if (i % 16)
printf("\n");
else if (i % 8)
printf(" ");
else
printf(" ");
}
num_bytes += xfr->actual_length;
num_xfer++;
if (libusb_submit_transfer(xfr) < 0) {
fprintf(stderr, "error re-submitting URB\n");
exit(1);
}
}
static int benchmark_in(uint8_t ep)
{
static uint8_t buf[2048];
static struct libusb_transfer *xfr;
int num_iso_pack = 0;
if (ep == EP_ISO_IN)
num_iso_pack = 16;
xfr = libusb_alloc_transfer(num_iso_pack);
if (!xfr) {
errno = ENOMEM;
return -1;
}
if (ep == EP_ISO_IN) {
libusb_fill_iso_transfer(xfr, devh, ep, buf,
sizeof(buf), num_iso_pack, cb_xfr, NULL, 0);
libusb_set_iso_packet_lengths(xfr, sizeof(buf)/num_iso_pack);
} else
libusb_fill_bulk_transfer(xfr, devh, ep, buf,
sizeof(buf), cb_xfr, NULL, 0);
get_timestamp(&tv_start);
/* NOTE: To reach maximum possible performance the program must
* submit *multiple* transfers here, not just one.
*
* When only one transfer is submitted there is a gap in the bus
* schedule from when the transfer completes until a new transfer
* is submitted by the callback. This causes some jitter for
* isochronous transfers and loss of throughput for bulk transfers.
*
* This is avoided by queueing multiple transfers in advance, so
* that the host controller is always kept busy, and will schedule
* more transfers on the bus while the callback is running for
* transfers which have completed on the bus.
*/
return libusb_submit_transfer(xfr);
}
static void measure(void)
{
struct timeval tv_stop;
unsigned long diff_msec;
get_timestamp(&tv_stop);
diff_msec = (tv_stop.tv_sec - tv_start.tv_sec) * 1000L;
diff_msec += (tv_stop.tv_usec - tv_start.tv_usec) / 1000L;
printf("%lu transfers (total %lu bytes) in %lu milliseconds => %lu bytes/sec\n",
num_xfer, num_bytes, diff_msec, (num_bytes * 1000L) / diff_msec);
}
static void sig_hdlr(int signum)
{
(void)signum;
measure();
do_exit = 1;
}
int main(void)
{
int rc;
#if defined(PLATFORM_POSIX)
struct sigaction sigact;
sigact.sa_handler = sig_hdlr;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
(void)sigaction(SIGINT, &sigact, NULL);
#else
(void)signal(SIGINT, sig_hdlr);
#endif
rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (rc < 0) {
fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
exit(1);
}
devh = libusb_open_device_with_vid_pid(NULL, 0x16c0, 0x0763);
if (!devh) {
fprintf(stderr, "Error finding USB device\n");
goto out;
}
rc = libusb_claim_interface(devh, 2);
if (rc < 0) {
fprintf(stderr, "Error claiming interface: %s\n", libusb_error_name(rc));
goto out;
}
benchmark_in(EP_ISO_IN);
while (!do_exit) {
rc = libusb_handle_events(NULL);
if (rc != LIBUSB_SUCCESS)
break;
}
/* Measurement has already been done by the signal handler. */
libusb_release_interface(devh, 2);
out:
if (devh)
libusb_close(devh);
libusb_exit(NULL);
return rc;
}

View file

@ -0,0 +1,311 @@
/*
* Test suite program based of libusb-0.1-compat testlibusb
* Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include "libusb.h"
int verbose = 0;
static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
{
printf(" USB 3.0 Endpoint Companion:\n");
printf(" bMaxBurst: %u\n", ep_comp->bMaxBurst);
printf(" bmAttributes: %02xh\n", ep_comp->bmAttributes);
printf(" wBytesPerInterval: %u\n", ep_comp->wBytesPerInterval);
}
static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
{
int i, ret;
printf(" Endpoint:\n");
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
printf(" wMaxPacketSize: %u\n", endpoint->wMaxPacketSize);
printf(" bInterval: %u\n", endpoint->bInterval);
printf(" bRefresh: %u\n", endpoint->bRefresh);
printf(" bSynchAddress: %u\n", endpoint->bSynchAddress);
for (i = 0; i < endpoint->extra_length;) {
if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {
struct libusb_ss_endpoint_companion_descriptor *ep_comp;
ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
if (LIBUSB_SUCCESS != ret)
continue;
print_endpoint_comp(ep_comp);
libusb_free_ss_endpoint_companion_descriptor(ep_comp);
}
i += endpoint->extra[i];
}
}
static void print_altsetting(const struct libusb_interface_descriptor *interface)
{
uint8_t i;
printf(" Interface:\n");
printf(" bInterfaceNumber: %u\n", interface->bInterfaceNumber);
printf(" bAlternateSetting: %u\n", interface->bAlternateSetting);
printf(" bNumEndpoints: %u\n", interface->bNumEndpoints);
printf(" bInterfaceClass: %u\n", interface->bInterfaceClass);
printf(" bInterfaceSubClass: %u\n", interface->bInterfaceSubClass);
printf(" bInterfaceProtocol: %u\n", interface->bInterfaceProtocol);
printf(" iInterface: %u\n", interface->iInterface);
for (i = 0; i < interface->bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)
{
printf(" USB 2.0 Extension Capabilities:\n");
printf(" bDevCapabilityType: %u\n", usb_2_0_ext_cap->bDevCapabilityType);
printf(" bmAttributes: %08xh\n", usb_2_0_ext_cap->bmAttributes);
}
static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
{
printf(" USB 3.0 Capabilities:\n");
printf(" bDevCapabilityType: %u\n", ss_usb_cap->bDevCapabilityType);
printf(" bmAttributes: %02xh\n", ss_usb_cap->bmAttributes);
printf(" wSpeedSupported: %u\n", ss_usb_cap->wSpeedSupported);
printf(" bFunctionalitySupport: %u\n", ss_usb_cap->bFunctionalitySupport);
printf(" bU1devExitLat: %u\n", ss_usb_cap->bU1DevExitLat);
printf(" bU2devExitLat: %u\n", ss_usb_cap->bU2DevExitLat);
}
static void print_bos(libusb_device_handle *handle)
{
struct libusb_bos_descriptor *bos;
uint8_t i;
int ret;
ret = libusb_get_bos_descriptor(handle, &bos);
if (ret < 0)
return;
printf(" Binary Object Store (BOS):\n");
printf(" wTotalLength: %u\n", bos->wTotalLength);
printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps);
for (i = 0; i < bos->bNumDeviceCaps; i++) {
struct libusb_bos_dev_capability_descriptor *dev_cap = bos->dev_capability[i];
if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
ret = libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_extension);
if (ret < 0)
return;
print_2_0_ext_cap(usb_2_0_extension);
libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
} else if (dev_cap->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
struct libusb_ss_usb_device_capability_descriptor *ss_dev_cap;
ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap);
if (ret < 0)
return;
print_ss_usb_cap(ss_dev_cap);
libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap);
}
}
libusb_free_bos_descriptor(bos);
}
static void print_interface(const struct libusb_interface *interface)
{
int i;
for (i = 0; i < interface->num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
static void print_configuration(struct libusb_config_descriptor *config)
{
uint8_t i;
printf(" Configuration:\n");
printf(" wTotalLength: %u\n", config->wTotalLength);
printf(" bNumInterfaces: %u\n", config->bNumInterfaces);
printf(" bConfigurationValue: %u\n", config->bConfigurationValue);
printf(" iConfiguration: %u\n", config->iConfiguration);
printf(" bmAttributes: %02xh\n", config->bmAttributes);
printf(" MaxPower: %u\n", config->MaxPower);
for (i = 0; i < config->bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
static void print_device(libusb_device *dev, libusb_device_handle *handle)
{
struct libusb_device_descriptor desc;
unsigned char string[256];
const char *speed;
int ret;
uint8_t i;
switch (libusb_get_device_speed(dev)) {
case LIBUSB_SPEED_LOW: speed = "1.5M"; break;
case LIBUSB_SPEED_FULL: speed = "12M"; break;
case LIBUSB_SPEED_HIGH: speed = "480M"; break;
case LIBUSB_SPEED_SUPER: speed = "5G"; break;
case LIBUSB_SPEED_SUPER_PLUS: speed = "10G"; break;
default: speed = "Unknown";
}
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}
printf("Dev (bus %u, device %u): %04X - %04X speed: %s\n",
libusb_get_bus_number(dev), libusb_get_device_address(dev),
desc.idVendor, desc.idProduct, speed);
if (!handle)
libusb_open(dev, &handle);
if (handle) {
if (desc.iManufacturer) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
if (ret > 0)
printf(" Manufacturer: %s\n", (char *)string);
}
if (desc.iProduct) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
if (ret > 0)
printf(" Product: %s\n", (char *)string);
}
if (desc.iSerialNumber && verbose) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
if (ret > 0)
printf(" Serial Number: %s\n", (char *)string);
}
}
if (verbose) {
for (i = 0; i < desc.bNumConfigurations; i++) {
struct libusb_config_descriptor *config;
ret = libusb_get_config_descriptor(dev, i, &config);
if (LIBUSB_SUCCESS != ret) {
printf(" Couldn't retrieve descriptors\n");
continue;
}
print_configuration(config);
libusb_free_config_descriptor(config);
}
if (handle && desc.bcdUSB >= 0x0201)
print_bos(handle);
}
if (handle)
libusb_close(handle);
}
#ifdef __linux__
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
static int test_wrapped_device(const char *device_name)
{
libusb_device_handle *handle;
int r, fd;
fd = open(device_name, O_RDWR);
if (fd < 0) {
printf("Error could not open %s: %s\n", device_name, strerror(errno));
return 1;
}
r = libusb_wrap_sys_device(NULL, fd, &handle);
if (r) {
printf("Error wrapping device: %s: %s\n", device_name, libusb_strerror(r));
close(fd);
return 1;
}
print_device(libusb_get_device(handle), handle);
close(fd);
return 0;
}
#else
static int test_wrapped_device(const char *device_name)
{
(void)device_name;
printf("Testing wrapped devices is not supported on your platform\n");
return 1;
}
#endif
int main(int argc, char *argv[])
{
const char *device_name = NULL;
libusb_device **devs;
ssize_t cnt;
int r, i;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-v")) {
verbose = 1;
} else if (!strcmp(argv[i], "-d") && (i + 1) < argc) {
i++;
device_name = argv[i];
} else {
printf("Usage %s [-v] [-d </dev/bus/usb/...>]\n", argv[0]);
printf("Note use -d to test libusb_wrap_sys_device()\n");
return 1;
}
}
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0)
return r;
if (device_name) {
r = test_wrapped_device(device_name);
} else {
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
libusb_exit(NULL);
return 1;
}
for (i = 0; devs[i]; i++)
print_device(devs[i], NULL);
libusb_free_device_list(devs, 1);
}
libusb_exit(NULL);
return r;
}

Some files were not shown because too many files have changed in this diff Show more