2
* libusb example program to manipulate U.are.U 4000B fingerprint scanner.
3
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
5
* Basic image capture program only, does not consider the powerup quirks or
6
* the fact that image encryption may be enabled. Not expected to work
7
* flawlessly all of the time.
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31
#include <libusb/libusb.h>
33
#define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
34
#define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
35
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
36
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
38
#define INTR_LENGTH 64
42
MODE_AWAIT_FINGER_ON = 0x10,
43
MODE_AWAIT_FINGER_OFF = 0x12,
49
static int next_state(void);
52
STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
53
STATE_AWAIT_IRQ_FINGER_DETECTED,
54
STATE_AWAIT_MODE_CHANGE_CAPTURE,
56
STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
57
STATE_AWAIT_IRQ_FINGER_REMOVED,
61
static struct libusb_device_handle *devh = NULL;
62
static unsigned char imgbuf[0x1b340];
63
static unsigned char irqbuf[INTR_LENGTH];
64
static struct libusb_transfer *img_transfer = NULL;
65
static struct libusb_transfer *irq_transfer = NULL;
66
static int img_idx = 0;
67
static int do_exit = 0;
69
static pthread_t poll_thread;
70
static pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
71
static pthread_mutex_t exit_cond_lock = PTHREAD_MUTEX_INITIALIZER;
73
static void request_exit(int code)
76
pthread_cond_signal(&exit_cond);
79
static void *poll_thread_main(void *arg)
82
printf("poll thread running\n");
85
struct timeval tv = { 1, 0 };
86
r = libusb_handle_events_timeout(NULL, &tv);
93
printf("poll thread shutting down\n");
98
static int find_dpfp_device(void)
100
devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
101
return devh ? 0 : -EIO;
104
static int print_f0_data(void)
106
unsigned char data[0x10];
110
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
113
fprintf(stderr, "F0 error %d\n", r);
116
if ((unsigned int) r < sizeof(data)) {
117
fprintf(stderr, "short read (%d)\n", r);
122
for (i = 0; i < sizeof(data); i++)
123
printf("%02x ", data[i]);
128
static int get_hwstat(unsigned char *status)
132
r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
134
fprintf(stderr, "read hwstat error %d\n", r);
137
if ((unsigned int) r < 1) {
138
fprintf(stderr, "short read (%d)\n", r);
142
printf("hwstat reads %02x\n", *status);
146
static int set_hwstat(unsigned char data)
150
printf("set hwstat to %02x\n", data);
151
r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
153
fprintf(stderr, "set hwstat error %d\n", r);
156
if ((unsigned int) r < 1) {
157
fprintf(stderr, "short write (%d)", r);
164
static int set_mode(unsigned char data)
167
printf("set mode %02x\n", data);
169
r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
171
fprintf(stderr, "set mode error %d\n", r);
174
if ((unsigned int) r < 1) {
175
fprintf(stderr, "short write (%d)", r);
182
static void cb_mode_changed(struct libusb_transfer *transfer)
184
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
185
fprintf(stderr, "mode change transfer not completed!\n");
189
printf("async cb_mode_changed length=%d actual_length=%d\n",
190
transfer->length, transfer->actual_length);
191
if (next_state() < 0)
195
static int set_mode_async(unsigned char data)
197
unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
198
struct libusb_transfer *transfer;
203
transfer = libusb_alloc_transfer(0);
209
printf("async set mode %02x\n", data);
210
libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
211
buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
212
libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
215
transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
216
| LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
217
return libusb_submit_transfer(transfer);
220
static int do_sync_intr(unsigned char *data)
225
r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
228
fprintf(stderr, "intr error %d\n", r);
231
if (transferred < INTR_LENGTH) {
232
fprintf(stderr, "short read (%d)\n", r);
236
printf("recv interrupt %04x\n", *((uint16_t *) data));
240
static int sync_intr(unsigned char type)
243
unsigned char data[INTR_LENGTH];
246
r = do_sync_intr(data);
254
static int save_to_file(unsigned char *data)
259
sprintf(filename, "finger%d.pgm", img_idx++);
260
fd = fopen(filename, "w");
264
fputs("P5 384 289 255 ", fd);
265
fwrite(data + 64, 1, 384*289, fd);
267
printf("saved image to %s\n", filename);
271
static int next_state(void)
274
printf("old state: %d\n", state);
276
case STATE_AWAIT_IRQ_FINGER_REMOVED:
277
state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
278
r = set_mode_async(MODE_AWAIT_FINGER_ON);
280
case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
281
state = STATE_AWAIT_IRQ_FINGER_DETECTED;
283
case STATE_AWAIT_IRQ_FINGER_DETECTED:
284
state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
285
r = set_mode_async(MODE_CAPTURE);
287
case STATE_AWAIT_MODE_CHANGE_CAPTURE:
288
state = STATE_AWAIT_IMAGE;
290
case STATE_AWAIT_IMAGE:
291
state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
292
r = set_mode_async(MODE_AWAIT_FINGER_OFF);
294
case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
295
state = STATE_AWAIT_IRQ_FINGER_REMOVED;
298
printf("unrecognised state %d\n", state);
301
fprintf(stderr, "error detected changing state\n");
305
printf("new state: %d\n", state);
309
static void cb_irq(struct libusb_transfer *transfer)
311
unsigned char irqtype = transfer->buffer[0];
313
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
314
fprintf(stderr, "irq transfer status %d?\n", transfer->status);
320
printf("IRQ callback %02x\n", irqtype);
322
case STATE_AWAIT_IRQ_FINGER_DETECTED:
323
if (irqtype == 0x01) {
324
if (next_state() < 0) {
329
printf("finger-on-sensor detected in wrong state!\n");
332
case STATE_AWAIT_IRQ_FINGER_REMOVED:
333
if (irqtype == 0x02) {
334
if (next_state() < 0) {
339
printf("finger-on-sensor detected in wrong state!\n");
343
if (libusb_submit_transfer(irq_transfer) < 0)
347
static void cb_img(struct libusb_transfer *transfer)
349
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
350
fprintf(stderr, "img transfer status %d?\n", transfer->status);
356
printf("Image callback\n");
357
save_to_file(imgbuf);
358
if (next_state() < 0) {
362
if (libusb_submit_transfer(img_transfer) < 0)
366
static int init_capture(void)
370
r = libusb_submit_transfer(irq_transfer);
374
r = libusb_submit_transfer(img_transfer);
376
libusb_cancel_transfer(irq_transfer);
378
if (libusb_handle_events(NULL) < 0)
383
/* start state machine */
384
state = STATE_AWAIT_IRQ_FINGER_REMOVED;
388
static int do_init(void)
390
unsigned char status;
393
r = get_hwstat(&status);
397
if (!(status & 0x80)) {
398
r = set_hwstat(status | 0x80);
401
r = get_hwstat(&status);
407
r = set_hwstat(status);
411
r = get_hwstat(&status);
422
static int alloc_transfers(void)
424
img_transfer = libusb_alloc_transfer(0);
428
irq_transfer = libusb_alloc_transfer(0);
432
libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
433
sizeof(imgbuf), cb_img, NULL, 0);
434
libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
435
sizeof(irqbuf), cb_irq, NULL, 0);
440
static void sighandler(int signum)
448
struct sigaction sigact;
452
r = libusb_init(NULL);
454
fprintf(stderr, "failed to initialise libusb\n");
458
r = find_dpfp_device();
460
fprintf(stderr, "Could not find/open device\n");
464
r = libusb_claim_interface(devh, 0);
466
fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
469
printf("claimed interface\n");
479
/* async from here onwards */
481
sigact.sa_handler = sighandler;
482
sigemptyset(&sigact.sa_mask);
484
sigaction(SIGINT, &sigact, NULL);
485
sigaction(SIGTERM, &sigact, NULL);
486
sigaction(SIGQUIT, &sigact, NULL);
488
signal(SIGINT, sighandler);
489
signal(SIGTERM, sighandler);
492
r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL);
496
r = alloc_transfers();
499
pthread_join(poll_thread, NULL);
506
pthread_join(poll_thread, NULL);
511
pthread_mutex_lock(&exit_cond_lock);
512
pthread_cond_wait(&exit_cond, &exit_cond_lock);
513
pthread_mutex_unlock(&exit_cond_lock);
516
printf("shutting down...\n");
517
pthread_join(poll_thread, NULL);
519
r = libusb_cancel_transfer(irq_transfer);
525
r = libusb_cancel_transfer(img_transfer);
531
while (img_transfer || irq_transfer)
532
if (libusb_handle_events(NULL) < 0)
541
libusb_free_transfer(img_transfer);
542
libusb_free_transfer(irq_transfer);
546
libusb_release_interface(devh, 0);
550
return r >= 0 ? r : -r;