early-access version 1701

This commit is contained in:
pineappleEA
2021-05-21 07:39:33 +02:00
parent f11e1d29de
commit 9256601d27
199 changed files with 15535 additions and 7268 deletions

View File

@@ -1,19 +1,12 @@
AM_CPPFLAGS = -I$(top_srcdir)/libusb
LDADD = ../libusb/libusb-1.0.la
LIBS =
noinst_PROGRAMS = listdevs xusb fxload hotplugtest testlibusb
noinst_PROGRAMS = dpfp dpfp_threaded fxload hotplugtest listdevs sam3u_benchmark testlibusb xusb
if HAVE_SIGACTION
noinst_PROGRAMS += dpfp
if THREADS_POSIX
dpfp_threaded_CFLAGS = $(AM_CFLAGS)
noinst_PROGRAMS += dpfp_threaded
endif
sam3u_benchmark_SOURCES = sam3u_benchmark.c
noinst_PROGRAMS += sam3u_benchmark
endif
dpfp_threaded_CPPFLAGS = $(AM_CPPFLAGS) -DDPFP_THREADED
dpfp_threaded_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
dpfp_threaded_LDADD = $(LDADD) $(THREAD_LIBS)
dpfp_threaded_SOURCES = dpfp.c
fxload_SOURCES = ezusb.c ezusb.h fxload.c
fxload_CFLAGS = $(THREAD_CFLAGS) $(AM_CFLAGS)

View File

@@ -1,6 +1,8 @@
/*
* 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
@@ -21,14 +23,121 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <signal.h>
#include <string.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];
sprintf(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)
@@ -57,25 +166,65 @@ enum {
};
static int state = 0;
static struct libusb_device_handle *devh = NULL;
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 int do_exit = 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);
return devh ? 0 : -EIO;
return devh ? 0 : -ENODEV;
}
static int print_f0_data(void)
{
unsigned char data[0x10];
size_t i;
int r;
unsigned int i;
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
sizeof(data), 0);
@@ -83,14 +232,14 @@ static int print_f0_data(void)
fprintf(stderr, "F0 error %d\n", r);
return r;
}
if ((unsigned int) r < sizeof(data)) {
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(" %02x", data[i]);
printf("\n");
return 0;
}
@@ -104,7 +253,7 @@ static int get_hwstat(unsigned char *status)
fprintf(stderr, "read hwstat error %d\n", r);
return r;
}
if ((unsigned int) r < 1) {
if (r < 1) {
fprintf(stderr, "short read (%d)\n", r);
return -1;
}
@@ -123,8 +272,8 @@ static int set_hwstat(unsigned char data)
fprintf(stderr, "set hwstat error %d\n", r);
return r;
}
if ((unsigned int) r < 1) {
fprintf(stderr, "short write (%d)", r);
if (r < 1) {
fprintf(stderr, "short write (%d)\n", r);
return -1;
}
@@ -134,15 +283,15 @@ static int set_hwstat(unsigned char data)
static int set_mode(unsigned char data)
{
int r;
printf("set mode %02x\n", data);
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 ((unsigned int) r < 1) {
fprintf(stderr, "short write (%d)", r);
if (r < 1) {
fprintf(stderr, "short write (%d)\n", r);
return -1;
}
@@ -153,18 +302,18 @@ 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");
do_exit = 2;
request_exit(2);
}
printf("async cb_mode_changed length=%d actual_length=%d\n",
transfer->length, transfer->actual_length);
if (next_state() < 0)
do_exit = 2;
request_exit(2);
}
static int set_mode_async(unsigned char data)
{
unsigned char *buf = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
struct libusb_transfer *transfer;
if (!buf)
@@ -203,7 +352,7 @@ static int do_sync_intr(unsigned char *data)
return -1;
}
printf("recv interrupt %04x\n", *((uint16_t *) data));
printf("recv interrupt %04x\n", *((uint16_t *)data));
return 0;
}
@@ -223,17 +372,17 @@ static int sync_intr(unsigned char type)
static int save_to_file(unsigned char *data)
{
FILE *fd;
FILE *f;
char filename[64];
snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
fd = fopen(filename, "w");
if (!fd)
f = fopen(filename, "w");
if (!f)
return -1;
fputs("P5 384 289 255 ", fd);
(void) fwrite(data + 64, 1, 384*289, fd);
fclose(fd);
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;
}
@@ -241,6 +390,7 @@ static int save_to_file(unsigned char *data)
static int next_state(void)
{
int r = 0;
printf("old state: %d\n", state);
switch (state) {
case STATE_AWAIT_IRQ_FINGER_REMOVED:
@@ -282,57 +432,60 @@ static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "irq transfer status %d?\n", transfer->status);
do_exit = 2;
libusb_free_transfer(transfer);
irq_transfer = NULL;
return;
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) {
do_exit = 2;
return;
}
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) {
do_exit = 2;
return;
}
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)
do_exit = 2;
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);
do_exit = 2;
libusb_free_transfer(transfer);
img_transfer = NULL;
return;
goto err_free_transfer;
}
printf("Image callback\n");
save_to_file(imgbuf);
if (next_state() < 0) {
do_exit = 2;
return;
}
if (next_state() < 0)
goto err_free_transfer;
if (libusb_submit_transfer(img_transfer) < 0)
do_exit = 2;
goto err_free_transfer;
return;
err_free_transfer:
libusb_free_transfer(transfer);
img_transfer = NULL;
request_exit(2);
}
static int init_capture(void)
@@ -413,17 +566,33 @@ static void sighandler(int signum)
{
(void)signum;
do_exit = 1;
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)
{
struct sigaction sigact;
int r;
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "failed to initialise libusb\n");
fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r));
exit(1);
}
@@ -435,7 +604,7 @@ int main(void)
r = libusb_claim_interface(devh, 0);
if (r < 0) {
fprintf(stderr, "usb_claim_interface error %d\n", r);
fprintf(stderr, "claim interface error %d - %s\n", r, libusb_strerror(r));
goto out;
}
printf("claimed interface\n");
@@ -449,45 +618,66 @@ int main(void)
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;
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
while (!do_exit) {
r = libusb_handle_events(NULL);
if (r < 0)
goto out_deinit;
request_exit(2);
}
#endif
printf("shutting down...\n");
if (irq_transfer) {
r = libusb_cancel_transfer(irq_transfer);
if (r < 0)
goto out_deinit;
}
#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)
goto out_deinit;
fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
}
while (irq_transfer || img_transfer)
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;
@@ -495,8 +685,10 @@ int main(void)
r = 1;
out_deinit:
libusb_free_transfer(img_transfer);
libusb_free_transfer(irq_transfer);
if (img_transfer)
libusb_free_transfer(img_transfer);
if (irq_transfer)
libusb_free_transfer(irq_transfer);
set_mode(0);
set_hwstat(0x80);
out_release:

View File

@@ -20,6 +20,9 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
@@ -29,9 +32,6 @@
#include "libusb.h"
#include "ezusb.h"
extern void logerror(const char *format, ...)
__attribute__ ((format(printf, 1, 2)));
/*
* This file contains functions for uploading firmware into Cypress
* EZ-USB microcontrollers. These chips use control endpoint 0 and vendor

View File

@@ -20,23 +20,10 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#if !defined(_MSC_VER)
#include <config.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_AN21 0 /* Original AnchorChips parts */
@@ -59,7 +46,7 @@
extern "C" {
#endif
/*
/*
* Automatically identified devices (VID, PID, type, designation).
* 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 */
extern int verbose;
extern void logerror(const char *format, ...) PRINTF_FORMAT(1, 2);
#ifdef __cplusplus
}
#endif

