|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* libusb example program to manipulate U.are.U 4000B fingerprint scanner.
|
|
kusano |
7d535a |
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org></dsd@gentoo.org>
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Basic image capture program only, does not consider the powerup quirks or
|
|
kusano |
7d535a |
* the fact that image encryption may be enabled. Not expected to work
|
|
kusano |
7d535a |
* flawlessly all of the time.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This library is free software; you can redistribute it and/or
|
|
kusano |
7d535a |
* modify it under the terms of the GNU Lesser General Public
|
|
kusano |
7d535a |
* License as published by the Free Software Foundation; either
|
|
kusano |
7d535a |
* version 2.1 of the License, or (at your option) any later version.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This library is distributed in the hope that it will be useful,
|
|
kusano |
7d535a |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
kusano |
7d535a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
kusano |
7d535a |
* Lesser General Public License for more details.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* You should have received a copy of the GNU Lesser General Public
|
|
kusano |
7d535a |
* License along with this library; if not, write to the Free Software
|
|
kusano |
7d535a |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <errno.h></errno.h>
|
|
kusano |
7d535a |
#include <pthread.h></pthread.h>
|
|
kusano |
7d535a |
#include <signal.h></signal.h>
|
|
kusano |
7d535a |
#include <string.h></string.h>
|
|
kusano |
7d535a |
#include <stdio.h></stdio.h>
|
|
kusano |
7d535a |
#include <stdlib.h></stdlib.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <libusb.h></libusb.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
|
|
kusano |
7d535a |
#define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
|
|
kusano |
7d535a |
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
|
|
kusano |
7d535a |
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
|
|
kusano |
7d535a |
#define USB_RQ 0x04
|
|
kusano |
7d535a |
#define INTR_LENGTH 64
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
enum {
|
|
kusano |
7d535a |
MODE_INIT = 0x00,
|
|
kusano |
7d535a |
MODE_AWAIT_FINGER_ON = 0x10,
|
|
kusano |
7d535a |
MODE_AWAIT_FINGER_OFF = 0x12,
|
|
kusano |
7d535a |
MODE_CAPTURE = 0x20,
|
|
kusano |
7d535a |
MODE_SHUT_UP = 0x30,
|
|
kusano |
7d535a |
MODE_READY = 0x80,
|
|
kusano |
7d535a |
};
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int next_state(void);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
enum {
|
|
kusano |
7d535a |
STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
|
|
kusano |
7d535a |
STATE_AWAIT_IRQ_FINGER_DETECTED,
|
|
kusano |
7d535a |
STATE_AWAIT_MODE_CHANGE_CAPTURE,
|
|
kusano |
7d535a |
STATE_AWAIT_IMAGE,
|
|
kusano |
7d535a |
STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
|
|
kusano |
7d535a |
STATE_AWAIT_IRQ_FINGER_REMOVED,
|
|
kusano |
7d535a |
};
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int state = 0;
|
|
kusano |
7d535a |
static struct libusb_device_handle *devh = NULL;
|
|
kusano |
7d535a |
static unsigned char imgbuf[0x1b340];
|
|
kusano |
7d535a |
static unsigned char irqbuf[INTR_LENGTH];
|
|
kusano |
7d535a |
static struct libusb_transfer *img_transfer = NULL;
|
|
kusano |
7d535a |
static struct libusb_transfer *irq_transfer = NULL;
|
|
kusano |
7d535a |
static int img_idx = 0;
|
|
kusano |
7d535a |
static int do_exit = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static pthread_t poll_thread;
|
|
kusano |
7d535a |
static pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
|
|
kusano |
7d535a |
static pthread_mutex_t exit_cond_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void request_exit(int code)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
do_exit = code;
|
|
kusano |
7d535a |
pthread_cond_signal(&exit_cond);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void *poll_thread_main(void *arg)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r = 0;
|
|
kusano |
7d535a |
printf("poll thread running\n");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (!do_exit) {
|
|
kusano |
7d535a |
struct timeval tv = { 1, 0 };
|
|
kusano |
7d535a |
r = libusb_handle_events_timeout(NULL, &tv);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("poll thread shutting down\n");
|
|
kusano |
7d535a |
return NULL;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int find_dpfp_device(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
|
|
kusano |
7d535a |
return devh ? 0 : -EIO;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int print_f0_data(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned char data[0x10];
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
unsigned int i;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
|
|
kusano |
7d535a |
sizeof(data), 0);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "F0 error %d\n", r);
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if ((unsigned int) r < sizeof(data)) {
|
|
kusano |
7d535a |
fprintf(stderr, "short read (%d)\n", r);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("F0 data:");
|
|
kusano |
7d535a |
for (i = 0; i < sizeof(data); i++)
|
|
kusano |
7d535a |
printf("%02x ", data[i]);
|
|
kusano |
7d535a |
printf("\n");
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int get_hwstat(unsigned char *status)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "read hwstat error %d\n", r);
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if ((unsigned int) r < 1) {
|
|
kusano |
7d535a |
fprintf(stderr, "short read (%d)\n", r);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("hwstat reads %02x\n", *status);
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int set_hwstat(unsigned char data)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("set hwstat to %02x\n", data);
|
|
kusano |
7d535a |
r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "set hwstat error %d\n", r);
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if ((unsigned int) r < 1) {
|
|
kusano |
7d535a |
fprintf(stderr, "short write (%d)", r);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int set_mode(unsigned char data)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
printf("set mode %02x\n", data);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "set mode error %d\n", r);
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if ((unsigned int) r < 1) {
|
|
kusano |
7d535a |
fprintf(stderr, "short write (%d)", r);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
|
|
kusano |
7d535a |
fprintf(stderr, "mode change transfer not completed!\n");
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("async cb_mode_changed length=%d actual_length=%d\n",
|
|
kusano |
7d535a |
transfer->length, transfer->actual_length);
|
|
kusano |
7d535a |
if (next_state() < 0)
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int set_mode_async(unsigned char data)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
|
|
kusano |
7d535a |
struct libusb_transfer *transfer;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (!buf)
|
|
kusano |
7d535a |
return -ENOMEM;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
transfer = libusb_alloc_transfer(0);
|
|
kusano |
7d535a |
if (!transfer) {
|
|
kusano |
7d535a |
free(buf);
|
|
kusano |
7d535a |
return -ENOMEM;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("async set mode %02x\n", data);
|
|
kusano |
7d535a |
libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
|
|
kusano |
7d535a |
buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
|
|
kusano |
7d535a |
libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
|
|
kusano |
7d535a |
1000);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
|
|
kusano |
7d535a |
| LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
|
|
kusano |
7d535a |
return libusb_submit_transfer(transfer);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int do_sync_intr(unsigned char *data)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
int transferred;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
|
|
kusano |
7d535a |
&transferred, 1000);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "intr error %d\n", r);
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (transferred < INTR_LENGTH) {
|
|
kusano |
7d535a |
fprintf(stderr, "short read (%d)\n", r);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("recv interrupt %04x\n", *((uint16_t *) data));
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int sync_intr(unsigned char type)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
unsigned char data[INTR_LENGTH];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (1) {
|
|
kusano |
7d535a |
r = do_sync_intr(data);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
if (data[0] == type)
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int save_to_file(unsigned char *data)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
FILE *fd;
|
|
kusano |
7d535a |
char filename[64];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
|
|
kusano |
7d535a |
fd = fopen(filename, "w");
|
|
kusano |
7d535a |
if (!fd)
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
fputs("P5 384 289 255 ", fd);
|
|
kusano |
7d535a |
(void) fwrite(data + 64, 1, 384*289, fd);
|
|
kusano |
7d535a |
fclose(fd);
|
|
kusano |
7d535a |
printf("saved image to %s\n", filename);
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int next_state(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r = 0;
|
|
kusano |
7d535a |
printf("old state: %d\n", state);
|
|
kusano |
7d535a |
switch (state) {
|
|
kusano |
7d535a |
case STATE_AWAIT_IRQ_FINGER_REMOVED:
|
|
kusano |
7d535a |
state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
|
|
kusano |
7d535a |
r = set_mode_async(MODE_AWAIT_FINGER_ON);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
|
|
kusano |
7d535a |
state = STATE_AWAIT_IRQ_FINGER_DETECTED;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case STATE_AWAIT_IRQ_FINGER_DETECTED:
|
|
kusano |
7d535a |
state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
|
|
kusano |
7d535a |
r = set_mode_async(MODE_CAPTURE);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case STATE_AWAIT_MODE_CHANGE_CAPTURE:
|
|
kusano |
7d535a |
state = STATE_AWAIT_IMAGE;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case STATE_AWAIT_IMAGE:
|
|
kusano |
7d535a |
state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
|
|
kusano |
7d535a |
r = set_mode_async(MODE_AWAIT_FINGER_OFF);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
|
|
kusano |
7d535a |
state = STATE_AWAIT_IRQ_FINGER_REMOVED;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
default:
|
|
kusano |
7d535a |
printf("unrecognised state %d\n", state);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "error detected changing state\n");
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("new state: %d\n", state);
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned char irqtype = transfer->buffer[0];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
|
|
kusano |
7d535a |
fprintf(stderr, "irq transfer status %d?\n", transfer->status);
|
|
kusano |
7d535a |
irq_transfer = NULL;
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
return;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("IRQ callback %02x\n", irqtype);
|
|
kusano |
7d535a |
switch (state) {
|
|
kusano |
7d535a |
case STATE_AWAIT_IRQ_FINGER_DETECTED:
|
|
kusano |
7d535a |
if (irqtype == 0x01) {
|
|
kusano |
7d535a |
if (next_state() < 0) {
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
return;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
printf("finger-on-sensor detected in wrong state!\n");
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
case STATE_AWAIT_IRQ_FINGER_REMOVED:
|
|
kusano |
7d535a |
if (irqtype == 0x02) {
|
|
kusano |
7d535a |
if (next_state() < 0) {
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
return;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
printf("finger-on-sensor detected in wrong state!\n");
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (libusb_submit_transfer(irq_transfer) < 0)
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
|
|
kusano |
7d535a |
fprintf(stderr, "img transfer status %d?\n", transfer->status);
|
|
kusano |
7d535a |
img_transfer = NULL;
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
return;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("Image callback\n");
|
|
kusano |
7d535a |
save_to_file(imgbuf);
|
|
kusano |
7d535a |
if (next_state() < 0) {
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
return;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (libusb_submit_transfer(img_transfer) < 0)
|
|
kusano |
7d535a |
request_exit(2);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int init_capture(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_submit_transfer(irq_transfer);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_submit_transfer(img_transfer);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
libusb_cancel_transfer(irq_transfer);
|
|
kusano |
7d535a |
while (irq_transfer)
|
|
kusano |
7d535a |
if (libusb_handle_events(NULL) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* start state machine */
|
|
kusano |
7d535a |
state = STATE_AWAIT_IRQ_FINGER_REMOVED;
|
|
kusano |
7d535a |
return next_state();
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int do_init(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned char status;
|
|
kusano |
7d535a |
int r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = get_hwstat(&status);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (!(status & 0x80)) {
|
|
kusano |
7d535a |
r = set_hwstat(status | 0x80);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
r = get_hwstat(&status);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
status &= ~0x80;
|
|
kusano |
7d535a |
r = set_hwstat(status);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = get_hwstat(&status);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = sync_intr(0x56);
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
return r;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int alloc_transfers(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
img_transfer = libusb_alloc_transfer(0);
|
|
kusano |
7d535a |
if (!img_transfer)
|
|
kusano |
7d535a |
return -ENOMEM;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
irq_transfer = libusb_alloc_transfer(0);
|
|
kusano |
7d535a |
if (!irq_transfer)
|
|
kusano |
7d535a |
return -ENOMEM;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
|
|
kusano |
7d535a |
sizeof(imgbuf), cb_img, NULL, 0);
|
|
kusano |
7d535a |
libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
|
|
kusano |
7d535a |
sizeof(irqbuf), cb_irq, NULL, 0);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void sighandler(int signum)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
request_exit(1);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int main(void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
struct sigaction sigact;
|
|
kusano |
7d535a |
int r = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_init(NULL);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "failed to initialise libusb\n");
|
|
kusano |
7d535a |
exit(1);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = find_dpfp_device();
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "Could not find/open device\n");
|
|
kusano |
7d535a |
goto out;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_claim_interface(devh, 0);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
|
|
kusano |
7d535a |
goto out;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
printf("claimed interface\n");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = print_f0_data();
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
goto out_release;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = do_init();
|
|
kusano |
7d535a |
if (r < 0)
|
|
kusano |
7d535a |
goto out_deinit;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* async from here onwards */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
sigact.sa_handler = sighandler;
|
|
kusano |
7d535a |
sigemptyset(&sigact.sa_mask);
|
|
kusano |
7d535a |
sigact.sa_flags = 0;
|
|
kusano |
7d535a |
sigaction(SIGINT, &sigact, NULL);
|
|
kusano |
7d535a |
sigaction(SIGTERM, &sigact, NULL);
|
|
kusano |
7d535a |
sigaction(SIGQUIT, &sigact, NULL);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL);
|
|
kusano |
7d535a |
if (r)
|
|
kusano |
7d535a |
goto out_deinit;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = alloc_transfers();
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
request_exit(1);
|
|
kusano |
7d535a |
pthread_join(poll_thread, NULL);
|
|
kusano |
7d535a |
goto out_deinit;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = init_capture();
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
request_exit(1);
|
|
kusano |
7d535a |
pthread_join(poll_thread, NULL);
|
|
kusano |
7d535a |
goto out_deinit;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (!do_exit) {
|
|
kusano |
7d535a |
pthread_mutex_lock(&exit_cond_lock);
|
|
kusano |
7d535a |
pthread_cond_wait(&exit_cond, &exit_cond_lock);
|
|
kusano |
7d535a |
pthread_mutex_unlock(&exit_cond_lock);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("shutting down...\n");
|
|
kusano |
7d535a |
pthread_join(poll_thread, NULL);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_cancel_transfer(irq_transfer);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
request_exit(1);
|
|
kusano |
7d535a |
goto out_deinit;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
r = libusb_cancel_transfer(img_transfer);
|
|
kusano |
7d535a |
if (r < 0) {
|
|
kusano |
7d535a |
request_exit(1);
|
|
kusano |
7d535a |
goto out_deinit;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (img_transfer || irq_transfer)
|
|
kusano |
7d535a |
if (libusb_handle_events(NULL) < 0)
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (do_exit == 1)
|
|
kusano |
7d535a |
r = 0;
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
r = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
out_deinit:
|
|
kusano |
7d535a |
libusb_free_transfer(img_transfer);
|
|
kusano |
7d535a |
libusb_free_transfer(irq_transfer);
|
|
kusano |
7d535a |
set_mode(0);
|
|
kusano |
7d535a |
set_hwstat(0x80);
|
|
kusano |
7d535a |
out_release:
|
|
kusano |
7d535a |
libusb_release_interface(devh, 0);
|
|
kusano |
7d535a |
out:
|
|
kusano |
7d535a |
libusb_close(devh);
|
|
kusano |
7d535a |
libusb_exit(NULL);
|
|
kusano |
7d535a |
return r >= 0 ? r : -r;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|