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>
6 * Basic image capture program only, does not consider the powerup quirks or
7 * the fact that image encryption may be enabled. Not expected to work
8 * flawlessly all of the time.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <semaphore.h>
36 #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
37 #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
38 #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
39 #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
41 #define INTR_LENGTH 64
42 #define SEM_NAME "/org.libusb.example.dpfp_threaded"
46 MODE_AWAIT_FINGER_ON = 0x10,
47 MODE_AWAIT_FINGER_OFF = 0x12,
53 static int next_state(void);
56 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
57 STATE_AWAIT_IRQ_FINGER_DETECTED,
58 STATE_AWAIT_MODE_CHANGE_CAPTURE,
60 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
61 STATE_AWAIT_IRQ_FINGER_REMOVED,
65 static struct libusb_device_handle *devh = NULL;
66 static unsigned char imgbuf[0x1b340];
67 static unsigned char irqbuf[INTR_LENGTH];
68 static struct libusb_transfer *img_transfer = NULL;
69 static struct libusb_transfer *irq_transfer = NULL;
70 static int img_idx = 0;
71 static volatile sig_atomic_t do_exit = 0;
73 static pthread_t poll_thread;
74 static sem_t *exit_sem;
76 static void request_exit(sig_atomic_t code)
82 static void *poll_thread_main(void *arg)
85 printf("poll thread running\n");
88 struct timeval tv = { 1, 0 };
89 r = libusb_handle_events_timeout(NULL, &tv);
96 printf("poll thread shutting down\n");
100 static int find_dpfp_device(void)
102 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
103 return devh ? 0 : -EIO;
106 static int print_f0_data(void)
108 unsigned char data[0x10];
112 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
115 fprintf(stderr, "F0 error %d\n", r);
118 if ((unsigned int) r < sizeof(data)) {
119 fprintf(stderr, "short read (%d)\n", r);
124 for (i = 0; i < sizeof(data); i++)
125 printf("%02x ", data[i]);
130 static int get_hwstat(unsigned char *status)
134 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
136 fprintf(stderr, "read hwstat error %d\n", r);
139 if ((unsigned int) r < 1) {
140 fprintf(stderr, "short read (%d)\n", r);
144 printf("hwstat reads %02x\n", *status);
148 static int set_hwstat(unsigned char data)
152 printf("set hwstat to %02x\n", data);
153 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
155 fprintf(stderr, "set hwstat error %d\n", r);
158 if ((unsigned int) r < 1) {
159 fprintf(stderr, "short write (%d)", r);
166 static int set_mode(unsigned char data)
169 printf("set mode %02x\n", data);
171 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
173 fprintf(stderr, "set mode error %d\n", r);
176 if ((unsigned int) r < 1) {
177 fprintf(stderr, "short write (%d)", r);
184 static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
186 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
187 fprintf(stderr, "mode change transfer not completed!\n");
191 printf("async cb_mode_changed length=%d actual_length=%d\n",
192 transfer->length, transfer->actual_length);
193 if (next_state() < 0)
197 static int set_mode_async(unsigned char data)
199 unsigned char *buf = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
200 struct libusb_transfer *transfer;
205 transfer = libusb_alloc_transfer(0);
211 printf("async set mode %02x\n", data);
212 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
213 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
214 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
217 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
218 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
219 return libusb_submit_transfer(transfer);
222 static int do_sync_intr(unsigned char *data)
227 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
230 fprintf(stderr, "intr error %d\n", r);
233 if (transferred < INTR_LENGTH) {
234 fprintf(stderr, "short read (%d)\n", r);
238 printf("recv interrupt %04x\n", *((uint16_t *) data));
242 static int sync_intr(unsigned char type)
245 unsigned char data[INTR_LENGTH];
248 r = do_sync_intr(data);
256 static int save_to_file(unsigned char *data)
261 snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
262 fd = fopen(filename, "w");
266 fputs("P5 384 289 255 ", fd);
267 (void) fwrite(data + 64, 1, 384*289, fd);
269 printf("saved image to %s\n", filename);
273 static int next_state(void)
276 printf("old state: %d\n", state);
278 case STATE_AWAIT_IRQ_FINGER_REMOVED:
279 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
280 r = set_mode_async(MODE_AWAIT_FINGER_ON);
282 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
283 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
285 case STATE_AWAIT_IRQ_FINGER_DETECTED:
286 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
287 r = set_mode_async(MODE_CAPTURE);
289 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
290 state = STATE_AWAIT_IMAGE;
292 case STATE_AWAIT_IMAGE:
293 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
294 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
296 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
297 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
300 printf("unrecognised state %d\n", state);
303 fprintf(stderr, "error detected changing state\n");
307 printf("new state: %d\n", state);
311 static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
313 unsigned char irqtype = transfer->buffer[0];
315 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
316 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
322 printf("IRQ callback %02x\n", irqtype);
324 case STATE_AWAIT_IRQ_FINGER_DETECTED:
325 if (irqtype == 0x01) {
326 if (next_state() < 0) {
331 printf("finger-on-sensor detected in wrong state!\n");
334 case STATE_AWAIT_IRQ_FINGER_REMOVED:
335 if (irqtype == 0x02) {
336 if (next_state() < 0) {
341 printf("finger-on-sensor detected in wrong state!\n");
345 if (libusb_submit_transfer(irq_transfer) < 0)
349 static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
351 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
352 fprintf(stderr, "img transfer status %d?\n", transfer->status);
358 printf("Image callback\n");
359 save_to_file(imgbuf);
360 if (next_state() < 0) {
364 if (libusb_submit_transfer(img_transfer) < 0)
368 static int init_capture(void)
372 r = libusb_submit_transfer(irq_transfer);
376 r = libusb_submit_transfer(img_transfer);
378 libusb_cancel_transfer(irq_transfer);
380 if (libusb_handle_events(NULL) < 0)
385 /* start state machine */
386 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
390 static int do_init(void)
392 unsigned char status;
395 r = get_hwstat(&status);
399 if (!(status & 0x80)) {
400 r = set_hwstat(status | 0x80);
403 r = get_hwstat(&status);
409 r = set_hwstat(status);
413 r = get_hwstat(&status);
424 static int alloc_transfers(void)
426 img_transfer = libusb_alloc_transfer(0);
430 irq_transfer = libusb_alloc_transfer(0);
434 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
435 sizeof(imgbuf), cb_img, NULL, 0);
436 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
437 sizeof(irqbuf), cb_irq, NULL, 0);
442 static void sighandler(int signum)
449 struct sigaction sigact;
452 exit_sem = sem_open (SEM_NAME, O_CREAT, 0);
454 fprintf(stderr, "failed to initialise semaphore error %d", errno);
458 /* only using this semaphore in this process so go ahead and unlink it now */
459 sem_unlink (SEM_NAME);
461 r = libusb_init(NULL);
463 fprintf(stderr, "failed to initialise libusb\n");
467 r = find_dpfp_device();
469 fprintf(stderr, "Could not find/open device\n");
473 r = libusb_claim_interface(devh, 0);
475 fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
478 printf("claimed interface\n");
488 /* async from here onwards */
490 sigact.sa_handler = sighandler;
491 sigemptyset(&sigact.sa_mask);
493 sigaction(SIGINT, &sigact, NULL);
494 sigaction(SIGTERM, &sigact, NULL);
495 sigaction(SIGQUIT, &sigact, NULL);
497 r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL);
501 r = alloc_transfers();
504 pthread_join(poll_thread, NULL);
511 pthread_join(poll_thread, NULL);
518 printf("shutting down...\n");
519 pthread_join(poll_thread, NULL);
521 r = libusb_cancel_transfer(irq_transfer);
527 r = libusb_cancel_transfer(img_transfer);
533 while (img_transfer || irq_transfer)
534 if (libusb_handle_events(NULL) < 0)
543 libusb_free_transfer(img_transfer);
544 libusb_free_transfer(irq_transfer);
548 libusb_release_interface(devh, 0);
552 return r >= 0 ? r : -r;