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");
90 struct timeval tv = { 1, 0 };
91 r = libusb_handle_events_timeout(NULL, &tv);
98 printf("poll thread shutting down\n");
102 static int find_dpfp_device(void)
104 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
105 return devh ? 0 : -EIO;
108 static int print_f0_data(void)
110 unsigned char data[0x10];
114 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
117 fprintf(stderr, "F0 error %d\n", r);
120 if ((unsigned int) r < sizeof(data)) {
121 fprintf(stderr, "short read (%d)\n", r);
126 for (i = 0; i < sizeof(data); i++)
127 printf("%02x ", data[i]);
132 static int get_hwstat(unsigned char *status)
136 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
138 fprintf(stderr, "read hwstat error %d\n", r);
141 if ((unsigned int) r < 1) {
142 fprintf(stderr, "short read (%d)\n", r);
146 printf("hwstat reads %02x\n", *status);
150 static int set_hwstat(unsigned char data)
154 printf("set hwstat to %02x\n", data);
155 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
157 fprintf(stderr, "set hwstat error %d\n", r);
160 if ((unsigned int) r < 1) {
161 fprintf(stderr, "short write (%d)", r);
168 static int set_mode(unsigned char data)
171 printf("set mode %02x\n", data);
173 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
175 fprintf(stderr, "set mode error %d\n", r);
178 if ((unsigned int) r < 1) {
179 fprintf(stderr, "short write (%d)", r);
186 static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
188 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
189 fprintf(stderr, "mode change transfer not completed!\n");
193 printf("async cb_mode_changed length=%d actual_length=%d\n",
194 transfer->length, transfer->actual_length);
195 if (next_state() < 0)
199 static int set_mode_async(unsigned char data)
201 unsigned char *buf = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
202 struct libusb_transfer *transfer;
207 transfer = libusb_alloc_transfer(0);
213 printf("async set mode %02x\n", data);
214 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
215 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
216 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
219 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
220 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
221 return libusb_submit_transfer(transfer);
224 static int do_sync_intr(unsigned char *data)
229 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
232 fprintf(stderr, "intr error %d\n", r);
235 if (transferred < INTR_LENGTH) {
236 fprintf(stderr, "short read (%d)\n", r);
240 printf("recv interrupt %04x\n", *((uint16_t *) data));
244 static int sync_intr(unsigned char type)
247 unsigned char data[INTR_LENGTH];
250 r = do_sync_intr(data);
258 static int save_to_file(unsigned char *data)
263 snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
264 fd = fopen(filename, "w");
268 fputs("P5 384 289 255 ", fd);
269 (void) fwrite(data + 64, 1, 384*289, fd);
271 printf("saved image to %s\n", filename);
275 static int next_state(void)
278 printf("old state: %d\n", state);
280 case STATE_AWAIT_IRQ_FINGER_REMOVED:
281 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
282 r = set_mode_async(MODE_AWAIT_FINGER_ON);
284 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
285 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
287 case STATE_AWAIT_IRQ_FINGER_DETECTED:
288 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
289 r = set_mode_async(MODE_CAPTURE);
291 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
292 state = STATE_AWAIT_IMAGE;
294 case STATE_AWAIT_IMAGE:
295 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
296 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
298 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
299 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
302 printf("unrecognised state %d\n", state);
305 fprintf(stderr, "error detected changing state\n");
309 printf("new state: %d\n", state);
313 static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
315 unsigned char irqtype = transfer->buffer[0];
317 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
318 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
324 printf("IRQ callback %02x\n", irqtype);
326 case STATE_AWAIT_IRQ_FINGER_DETECTED:
327 if (irqtype == 0x01) {
328 if (next_state() < 0) {
333 printf("finger-on-sensor detected in wrong state!\n");
336 case STATE_AWAIT_IRQ_FINGER_REMOVED:
337 if (irqtype == 0x02) {
338 if (next_state() < 0) {
343 printf("finger-on-sensor detected in wrong state!\n");
347 if (libusb_submit_transfer(irq_transfer) < 0)
351 static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
353 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
354 fprintf(stderr, "img transfer status %d?\n", transfer->status);
360 printf("Image callback\n");
361 save_to_file(imgbuf);
362 if (next_state() < 0) {
366 if (libusb_submit_transfer(img_transfer) < 0)
370 static int init_capture(void)
374 r = libusb_submit_transfer(irq_transfer);
378 r = libusb_submit_transfer(img_transfer);
380 libusb_cancel_transfer(irq_transfer);
382 if (libusb_handle_events(NULL) < 0)
387 /* start state machine */
388 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
392 static int do_init(void)
394 unsigned char status;
397 r = get_hwstat(&status);
401 if (!(status & 0x80)) {
402 r = set_hwstat(status | 0x80);
405 r = get_hwstat(&status);
411 r = set_hwstat(status);
415 r = get_hwstat(&status);
426 static int alloc_transfers(void)
428 img_transfer = libusb_alloc_transfer(0);
432 irq_transfer = libusb_alloc_transfer(0);
436 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
437 sizeof(imgbuf), cb_img, NULL, 0);
438 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
439 sizeof(irqbuf), cb_irq, NULL, 0);
444 static void sighandler(int signum)
453 struct sigaction sigact;
456 exit_sem = sem_open (SEM_NAME, O_CREAT, 0);
458 fprintf(stderr, "failed to initialise semaphore error %d", errno);
462 /* only using this semaphore in this process so go ahead and unlink it now */
463 sem_unlink (SEM_NAME);
465 r = libusb_init(NULL);
467 fprintf(stderr, "failed to initialise libusb\n");
471 r = find_dpfp_device();
473 fprintf(stderr, "Could not find/open device\n");
477 r = libusb_claim_interface(devh, 0);
479 fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
482 printf("claimed interface\n");
492 /* async from here onwards */
494 sigact.sa_handler = sighandler;
495 sigemptyset(&sigact.sa_mask);
497 sigaction(SIGINT, &sigact, NULL);
498 sigaction(SIGTERM, &sigact, NULL);
499 sigaction(SIGQUIT, &sigact, NULL);
501 r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL);
505 r = alloc_transfers();
508 pthread_join(poll_thread, NULL);
515 pthread_join(poll_thread, NULL);
522 printf("shutting down...\n");
523 pthread_join(poll_thread, NULL);
525 r = libusb_cancel_transfer(irq_transfer);
531 r = libusb_cancel_transfer(img_transfer);
537 while (img_transfer || irq_transfer)
538 if (libusb_handle_events(NULL) < 0)
547 libusb_free_transfer(img_transfer);
548 libusb_free_transfer(irq_transfer);
552 libusb_release_interface(devh, 0);
556 return r >= 0 ? r : -r;