View File

@@ -21,6 +21,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -32,7 +34,7 @@
#include "libusb.h"
#include "ezusb.h"
#if !defined(_WIN32) || defined(__CYGWIN__ )
#if !defined(_WIN32) || defined(__CYGWIN__)
#include <syslog.h>
static bool dosyslog = false;
#include <strings.h>
@@ -47,15 +49,12 @@ static bool dosyslog = false;
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
#endif
void logerror(const char *format, ...)
__attribute__ ((format (__printf__, 1, 2)));
void logerror(const char *format, ...)
{
va_list ap;
va_start(ap, format);
#if !defined(_WIN32) || defined(__CYGWIN__ )
#if !defined(_WIN32) || defined(__CYGWIN__)
if (dosyslog)
vsyslog(LOG_ERR, format, ap);
else

View File

@@ -95,7 +95,7 @@ int main(int argc, char *argv[])
}
if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
printf ("Hotplug capabilites are not supported on this platform\n");
printf ("Hotplug capabilities are not supported on this platform\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}

View File

@@ -22,24 +22,54 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#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>
#include "libusb.h"
#define EP_DATA_IN 0x82
#define EP_ISO_IN 0x86
static int do_exit = 0;
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;
@@ -103,7 +133,7 @@ static int benchmark_in(uint8_t ep)
libusb_fill_bulk_transfer(xfr, devh, ep, buf,
sizeof(buf), cb_xfr, NULL, 0);
gettimeofday(&tv_start, NULL);
get_timestamp(&tv_start);
/* NOTE: To reach maximum possible performance the program must
* submit *multiple* transfers here, not just one.
@@ -125,36 +155,39 @@ static int benchmark_in(uint8_t ep)
static void measure(void)
{
struct timeval tv_stop;
unsigned int diff_msec;
unsigned long diff_msec;
gettimeofday(&tv_stop, NULL);
get_timestamp(&tv_stop);
diff_msec = (tv_stop.tv_sec - tv_start.tv_sec)*1000;
diff_msec += (tv_stop.tv_usec - tv_start.tv_usec)/1000;
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 %u miliseconds => %lu bytes/sec\n",
num_xfer, num_bytes, diff_msec, (num_bytes*1000)/diff_msec);
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)
{
switch (signum) {
case SIGINT:
measure();
do_exit = 1;
break;
}
(void)signum;
measure();
do_exit = 1;
}
int main(int argc, char **argv)
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;
sigaction(SIGINT, &sigact, NULL);
(void)sigaction(SIGINT, &sigact, NULL);
#else
(void)signal(SIGINT, sig_hdlr);
#endif
rc = libusb_init(NULL);
if (rc < 0) {
@@ -184,7 +217,7 @@ int main(int argc, char **argv)
/* Measurement has already been done by the signal handler. */
libusb_release_interface(devh, 0);
libusb_release_interface(devh, 2);
out:
if (devh)
libusb_close(devh);

View File

@@ -21,18 +21,14 @@
#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);
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)
@@ -40,21 +36,20 @@ 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);
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) {
if (LIBUSB_SUCCESS != ret)
continue;
}
print_endpoint_comp(ep_comp);
@@ -70,13 +65,13 @@ 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);
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]);
@@ -85,58 +80,58 @@ static void print_altsetting(const struct libusb_interface_descriptor *interface
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);
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: %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);
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 (0 > ret) {
if (ret < 0)
return;
}
printf(" Binary Object Store (BOS):\n");
printf(" wTotalLength: %d\n", bos->wTotalLength);
printf(" bNumDeviceCaps: %d\n", bos->bNumDeviceCaps);
printf(" wTotalLength: %u\n", bos->wTotalLength);
printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps);
if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
for (i = 0; i < bos->bNumDeviceCaps; i++) {
struct libusb_bos_dev_capability_descriptor *dev_cap = bos->dev_capability[i];
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;
}
if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
print_2_0_ext_cap(usb_2_0_extension);
libusb_free_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;
if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
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;
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;
}
ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap);
if (ret < 0)
return;
print_ss_usb_cap(dev_cap);
libusb_free_ss_usb_device_capability_descriptor(dev_cap);
}
print_ss_usb_cap(ss_dev_cap);
libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap);
}
}
libusb_free_bos_descriptor(bos);
}
@@ -154,79 +149,71 @@ 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);
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 int print_device(libusb_device *dev, int level)
static void print_device(libusb_device *dev, libusb_device_handle *handle)
{
struct libusb_device_descriptor desc;
libusb_device_handle *handle = NULL;
char description[260];
char string[256];
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 -1;
return;
}
ret = libusb_open(dev, &handle);
if (LIBUSB_SUCCESS == ret) {
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)
snprintf(description, sizeof(description), "%s - ", string);
else
snprintf(description, sizeof(description), "%04X - ",
desc.idVendor);
printf(" Manufacturer: %s\n", (char *)string);
}
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);
printf(" Product: %s\n", (char *)string);
}
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) {
if (desc.iSerialNumber && verbose) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
if (ret > 0)
printf("%.*s - Serial Number: %s\n", level * 2,
" ", string);
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");
@@ -238,40 +225,87 @@ static int print_device(libusb_device *dev, int level)
libusb_free_config_descriptor(config);
}
if (handle && desc.bcdUSB >= 0x0201) {
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;
if (argc > 1 && !strcmp(argv[1], "-v"))
verbose = 1;
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(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return (int)cnt;
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], 0);
for (i = 0; devs[i]; i++)
print_device(devs[i], NULL);
libusb_free_device_list(devs, 1);
}
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
return r;
}

