2 * libusb example program to manipulate U.are.U 4000B fingerprint scanner.
3 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
4 * Copyright © 2016 Nathan Hjelm <hjelmn@mac.com>
5 * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
7 * Basic image capture program only, does not consider the powerup quirks or
8 * the fact that image encryption may be enabled. Not expected to work
9 * flawlessly all of the time.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #define snprintf _snprintf
40 #if defined(DPFP_THREADED)
41 #if defined(PLATFORM_POSIX)
44 #include <semaphore.h>
47 #define THREAD_RETURN_VALUE NULL
48 typedef sem_t * semaphore_t;
49 typedef pthread_t thread_t;
51 static inline semaphore_t semaphore_create(void)
56 sprintf(name, "/org.libusb.example.dpfp_threaded:%d", (int)getpid());
57 semaphore = sem_open(name, O_CREAT | O_EXCL, 0, 0);
58 if (semaphore == SEM_FAILED)
60 /* Remove semaphore so that it does not persist after process exits */
61 (void)sem_unlink(name);
65 static inline void semaphore_give(semaphore_t semaphore)
67 (void)sem_post(semaphore);
70 static inline void semaphore_take(semaphore_t semaphore)
72 (void)sem_wait(semaphore);
75 static inline void semaphore_destroy(semaphore_t semaphore)
77 (void)sem_close(semaphore);
80 static inline int thread_create(thread_t *thread,
81 void *(*thread_entry)(void *arg), void *arg)
83 return pthread_create(thread, NULL, thread_entry, arg) == 0 ? 0 : -1;
86 static inline void thread_join(thread_t thread)
88 (void)pthread_join(thread, NULL);
90 #elif defined(PLATFORM_WINDOWS)
91 #define THREAD_RETURN_VALUE 0
92 typedef HANDLE semaphore_t;
93 typedef HANDLE thread_t;
95 #if defined(__CYGWIN__)
96 typedef DWORD thread_return_t;
99 typedef unsigned thread_return_t;
102 static inline semaphore_t semaphore_create(void)
104 return CreateSemaphore(NULL, 0, 1, NULL);
107 static inline void semaphore_give(semaphore_t semaphore)
109 (void)ReleaseSemaphore(semaphore, 1, NULL);
112 static inline void semaphore_take(semaphore_t semaphore)
114 (void)WaitForSingleObject(semaphore, INFINITE);
117 static inline void semaphore_destroy(semaphore_t semaphore)
119 (void)CloseHandle(semaphore);
122 static inline int thread_create(thread_t *thread,
123 thread_return_t (__stdcall *thread_entry)(void *arg), void *arg)
125 #if defined(__CYGWIN__)
126 *thread = CreateThread(NULL, 0, thread_entry, arg, 0, NULL);
128 *thread = (HANDLE)_beginthreadex(NULL, 0, thread_entry, arg, 0, NULL);
130 return *thread != NULL ? 0 : -1;
133 static inline void thread_join(thread_t thread)
135 (void)WaitForSingleObject(thread, INFINITE);
136 (void)CloseHandle(thread);
141 #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
142 #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
143 #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
144 #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
146 #define INTR_LENGTH 64
150 MODE_AWAIT_FINGER_ON = 0x10,
151 MODE_AWAIT_FINGER_OFF = 0x12,
157 static int next_state(void);
160 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
161 STATE_AWAIT_IRQ_FINGER_DETECTED,
162 STATE_AWAIT_MODE_CHANGE_CAPTURE,
164 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
165 STATE_AWAIT_IRQ_FINGER_REMOVED,
168 static int state = 0;
169 static libusb_device_handle *devh = NULL;
170 static unsigned char imgbuf[0x1b340];
171 static unsigned char irqbuf[INTR_LENGTH];
172 static struct libusb_transfer *img_transfer = NULL;
173 static struct libusb_transfer *irq_transfer = NULL;
174 static int img_idx = 0;
175 static volatile sig_atomic_t do_exit = 0;
177 #if defined(DPFP_THREADED)
178 static semaphore_t exit_semaphore;
179 static thread_t poll_thread;
182 static void request_exit(sig_atomic_t code)
185 #if defined(DPFP_THREADED)
186 semaphore_give(exit_semaphore);
190 #if defined(DPFP_THREADED)
191 #if defined(PLATFORM_POSIX)
192 static void *poll_thread_main(void *arg)
193 #elif defined(PLATFORM_WINDOWS)
194 static thread_return_t __stdcall poll_thread_main(void *arg)
199 printf("poll thread running\n");
202 struct timeval tv = { 1, 0 };
205 r = libusb_handle_events_timeout(NULL, &tv);
212 printf("poll thread shutting down\n");
213 return THREAD_RETURN_VALUE;
217 static int find_dpfp_device(void)
219 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
220 return devh ? 0 : -ENODEV;
223 static int print_f0_data(void)
225 unsigned char data[0x10];
229 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
232 fprintf(stderr, "F0 error %d\n", r);
235 if (r < (int)sizeof(data)) {
236 fprintf(stderr, "short read (%d)\n", r);
241 for (i = 0; i < sizeof(data); i++)
242 printf(" %02x", data[i]);
247 static int get_hwstat(unsigned char *status)
251 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
253 fprintf(stderr, "read hwstat error %d\n", r);
257 fprintf(stderr, "short read (%d)\n", r);
261 printf("hwstat reads %02x\n", *status);
265 static int set_hwstat(unsigned char data)
269 printf("set hwstat to %02x\n", data);
270 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
272 fprintf(stderr, "set hwstat error %d\n", r);
276 fprintf(stderr, "short write (%d)\n", r);
283 static int set_mode(unsigned char data)
287 printf("set mode %02x\n", data);
288 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
290 fprintf(stderr, "set mode error %d\n", r);
294 fprintf(stderr, "short write (%d)\n", r);
301 static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
303 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
304 fprintf(stderr, "mode change transfer not completed!\n");
308 printf("async cb_mode_changed length=%d actual_length=%d\n",
309 transfer->length, transfer->actual_length);
310 if (next_state() < 0)
314 static int set_mode_async(unsigned char data)
316 unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
317 struct libusb_transfer *transfer;
322 transfer = libusb_alloc_transfer(0);
328 printf("async set mode %02x\n", data);
329 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
330 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
331 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
334 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
335 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
336 return libusb_submit_transfer(transfer);
339 static int do_sync_intr(unsigned char *data)
344 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
347 fprintf(stderr, "intr error %d\n", r);
350 if (transferred < INTR_LENGTH) {
351 fprintf(stderr, "short read (%d)\n", r);
355 printf("recv interrupt %04x\n", *((uint16_t *)data));
359 static int sync_intr(unsigned char type)
362 unsigned char data[INTR_LENGTH];
365 r = do_sync_intr(data);
373 static int save_to_file(unsigned char *data)
378 snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
379 f = fopen(filename, "w");
383 fputs("P5 384 289 255 ", f);
384 (void)fwrite(data + 64, 1, 384*289, f);
386 printf("saved image to %s\n", filename);
390 static int next_state(void)
394 printf("old state: %d\n", state);
396 case STATE_AWAIT_IRQ_FINGER_REMOVED:
397 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
398 r = set_mode_async(MODE_AWAIT_FINGER_ON);
400 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
401 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
403 case STATE_AWAIT_IRQ_FINGER_DETECTED:
404 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
405 r = set_mode_async(MODE_CAPTURE);
407 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
408 state = STATE_AWAIT_IMAGE;
410 case STATE_AWAIT_IMAGE:
411 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
412 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
414 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
415 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
418 printf("unrecognised state %d\n", state);
421 fprintf(stderr, "error detected changing state\n");
425 printf("new state: %d\n", state);
429 static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
431 unsigned char irqtype = transfer->buffer[0];
433 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
434 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
435 goto err_free_transfer;
438 printf("IRQ callback %02x\n", irqtype);
440 case STATE_AWAIT_IRQ_FINGER_DETECTED:
441 if (irqtype == 0x01) {
442 if (next_state() < 0)
443 goto err_free_transfer;
445 printf("finger-on-sensor detected in wrong state!\n");
448 case STATE_AWAIT_IRQ_FINGER_REMOVED:
449 if (irqtype == 0x02) {
450 if (next_state() < 0)
451 goto err_free_transfer;
453 printf("finger-on-sensor detected in wrong state!\n");
457 if (libusb_submit_transfer(irq_transfer) < 0)
458 goto err_free_transfer;
463 libusb_free_transfer(transfer);
468 static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
470 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
471 fprintf(stderr, "img transfer status %d?\n", transfer->status);
472 goto err_free_transfer;
475 printf("Image callback\n");
476 save_to_file(imgbuf);
477 if (next_state() < 0)
478 goto err_free_transfer;
480 if (libusb_submit_transfer(img_transfer) < 0)
481 goto err_free_transfer;
486 libusb_free_transfer(transfer);
491 static int init_capture(void)
495 r = libusb_submit_transfer(irq_transfer);
499 r = libusb_submit_transfer(img_transfer);
501 libusb_cancel_transfer(irq_transfer);
503 if (libusb_handle_events(NULL) < 0)
508 /* start state machine */
509 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
513 static int do_init(void)
515 unsigned char status;
518 r = get_hwstat(&status);
522 if (!(status & 0x80)) {
523 r = set_hwstat(status | 0x80);
526 r = get_hwstat(&status);
532 r = set_hwstat(status);
536 r = get_hwstat(&status);
547 static int alloc_transfers(void)
549 img_transfer = libusb_alloc_transfer(0);
553 irq_transfer = libusb_alloc_transfer(0);
557 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
558 sizeof(imgbuf), cb_img, NULL, 0);
559 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
560 sizeof(irqbuf), cb_irq, NULL, 0);
565 static void sighandler(int signum)
572 static void setup_signals(void)
574 #if defined(PLATFORM_POSIX)
575 struct sigaction sigact;
577 sigact.sa_handler = sighandler;
578 sigemptyset(&sigact.sa_mask);
580 (void)sigaction(SIGINT, &sigact, NULL);
581 (void)sigaction(SIGTERM, &sigact, NULL);
582 (void)sigaction(SIGQUIT, &sigact, NULL);
584 (void)signal(SIGINT, sighandler);
585 (void)signal(SIGTERM, sighandler);
593 r = libusb_init(NULL);
595 fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r));
599 r = find_dpfp_device();
601 fprintf(stderr, "Could not find/open device\n");
605 r = libusb_claim_interface(devh, 0);
607 fprintf(stderr, "claim interface error %d - %s\n", r, libusb_strerror(r));
610 printf("claimed interface\n");
620 /* async from here onwards */
623 r = alloc_transfers();
627 #if defined(DPFP_THREADED)
628 exit_semaphore = semaphore_create();
629 if (!exit_semaphore) {
630 fprintf(stderr, "failed to initialise semaphore\n");
634 r = thread_create(&poll_thread, poll_thread_main, NULL);
636 semaphore_destroy(exit_semaphore);
645 semaphore_take(exit_semaphore);
652 r = libusb_handle_events(NULL);
658 printf("shutting down...\n");
660 #if defined(DPFP_THREADED)
661 thread_join(poll_thread);
662 semaphore_destroy(exit_semaphore);
666 r = libusb_cancel_transfer(img_transfer);
668 fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
672 r = libusb_cancel_transfer(irq_transfer);
674 fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
677 while (img_transfer || irq_transfer) {
678 if (libusb_handle_events(NULL) < 0)
689 libusb_free_transfer(img_transfer);
691 libusb_free_transfer(irq_transfer);
695 libusb_release_interface(devh, 0);
699 return r >= 0 ? r : -r;