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>
35 #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
36 #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
37 #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
38 #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
40 #define INTR_LENGTH 64
41 #define SEM_NAME "/org.libusb.example.dpfp_threaded"
45 MODE_AWAIT_FINGER_ON = 0x10,
46 MODE_AWAIT_FINGER_OFF = 0x12,
52 static int next_state(void);
55 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
56 STATE_AWAIT_IRQ_FINGER_DETECTED,
57 STATE_AWAIT_MODE_CHANGE_CAPTURE,
59 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
60 STATE_AWAIT_IRQ_FINGER_REMOVED,
64 static struct libusb_device_handle *devh = NULL;
65 static unsigned char imgbuf[0x1b340];
66 static unsigned char irqbuf[INTR_LENGTH];
67 static struct libusb_transfer *img_transfer = NULL;
68 static struct libusb_transfer *irq_transfer = NULL;
69 static int img_idx = 0;
70 static volatile sig_atomic_t do_exit = 0;
72 static pthread_t poll_thread;
73 static sem_t *exit_sem;
75 static void request_exit(sig_atomic_t code)
81 static void *poll_thread_main(void *arg)
84 printf("poll thread running\n");
87 struct timeval tv = { 1, 0 };
88 r = libusb_handle_events_timeout(NULL, &tv);
95 printf("poll thread shutting down\n");
99 static int find_dpfp_device(void)
101 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
102 return devh ? 0 : -EIO;
105 static int print_f0_data(void)
107 unsigned char data[0x10];
111 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
114 fprintf(stderr, "F0 error %d\n", r);
117 if ((unsigned int) r < sizeof(data)) {
118 fprintf(stderr, "short read (%d)\n", r);
123 for (i = 0; i < sizeof(data); i++)
124 printf("%02x ", data[i]);
129 static int get_hwstat(unsigned char *status)
133 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
135 fprintf(stderr, "read hwstat error %d\n", r);
138 if ((unsigned int) r < 1) {
139 fprintf(stderr, "short read (%d)\n", r);
143 printf("hwstat reads %02x\n", *status);
147 static int set_hwstat(unsigned char data)
151 printf("set hwstat to %02x\n", data);
152 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
154 fprintf(stderr, "set hwstat error %d\n", r);
157 if ((unsigned int) r < 1) {
158 fprintf(stderr, "short write (%d)", r);
165 static int set_mode(unsigned char data)
168 printf("set mode %02x\n", data);
170 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
172 fprintf(stderr, "set mode error %d\n", r);
175 if ((unsigned int) r < 1) {
176 fprintf(stderr, "short write (%d)", r);
183 static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
185 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
186 fprintf(stderr, "mode change transfer not completed!\n");
190 printf("async cb_mode_changed length=%d actual_length=%d\n",
191 transfer->length, transfer->actual_length);
192 if (next_state() < 0)
196 static int set_mode_async(unsigned char data)
198 unsigned char *buf = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
199 struct libusb_transfer *transfer;
204 transfer = libusb_alloc_transfer(0);
210 printf("async set mode %02x\n", data);
211 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
212 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
213 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
216 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
217 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
218 return libusb_submit_transfer(transfer);
221 static int do_sync_intr(unsigned char *data)
226 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
229 fprintf(stderr, "intr error %d\n", r);
232 if (transferred < INTR_LENGTH) {
233 fprintf(stderr, "short read (%d)\n", r);
237 printf("recv interrupt %04x\n", *((uint16_t *) data));
241 static int sync_intr(unsigned char type)
244 unsigned char data[INTR_LENGTH];
247 r = do_sync_intr(data);
255 static int save_to_file(unsigned char *data)
260 snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
261 fd = fopen(filename, "w");
265 fputs("P5 384 289 255 ", fd);
266 (void) fwrite(data + 64, 1, 384*289, fd);
268 printf("saved image to %s\n", filename);
272 static int next_state(void)
275 printf("old state: %d\n", state);
277 case STATE_AWAIT_IRQ_FINGER_REMOVED:
278 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
279 r = set_mode_async(MODE_AWAIT_FINGER_ON);
281 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
282 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
284 case STATE_AWAIT_IRQ_FINGER_DETECTED:
285 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
286 r = set_mode_async(MODE_CAPTURE);
288 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
289 state = STATE_AWAIT_IMAGE;
291 case STATE_AWAIT_IMAGE:
292 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
293 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
295 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
296 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
299 printf("unrecognised state %d\n", state);
302 fprintf(stderr, "error detected changing state\n");
306 printf("new state: %d\n", state);
310 static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
312 unsigned char irqtype = transfer->buffer[0];
314 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
315 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
321 printf("IRQ callback %02x\n", irqtype);
323 case STATE_AWAIT_IRQ_FINGER_DETECTED:
324 if (irqtype == 0x01) {
325 if (next_state() < 0) {
330 printf("finger-on-sensor detected in wrong state!\n");
333 case STATE_AWAIT_IRQ_FINGER_REMOVED:
334 if (irqtype == 0x02) {
335 if (next_state() < 0) {
340 printf("finger-on-sensor detected in wrong state!\n");
344 if (libusb_submit_transfer(irq_transfer) < 0)
348 static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
350 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
351 fprintf(stderr, "img transfer status %d?\n", transfer->status);
357 printf("Image callback\n");
358 save_to_file(imgbuf);
359 if (next_state() < 0) {
363 if (libusb_submit_transfer(img_transfer) < 0)
367 static int init_capture(void)
371 r = libusb_submit_transfer(irq_transfer);
375 r = libusb_submit_transfer(img_transfer);
377 libusb_cancel_transfer(irq_transfer);
379 if (libusb_handle_events(NULL) < 0)
384 /* start state machine */
385 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
389 static int do_init(void)
391 unsigned char status;
394 r = get_hwstat(&status);
398 if (!(status & 0x80)) {
399 r = set_hwstat(status | 0x80);
402 r = get_hwstat(&status);
408 r = set_hwstat(status);
412 r = get_hwstat(&status);
423 static int alloc_transfers(void)
425 img_transfer = libusb_alloc_transfer(0);
429 irq_transfer = libusb_alloc_transfer(0);
433 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
434 sizeof(imgbuf), cb_img, NULL, 0);
435 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
436 sizeof(irqbuf), cb_irq, NULL, 0);
441 static void sighandler(int signum)
448 struct sigaction sigact;
451 exit_sem = sem_open (SEM_NAME, O_CREAT, 0);
453 fprintf(stderr, "failed to initialise semaphore error %d", errno);
457 /* only using this semaphore in this process so go ahead and unlink it now */
458 sem_unlink (SEM_NAME);
460 r = libusb_init(NULL);
462 fprintf(stderr, "failed to initialise libusb\n");
466 r = find_dpfp_device();
468 fprintf(stderr, "Could not find/open device\n");
472 r = libusb_claim_interface(devh, 0);
474 fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
477 printf("claimed interface\n");
487 /* async from here onwards */
489 sigact.sa_handler = sighandler;
490 sigemptyset(&sigact.sa_mask);
492 sigaction(SIGINT, &sigact, NULL);
493 sigaction(SIGTERM, &sigact, NULL);
494 sigaction(SIGQUIT, &sigact, NULL);
496 r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL);
500 r = alloc_transfers();
503 pthread_join(poll_thread, NULL);
510 pthread_join(poll_thread, NULL);
517 printf("shutting down...\n");
518 pthread_join(poll_thread, NULL);
520 r = libusb_cancel_transfer(irq_transfer);
526 r = libusb_cancel_transfer(img_transfer);
532 while (img_transfer || irq_transfer)
533 if (libusb_handle_events(NULL) < 0)
542 libusb_free_transfer(img_transfer);
543 libusb_free_transfer(irq_transfer);
547 libusb_release_interface(devh, 0);
551 return r >= 0 ? r : -r;