View File

@@ -18,36 +18,21 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "libusb.h"
#if defined(_WIN32)
#define msleep(msecs) Sleep(msecs)
#else
#include <time.h>
#define msleep(msecs) nanosleep(&(struct timespec){msecs / 1000, (msecs * 1000000) % 1000000000UL}, NULL);
#endif
#if defined(_MSC_VER)
#define snprintf _snprintf
#define putenv _putenv
#endif
#if !defined(bool)
#define bool int
#endif
#if !defined(true)
#define true (1 == 1)
#endif
#if !defined(false)
#define false (!true)
#endif
// Future versions of libusb will use usb_interface instead of interface
// in libusb_config_descriptor => catter for that
#define usb_interface interface
@@ -58,6 +43,16 @@ static bool extra_info = false;
static bool force_device_request = false; // For WCID descriptor queries
static const char* binary_name = NULL;
static inline void msleep(int msecs)
{
#if defined(_WIN32)
Sleep(msecs);
#else
const struct timespec ts = { msecs / 1000, (msecs % 1000) * 1000000L };
nanosleep(&ts, NULL);
#endif
}
static void perr(char const *format, ...)
{
va_list args;
@@ -976,6 +971,7 @@ static int test_device(uint16_t vid, uint16_t pid)
int main(int argc, char** argv)
{
static char debug_env_str[] = "LIBUSB_DEBUG=4"; // LIBUSB_LOG_LEVEL_DEBUG
bool show_help = false;
bool debug_mode = false;
const struct libusb_version* version;
@@ -1103,7 +1099,7 @@ int main(int argc, char** argv)
// but since we can't call on libusb_set_option() before libusb_init(), we use the env variable method
old_dbg_str = getenv("LIBUSB_DEBUG");
if (debug_mode) {
if (putenv("LIBUSB_DEBUG=4") != 0) // LIBUSB_LOG_LEVEL_DEBUG
if (putenv(debug_env_str) != 0)
printf("Unable to set debug level\n");
}