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);
227 static int print_f0_data(void)
229 unsigned char data[0x10];
233 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
236 fprintf(stderr, "F0 error %d\n", r);
239 if (r < (int)sizeof(data)) {
240 fprintf(stderr, "short read (%d)\n", r);
245 for (i = 0; i < sizeof(data); i++)
246 printf(" %02x", data[i]);
251 static int get_hwstat(unsigned char *status)
255 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
257 fprintf(stderr, "read hwstat error %d\n", r);
261 fprintf(stderr, "short read (%d)\n", r);
265 printf("hwstat reads %02x\n", *status);
269 static int set_hwstat(unsigned char data)
273 printf("set hwstat to %02x\n", data);
274 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
276 fprintf(stderr, "set hwstat error %d\n", r);
280 fprintf(stderr, "short write (%d)\n", r);
287 static int set_mode(unsigned char data)
291 printf("set mode %02x\n", data);
292 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
294 fprintf(stderr, "set mode error %d\n", r);
298 fprintf(stderr, "short write (%d)\n", r);
305 static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
307 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
308 fprintf(stderr, "mode change transfer not completed!\n");
312 printf("async cb_mode_changed length=%d actual_length=%d\n",
313 transfer->length, transfer->actual_length);
314 if (next_state() < 0)
318 static int set_mode_async(unsigned char data)
320 unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
321 struct libusb_transfer *transfer;
328 transfer = libusb_alloc_transfer(0);
335 printf("async set mode %02x\n", data);
336 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
337 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
338 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
341 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
342 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
343 return libusb_submit_transfer(transfer);
346 static int do_sync_intr(unsigned char *data)
351 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
354 fprintf(stderr, "intr error %d\n", r);
357 if (transferred < INTR_LENGTH) {
358 fprintf(stderr, "short read (%d)\n", r);
362 printf("recv interrupt %04x\n", *((uint16_t *)data));
366 static int sync_intr(unsigned char type)
369 unsigned char data[INTR_LENGTH];
372 r = do_sync_intr(data);
380 static int save_to_file(unsigned char *data)
385 snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
386 f = fopen(filename, "w");
390 fputs("P5 384 289 255 ", f);
391 (void)fwrite(data + 64, 1, 384*289, f);
393 printf("saved image to %s\n", filename);
397 static int next_state(void)
401 printf("old state: %d\n", state);
403 case STATE_AWAIT_IRQ_FINGER_REMOVED:
404 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
405 r = set_mode_async(MODE_AWAIT_FINGER_ON);
407 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
408 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
410 case STATE_AWAIT_IRQ_FINGER_DETECTED:
411 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
412 r = set_mode_async(MODE_CAPTURE);
414 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
415 state = STATE_AWAIT_IMAGE;
417 case STATE_AWAIT_IMAGE:
418 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
419 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
421 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
422 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
425 printf("unrecognised state %d\n", state);
428 fprintf(stderr, "error detected changing state\n");
432 printf("new state: %d\n", state);
436 static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
438 unsigned char irqtype = transfer->buffer[0];
440 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
441 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
442 goto err_free_transfer;
445 printf("IRQ callback %02x\n", irqtype);
447 case STATE_AWAIT_IRQ_FINGER_DETECTED:
448 if (irqtype == 0x01) {
449 if (next_state() < 0)
450 goto err_free_transfer;
452 printf("finger-on-sensor detected in wrong state!\n");
455 case STATE_AWAIT_IRQ_FINGER_REMOVED:
456 if (irqtype == 0x02) {
457 if (next_state() < 0)
458 goto err_free_transfer;
460 printf("finger-on-sensor detected in wrong state!\n");
464 if (libusb_submit_transfer(irq_transfer) < 0)
465 goto err_free_transfer;
470 libusb_free_transfer(transfer);
475 static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
477 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
478 fprintf(stderr, "img transfer status %d?\n", transfer->status);
479 goto err_free_transfer;
482 printf("Image callback\n");
483 save_to_file(imgbuf);
484 if (next_state() < 0)
485 goto err_free_transfer;
487 if (libusb_submit_transfer(img_transfer) < 0)
488 goto err_free_transfer;
493 libusb_free_transfer(transfer);
498 static int init_capture(void)
502 r = libusb_submit_transfer(irq_transfer);
506 r = libusb_submit_transfer(img_transfer);
508 libusb_cancel_transfer(irq_transfer);
510 if (libusb_handle_events(NULL) < 0)
515 /* start state machine */
516 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
520 static int do_init(void)
522 unsigned char status;
525 r = get_hwstat(&status);
529 if (!(status & 0x80)) {
530 r = set_hwstat(status | 0x80);
533 r = get_hwstat(&status);
539 r = set_hwstat(status);
543 r = get_hwstat(&status);
554 static int alloc_transfers(void)
556 img_transfer = libusb_alloc_transfer(0);
562 irq_transfer = libusb_alloc_transfer(0);
568 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
569 sizeof(imgbuf), cb_img, NULL, 0);
570 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
571 sizeof(irqbuf), cb_irq, NULL, 0);
576 static void sighandler(int signum)
583 static void setup_signals(void)
585 #if defined(PLATFORM_POSIX)
586 struct sigaction sigact;
588 sigact.sa_handler = sighandler;
589 sigemptyset(&sigact.sa_mask);
591 (void)sigaction(SIGINT, &sigact, NULL);
592 (void)sigaction(SIGTERM, &sigact, NULL);
593 (void)sigaction(SIGQUIT, &sigact, NULL);
595 (void)signal(SIGINT, sighandler);
596 (void)signal(SIGTERM, sighandler);
604 r = libusb_init(NULL);
606 fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r));
610 r = find_dpfp_device();
612 fprintf(stderr, "Could not find/open device\n");
616 r = libusb_claim_interface(devh, 0);
618 fprintf(stderr, "claim interface error %d - %s\n", r, libusb_strerror(r));
621 printf("claimed interface\n");
631 /* async from here onwards */
634 r = alloc_transfers();
638 #if defined(DPFP_THREADED)
639 exit_semaphore = semaphore_create();
640 if (!exit_semaphore) {
641 fprintf(stderr, "failed to initialise semaphore\n");
645 r = thread_create(&poll_thread, poll_thread_main, NULL);
647 semaphore_destroy(exit_semaphore);
656 semaphore_take(exit_semaphore);
663 r = libusb_handle_events(NULL);
669 printf("shutting down...\n");
671 #if defined(DPFP_THREADED)
672 thread_join(poll_thread);
673 semaphore_destroy(exit_semaphore);
677 r = libusb_cancel_transfer(img_transfer);
679 fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
683 r = libusb_cancel_transfer(irq_transfer);
685 fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
688 while (img_transfer || irq_transfer) {
689 if (libusb_handle_events(NULL) < 0)
700 libusb_free_transfer(img_transfer);
702 libusb_free_transfer(irq_transfer);
706 libusb_release_interface(devh, 0);
710 return r >= 0 ? r : -r;