mirror of https://github.com/GOSTSec/sgminer
Con Kolivas
11 years ago
8 changed files with 0 additions and 2727 deletions
@ -1,9 +0,0 @@
@@ -1,9 +0,0 @@
|
||||
EXTRA_DIST = doxygen.cfg.in |
||||
|
||||
docs: doxygen.cfg |
||||
doxygen $^ |
||||
|
||||
docs-upload: docs |
||||
ln -s html api-1.0 |
||||
scp -r api-1.0 pbatard@web.sourceforge.net:/home/project-web/libusbx/htdocs |
||||
rm -f api-1.0 |
File diff suppressed because it is too large
Load Diff
@ -1,19 +0,0 @@
@@ -1,19 +0,0 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/libusb |
||||
LDADD = ../libusb/libusb-1.0.la |
||||
|
||||
noinst_PROGRAMS = listdevs xusb fxload hotplugtest |
||||
|
||||
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 |
||||
|
||||
fxload_SOURCES = ezusb.c ezusb.h fxload.c |
||||
fxload_CFLAGS = $(THREAD_CFLAGS) $(AM_CFLAGS) |
@ -1,506 +0,0 @@
@@ -1,506 +0,0 @@
|
||||
/*
|
||||
* libusbx example program to manipulate U.are.U 4000B fingerprint scanner. |
||||
* Copyright © 2007 Daniel Drake <dsd@gentoo.org> |
||||
* |
||||
* 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 <errno.h> |
||||
#include <signal.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "libusb.h" |
||||
|
||||
#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 struct 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 int find_dpfp_device(void) |
||||
{ |
||||
devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a); |
||||
return devh ? 0 : -EIO; |
||||
} |
||||
|
||||
static int print_f0_data(void) |
||||
{ |
||||
unsigned char data[0x10]; |
||||
int r; |
||||
unsigned int i; |
||||
|
||||
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 ((unsigned int) r < 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 ((unsigned int) 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 ((unsigned int) r < 1) { |
||||
fprintf(stderr, "short write (%d)", 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 ((unsigned int) r < 1) { |
||||
fprintf(stderr, "short write (%d)", 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"); |
||||
do_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; |
||||
} |
||||
|
||||
static int set_mode_async(unsigned char data) |
||||
{ |
||||
unsigned char *buf = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1); |
||||
struct libusb_transfer *transfer; |
||||
|
||||
if (!buf) |
||||
return -ENOMEM; |
||||
|
||||
transfer = libusb_alloc_transfer(0); |
||||
if (!transfer) { |
||||
free(buf); |
||||
return -ENOMEM; |
||||
} |
||||
|
||||
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 *fd; |
||||
char filename[64]; |
||||
|
||||
snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++); |
||||
fd = fopen(filename, "w"); |
||||
if (!fd) |
||||
return -1; |
||||
|
||||
fputs("P5 384 289 255 ", fd); |
||||
(void) fwrite(data + 64, 1, 384*289, fd); |
||||
fclose(fd); |
||||
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); |
||||
do_exit = 2; |
||||
libusb_free_transfer(transfer); |
||||
irq_transfer = NULL; |
||||
return; |
||||
} |
||||
|
||||
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; |
||||
} |
||||
} 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; |
||||
} |
||||
} else { |
||||
printf("finger-on-sensor detected in wrong state!\n"); |
||||
} |
||||
break; |
||||
} |
||||
if (libusb_submit_transfer(irq_transfer) < 0) |
||||
do_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; |
||||
} |
||||
|
||||
printf("Image callback\n"); |
||||
save_to_file(imgbuf); |
||||
if (next_state() < 0) { |
||||
do_exit = 2; |
||||
return; |
||||
} |
||||
if (libusb_submit_transfer(img_transfer) < 0) |
||||
do_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) |
||||
return -ENOMEM; |
||||
|
||||
irq_transfer = libusb_alloc_transfer(0); |
||||
if (!irq_transfer) |
||||
return -ENOMEM; |
||||
|
||||
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) |
||||
{ |
||||
do_exit = 1; |
||||
} |
||||
|
||||
int main(void) |
||||
{ |
||||
struct sigaction sigact; |
||||
int r = 1; |
||||
|
||||
r = libusb_init(NULL); |
||||
if (r < 0) { |
||||
fprintf(stderr, "failed to initialise libusb\n"); |
||||
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, "usb_claim_interface error %d\n", 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 */ |
||||
|
||||
r = alloc_transfers(); |
||||
if (r < 0) |
||||
goto out_deinit; |
||||
|
||||
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; |
||||
} |
||||
|
||||
printf("shutting down...\n"); |
||||
|
||||
if (irq_transfer) { |
||||
r = libusb_cancel_transfer(irq_transfer); |
||||
if (r < 0) |
||||
goto out_deinit; |
||||
} |
||||
|
||||
if (img_transfer) { |
||||
r = libusb_cancel_transfer(img_transfer); |
||||
if (r < 0) |
||||
goto out_deinit; |
||||
} |
||||
|
||||
while (irq_transfer || img_transfer) |
||||
if (libusb_handle_events(NULL) < 0) |
||||
break; |
||||
|
||||
if (do_exit == 1) |
||||
r = 0; |
||||
else |
||||
r = 1; |
||||
|
||||
out_deinit: |
||||
libusb_free_transfer(img_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; |
||||
} |
@ -1,544 +0,0 @@
@@ -1,544 +0,0 @@
|
||||
/*
|
||||
* libusbx example program to manipulate U.are.U 4000B fingerprint scanner. |
||||
* Copyright © 2007 Daniel Drake <dsd@gentoo.org> |
||||
* |
||||
* 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 <errno.h> |
||||
#include <pthread.h> |
||||
#include <signal.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "libusb.h" |
||||
|
||||
#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 struct 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 pthread_t poll_thread; |
||||
static pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER; |
||||
static pthread_mutex_t exit_cond_lock = PTHREAD_MUTEX_INITIALIZER; |
||||
|
||||
static void request_exit(int code) |
||||
{ |
||||
do_exit = code; |
||||
pthread_cond_signal(&exit_cond); |
||||
} |
||||
|
||||
static void *poll_thread_main(void *arg) |
||||
{ |
||||
int r = 0; |
||||
printf("poll thread running\n"); |
||||
|
||||
while (!do_exit) { |
||||
struct timeval tv = { 1, 0 }; |
||||
r = libusb_handle_events_timeout(NULL, &tv); |
||||
if (r < 0) { |
||||
request_exit(2); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
printf("poll thread shutting down\n"); |
||||
return NULL; |
||||
} |
||||
|
||||
static int find_dpfp_device(void) |
||||
{ |
||||
devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a); |
||||
return devh ? 0 : -EIO; |
||||
} |
||||
|
||||
static int print_f0_data(void) |
||||
{ |
||||
unsigned char data[0x10]; |
||||
int r; |
||||
unsigned int i; |
||||
|
||||
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 ((unsigned int) r < 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 ((unsigned int) 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 ((unsigned int) r < 1) { |
||||
fprintf(stderr, "short write (%d)", 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 ((unsigned int) r < 1) { |
||||
fprintf(stderr, "short write (%d)", 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 = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1); |
||||
struct libusb_transfer *transfer; |
||||
|
||||
if (!buf) |
||||
return -ENOMEM; |
||||
|
||||
transfer = libusb_alloc_transfer(0); |
||||
if (!transfer) { |
||||
free(buf); |
||||
return -ENOMEM; |
||||
} |
||||
|
||||
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 *fd; |
||||
char filename[64]; |
||||
|
||||
snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++); |
||||
fd = fopen(filename, "w"); |
||||
if (!fd) |
||||
return -1; |
||||
|
||||
fputs("P5 384 289 255 ", fd); |
||||
(void) fwrite(data + 64, 1, 384*289, fd); |
||||
fclose(fd); |
||||
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); |
||||
irq_transfer = NULL; |
||||
request_exit(2); |
||||
return; |
||||
} |
||||
|
||||
printf("IRQ callback %02x\n", irqtype); |
||||
switch (state) { |
||||
case STATE_AWAIT_IRQ_FINGER_DETECTED: |
||||
if (irqtype == 0x01) { |
||||
if (next_state() < 0) { |
||||
request_exit(2); |
||||
return; |
||||
} |
||||
} else { |
||||
printf("finger-on-sensor detected in wrong state!\n"); |
||||
} |
||||
break; |
||||
case STATE_AWAIT_IRQ_FINGER_REMOVED: |
||||
if (irqtype == 0x02) { |
||||
if (next_state() < 0) { |
||||
request_exit(2); |
||||
return; |
||||
} |
||||
} else { |
||||
printf("finger-on-sensor detected in wrong state!\n"); |
||||
} |
||||
break; |
||||
} |
||||
if (libusb_submit_transfer(irq_transfer) < 0) |
||||
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); |
||||
img_transfer = NULL; |
||||
request_exit(2); |
||||
return; |
||||
} |
||||
|
||||
printf("Image callback\n"); |
||||
save_to_file(imgbuf); |
||||
if (next_state() < 0) { |
||||
request_exit(2); |
||||
return; |
||||
} |
||||
if (libusb_submit_transfer(img_transfer) < 0) |
||||
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) |
||||
return -ENOMEM; |
||||
|
||||
irq_transfer = libusb_alloc_transfer(0); |
||||
if (!irq_transfer) |
||||
return -ENOMEM; |
||||
|
||||
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) |
||||
{ |
||||
request_exit(1); |
||||
} |
||||
|
||||
int main(void) |
||||
{ |
||||
struct sigaction sigact; |
||||
int r = 1; |
||||
|
||||
r = libusb_init(NULL); |
||||
if (r < 0) { |
||||
fprintf(stderr, "failed to initialise libusb\n"); |
||||
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, "usb_claim_interface error %d %s\n", r, 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 */ |
||||
|
||||
sigact.sa_handler = sighandler; |
||||
sigemptyset(&sigact.sa_mask); |
||||
sigact.sa_flags = 0; |
||||
sigaction(SIGINT, &sigact, NULL); |
||||
sigaction(SIGTERM, &sigact, NULL); |
||||
sigaction(SIGQUIT, &sigact, NULL); |
||||
|
||||
r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL); |
||||
if (r) |
||||
goto out_deinit; |
||||
|
||||
r = alloc_transfers(); |
||||
if (r < 0) { |
||||
request_exit(1); |
||||
pthread_join(poll_thread, NULL); |
||||
goto out_deinit; |
||||
} |
||||
|
||||
r = init_capture(); |
||||
if (r < 0) { |
||||
request_exit(1); |
||||
pthread_join(poll_thread, NULL); |
||||
goto out_deinit; |
||||
} |
||||
|
||||
while (!do_exit) { |
||||
pthread_mutex_lock(&exit_cond_lock); |
||||
pthread_cond_wait(&exit_cond, &exit_cond_lock); |
||||
pthread_mutex_unlock(&exit_cond_lock); |
||||
} |
||||
|
||||
printf("shutting down...\n"); |
||||
pthread_join(poll_thread, NULL); |
||||
|
||||
r = libusb_cancel_transfer(irq_transfer); |
||||
if (r < 0) { |
||||
request_exit(1); |
||||
goto out_deinit; |
||||
} |
||||
|
||||
r = libusb_cancel_transfer(img_transfer); |
||||
if (r < 0) { |
||||
request_exit(1); |
||||
goto out_deinit; |
||||
} |
||||
|
||||
while (img_transfer || irq_transfer) |
||||
if (libusb_handle_events(NULL) < 0) |
||||
break; |
||||
|
||||
if (do_exit == 1) |
||||
r = 0; |
||||
else |
||||
r = 1; |
||||
|
||||
out_deinit: |
||||
libusb_free_transfer(img_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; |
||||
} |
@ -1,97 +0,0 @@
@@ -1,97 +0,0 @@
|
||||
/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */ |
||||
/*
|
||||
* libusb example program for hotplug API |
||||
* Copyright © 2012-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 <stdlib.h> |
||||
#include <stdio.h> |
||||
|
||||
#include "libusb.h" |
||||
|
||||
int done = 0; |
||||
libusb_device_handle *handle; |
||||
|
||||
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; |
||||
|
||||
rc = libusb_get_device_descriptor(dev, &desc); |
||||
if (LIBUSB_SUCCESS != rc) { |
||||
fprintf (stderr, "Error getting device descriptor\n"); |
||||
} |
||||
|
||||
printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct); |
||||
|
||||
libusb_open (dev, &handle); |
||||
|
||||
done++; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) |
||||
{ |
||||
printf ("Device detached\n"); |
||||
|
||||
libusb_close (handle); |
||||
|
||||
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) ? strtol (argv[1], NULL, 0) : 0x045a; |
||||
product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005; |
||||
class_id = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY; |
||||
|
||||
libusb_init (NULL); |
||||
|
||||
if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) { |
||||
printf ("Hotplug capabilites 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) { |
||||
libusb_handle_events (NULL); |
||||
} |
||||
|
||||
libusb_exit (NULL); |
||||
} |
@ -1,71 +0,0 @@
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* libusbx example program to list devices on the bus |
||||
* Copyright © 2007 Daniel Drake <dsd@gentoo.org> |
||||
* |
||||
* 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 "libusb.h" |
||||
|
||||
static void print_devs(libusb_device **devs) |
||||
{ |
||||
libusb_device *dev; |
||||
int i = 0, j = 0; |
||||
uint8_t path[8]; |
||||
|
||||
while ((dev = devs[i++]) != NULL) { |
||||
struct libusb_device_descriptor desc; |
||||
int r = libusb_get_device_descriptor(dev, &desc); |
||||
if (r < 0) { |
||||
fprintf(stderr, "failed to get device descriptor"); |
||||
return; |
||||
} |
||||
|
||||
printf("%04x:%04x (bus %d, device %d)", |
||||
desc.idVendor, desc.idProduct, |
||||
libusb_get_bus_number(dev), libusb_get_device_address(dev)); |
||||
|
||||
r = libusb_get_port_numbers(dev, path, sizeof(path)); |
||||
if (r > 0) { |
||||
printf(" path: %d", path[0]); |
||||
for (j = 1; j < r; j++) |
||||
printf(".%d", path[j]); |
||||
} |
||||
printf("\n"); |
||||
} |
||||
} |
||||
|
||||
int main(void) |
||||
{ |
||||
libusb_device **devs; |
||||
int r; |
||||
ssize_t cnt; |
||||
|
||||
r = libusb_init(NULL); |
||||
if (r < 0) |
||||
return r; |
||||
|
||||
cnt = libusb_get_device_list(NULL, &devs); |
||||
if (cnt < 0) |
||||
return (int) cnt; |
||||
|
||||
print_devs(devs); |
||||
libusb_free_device_list(devs, 1); |
||||
|
||||
libusb_exit(NULL); |
||||
return 0; |
||||
} |
@ -1,193 +0,0 @@
@@ -1,193 +0,0 @@
|
||||
/*
|
||||
* 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 <unistd.h> |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <errno.h> |
||||
#include <signal.h> |
||||
|
||||
#include <libusb.h> |
||||
|
||||
|
||||
#define EP_DATA_IN 0x82 |
||||
#define EP_ISO_IN 0x86 |
||||
|
||||
static int 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 LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr) |
||||
{ |
||||
unsigned 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 %u status %d\n", i, pack->status); |
||||
exit(5); |
||||
} |
||||
|
||||
printf("pack%u 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) |
||||
return -ENOMEM; |
||||
|
||||
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); |
||||
|
||||
gettimeofday(&tv_start, NULL); |
||||
|
||||
/* 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 int diff_msec; |
||||
|
||||
gettimeofday(&tv_stop, NULL); |
||||
|
||||
diff_msec = (tv_stop.tv_sec - tv_start.tv_sec)*1000; |
||||
diff_msec += (tv_stop.tv_usec - tv_start.tv_usec)/1000; |
||||
|
||||
printf("%lu transfers (total %lu bytes) in %u miliseconds => %lu bytes/sec\n", |
||||
num_xfer, num_bytes, diff_msec, (num_bytes*1000)/diff_msec); |
||||
} |
||||
|
||||
static void sig_hdlr(int signum) |
||||
{ |
||||
switch (signum) { |
||||
case SIGINT: |
||||
measure(); |
||||
do_exit = 1; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
int main(int argc, char **argv) |
||||
{ |
||||
int rc; |
||||
struct sigaction sigact; |
||||
|
||||
sigact.sa_handler = sig_hdlr; |
||||
sigemptyset(&sigact.sa_mask); |
||||
sigact.sa_flags = 0; |
||||
sigaction(SIGINT, &sigact, NULL); |
||||
|
||||
rc = libusb_init(NULL); |
||||
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, 0); |
||||
out: |
||||
if (devh) |
||||
libusb_close(devh); |
||||
libusb_exit(NULL); |
||||
return rc; |
||||
} |
Loading…
Reference in new issue